Skip to content

Commit ab315f7

Browse files
committed
fix: address Greptile review feedback on verify-imports (#540)
- Broaden regex to catch non-awaited import() calls (return, .then()) - Fix block-comment tracking to handle mid-line /* openings - Replace naive before.includes('//') with quote-aware isInsideLineComment to avoid false negatives on URLs in string literals
1 parent 38f4182 commit ab315f7

1 file changed

Lines changed: 39 additions & 10 deletions

File tree

scripts/verify-imports.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,27 @@ function walk(dir) {
3535
}
3636

3737
// ── extract dynamic import specifiers ───────────────────────────────────
38-
// Matches: await import('...') and await import("...")
39-
const DYNAMIC_IMPORT_RE = /await\s+import\(\s*(['"])(.+?)\1\s*\)/g;
38+
// Matches: import('...') and import("...") — with or without await
39+
const DYNAMIC_IMPORT_RE = /(?:await\s+)?import\(\s*(['"])(.+?)\1\s*\)/g;
40+
41+
/**
42+
* Check whether the text contains a `//` line-comment marker that is NOT
43+
* inside a string literal. Walks character-by-character tracking quote state.
44+
*/
45+
function isInsideLineComment(text) {
46+
let inStr = null; // null | "'" | '"' | '`'
47+
for (let i = 0; i < text.length; i++) {
48+
const ch = text[i];
49+
if (ch === '\\' && inStr) { i++; continue; } // skip escaped char
50+
if (inStr) {
51+
if (ch === inStr) inStr = null;
52+
continue;
53+
}
54+
if (ch === "'" || ch === '"' || ch === '`') { inStr = ch; continue; }
55+
if (ch === '/' && text[i + 1] === '/') return true;
56+
}
57+
return false;
58+
}
4059

4160
function extractDynamicImports(filePath) {
4261
const src = readFileSync(filePath, 'utf8');
@@ -52,19 +71,29 @@ function extractDynamicImports(filePath) {
5271
if (line.includes('*/')) inBlockComment = false;
5372
continue;
5473
}
55-
if (/^\s*\/\*/.test(line)) {
56-
if (!line.includes('*/')) inBlockComment = true;
57-
continue;
58-
}
5974
// Skip single-line comments
6075
if (/^\s*\/\//.test(line)) continue;
6176

77+
// Strip block comments from the line before scanning for imports.
78+
// Handles: mid-line /* ... */ (single-line) and opening /* without close.
79+
let scanLine = line;
80+
if (scanLine.includes('/*')) {
81+
// Remove fully closed inline block comments: code /* ... */ more code
82+
scanLine = scanLine.replace(/\/\*.*?\*\//g, '');
83+
// If an unclosed /* remains, keep only the part before it and enter block mode
84+
const openIdx = scanLine.indexOf('/*');
85+
if (openIdx !== -1) {
86+
scanLine = scanLine.slice(0, openIdx);
87+
inBlockComment = true;
88+
}
89+
}
90+
6291
let match;
6392
DYNAMIC_IMPORT_RE.lastIndex = 0;
64-
while ((match = DYNAMIC_IMPORT_RE.exec(line)) !== null) {
65-
// Skip if the match is inside a trailing comment
66-
const before = line.slice(0, match.index);
67-
if (before.includes('//') || before.includes('/*')) continue;
93+
while ((match = DYNAMIC_IMPORT_RE.exec(scanLine)) !== null) {
94+
// Skip if the match is inside a trailing line comment (// outside quotes)
95+
const before = scanLine.slice(0, match.index);
96+
if (isInsideLineComment(before)) continue;
6897

6998
imports.push({ specifier: match[2], line: i + 1 });
7099
}

0 commit comments

Comments
 (0)