Skip to content

Commit

Permalink
Preserve trailing comments on specifiers (#80)
Browse files Browse the repository at this point in the history
Fixes #79

Trailing comments on specifiers don't get automatically saved like
top-level ones do. This ensures that such comments are not lost

Before:
```ts
import {
    b2,a2,
    // @ts-expect-error
} from "b";

import {
    b1,
    a1,

    // @ts-expect-error
} from "a";
```
After
```ts
import {
    a1,
    b1,
    // @ts-expect-error
} from "a";
import {
    a2,
    b2,
    // @ts-expect-error
} from "b";
```

---------

Co-authored-by: Ian VanSchooten <ian.vanschooten@gmail.com>
  • Loading branch information
fbartho and IanVS committed May 15, 2023
1 parent 1e2ff94 commit dbf31ee
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/utils/get-comment-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,14 @@ export function attachCommentsToOutputNodes(
];

ownerNode = lastSpecifier;

// Start the comment on the line below the owner, to avoid gaps
if (
comment.loc?.start.line !== undefined &&
ownerNode.loc?.end.line
) {
comment.loc.start.line = ownerNode.loc?.end.line + 1;
}
}

if (!ownerNode) {
Expand Down
12 changes: 11 additions & 1 deletion tests/ImportCommentsPreserved/__snapshots__/ppsi.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ import {
/* @ts-expect-error*/
} from "c";
import {
b4,
a4,
// @ts-expect-error
} from "c2";
import {
b2,a2,
// @ts-expect-error
Expand All @@ -256,7 +262,6 @@ import {
import {
a1,
b1,
// @ts-expect-error
} from "a";
import {
Expand All @@ -265,5 +270,10 @@ import {
// @ts-expect-error
} from "b";
import { a3, b3 } from /* @ts-expect-error*/ "c";
import {
a4,
b4,
// @ts-expect-error
} from "c2";
`;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import {
/* @ts-expect-error*/
} from "c";

import {
b4,
a4,
// @ts-expect-error
} from "c2";

import {
b2,a2,
// @ts-expect-error
Expand Down

0 comments on commit dbf31ee

Please sign in to comment.