@interlace/eslint-devkit 1.4.0 → 1.4.2
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 +12 -0
- package/README.md +11 -2
- package/package.json +2 -2
- package/src/aria-definitions.d.ts +5 -0
- package/src/aria-definitions.js +28 -1
- package/src/node/path.js +29 -5
- package/src/resolver/dependency-analysis.d.ts +20 -0
- package/src/resolver/dependency-analysis.js +170 -34
- package/src/resolver/resolver-detection.d.ts +8 -0
- package/src/resolver/resolver-detection.js +15 -6
- package/src/resolver/resolver.js +0 -6
- package/src/rule-creation/index.d.ts +1 -0
- package/src/rule-creation/index.js +1 -0
- package/src/rule-creation/mock-context.d.ts +40 -0
- package/src/rule-creation/mock-context.js +47 -0
- package/src/security/security-utils.d.ts +0 -2
- package/src/security/security-utils.js +5 -34
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
## [1.4.0] - 2026-05-03
|
|
2
2
|
|
|
3
|
+
## 1.4.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#269](https://github.com/ofri-peretz/eslint/pull/269) [`7028fe2`](https://github.com/ofri-peretz/eslint/commit/7028fe2668a42266d831014184dcef70e73101ad) Thanks [@ofri-peretz](https://github.com/ofri-peretz)! - docs: dual-logo README header (Interlace mark + ESLint mark side by side) and closing Interlace footer — refreshes the README rendered on npmjs.com. No runtime changes.
|
|
8
|
+
|
|
9
|
+
## 1.4.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#155](https://github.com/ofri-peretz/eslint/pull/155) [`736a5fe`](https://github.com/ofri-peretz/eslint/commit/736a5fed47e673f6157ea900b29fe2a54e4bc7df) Thanks [@ofri-peretz](https://github.com/ofri-peretz)! - Fix `patternToRegex` (glob→regex) to escape **all** regex metacharacters, not just `.`. The previous chained `.replace()` left `\ + ( ) | [ ] { } ^ $` to leak through as regex syntax, so an ignore glob such as `a+b` or `(x)` compiled to a quantifier / capture group and matched the wrong files (CWE-116, surfaced by CodeQL `js/incomplete-sanitization`). The wildcard translation (`**`, `*`, `?`) is unchanged; a regression-lock test pins the metacharacter behavior.
|
|
14
|
+
|
|
3
15
|
### Changed
|
|
4
16
|
|
|
5
17
|
- Module resolver swapped from `enhanced-resolve` + `get-tsconfig` to [`oxc-resolver`](https://www.npmjs.com/package/oxc-resolver) (Rust NAPI, ~18-30× faster). Per-tsconfig caching for monorepo path-alias correctness across package boundaries.
|
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@interlace/eslint-devkit)
|
|
6
6
|
[](https://www.npmjs.com/package/@interlace/eslint-devkit)
|
|
7
|
-
[](https://app.codecov.io/gh/ofri-peretz/eslint/tree/main)
|
|
8
8
|
|
|
9
9
|
> **Keywords:** ESLint utilities, LLM-optimized, AI assistant, auto-fix, TypeScript ESLint, AST utilities, type checking, rule creation, GitHub Copilot, Cursor AI, Claude AI, structured error messages, deterministic fixes
|
|
10
10
|
|
|
@@ -522,10 +522,19 @@ MIT © [Ofri Peretz](https://github.com/ofri-peretz)
|
|
|
522
522
|
|
|
523
523
|
## Contributing
|
|
524
524
|
|
|
525
|
-
Contributions welcome! See
|
|
525
|
+
Contributions welcome! See CONTRIBUTING.md (planned).
|
|
526
526
|
|
|
527
527
|
---
|
|
528
528
|
|
|
529
|
+
<!-- INTERLACE:STAR_CTA:START -->
|
|
530
|
+
## ⭐ Support & follow
|
|
531
|
+
|
|
532
|
+
If the devkit saved you time, **[star the repo](https://github.com/ofri-peretz/eslint)** — stars are the signal that keeps the Interlace ESLint ecosystem maintained — and **[follow the writeups on Dev.to](https://dev.to/ofri-peretz)** for the benchmarks and engineering behind these tools.
|
|
533
|
+
|
|
534
|
+
[](https://github.com/ofri-peretz/eslint)
|
|
535
|
+
|
|
536
|
+
<!-- INTERLACE:STAR_CTA:END -->
|
|
537
|
+
|
|
529
538
|
## Changelog
|
|
530
539
|
|
|
531
540
|
See [CHANGELOG.md](./CHANGELOG.md) for a list of changes and version history.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@interlace/eslint-devkit",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2",
|
|
4
4
|
"description": "TypeScript utilities for building LLM-optimized ESLint plugins - AST helpers, type utilities, security benchmarks, and SARIF output",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -77,7 +77,7 @@
|
|
|
77
77
|
},
|
|
78
78
|
"dependencies": {
|
|
79
79
|
"@typescript-eslint/utils": "^7.0.0 || ^8.0.0",
|
|
80
|
-
"oxc-resolver": "^11.
|
|
80
|
+
"oxc-resolver": "^11.24.2"
|
|
81
81
|
},
|
|
82
82
|
"devDependencies": {
|
|
83
83
|
"@typescript-eslint/parser": "^8.46.2",
|
|
@@ -11,4 +11,9 @@ export declare const ARIA_ROLES: Set<string>;
|
|
|
11
11
|
export declare const ARIA_ATTRIBUTES: Set<string>;
|
|
12
12
|
export declare const ARIA_REQUIRED_PROPS: Record<string, string[]>;
|
|
13
13
|
export declare const ARIA_UNSUPPORTED_ELEMENTS: Set<string>;
|
|
14
|
+
/**
|
|
15
|
+
* Global ARIA properties valid on every element regardless of role.
|
|
16
|
+
* Per WAI-ARIA 1.2 § 6.2 "Global States and Properties".
|
|
17
|
+
*/
|
|
18
|
+
export declare const GLOBAL_ARIA_PROPS: Set<string>;
|
|
14
19
|
export declare const ROLE_SUPPORTED_ARIA_PROPS: Record<string, Set<string>>;
|
package/src/aria-definitions.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* MIT license that can be found in the LICENSE file.
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.ROLE_SUPPORTED_ARIA_PROPS = exports.ARIA_UNSUPPORTED_ELEMENTS = exports.ARIA_REQUIRED_PROPS = exports.ARIA_ATTRIBUTES = exports.ARIA_ROLES = void 0;
|
|
8
|
+
exports.ROLE_SUPPORTED_ARIA_PROPS = exports.GLOBAL_ARIA_PROPS = exports.ARIA_UNSUPPORTED_ELEMENTS = exports.ARIA_REQUIRED_PROPS = exports.ARIA_ATTRIBUTES = exports.ARIA_ROLES = void 0;
|
|
9
9
|
/**
|
|
10
10
|
* ARIA Definitions
|
|
11
11
|
* Lists of valid ARIA roles and attributes
|
|
@@ -177,6 +177,33 @@ exports.ARIA_UNSUPPORTED_ELEMENTS = new Set([
|
|
|
177
177
|
'title',
|
|
178
178
|
'track',
|
|
179
179
|
]);
|
|
180
|
+
/**
|
|
181
|
+
* Global ARIA properties valid on every element regardless of role.
|
|
182
|
+
* Per WAI-ARIA 1.2 § 6.2 "Global States and Properties".
|
|
183
|
+
*/
|
|
184
|
+
exports.GLOBAL_ARIA_PROPS = new Set([
|
|
185
|
+
'aria-atomic',
|
|
186
|
+
'aria-busy',
|
|
187
|
+
'aria-controls',
|
|
188
|
+
'aria-current',
|
|
189
|
+
'aria-describedby',
|
|
190
|
+
'aria-details',
|
|
191
|
+
'aria-disabled',
|
|
192
|
+
'aria-dropeffect',
|
|
193
|
+
'aria-errormessage',
|
|
194
|
+
'aria-flowto',
|
|
195
|
+
'aria-grabbed',
|
|
196
|
+
'aria-haspopup',
|
|
197
|
+
'aria-hidden',
|
|
198
|
+
'aria-invalid',
|
|
199
|
+
'aria-keyshortcuts',
|
|
200
|
+
'aria-label',
|
|
201
|
+
'aria-labelledby',
|
|
202
|
+
'aria-live',
|
|
203
|
+
'aria-owns',
|
|
204
|
+
'aria-relevant',
|
|
205
|
+
'aria-roledescription',
|
|
206
|
+
]);
|
|
180
207
|
// Mapping of roles to supported ARIA properties
|
|
181
208
|
exports.ROLE_SUPPORTED_ARIA_PROPS = {
|
|
182
209
|
alert: new Set([
|
package/src/node/path.js
CHANGED
|
@@ -47,11 +47,35 @@ function patternToRegex(pattern, cache) {
|
|
|
47
47
|
if (cached) {
|
|
48
48
|
return cached;
|
|
49
49
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
// Translate the glob in a single left-to-right pass: escape every regex
|
|
51
|
+
// metacharacter, then map the glob wildcards (**, *, ?) to their regex
|
|
52
|
+
// equivalents. Doing it in one pass means an escaped metacharacter can never
|
|
53
|
+
// be re-interpreted as a wildcard. The previous chained-replace only handled
|
|
54
|
+
// `. * ?`, leaving `\ + ( ) [ ] { } ^ $ |` to leak through as regex syntax
|
|
55
|
+
// (CWE-116, incomplete sanitization) — e.g. the glob `a+b` compiled to the
|
|
56
|
+
// quantifier `a+b` instead of matching the literal `a+b`.
|
|
57
|
+
let escaped = '';
|
|
58
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
59
|
+
const ch = pattern[i];
|
|
60
|
+
if (ch === '*') {
|
|
61
|
+
if (pattern[i + 1] === '*') {
|
|
62
|
+
escaped += '.*';
|
|
63
|
+
i++;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
escaped += '[^/]*';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else if (ch === '?') {
|
|
70
|
+
escaped += '.';
|
|
71
|
+
}
|
|
72
|
+
else if (/[.+^${}()|[\]\\]/.test(ch)) {
|
|
73
|
+
escaped += '\\' + ch;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
escaped += ch;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
55
79
|
const regex = new RegExp(escaped);
|
|
56
80
|
cache.set(pattern, regex);
|
|
57
81
|
return regex;
|
|
@@ -40,6 +40,8 @@ export interface ImportInfo {
|
|
|
40
40
|
source: string;
|
|
41
41
|
/** Whether this is a dynamic import (import()) */
|
|
42
42
|
dynamic?: boolean;
|
|
43
|
+
/** Whether this is a type-only import (import type {...}) — erased at compile time */
|
|
44
|
+
typeOnly?: boolean;
|
|
43
45
|
}
|
|
44
46
|
/**
|
|
45
47
|
* Strongly Connected Component result
|
|
@@ -258,6 +260,24 @@ export declare function computeSCCsFromFile(startFile: string, options: Omit<Cyc
|
|
|
258
260
|
* @returns true if file is part of a cycle
|
|
259
261
|
*/
|
|
260
262
|
export declare function isFileInCycle(file: string, cache: FileSystemCache): boolean;
|
|
263
|
+
/**
|
|
264
|
+
* Find the shortest cycle path from targetFile back to sourceFile via BFS.
|
|
265
|
+
*
|
|
266
|
+
* This is the path-finding phase — called ONLY after the SCC pre-check
|
|
267
|
+
* confirms that sourceFile and targetFile are in the same SCC (meaning a
|
|
268
|
+
* cycle MUST exist). BFS guarantees the shortest cycle path is returned.
|
|
269
|
+
*
|
|
270
|
+
* Only traverses files within the same SCC (O(1) lookup per hop), so the
|
|
271
|
+
* worst-case work is proportional to the SCC size, not the entire graph.
|
|
272
|
+
*
|
|
273
|
+
* @param sourceFile - The file that contains the import (cycle closes here)
|
|
274
|
+
* @param targetFile - The file being imported (BFS starts here)
|
|
275
|
+
* @param options - Traversal options (workspaceRoot, barrelExports, cache, resolverSettings)
|
|
276
|
+
* @returns The cycle path [targetFile, ..., sourceFile], or null if not found
|
|
277
|
+
*/
|
|
278
|
+
export declare function findShortestCyclePath(sourceFile: string, targetFile: string, options: Omit<CycleDetectionOptions, 'reportAllCycles' | 'maxDepth'> & {
|
|
279
|
+
maxDepth?: number;
|
|
280
|
+
}): string[] | null;
|
|
261
281
|
/**
|
|
262
282
|
* Get the cycle containing a specific file
|
|
263
283
|
*
|
|
@@ -21,6 +21,7 @@ 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;
|
|
25
26
|
exports.detectCycleFromImport = detectCycleFromImport;
|
|
26
27
|
exports.findAllCircularDependencies = findAllCircularDependencies;
|
|
@@ -139,7 +140,7 @@ const TYPE_IMPORT_REGEX = /import\s+type\s/;
|
|
|
139
140
|
* Negative lookahead `(?!type[\s{])` excludes `export type ... from` —
|
|
140
141
|
* those are erased at compile time and don't cause runtime cycles.
|
|
141
142
|
*/
|
|
142
|
-
const EXPORT_FROM_REGEX = /
|
|
143
|
+
const EXPORT_FROM_REGEX = /export\s+\{[\s\S]*?\}\s+from\s+['"]([^'"]+)['"]/g;
|
|
143
144
|
/**
|
|
144
145
|
* Default extensions for import resolution.
|
|
145
146
|
* Module-level constant to avoid allocating a new array per resolve call.
|
|
@@ -464,16 +465,26 @@ function getFileImports(file, options) {
|
|
|
464
465
|
// Track resolved paths to dedupe across the three regexes (a single file
|
|
465
466
|
// may have both `import X from 'y'` and `export { X } from 'y'`).
|
|
466
467
|
const seen = new Set();
|
|
467
|
-
const pushImport = (importPath, dynamic = false) => {
|
|
468
|
+
const pushImport = (importPath, dynamic = false, typeOnly = false) => {
|
|
468
469
|
const resolved = resolveImportPath(importPath, resolveOpts);
|
|
469
470
|
if (resolved && fileExists(resolved, cache) && !seen.has(resolved)) {
|
|
470
471
|
seen.add(resolved);
|
|
471
|
-
|
|
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);
|
|
472
478
|
}
|
|
473
479
|
};
|
|
474
|
-
// Match ES6 static imports using pre-compiled regex
|
|
475
|
-
|
|
476
|
-
|
|
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);
|
|
487
|
+
}
|
|
477
488
|
// Match re-exports (`export … from '…'`). Without this, cycles routed
|
|
478
489
|
// through re-export-only files (a common pattern in barrel-heavy
|
|
479
490
|
// codebases) are invisible.
|
|
@@ -541,9 +552,19 @@ function hasOnlyTypeImports(cycle, cache) {
|
|
|
541
552
|
* @returns Array of SCCs (each SCC is an array of file paths)
|
|
542
553
|
*/
|
|
543
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 */ }
|
|
544
563
|
const { workspaceRoot, barrelExports, cache, resolverSettings } = options;
|
|
545
|
-
// If SCC
|
|
546
|
-
|
|
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)) {
|
|
547
568
|
return cache.sccs;
|
|
548
569
|
}
|
|
549
570
|
const state = {
|
|
@@ -559,9 +580,9 @@ function computeSCCsFromFile(startFile, options) {
|
|
|
559
580
|
const filesToProcess = [startFile];
|
|
560
581
|
// First pass: discover all reachable files (breadth-first for efficiency)
|
|
561
582
|
while (filesToProcess.length > 0) {
|
|
583
|
+
// pop() cannot return undefined here: the loop guard ensures the stack
|
|
584
|
+
// is non-empty and every pushed path is a non-empty string.
|
|
562
585
|
const file = filesToProcess.pop();
|
|
563
|
-
if (!file)
|
|
564
|
-
continue;
|
|
565
586
|
if (visited.has(file))
|
|
566
587
|
continue;
|
|
567
588
|
visited.add(file);
|
|
@@ -572,7 +593,11 @@ function computeSCCsFromFile(startFile, options) {
|
|
|
572
593
|
resolverSettings,
|
|
573
594
|
});
|
|
574
595
|
for (const imp of imports) {
|
|
575
|
-
|
|
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)) {
|
|
576
601
|
filesToProcess.push(imp.path);
|
|
577
602
|
}
|
|
578
603
|
}
|
|
@@ -584,20 +609,44 @@ function computeSCCsFromFile(startFile, options) {
|
|
|
584
609
|
}
|
|
585
610
|
}
|
|
586
611
|
// Convert to SCCResult format
|
|
587
|
-
const
|
|
612
|
+
const newResults = state.sccs.map((files) => ({
|
|
588
613
|
files,
|
|
589
614
|
hasCycle: files.length > 1,
|
|
590
615
|
}));
|
|
591
|
-
//
|
|
592
|
-
|
|
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);
|
|
593
622
|
cache.sccComputed = true;
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
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
|
+
}
|
|
598
647
|
}
|
|
599
648
|
}
|
|
600
|
-
return
|
|
649
|
+
return cache.sccs;
|
|
601
650
|
}
|
|
602
651
|
/**
|
|
603
652
|
* Tarjan's strong connect function (recursive part)
|
|
@@ -622,18 +671,26 @@ function tarjanStrongConnect(file, state, options, depth) {
|
|
|
622
671
|
resolverSettings,
|
|
623
672
|
});
|
|
624
673
|
for (const imp of imports) {
|
|
625
|
-
// Skip dynamic imports
|
|
626
|
-
|
|
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)
|
|
627
677
|
continue;
|
|
628
678
|
const successor = imp.path;
|
|
629
679
|
if (!state.indices.has(successor)) {
|
|
630
680
|
// Successor has not yet been visited; recurse on it
|
|
631
681
|
tarjanStrongConnect(successor, state, options, depth + 1);
|
|
632
|
-
state.lowlinks.set(file, Math.min(
|
|
682
|
+
state.lowlinks.set(file, Math.min(
|
|
683
|
+
// `file`'s lowlink is always set at function entry above.
|
|
684
|
+
state.lowlinks.get(file),
|
|
685
|
+
// The successor's lowlink can be missing when the recursive call
|
|
686
|
+
// returned early on the depth limit before initializing it.
|
|
687
|
+
state.lowlinks.get(successor) ?? 0));
|
|
633
688
|
}
|
|
634
689
|
else if (state.onStack.has(successor)) {
|
|
635
|
-
// Successor is in stack and hence in the current SCC
|
|
636
|
-
|
|
690
|
+
// Successor is in stack and hence in the current SCC.
|
|
691
|
+
// Both values are guaranteed: `file` got a lowlink at function entry,
|
|
692
|
+
// and an on-stack successor always received an index before pushing.
|
|
693
|
+
state.lowlinks.set(file, Math.min(state.lowlinks.get(file), state.indices.get(successor)));
|
|
637
694
|
}
|
|
638
695
|
}
|
|
639
696
|
const lowlink = state.lowlinks.get(file);
|
|
@@ -643,12 +700,13 @@ function tarjanStrongConnect(file, state, options, depth) {
|
|
|
643
700
|
const scc = [];
|
|
644
701
|
let w;
|
|
645
702
|
do {
|
|
703
|
+
// The stack always contains `file` at this point (it was pushed at
|
|
704
|
+
// function entry), so pop() cannot return undefined before the loop
|
|
705
|
+
// terminates at `w === file`.
|
|
646
706
|
w = state.stack.pop();
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
}
|
|
651
|
-
} while (w && w !== file);
|
|
707
|
+
state.onStack.delete(w);
|
|
708
|
+
scc.push(w);
|
|
709
|
+
} while (w !== file);
|
|
652
710
|
// Only store SCCs (single nodes aren't cycles, but we track them for completeness)
|
|
653
711
|
state.sccs.push(scc);
|
|
654
712
|
}
|
|
@@ -669,6 +727,56 @@ function isFileInCycle(file, cache) {
|
|
|
669
727
|
const scc = cache.sccs[sccIndex];
|
|
670
728
|
return scc && scc.hasCycle;
|
|
671
729
|
}
|
|
730
|
+
/**
|
|
731
|
+
* Find the shortest cycle path from targetFile back to sourceFile via BFS.
|
|
732
|
+
*
|
|
733
|
+
* This is the path-finding phase — called ONLY after the SCC pre-check
|
|
734
|
+
* confirms that sourceFile and targetFile are in the same SCC (meaning a
|
|
735
|
+
* cycle MUST exist). BFS guarantees the shortest cycle path is returned.
|
|
736
|
+
*
|
|
737
|
+
* Only traverses files within the same SCC (O(1) lookup per hop), so the
|
|
738
|
+
* worst-case work is proportional to the SCC size, not the entire graph.
|
|
739
|
+
*
|
|
740
|
+
* @param sourceFile - The file that contains the import (cycle closes here)
|
|
741
|
+
* @param targetFile - The file being imported (BFS starts here)
|
|
742
|
+
* @param options - Traversal options (workspaceRoot, barrelExports, cache, resolverSettings)
|
|
743
|
+
* @returns The cycle path [targetFile, ..., sourceFile], or null if not found
|
|
744
|
+
*/
|
|
745
|
+
function findShortestCyclePath(sourceFile, targetFile, options) {
|
|
746
|
+
const { workspaceRoot, barrelExports, cache, resolverSettings } = options;
|
|
747
|
+
const srcSCC = cache.sccIndex.get(sourceFile);
|
|
748
|
+
if (srcSCC === undefined)
|
|
749
|
+
return null;
|
|
750
|
+
// BFS from targetFile, looking for a path back to sourceFile
|
|
751
|
+
const queue = [[targetFile]];
|
|
752
|
+
const visited = new Set([targetFile]);
|
|
753
|
+
while (queue.length > 0) {
|
|
754
|
+
const path = queue.shift();
|
|
755
|
+
const current = path[path.length - 1];
|
|
756
|
+
const imports = getFileImports(current, {
|
|
757
|
+
workspaceRoot,
|
|
758
|
+
barrelExports,
|
|
759
|
+
cache,
|
|
760
|
+
resolverSettings,
|
|
761
|
+
});
|
|
762
|
+
for (const imp of imports) {
|
|
763
|
+
if (imp.dynamic || imp.typeOnly)
|
|
764
|
+
continue;
|
|
765
|
+
if (imp.path === sourceFile) {
|
|
766
|
+
// Found the cycle — return the complete path
|
|
767
|
+
return [...path, sourceFile];
|
|
768
|
+
}
|
|
769
|
+
if (visited.has(imp.path))
|
|
770
|
+
continue;
|
|
771
|
+
// Only traverse files in the same SCC — anything else cannot reach sourceFile
|
|
772
|
+
if (cache.sccIndex.get(imp.path) !== srcSCC)
|
|
773
|
+
continue;
|
|
774
|
+
visited.add(imp.path);
|
|
775
|
+
queue.push([...path, imp.path]);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
return null;
|
|
779
|
+
}
|
|
672
780
|
/**
|
|
673
781
|
* Get the cycle containing a specific file
|
|
674
782
|
*
|
|
@@ -708,6 +816,17 @@ function getCycleForFile(file, cache) {
|
|
|
708
816
|
* @returns Array of cycle paths found (each is an array of file paths ending at sourceFile)
|
|
709
817
|
*/
|
|
710
818
|
function detectCycleFromImport(sourceFile, targetFile, options) {
|
|
819
|
+
// Normalize paths: on macOS /tmp is a symlink to /private/tmp.
|
|
820
|
+
// ESLint provides the symlink path; resolved imports use the real path.
|
|
821
|
+
// Without normalization, `file === sourceFile` never matches in the DFS.
|
|
822
|
+
try {
|
|
823
|
+
sourceFile = (0, node_fs_1.realpathSync)(sourceFile);
|
|
824
|
+
}
|
|
825
|
+
catch { /* ignore */ }
|
|
826
|
+
try {
|
|
827
|
+
targetFile = (0, node_fs_1.realpathSync)(targetFile);
|
|
828
|
+
}
|
|
829
|
+
catch { /* ignore */ }
|
|
711
830
|
const { maxDepth, reportAllCycles, workspaceRoot, barrelExports, cache, resolverSettings, } = options;
|
|
712
831
|
// Fast path: target is known to not be in any cycle
|
|
713
832
|
if (cache.nonCyclicFiles.has(targetFile)) {
|
|
@@ -741,9 +860,10 @@ function detectCycleFromImport(sourceFile, targetFile, options) {
|
|
|
741
860
|
// Known non-cyclic from previous lint file visits
|
|
742
861
|
if (cache.nonCyclicFiles.has(file))
|
|
743
862
|
return;
|
|
744
|
-
//
|
|
745
|
-
|
|
746
|
-
|
|
863
|
+
// NOTE: no `!reportAllCycles && allCycles.length > 0` early-exit here —
|
|
864
|
+
// it would be unreachable: the import loop below performs the identical
|
|
865
|
+
// check (and breaks) before every recursive dfs() call, and the root
|
|
866
|
+
// call always happens before any cycle can have been recorded.
|
|
747
867
|
visited.add(file);
|
|
748
868
|
pathStack.push(file);
|
|
749
869
|
const imports = getFileImports(file, {
|
|
@@ -795,8 +915,18 @@ function detectCycleFromImport(sourceFile, targetFile, options) {
|
|
|
795
915
|
* @param visited - Set of fully explored files (internal use, persistent)
|
|
796
916
|
* @returns Array of cycles found (each cycle is an array of file paths)
|
|
797
917
|
*/
|
|
798
|
-
function findAllCircularDependencies(file, options, currentPath = [],
|
|
918
|
+
function findAllCircularDependencies(file, options, currentPath = [],
|
|
919
|
+
// NOTE: depth=0 is the root call; normalization happens only there.
|
|
920
|
+
depth = 0, visited = new Set()) {
|
|
799
921
|
const { maxDepth, reportAllCycles, workspaceRoot, barrelExports, cache, resolverSettings, } = options;
|
|
922
|
+
// Normalize to real path at the root call (depth=0 only — recursive calls
|
|
923
|
+
// already receive normalised paths from the resolver's import resolution).
|
|
924
|
+
if (depth === 0) {
|
|
925
|
+
try {
|
|
926
|
+
file = (0, node_fs_1.realpathSync)(file);
|
|
927
|
+
}
|
|
928
|
+
catch { /* ignore broken symlinks */ }
|
|
929
|
+
}
|
|
800
930
|
// OPTIMIZATION 1: Check if file is known to not be in any cycle
|
|
801
931
|
// This is populated across the lint run — once a file is confirmed
|
|
802
932
|
// non-cyclic, all future calls skip it in O(1).
|
|
@@ -830,10 +960,16 @@ function findAllCircularDependencies(file, options, currentPath = [], depth = 0,
|
|
|
830
960
|
if (currentPath.includes(file)) {
|
|
831
961
|
return [[...currentPath, file]];
|
|
832
962
|
}
|
|
833
|
-
// OPTIMIZATION 3: Skip if already
|
|
963
|
+
// OPTIMIZATION 3: Skip if already visited in this DFS run.
|
|
964
|
+
// `visited` is added BEFORE exploring children (below) to prevent
|
|
965
|
+
// infinite recursion through cyclic graphs. This is correct: if we
|
|
966
|
+
// encounter a file already on our `visited` set it means we've started
|
|
967
|
+
// exploring it from a sibling branch — we can safely skip it because
|
|
968
|
+
// the currentPath.includes() check above already handles the cycle case.
|
|
834
969
|
if (visited.has(file)) {
|
|
835
970
|
return [];
|
|
836
971
|
}
|
|
972
|
+
// Mark visited BEFORE recursing to prevent infinite loops on cyclic graphs.
|
|
837
973
|
visited.add(file);
|
|
838
974
|
const imports = getFileImports(file, {
|
|
839
975
|
workspaceRoot,
|
|
@@ -16,6 +16,14 @@ export interface ResolverDetectionResult {
|
|
|
16
16
|
* Detect available resolvers based on project structure
|
|
17
17
|
*/
|
|
18
18
|
export declare function detectResolvers(workspaceRoot: string): ResolverDetectionResult[];
|
|
19
|
+
/**
|
|
20
|
+
* Comparator for resolver priority ordering. Known resolver names sort by
|
|
21
|
+
* their position in the priority list; unknown names sort last (999).
|
|
22
|
+
*
|
|
23
|
+
* Extracted from `generateRecommendedConfig` so the unknown-name fallback is
|
|
24
|
+
* directly unit-testable (detectResolvers only ever emits known names).
|
|
25
|
+
*/
|
|
26
|
+
export declare function compareResolverPriority(aName: string, bName: string): number;
|
|
19
27
|
/**
|
|
20
28
|
* Generate recommended ESLint resolver configuration
|
|
21
29
|
*/
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.detectResolvers = detectResolvers;
|
|
9
|
+
exports.compareResolverPriority = compareResolverPriority;
|
|
9
10
|
exports.generateRecommendedConfig = generateRecommendedConfig;
|
|
10
11
|
exports.migrateFromEslintImport = migrateFromEslintImport;
|
|
11
12
|
exports.validateResolverConfig = validateResolverConfig;
|
|
@@ -77,6 +78,19 @@ function detectResolvers(workspaceRoot) {
|
|
|
77
78
|
});
|
|
78
79
|
return results;
|
|
79
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Comparator for resolver priority ordering. Known resolver names sort by
|
|
83
|
+
* their position in the priority list; unknown names sort last (999).
|
|
84
|
+
*
|
|
85
|
+
* Extracted from `generateRecommendedConfig` so the unknown-name fallback is
|
|
86
|
+
* directly unit-testable (detectResolvers only ever emits known names).
|
|
87
|
+
*/
|
|
88
|
+
function compareResolverPriority(aName, bName) {
|
|
89
|
+
const priorityOrder = ['typescript', 'webpack', 'vite', 'rollup', 'css'];
|
|
90
|
+
const aIndex = priorityOrder.indexOf(aName);
|
|
91
|
+
const bIndex = priorityOrder.indexOf(bName);
|
|
92
|
+
return (aIndex === -1 ? 999 : aIndex) - (bIndex === -1 ? 999 : bIndex);
|
|
93
|
+
}
|
|
80
94
|
/**
|
|
81
95
|
* Generate recommended ESLint resolver configuration
|
|
82
96
|
*/
|
|
@@ -93,12 +107,7 @@ function generateRecommendedConfig(workspaceRoot) {
|
|
|
93
107
|
}
|
|
94
108
|
// Prioritize resolvers
|
|
95
109
|
// oxlint-disable-next-line no-array-sort
|
|
96
|
-
const prioritized = availableResolvers.sort((a, b) =>
|
|
97
|
-
const priorityOrder = ['typescript', 'webpack', 'vite', 'rollup', 'css'];
|
|
98
|
-
const aIndex = priorityOrder.indexOf(a.name);
|
|
99
|
-
const bIndex = priorityOrder.indexOf(b.name);
|
|
100
|
-
return (aIndex === -1 ? 999 : aIndex) - (bIndex === -1 ? 999 : bIndex);
|
|
101
|
-
});
|
|
110
|
+
const prioritized = availableResolvers.sort((a, b) => compareResolverPriority(a.name, b.name));
|
|
102
111
|
// Create prioritized resolver configuration
|
|
103
112
|
const resolverConfig = {};
|
|
104
113
|
prioritized.forEach((resolver, index) => {
|
package/src/resolver/resolver.js
CHANGED
|
@@ -267,9 +267,3 @@ function clearResolverCache() {
|
|
|
267
267
|
oxcResolverCache.clear();
|
|
268
268
|
tsconfigPathCache.clear();
|
|
269
269
|
}
|
|
270
|
-
// Lines 322-324: Defensive error handling for truly exceptional cases
|
|
271
|
-
// These lines handle unexpected exceptions from resolution logic that are:
|
|
272
|
-
// 1. Difficult to reproduce in tests (would require mocking library functions to throw)
|
|
273
|
-
// 2. Represent proper defensive programming practices
|
|
274
|
-
// 3. Ensure the resolver never crashes with unhandled exceptions
|
|
275
|
-
// Coverage: Intentionally uncovered as these are edge case error paths
|
|
@@ -8,3 +8,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
8
8
|
const tslib_1 = require("tslib");
|
|
9
9
|
// Rule creation utilities for building ESLint rules
|
|
10
10
|
tslib_1.__exportStar(require("./rule-creator"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./mock-context"), exports);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
4
|
+
* MIT license that can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Test helper: run a rule's `create()` against a minimal mock context.
|
|
8
|
+
*
|
|
9
|
+
* Layer-2 testing utility for branches that a real parser can never produce
|
|
10
|
+
* (synthetic AST nodes with `parent: null`, inverted ranges, missing tokens).
|
|
11
|
+
* Call the returned `listeners` directly with synthetic nodes and assert on
|
|
12
|
+
* the recorded `reports`.
|
|
13
|
+
*/
|
|
14
|
+
import type { TSESLint } from '@typescript-eslint/utils';
|
|
15
|
+
/** Anything shaped like an ESLint rule module (`{ create(context) }`). */
|
|
16
|
+
export interface RuleLike {
|
|
17
|
+
create(context: never): Record<string, unknown>;
|
|
18
|
+
defaultOptions?: readonly unknown[];
|
|
19
|
+
}
|
|
20
|
+
/** Optional overrides for the mock context. */
|
|
21
|
+
export interface MockContextOptions {
|
|
22
|
+
options?: readonly unknown[];
|
|
23
|
+
settings?: TSESLint.SharedConfigurationSettings;
|
|
24
|
+
filename?: string;
|
|
25
|
+
/** Text returned by the `sourceCode.getText()` stub. */
|
|
26
|
+
sourceText?: string;
|
|
27
|
+
}
|
|
28
|
+
export interface MockContextResult {
|
|
29
|
+
/** Visitor listeners returned by `rule.create(context)`. */
|
|
30
|
+
listeners: Record<string, unknown>;
|
|
31
|
+
/** Every descriptor passed to `context.report`, in call order. */
|
|
32
|
+
reports: TSESLint.ReportDescriptor<string>[];
|
|
33
|
+
/** The mock context handed to `create()` (report recorder attached). */
|
|
34
|
+
context: TSESLint.RuleContext<string, readonly unknown[]>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Build a minimal rule context, call `rule.create(context)`, and return the
|
|
38
|
+
* listeners plus a recorder of every `context.report(...)` descriptor.
|
|
39
|
+
*/
|
|
40
|
+
export declare function createWithMockContext(rule: RuleLike, opts?: MockContextOptions): MockContextResult;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
+
* MIT license that can be found in the LICENSE file.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.createWithMockContext = createWithMockContext;
|
|
9
|
+
/**
|
|
10
|
+
* Build a minimal rule context, call `rule.create(context)`, and return the
|
|
11
|
+
* listeners plus a recorder of every `context.report(...)` descriptor.
|
|
12
|
+
*/
|
|
13
|
+
function createWithMockContext(rule, opts = {}) {
|
|
14
|
+
const reports = [];
|
|
15
|
+
const filename = opts.filename ?? 'mock.ts';
|
|
16
|
+
const emptyScope = { variables: [], references: [], childScopes: [] };
|
|
17
|
+
const sourceCode = {
|
|
18
|
+
text: opts.sourceText ?? '',
|
|
19
|
+
getText: () => opts.sourceText ?? '',
|
|
20
|
+
getScope: () => emptyScope,
|
|
21
|
+
getAncestors: () => [],
|
|
22
|
+
getCommentsBefore: () => [],
|
|
23
|
+
getDeclaredVariables: () => [],
|
|
24
|
+
};
|
|
25
|
+
const context = {
|
|
26
|
+
id: 'mock-rule',
|
|
27
|
+
filename,
|
|
28
|
+
physicalFilename: filename,
|
|
29
|
+
cwd: '/',
|
|
30
|
+
options: opts.options ?? rule.defaultOptions ?? [],
|
|
31
|
+
settings: opts.settings ?? {},
|
|
32
|
+
parserOptions: {},
|
|
33
|
+
languageOptions: {},
|
|
34
|
+
sourceCode,
|
|
35
|
+
getFilename: () => filename,
|
|
36
|
+
getPhysicalFilename: () => filename,
|
|
37
|
+
getCwd: () => '/',
|
|
38
|
+
getSourceCode: () => sourceCode,
|
|
39
|
+
getScope: () => emptyScope,
|
|
40
|
+
getAncestors: () => [],
|
|
41
|
+
report: (descriptor) => {
|
|
42
|
+
reports.push(descriptor);
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
const listeners = rule.create(context);
|
|
46
|
+
return { listeners, reports, context };
|
|
47
|
+
}
|
|
@@ -266,11 +266,9 @@ export type SafetyChecker = ReturnType<typeof createSafetyChecker>;
|
|
|
266
266
|
*
|
|
267
267
|
* ```typescript
|
|
268
268
|
* // Instead of this (3-5 lines each, 75+ occurrences):
|
|
269
|
-
* /* c8 ignore start -- safetyChecker requires JSDoc annotations not testable via RuleTester *\/
|
|
270
269
|
* if (safetyChecker.isSafe(node, context)) {
|
|
271
270
|
* return;
|
|
272
271
|
* }
|
|
273
|
-
* /* c8 ignore stop *\/
|
|
274
272
|
*
|
|
275
273
|
* // Use this (1 line):
|
|
276
274
|
* if (shouldSkipForSafety(safetyChecker, node, context)) return;
|
|
@@ -336,36 +336,11 @@ function hasSafeAnnotation(node, context, customAnnotations = []) {
|
|
|
336
336
|
}
|
|
337
337
|
current = current.parent;
|
|
338
338
|
}
|
|
339
|
-
//
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
current.type === 'ArrowFunctionExpression' ||
|
|
345
|
-
current.type === 'MethodDefinition') {
|
|
346
|
-
// Check for JSDoc comments
|
|
347
|
-
const comments = sourceCode.getCommentsBefore(current);
|
|
348
|
-
for (const comment of comments) {
|
|
349
|
-
if (comment.type === 'Block' && comment.value.startsWith('*')) {
|
|
350
|
-
// JSDoc comment
|
|
351
|
-
const commentText = comment.value.toLowerCase();
|
|
352
|
-
if (allAnnotations.some((ann) => commentText.includes(ann.toLowerCase()))) {
|
|
353
|
-
return true;
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
// Also check inline comments
|
|
358
|
-
const leadingComments = sourceCode.getCommentsBefore(current);
|
|
359
|
-
for (const comment of leadingComments) {
|
|
360
|
-
const commentText = comment.value.toLowerCase();
|
|
361
|
-
if (allAnnotations.some((ann) => commentText.includes(ann.toLowerCase()))) {
|
|
362
|
-
return true;
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
break; // Only check the innermost function
|
|
366
|
-
}
|
|
367
|
-
current = current.parent;
|
|
368
|
-
}
|
|
339
|
+
// NOTE: a second pass that re-checked the innermost function's JSDoc was
|
|
340
|
+
// removed as provably redundant: the walk above already inspects the
|
|
341
|
+
// comments before every ancestor on the same parent chain — including the
|
|
342
|
+
// containing function/method — before breaking at the function boundary,
|
|
343
|
+
// and with the same annotation predicate.
|
|
369
344
|
return false;
|
|
370
345
|
}
|
|
371
346
|
/**
|
|
@@ -515,11 +490,9 @@ function createSafetyChecker(options = {}) {
|
|
|
515
490
|
*
|
|
516
491
|
* ```typescript
|
|
517
492
|
* // Instead of this (3-5 lines each, 75+ occurrences):
|
|
518
|
-
* /* c8 ignore start -- safetyChecker requires JSDoc annotations not testable via RuleTester *\/
|
|
519
493
|
* if (safetyChecker.isSafe(node, context)) {
|
|
520
494
|
* return;
|
|
521
495
|
* }
|
|
522
|
-
* /* c8 ignore stop *\/
|
|
523
496
|
*
|
|
524
497
|
* // Use this (1 line):
|
|
525
498
|
* if (shouldSkipForSafety(safetyChecker, node, context)) return;
|
|
@@ -537,11 +510,9 @@ function createSafetyChecker(options = {}) {
|
|
|
537
510
|
* }
|
|
538
511
|
* ```
|
|
539
512
|
*/
|
|
540
|
-
/* c8 ignore start -- safetyChecker requires JSDoc annotations not testable via RuleTester */
|
|
541
513
|
function shouldSkipForSafety(safetyChecker, node, context) {
|
|
542
514
|
return safetyChecker.isSafe(node, context);
|
|
543
515
|
}
|
|
544
|
-
/* c8 ignore stop */
|
|
545
516
|
// ============================================================================
|
|
546
517
|
// AST TRAVERSAL UTILITIES
|
|
547
518
|
// ============================================================================
|