Skip to content

Commit

Permalink
fix(core): find imports after template literals with vars (#13922)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer committed Dec 28, 2022
1 parent b350188 commit ba7eb9d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
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

1 comment on commit ba7eb9d

@vercel
Copy link

@vercel vercel bot commented on ba7eb9d Dec 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.