@interlace/eslint-devkit 1.3.3 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +113 -0
- package/README.md +20 -9
- package/package.json +13 -11
- package/src/aria-definitions.d.ts +5 -0
- package/src/aria-definitions.js +28 -1
- package/src/index.d.ts +1 -0
- package/src/index.js +4 -0
- package/src/messaging/formatters-ai.d.ts +22 -3
- package/src/messaging/formatters-v2.d.ts +84 -0
- package/src/messaging/formatters-v2.js +363 -0
- package/src/messaging/types-v2.d.ts +112 -0
- package/src/messaging/types-v2.js +7 -0
- package/src/node/path.js +29 -5
- package/src/resolver/dependency-analysis.d.ts +73 -11
- package/src/resolver/dependency-analysis.js +375 -84
- package/src/resolver/resolver-adapter.js +1 -0
- package/src/resolver/resolver-detection.js +16 -11
- package/src/resolver/resolver.d.ts +6 -26
- package/src/resolver/resolver.js +162 -185
- package/src/security/security-utils.js +3 -3
- package/src/types/index.d.ts +1 -0
- package/src/types/index.js +4 -0
- package/src/types/meta-augmentation.d.ts +44 -0
- package/src/types/meta-augmentation.js +7 -0
|
@@ -21,7 +21,9 @@ exports.getFileImports = getFileImports;
|
|
|
21
21
|
exports.hasOnlyTypeImports = hasOnlyTypeImports;
|
|
22
22
|
exports.computeSCCsFromFile = computeSCCsFromFile;
|
|
23
23
|
exports.isFileInCycle = isFileInCycle;
|
|
24
|
+
exports.findShortestCyclePath = findShortestCyclePath;
|
|
24
25
|
exports.getCycleForFile = getCycleForFile;
|
|
26
|
+
exports.detectCycleFromImport = detectCycleFromImport;
|
|
25
27
|
exports.findAllCircularDependencies = findAllCircularDependencies;
|
|
26
28
|
exports.getMinimalCycle = getMinimalCycle;
|
|
27
29
|
exports.getCycleHash = getCycleHash;
|
|
@@ -40,6 +42,7 @@ exports.isIncrementalAvailable = isIncrementalAvailable;
|
|
|
40
42
|
* It relies on node-utils for low-level system operations.
|
|
41
43
|
*/
|
|
42
44
|
const resolver_1 = require("./resolver");
|
|
45
|
+
const node_fs_1 = require("node:fs");
|
|
43
46
|
const fs_1 = require("../node/fs");
|
|
44
47
|
const path_1 = require("../node/path");
|
|
45
48
|
// =============================================================================
|
|
@@ -49,8 +52,13 @@ const path_1 = require("../node/path");
|
|
|
49
52
|
/**
|
|
50
53
|
* Maximum entries in LRU cache before eviction
|
|
51
54
|
* eslint-plugin-import uses similar approach for their ExportMap cache
|
|
55
|
+
*
|
|
56
|
+
* PERFORMANCE: Sized for large codebases (5K+ files × ~10 imports = ~50K entries).
|
|
57
|
+
* A too-small cache causes thrashing: every evicted entry requires a full
|
|
58
|
+
* re-resolution through the resolver pipeline (enhanced-resolve, getTsconfig,
|
|
59
|
+
* extension fan-out). Memory cost is ~2KB per entry, so 50K ≈ 100MB max.
|
|
52
60
|
*/
|
|
53
|
-
const LRU_MAX_SIZE =
|
|
61
|
+
const LRU_MAX_SIZE = 50_000;
|
|
54
62
|
/**
|
|
55
63
|
* Quick check patterns for skipping external modules early
|
|
56
64
|
* This is a key optimization - skip node_modules paths without full resolution
|
|
@@ -121,6 +129,23 @@ const DYNAMIC_IMPORT_REGEX = /import\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
|
|
|
121
129
|
* Matches: import type { x } from 'y'
|
|
122
130
|
*/
|
|
123
131
|
const TYPE_IMPORT_REGEX = /import\s+type\s/;
|
|
132
|
+
/**
|
|
133
|
+
* Pre-compiled regex for re-exports (export … from '…').
|
|
134
|
+
*
|
|
135
|
+
* Re-exports establish runtime import edges. The cycle in next.js's
|
|
136
|
+
* `client/router.ts` ↔ `client/with-router.tsx` propagates ENTIRELY through
|
|
137
|
+
* re-exports — without this regex our DFS doesn't see the edge and misses
|
|
138
|
+
* the cycle (verified against oxlint native, which catches it).
|
|
139
|
+
*
|
|
140
|
+
* Negative lookahead `(?!type[\s{])` excludes `export type ... from` —
|
|
141
|
+
* those are erased at compile time and don't cause runtime cycles.
|
|
142
|
+
*/
|
|
143
|
+
const EXPORT_FROM_REGEX = /export\s+\{[\s\S]*?\}\s+from\s+['"]([^'"]+)['"]/g;
|
|
144
|
+
/**
|
|
145
|
+
* Default extensions for import resolution.
|
|
146
|
+
* Module-level constant to avoid allocating a new array per resolve call.
|
|
147
|
+
*/
|
|
148
|
+
const DEFAULT_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx'];
|
|
124
149
|
// =============================================================================
|
|
125
150
|
// LRU Cache Implementation (inspired by eslint-plugin-import's caching)
|
|
126
151
|
// =============================================================================
|
|
@@ -204,8 +229,9 @@ function createFileSystemCache() {
|
|
|
204
229
|
sccs: [],
|
|
205
230
|
sccComputed: false,
|
|
206
231
|
graphHash: '',
|
|
207
|
-
resolvedPaths: new
|
|
232
|
+
resolvedPaths: new Map(),
|
|
208
233
|
nonCyclicFiles: new Set(),
|
|
234
|
+
pendingCycleReports: new Map(),
|
|
209
235
|
};
|
|
210
236
|
}
|
|
211
237
|
/**
|
|
@@ -221,8 +247,9 @@ function clearCache(cache) {
|
|
|
221
247
|
cache.sccs = [];
|
|
222
248
|
cache.sccComputed = false;
|
|
223
249
|
cache.graphHash = '';
|
|
224
|
-
cache.resolvedPaths
|
|
250
|
+
cache.resolvedPaths = new Map();
|
|
225
251
|
cache.nonCyclicFiles.clear();
|
|
252
|
+
cache.pendingCycleReports.clear();
|
|
226
253
|
}
|
|
227
254
|
/**
|
|
228
255
|
* Invalidate SCC cache (call when graph changes)
|
|
@@ -308,23 +335,41 @@ function shouldIgnoreFileWithCache(file, patterns, cache) {
|
|
|
308
335
|
* @returns Absolute file path or null for external packages
|
|
309
336
|
*/
|
|
310
337
|
function resolveImportPath(importPath, options) {
|
|
311
|
-
const
|
|
338
|
+
const fromFile = options.fromFile;
|
|
339
|
+
const extensions = options.extensions || DEFAULT_EXTENSIONS;
|
|
340
|
+
const cache = options.cache;
|
|
341
|
+
const resolverSettings = options.resolverSettings;
|
|
312
342
|
// OPTIMIZATION 1: Quick external check (no fs access needed)
|
|
313
343
|
// This avoids unnecessary file system calls for packages like 'lodash', 'react', etc.
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
344
|
+
if (isDefinitelyExternal(importPath)) {
|
|
345
|
+
// For @-scoped imports, we can't use isDefinitelyExternal alone
|
|
346
|
+
// because @app/foo (internal alias) looks like @testing-library/react (external).
|
|
347
|
+
// We still skip the external check for these and let the resolver handle them.
|
|
348
|
+
if (!importPath.startsWith('@')) {
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
// OPTIMIZATION 2a: For non-relative imports, use a global cache keyed by
|
|
353
|
+
// just the import specifier. Non-relative imports (like @app/utils, react)
|
|
354
|
+
// resolve to the same path regardless of which file imports them in a
|
|
355
|
+
// single-tsconfig project. This turns ~14K resolution calls into ~400.
|
|
356
|
+
const isRelative = importPath.startsWith('.');
|
|
357
|
+
if (!isRelative) {
|
|
358
|
+
const globalCached = cache.resolvedPaths.get(importPath);
|
|
359
|
+
if (globalCached !== undefined) {
|
|
360
|
+
return globalCached;
|
|
361
|
+
}
|
|
317
362
|
}
|
|
318
|
-
// OPTIMIZATION
|
|
319
|
-
const cacheKey = `${fromFile}::${importPath}
|
|
363
|
+
// OPTIMIZATION 2b: Per-file cache for relative imports (./foo varies by dir)
|
|
364
|
+
const cacheKey = isRelative ? `${fromFile}::${importPath}` : importPath;
|
|
320
365
|
const cached = cache.resolvedPaths.get(cacheKey);
|
|
321
366
|
if (cached !== undefined) {
|
|
322
367
|
return cached;
|
|
323
368
|
}
|
|
324
|
-
// Use the
|
|
369
|
+
// Use the robust resolver
|
|
325
370
|
// This supports:
|
|
326
371
|
// 1. TypeScript paths (tsconfig.json)
|
|
327
|
-
// 2. Webpack aliases (via
|
|
372
|
+
// 2. Webpack aliases (via oxc-resolver/tsconfig)
|
|
328
373
|
// 3. Package.json exports
|
|
329
374
|
// 4. Monorepo resolution
|
|
330
375
|
let result = (0, resolver_1.resolveModule)(importPath, fromFile, {
|
|
@@ -334,8 +379,30 @@ function resolveImportPath(importPath, options) {
|
|
|
334
379
|
mainFields: ['module', 'main'],
|
|
335
380
|
resolverSettings,
|
|
336
381
|
});
|
|
382
|
+
// OPTIMIZATION 3: If non-relative import resolved to node_modules, treat as external.
|
|
383
|
+
// This prevents the DFS from traversing into external packages.
|
|
384
|
+
//
|
|
385
|
+
// CAVEAT: In monorepos, workspace packages are often symlinked into
|
|
386
|
+
// node_modules (e.g., pnpm/npm workspaces). We use realpathSync to check
|
|
387
|
+
// if the file truly lives in node_modules vs being a symlinked local package.
|
|
388
|
+
if (result && !isRelative && result.includes('node_modules')) {
|
|
389
|
+
try {
|
|
390
|
+
const realPath = (0, node_fs_1.realpathSync)(result);
|
|
391
|
+
if (realPath.includes('node_modules')) {
|
|
392
|
+
cache.resolvedPaths.set(cacheKey, null);
|
|
393
|
+
return null;
|
|
394
|
+
}
|
|
395
|
+
// Symlinked workspace package — use the real path for DFS traversal
|
|
396
|
+
result = realPath;
|
|
397
|
+
}
|
|
398
|
+
catch {
|
|
399
|
+
// realpathSync failed (broken symlink, etc.) — treat as external
|
|
400
|
+
cache.resolvedPaths.set(cacheKey, null);
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
337
404
|
// Fallback for relative imports that don't exist (useful for error reporting)
|
|
338
|
-
if (!result &&
|
|
405
|
+
if (!result && isRelative) {
|
|
339
406
|
// We can't know the extension, but we can return the absolute path base
|
|
340
407
|
// This matches what tests expect for non-existent files
|
|
341
408
|
result = (0, path_1.resolvePath)((0, path_1.getDirname)(fromFile), importPath);
|
|
@@ -359,12 +426,14 @@ function resolveImportPath(importPath, options) {
|
|
|
359
426
|
*/
|
|
360
427
|
function getFileImports(file, options) {
|
|
361
428
|
const { workspaceRoot, barrelExports, cache, resolverSettings } = options;
|
|
362
|
-
//
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
429
|
+
// PHASE 6 OPTIMIZATION: Within a single lint run, files don't change.
|
|
430
|
+
// Skip the expensive isCacheValid (which calls statSync per file) and
|
|
431
|
+
// just check if we already have cached dependencies. The hash-based
|
|
432
|
+
// invalidation still runs after parsing (line ~635) to support watch
|
|
433
|
+
// mode and cross-run ESLint cache.
|
|
434
|
+
const cached = cache.dependencies.get(file);
|
|
435
|
+
if (cached) {
|
|
436
|
+
return cached;
|
|
368
437
|
}
|
|
369
438
|
const imports = [];
|
|
370
439
|
try {
|
|
@@ -375,40 +444,55 @@ function getFileImports(file, options) {
|
|
|
375
444
|
if (content === null) {
|
|
376
445
|
return imports;
|
|
377
446
|
}
|
|
447
|
+
// PHASE 6 OPTIMIZATION: Pre-allocate the resolve options object ONCE per file.
|
|
448
|
+
// Previously, a new options object was created for every regex match (~25K
|
|
449
|
+
// allocations per lint run on snappy-client-dashboard). The `importPath`
|
|
450
|
+
// field is set to a placeholder and will never be used because
|
|
451
|
+
// resolveImportPath takes it as a separate argument.
|
|
452
|
+
const resolveOpts = {
|
|
453
|
+
fromFile: file,
|
|
454
|
+
workspaceRoot,
|
|
455
|
+
barrelExports,
|
|
456
|
+
cache,
|
|
457
|
+
extensions: DEFAULT_EXTENSIONS,
|
|
458
|
+
resolverSettings,
|
|
459
|
+
};
|
|
378
460
|
// Use pre-compiled regex - reset lastIndex for reuse
|
|
379
461
|
IMPORT_REGEX.lastIndex = 0;
|
|
380
462
|
DYNAMIC_IMPORT_REGEX.lastIndex = 0;
|
|
463
|
+
EXPORT_FROM_REGEX.lastIndex = 0;
|
|
381
464
|
let match;
|
|
382
|
-
//
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
imports.push(
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
// Match dynamic imports using pre-compiled regex
|
|
398
|
-
while ((match = DYNAMIC_IMPORT_REGEX.exec(content)) !== null) {
|
|
399
|
-
const importPath = match[1];
|
|
400
|
-
const resolved = resolveImportPath(importPath, {
|
|
401
|
-
fromFile: file,
|
|
402
|
-
workspaceRoot,
|
|
403
|
-
barrelExports,
|
|
404
|
-
cache,
|
|
405
|
-
extensions: ['.ts', '.tsx', '.js', '.jsx'],
|
|
406
|
-
resolverSettings,
|
|
407
|
-
});
|
|
408
|
-
if (resolved && fileExists(resolved, cache)) {
|
|
409
|
-
imports.push({ path: resolved, source: importPath, dynamic: true });
|
|
465
|
+
// Track resolved paths to dedupe across the three regexes (a single file
|
|
466
|
+
// may have both `import X from 'y'` and `export { X } from 'y'`).
|
|
467
|
+
const seen = new Set();
|
|
468
|
+
const pushImport = (importPath, dynamic = false, typeOnly = false) => {
|
|
469
|
+
const resolved = resolveImportPath(importPath, resolveOpts);
|
|
470
|
+
if (resolved && fileExists(resolved, cache) && !seen.has(resolved)) {
|
|
471
|
+
seen.add(resolved);
|
|
472
|
+
const info = { path: resolved, source: importPath };
|
|
473
|
+
if (dynamic)
|
|
474
|
+
info.dynamic = true;
|
|
475
|
+
if (typeOnly)
|
|
476
|
+
info.typeOnly = true;
|
|
477
|
+
imports.push(info);
|
|
410
478
|
}
|
|
479
|
+
};
|
|
480
|
+
// Match ES6 static imports using pre-compiled regex.
|
|
481
|
+
// Detect `import type { ... }` by checking if the full match starts with
|
|
482
|
+
// "import type" — these edges are erased at compile time and must not
|
|
483
|
+
// participate in the SCC graph (they cannot cause runtime cycles).
|
|
484
|
+
while ((match = IMPORT_REGEX.exec(content)) !== null) {
|
|
485
|
+
const isTypeImport = /^import\s+type[\s{]/.test(match[0]);
|
|
486
|
+
pushImport(match[1], false, isTypeImport);
|
|
411
487
|
}
|
|
488
|
+
// Match re-exports (`export … from '…'`). Without this, cycles routed
|
|
489
|
+
// through re-export-only files (a common pattern in barrel-heavy
|
|
490
|
+
// codebases) are invisible.
|
|
491
|
+
while ((match = EXPORT_FROM_REGEX.exec(content)) !== null)
|
|
492
|
+
pushImport(match[1]);
|
|
493
|
+
// Match dynamic imports
|
|
494
|
+
while ((match = DYNAMIC_IMPORT_REGEX.exec(content)) !== null)
|
|
495
|
+
pushImport(match[1], true);
|
|
412
496
|
// Store file hash for cache invalidation + invalidate SCC if graph changed
|
|
413
497
|
const hash = getFileHash(file);
|
|
414
498
|
if (hash) {
|
|
@@ -468,9 +552,19 @@ function hasOnlyTypeImports(cycle, cache) {
|
|
|
468
552
|
* @returns Array of SCCs (each SCC is an array of file paths)
|
|
469
553
|
*/
|
|
470
554
|
function computeSCCsFromFile(startFile, options) {
|
|
555
|
+
// Normalize to real path: on macOS /tmp is a symlink to /private/tmp.
|
|
556
|
+
// Without this, the starting file is keyed as /tmp/... while all imports
|
|
557
|
+
// resolve to /private/tmp/... — Tarjan's sees them as distinct nodes and
|
|
558
|
+
// misses cycles that cross the symlink boundary.
|
|
559
|
+
try {
|
|
560
|
+
startFile = (0, node_fs_1.realpathSync)(startFile);
|
|
561
|
+
}
|
|
562
|
+
catch { /* ignore broken symlinks */ }
|
|
471
563
|
const { workspaceRoot, barrelExports, cache, resolverSettings } = options;
|
|
472
|
-
// If SCC
|
|
473
|
-
|
|
564
|
+
// If SCC already computed AND covers this file, return cached result.
|
|
565
|
+
// If not in the index, the file wasn't reachable from the previous root —
|
|
566
|
+
// fall through to extend the SCC from this new starting file.
|
|
567
|
+
if (cache.sccComputed && cache.sccs.length > 0 && cache.sccIndex.has(startFile)) {
|
|
474
568
|
return cache.sccs;
|
|
475
569
|
}
|
|
476
570
|
const state = {
|
|
@@ -499,7 +593,11 @@ function computeSCCsFromFile(startFile, options) {
|
|
|
499
593
|
resolverSettings,
|
|
500
594
|
});
|
|
501
595
|
for (const imp of imports) {
|
|
502
|
-
|
|
596
|
+
// Skip dynamic imports and type-only imports — type edges are erased at
|
|
597
|
+
// compile time and cannot form runtime cycles. Excluding them from the
|
|
598
|
+
// SCC graph produces a smaller, more accurate graph and eliminates FPs
|
|
599
|
+
// that would appear for `import type { Foo }` back-references.
|
|
600
|
+
if (!imp.dynamic && !imp.typeOnly && !visited.has(imp.path)) {
|
|
503
601
|
filesToProcess.push(imp.path);
|
|
504
602
|
}
|
|
505
603
|
}
|
|
@@ -511,20 +609,44 @@ function computeSCCsFromFile(startFile, options) {
|
|
|
511
609
|
}
|
|
512
610
|
}
|
|
513
611
|
// Convert to SCCResult format
|
|
514
|
-
const
|
|
612
|
+
const newResults = state.sccs.map((files) => ({
|
|
515
613
|
files,
|
|
516
614
|
hasCycle: files.length > 1,
|
|
517
615
|
}));
|
|
518
|
-
//
|
|
519
|
-
|
|
616
|
+
// MERGE into existing cache — never overwrite. When called from a new root
|
|
617
|
+
// file not previously reachable, we extend the global SCC rather than
|
|
618
|
+
// replacing it. Existing sccIndex entries are preserved so previously
|
|
619
|
+
// processed files retain their cycle membership information.
|
|
620
|
+
const offset = cache.sccs.length;
|
|
621
|
+
cache.sccs.push(...newResults);
|
|
520
622
|
cache.sccComputed = true;
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
623
|
+
for (let i = 0; i < newResults.length; i++) {
|
|
624
|
+
for (const file of newResults[i].files) {
|
|
625
|
+
if (!cache.sccIndex.has(file)) {
|
|
626
|
+
cache.sccIndex.set(file, offset + i);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
// Update nonCyclicFiles from SCC results:
|
|
631
|
+
// - Singleton SCCs (hasCycle=false) → file is PROVABLY non-cyclic → add permanently
|
|
632
|
+
// - Multi-file cyclic SCCs → remove from nonCyclicFiles in case a previous pass
|
|
633
|
+
// incorrectly marked them as non-cyclic (e.g., from an incomplete DFS)
|
|
634
|
+
//
|
|
635
|
+
// With nonCyclicFiles populated here from the authoritative SCC, the ImportDeclaration
|
|
636
|
+
// handler can use it as an O(1) fast path without needing to clear it per-file.
|
|
637
|
+
for (const result of newResults) {
|
|
638
|
+
if (result.hasCycle) {
|
|
639
|
+
for (const file of result.files) {
|
|
640
|
+
cache.nonCyclicFiles.delete(file);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
else {
|
|
644
|
+
for (const file of result.files) {
|
|
645
|
+
cache.nonCyclicFiles.add(file);
|
|
646
|
+
}
|
|
525
647
|
}
|
|
526
648
|
}
|
|
527
|
-
return
|
|
649
|
+
return cache.sccs;
|
|
528
650
|
}
|
|
529
651
|
/**
|
|
530
652
|
* Tarjan's strong connect function (recursive part)
|
|
@@ -549,8 +671,9 @@ function tarjanStrongConnect(file, state, options, depth) {
|
|
|
549
671
|
resolverSettings,
|
|
550
672
|
});
|
|
551
673
|
for (const imp of imports) {
|
|
552
|
-
// Skip dynamic imports
|
|
553
|
-
|
|
674
|
+
// Skip dynamic and type-only imports — type edges are erased at compile
|
|
675
|
+
// time and must not participate in SCC graph edges.
|
|
676
|
+
if (imp.dynamic || imp.typeOnly)
|
|
554
677
|
continue;
|
|
555
678
|
const successor = imp.path;
|
|
556
679
|
if (!state.indices.has(successor)) {
|
|
@@ -596,6 +719,56 @@ function isFileInCycle(file, cache) {
|
|
|
596
719
|
const scc = cache.sccs[sccIndex];
|
|
597
720
|
return scc && scc.hasCycle;
|
|
598
721
|
}
|
|
722
|
+
/**
|
|
723
|
+
* Find the shortest cycle path from targetFile back to sourceFile via BFS.
|
|
724
|
+
*
|
|
725
|
+
* This is the path-finding phase — called ONLY after the SCC pre-check
|
|
726
|
+
* confirms that sourceFile and targetFile are in the same SCC (meaning a
|
|
727
|
+
* cycle MUST exist). BFS guarantees the shortest cycle path is returned.
|
|
728
|
+
*
|
|
729
|
+
* Only traverses files within the same SCC (O(1) lookup per hop), so the
|
|
730
|
+
* worst-case work is proportional to the SCC size, not the entire graph.
|
|
731
|
+
*
|
|
732
|
+
* @param sourceFile - The file that contains the import (cycle closes here)
|
|
733
|
+
* @param targetFile - The file being imported (BFS starts here)
|
|
734
|
+
* @param options - Traversal options (workspaceRoot, barrelExports, cache, resolverSettings)
|
|
735
|
+
* @returns The cycle path [targetFile, ..., sourceFile], or null if not found
|
|
736
|
+
*/
|
|
737
|
+
function findShortestCyclePath(sourceFile, targetFile, options) {
|
|
738
|
+
const { workspaceRoot, barrelExports, cache, resolverSettings } = options;
|
|
739
|
+
const srcSCC = cache.sccIndex.get(sourceFile);
|
|
740
|
+
if (srcSCC === undefined)
|
|
741
|
+
return null;
|
|
742
|
+
// BFS from targetFile, looking for a path back to sourceFile
|
|
743
|
+
const queue = [[targetFile]];
|
|
744
|
+
const visited = new Set([targetFile]);
|
|
745
|
+
while (queue.length > 0) {
|
|
746
|
+
const path = queue.shift();
|
|
747
|
+
const current = path[path.length - 1];
|
|
748
|
+
const imports = getFileImports(current, {
|
|
749
|
+
workspaceRoot,
|
|
750
|
+
barrelExports,
|
|
751
|
+
cache,
|
|
752
|
+
resolverSettings,
|
|
753
|
+
});
|
|
754
|
+
for (const imp of imports) {
|
|
755
|
+
if (imp.dynamic || imp.typeOnly)
|
|
756
|
+
continue;
|
|
757
|
+
if (imp.path === sourceFile) {
|
|
758
|
+
// Found the cycle — return the complete path
|
|
759
|
+
return [...path, sourceFile];
|
|
760
|
+
}
|
|
761
|
+
if (visited.has(imp.path))
|
|
762
|
+
continue;
|
|
763
|
+
// Only traverse files in the same SCC — anything else cannot reach sourceFile
|
|
764
|
+
if (cache.sccIndex.get(imp.path) !== srcSCC)
|
|
765
|
+
continue;
|
|
766
|
+
visited.add(imp.path);
|
|
767
|
+
queue.push([...path, imp.path]);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
return null;
|
|
771
|
+
}
|
|
599
772
|
/**
|
|
600
773
|
* Get the cycle containing a specific file
|
|
601
774
|
*
|
|
@@ -615,35 +788,148 @@ function getCycleForFile(file, cache) {
|
|
|
615
788
|
return scc.files;
|
|
616
789
|
}
|
|
617
790
|
/**
|
|
618
|
-
*
|
|
791
|
+
* Lightweight per-import cycle detector (Phase 3 optimization)
|
|
619
792
|
*
|
|
620
|
-
*
|
|
621
|
-
*
|
|
622
|
-
*
|
|
623
|
-
*
|
|
793
|
+
* Instead of building the full graph upfront via BFS + Tarjan SCC,
|
|
794
|
+
* this function does a simple bounded DFS from a target file, looking
|
|
795
|
+
* for any path back to the source file. This is architecturally similar
|
|
796
|
+
* to eslint-plugin-import's no-cycle approach and is dramatically faster
|
|
797
|
+
* because it only reads files along the actual traversal path.
|
|
624
798
|
*
|
|
625
|
-
*
|
|
626
|
-
* -
|
|
627
|
-
* - O(1)
|
|
628
|
-
* -
|
|
799
|
+
* Performance characteristics:
|
|
800
|
+
* - O(depth) file reads per call (not O(|V|) like BFS)
|
|
801
|
+
* - nonCyclicFiles cache provides O(1) rejection across the lint run
|
|
802
|
+
* - getFileImports cache reuses parsed results across files
|
|
803
|
+
* - Bounded by maxDepth to prevent runaway traversal
|
|
804
|
+
*
|
|
805
|
+
* @param sourceFile - The file containing the import (looking for path back to here)
|
|
806
|
+
* @param targetFile - The file being imported (starting point for DFS)
|
|
807
|
+
* @param options - Detection options
|
|
808
|
+
* @returns Array of cycle paths found (each is an array of file paths ending at sourceFile)
|
|
809
|
+
*/
|
|
810
|
+
function detectCycleFromImport(sourceFile, targetFile, options) {
|
|
811
|
+
// Normalize paths: on macOS /tmp is a symlink to /private/tmp.
|
|
812
|
+
// ESLint provides the symlink path; resolved imports use the real path.
|
|
813
|
+
// Without normalization, `file === sourceFile` never matches in the DFS.
|
|
814
|
+
try {
|
|
815
|
+
sourceFile = (0, node_fs_1.realpathSync)(sourceFile);
|
|
816
|
+
}
|
|
817
|
+
catch { /* ignore */ }
|
|
818
|
+
try {
|
|
819
|
+
targetFile = (0, node_fs_1.realpathSync)(targetFile);
|
|
820
|
+
}
|
|
821
|
+
catch { /* ignore */ }
|
|
822
|
+
const { maxDepth, reportAllCycles, workspaceRoot, barrelExports, cache, resolverSettings, } = options;
|
|
823
|
+
// Fast path: target is known to not be in any cycle
|
|
824
|
+
if (cache.nonCyclicFiles.has(targetFile)) {
|
|
825
|
+
return [];
|
|
826
|
+
}
|
|
827
|
+
const allCycles = [];
|
|
828
|
+
// PERFORMANCE: Use mutable stack instead of [...currentPath, file]
|
|
829
|
+
// to avoid O(depth) array allocations per DFS branch. The path is
|
|
830
|
+
// only copied when a cycle is actually found (rare).
|
|
831
|
+
const pathStack = [sourceFile];
|
|
832
|
+
// Track whether the DFS was truncated by the depth limit. If it was, we
|
|
833
|
+
// CANNOT cache the target as `nonCyclic` — depth-limit truncation isn't
|
|
834
|
+
// proof of acyclicity, and a poisoned cache cascades into every later
|
|
835
|
+
// lint pass that touches a file in the same subtree (verified on next.js:
|
|
836
|
+
// wide scope reported 0 cycles vs. 17 in oxlint, narrow scope reported 5+).
|
|
837
|
+
let depthLimitHit = false;
|
|
838
|
+
function dfs(file, depth, visited) {
|
|
839
|
+
// Found the cycle — we've reached back to the source file
|
|
840
|
+
if (file === sourceFile) {
|
|
841
|
+
allCycles.push([...pathStack, file]);
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
844
|
+
// Depth limit
|
|
845
|
+
if (depth >= maxDepth) {
|
|
846
|
+
depthLimitHit = true;
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
// Already visited in this search tree
|
|
850
|
+
if (visited.has(file))
|
|
851
|
+
return;
|
|
852
|
+
// Known non-cyclic from previous lint file visits
|
|
853
|
+
if (cache.nonCyclicFiles.has(file))
|
|
854
|
+
return;
|
|
855
|
+
// Early exit when only first cycle needed
|
|
856
|
+
if (!reportAllCycles && allCycles.length > 0)
|
|
857
|
+
return;
|
|
858
|
+
visited.add(file);
|
|
859
|
+
pathStack.push(file);
|
|
860
|
+
const imports = getFileImports(file, {
|
|
861
|
+
workspaceRoot,
|
|
862
|
+
barrelExports,
|
|
863
|
+
cache,
|
|
864
|
+
resolverSettings,
|
|
865
|
+
});
|
|
866
|
+
for (const imp of imports) {
|
|
867
|
+
if (imp.dynamic)
|
|
868
|
+
continue;
|
|
869
|
+
if (!reportAllCycles && allCycles.length > 0)
|
|
870
|
+
break;
|
|
871
|
+
dfs(imp.path, depth + 1, visited);
|
|
872
|
+
}
|
|
873
|
+
pathStack.pop();
|
|
874
|
+
}
|
|
875
|
+
const visited = new Set();
|
|
876
|
+
dfs(targetFile, 1, visited);
|
|
877
|
+
// Only cache as non-cyclic when the DFS was complete AND found no cycles.
|
|
878
|
+
// A depth-truncated DFS cannot prove acyclicity (the cycle may exist past
|
|
879
|
+
// the depth limit), and caching it poisons every future lint pass that
|
|
880
|
+
// traverses the same subtree.
|
|
881
|
+
if (allCycles.length === 0 && !depthLimitHit) {
|
|
882
|
+
cache.nonCyclicFiles.add(targetFile);
|
|
883
|
+
}
|
|
884
|
+
return allCycles;
|
|
885
|
+
}
|
|
886
|
+
/**
|
|
887
|
+
* Find all circular dependencies using SCC + DFS hybrid approach
|
|
888
|
+
*
|
|
889
|
+
* Performance Strategy (v2 — optimized based on benchmark data):
|
|
890
|
+
* 1. Compute SCCs via Tarjan's algorithm on first call (cached for lint run)
|
|
891
|
+
* — now significantly faster thanks to resolver fast path + getTsconfig cache
|
|
892
|
+
* 2. Use O(1) SCC membership for instant non-cyclic file rejection
|
|
893
|
+
* 3. Only run DFS for files known to be in a cycle (to extract paths)
|
|
894
|
+
* 4. Build graph lazily via getFileImports() with caching
|
|
895
|
+
*
|
|
896
|
+
* Key optimizations vs v1:
|
|
897
|
+
* - Resolver fast path: existsSync for relative imports (10-50x per call)
|
|
898
|
+
* - Global getTsconfig cache: eliminates per-file directory traversal
|
|
899
|
+
* - visited Set: prevents DFS re-traversal of explored subtrees
|
|
900
|
+
* - nonCyclicFiles cache: O(1) rejection across entire lint run
|
|
629
901
|
*
|
|
630
902
|
* @param file - Starting file for cycle detection
|
|
631
903
|
* @param options - Detection options
|
|
632
904
|
* @param currentPath - Current path in DFS (internal use)
|
|
633
905
|
* @param depth - Current depth (internal use)
|
|
634
|
-
* @param visited - Set of
|
|
906
|
+
* @param visited - Set of fully explored files (internal use, persistent)
|
|
635
907
|
* @returns Array of cycles found (each cycle is an array of file paths)
|
|
636
908
|
*/
|
|
637
|
-
function findAllCircularDependencies(file, options, currentPath = [],
|
|
909
|
+
function findAllCircularDependencies(file, options, currentPath = [],
|
|
910
|
+
// NOTE: depth=0 is the root call; normalization happens only there.
|
|
911
|
+
depth = 0, visited = new Set()) {
|
|
638
912
|
const { maxDepth, reportAllCycles, workspaceRoot, barrelExports, cache, resolverSettings, } = options;
|
|
913
|
+
// Normalize to real path at the root call (depth=0 only — recursive calls
|
|
914
|
+
// already receive normalised paths from the resolver's import resolution).
|
|
915
|
+
if (depth === 0) {
|
|
916
|
+
try {
|
|
917
|
+
file = (0, node_fs_1.realpathSync)(file);
|
|
918
|
+
}
|
|
919
|
+
catch { /* ignore broken symlinks */ }
|
|
920
|
+
}
|
|
639
921
|
// OPTIMIZATION 1: Check if file is known to not be in any cycle
|
|
640
|
-
// This
|
|
922
|
+
// This is populated across the lint run — once a file is confirmed
|
|
923
|
+
// non-cyclic, all future calls skip it in O(1).
|
|
641
924
|
if (cache.nonCyclicFiles.has(file)) {
|
|
642
925
|
return [];
|
|
643
926
|
}
|
|
644
|
-
// OPTIMIZATION 2:
|
|
927
|
+
// OPTIMIZATION 2: At the root call, compute or use cached SCC data.
|
|
928
|
+
// SCC computation uses Tarjan's O(V+E) algorithm and is now much faster
|
|
929
|
+
// thanks to the resolver fast path (existsSync for relative imports) and
|
|
930
|
+
// global getTsconfig caching. The one-time cost is amortized across all
|
|
931
|
+
// file visits in the lint run via O(1) cycle membership lookups.
|
|
645
932
|
if (depth === 0) {
|
|
646
|
-
// Compute SCCs (cached - only computed once per lint run)
|
|
647
933
|
computeSCCsFromFile(file, {
|
|
648
934
|
maxDepth,
|
|
649
935
|
workspaceRoot,
|
|
@@ -651,28 +937,30 @@ function findAllCircularDependencies(file, options, currentPath = [], depth = 0,
|
|
|
651
937
|
cache,
|
|
652
938
|
resolverSettings,
|
|
653
939
|
});
|
|
654
|
-
// Fast path:
|
|
940
|
+
// Fast path: O(1) SCC lookup — if file is not in any cycle, skip
|
|
655
941
|
if (!isFileInCycle(file, cache)) {
|
|
656
|
-
// Mark this file as non-cyclic for future fast lookups
|
|
657
942
|
cache.nonCyclicFiles.add(file);
|
|
658
|
-
return [];
|
|
943
|
+
return [];
|
|
659
944
|
}
|
|
660
|
-
// File is in a cycle - now use DFS to get the actual cycle path for error messages
|
|
661
945
|
}
|
|
662
|
-
//
|
|
946
|
+
// Depth limit for performance
|
|
663
947
|
if (depth > maxDepth) {
|
|
664
948
|
return [];
|
|
665
949
|
}
|
|
666
|
-
// Found a cycle
|
|
950
|
+
// Found a cycle — the current file is already on the current DFS path
|
|
667
951
|
if (currentPath.includes(file)) {
|
|
668
952
|
return [[...currentPath, file]];
|
|
669
953
|
}
|
|
670
|
-
// OPTIMIZATION 3: Skip if already visited in this
|
|
671
|
-
|
|
954
|
+
// OPTIMIZATION 3: Skip if already visited in this DFS run.
|
|
955
|
+
// `visited` is added BEFORE exploring children (below) to prevent
|
|
956
|
+
// infinite recursion through cyclic graphs. This is correct: if we
|
|
957
|
+
// encounter a file already on our `visited` set it means we've started
|
|
958
|
+
// exploring it from a sibling branch — we can safely skip it because
|
|
959
|
+
// the currentPath.includes() check above already handles the cycle case.
|
|
672
960
|
if (visited.has(file)) {
|
|
673
961
|
return [];
|
|
674
962
|
}
|
|
675
|
-
|
|
963
|
+
// Mark visited BEFORE recursing to prevent infinite loops on cyclic graphs.
|
|
676
964
|
visited.add(file);
|
|
677
965
|
const imports = getFileImports(file, {
|
|
678
966
|
workspaceRoot,
|
|
@@ -687,14 +975,17 @@ function findAllCircularDependencies(file, options, currentPath = [], depth = 0,
|
|
|
687
975
|
if (imp.dynamic) {
|
|
688
976
|
continue;
|
|
689
977
|
}
|
|
690
|
-
// OPTIMIZATION 4:
|
|
691
|
-
// If reportAllCycles is false and we already found one, skip remaining imports
|
|
978
|
+
// OPTIMIZATION 4: Early exit when only first cycle is needed
|
|
692
979
|
if (!reportAllCycles && allCycles.length > 0) {
|
|
693
980
|
break;
|
|
694
981
|
}
|
|
695
982
|
const cycles = findAllCircularDependencies(imp.path, options, newPath, depth + 1, visited);
|
|
696
983
|
allCycles.push(...cycles);
|
|
697
984
|
}
|
|
985
|
+
// If no cycles found for this file in this search, mark as non-cyclic
|
|
986
|
+
if (depth === 0 && allCycles.length === 0) {
|
|
987
|
+
cache.nonCyclicFiles.add(file);
|
|
988
|
+
}
|
|
698
989
|
return allCycles;
|
|
699
990
|
}
|
|
700
991
|
/**
|