Skip to content

Commit 9a6d5e6

Browse files
committed
fix: use langId fallback in extension-set guards for content-inferred files (#743)
The needsComplexity/needsCfg/needsDataflow guards gated solely on file-extension sets, so content-inferred files (.vue tagged as "javascript", extensionless shebang files) could never reach the native or WASM analysis paths despite having a valid langId. Add langId-based fallback checks to all guard sites: the native analysis dispatcher, the WASM pre-parse check, the per-file CFG visitor setup, and the per-file dataflow visitor setup.
1 parent 61b04e9 commit 9a6d5e6

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

src/ast-analysis/engine.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,17 @@ function runNativeAnalysis(
127127

128128
const defs = symbols.definitions || [];
129129

130+
const langSupportsComplexity = COMPLEXITY_EXTENSIONS.has(ext) || COMPLEXITY_RULES.has(langId);
131+
const langSupportsCfg = CFG_EXTENSIONS.has(ext) || CFG_RULES.has(langId);
132+
const langSupportsDataflow = DATAFLOW_EXTENSIONS.has(ext) || DATAFLOW_RULES.has(langId);
133+
130134
const needsComplexity =
131-
doComplexity &&
132-
COMPLEXITY_EXTENSIONS.has(ext) &&
133-
defs.some((d) => hasFuncBody(d) && !d.complexity);
135+
doComplexity && langSupportsComplexity && defs.some((d) => hasFuncBody(d) && !d.complexity);
134136
const needsCfg =
135137
doCfg &&
136-
CFG_EXTENSIONS.has(ext) &&
138+
langSupportsCfg &&
137139
defs.some((d) => hasFuncBody(d) && d.cfg !== null && !Array.isArray(d.cfg?.blocks));
138-
const needsDataflow = doDataflow && !symbols.dataflow && DATAFLOW_EXTENSIONS.has(ext);
140+
const needsDataflow = doDataflow && !symbols.dataflow && langSupportsDataflow;
139141

140142
if (!needsComplexity && !needsCfg && !needsDataflow) continue;
141143

@@ -305,16 +307,21 @@ async function ensureWasmTreesIfNeeded(
305307
!d.name.includes('.');
306308

307309
// AST: need tree when native didn't provide non-call astNodes
308-
const needsAst = doAst && !Array.isArray(symbols.astNodes) && WALK_EXTENSIONS.has(ext);
310+
const lid = symbols._langId || '';
311+
const needsAst =
312+
doAst &&
313+
!Array.isArray(symbols.astNodes) &&
314+
(WALK_EXTENSIONS.has(ext) || AST_TYPE_MAPS.has(lid));
309315
const needsComplexity =
310316
doComplexity &&
311-
COMPLEXITY_EXTENSIONS.has(ext) &&
317+
(COMPLEXITY_EXTENSIONS.has(ext) || COMPLEXITY_RULES.has(lid)) &&
312318
defs.some((d) => hasFuncBody(d) && !d.complexity);
313319
const needsCfg =
314320
doCfg &&
315-
CFG_EXTENSIONS.has(ext) &&
321+
(CFG_EXTENSIONS.has(ext) || CFG_RULES.has(lid)) &&
316322
defs.some((d) => hasFuncBody(d) && d.cfg !== null && !Array.isArray(d.cfg?.blocks));
317-
const needsDataflow = doDataflow && !symbols.dataflow && DATAFLOW_EXTENSIONS.has(ext);
323+
const needsDataflow =
324+
doDataflow && !symbols.dataflow && (DATAFLOW_EXTENSIONS.has(ext) || DATAFLOW_RULES.has(lid));
318325

319326
if (needsAst || needsComplexity || needsCfg || needsDataflow) {
320327
needsWasmTrees = true;
@@ -396,9 +403,9 @@ function setupComplexityVisitorForFile(
396403
}
397404

398405
/** Set up CFG visitor if any definitions need WASM CFG analysis. */
399-
function setupCfgVisitorForFile(defs: Definition[], langId: string, ext: string): Visitor | null {
406+
function setupCfgVisitorForFile(defs: Definition[], langId: string): Visitor | null {
400407
const cfgRulesForLang = CFG_RULES.get(langId);
401-
if (!cfgRulesForLang || !CFG_EXTENSIONS.has(ext)) return null;
408+
if (!cfgRulesForLang) return null;
402409

403410
const needsWasmCfg = defs.some(
404411
(d) => hasFuncBody(d) && d.cfg !== null && !Array.isArray(d.cfg?.blocks),
@@ -432,12 +439,12 @@ function setupVisitors(
432439
opts.complexity !== false ? setupComplexityVisitorForFile(defs, langId, walkerOpts) : null;
433440
if (complexityVisitor) visitors.push(complexityVisitor);
434441

435-
const cfgVisitor = opts.cfg !== false ? setupCfgVisitorForFile(defs, langId, ext) : null;
442+
const cfgVisitor = opts.cfg !== false ? setupCfgVisitorForFile(defs, langId) : null;
436443
if (cfgVisitor) visitors.push(cfgVisitor);
437444

438445
let dataflowVisitor: Visitor | null = null;
439446
const dfRules = DATAFLOW_RULES.get(langId);
440-
if (opts.dataflow !== false && dfRules && DATAFLOW_EXTENSIONS.has(ext) && !symbols.dataflow) {
447+
if (opts.dataflow !== false && dfRules && !symbols.dataflow) {
441448
dataflowVisitor = createDataflowVisitor(dfRules);
442449
visitors.push(dataflowVisitor);
443450
}

0 commit comments

Comments
 (0)