Skip to content

Commit

Permalink
fix: register regex backtracking (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Dec 26, 2024
1 parent dfce0ab commit fb42a72
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions src/trace/analysis.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { JspmError } from "../common/err.js";
import { getIntegrity } from "../common/integrity.js";

export type Analysis = AnalysisData | {
parseError: JspmError | Error
};
export type Analysis =
| AnalysisData
| {
parseError: JspmError | Error;
};

export interface AnalysisData {
deps: string[];
Expand Down Expand Up @@ -31,8 +33,9 @@ export async function createEsmAnalysis(
imports: any[],
source: string,
url: string
): Promise<Analysis> { // Change the return type to Promise<Analysis>
if (!imports.length && registerRegEx.test(source))
): Promise<Analysis> {
// Change the return type to Promise<Analysis>
if (!imports.length && systemMatch(source))
return createSystemAnalysis(source, imports, url);
const deps: string[] = [];
const dynamicDeps: string[] = [];
Expand Down Expand Up @@ -67,15 +70,21 @@ export async function createEsmAnalysis(
integrity: await getIntegrity(source),
};
}
const leadingCommentRegex = /^\s*(\/\*[\s\S]*?\*\/|\s*\/\/[^\n]*)*/;
const registerRegex = /^\s*System\s*\.\s*register\s*\(\s*(\[[^\]]*\])\s*,\s*\(?function\s*\(\s*([^\),\s]+\s*(,\s*([^\),\s]+)\s*)?\s*)?\)/;

function systemMatch(code) {
const commentMatch = code.match(leadingCommentRegex);
const offset = commentMatch ? commentMatch[0].length : 0;
return code.slice(offset).match(registerRegex);
}

const registerRegEx =
/^\s*(\/\*[^\*]*(\*(?!\/)[^\*]*)*\*\/|\s*\/\/[^\n]*)*\s*System\s*\.\s*register\s*\(\s*(\[[^\]]*\])\s*,\s*\(?function\s*\(\s*([^\),\s]+\s*(,\s*([^\),\s]+)\s*)?\s*)?\)/;
export async function createSystemAnalysis(
source: string,
imports: string[],
url: string
): Promise<Analysis> {
const [, , , rawDeps, , , contextId] = source.match(registerRegEx) || [];
const [, rawDeps, contextId] = systemMatch(source) || [];
if (!rawDeps) return createEsmAnalysis(imports, source, url);
const deps = JSON.parse(rawDeps.replace(/'/g, '"'));
const dynamicDeps: string[] = [];
Expand Down
2 changes: 1 addition & 1 deletion test/resolve/unused-cjs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ await (async () => {
await generator.install({ target: './cjspkg', subpath: './browser.js' });
assert.deepStrictEqual(generator.getMap(), {
imports: {
'./cjspkg/browser-dep-exclude.js': 'https://ga.jspm.io/npm:@jspm/core@2.0.1/nodelibs/@empty.js',
'./cjspkg/browser-dep-exclude.js': 'https://ga.jspm.io/npm:@jspm/core@2.1.0/nodelibs/@empty.js',
'cjspkg/browser.js': './cjspkg/browser.js',
unusedcjspkg: './unusedcjspkg/index.js'
},
Expand Down

0 comments on commit fb42a72

Please sign in to comment.