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

Only preserve directives already at the start of the program #198

Merged
merged 2 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 13 additions & 4 deletions src/utils/extract-ast-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ export function extractASTNodes(ast: ParseResult<File>) {
const importNodes: ImportDeclaration[] = [];
const directives: Directive[] = [];
traverse(ast, {
Directive({ node }) {
directives.push(node);
Directive(path: NodePath<Directive>) {
// Only capture directives if they are at the top scope of the source
// and their previous siblings are all directives
if (
path.parent.type === 'Program' &&
path.getAllPrevSiblings().every((s) => {
return s.type === 'Directive';
})
) {
directives.push(path.node);

// Trailing comments probably shouldn't be attached to the directive
node.trailingComments = null;
// Trailing comments probably shouldn't be attached to the directive
path.node.trailingComments = null;
}
},

ImportDeclaration(path: NodePath<ImportDeclaration>) {
Expand Down
34 changes: 34 additions & 0 deletions tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ import abc from "@core/abc";
function add(a:number,b:number) {
return a + b;
}
function addStrict(a:number,b:number) {
'use strict';
return a + b;
}

'preserve me';

const workletAdd = (a:number,b:number) => {
'worklet';
return a + b;
}

(function() {
'use strict';
// some iffe example
return true;
})();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"use strict";
"use client";
Expand All @@ -171,6 +188,23 @@ import otherthing from "@core/otherthing";
function add(a: number, b: number) {
return a + b;
}
function addStrict(a: number, b: number) {
"use strict";
return a + b;
}

("preserve me");

const workletAdd = (a: number, b: number) => {
"worklet";
return a + b;
};

(function () {
"use strict";
// some iffe example
return true;
})();

`;

Expand Down
17 changes: 17 additions & 0 deletions tests/ImportsNotSeparated/imports-with-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,20 @@ import abc from "@core/abc";
function add(a:number,b:number) {
return a + b;
}
function addStrict(a:number,b:number) {
'use strict';
return a + b;
}

'preserve me';

const workletAdd = (a:number,b:number) => {
'worklet';
return a + b;
}

(function() {
'use strict';
// some iffe example
return true;
})();
36 changes: 36 additions & 0 deletions tests/ImportsSeparated/__snapshots__/ppsi.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,24 @@ import abc from "@core/abc";
function add(a:number,b:number) {
return a + b;
}

function addStrict(a:number,b:number) {
'use strict';
return a + b;
}

'preserve me';

const workletAdd = (a:number,b:number) => {
'worklet';
return a + b;
}

(function() {
'use strict';
// some iffe example
return true;
})();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"use strict";
"use client";
Expand All @@ -187,6 +205,24 @@ function add(a: number, b: number) {
return a + b;
}

function addStrict(a: number, b: number) {
"use strict";
return a + b;
}

("preserve me");

const workletAdd = (a: number, b: number) => {
"worklet";
return a + b;
};

(function () {
"use strict";
// some iffe example
return true;
})();

`;

exports[`imports-with-file-level-comments.ts - typescript-verify: imports-with-file-level-comments.ts 1`] = `
Expand Down
18 changes: 18 additions & 0 deletions tests/ImportsSeparated/imports-with-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,21 @@ import abc from "@core/abc";
function add(a:number,b:number) {
return a + b;
}

function addStrict(a:number,b:number) {
'use strict';
return a + b;
}

'preserve me';

const workletAdd = (a:number,b:number) => {
'worklet';
return a + b;
}

(function() {
'use strict';
// some iffe example
return true;
})();