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(core): find imports after template literals with vars #13922

Merged
merged 1 commit into from
Dec 28, 2022
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
38 changes: 38 additions & 0 deletions packages/nx/src/utils/strip-source-code.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,42 @@ require('./c')`;

expect(stripSourceCode(scanner, input)).toEqual(expected);
});

it('should find an import after a template literal with a variable in it', () => {
const input = `
const a = 1;
const b = \`a: $\{a}\`
const c = await import('./c')
const d = require('./d')
`;
const expected = `import('./c')
require('./d')`;

expect(stripSourceCode(scanner, input)).toEqual(expected);
});

it('finds imports after an escaped character', () => {
const input = `
const b = unquotedLiteral.replace(/"/g, '\\\\"')
const c = await import('./c')
const d = require('./d')
`;
const expected = `import('./c')
require('./d')`;

expect(stripSourceCode(scanner, input)).toEqual(expected);
});

it('finds imports after template literals with a regex inside', () => {
const input = `
const a = 1;
const b = \`"$\{unquotedLiteral.replace(/"/g, '\\\\"')}"\`
const c = await import('./c')
const d = require('./d')
`;
const expected = `import('./c')
require('./d')`;

expect(stripSourceCode(scanner, input)).toEqual(expected);
});
});
22 changes: 22 additions & 0 deletions packages/nx/src/utils/strip-source-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ export function stripSourceCode(scanner: Scanner, contents: string): string {
break;
}

case SyntaxKind.TemplateHead: {
while (true) {
token = scanner.scan();

if (token === SyntaxKind.SlashToken) {
token = scanner.reScanSlashToken();
}

if (token === SyntaxKind.EndOfFileToken) {
// either the template is unterminated, or there
// is some other edge case we haven't compensated for
break;
}

if (token === SyntaxKind.CloseBraceToken) {
token = scanner.reScanTemplateToken(false);
break;
}
}
break;
}

case SyntaxKind.ExportKeyword: {
token = scanner.scan();
while (
Expand Down