Skip to content

Commit

Permalink
Prevent comment gap when re-arranging first import (#85)
Browse files Browse the repository at this point in the history
This should address
#54 (comment).

Note, we don't currently have a way to _force_ or _remove_ a line at the
top, under top-of-file comments, but I think this is okay. We preserve
whatever the user had before, which seems fine. We can re-evaluate in
the future if we hear from users.
  • Loading branch information
IanVS committed May 16, 2023
1 parent 29cbbfe commit 4cd5b1d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils/get-comment-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ export function attachCommentsToOutputNodes(
* This works since late 2022, Babel uses `loc` (if-present) to hint how to render for some cases.
*/
const patchNewFirstImportLocationOnlyOnce = () => {
if (!hasPatchedNewFirstImportLocation) {
if (hasPatchedNewFirstImportLocation) {
return;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/ImportCommentsPreserved/__snapshots__/ppsi.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ import { useState } from "react";
`;

exports[`import-comments-no-top-gap.ts - typescript-verify > import-comments-no-top-gap.ts 1`] = `
/**
* @prettier
*/
import { requireValue } from './utils/require/requireValue';
import { fooValue } from './foo';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* @prettier
*/
import { fooValue } from "./foo";
import { requireValue } from "./utils/require/requireValue";
`;

exports[`import-comments-outer-comments-block-comments.ts - typescript-verify > import-comments-outer-comments-block-comments.ts 1`] = `
// Loose leading comment before imports (should not be dragged down with B)
Expand Down
5 changes: 5 additions & 0 deletions tests/ImportCommentsPreserved/import-comments-no-top-gap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* @prettier
*/
import { requireValue } from './utils/require/requireValue';
import { fooValue } from './foo';

0 comments on commit 4cd5b1d

Please sign in to comment.