circle-ir-ai 4.37.0 → 4.40.0
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 +126 -0
- package/dist/agents/mastra/workflow.d.ts +16 -0
- package/dist/agents/mastra/workflow.d.ts.map +1 -1
- package/dist/agents/mastra/workflow.js +118 -22
- package/dist/agents/mastra/workflow.js.map +1 -1
- package/dist/blast-radius/import-map.d.ts +121 -0
- package/dist/blast-radius/import-map.d.ts.map +1 -0
- package/dist/blast-radius/import-map.js +735 -0
- package/dist/blast-radius/import-map.js.map +1 -0
- package/dist/blast-radius/index.d.ts +36 -4
- package/dist/blast-radius/index.d.ts.map +1 -1
- package/dist/blast-radius/index.js +397 -45
- package/dist/blast-radius/index.js.map +1 -1
- package/dist/blast-radius/radius.d.ts +304 -6
- package/dist/blast-radius/radius.d.ts.map +1 -1
- package/dist/blast-radius/radius.js +1547 -6
- package/dist/blast-radius/radius.js.map +1 -1
- package/dist/blast-radius/types.d.ts +79 -2
- package/dist/blast-radius/types.d.ts.map +1 -1
- package/dist/blast-radius/types.js +2 -2
- package/dist/cache/tree-cache.d.ts +10 -0
- package/dist/cache/tree-cache.d.ts.map +1 -1
- package/dist/cache/tree-cache.js +3 -1
- package/dist/cache/tree-cache.js.map +1 -1
- package/dist/cache/verify-cache.d.ts +7 -0
- package/dist/cache/verify-cache.d.ts.map +1 -1
- package/dist/cache/verify-cache.js +19 -2
- package/dist/cache/verify-cache.js.map +1 -1
- package/dist/dead-code/detector.d.ts +29 -2
- package/dist/dead-code/detector.d.ts.map +1 -1
- package/dist/dead-code/detector.js +6 -2
- package/dist/dead-code/detector.js.map +1 -1
- package/dist/dead-code/engine.d.ts +35 -1
- package/dist/dead-code/engine.d.ts.map +1 -1
- package/dist/dead-code/engine.js +247 -3
- package/dist/dead-code/engine.js.map +1 -1
- package/dist/dead-code/illata-client.d.ts +160 -0
- package/dist/dead-code/illata-client.d.ts.map +1 -0
- package/dist/dead-code/illata-client.js +291 -0
- package/dist/dead-code/illata-client.js.map +1 -0
- package/dist/dead-code/index.d.ts +1 -0
- package/dist/dead-code/index.d.ts.map +1 -1
- package/dist/dead-code/index.js +1 -0
- package/dist/dead-code/index.js.map +1 -1
- package/dist/dead-code/rules/dead-code-java.dl +132 -0
- package/dist/health-score/calculator.d.ts.map +1 -1
- package/dist/health-score/calculator.js +13 -5
- package/dist/health-score/calculator.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/llm/config.d.ts +21 -0
- package/dist/llm/config.d.ts.map +1 -1
- package/dist/llm/config.js +36 -0
- package/dist/llm/config.js.map +1 -1
- package/dist/llm/cwe-rubrics.d.ts +17 -0
- package/dist/llm/cwe-rubrics.d.ts.map +1 -0
- package/dist/llm/cwe-rubrics.js +51 -0
- package/dist/llm/cwe-rubrics.js.map +1 -0
- package/dist/llm/model-route-dispatcher.d.ts +17 -0
- package/dist/llm/model-route-dispatcher.d.ts.map +1 -1
- package/dist/llm/model-route-dispatcher.js +53 -3
- package/dist/llm/model-route-dispatcher.js.map +1 -1
- package/dist/llm/model-route.d.ts.map +1 -1
- package/dist/llm/model-route.js +4 -1
- package/dist/llm/model-route.js.map +1 -1
- package/dist/llm/verification.d.ts +1 -0
- package/dist/llm/verification.d.ts.map +1 -1
- package/dist/llm/verification.js +14 -6
- package/dist/llm/verification.js.map +1 -1
- package/dist/mcp/tools/blast-radius.d.ts +6 -4
- package/dist/mcp/tools/blast-radius.d.ts.map +1 -1
- package/dist/mcp/tools/blast-radius.js +53 -12
- package/dist/mcp/tools/blast-radius.js.map +1 -1
- package/dist/quality/calculator.d.ts.map +1 -1
- package/dist/quality/calculator.js +11 -2
- package/dist/quality/calculator.js.map +1 -1
- package/dist/scope/product-paths.d.ts +117 -12
- package/dist/scope/product-paths.d.ts.map +1 -1
- package/dist/scope/product-paths.js +422 -38
- package/dist/scope/product-paths.js.map +1 -1
- package/dist/secret-scan/scanner.d.ts +5 -0
- package/dist/secret-scan/scanner.d.ts.map +1 -1
- package/dist/secret-scan/scanner.js +74 -3
- package/dist/secret-scan/scanner.js.map +1 -1
- package/dist/security-scan/finding-labels.d.ts +3 -0
- package/dist/security-scan/finding-labels.d.ts.map +1 -0
- package/dist/security-scan/finding-labels.js +18 -0
- package/dist/security-scan/finding-labels.js.map +1 -0
- package/dist/security-scan/scanner.d.ts +32 -4
- package/dist/security-scan/scanner.d.ts.map +1 -1
- package/dist/security-scan/scanner.js +81 -13
- package/dist/security-scan/scanner.js.map +1 -1
- package/dist/selectors/exclude.d.ts +5 -1
- package/dist/selectors/exclude.d.ts.map +1 -1
- package/dist/selectors/exclude.js +15 -2
- package/dist/selectors/exclude.js.map +1 -1
- package/dist/skills/bundle-loader.d.ts.map +1 -1
- package/dist/skills/bundle-loader.js +6 -3
- package/dist/skills/bundle-loader.js.map +1 -1
- package/dist/spec-gap/analyzer.d.ts +8 -0
- package/dist/spec-gap/analyzer.d.ts.map +1 -1
- package/dist/spec-gap/analyzer.js +6 -3
- package/dist/spec-gap/analyzer.js.map +1 -1
- package/dist/specifica/binary-entries.d.ts +96 -0
- package/dist/specifica/binary-entries.d.ts.map +1 -0
- package/dist/specifica/binary-entries.js +318 -0
- package/dist/specifica/binary-entries.js.map +1 -0
- package/dist/specifica/environment/index.d.ts.map +1 -1
- package/dist/specifica/environment/index.js +5 -3
- package/dist/specifica/environment/index.js.map +1 -1
- package/dist/specifica/environment/read-code.d.ts +9 -2
- package/dist/specifica/environment/read-code.d.ts.map +1 -1
- package/dist/specifica/environment/read-code.js +12 -5
- package/dist/specifica/environment/read-code.js.map +1 -1
- package/dist/specifica/evidence-extractor.d.ts.map +1 -1
- package/dist/specifica/evidence-extractor.js +12 -1
- package/dist/specifica/evidence-extractor.js.map +1 -1
- package/dist/specifica/file-routes.d.ts.map +1 -1
- package/dist/specifica/file-routes.js +2 -1
- package/dist/specifica/file-routes.js.map +1 -1
- package/dist/specifica/generator.js +1 -0
- package/dist/specifica/generator.js.map +1 -1
- package/dist/specifica/learned-diff.d.ts.map +1 -1
- package/dist/specifica/learned-diff.js +3 -2
- package/dist/specifica/learned-diff.js.map +1 -1
- package/dist/specifica/level-design.d.ts +7 -0
- package/dist/specifica/level-design.d.ts.map +1 -1
- package/dist/specifica/level-design.js +10 -7
- package/dist/specifica/level-design.js.map +1 -1
- package/dist/specifica/library-surface-python-rust.d.ts +56 -0
- package/dist/specifica/library-surface-python-rust.d.ts.map +1 -0
- package/dist/specifica/library-surface-python-rust.js +466 -0
- package/dist/specifica/library-surface-python-rust.js.map +1 -0
- package/dist/specifica/library-surface.d.ts +13 -1
- package/dist/specifica/library-surface.d.ts.map +1 -1
- package/dist/specifica/library-surface.js +41 -1
- package/dist/specifica/library-surface.js.map +1 -1
- package/dist/specifica/modules.d.ts +28 -1
- package/dist/specifica/modules.d.ts.map +1 -1
- package/dist/specifica/modules.js +141 -10
- package/dist/specifica/modules.js.map +1 -1
- package/dist/specifica/spec-tree.d.ts +34 -1
- package/dist/specifica/spec-tree.d.ts.map +1 -1
- package/dist/specifica/spec-tree.js +242 -54
- package/dist/specifica/spec-tree.js.map +1 -1
- package/dist/specifica/toml-lines.d.ts +33 -0
- package/dist/specifica/toml-lines.d.ts.map +1 -0
- package/dist/specifica/toml-lines.js +77 -0
- package/dist/specifica/toml-lines.js.map +1 -0
- package/dist/specifica/tree-types.d.ts +2 -2
- package/dist/specifica/tree-types.d.ts.map +1 -1
- package/dist/specifica/tree-writer.d.ts +1 -0
- package/dist/specifica/tree-writer.d.ts.map +1 -1
- package/dist/specifica/tree-writer.js +4 -1
- package/dist/specifica/tree-writer.js.map +1 -1
- package/dist/specifica/ts-rule-sources.d.ts +10 -1
- package/dist/specifica/ts-rule-sources.d.ts.map +1 -1
- package/dist/specifica/ts-rule-sources.js +98 -1
- package/dist/specifica/ts-rule-sources.js.map +1 -1
- package/dist/specifica/types.d.ts +13 -0
- package/dist/specifica/types.d.ts.map +1 -1
- package/dist/specifica/use-cases.d.ts +78 -3
- package/dist/specifica/use-cases.d.ts.map +1 -1
- package/dist/specifica/use-cases.js +236 -12
- package/dist/specifica/use-cases.js.map +1 -1
- package/dist/trust/compare.d.ts.map +1 -1
- package/dist/trust/compare.js +11 -1
- package/dist/trust/compare.js.map +1 -1
- package/dist/trust/passes/ai-config-detect.d.ts +4 -0
- package/dist/trust/passes/ai-config-detect.d.ts.map +1 -1
- package/dist/trust/passes/ai-config-detect.js +23 -2
- package/dist/trust/passes/ai-config-detect.js.map +1 -1
- package/dist/trust/passes/ai-generated-fingerprint.d.ts.map +1 -1
- package/dist/trust/passes/ai-generated-fingerprint.js +2 -1
- package/dist/trust/passes/ai-generated-fingerprint.js.map +1 -1
- package/dist/trust/passes/code-complexity.d.ts.map +1 -1
- package/dist/trust/passes/code-complexity.js +2 -1
- package/dist/trust/passes/code-complexity.js.map +1 -1
- package/dist/trust/passes/conformance-attestation.d.ts.map +1 -1
- package/dist/trust/passes/conformance-attestation.js +2 -1
- package/dist/trust/passes/conformance-attestation.js.map +1 -1
- package/dist/trust/passes/cross-skill-interaction.d.ts.map +1 -1
- package/dist/trust/passes/cross-skill-interaction.js +3 -2
- package/dist/trust/passes/cross-skill-interaction.js.map +1 -1
- package/dist/trust/passes/data-exfiltration.d.ts.map +1 -1
- package/dist/trust/passes/data-exfiltration.js +2 -1
- package/dist/trust/passes/data-exfiltration.js.map +1 -1
- package/dist/trust/passes/dep-manifests.d.ts.map +1 -1
- package/dist/trust/passes/dep-manifests.js +2 -1
- package/dist/trust/passes/dep-manifests.js.map +1 -1
- package/dist/trust/passes/deprecated-usage.d.ts.map +1 -1
- package/dist/trust/passes/deprecated-usage.js +2 -1
- package/dist/trust/passes/deprecated-usage.js.map +1 -1
- package/dist/trust/passes/deprecation-no-replacement.d.ts.map +1 -1
- package/dist/trust/passes/deprecation-no-replacement.js +2 -1
- package/dist/trust/passes/deprecation-no-replacement.js.map +1 -1
- package/dist/trust/passes/doc-coverage.d.ts.map +1 -1
- package/dist/trust/passes/doc-coverage.js +2 -1
- package/dist/trust/passes/doc-coverage.js.map +1 -1
- package/dist/trust/passes/eu-ai-act-flags.d.ts.map +1 -1
- package/dist/trust/passes/eu-ai-act-flags.js +3 -2
- package/dist/trust/passes/eu-ai-act-flags.js.map +1 -1
- package/dist/trust/passes/excessive-permissions.d.ts.map +1 -1
- package/dist/trust/passes/excessive-permissions.js +2 -1
- package/dist/trust/passes/excessive-permissions.js.map +1 -1
- package/dist/trust/passes/inconsistent-error-handling.d.ts.map +1 -1
- package/dist/trust/passes/inconsistent-error-handling.js +2 -1
- package/dist/trust/passes/inconsistent-error-handling.js.map +1 -1
- package/dist/trust/passes/inconsistent-naming.d.ts.map +1 -1
- package/dist/trust/passes/inconsistent-naming.js +51 -12
- package/dist/trust/passes/inconsistent-naming.js.map +1 -1
- package/dist/trust/passes/inconsistent-param-order.d.ts.map +1 -1
- package/dist/trust/passes/inconsistent-param-order.js +2 -1
- package/dist/trust/passes/inconsistent-param-order.js.map +1 -1
- package/dist/trust/passes/missing-guard-dom.d.ts.map +1 -1
- package/dist/trust/passes/missing-guard-dom.js +2 -1
- package/dist/trust/passes/missing-guard-dom.js.map +1 -1
- package/dist/trust/passes/performance-passes.d.ts.map +1 -1
- package/dist/trust/passes/performance-passes.js +2 -1
- package/dist/trust/passes/performance-passes.js.map +1 -1
- package/dist/trust/passes/prompt-injection.d.ts.map +1 -1
- package/dist/trust/passes/prompt-injection.js +36 -4
- package/dist/trust/passes/prompt-injection.js.map +1 -1
- package/dist/trust/passes/throws-mismatch.d.ts.map +1 -1
- package/dist/trust/passes/throws-mismatch.js +2 -1
- package/dist/trust/passes/throws-mismatch.js.map +1 -1
- package/dist/trust/passes/tool-poisoning.d.ts.map +1 -1
- package/dist/trust/passes/tool-poisoning.js +3 -2
- package/dist/trust/passes/tool-poisoning.js.map +1 -1
- package/dist/trust/passes/unnecessary-object-hotspot.d.ts.map +1 -1
- package/dist/trust/passes/unnecessary-object-hotspot.js +2 -1
- package/dist/trust/passes/unnecessary-object-hotspot.js.map +1 -1
- package/dist/trust/scoring.d.ts.map +1 -1
- package/dist/trust/scoring.js +6 -2
- package/dist/trust/scoring.js.map +1 -1
- package/dist/utils/dirent-order.d.ts +21 -0
- package/dist/utils/dirent-order.d.ts.map +1 -0
- package/dist/utils/dirent-order.js +21 -0
- package/dist/utils/dirent-order.js.map +1 -0
- package/dist/utils/file-skip.d.ts +11 -0
- package/dist/utils/file-skip.d.ts.map +1 -1
- package/dist/utils/file-skip.js +29 -0
- package/dist/utils/file-skip.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,735 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Import specifiers the TypeScript call graph does not resolve by itself
|
|
3
|
+
* (it follows relative paths, and aliases only through one global
|
|
4
|
+
* `moduleAliases` map): tsconfig `paths` and `baseUrl` (`@/lib/fmt`) and
|
|
5
|
+
* workspace package names (`@repo/core/app-routes`, `@acme/core`). For the
|
|
6
|
+
* test walk only, each file's non-relative specifiers that name a file in
|
|
7
|
+
* the repository are rewritten to relative paths before its graph is
|
|
8
|
+
* built, the way TypeScript resolves them, so two apps can each have their
|
|
9
|
+
* own `@/`. Declaration ids do not change (they come from the file's path
|
|
10
|
+
* and declarations, not its import lines); `call_sites` keep coming from
|
|
11
|
+
* the unrewritten graph.
|
|
12
|
+
*
|
|
13
|
+
* - **tsconfig:** a file is governed by the nearest `tsconfig.json` above
|
|
14
|
+
* it, with or without `paths`, when its `files` / `include` / `exclude`
|
|
15
|
+
* take the file in, and by each config its `references` lead to that
|
|
16
|
+
* does (a solution file with `files: []` governs nothing itself), and by
|
|
17
|
+
* none if none does. When two that take it in send a specifier to
|
|
18
|
+
* different files, that specifier is not rewritten: which one applies
|
|
19
|
+
* depends on the tool that runs the tests. Each config's `extends` chain is followed (relative files, and
|
|
20
|
+
* package specifiers through `node_modules` inside the repository; an
|
|
21
|
+
* array is applied in order), `compilerOptions`, `files`, `include` and
|
|
22
|
+
* `exclude` merged, later wins. `baseUrl` is relative to the config that
|
|
23
|
+
* sets it; `paths` resolve against the effective `baseUrl`, else against
|
|
24
|
+
* the directory of the config that declares them; an exact key tries only
|
|
25
|
+
* its own targets. If a config in the chain cannot be read (and the config
|
|
26
|
+
* does not set both `baseUrl` and `paths` itself) and may take the file
|
|
27
|
+
* in, or no config governs the file, nothing in it is rewritten: its
|
|
28
|
+
* imports stay unresolved, so what they may reach is not known (sound).
|
|
29
|
+
* Whether such a config may take the file in is read from its own
|
|
30
|
+
* `include` (and `files`, `exclude`) when it sets `include` and extends
|
|
31
|
+
* only packages; otherwise it may. A reference to a config file that is
|
|
32
|
+
* not there is skipped.
|
|
33
|
+
* - **packages:** only the members of a declared workspace (a
|
|
34
|
+
* `package.json` `workspaces`, or a `pnpm-workspace.yaml`), for files
|
|
35
|
+
* inside that workspace, nearest workspace first. A name two members
|
|
36
|
+
* claim is not rewritten.
|
|
37
|
+
*
|
|
38
|
+
* Reads only files inside the repository (a path whose real location is
|
|
39
|
+
* outside it is not read), each at most 1 MB.
|
|
40
|
+
*/
|
|
41
|
+
import * as fs from 'fs';
|
|
42
|
+
import * as path from 'path';
|
|
43
|
+
import { minimatch } from 'minimatch';
|
|
44
|
+
const SKIP_DIRS = new Set(['node_modules', '.git', 'dist', 'build', 'coverage', '.next', 'vendor', 'target', '__pycache__']);
|
|
45
|
+
const EXTENSIONS = ['.ts', '.tsx', '.mts', '.cts', '.js', '.jsx', '.mjs', '.cjs'];
|
|
46
|
+
const MAX_CONFIG_BYTES = 1024 * 1024;
|
|
47
|
+
/** JSON with comments and trailing commas (tsconfig's dialect). */
|
|
48
|
+
export function parseJsonc(text) {
|
|
49
|
+
// Pass 1: drop comments, keeping strings as they are.
|
|
50
|
+
let noComments = '';
|
|
51
|
+
let inString = false;
|
|
52
|
+
for (let i = 0; i < text.length; i++) {
|
|
53
|
+
const c = text[i];
|
|
54
|
+
if (inString) {
|
|
55
|
+
noComments += c;
|
|
56
|
+
if (c === '\\') {
|
|
57
|
+
noComments += text[i + 1] ?? '';
|
|
58
|
+
i++;
|
|
59
|
+
}
|
|
60
|
+
else if (c === '"')
|
|
61
|
+
inString = false;
|
|
62
|
+
}
|
|
63
|
+
else if (c === '"') {
|
|
64
|
+
inString = true;
|
|
65
|
+
noComments += c;
|
|
66
|
+
}
|
|
67
|
+
else if (c === '/' && text[i + 1] === '/') {
|
|
68
|
+
while (i < text.length && text[i] !== '\n')
|
|
69
|
+
i++;
|
|
70
|
+
noComments += '\n';
|
|
71
|
+
}
|
|
72
|
+
else if (c === '/' && text[i + 1] === '*') {
|
|
73
|
+
i += 2;
|
|
74
|
+
while (i < text.length && !(text[i] === '*' && text[i + 1] === '/'))
|
|
75
|
+
i++;
|
|
76
|
+
i++;
|
|
77
|
+
}
|
|
78
|
+
else
|
|
79
|
+
noComments += c;
|
|
80
|
+
}
|
|
81
|
+
// Pass 2: drop a comma before `}` or `]`, outside strings.
|
|
82
|
+
let out = '';
|
|
83
|
+
inString = false;
|
|
84
|
+
for (let i = 0; i < noComments.length; i++) {
|
|
85
|
+
const c = noComments[i];
|
|
86
|
+
if (inString) {
|
|
87
|
+
out += c;
|
|
88
|
+
if (c === '\\') {
|
|
89
|
+
out += noComments[i + 1] ?? '';
|
|
90
|
+
i++;
|
|
91
|
+
}
|
|
92
|
+
else if (c === '"')
|
|
93
|
+
inString = false;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (c === '"')
|
|
97
|
+
inString = true;
|
|
98
|
+
if (c === ',') {
|
|
99
|
+
let j = i + 1;
|
|
100
|
+
while (j < noComments.length && /\s/.test(noComments[j]))
|
|
101
|
+
j++;
|
|
102
|
+
if (noComments[j] === '}' || noComments[j] === ']')
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
out += c;
|
|
106
|
+
}
|
|
107
|
+
return JSON.parse(out);
|
|
108
|
+
}
|
|
109
|
+
const isRecord = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
110
|
+
const norm = (p) => {
|
|
111
|
+
const n = path.posix.normalize(p.split(path.sep).join('/'));
|
|
112
|
+
return n === '.' ? '' : n.replace(/^\.\//, '').replace(/\/$/, '');
|
|
113
|
+
};
|
|
114
|
+
const inRoot = (rel) => rel !== '..' && !rel.startsWith('../') && !path.posix.isAbsolute(rel);
|
|
115
|
+
const underNodeModules = (rel) => /(^|\/)node_modules(\/|$)/.test(rel);
|
|
116
|
+
const dirOf = (rel) => norm(path.posix.dirname(rel));
|
|
117
|
+
const under = (file, dir) => dir === '' || file.startsWith(`${dir}/`);
|
|
118
|
+
/** Every target of an export, conditions in the object's own order (`{ import: { types, default } }` gives both). */
|
|
119
|
+
function exportTargets(v, depth = 0) {
|
|
120
|
+
if (typeof v === 'string')
|
|
121
|
+
return [v];
|
|
122
|
+
if (depth > 4)
|
|
123
|
+
return [];
|
|
124
|
+
if (Array.isArray(v))
|
|
125
|
+
return v.flatMap((x) => exportTargets(x, depth + 1));
|
|
126
|
+
if (!isRecord(v))
|
|
127
|
+
return [];
|
|
128
|
+
return Object.values(v).flatMap((x) => exportTargets(x, depth + 1));
|
|
129
|
+
}
|
|
130
|
+
/** Reads files inside the repository only (by real path), at most `MAX_CONFIG_BYTES`. */
|
|
131
|
+
class RepoReader {
|
|
132
|
+
root;
|
|
133
|
+
realRoot;
|
|
134
|
+
constructor(root) {
|
|
135
|
+
this.root = root;
|
|
136
|
+
let real = root;
|
|
137
|
+
try {
|
|
138
|
+
real = fs.realpathSync(root);
|
|
139
|
+
}
|
|
140
|
+
catch { /* keep root */ }
|
|
141
|
+
this.realRoot = real;
|
|
142
|
+
}
|
|
143
|
+
/** Root-relative path of an existing regular file, or null. */
|
|
144
|
+
file(rel) {
|
|
145
|
+
const r = norm(rel);
|
|
146
|
+
if (!inRoot(r))
|
|
147
|
+
return null;
|
|
148
|
+
try {
|
|
149
|
+
const real = fs.realpathSync(path.join(this.root, r));
|
|
150
|
+
if (real !== this.realRoot && !real.startsWith(this.realRoot + path.sep))
|
|
151
|
+
return null;
|
|
152
|
+
const st = fs.statSync(real);
|
|
153
|
+
return st.isFile() && st.size <= MAX_CONFIG_BYTES ? r : null;
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/** Whether anything is at this root-relative path (a file, a directory, a link, readable or not). */
|
|
160
|
+
exists(rel) {
|
|
161
|
+
const r = norm(rel);
|
|
162
|
+
if (!inRoot(r))
|
|
163
|
+
return true;
|
|
164
|
+
try {
|
|
165
|
+
fs.lstatSync(path.join(this.root, r));
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
text(rel) {
|
|
173
|
+
const r = this.file(rel);
|
|
174
|
+
if (r === null)
|
|
175
|
+
return null;
|
|
176
|
+
try {
|
|
177
|
+
return fs.readFileSync(path.join(this.root, r), 'utf-8');
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
json(rel) {
|
|
184
|
+
const t = this.text(rel);
|
|
185
|
+
if (t === null)
|
|
186
|
+
return undefined;
|
|
187
|
+
try {
|
|
188
|
+
return parseJsonc(t);
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/** Where an `extends` entry points, root-relative, or null when it cannot be found in the repository. */
|
|
196
|
+
function resolveExtends(reader, fromDir, spec) {
|
|
197
|
+
const candidates = (base) => base.endsWith('.json') ? [base] : [base, `${base}.json`, `${base}/tsconfig.json`];
|
|
198
|
+
if (spec.startsWith('.') || spec.startsWith('/')) {
|
|
199
|
+
if (spec.startsWith('/'))
|
|
200
|
+
return null;
|
|
201
|
+
for (const c of candidates(norm(path.posix.join(fromDir, spec)))) {
|
|
202
|
+
const f = reader.file(c);
|
|
203
|
+
if (f)
|
|
204
|
+
return f;
|
|
205
|
+
}
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
// A package specifier: the nearest node_modules above, inside the repository.
|
|
209
|
+
for (let dir = fromDir;; dir = dirOf(dir)) {
|
|
210
|
+
const nm = dir === '' ? 'node_modules' : `${dir}/node_modules`;
|
|
211
|
+
for (const c of candidates(`${nm}/${spec}`)) {
|
|
212
|
+
const f = reader.file(c);
|
|
213
|
+
if (f)
|
|
214
|
+
return f;
|
|
215
|
+
}
|
|
216
|
+
if (dir === '')
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const stringList = (v) => Array.isArray(v) ? v.filter((x) => typeof x === 'string') : undefined;
|
|
221
|
+
/**
|
|
222
|
+
* A config's options with its `extends` chain applied; null when a config in
|
|
223
|
+
* the chain cannot be read. An `extends` that cannot be read is let go only
|
|
224
|
+
* when the config itself sets both `baseUrl` and `paths` (nothing it could
|
|
225
|
+
* inherit would change how an import resolves).
|
|
226
|
+
*/
|
|
227
|
+
function effectiveOptions(reader, rel, seen, cache) {
|
|
228
|
+
if (cache.has(rel))
|
|
229
|
+
return cache.get(rel);
|
|
230
|
+
if (seen.has(rel))
|
|
231
|
+
return null;
|
|
232
|
+
const config = reader.json(rel);
|
|
233
|
+
let out = null;
|
|
234
|
+
if (isRecord(config)) {
|
|
235
|
+
const options = isRecord(config.compilerOptions) ? config.compilerOptions : {};
|
|
236
|
+
const ownBase = typeof options.baseUrl === 'string';
|
|
237
|
+
const ownPaths = isRecord(options.paths);
|
|
238
|
+
out = {};
|
|
239
|
+
const ext = config.extends;
|
|
240
|
+
const list = ext === undefined ? [] : typeof ext === 'string' ? [ext] : Array.isArray(ext) ? ext : null;
|
|
241
|
+
if (list === null)
|
|
242
|
+
out = null;
|
|
243
|
+
for (const e of list ?? []) {
|
|
244
|
+
if (out === null)
|
|
245
|
+
break;
|
|
246
|
+
const target = typeof e === 'string' ? resolveExtends(reader, dirOf(rel), e) : null;
|
|
247
|
+
const parent = target === null ? null : effectiveOptions(reader, target, new Set([...seen, rel]), cache);
|
|
248
|
+
if (parent === null) {
|
|
249
|
+
if (!(ownBase && ownPaths))
|
|
250
|
+
out = null;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
const { references: _notInherited, ...inherited } = parent;
|
|
254
|
+
out = { ...out, ...Object.fromEntries(Object.entries(inherited).filter(([, v]) => v !== undefined)) };
|
|
255
|
+
}
|
|
256
|
+
if (out !== null) {
|
|
257
|
+
const dir = dirOf(rel);
|
|
258
|
+
if (ownBase)
|
|
259
|
+
out.baseUrl = { value: options.baseUrl, dir };
|
|
260
|
+
if (ownPaths)
|
|
261
|
+
out.paths = { value: options.paths, dir };
|
|
262
|
+
for (const key of ['files', 'include', 'exclude']) {
|
|
263
|
+
const v = stringList(config[key]);
|
|
264
|
+
if (v)
|
|
265
|
+
out[key] = { value: v, dir };
|
|
266
|
+
}
|
|
267
|
+
out.references = ownReferences(reader, config, dir);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
cache.set(rel, out);
|
|
271
|
+
return out;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* A config's own `references`, as root-relative config paths: `null` for one
|
|
275
|
+
* that exists but cannot be read (it may take a file in), and none for one
|
|
276
|
+
* that names nothing (no tool can apply a config that is not there).
|
|
277
|
+
*/
|
|
278
|
+
function ownReferences(reader, config, dir) {
|
|
279
|
+
const refs = Array.isArray(config.references) ? config.references : [];
|
|
280
|
+
const out = [];
|
|
281
|
+
for (const r of refs) {
|
|
282
|
+
const p = isRecord(r) && typeof r.path === 'string' ? r.path : null;
|
|
283
|
+
if (p === null || p.startsWith('/')) {
|
|
284
|
+
out.push(null);
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
const base = norm(path.posix.join(dir, p));
|
|
288
|
+
const candidates = base.endsWith('.json') ? [base] : [`${base}/tsconfig.json`, `${base}.json`];
|
|
289
|
+
const found = candidates.map((c) => reader.file(c)).find((f) => f !== null);
|
|
290
|
+
if (found)
|
|
291
|
+
out.push(found);
|
|
292
|
+
else if (candidates.some((c) => reader.exists(c)))
|
|
293
|
+
out.push(null);
|
|
294
|
+
}
|
|
295
|
+
return out;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* For a config whose `extends` chain cannot be read: which files it may take
|
|
299
|
+
* in, from its own settings, when they decide it. That is when it sets
|
|
300
|
+
* `include` itself (an own `include`, `files` and `exclude` replace inherited
|
|
301
|
+
* ones) and extends only packages (`@tsconfig/node20/tsconfig.json`, not
|
|
302
|
+
* installed in a fresh clone): an inherited `files` would name files under
|
|
303
|
+
* the package's own folder, not the repository's, unless it writes
|
|
304
|
+
* `${configDir}`, which no one can see while the package is not installed:
|
|
305
|
+
* that residual is accepted (a published base that lists a project's test
|
|
306
|
+
* files is not expected). Without an own `exclude`, nothing is excluded
|
|
307
|
+
* (taking in more is the safe side). `null` otherwise.
|
|
308
|
+
*/
|
|
309
|
+
function coverageOnly(reader, rel) {
|
|
310
|
+
const config = reader.json(rel);
|
|
311
|
+
if (!isRecord(config) || stringList(config.include) === undefined)
|
|
312
|
+
return null;
|
|
313
|
+
const ext = config.extends;
|
|
314
|
+
const list = ext === undefined ? [] : typeof ext === 'string' ? [ext] : Array.isArray(ext) ? ext : null;
|
|
315
|
+
if (list === null || !list.every((e) => typeof e === 'string' && !e.startsWith('.') && !e.startsWith('/')))
|
|
316
|
+
return null;
|
|
317
|
+
const dir = dirOf(rel);
|
|
318
|
+
const own = (key) => {
|
|
319
|
+
const v = stringList(config[key]);
|
|
320
|
+
return v ? { value: v, dir } : undefined;
|
|
321
|
+
};
|
|
322
|
+
return {
|
|
323
|
+
...(own('files') ? { files: own('files') } : {}),
|
|
324
|
+
include: own('include'),
|
|
325
|
+
exclude: own('exclude') ?? { value: [], dir },
|
|
326
|
+
references: ownReferences(reader, config, dir),
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
/** A tsconfig glob as minimatch reads it: a name with no `*` and no extension is a directory. */
|
|
330
|
+
const CONFIG_DIR = '${configDir}';
|
|
331
|
+
/**
|
|
332
|
+
* A path a config writes, root-relative: relative to the config that writes
|
|
333
|
+
* it, or, when it holds `${configDir}` (TypeScript 5.5), with that replaced
|
|
334
|
+
* by the leaf config's directory (the one being built, whichever config in
|
|
335
|
+
* the `extends` chain wrote the path).
|
|
336
|
+
*/
|
|
337
|
+
function configPath(dir, entry, leaf) {
|
|
338
|
+
if (!entry.includes(CONFIG_DIR))
|
|
339
|
+
return norm(path.posix.join(dir, entry));
|
|
340
|
+
return norm(entry.split(CONFIG_DIR).join(leaf === '' ? '.' : leaf));
|
|
341
|
+
}
|
|
342
|
+
function tsGlob(dir, entry, leaf = dir) {
|
|
343
|
+
const g = configPath(dir, entry, leaf);
|
|
344
|
+
const last = g.slice(g.lastIndexOf('/') + 1);
|
|
345
|
+
if (last === '**')
|
|
346
|
+
return `${g}/*`;
|
|
347
|
+
if (!last.includes('*') && !last.includes('.'))
|
|
348
|
+
return g === '' ? '**/*' : `${g}/**/*`;
|
|
349
|
+
return g;
|
|
350
|
+
}
|
|
351
|
+
/** Whether `files` / `include` / `exclude` (TypeScript's defaults when unset) take in a file. */
|
|
352
|
+
function coverage(raw, configDir) {
|
|
353
|
+
const files = new Set((raw.files?.value ?? []).map((f) => configPath(raw.files.dir, f, configDir)));
|
|
354
|
+
const include = raw.include
|
|
355
|
+
? raw.include.value.map((g) => tsGlob(raw.include.dir, g, configDir))
|
|
356
|
+
: raw.files ? [] : [tsGlob(configDir, '**/*')];
|
|
357
|
+
const exclude = raw.exclude
|
|
358
|
+
? raw.exclude.value.map((g) => tsGlob(raw.exclude.dir, g, configDir))
|
|
359
|
+
: ['node_modules', 'bower_components', 'jspm_packages'].map((g) => tsGlob(configDir, g));
|
|
360
|
+
return (file) => files.has(file) || (include.some((g) => minimatch(file, g)) && !exclude.some((g) => minimatch(file, g)));
|
|
361
|
+
}
|
|
362
|
+
function compileOptions(raw, leaf) {
|
|
363
|
+
const baseUrl = raw.baseUrl ? configPath(raw.baseUrl.dir, raw.baseUrl.value, leaf) : undefined;
|
|
364
|
+
const base = baseUrl ?? raw.paths?.dir ?? '';
|
|
365
|
+
const exact = new Map();
|
|
366
|
+
const wildcards = [];
|
|
367
|
+
for (const [key, value] of Object.entries(raw.paths?.value ?? {})) {
|
|
368
|
+
// Targets outside the repository stay in the list (as unresolvable), so the key still claims the specifier.
|
|
369
|
+
const targets = (Array.isArray(value) ? value : []).filter((t) => typeof t === 'string').map((t) => configPath(base, t, leaf));
|
|
370
|
+
const star = key.indexOf('*');
|
|
371
|
+
if (star < 0)
|
|
372
|
+
exact.set(key, targets);
|
|
373
|
+
else
|
|
374
|
+
wildcards.push({ prefix: key.slice(0, star), suffix: key.slice(star + 1), targets });
|
|
375
|
+
}
|
|
376
|
+
// Longest prefix first, as TypeScript picks.
|
|
377
|
+
wildcards.sort((a, b) => b.prefix.length - a.prefix.length || (a.prefix < b.prefix ? -1 : a.prefix > b.prefix ? 1 : 0));
|
|
378
|
+
return { ...(baseUrl !== undefined && inRoot(baseUrl) ? { baseUrl } : {}), exact, wildcards };
|
|
379
|
+
}
|
|
380
|
+
/** `packages:` of a pnpm-workspace.yaml (a plain list; anything else is not read). */
|
|
381
|
+
function pnpmGlobs(text) {
|
|
382
|
+
const out = [];
|
|
383
|
+
let inPackages = false;
|
|
384
|
+
for (const line of text.split('\n')) {
|
|
385
|
+
if (/^packages\s*:/.test(line)) {
|
|
386
|
+
inPackages = true;
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
if (inPackages) {
|
|
390
|
+
const m = line.match(/^\s+-\s*['"]?([^'"#]+?)['"]?\s*(?:#.*)?$/);
|
|
391
|
+
if (m)
|
|
392
|
+
out.push(m[1].trim());
|
|
393
|
+
else if (/^\S/.test(line))
|
|
394
|
+
inPackages = false;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return out;
|
|
398
|
+
}
|
|
399
|
+
function workspaceGlobs(pkg) {
|
|
400
|
+
if (!isRecord(pkg))
|
|
401
|
+
return null;
|
|
402
|
+
const ws = pkg.workspaces;
|
|
403
|
+
const list = Array.isArray(ws) ? ws : isRecord(ws) && Array.isArray(ws.packages) ? ws.packages : null;
|
|
404
|
+
return list ? list.filter((g) => typeof g === 'string') : null;
|
|
405
|
+
}
|
|
406
|
+
function readPackage(reader, rel) {
|
|
407
|
+
const pkg = reader.json(rel);
|
|
408
|
+
if (!isRecord(pkg) || typeof pkg.name !== 'string' || pkg.name === '')
|
|
409
|
+
return null;
|
|
410
|
+
const exportsMap = new Map();
|
|
411
|
+
const ex = pkg.exports;
|
|
412
|
+
const put = (key, v) => {
|
|
413
|
+
// `null` closes a subpath: kept as an empty list, so it is not rewritten.
|
|
414
|
+
exportsMap.set(key, v === null ? [] : exportTargets(v));
|
|
415
|
+
};
|
|
416
|
+
if (typeof ex === 'string' || Array.isArray(ex))
|
|
417
|
+
put('.', ex);
|
|
418
|
+
else if (isRecord(ex)) {
|
|
419
|
+
const keys = Object.keys(ex);
|
|
420
|
+
if (keys.length > 0 && keys.every((k) => k.startsWith('.')))
|
|
421
|
+
for (const k of keys)
|
|
422
|
+
put(k, ex[k]);
|
|
423
|
+
else
|
|
424
|
+
put('.', ex);
|
|
425
|
+
}
|
|
426
|
+
const main = [pkg.module, pkg.main, pkg.types].find((v) => typeof v === 'string');
|
|
427
|
+
return { name: pkg.name, dir: dirOf(rel), exports: exportsMap, ...(main ? { main } : {}) };
|
|
428
|
+
}
|
|
429
|
+
/** Every `tsconfig.json` and every declared workspace under `root`. */
|
|
430
|
+
export function loadImportMap(root, maxDepth) {
|
|
431
|
+
const reader = new RepoReader(root);
|
|
432
|
+
const configFiles = [];
|
|
433
|
+
const packageFiles = [];
|
|
434
|
+
const pnpmFiles = [];
|
|
435
|
+
const visit = (rel, depth) => {
|
|
436
|
+
let entries;
|
|
437
|
+
try {
|
|
438
|
+
entries = fs.readdirSync(path.join(root, rel), { withFileTypes: true });
|
|
439
|
+
}
|
|
440
|
+
catch {
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
444
|
+
for (const e of entries) {
|
|
445
|
+
const childRel = rel ? `${rel}/${e.name}` : e.name;
|
|
446
|
+
if (e.isDirectory()) {
|
|
447
|
+
if (SKIP_DIRS.has(e.name) || e.name.startsWith('.') || depth >= maxDepth)
|
|
448
|
+
continue;
|
|
449
|
+
visit(childRel, depth + 1);
|
|
450
|
+
}
|
|
451
|
+
else if (e.isFile()) {
|
|
452
|
+
if (e.name === 'tsconfig.json')
|
|
453
|
+
configFiles.push(childRel);
|
|
454
|
+
else if (e.name === 'package.json')
|
|
455
|
+
packageFiles.push(childRel);
|
|
456
|
+
else if (e.name === 'pnpm-workspace.yaml')
|
|
457
|
+
pnpmFiles.push(childRel);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
visit('', 0);
|
|
462
|
+
const cache = new Map();
|
|
463
|
+
const built = new Map();
|
|
464
|
+
const effective = (rel, depth) => {
|
|
465
|
+
if (built.has(rel))
|
|
466
|
+
return built.get(rel);
|
|
467
|
+
if (depth > 4)
|
|
468
|
+
return null;
|
|
469
|
+
const raw = effectiveOptions(reader, rel, new Set(), cache);
|
|
470
|
+
const own = raw === null ? coverageOnly(reader, rel) : null;
|
|
471
|
+
if (raw === null && own === null)
|
|
472
|
+
return null;
|
|
473
|
+
const config = raw !== null
|
|
474
|
+
? { options: compileOptions(raw, dirOf(rel)), covers: coverage(raw, dirOf(rel)), references: [] }
|
|
475
|
+
: { options: null, covers: coverage(own, dirOf(rel)), references: [] };
|
|
476
|
+
built.set(rel, config);
|
|
477
|
+
config.references = ((raw ?? own).references ?? []).map((r) => (r === null ? null : effective(r, depth + 1)));
|
|
478
|
+
return config;
|
|
479
|
+
};
|
|
480
|
+
const tsconfigs = configFiles.map((rel) => ({ dir: dirOf(rel), config: effective(rel, 0) }));
|
|
481
|
+
// Declared workspaces and their members.
|
|
482
|
+
const declared = [];
|
|
483
|
+
for (const rel of packageFiles) {
|
|
484
|
+
const globs = workspaceGlobs(reader.json(rel));
|
|
485
|
+
if (globs && globs.length > 0)
|
|
486
|
+
declared.push({ dir: dirOf(rel), globs });
|
|
487
|
+
}
|
|
488
|
+
for (const rel of pnpmFiles) {
|
|
489
|
+
const text = reader.text(rel);
|
|
490
|
+
const globs = text ? pnpmGlobs(text) : [];
|
|
491
|
+
if (globs.length > 0)
|
|
492
|
+
declared.push({ dir: dirOf(rel), globs });
|
|
493
|
+
}
|
|
494
|
+
const workspaces = [];
|
|
495
|
+
for (const w of declared) {
|
|
496
|
+
const include = w.globs.filter((g) => !g.startsWith('!')).map((g) => norm(g));
|
|
497
|
+
const exclude = w.globs.filter((g) => g.startsWith('!')).map((g) => norm(g.slice(1)));
|
|
498
|
+
const byName = new Map();
|
|
499
|
+
for (const rel of packageFiles) {
|
|
500
|
+
const dir = dirOf(rel);
|
|
501
|
+
if (dir === w.dir || !under(dir, w.dir))
|
|
502
|
+
continue;
|
|
503
|
+
const inside = w.dir === '' ? dir : dir.slice(w.dir.length + 1);
|
|
504
|
+
if (!include.some((g) => minimatch(inside, g)) || exclude.some((g) => minimatch(inside, g)))
|
|
505
|
+
continue;
|
|
506
|
+
const pkg = readPackage(reader, rel);
|
|
507
|
+
if (!pkg)
|
|
508
|
+
continue;
|
|
509
|
+
byName.set(pkg.name, byName.has(pkg.name) ? null : pkg);
|
|
510
|
+
}
|
|
511
|
+
// Two declarations of one directory (package.json and pnpm-workspace.yaml) merge; a clash makes the name ambiguous.
|
|
512
|
+
const same = workspaces.find((x) => x.dir === w.dir);
|
|
513
|
+
if (same) {
|
|
514
|
+
for (const [name, pkg] of byName) {
|
|
515
|
+
const had = same.byName.get(name);
|
|
516
|
+
same.byName.set(name, had === undefined ? pkg : had !== null && pkg !== null && had.dir === pkg.dir ? had : null);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
else
|
|
520
|
+
workspaces.push({ dir: w.dir, byName });
|
|
521
|
+
}
|
|
522
|
+
const deepestFirst = (a, b) => b.dir.length - a.dir.length || (a.dir < b.dir ? -1 : a.dir > b.dir ? 1 : 0);
|
|
523
|
+
tsconfigs.sort(deepestFirst);
|
|
524
|
+
workspaces.sort(deepestFirst);
|
|
525
|
+
return { tsconfigs, workspaces };
|
|
526
|
+
}
|
|
527
|
+
export function importMapIsEmpty(map) {
|
|
528
|
+
const resolvesNothing = (c, depth = 0) => c === null || depth > 4 || ((c.options === null || (c.options.baseUrl === undefined && c.options.exact.size === 0 && c.options.wildcards.length === 0))
|
|
529
|
+
&& c.references.every((r) => resolvesNothing(r, depth + 1)));
|
|
530
|
+
return map.tsconfigs.every((c) => resolvesNothing(c.config)) && map.workspaces.every((w) => w.byName.size === 0);
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* The configs that may govern a file: the nearest `tsconfig.json` if its
|
|
534
|
+
* `files` / `include` / `exclude` take the file in, and every config its
|
|
535
|
+
* `references` lead to (depth first) that does. Which of them a tool uses
|
|
536
|
+
* depends on the tool (tsserver and vite-tsconfig-paths take the first,
|
|
537
|
+
* `tsc -b` builds the file in each, jest maps paths from the one its
|
|
538
|
+
* `moduleNameMapper` names), so `resolveSpecifier` rewrites a specifier
|
|
539
|
+
* only when they all resolve it to the same file. `null` when a config on
|
|
540
|
+
* the way cannot be read and may take the file in (with its own `paths`):
|
|
541
|
+
* then nothing in the file is rewritten. A config whose chain cannot be
|
|
542
|
+
* read but whose own `include` leaves the file out (`coverageOnly`, the
|
|
543
|
+
* Vite-style `tsconfig.node.json` extending an uninstalled package) does
|
|
544
|
+
* not stop it, and a reference to a config that is not there is skipped.
|
|
545
|
+
* Empty when none takes the file in.
|
|
546
|
+
*/
|
|
547
|
+
function governing(config, file) {
|
|
548
|
+
const out = [];
|
|
549
|
+
const seen = new Set();
|
|
550
|
+
const visit = (c, depth) => {
|
|
551
|
+
if (c === null || depth > 4)
|
|
552
|
+
return false;
|
|
553
|
+
if (seen.has(c))
|
|
554
|
+
return true;
|
|
555
|
+
seen.add(c);
|
|
556
|
+
if (c.covers(file)) {
|
|
557
|
+
// It takes the file in, and how it maps a specifier is not known.
|
|
558
|
+
if (c.options === null)
|
|
559
|
+
return false;
|
|
560
|
+
out.push(c);
|
|
561
|
+
}
|
|
562
|
+
return c.references.every((ref) => visit(ref, depth + 1));
|
|
563
|
+
};
|
|
564
|
+
return visit(config, 0) ? out : null;
|
|
565
|
+
}
|
|
566
|
+
/** Where a config's `paths` and `baseUrl` send a specifier, or `null`. */
|
|
567
|
+
function resolveByConfig(options, specifier, files) {
|
|
568
|
+
const exact = options.exact.get(specifier);
|
|
569
|
+
if (exact) {
|
|
570
|
+
for (const t of exact) {
|
|
571
|
+
const f = files.resolve(t);
|
|
572
|
+
if (f)
|
|
573
|
+
return f;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
else {
|
|
577
|
+
for (const p of options.wildcards) {
|
|
578
|
+
if (!specifier.startsWith(p.prefix) || !specifier.endsWith(p.suffix) || specifier.length < p.prefix.length + p.suffix.length)
|
|
579
|
+
continue;
|
|
580
|
+
const star = specifier.slice(p.prefix.length, specifier.length - p.suffix.length);
|
|
581
|
+
for (const t of p.targets) {
|
|
582
|
+
const f = files.resolve(t.replace('*', star));
|
|
583
|
+
if (f)
|
|
584
|
+
return f;
|
|
585
|
+
}
|
|
586
|
+
// TypeScript tries only the longest matching pattern (and an exact key only its own targets).
|
|
587
|
+
break;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
if (options.baseUrl !== undefined) {
|
|
591
|
+
const f = files.resolve(path.posix.join(options.baseUrl, specifier));
|
|
592
|
+
if (f)
|
|
593
|
+
return f;
|
|
594
|
+
}
|
|
595
|
+
return null;
|
|
596
|
+
}
|
|
597
|
+
/** Workspace packages, nearest workspace first; a name two members claim is left alone. */
|
|
598
|
+
function resolveInWorkspaces(fromFile, specifier, map, files) {
|
|
599
|
+
for (const w of map.workspaces) {
|
|
600
|
+
if (!under(fromFile, w.dir))
|
|
601
|
+
continue;
|
|
602
|
+
const name = [...w.byName.keys()].filter((n) => specifier === n || specifier.startsWith(`${n}/`)).sort((a, b) => b.length - a.length)[0];
|
|
603
|
+
if (name === undefined)
|
|
604
|
+
continue;
|
|
605
|
+
const pkg = w.byName.get(name);
|
|
606
|
+
if (!pkg)
|
|
607
|
+
return null;
|
|
608
|
+
return resolveInPackage(pkg, `.${specifier.slice(name.length)}`, files);
|
|
609
|
+
}
|
|
610
|
+
return null;
|
|
611
|
+
}
|
|
612
|
+
/** Resolves a root-relative candidate to a file as TypeScript would (`.ts` for `.js`, extensions, `index`). */
|
|
613
|
+
export class FileResolver {
|
|
614
|
+
root;
|
|
615
|
+
cache = new Map();
|
|
616
|
+
constructor(root) {
|
|
617
|
+
this.root = root;
|
|
618
|
+
}
|
|
619
|
+
isFile(rel) {
|
|
620
|
+
try {
|
|
621
|
+
return fs.statSync(path.join(this.root, rel)).isFile();
|
|
622
|
+
}
|
|
623
|
+
catch {
|
|
624
|
+
return false;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
resolve(candidate) {
|
|
628
|
+
const rel = norm(candidate);
|
|
629
|
+
if (!inRoot(rel) || underNodeModules(rel))
|
|
630
|
+
return null;
|
|
631
|
+
const hit = this.cache.get(rel);
|
|
632
|
+
if (hit !== undefined)
|
|
633
|
+
return hit;
|
|
634
|
+
const stems = [rel];
|
|
635
|
+
const js = rel.match(/^(.*)\.(?:m|c)?jsx?$/);
|
|
636
|
+
if (js)
|
|
637
|
+
stems.push(js[1]);
|
|
638
|
+
let found = null;
|
|
639
|
+
for (const stem of stems) {
|
|
640
|
+
if (/\.(?:[cm]?[jt]sx?)$/.test(stem) && this.isFile(stem)) {
|
|
641
|
+
found = stem;
|
|
642
|
+
break;
|
|
643
|
+
}
|
|
644
|
+
const ext = EXTENSIONS.find((x) => this.isFile(stem + x));
|
|
645
|
+
if (ext) {
|
|
646
|
+
found = stem + ext;
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
const index = EXTENSIONS.find((x) => this.isFile(`${stem}/index${x}`));
|
|
650
|
+
if (index) {
|
|
651
|
+
found = `${stem}/index${index}`;
|
|
652
|
+
break;
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
this.cache.set(rel, found);
|
|
656
|
+
return found;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
function resolveInPackage(pkg, sub, files) {
|
|
660
|
+
const inPkg = (t) => files.resolve(path.posix.join(pkg.dir, t));
|
|
661
|
+
const first = (targets, star) => {
|
|
662
|
+
for (const t of targets) {
|
|
663
|
+
const f = inPkg(star === undefined ? t : t.replace('*', star));
|
|
664
|
+
if (f)
|
|
665
|
+
return f;
|
|
666
|
+
}
|
|
667
|
+
return null;
|
|
668
|
+
};
|
|
669
|
+
// A subpath the map names: its own targets only (none for `null`).
|
|
670
|
+
const direct = pkg.exports.get(sub);
|
|
671
|
+
if (direct)
|
|
672
|
+
return first(direct);
|
|
673
|
+
for (const [key, targets] of pkg.exports) {
|
|
674
|
+
const star = key.indexOf('*');
|
|
675
|
+
if (star < 0)
|
|
676
|
+
continue;
|
|
677
|
+
const pre = key.slice(0, star);
|
|
678
|
+
const suf = key.slice(star + 1);
|
|
679
|
+
if (sub.startsWith(pre) && sub.endsWith(suf) && sub.length >= pre.length + suf.length) {
|
|
680
|
+
return first(targets, sub.slice(pre.length, sub.length - suf.length));
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
// With an exports map, a subpath it does not list cannot be imported.
|
|
684
|
+
if (pkg.exports.size > 0)
|
|
685
|
+
return null;
|
|
686
|
+
if (sub === '.')
|
|
687
|
+
return pkg.main ? inPkg(pkg.main) : inPkg('.');
|
|
688
|
+
return inPkg(sub);
|
|
689
|
+
}
|
|
690
|
+
/** Where a non-relative specifier in `fromFile` lands in the repository, or `null` (left as it is). */
|
|
691
|
+
export function resolveSpecifier(fromFile, specifier, map, files) {
|
|
692
|
+
if (specifier.startsWith('.') || specifier.startsWith('/') || /^[a-z][\w+.-]*:/i.test(specifier))
|
|
693
|
+
return null;
|
|
694
|
+
// The nearest tsconfig.json, or the configs it references that take the file in, govern the file.
|
|
695
|
+
// None (a solution file that includes nothing, a config that leaves the file out), or a chain that
|
|
696
|
+
// cannot be read: nothing in the file is rewritten. Configs that send the specifier to different
|
|
697
|
+
// files: it is not rewritten either (which one applies depends on the tool that runs the tests).
|
|
698
|
+
const nearest = map.tsconfigs.find((c) => under(fromFile, c.dir));
|
|
699
|
+
if (!nearest)
|
|
700
|
+
return resolveInWorkspaces(fromFile, specifier, map, files);
|
|
701
|
+
const configs = governing(nearest.config, fromFile);
|
|
702
|
+
if (configs === null || configs.length === 0)
|
|
703
|
+
return null;
|
|
704
|
+
let answer;
|
|
705
|
+
for (const c of configs) {
|
|
706
|
+
const r = resolveByConfig(c.options, specifier, files) ?? resolveInWorkspaces(fromFile, specifier, map, files);
|
|
707
|
+
if (answer === undefined)
|
|
708
|
+
answer = r;
|
|
709
|
+
else if (r !== answer)
|
|
710
|
+
return null;
|
|
711
|
+
}
|
|
712
|
+
return answer ?? null;
|
|
713
|
+
}
|
|
714
|
+
const SPECIFIER = /(\bfrom\s*|\bimport\s*\(\s*|\brequire\s*\(\s*|^[ \t]*import\s+)(['"])([^'"\n]+)\2/gm;
|
|
715
|
+
/**
|
|
716
|
+
* `code` with each non-relative import specifier that lands in the
|
|
717
|
+
* repository replaced by a relative path to the file, and the files it
|
|
718
|
+
* lands on. Lines are kept (only the specifiers change), so call sites keep
|
|
719
|
+
* their line numbers.
|
|
720
|
+
*/
|
|
721
|
+
export function rewriteImports(fromFile, code, map, files) {
|
|
722
|
+
const targets = [];
|
|
723
|
+
const out = code.replace(SPECIFIER, (whole, lead, quote, spec) => {
|
|
724
|
+
const target = resolveSpecifier(fromFile, spec, map, files);
|
|
725
|
+
if (!target)
|
|
726
|
+
return whole;
|
|
727
|
+
targets.push(target);
|
|
728
|
+
let rel = path.posix.relative(path.posix.dirname(fromFile), target);
|
|
729
|
+
if (!rel.startsWith('.'))
|
|
730
|
+
rel = `./${rel}`;
|
|
731
|
+
return `${lead}${quote}${rel}${quote}`;
|
|
732
|
+
});
|
|
733
|
+
return { code: out, targets };
|
|
734
|
+
}
|
|
735
|
+
//# sourceMappingURL=import-map.js.map
|