Skip to content

fix: keep header comments before sorted import declarations - #1151

Open
RainyMarks wants to merge 1 commit into
jhipster:mainfrom
RainyMarks:fix/372-import-header-comments
Open

RainyMarks wants to merge 1 commit into
jhipster:mainfrom
RainyMarks:fix/372-import-header-comments

Conversation

@RainyMarks

Copy link
Copy Markdown

What changed with this PR:

When a file has a license header, section comment, or @format / @prettier pragma before the first import declaration, Prettier attaches those leading comments to the first ImportDeclaration in source order (since Program does not attach comments). Previously, sorting imports could move that first ImportDeclaration after another import or into a later import group, dragging the header comment into the middle of the import list (and breaking --require-pragma detection when no package declaration is present).

This PR:

  • Implements willPrintOwnComments for the first ImportDeclaration in a Program (isFirstImportDeclaration in src/comments.ts, wired in src/printer.ts).
  • Splits leading comments on the first ImportDeclaration via printImportComments so own-line header comments before any prettier-ignore directive stay above the sorted import groups, while prettier-ignore comments, same-line leading block comments, and trailing comments remain attached to that import declaration.
  • Adds unit tests in test/unit-test/package_and_imports/ covering license headers after package, file headers and @format pragmas without package, multi-group import sorting (import module / import static / import), prettier-ignore, inline leading block comments, trailing comments, and module-info declarations.

Example

Input

package org.mypackage;

/*-
 * License header
 */

import java.lang.Util;
import java.util.Map;
import com.me.Util;

public class Prettier {}

Output

package org.mypackage;

/*-
 * License header
 */

import com.me.Util;
import java.lang.Util;
import java.util.Map;

public class Prettier {}

Relative issues or prs:

Closes #372

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Comment section moved between two import declarations

1 participant