Skip to content

Commit

Permalink
keep script directives at top of file, fixes trivago#185
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Nov 13, 2022
1 parent df8d10d commit 9131505
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
14 changes: 12 additions & 2 deletions 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';
Expand All @@ -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<ImportDeclaration>) {
const tsModuleParent = path.findParent((p) =>
Expand All @@ -48,5 +58,5 @@ export function preprocessor(code: string, options: PrettierOptions) {
importOrderSortSpecifiers,
});

return getCodeFromAst(allImports, code, interpreter);
return getCodeFromAst(allImports, directives, code, interpreter);
}
6 changes: 4 additions & 2 deletions 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';
Expand All @@ -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] : []),
Expand All @@ -31,7 +33,7 @@ export const getCodeFromAst = (
const newAST = file({
type: 'Program',
body: nodes,
directives: [],
directives,
sourceType: 'module',
interpreter: interpreter,
sourceFile: '',
Expand Down
2 changes: 2 additions & 0 deletions src/utils/remove-nodes-from-original-code.ts
@@ -1,6 +1,7 @@
import {
CommentBlock,
CommentLine,
Directive,
ImportDeclaration,
InterpreterDirective,
Statement,
Expand All @@ -21,6 +22,7 @@ export const removeNodesFromOriginalCode = (
nodes: (
| Statement
| CommentBlock
| Directive
| CommentLine
| ImportDeclaration
| InterpreterDirective
Expand Down
13 changes: 13 additions & 0 deletions tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap
Expand Up @@ -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";
Expand Down
6 changes: 6 additions & 0 deletions 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";

0 comments on commit 9131505

Please sign in to comment.