Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Top-of-file Comments get duplicated when moving runtime code #87 #88

Merged
merged 3 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";