Skip to content

Commit

Permalink
Fix: Top-of-file Comments get duplicated when moving runtime code #87 (
Browse files Browse the repository at this point in the history
…#88)

Fixes #87

---------

Co-authored-by: Ian VanSchooten <ian.vanschooten@gmail.com>
  • Loading branch information
fbartho and IanVS authored May 17, 2023
1 parent 3c02cfa commit b74ccc3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/utils/get-all-comments-from-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import type {
CommentBlock,
CommentLine,
Directive,
InterpreterDirective,
Statement,
} from '@babel/types';

import { SomeSpecifier } from '../types';

export const getAllCommentsFromNodes = (
nodes: readonly (Directive | Statement | SomeSpecifier)[],
nodes: readonly (
| Directive
| Statement
| SomeSpecifier
| InterpreterDirective
)[],
) =>
nodes.reduce((acc, node) => {
if (
Expand Down
4 changes: 4 additions & 0 deletions src/utils/get-code-from-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ export const getCodeFromAst = ({
}) => {
const allCommentsFromImports = getAllCommentsFromNodes(nodesToOutput);
const allCommentsFromDirectives = getAllCommentsFromNodes(directives);
const allCommentsFromIntrepter = interpreter
? getAllCommentsFromNodes([interpreter])
: [];

const nodesToRemoveFromCode = [
...nodesToOutput,
...allOriginalImportNodes,
...allCommentsFromImports,
...allCommentsFromDirectives,
...allCommentsFromIntrepter,
...(interpreter ? [interpreter] : []),
...directives,
];
Expand Down
26 changes: 26 additions & 0 deletions tests/ImportCommentsNoDuplicate/__snapshots__/ppsi.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`trailing-comments-on-interpreter.ts - typescript-verify > trailing-comments-on-interpreter.ts 1`] = `
#!/usr/bin/env ts-node
// ################################################
// ## Highlander comment: There can only be one! ##
// ################################################
const someRandomGlobal = "stuff";
import * as fs from "fs";
import * as path from "path";
import shell from "shelljs";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/usr/bin/env ts-node
// ################################################
// ## Highlander comment: There can only be one! ##
// ################################################
import * as fs from "fs";
import * as path from "path";
import shell from "shelljs";
const someRandomGlobal = "stuff";
`;
5 changes: 5 additions & 0 deletions tests/ImportCommentsNoDuplicate/ppsi.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {run_spec} from '../../test-setup/run_spec';

run_spec(__dirname, ["typescript"], {
importOrderParserPlugins : ['typescript']
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env ts-node
// ################################################
// ## Highlander comment: There can only be one! ##
// ################################################

const someRandomGlobal = "stuff";

import * as fs from "fs";
import * as path from "path";
import shell from "shelljs";

0 comments on commit b74ccc3

Please sign in to comment.