From 9131505ee1c9b82497a42927cd71ef94f2c3a515 Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Sun, 13 Nov 2022 10:21:20 -0800 Subject: [PATCH 1/8] keep script directives at top of file, fixes #185 --- src/preprocessors/preprocessor.ts | 14 ++++++++++++-- src/utils/get-code-from-ast.ts | 6 ++++-- src/utils/remove-nodes-from-original-code.ts | 2 ++ .../__snapshots__/ppsi.spec.js.snap | 13 +++++++++++++ .../imports-with-use-directive.ts | 6 ++++++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 tests/ImportsNotSeparated/imports-with-use-directive.ts diff --git a/src/preprocessors/preprocessor.ts b/src/preprocessors/preprocessor.ts index db559ff..abd37d9 100644 --- a/src/preprocessors/preprocessor.ts +++ b/src/preprocessors/preprocessor.ts @@ -1,6 +1,6 @@ import { ParserOptions, parse as babelParser } from '@babel/parser'; import traverse, { NodePath } from '@babel/traverse'; -import { ImportDeclaration, isTSModuleDeclaration } from '@babel/types'; +import { Directive, ImportDeclaration, isTSModuleDeclaration } from '@babel/types'; import { PrettierOptions } from '../types'; import { getCodeFromAst } from '../utils/get-code-from-ast'; @@ -26,6 +26,16 @@ export function preprocessor(code: string, options: PrettierOptions) { const ast = babelParser(code, parserOptions); const interpreter = ast.program.interpreter; + const directives: Directive[] = []; + traverse(ast, { + Directive({ node }) { + directives.push(node); + + // Trailing comments probably shouldn't be attached to the directive + delete node.trailingComments; + }, + }); + traverse(ast, { ImportDeclaration(path: NodePath) { const tsModuleParent = path.findParent((p) => @@ -48,5 +58,5 @@ export function preprocessor(code: string, options: PrettierOptions) { importOrderSortSpecifiers, }); - return getCodeFromAst(allImports, code, interpreter); + return getCodeFromAst(allImports, directives, code, interpreter); } diff --git a/src/utils/get-code-from-ast.ts b/src/utils/get-code-from-ast.ts index e3e96a6..2856266 100644 --- a/src/utils/get-code-from-ast.ts +++ b/src/utils/get-code-from-ast.ts @@ -1,5 +1,5 @@ import generate from '@babel/generator'; -import { InterpreterDirective, Statement, file } from '@babel/types'; +import { Directive, InterpreterDirective, Statement, file } from '@babel/types'; import { newLineCharacters } from '../constants'; import { getAllCommentsFromNodes } from './get-all-comments-from-nodes'; @@ -12,12 +12,14 @@ import { removeNodesFromOriginalCode } from './remove-nodes-from-original-code'; */ export const getCodeFromAst = ( nodes: Statement[], + directives: Directive[], originalCode: string, interpreter?: InterpreterDirective | null, ) => { const allCommentsFromImports = getAllCommentsFromNodes(nodes); const nodesToRemoveFromCode = [ + ...directives, ...nodes, ...allCommentsFromImports, ...(interpreter ? [interpreter] : []), @@ -31,7 +33,7 @@ export const getCodeFromAst = ( const newAST = file({ type: 'Program', body: nodes, - directives: [], + directives, sourceType: 'module', interpreter: interpreter, sourceFile: '', diff --git a/src/utils/remove-nodes-from-original-code.ts b/src/utils/remove-nodes-from-original-code.ts index ba1a8d0..a2c2a35 100644 --- a/src/utils/remove-nodes-from-original-code.ts +++ b/src/utils/remove-nodes-from-original-code.ts @@ -1,6 +1,7 @@ import { CommentBlock, CommentLine, + Directive, ImportDeclaration, InterpreterDirective, Statement, @@ -21,6 +22,7 @@ export const removeNodesFromOriginalCode = ( nodes: ( | Statement | CommentBlock + | Directive | CommentLine | ImportDeclaration | InterpreterDirective diff --git a/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap b/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap index be225c5..018e031 100644 --- a/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap +++ b/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap @@ -255,6 +255,19 @@ function add(a: number, b: number) { `; +exports[`imports-with-use-directive.ts - typescript-verify: imports-with-use-directive.ts 1`] = ` +'use strict'; +'use client'; + +// comments after directives +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +"use strict"; +"use client"; + +// comments after directives + +`; + exports[`imports-without-third-party.ts - typescript-verify: imports-without-third-party.ts 1`] = ` // I am top level comment import otherthing from "@core/otherthing"; diff --git a/tests/ImportsNotSeparated/imports-with-use-directive.ts b/tests/ImportsNotSeparated/imports-with-use-directive.ts new file mode 100644 index 0000000..37e651a --- /dev/null +++ b/tests/ImportsNotSeparated/imports-with-use-directive.ts @@ -0,0 +1,6 @@ +'use strict'; +'use client'; + +// comments after directives +import otherthing from "@core/otherthing"; +import abc from "@core/abc"; From 90054386183004579c465462366ad5d5936fc8da Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Sun, 13 Nov 2022 10:32:37 -0800 Subject: [PATCH 2/8] fix failing test --- src/utils/__tests__/get-code-from-ast.spec.ts | 2 +- tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/utils/__tests__/get-code-from-ast.spec.ts b/src/utils/__tests__/get-code-from-ast.spec.ts index 23009ce..a684a9c 100644 --- a/src/utils/__tests__/get-code-from-ast.spec.ts +++ b/src/utils/__tests__/get-code-from-ast.spec.ts @@ -22,7 +22,7 @@ import a from 'a'; importOrderGroupNamespaceSpecifiers: false, importOrderSortSpecifiers: false, }); - const formatted = getCodeFromAst(sortedNodes, code, null); + const formatted = getCodeFromAst(sortedNodes, [], code, null); expect(format(formatted, { parser: 'babel' })).toEqual( `// first comment // second comment diff --git a/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap b/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap index 018e031..4e9f010 100644 --- a/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap +++ b/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap @@ -260,11 +260,15 @@ exports[`imports-with-use-directive.ts - typescript-verify: imports-with-use-dir 'use client'; // comments after directives +import otherthing from "@core/otherthing"; +import abc from "@core/abc"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "use strict"; "use client"; // comments after directives +import abc from "@core/abc"; +import otherthing from "@core/otherthing"; `; From 39d08251641fad09a13c1d55d9c8cad008d4ff07 Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Sun, 13 Nov 2022 10:35:10 -0800 Subject: [PATCH 3/8] fix delete operand warning --- src/preprocessors/preprocessor.ts | 2 +- .../__snapshots__/ppsi.spec.js.snap | 34 +++++++++---------- .../imports-with-use-directive.ts | 6 ---- 3 files changed, 18 insertions(+), 24 deletions(-) delete mode 100644 tests/ImportsNotSeparated/imports-with-use-directive.ts diff --git a/src/preprocessors/preprocessor.ts b/src/preprocessors/preprocessor.ts index abd37d9..7443047 100644 --- a/src/preprocessors/preprocessor.ts +++ b/src/preprocessors/preprocessor.ts @@ -32,7 +32,7 @@ export function preprocessor(code: string, options: PrettierOptions) { directives.push(node); // Trailing comments probably shouldn't be attached to the directive - delete node.trailingComments; + node.trailingComments = null; }, }); diff --git a/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap b/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap index 4e9f010..f378ff8 100644 --- a/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap +++ b/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap @@ -151,6 +151,23 @@ function add(a: number, b: number) { `; +exports[`imports-with-directives.ts - typescript-verify: imports-with-directives.ts 1`] = ` +'use strict'; +'use client'; + +// comments after directives +import otherthing from "@core/otherthing"; +import abc from "@core/abc"; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +"use strict"; +"use client"; + +// comments after directives +import abc from "@core/abc"; +import otherthing from "@core/otherthing"; + +`; + exports[`imports-with-file-level-comments.ts - typescript-verify: imports-with-file-level-comments.ts 1`] = ` //@ts-ignore // I am file top level comments @@ -255,23 +272,6 @@ function add(a: number, b: number) { `; -exports[`imports-with-use-directive.ts - typescript-verify: imports-with-use-directive.ts 1`] = ` -'use strict'; -'use client'; - -// comments after directives -import otherthing from "@core/otherthing"; -import abc from "@core/abc"; -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -"use strict"; -"use client"; - -// comments after directives -import abc from "@core/abc"; -import otherthing from "@core/otherthing"; - -`; - exports[`imports-without-third-party.ts - typescript-verify: imports-without-third-party.ts 1`] = ` // I am top level comment import otherthing from "@core/otherthing"; diff --git a/tests/ImportsNotSeparated/imports-with-use-directive.ts b/tests/ImportsNotSeparated/imports-with-use-directive.ts deleted file mode 100644 index 37e651a..0000000 --- a/tests/ImportsNotSeparated/imports-with-use-directive.ts +++ /dev/null @@ -1,6 +0,0 @@ -'use strict'; -'use client'; - -// comments after directives -import otherthing from "@core/otherthing"; -import abc from "@core/abc"; From f3d887f188c1d7be6adee67c47c81c76452ced3c Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Sun, 13 Nov 2022 10:43:31 -0800 Subject: [PATCH 4/8] missed test file --- tests/ImportsNotSeparated/imports-with-directives.ts | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 tests/ImportsNotSeparated/imports-with-directives.ts diff --git a/tests/ImportsNotSeparated/imports-with-directives.ts b/tests/ImportsNotSeparated/imports-with-directives.ts new file mode 100644 index 0000000..37e651a --- /dev/null +++ b/tests/ImportsNotSeparated/imports-with-directives.ts @@ -0,0 +1,6 @@ +'use strict'; +'use client'; + +// comments after directives +import otherthing from "@core/otherthing"; +import abc from "@core/abc"; From 49fc99c77116d20d04da0caf481e2ca73cd9e0bf Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Fri, 18 Nov 2022 15:41:59 -0800 Subject: [PATCH 5/8] combine traverse() calls --- src/preprocessors/preprocessor.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/preprocessors/preprocessor.ts b/src/preprocessors/preprocessor.ts index 7443047..2a71ee5 100644 --- a/src/preprocessors/preprocessor.ts +++ b/src/preprocessors/preprocessor.ts @@ -1,4 +1,4 @@ -import { ParserOptions, parse as babelParser } from '@babel/parser'; +import { parse as babelParser, ParserOptions } from '@babel/parser'; import traverse, { NodePath } from '@babel/traverse'; import { Directive, ImportDeclaration, isTSModuleDeclaration } from '@babel/types'; @@ -34,9 +34,7 @@ export function preprocessor(code: string, options: PrettierOptions) { // Trailing comments probably shouldn't be attached to the directive node.trailingComments = null; }, - }); - traverse(ast, { ImportDeclaration(path: NodePath) { const tsModuleParent = path.findParent((p) => isTSModuleDeclaration(p), From daadfadbce4ec9c763f248686b307f5fd4da38bf Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Mon, 21 Nov 2022 10:57:40 -0800 Subject: [PATCH 6/8] add separated test, fixup both tests --- .../__snapshots__/ppsi.spec.js.snap | 12 ++++++-- .../imports-with-directives.ts | 6 ++-- .../__snapshots__/ppsi.spec.js.snap | 29 +++++++++++++++++++ .../imports-with-directives.ts | 12 ++++++++ 4 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 tests/ImportsSeparated/imports-with-directives.ts diff --git a/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap b/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap index f378ff8..46ab77d 100644 --- a/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap +++ b/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap @@ -154,18 +154,24 @@ function add(a: number, b: number) { exports[`imports-with-directives.ts - typescript-verify: imports-with-directives.ts 1`] = ` 'use strict'; 'use client'; - -// comments after directives import otherthing from "@core/otherthing"; import abc from "@core/abc"; +// Comment +function add(a:number,b:number) { + return a + b; +} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "use strict"; "use client"; -// comments after directives import abc from "@core/abc"; import otherthing from "@core/otherthing"; +// Comment +function add(a: number, b: number) { + return a + b; +} + `; exports[`imports-with-file-level-comments.ts - typescript-verify: imports-with-file-level-comments.ts 1`] = ` diff --git a/tests/ImportsNotSeparated/imports-with-directives.ts b/tests/ImportsNotSeparated/imports-with-directives.ts index 37e651a..4782ede 100644 --- a/tests/ImportsNotSeparated/imports-with-directives.ts +++ b/tests/ImportsNotSeparated/imports-with-directives.ts @@ -1,6 +1,8 @@ 'use strict'; 'use client'; - -// comments after directives import otherthing from "@core/otherthing"; import abc from "@core/abc"; +// Comment +function add(a:number,b:number) { + return a + b; +} diff --git a/tests/ImportsSeparated/__snapshots__/ppsi.spec.js.snap b/tests/ImportsSeparated/__snapshots__/ppsi.spec.js.snap index fbd4c8c..0e3a6ef 100644 --- a/tests/ImportsSeparated/__snapshots__/ppsi.spec.js.snap +++ b/tests/ImportsSeparated/__snapshots__/ppsi.spec.js.snap @@ -160,6 +160,35 @@ function add(a: number, b: number) { `; +exports[`imports-with-directives.ts - typescript-verify: imports-with-directives.ts 1`] = ` +'use strict'; +'use client'; + +// comment after directives +import otherthing from "@core/otherthing"; +import abc from "@core/abc"; + +// Comment + +function add(a:number,b:number) { + return a + b; +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +"use strict"; +"use client"; + +// comment after directives +import abc from "@core/abc"; +import otherthing from "@core/otherthing"; + +// Comment + +function add(a: number, b: number) { + return a + b; +} + +`; + exports[`imports-with-file-level-comments.ts - typescript-verify: imports-with-file-level-comments.ts 1`] = ` //@ts-ignore // I am file top level comments diff --git a/tests/ImportsSeparated/imports-with-directives.ts b/tests/ImportsSeparated/imports-with-directives.ts new file mode 100644 index 0000000..5188471 --- /dev/null +++ b/tests/ImportsSeparated/imports-with-directives.ts @@ -0,0 +1,12 @@ +'use strict'; +'use client'; + +// comment after directives +import otherthing from "@core/otherthing"; +import abc from "@core/abc"; + +// Comment + +function add(a:number,b:number) { + return a + b; +} From 52577f913d6e47da7de647f749c9b5011f5ee41d Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Mon, 21 Nov 2022 11:40:07 -0800 Subject: [PATCH 7/8] add getCodeFromAst() test w/ directives --- src/utils/__tests__/get-code-from-ast.spec.ts | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/utils/__tests__/get-code-from-ast.spec.ts b/src/utils/__tests__/get-code-from-ast.spec.ts index a684a9c..7b4a8e2 100644 --- a/src/utils/__tests__/get-code-from-ast.spec.ts +++ b/src/utils/__tests__/get-code-from-ast.spec.ts @@ -1,6 +1,15 @@ +import { parse as babelParser } from '@babel/core'; +import { ParserOptions } from '@babel/parser'; +import traverse, { NodePath } from '@babel/traverse'; +import { + Directive, + ImportDeclaration, + isTSModuleDeclaration, +} from '@babel/types'; import { format } from 'prettier'; import { getCodeFromAst } from '../get-code-from-ast'; +import { getExperimentalParserPlugins } from '../get-experimental-parser-plugins'; import { getImportNodes } from '../get-import-nodes'; import { getSortedNodes } from '../get-sorted-nodes'; @@ -35,3 +44,47 @@ import z from "z"; `, ); }); + +test('it renders directives correctly', () => { + const code = ` + "use client"; +// first comment +import b from 'b'; +import a from 'a';`; + + const parserOptions: ParserOptions = { + sourceType: 'module', + plugins: getExperimentalParserPlugins([]), + }; + const ast = babelParser(code, parserOptions); + const directives: Directive[] = []; + const importNodes: ImportDeclaration[] = []; + + traverse(ast, { + Directive({ node }) { + directives.push(node); + + // Trailing comments probably shouldn't be attached to the directive + node.trailingComments = null; + }, + + ImportDeclaration(path: NodePath) { + const tsModuleParent = path.findParent((p) => + isTSModuleDeclaration(p), + ); + if (!tsModuleParent) { + importNodes.push(path.node); + } + }, + }); + + const formatted = getCodeFromAst(importNodes, directives, code, null); + expect(format(formatted, { parser: 'babel' })).toEqual( + `"use client"; + +// first comment +import b from "b"; +import a from "a"; +`, + ); +}); From 7a39718ab1899d3ae304a2c1a3c3a00295e8cb2e Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Mon, 21 Nov 2022 12:03:10 -0800 Subject: [PATCH 8/8] refactor traverse() logic into extract-ast-nodes --- src/preprocessors/preprocessor.ts | 30 +++++------------- src/utils/__tests__/get-code-from-ast.spec.ts | 29 ++--------------- src/utils/extract-ast-nodes.ts | 31 +++++++++++++++++++ 3 files changed, 42 insertions(+), 48 deletions(-) create mode 100644 src/utils/extract-ast-nodes.ts diff --git a/src/preprocessors/preprocessor.ts b/src/preprocessors/preprocessor.ts index 2a71ee5..04f9625 100644 --- a/src/preprocessors/preprocessor.ts +++ b/src/preprocessors/preprocessor.ts @@ -1,8 +1,8 @@ -import { parse as babelParser, ParserOptions } from '@babel/parser'; -import traverse, { NodePath } from '@babel/traverse'; -import { Directive, ImportDeclaration, isTSModuleDeclaration } from '@babel/types'; +import { ParserOptions, parse as babelParser } from '@babel/parser'; +import { Directive, ImportDeclaration } from '@babel/types'; import { PrettierOptions } from '../types'; +import { extractASTNodes } from '../utils/extract-ast-nodes'; import { getCodeFromAst } from '../utils/get-code-from-ast'; import { getExperimentalParserPlugins } from '../utils/get-experimental-parser-plugins'; import { getSortedNodes } from '../utils/get-sorted-nodes'; @@ -17,7 +17,6 @@ export function preprocessor(code: string, options: PrettierOptions) { importOrderSortSpecifiers, } = options; - const importNodes: ImportDeclaration[] = []; const parserOptions: ParserOptions = { sourceType: 'module', plugins: getExperimentalParserPlugins(importOrderParserPlugins), @@ -26,24 +25,11 @@ export function preprocessor(code: string, options: PrettierOptions) { const ast = babelParser(code, parserOptions); const interpreter = ast.program.interpreter; - const directives: Directive[] = []; - traverse(ast, { - Directive({ node }) { - directives.push(node); - - // Trailing comments probably shouldn't be attached to the directive - node.trailingComments = null; - }, - - ImportDeclaration(path: NodePath) { - const tsModuleParent = path.findParent((p) => - isTSModuleDeclaration(p), - ); - if (!tsModuleParent) { - importNodes.push(path.node); - } - }, - }); + const { + importNodes, + directives, + }: { importNodes: ImportDeclaration[]; directives: Directive[] } = + extractASTNodes(ast); // short-circuit if there are no import declaration if (importNodes.length === 0) return code; diff --git a/src/utils/__tests__/get-code-from-ast.spec.ts b/src/utils/__tests__/get-code-from-ast.spec.ts index 7b4a8e2..da58c04 100644 --- a/src/utils/__tests__/get-code-from-ast.spec.ts +++ b/src/utils/__tests__/get-code-from-ast.spec.ts @@ -1,13 +1,8 @@ import { parse as babelParser } from '@babel/core'; import { ParserOptions } from '@babel/parser'; -import traverse, { NodePath } from '@babel/traverse'; -import { - Directive, - ImportDeclaration, - isTSModuleDeclaration, -} from '@babel/types'; import { format } from 'prettier'; +import { extractASTNodes } from '../extract-ast-nodes'; import { getCodeFromAst } from '../get-code-from-ast'; import { getExperimentalParserPlugins } from '../get-experimental-parser-plugins'; import { getImportNodes } from '../get-import-nodes'; @@ -57,26 +52,8 @@ import a from 'a';`; plugins: getExperimentalParserPlugins([]), }; const ast = babelParser(code, parserOptions); - const directives: Directive[] = []; - const importNodes: ImportDeclaration[] = []; - - traverse(ast, { - Directive({ node }) { - directives.push(node); - - // Trailing comments probably shouldn't be attached to the directive - node.trailingComments = null; - }, - - ImportDeclaration(path: NodePath) { - const tsModuleParent = path.findParent((p) => - isTSModuleDeclaration(p), - ); - if (!tsModuleParent) { - importNodes.push(path.node); - } - }, - }); + if (!ast) throw new Error('ast is null'); + const { directives, importNodes } = extractASTNodes(ast); const formatted = getCodeFromAst(importNodes, directives, code, null); expect(format(formatted, { parser: 'babel' })).toEqual( diff --git a/src/utils/extract-ast-nodes.ts b/src/utils/extract-ast-nodes.ts new file mode 100644 index 0000000..14e19b1 --- /dev/null +++ b/src/utils/extract-ast-nodes.ts @@ -0,0 +1,31 @@ +import { ParseResult } from '@babel/parser'; +import traverse, { NodePath } from '@babel/traverse'; +import { + Directive, + File, + ImportDeclaration, + isTSModuleDeclaration, +} from '@babel/types'; + +export function extractASTNodes(ast: ParseResult) { + const importNodes: ImportDeclaration[] = []; + const directives: Directive[] = []; + traverse(ast, { + Directive({ node }) { + directives.push(node); + + // Trailing comments probably shouldn't be attached to the directive + node.trailingComments = null; + }, + + ImportDeclaration(path: NodePath) { + const tsModuleParent = path.findParent((p) => + isTSModuleDeclaration(p), + ); + if (!tsModuleParent) { + importNodes.push(path.node); + } + }, + }); + return { importNodes, directives }; +}