Skip to content

Commit 4d7f537

Browse files
fix(scanner): fix regex catastrophic backtracking in import parser
The STATIC_IMPORT_RE regex used [\s\S]*? which causes exponential backtracking on files with complex import statements. Replaced with [^'"]*? which is bounded and cannot backtrack. Also split out side-effect imports into a separate regex for clarity. This was causing mex setup to hang on "Scanning codebase..." step.
1 parent 4b64e1a commit 4d7f537

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/scanner/import-graph.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ const INDEX_RESOLUTION = [
1313
];
1414

1515
// Matches: import ... from './path' | export ... from './path'
16+
// Uses [^'"\n]* instead of [\s\S]*? to avoid catastrophic backtracking
1617
const STATIC_IMPORT_RE =
17-
/(?:import|export)\s+(?:[\s\S]*?\s+from\s+)?['"](\.[^'"]+)['"]/g;
18+
/(?:import|export)\s+[^'"]*?from\s+['"](\.[^'"]+)['"]/g;
19+
20+
// Matches: side-effect import: import './path'
21+
const SIDE_EFFECT_IMPORT_RE = /import\s+['"](\.[^'"]+)['"]/g;
1822

1923
// Matches: import('./path')
2024
const DYNAMIC_IMPORT_RE = /import\s*\(\s*['"](\.[^'"]+)['"]\s*\)/g;
@@ -85,7 +89,7 @@ function resolveImport(
8589
/** Extract all relative import specifiers from file content */
8690
function extractImportSpecifiers(content: string): string[] {
8791
const specifiers: string[] = [];
88-
const regexes = [STATIC_IMPORT_RE, DYNAMIC_IMPORT_RE, REQUIRE_RE];
92+
const regexes = [STATIC_IMPORT_RE, SIDE_EFFECT_IMPORT_RE, DYNAMIC_IMPORT_RE, REQUIRE_RE];
8993

9094
for (const re of regexes) {
9195
re.lastIndex = 0;

0 commit comments

Comments
 (0)