eslint-plugin-julia 0.0.0-stage → 0.3.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 +159 -0
- package/LICENSE +21 -0
- package/README.md +213 -2
- package/data/base-core-exports.json +5 -0
- package/data/public-only.json +35 -0
- package/data/stdlib-exports.json +180 -0
- package/package.json +76 -4
- package/src/index.js +226 -0
- package/src/language.js +46 -0
- package/src/macro-signatures.js +277 -0
- package/src/module-resolution.js +1187 -0
- package/src/orphans.js +631 -0
- package/src/parser/flags.js +25 -0
- package/src/parser/green-tree.js +41 -0
- package/src/parser/positions.js +29 -0
- package/src/parser/syntax-tree.js +91 -0
- package/src/parser/wasm-host.js +92 -0
- package/src/resolver/index.js +232 -0
- package/src/resolver/pkg-child.mjs +165 -0
- package/src/resolver/toml.js +63 -0
- package/src/resolver/warm.mjs +26 -0
- package/src/rules/common.js +52 -0
- package/src/rules/correctness-common.js +90 -0
- package/src/rules/growth-common.js +93 -0
- package/src/rules/idiom-common.js +116 -0
- package/src/rules/no-collapsible-if.js +38 -0
- package/src/rules/no-collect-before-range-iteration.js +24 -0
- package/src/rules/no-compare-negative-zero.js +50 -0
- package/src/rules/no-concat-growth-in-loop.js +18 -0
- package/src/rules/no-cond-assign.js +126 -0
- package/src/rules/no-const-reassign.js +61 -0
- package/src/rules/no-constant-condition.js +59 -0
- package/src/rules/no-debugger.js +96 -0
- package/src/rules/no-discarded-copy-result.js +50 -0
- package/src/rules/no-dupe-args.js +25 -0
- package/src/rules/no-dupe-elseif.js +57 -0
- package/src/rules/no-duplicate-dict-keys.js +95 -0
- package/src/rules/no-duplicate-keyword-arguments.js +40 -0
- package/src/rules/no-duplicate-logical-operands.js +85 -0
- package/src/rules/no-duplicate-method-signatures.js +90 -0
- package/src/rules/no-duplicate-struct-fields.js +38 -0
- package/src/rules/no-empty-block.js +49 -0
- package/src/rules/no-empty-catch.js +32 -0
- package/src/rules/no-empty-literal-range.js +26 -0
- package/src/rules/no-env-mutation.js +62 -0
- package/src/rules/no-eval-local-symbol.js +85 -0
- package/src/rules/no-fill-with-mutable.js +43 -0
- package/src/rules/no-full-slice-in-consumer.js +41 -0
- package/src/rules/no-identical-branches.js +44 -0
- package/src/rules/no-import-assign.js +64 -0
- package/src/rules/no-impossible-length-comparison.js +49 -0
- package/src/rules/no-invalid-foreign-call-signature.js +58 -0
- package/src/rules/no-isdefined-local-call.js +23 -0
- package/src/rules/no-loss-of-float-integer-precision.js +41 -0
- package/src/rules/no-nothing-return-conversion.js +127 -0
- package/src/rules/no-obvious-type-piracy.js +112 -0
- package/src/rules/no-orphan-file.js +39 -0
- package/src/rules/no-overwritten-literal-index.js +80 -0
- package/src/rules/no-reassigned-capture.js +107 -0
- package/src/rules/no-redundant-type-assertion.js +36 -0
- package/src/rules/no-redundant-union-members.js +42 -0
- package/src/rules/no-restricted-calls.js +74 -0
- package/src/rules/no-restricted-imports.js +105 -0
- package/src/rules/no-restricted-macros.js +61 -0
- package/src/rules/no-self-assign.js +65 -0
- package/src/rules/no-self-identity-comparison.js +37 -0
- package/src/rules/no-shadowed-loop-var.js +83 -0
- package/src/rules/no-show-macro.js +90 -0
- package/src/rules/no-single-iteration-loop.js +50 -0
- package/src/rules/no-string-growth-in-loop.js +18 -0
- package/src/rules/no-throw-literal.js +33 -0
- package/src/rules/no-unbound-type-parameters.js +91 -0
- package/src/rules/no-undef.js +284 -0
- package/src/rules/no-undefined-exports.js +60 -0
- package/src/rules/no-uninitialized-ref-read.js +38 -0
- package/src/rules/no-unreachable.js +82 -0
- package/src/rules/no-unsafe-finally.js +35 -0
- package/src/rules/no-unsafe-negation.js +77 -0
- package/src/rules/no-unused-expressions.js +137 -0
- package/src/rules/no-unused-type-parameters.js +55 -0
- package/src/rules/no-unused-vars.js +123 -0
- package/src/rules/no-use-before-define.js +139 -0
- package/src/rules/no-useless-assignment.js +75 -0
- package/src/rules/no-useless-catch.js +28 -0
- package/src/rules/parse-errors.js +19 -0
- package/src/rules/performance-common.js +119 -0
- package/src/rules/prefer-any.js +75 -0
- package/src/rules/prefer-const-global.js +103 -0
- package/src/rules/prefer-count-over-findall-length.js +46 -0
- package/src/rules/prefer-eachindex.js +120 -0
- package/src/rules/prefer-extreme-over-sort.js +73 -0
- package/src/rules/prefer-generator-sum.js +34 -0
- package/src/rules/prefer-identity-nothing.js +43 -0
- package/src/rules/prefer-in.js +67 -0
- package/src/rules/prefer-isa.js +70 -0
- package/src/rules/prefer-isempty.js +57 -0
- package/src/rules/prefer-ismissing.js +42 -0
- package/src/rules/prefer-parenthesized-interpolation.js +160 -0
- package/src/rules/prefer-size-dimension.js +19 -0
- package/src/rules/prefer-sized-push-buffer.js +81 -0
- package/src/rules/prefer-typed-empty-array.js +90 -0
- package/src/rules/require-typed-global.js +60 -0
- package/src/rules/restricted-common.js +211 -0
- package/src/rules/semantic-common.js +105 -0
- package/src/rules/use-isnan.js +28 -0
- package/src/scope.js +1338 -0
- package/src/source-code.js +100 -0
- package/types/ast.d.ts +49 -0
- package/types/index.d.ts +65 -0
- package/types/scope.d.ts +71 -0
- package/wasm/LICENSES/Julia.md +26 -0
- package/wasm/LICENSES/JuliaSyntax-MIT.md +46 -0
- package/wasm/LICENSES/WasmCodegen-MIT.txt +21 -0
- package/wasm/manifest.json +44 -0
- package/wasm/parser.wasm +0 -0
|
@@ -0,0 +1,1187 @@
|
|
|
1
|
+
// Syntactic module resolution only: never loads a package or executes Julia.
|
|
2
|
+
import { readFileSync, statSync, readdirSync, writeFileSync, mkdirSync, renameSync } from 'node:fs';
|
|
3
|
+
import { resolve, dirname, join, basename, delimiter, isAbsolute, relative, sep } from 'node:path';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import { createHash } from 'node:crypto';
|
|
6
|
+
import { parseJulia } from './parser/syntax-tree.js';
|
|
7
|
+
import { JuliaSourceCode } from './source-code.js';
|
|
8
|
+
import { getLocalScopeManager, identifierName } from './scope.js';
|
|
9
|
+
import { resolveEnvironment, temporaryName } from './resolver/index.js';
|
|
10
|
+
import { MANIFESTS } from './resolver/toml.js';
|
|
11
|
+
import { includesOf } from './orphans.js';
|
|
12
|
+
|
|
13
|
+
const posixPath = (path) => path.split(sep).join('/');
|
|
14
|
+
|
|
15
|
+
const inventory = JSON.parse(
|
|
16
|
+
readFileSync(new URL('../data/base-core-exports.json', import.meta.url)),
|
|
17
|
+
);
|
|
18
|
+
const stdlibs = JSON.parse(
|
|
19
|
+
readFileSync(new URL('../data/stdlib-exports.json', import.meta.url)),
|
|
20
|
+
).modules;
|
|
21
|
+
// Names that are `public` but not exported, per inventory key: `using M` doesn't bring them in.
|
|
22
|
+
const publicOnly = JSON.parse(
|
|
23
|
+
readFileSync(new URL('../data/public-only.json', import.meta.url)),
|
|
24
|
+
).modules;
|
|
25
|
+
const exportedOnly = (key, names) => {
|
|
26
|
+
const hidden = new Set(publicOnly[key] ?? []);
|
|
27
|
+
return new Set(names.filter((n) => !hidden.has(n)));
|
|
28
|
+
};
|
|
29
|
+
const factsCache = new Map(),
|
|
30
|
+
graphCache = new Map(),
|
|
31
|
+
tomlCache = new Map();
|
|
32
|
+
const hash = (text) => createHash('sha256').update(text).digest('hex');
|
|
33
|
+
// A file's version for the caches. Timestamps can't distinguish two same-size writes within
|
|
34
|
+
// one timestamp tick (coarse on some filesystems), so a recently modified file's version
|
|
35
|
+
// also includes its content hash, as git does for "racily clean" files.
|
|
36
|
+
const RACY_MS = 2000;
|
|
37
|
+
const stamp = (path) => {
|
|
38
|
+
let s;
|
|
39
|
+
try {
|
|
40
|
+
s = statSync(path);
|
|
41
|
+
} catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
const base = `${s.mtimeMs}:${s.ctimeMs}:${s.size}`;
|
|
45
|
+
if (!s.isFile() || Date.now() - Math.max(s.mtimeMs, s.ctimeMs) > RACY_MS) return base;
|
|
46
|
+
try {
|
|
47
|
+
return `${base}:${hash(readFileSync(path))}`;
|
|
48
|
+
} catch {
|
|
49
|
+
return base;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const exists = (path) => stamp(path) !== null;
|
|
53
|
+
const virtual = (path) => !path || /^<.*>$/u.test(path);
|
|
54
|
+
const textOf = (source, node) => source.getText(node).replace(/\s/gu, '');
|
|
55
|
+
|
|
56
|
+
// Only the scalar fields needed from Project/Manifest are read. Handles Manifest v1
|
|
57
|
+
// [[Name]] and v2 [[deps.Name]], including quoted table names. No TOML evaluation.
|
|
58
|
+
function toml(path, track) {
|
|
59
|
+
const version = stamp(path);
|
|
60
|
+
track?.set(path, version);
|
|
61
|
+
if (tomlCache.get(path)?.version === version) return tomlCache.get(path).value;
|
|
62
|
+
const result = { root: {}, packages: new Map(), deps: new Set() };
|
|
63
|
+
let current = result.root,
|
|
64
|
+
deps = false;
|
|
65
|
+
try {
|
|
66
|
+
for (const line of readFileSync(path, 'utf8').split(/\r?\n/u)) {
|
|
67
|
+
const table = line.match(/^\s*\[\[\s*(?:deps\.)?(?:"([^"]+)"|([\w-]+))\s*\]\]/u);
|
|
68
|
+
if (table) {
|
|
69
|
+
const name = table[1] ?? table[2];
|
|
70
|
+
current = {};
|
|
71
|
+
if (!result.packages.has(name)) result.packages.set(name, []);
|
|
72
|
+
result.packages.get(name).push(current);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (/^\s*\[/u.test(line)) {
|
|
76
|
+
current = {};
|
|
77
|
+
deps = /^\s*\[\s*deps\s*\]/u.test(line);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
// Project `[deps]` names, for deciding whether the environment can supply a package.
|
|
81
|
+
const dep = deps && line.match(/^\s*(?:"([^"]+)"|([\w-]+))\s*=/u);
|
|
82
|
+
if (dep) result.deps.add(dep[1] ?? dep[2]);
|
|
83
|
+
const field = line.match(
|
|
84
|
+
/^\s*(name|uuid|path|git-tree-sha1)\s*=\s*("(?:[^"\\]|\\.)*"|'[^']*')/u,
|
|
85
|
+
);
|
|
86
|
+
if (field) {
|
|
87
|
+
try {
|
|
88
|
+
current[field[1]] = field[2][0] === '"' ? JSON.parse(field[2]) : field[2].slice(1, -1);
|
|
89
|
+
} catch {
|
|
90
|
+
/* unsupported escape */
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
} catch {
|
|
95
|
+
/* missing/unreadable project is unresolved */
|
|
96
|
+
}
|
|
97
|
+
tomlCache.set(path, { version, value: result });
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
function upward(file) {
|
|
101
|
+
const dirs = [];
|
|
102
|
+
let at = dirname(file);
|
|
103
|
+
for (;;) {
|
|
104
|
+
dirs.push(at);
|
|
105
|
+
const parent = dirname(at);
|
|
106
|
+
if (parent === at) break;
|
|
107
|
+
at = parent;
|
|
108
|
+
}
|
|
109
|
+
return dirs;
|
|
110
|
+
}
|
|
111
|
+
function projectFor(file, track) {
|
|
112
|
+
for (const dir of upward(file)) {
|
|
113
|
+
const path = join(dir, 'Project.toml');
|
|
114
|
+
const p = toml(path, track).root;
|
|
115
|
+
if (p.name) return { dir, ...p };
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
function manifestFor(file, track) {
|
|
120
|
+
for (const dir of upward(file)) {
|
|
121
|
+
// Project and Manifest can live above a package's own Project.toml. Julia 1.13 prefers
|
|
122
|
+
// its versioned Manifest.
|
|
123
|
+
for (const name of MANIFESTS) {
|
|
124
|
+
const path = join(dir, name);
|
|
125
|
+
const data = toml(path, track);
|
|
126
|
+
if (exists(path)) return { path, ...data };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
function depots() {
|
|
132
|
+
// An empty JULIA_DEPOT_PATH component includes the default user depot.
|
|
133
|
+
return (process.env.JULIA_DEPOT_PATH ?? join(homedir(), '.julia'))
|
|
134
|
+
.split(delimiter)
|
|
135
|
+
.map((p) => p || join(homedir(), '.julia'))
|
|
136
|
+
.map((p) => resolve(p));
|
|
137
|
+
}
|
|
138
|
+
// The environment resolver (settings.julia.resolver): a throwaway, cached, source-only
|
|
139
|
+
// copy of the linted file's environment, asked for once, when a package isn't installed.
|
|
140
|
+
function environmentFor(file, settings, track) {
|
|
141
|
+
let projectFile = null;
|
|
142
|
+
for (const dir of upward(file)) {
|
|
143
|
+
const path = join(dir, 'Project.toml');
|
|
144
|
+
if (exists(path)) {
|
|
145
|
+
projectFile = path;
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (!projectFile) return null;
|
|
150
|
+
const dir = dirname(projectFile);
|
|
151
|
+
// Both candidates are tracked, present or not: creating or editing either changes the
|
|
152
|
+
// environment.
|
|
153
|
+
const candidates = MANIFESTS.map((m) => join(dir, m));
|
|
154
|
+
for (const candidate of candidates) track.set(candidate, stamp(candidate));
|
|
155
|
+
track.set(projectFile, stamp(projectFile));
|
|
156
|
+
const manifestFile = candidates.find(exists) ?? null;
|
|
157
|
+
const result = resolveEnvironment(
|
|
158
|
+
{ projectFile, manifestFile, packageName: projectFor(file, track)?.name },
|
|
159
|
+
settings,
|
|
160
|
+
);
|
|
161
|
+
if (result?.envDir) {
|
|
162
|
+
const marker = join(result.envDir, 'env.json');
|
|
163
|
+
track.set(marker, stamp(marker));
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
// Entries that came from the resolver's cache, with the folder of their export index.
|
|
168
|
+
const indexed = new Map();
|
|
169
|
+
function packageEntry(name, from, track, state = {}) {
|
|
170
|
+
const local = installedEntry(name, from, track);
|
|
171
|
+
if (local || !state.environment || !state.dependsOn?.(name)) return local;
|
|
172
|
+
const environment = state.environment();
|
|
173
|
+
const entry = environment?.packages?.[name]?.entry;
|
|
174
|
+
if (!entry || !exists(entry)) return null;
|
|
175
|
+
// Only the resolver's own, content-addressed copies are indexed (a `path` dependency is
|
|
176
|
+
// the developer's editable directory). Re-exports depend on the environment's versions,
|
|
177
|
+
// so the index is per environment.
|
|
178
|
+
const cache = environment.envDir && dirname(dirname(environment.envDir));
|
|
179
|
+
if (cache && entry.startsWith(join(cache, 'depot', 'packages') + sep))
|
|
180
|
+
indexed.set(entry, { dir: join(cache, 'index'), key: environment.key ?? environment.envDir });
|
|
181
|
+
return entry;
|
|
182
|
+
}
|
|
183
|
+
// The export index of a cached, immutable package source tree: its solved module tree
|
|
184
|
+
// (exports and submodules), so later runs don't parse the package again.
|
|
185
|
+
const INDEX_VERSION = 4;
|
|
186
|
+
const { version: pluginVersion } = JSON.parse(
|
|
187
|
+
readFileSync(new URL('../package.json', import.meta.url)),
|
|
188
|
+
);
|
|
189
|
+
const indexFile = (entry) => {
|
|
190
|
+
const { dir, key } = indexed.get(entry);
|
|
191
|
+
const id = [INDEX_VERSION, pluginVersion, stdlibs.julia ?? '', key, entry, variant].join('\0');
|
|
192
|
+
return join(dir, `${hash(id).slice(0, 24)}.json`);
|
|
193
|
+
};
|
|
194
|
+
// Keyed by the index file: the same package source can have different indexes per
|
|
195
|
+
// environment (its re-exports follow the environment's versions) and settings.
|
|
196
|
+
const indexMemo = new Map();
|
|
197
|
+
function loadIndex(entry) {
|
|
198
|
+
if (!indexed.has(entry)) return null;
|
|
199
|
+
const file = indexFile(entry);
|
|
200
|
+
if (indexMemo.has(file)) return indexMemo.get(file);
|
|
201
|
+
let data;
|
|
202
|
+
try {
|
|
203
|
+
data = JSON.parse(readFileSync(file, 'utf8'));
|
|
204
|
+
} catch {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
const build = (d, parent) => {
|
|
208
|
+
const m = moduleModel(d.name, parent);
|
|
209
|
+
d.exports.forEach((n) => m.exports.add(n));
|
|
210
|
+
m.unknownExports = Boolean(d.unknownExports);
|
|
211
|
+
// An index lists exports only, not every name the module defines.
|
|
212
|
+
m.indexed = true;
|
|
213
|
+
(d.generatedPrefixes ?? []).forEach((prefix) => m.generatedPrefixes.add(prefix));
|
|
214
|
+
for (const c of d.children) m.children.set(c.name, build(c, m));
|
|
215
|
+
return m;
|
|
216
|
+
};
|
|
217
|
+
const model =
|
|
218
|
+
data.version === INDEX_VERSION && data.entry === entry ? build(data.module, null) : null;
|
|
219
|
+
indexMemo.set(file, model);
|
|
220
|
+
return model;
|
|
221
|
+
}
|
|
222
|
+
// `sources`: the Julia files the solved model was built from. A model that read an editable
|
|
223
|
+
// source (a `path` dependency it re-exports) can change without its entry changing, so it is
|
|
224
|
+
// not indexed.
|
|
225
|
+
function storeIndex(entry, model, sources) {
|
|
226
|
+
if (!indexed.has(entry) || !model) return;
|
|
227
|
+
const packages = join(dirname(indexed.get(entry).dir), 'depot', 'packages') + sep;
|
|
228
|
+
if ([...sources].some((p) => p.endsWith('.jl') && !p.startsWith(packages))) return;
|
|
229
|
+
const dump = (m) => ({
|
|
230
|
+
name: m.name,
|
|
231
|
+
exports: [...m.exports],
|
|
232
|
+
unknownExports: m.unknownExports || m.dynamicExports,
|
|
233
|
+
generatedPrefixes: [...m.generatedPrefixes],
|
|
234
|
+
children: [...m.children.values()].map(dump),
|
|
235
|
+
});
|
|
236
|
+
try {
|
|
237
|
+
const file = indexFile(entry);
|
|
238
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
239
|
+
const temporary = temporaryName(file);
|
|
240
|
+
writeFileSync(
|
|
241
|
+
temporary,
|
|
242
|
+
JSON.stringify({ version: INDEX_VERSION, entry, module: dump(model) }),
|
|
243
|
+
);
|
|
244
|
+
renameSync(temporary, file);
|
|
245
|
+
} catch {
|
|
246
|
+
/* An unwritable cache only costs a re-parse. */
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
function installedEntry(name, from, track) {
|
|
250
|
+
const own = projectFor(from, track);
|
|
251
|
+
if (own?.name === name) return join(own.dir, 'src', `${name}.jl`);
|
|
252
|
+
const manifest = manifestFor(from, track);
|
|
253
|
+
const records = manifest?.packages.get(name);
|
|
254
|
+
if (!records?.length) return null;
|
|
255
|
+
// Duplicate dependency names cannot be disambiguated without a UUID dependency graph.
|
|
256
|
+
if (records.length !== 1) return null;
|
|
257
|
+
const record = records[0];
|
|
258
|
+
if (record.path) return join(resolve(dirname(manifest.path), record.path), 'src', `${name}.jl`);
|
|
259
|
+
if (!record.uuid) return null;
|
|
260
|
+
for (const depot of depots()) {
|
|
261
|
+
const dir = join(depot, 'packages', name);
|
|
262
|
+
track.set(dir, stamp(dir));
|
|
263
|
+
let slugs;
|
|
264
|
+
try {
|
|
265
|
+
slugs = readdirSync(dir).sort();
|
|
266
|
+
} catch {
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
const matches = slugs.filter(
|
|
270
|
+
(slug) => toml(join(dir, slug, 'Project.toml'), track).root.uuid === record.uuid,
|
|
271
|
+
);
|
|
272
|
+
// UUID scan is the documented alternative to Julia's version_slug. Refuse an
|
|
273
|
+
// ambiguous installation rather than arbitrarily choosing a package version.
|
|
274
|
+
if (matches.length === 1) return join(dir, matches[0], 'src', `${name}.jl`);
|
|
275
|
+
}
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
function literal(node, file) {
|
|
279
|
+
if (node?.kind === 'string' && node.children.every((c) => c.kind === 'String')) {
|
|
280
|
+
const value = node.children.map((c) => c.value).join('');
|
|
281
|
+
// Decode the common literal escapes; interpolation and unsupported escapes
|
|
282
|
+
// are deliberately unresolved (never interpret source as JavaScript code).
|
|
283
|
+
if (/\\(?![\\"nrt])/u.test(value)) return null;
|
|
284
|
+
return value.replace(/\\([\\"nrt])/gu, (_, c) => ({ n: '\n', r: '\r', t: '\t' })[c] ?? c);
|
|
285
|
+
}
|
|
286
|
+
if (
|
|
287
|
+
node?.kind === 'macrocall' &&
|
|
288
|
+
identifierName(node.children[0]) === '@__DIR__' &&
|
|
289
|
+
node.children.length === 1
|
|
290
|
+
)
|
|
291
|
+
return dirname(file);
|
|
292
|
+
if (node?.kind === 'call' && identifierName(node.children[0]) === 'joinpath') {
|
|
293
|
+
const pieces = node.children.slice(1).map((n) => literal(n, file));
|
|
294
|
+
if (pieces.length && pieces.every((p) => p !== null))
|
|
295
|
+
return pieces.reduce((path, p) => (isAbsolute(p) ? p : join(path, p)), '');
|
|
296
|
+
}
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
299
|
+
function facts(source, file) {
|
|
300
|
+
const manager = getLocalScopeManager(source, currentMacros),
|
|
301
|
+
modules = [];
|
|
302
|
+
const ids = new Map(
|
|
303
|
+
manager.scopes
|
|
304
|
+
.filter((s) => s.kind === 'module')
|
|
305
|
+
.map((s) => [s, s === manager.globalScope ? -1 : s.node.range[0]]),
|
|
306
|
+
);
|
|
307
|
+
for (const s of manager.scopes.filter((s) => s.kind === 'module')) {
|
|
308
|
+
modules.push({
|
|
309
|
+
id: ids.get(s),
|
|
310
|
+
parent: ids.get(s.parent?.module),
|
|
311
|
+
name: s === manager.globalScope ? null : identifierName(s.node.children[0]),
|
|
312
|
+
bare: s.bare,
|
|
313
|
+
// Module-level names an unknown macro declares (`@verbosity_specifier V begin … end`)
|
|
314
|
+
// are visible to sibling files too.
|
|
315
|
+
names: [...s.bindings.values(), ...(s.macroBindings?.values() ?? [])].map((b) => ({
|
|
316
|
+
name: b.name,
|
|
317
|
+
kind: b.kind,
|
|
318
|
+
})),
|
|
319
|
+
exports: [],
|
|
320
|
+
imports: [],
|
|
321
|
+
includes: [],
|
|
322
|
+
includeMacros: [],
|
|
323
|
+
importMacros: [],
|
|
324
|
+
topMacros: [],
|
|
325
|
+
macroIncludes: [],
|
|
326
|
+
reads: [],
|
|
327
|
+
unresolvedIncludes: false,
|
|
328
|
+
dynamicIncludes: false,
|
|
329
|
+
dynamicDefinitions: false,
|
|
330
|
+
dynamicExports: false,
|
|
331
|
+
dynamicEvaluation: false,
|
|
332
|
+
generatedPrefixes: [],
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
const byId = new Map(modules.map((m) => [m.id, m]));
|
|
336
|
+
for (const r of manager.references)
|
|
337
|
+
if (r.read && (!r.binding || r.binding.scope?.kind === 'module'))
|
|
338
|
+
byId.get(ids.get(r.scope.module))?.reads.push(r.name);
|
|
339
|
+
for (const d of manager.definitions)
|
|
340
|
+
if (['function', 'macro'].includes(d.kind) && d.binding?.scope.kind === 'module')
|
|
341
|
+
byId.get(ids.get(d.binding.scope))?.reads.push(d.name);
|
|
342
|
+
function walk(node, macros = []) {
|
|
343
|
+
const s = manager.scopeFor(node),
|
|
344
|
+
m = byId.get(ids.get(s?.module));
|
|
345
|
+
// A literal call to a source-visible include wrapper can share its context.
|
|
346
|
+
// Record syntax only; the macro is never expanded or executed.
|
|
347
|
+
if (m && node.kind === 'macro') {
|
|
348
|
+
const [signature, body] = node.children;
|
|
349
|
+
// A macro that builds `using`/`import` statements (`Expr(:using, …)`) imports names
|
|
350
|
+
// nothing here can list.
|
|
351
|
+
const generatesImports = (n) =>
|
|
352
|
+
(n.kind === 'call' &&
|
|
353
|
+
identifierName(n.children[0]) === 'Expr' &&
|
|
354
|
+
n.children[1]?.kind === 'quote' &&
|
|
355
|
+
['using', 'import'].includes(identifierName(n.children[1].children[0]))) ||
|
|
356
|
+
(n.kind === 'quote' && n.children.some((c) => ['using', 'import'].includes(c.kind))) ||
|
|
357
|
+
n.children?.some(generatesImports);
|
|
358
|
+
let macroName = signature;
|
|
359
|
+
while (['call', 'where'].includes(macroName?.kind)) macroName = macroName.children[0];
|
|
360
|
+
if (body && identifierName(macroName) && generatesImports(body))
|
|
361
|
+
m.importMacros.push(`@${identifierName(macroName)}`);
|
|
362
|
+
if (signature?.kind === 'call' && signature.children.length === 2) {
|
|
363
|
+
const parameter = signature.children[1];
|
|
364
|
+
const name = identifierName(parameter.kind === '::' ? parameter.children[0] : parameter);
|
|
365
|
+
const forwards = (n, depth = 0) => {
|
|
366
|
+
if (['inert', 'function', 'macro', '$'].includes(n.kind)) return false;
|
|
367
|
+
if (
|
|
368
|
+
n.kind === 'call' &&
|
|
369
|
+
depth === 1 &&
|
|
370
|
+
identifierName(n.children[0]) === 'include' &&
|
|
371
|
+
n.children.length === 2 &&
|
|
372
|
+
n.children[1].kind === '$' &&
|
|
373
|
+
identifierName(n.children[1].children[0]) === name
|
|
374
|
+
)
|
|
375
|
+
return true;
|
|
376
|
+
return n.children.some((child) => forwards(child, depth + (n.kind === 'quote' ? 1 : 0)));
|
|
377
|
+
};
|
|
378
|
+
const returned = body?.children.at(-1);
|
|
379
|
+
if (name && returned?.kind === 'quote' && forwards(returned))
|
|
380
|
+
m.includeMacros.push(`@${identifierName(signature.children[0])}`);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if (m && node.kind === 'macrocall') {
|
|
384
|
+
let head = node.children[0];
|
|
385
|
+
if (s?.kind === 'module' && identifierName(head)) m.topMacros.push(identifierName(head));
|
|
386
|
+
const path =
|
|
387
|
+
node.children.length === 2 && s?.kind === 'module' ? literal(node.children[1], file) : null;
|
|
388
|
+
if (path !== null && !virtual(file))
|
|
389
|
+
m.macroIncludes.push({ name: identifierName(head), path: resolve(dirname(file), path) });
|
|
390
|
+
if (
|
|
391
|
+
identifierName(head) === '@eval' ||
|
|
392
|
+
(head?.kind === '.' && identifierName(head.children.at(-1)) === '@eval')
|
|
393
|
+
) {
|
|
394
|
+
m.dynamicEvaluation = true;
|
|
395
|
+
const code = node.children.at(-1);
|
|
396
|
+
const own = code?.kind === '$' ? namesImport(node, source, m) : null;
|
|
397
|
+
if (own) m.imports.push({ ...own, file });
|
|
398
|
+
else if (
|
|
399
|
+
code?.kind === '$' ||
|
|
400
|
+
(code?.kind === 'block' && code.children.some((c) => c.kind === '$'))
|
|
401
|
+
) {
|
|
402
|
+
m.dynamicDefinitions = true;
|
|
403
|
+
if (node.children.length === 3) evalInto.push([m, identifierName(node.children[1])]);
|
|
404
|
+
}
|
|
405
|
+
generatedNames(m, node.children.slice(1), source, node);
|
|
406
|
+
}
|
|
407
|
+
if (head?.kind === 'StrMacroName') m.reads.push(`@${head.value}_str`);
|
|
408
|
+
else if (identifierName(head)) m.reads.push(identifierName(head));
|
|
409
|
+
else if (head?.kind === '.') {
|
|
410
|
+
while (head?.kind === '.') head = head.children[0];
|
|
411
|
+
if (identifierName(head)) m.reads.push(identifierName(head));
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
// Only exported names come in with `using M`; `public` ones need qualification.
|
|
415
|
+
if (m && node.kind === 'export' && !macros.length)
|
|
416
|
+
m.exports.push(...node.children.map(identifierName).filter(Boolean));
|
|
417
|
+
if (m && ['using', 'import'].includes(node.kind)) {
|
|
418
|
+
for (const clause of node.children) {
|
|
419
|
+
const named = clause.kind === ':';
|
|
420
|
+
const path = named
|
|
421
|
+
? clause.children[0]
|
|
422
|
+
: clause.kind === 'as'
|
|
423
|
+
? clause.children[0]
|
|
424
|
+
: clause;
|
|
425
|
+
m.imports.push({
|
|
426
|
+
path: textOf(source, path),
|
|
427
|
+
kind: node.kind,
|
|
428
|
+
alias: clause.kind === 'as' ? textOf(source, clause.children[1]) : null,
|
|
429
|
+
wildcard: !named,
|
|
430
|
+
reexport: macros.includes('@reexport'),
|
|
431
|
+
file,
|
|
432
|
+
names: named
|
|
433
|
+
? clause.children
|
|
434
|
+
.slice(1)
|
|
435
|
+
.map((p) => textOf(source, p.kind === 'as' ? p.children[1] : p))
|
|
436
|
+
: [],
|
|
437
|
+
sourceNames: named
|
|
438
|
+
? clause.children
|
|
439
|
+
.slice(1)
|
|
440
|
+
.map((p) => textOf(source, p.kind === 'as' ? p.children[0] : p))
|
|
441
|
+
: [],
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
if (m && node.kind === 'call') {
|
|
446
|
+
const head = node.children[0];
|
|
447
|
+
const name =
|
|
448
|
+
identifierName(head) ?? (head?.kind === '.' ? identifierName(head.children.at(-1)) : null);
|
|
449
|
+
if (['eval', 'include_string'].includes(name)) {
|
|
450
|
+
m.dynamicEvaluation = true;
|
|
451
|
+
generatedNames(m, node.children.slice(1), source, node);
|
|
452
|
+
// `Core.eval(Sub, ex)` with computed code: `Sub` gets definitions (and exports)
|
|
453
|
+
// nothing here can list.
|
|
454
|
+
const code = node.children.at(-1);
|
|
455
|
+
if (
|
|
456
|
+
name === 'eval' &&
|
|
457
|
+
node.children.length === 3 &&
|
|
458
|
+
!['quote', 'inert'].includes(code?.kind)
|
|
459
|
+
)
|
|
460
|
+
evalInto.push([m, identifierName(node.children[1])]);
|
|
461
|
+
// `eval(make_def(op))` (or `Core.eval(@__MODULE__, ex)`) outside a function: computed
|
|
462
|
+
// definitions in this module, as `@eval $(…)`. A literal quote without `$` defines
|
|
463
|
+
// only what it spells.
|
|
464
|
+
const hasDollar = (n) => n.kind === '$' || n.children?.some(hasDollar);
|
|
465
|
+
const self =
|
|
466
|
+
node.children.length === 2 ||
|
|
467
|
+
(node.children.length === 3 &&
|
|
468
|
+
node.children[1].kind === 'macrocall' &&
|
|
469
|
+
identifierName(node.children[1].children[0]) === '@__MODULE__');
|
|
470
|
+
let inFunction = false;
|
|
471
|
+
for (let at = s; at && at.kind !== 'module'; at = at.parent)
|
|
472
|
+
if (at.kind === 'function') inFunction = true;
|
|
473
|
+
if (
|
|
474
|
+
name === 'eval' &&
|
|
475
|
+
self &&
|
|
476
|
+
!inFunction &&
|
|
477
|
+
code &&
|
|
478
|
+
(!['quote', 'inert'].includes(code.kind) || hasDollar(code))
|
|
479
|
+
)
|
|
480
|
+
m.dynamicDefinitions = true;
|
|
481
|
+
}
|
|
482
|
+
if (name === 'include') {
|
|
483
|
+
// `include` evaluates the file at module scope, also from a `@testset`, `let`, loop
|
|
484
|
+
// or conditional block; only one inside a function body runs at an unknown time.
|
|
485
|
+
let topLevel = false;
|
|
486
|
+
for (let at = s; at; at = at.parent) {
|
|
487
|
+
if (at.kind === 'module') topLevel = true;
|
|
488
|
+
if (['module', 'function'].includes(at.kind)) break;
|
|
489
|
+
}
|
|
490
|
+
const path =
|
|
491
|
+
topLevel && identifierName(head) === 'include' && node.children.length === 2
|
|
492
|
+
? literal(node.children[1], file)
|
|
493
|
+
: null;
|
|
494
|
+
if (path !== null && !virtual(file))
|
|
495
|
+
m.includes.push({ path: resolve(dirname(file), path), literal: path });
|
|
496
|
+
else {
|
|
497
|
+
m.unresolvedIncludes = true;
|
|
498
|
+
// A computed path, or an include run inside a function or block: its file's
|
|
499
|
+
// globals are unknowable here.
|
|
500
|
+
if (!virtual(file)) m.dynamicIncludes = true;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
// Quotes are data, including their export/include-looking syntax.
|
|
505
|
+
if (['quote', 'inert'].includes(node.kind)) return;
|
|
506
|
+
// Qualified macros (`Reexport.@reexport`) count by their own name.
|
|
507
|
+
const head = node.children[0];
|
|
508
|
+
const macro = head?.kind === '.' ? identifierName(head.children.at(-1)) : identifierName(head);
|
|
509
|
+
const next = node.kind === 'macrocall' ? [...macros, macro] : macros;
|
|
510
|
+
node.children.forEach((n) => walk(n, next));
|
|
511
|
+
}
|
|
512
|
+
const evalInto = [];
|
|
513
|
+
walk(source.ast);
|
|
514
|
+
for (const [m, name] of evalInto) {
|
|
515
|
+
const target = modules.find((t) => t.parent === m.id && t.name === name);
|
|
516
|
+
if (target) {
|
|
517
|
+
target.dynamicDefinitions = true;
|
|
518
|
+
target.dynamicExports = true;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return modules;
|
|
522
|
+
}
|
|
523
|
+
// `let names = names(M; all = true) … ex = Expr(:using, …) … @eval Mod $ex end`: a generated
|
|
524
|
+
// `using M: <every name M defines>`. `names(M; all = true)` lists M's own definitions, not
|
|
525
|
+
// the names M imports.
|
|
526
|
+
function namesImport(node, source, m) {
|
|
527
|
+
const call = (n, name) =>
|
|
528
|
+
n.kind === 'call' &&
|
|
529
|
+
(identifierName(n.children[0]) === name ||
|
|
530
|
+
(n.children[0]?.kind === '.' && identifierName(n.children[0].children.at(-1)) === name));
|
|
531
|
+
for (let at = source.getParent(node); at; at = source.getParent(at)) {
|
|
532
|
+
if (['module', 'function', 'macro'].includes(at.kind)) return null;
|
|
533
|
+
if (at.kind !== 'let') continue;
|
|
534
|
+
let target = null,
|
|
535
|
+
using = false;
|
|
536
|
+
const scan = (n) => {
|
|
537
|
+
if (call(n, 'names') && /\ball\s*=\s*true\b/u.test(source.getText(n)))
|
|
538
|
+
target ??= textOf(source, n.children[1]);
|
|
539
|
+
if (
|
|
540
|
+
call(n, 'Expr') &&
|
|
541
|
+
n.children[1]?.kind === 'quote' &&
|
|
542
|
+
identifierName(n.children[1].children[0]) === 'using'
|
|
543
|
+
)
|
|
544
|
+
using = true;
|
|
545
|
+
n.children?.forEach(scan);
|
|
546
|
+
};
|
|
547
|
+
scan(at);
|
|
548
|
+
if (!target || !using || !/^[\p{L}_][\p{L}\p{N}_.!]*$/u.test(target)) return null;
|
|
549
|
+
const alias = m.imports.find((imp) => imp.alias === target);
|
|
550
|
+
return {
|
|
551
|
+
path: alias ? alias.path : target,
|
|
552
|
+
kind: 'using',
|
|
553
|
+
alias: null,
|
|
554
|
+
wildcard: false,
|
|
555
|
+
ownNames: true,
|
|
556
|
+
reexport: false,
|
|
557
|
+
names: [],
|
|
558
|
+
sourceNames: [],
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
return null;
|
|
562
|
+
}
|
|
563
|
+
// The interpolations that name what generated code defines (`@eval $fname(x) = …`,
|
|
564
|
+
// `function $(Symbol(:get_, p))(…)`, `const $name = …`).
|
|
565
|
+
function interpolatedNames(node, out = []) {
|
|
566
|
+
if (!node) return out;
|
|
567
|
+
const named = (n) => {
|
|
568
|
+
while (['where', '::', 'curly', 'call'].includes(n?.kind)) n = n.children[0];
|
|
569
|
+
return n?.kind === '$' ? n : null;
|
|
570
|
+
};
|
|
571
|
+
const kinds = ['function', 'macro', 'struct', 'abstract', 'primitive', '=', 'const', 'global'];
|
|
572
|
+
const name = kinds.includes(node.kind) ? named(node.children[0]) : null;
|
|
573
|
+
if (name) out.push(name);
|
|
574
|
+
// `@eval export $(name)`: generated exports.
|
|
575
|
+
if (['export', 'public'].includes(node.kind))
|
|
576
|
+
out.push(...node.children.filter((c) => c.kind === '$'));
|
|
577
|
+
node.children?.forEach((c) => interpolatedNames(c, out));
|
|
578
|
+
return out;
|
|
579
|
+
}
|
|
580
|
+
// The symbols `$f` takes when an enclosing `for f in (:a, :b)` iterates literal symbols.
|
|
581
|
+
function loopSymbols(interpolation, source) {
|
|
582
|
+
const variable = identifierName(interpolation.children[0]);
|
|
583
|
+
if (!variable) return null;
|
|
584
|
+
for (let at = source.getParent(interpolation); at; at = source.getParent(at)) {
|
|
585
|
+
if (at.kind !== 'for') continue;
|
|
586
|
+
const clause = at.children[0];
|
|
587
|
+
for (const pair of clause?.kind === 'iteration' ? clause.children : []) {
|
|
588
|
+
if (identifierName(pair.children[0]) !== variable) continue;
|
|
589
|
+
const values = pair.children[1];
|
|
590
|
+
if (!['tuple', 'vect'].includes(values?.kind)) return null;
|
|
591
|
+
const names = values.children.map((v) =>
|
|
592
|
+
v.kind === 'quote' && v.children.length === 1 ? identifierName(v.children[0]) : null,
|
|
593
|
+
);
|
|
594
|
+
return names.every(Boolean) ? names : null;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
return null;
|
|
598
|
+
}
|
|
599
|
+
// Generated code under interpolated names: literal-loop names are defined, others unknowable.
|
|
600
|
+
// `fname = Symbol(:get_, prop)` before `@eval $fname(…) = …`: generated names that all start
|
|
601
|
+
// with `get_`. Only names with that prefix are unknown.
|
|
602
|
+
function symbolPrefix(interpolation, source, from) {
|
|
603
|
+
const variable = identifierName(interpolation.children[0]);
|
|
604
|
+
if (!variable) return null;
|
|
605
|
+
for (let at = source.getParent(from); at; at = source.getParent(at)) {
|
|
606
|
+
if (['module', 'function', 'macro'].includes(at.kind)) return null;
|
|
607
|
+
if (!['for', 'let', 'block'].includes(at.kind)) continue;
|
|
608
|
+
for (const statement of at.children) {
|
|
609
|
+
if (statement.kind !== '=' || identifierName(statement.children[0]) !== variable) continue;
|
|
610
|
+
const value = statement.children[1];
|
|
611
|
+
if (value?.kind !== 'call' || identifierName(value.children[0]) !== 'Symbol') return null;
|
|
612
|
+
const first = value.children[1];
|
|
613
|
+
const text =
|
|
614
|
+
first?.kind === 'quote' && first.children.length === 1
|
|
615
|
+
? identifierName(first.children[0])
|
|
616
|
+
: first?.kind === 'string' && first.children.every((c) => c.kind === 'String')
|
|
617
|
+
? first.children.map((c) => c.value).join('')
|
|
618
|
+
: null;
|
|
619
|
+
// A prefix too short to say anything (`Symbol(:_, x)`) is as good as unknown.
|
|
620
|
+
return text && text.length >= 3 && value.children.length > 2 ? text : null;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
return null;
|
|
624
|
+
}
|
|
625
|
+
function generatedNames(m, args, source, from) {
|
|
626
|
+
for (const interpolation of args.flatMap((c) => interpolatedNames(c))) {
|
|
627
|
+
const names = loopSymbols(interpolation, source);
|
|
628
|
+
const prefix = names || !from ? null : symbolPrefix(interpolation, source, from);
|
|
629
|
+
if (names) m.names.push(...names.map((name) => ({ name, kind: 'function' })));
|
|
630
|
+
else if (prefix) m.generatedPrefixes.push(prefix);
|
|
631
|
+
else m.dynamicDefinitions = true;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
function fileFacts(file, graph) {
|
|
635
|
+
const version = stamp(file);
|
|
636
|
+
graph.versions.set(file, version);
|
|
637
|
+
if (graph.overlay?.file === file) return graph.overlay.facts;
|
|
638
|
+
const cached = factsCache.get(file);
|
|
639
|
+
if (cached?.version === version && cached.variant === variant) return cached.facts;
|
|
640
|
+
let result = [],
|
|
641
|
+
digest;
|
|
642
|
+
if (version) {
|
|
643
|
+
try {
|
|
644
|
+
const text = readFileSync(file, 'utf8'),
|
|
645
|
+
parsed = parseJulia(text);
|
|
646
|
+
digest = hash(text);
|
|
647
|
+
if (!parsed.diagnostics.some((d) => d.level === 1))
|
|
648
|
+
result = facts(new JuliaSourceCode(text, parsed), file);
|
|
649
|
+
} catch {
|
|
650
|
+
/* unreadable/invalid dependency contributes no names */
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
factsCache.set(file, { version, variant, facts: result, hash: digest });
|
|
654
|
+
return result;
|
|
655
|
+
}
|
|
656
|
+
function moduleModel(name, parent, bare = false) {
|
|
657
|
+
return {
|
|
658
|
+
name,
|
|
659
|
+
parent,
|
|
660
|
+
bare,
|
|
661
|
+
names: new Map(),
|
|
662
|
+
children: new Map(),
|
|
663
|
+
includeMacros: new Set(),
|
|
664
|
+
importMacros: new Set(),
|
|
665
|
+
topMacros: new Set(),
|
|
666
|
+
exports: new Set(),
|
|
667
|
+
imports: [],
|
|
668
|
+
unknown: new Set(),
|
|
669
|
+
unknownExports: false,
|
|
670
|
+
reads: new Set(),
|
|
671
|
+
unresolvedIncludes: false,
|
|
672
|
+
dynamicIncludes: false,
|
|
673
|
+
dynamicDefinitions: false,
|
|
674
|
+
dynamicExports: false,
|
|
675
|
+
dynamicEvaluation: false,
|
|
676
|
+
// Generated definitions known only by a prefix (`get_…`), own or brought in by imports.
|
|
677
|
+
generatedPrefixes: new Set(),
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
function addFile(graph, file, into, active = new Set()) {
|
|
681
|
+
// Path-based stack also stops cycles that enter a freshly nested module.
|
|
682
|
+
if (active.has(file)) return true;
|
|
683
|
+
const modules = fileFacts(file, graph);
|
|
684
|
+
if (!modules.length) return false;
|
|
685
|
+
const next = new Set(active).add(file),
|
|
686
|
+
map = new Map([[-1, into]]);
|
|
687
|
+
if (!graph.files.has(file)) graph.files.set(file, []);
|
|
688
|
+
for (const fact of modules) {
|
|
689
|
+
let m = into;
|
|
690
|
+
if (fact.id !== -1) {
|
|
691
|
+
const parent = map.get(fact.parent) ?? into;
|
|
692
|
+
if (!parent.children.has(fact.name))
|
|
693
|
+
parent.children.set(fact.name, moduleModel(fact.name, parent, fact.bare));
|
|
694
|
+
m = parent.children.get(fact.name);
|
|
695
|
+
map.set(fact.id, m);
|
|
696
|
+
}
|
|
697
|
+
graph.files.get(file).push({ id: fact.id, module: m });
|
|
698
|
+
for (const b of fact.names) m.names.set(b.name, b.kind);
|
|
699
|
+
for (const name of fact.exports) m.exports.add(name);
|
|
700
|
+
m.imports.push(...fact.imports.map((imp) => ({ ...imp })));
|
|
701
|
+
fact.reads.forEach((name) => m.reads.add(name));
|
|
702
|
+
m.unresolvedIncludes ||= fact.unresolvedIncludes;
|
|
703
|
+
m.dynamicIncludes ||= fact.dynamicIncludes;
|
|
704
|
+
m.dynamicDefinitions ||= fact.dynamicDefinitions;
|
|
705
|
+
m.dynamicExports ||= fact.dynamicExports;
|
|
706
|
+
fact.generatedPrefixes.forEach((prefix) => m.generatedPrefixes.add(prefix));
|
|
707
|
+
m.dynamicEvaluation ||= fact.dynamicEvaluation;
|
|
708
|
+
fact.includeMacros.forEach((name) => m.includeMacros.add(name));
|
|
709
|
+
fact.importMacros.forEach((name) => m.importMacros.add(name));
|
|
710
|
+
fact.topMacros.forEach((name) => m.topMacros.add(name));
|
|
711
|
+
for (const include of fact.includes) {
|
|
712
|
+
let included = include.path;
|
|
713
|
+
// Base's bootstrap include is cwd-relative, before it installs Julia's
|
|
714
|
+
// ordinary per-file include. Recognize its base/ rooted literal paths.
|
|
715
|
+
if (graph.baseDir && !isAbsolute(include.literal)) {
|
|
716
|
+
graph.versions.set(included, stamp(included));
|
|
717
|
+
if (!exists(included)) included = resolve(graph.baseDir, include.literal);
|
|
718
|
+
}
|
|
719
|
+
if (!addFile(graph, included, m, next)) m.unresolvedIncludes = true;
|
|
720
|
+
}
|
|
721
|
+
for (const include of fact.macroIncludes)
|
|
722
|
+
if (m.includeMacros.has(include.name)) {
|
|
723
|
+
if (!addFile(graph, include.path, m, next)) m.unresolvedIncludes = true;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
return true;
|
|
727
|
+
}
|
|
728
|
+
// Solved graphs depend on the resolver settings of the analysis that built them.
|
|
729
|
+
let variant = '';
|
|
730
|
+
// With an environment resolver, a graph's dependencies resolve in the linted file's
|
|
731
|
+
// environment (its nearest Project.toml): solved graphs are per environment.
|
|
732
|
+
let environmentKey = '';
|
|
733
|
+
function resolverActive(settings) {
|
|
734
|
+
const j = settings?.julia;
|
|
735
|
+
if (!j) return false;
|
|
736
|
+
const r = j.resolver;
|
|
737
|
+
return typeof r === 'function' || r === 'pkg' || (r == null && Boolean(j.pkgResolver));
|
|
738
|
+
}
|
|
739
|
+
function nearestProject(file) {
|
|
740
|
+
for (const dir of upward(file))
|
|
741
|
+
if (exists(join(dir, 'Project.toml'))) return join(dir, 'Project.toml');
|
|
742
|
+
return null;
|
|
743
|
+
}
|
|
744
|
+
// The user's macro signatures (`settings.julia.macros`), for dependency and sibling facts.
|
|
745
|
+
let currentMacros;
|
|
746
|
+
const resolverIds = new WeakMap();
|
|
747
|
+
function resolverVariant(settings) {
|
|
748
|
+
const j = settings?.julia;
|
|
749
|
+
if (!j) return '';
|
|
750
|
+
let resolver = j.resolver ?? null;
|
|
751
|
+
if (typeof resolver === 'function') {
|
|
752
|
+
if (!resolverIds.has(resolver)) resolverIds.set(resolver, `fn:${Math.random()}`);
|
|
753
|
+
resolver = resolverIds.get(resolver);
|
|
754
|
+
}
|
|
755
|
+
return JSON.stringify([
|
|
756
|
+
resolver,
|
|
757
|
+
j.pkgResolver ?? null,
|
|
758
|
+
j.cacheDir ?? null,
|
|
759
|
+
j.offline ?? null,
|
|
760
|
+
j.macros ?? null,
|
|
761
|
+
]);
|
|
762
|
+
}
|
|
763
|
+
function graphFor(entries, overlay) {
|
|
764
|
+
const key = JSON.stringify([
|
|
765
|
+
entries,
|
|
766
|
+
depots(),
|
|
767
|
+
overlay?.file,
|
|
768
|
+
overlay?.hash,
|
|
769
|
+
variant,
|
|
770
|
+
environmentKey,
|
|
771
|
+
]);
|
|
772
|
+
const previous = graphCache.get(key);
|
|
773
|
+
if (previous && [...previous.versions].every(([p, v]) => stamp(p) === v)) return previous;
|
|
774
|
+
const baseEntry = entries.find(
|
|
775
|
+
(p) => basename(p) === 'Base.jl' && basename(dirname(p)) === 'base',
|
|
776
|
+
);
|
|
777
|
+
const graph = {
|
|
778
|
+
root: moduleModel('Main', null),
|
|
779
|
+
files: new Map(),
|
|
780
|
+
versions: new Map(),
|
|
781
|
+
overlay,
|
|
782
|
+
entries,
|
|
783
|
+
baseDir: baseEntry && dirname(baseEntry),
|
|
784
|
+
};
|
|
785
|
+
for (const entry of entries) addFile(graph, entry, graph.root);
|
|
786
|
+
// Keep models compact: source trees/SourceCode objects never enter this cache.
|
|
787
|
+
delete graph.overlay;
|
|
788
|
+
graphCache.set(key, graph);
|
|
789
|
+
// Editor buffers may produce many distinct hashes.
|
|
790
|
+
if (graphCache.size > 128) graphCache.delete(graphCache.keys().next().value);
|
|
791
|
+
return graph;
|
|
792
|
+
}
|
|
793
|
+
function entryCandidates(file, context, track) {
|
|
794
|
+
const candidates = [];
|
|
795
|
+
const configured = context.settings?.julia?.entryFiles;
|
|
796
|
+
if (Array.isArray(configured)) {
|
|
797
|
+
// One include universe per configured entry; multiple entries can share Base.
|
|
798
|
+
for (const entry of configured)
|
|
799
|
+
if (typeof entry === 'string') candidates.push([resolve(entry)]);
|
|
800
|
+
}
|
|
801
|
+
const project = projectFor(file, track);
|
|
802
|
+
if (project) {
|
|
803
|
+
candidates.push([join(project.dir, 'src', `${project.name}.jl`)]);
|
|
804
|
+
candidates.push([join(project.dir, 'test', 'runtests.jl')]);
|
|
805
|
+
// A package extension's own files: ext/<Ext>/<Ext>.jl includes ext/<Ext>/*.jl.
|
|
806
|
+
const parts = relative(project.dir, file).split(sep);
|
|
807
|
+
if (parts[0] === 'ext' && parts.length > 2)
|
|
808
|
+
candidates.push([join(project.dir, 'ext', parts[1], `${parts[1]}.jl`)]);
|
|
809
|
+
}
|
|
810
|
+
// Harness scripts that include the file from a directory above it: `runtests.jl`,
|
|
811
|
+
// Documenter's `make.jl`/`makedocs.jl`, up to the package or repository root.
|
|
812
|
+
for (const entry of harnessEntries(file, project, track)) candidates.push([entry]);
|
|
813
|
+
for (const dir of upward(file)) {
|
|
814
|
+
if (basename(dir) === 'base' && exists(join(dir, 'Base.jl'))) {
|
|
815
|
+
candidates.push(
|
|
816
|
+
['Base.jl', 'Base_compiler.jl', 'sysimg.jl', 'compiler/compiler.jl']
|
|
817
|
+
.map((p) => join(dir, p))
|
|
818
|
+
.filter(exists),
|
|
819
|
+
);
|
|
820
|
+
break;
|
|
821
|
+
}
|
|
822
|
+
// Julia source-tree stdlibs without a package Project.toml.
|
|
823
|
+
if (basename(dirname(dir)) === 'stdlib')
|
|
824
|
+
candidates.push([join(dir, 'src', `${basename(dir)}.jl`)]);
|
|
825
|
+
}
|
|
826
|
+
return candidates;
|
|
827
|
+
}
|
|
828
|
+
const HARNESS = ['runtests.jl', 'make.jl', 'makedocs.jl'];
|
|
829
|
+
function harnessEntries(file, project, track) {
|
|
830
|
+
const entries = [];
|
|
831
|
+
for (const dir of upward(file)) {
|
|
832
|
+
for (const name of HARNESS) {
|
|
833
|
+
const entry = join(dir, name);
|
|
834
|
+
track.set(entry, stamp(entry));
|
|
835
|
+
if (entry !== file && exists(entry)) entries.push(entry);
|
|
836
|
+
}
|
|
837
|
+
if ((project && dir === project.dir) || exists(join(dir, '.git'))) break;
|
|
838
|
+
}
|
|
839
|
+
return entries;
|
|
840
|
+
}
|
|
841
|
+
function findModule(path, from, graph, state) {
|
|
842
|
+
const dots = path.match(/^\.+/u)?.[0].length ?? 0;
|
|
843
|
+
const parts = path.slice(dots).split('.').filter(Boolean);
|
|
844
|
+
if (dots) {
|
|
845
|
+
let at = from;
|
|
846
|
+
for (let i = 1; i < dots && at; i++) at = at.parent;
|
|
847
|
+
for (const [i, part] of parts.entries()) {
|
|
848
|
+
const child = at?.children.get(part) ?? (at?.name === part ? at : null);
|
|
849
|
+
if (child || !at) {
|
|
850
|
+
at = child;
|
|
851
|
+
continue;
|
|
852
|
+
}
|
|
853
|
+
// `..TOML` where the parent did `import TOML`/`using TOML`: follow that import.
|
|
854
|
+
const via = at.imports.find(
|
|
855
|
+
(imp) => !imp.names.length && (imp.alias ?? imp.path.split('.').at(-1)) === part,
|
|
856
|
+
);
|
|
857
|
+
if (!via || (state.depth ?? 0) > 16) return null;
|
|
858
|
+
const target = findModule(via.path, at, graph, { ...state, depth: (state.depth ?? 0) + 1 });
|
|
859
|
+
if (!target) return null;
|
|
860
|
+
const rest = parts.slice(i + 1);
|
|
861
|
+
if (!rest.length) return target;
|
|
862
|
+
return target.module
|
|
863
|
+
? findModule(`.${rest.join('.')}`, target.module, graph, state)
|
|
864
|
+
: (stdlibInventory(`${target.key}.${rest.join('.')}`) ?? null);
|
|
865
|
+
}
|
|
866
|
+
if (at?.dynamicExports) at.unknownExports = true;
|
|
867
|
+
return at ? { exports: at.exports, module: at } : null;
|
|
868
|
+
}
|
|
869
|
+
const name = parts.shift();
|
|
870
|
+
if (!parts.length && Object.hasOwn(inventory, name))
|
|
871
|
+
return { exports: exportedOnly(name, inventory[name]), key: name };
|
|
872
|
+
// Standard libraries and Base/Core/stdlib submodules (`Base.BinaryPlatforms`, `LinearAlgebra.BLAS`).
|
|
873
|
+
const known = stdlibInventory([name, ...parts].join('.'));
|
|
874
|
+
if (known) return known;
|
|
875
|
+
let at = graph.root.children.get(name);
|
|
876
|
+
if (!at) {
|
|
877
|
+
const entry = packageEntry(name, state.file, graph.versions, state);
|
|
878
|
+
if (!entry) return null;
|
|
879
|
+
at = loadIndex(entry);
|
|
880
|
+
if (!at) {
|
|
881
|
+
const dependency = graphFor([entry]);
|
|
882
|
+
solve(dependency, state);
|
|
883
|
+
for (const [p, v] of dependency.versions) graph.versions.set(p, v);
|
|
884
|
+
at = dependency.root.children.get(name);
|
|
885
|
+
// JLL packages generate their exports per platform (in JLLWrappers macros), and a
|
|
886
|
+
// module that includes files at run time may export anything from them.
|
|
887
|
+
if (at && (name.endsWith('_jll') || at.dynamicIncludes || at.dynamicDefinitions))
|
|
888
|
+
at.unknownExports = true;
|
|
889
|
+
storeIndex(entry, at, dependency.versions.keys());
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
for (const part of parts) {
|
|
893
|
+
at = at?.children.get(part);
|
|
894
|
+
// A submodule filled by computed code from outside (`Core.eval(Sub, ex)`).
|
|
895
|
+
if (at?.dynamicExports) at.unknownExports = true;
|
|
896
|
+
}
|
|
897
|
+
return at ? { exports: at.exports, module: at } : null;
|
|
898
|
+
}
|
|
899
|
+
function stdlibInventory(key) {
|
|
900
|
+
return Object.hasOwn(stdlibs, key) ? { exports: exportedOnly(key, stdlibs[key]), key } : null;
|
|
901
|
+
}
|
|
902
|
+
function allModules(root) {
|
|
903
|
+
return [root, ...[...root.children.values()].flatMap(allModules)];
|
|
904
|
+
}
|
|
905
|
+
function solve(graph, state) {
|
|
906
|
+
if (graph.solved || state.active.has(graph)) return;
|
|
907
|
+
state.active.add(graph);
|
|
908
|
+
const modules = allModules(graph.root);
|
|
909
|
+
// A module that runs, at top level, its own macro that generates imports.
|
|
910
|
+
for (const m of modules)
|
|
911
|
+
for (let at = m; at; at = at.parent)
|
|
912
|
+
if ([...m.topMacros].some((name) => at.importMacros.has(name))) m.dynamicDefinitions = true;
|
|
913
|
+
// Monotone fixed point handles relative imports and re-export cycles.
|
|
914
|
+
let changed = true;
|
|
915
|
+
while (changed) {
|
|
916
|
+
changed = false;
|
|
917
|
+
for (const m of modules) {
|
|
918
|
+
m.unknown.clear();
|
|
919
|
+
for (const imp of m.imports) {
|
|
920
|
+
const target = findModule(imp.path, m, graph, { ...state, file: imp.file });
|
|
921
|
+
const uncertain =
|
|
922
|
+
!target ||
|
|
923
|
+
target.module?.unknownExports ||
|
|
924
|
+
(imp.ownNames && (!target.module || target.module.indexed));
|
|
925
|
+
imp.resolvedExports = uncertain ? undefined : target.exports;
|
|
926
|
+
if (uncertain) {
|
|
927
|
+
// Only `using M` can bring in unknown names: an unresolved `import M`/`import ..f`
|
|
928
|
+
// binds just the written name (and may not even be a module).
|
|
929
|
+
if ((imp.wildcard || imp.ownNames) && imp.kind !== 'import') m.unknown.add(imp.path);
|
|
930
|
+
if (imp.reexport && !m.unknownExports) {
|
|
931
|
+
m.unknownExports = true;
|
|
932
|
+
changed = true;
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
if (!target || (uncertain && imp.ownNames)) continue;
|
|
936
|
+
// A generated name (`get_…`) may be among what's imported.
|
|
937
|
+
for (const prefix of target.module?.generatedPrefixes ?? [])
|
|
938
|
+
if (!m.generatedPrefixes.has(prefix)) {
|
|
939
|
+
m.generatedPrefixes.add(prefix);
|
|
940
|
+
changed = true;
|
|
941
|
+
}
|
|
942
|
+
const names = imp.wildcard
|
|
943
|
+
? target.exports
|
|
944
|
+
: imp.ownNames
|
|
945
|
+
? [...target.module.names].filter(([, kind]) => kind !== 'import').map(([n]) => n)
|
|
946
|
+
: imp.names;
|
|
947
|
+
if (target.module && !imp.wildcard)
|
|
948
|
+
imp.sourceNames.forEach((name) => target.module.reads.add(name));
|
|
949
|
+
for (const name of names) {
|
|
950
|
+
if (!m.names.has(name)) {
|
|
951
|
+
m.names.set(name, 'import');
|
|
952
|
+
changed = true;
|
|
953
|
+
}
|
|
954
|
+
if (imp.reexport && !m.exports.has(name)) {
|
|
955
|
+
m.exports.add(name);
|
|
956
|
+
changed = true;
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
state.active.delete(graph);
|
|
963
|
+
graph.solved = true;
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
// A file that no entry's include tree reaches but that runs inside another file's context:
|
|
967
|
+
// its includer's imports and definitions are unknown here. Returns the context to assume
|
|
968
|
+
// (the package it belongs to, and the harness that most likely runs it), or null.
|
|
969
|
+
function fragmentOf(file, ownFacts, track) {
|
|
970
|
+
const project = projectFor(file, track);
|
|
971
|
+
const top = ownFacts.find((m) => m.id === -1);
|
|
972
|
+
const own = (imp) =>
|
|
973
|
+
project && (imp.path === project.name || imp.path.startsWith(`${project.name}.`));
|
|
974
|
+
const inTest =
|
|
975
|
+
project &&
|
|
976
|
+
relative(project.dir, file)
|
|
977
|
+
.split(sep)
|
|
978
|
+
.some((part) => part === 'test' || part === 'tests');
|
|
979
|
+
const harnesses = harnessEntries(file, project, track);
|
|
980
|
+
// A harness whose computed include could reach the file (`for f in readdir(@__DIR__)
|
|
981
|
+
// include(f) end`) runs it: the file is that harness's fragment even if it also imports
|
|
982
|
+
// its package so it can run alone.
|
|
983
|
+
const runner = harnesses.find((h) => {
|
|
984
|
+
const computed = [...includesOf(h).computed];
|
|
985
|
+
return computed.some((p) => new RegExp(p, 'u').test(posixPath(file)));
|
|
986
|
+
});
|
|
987
|
+
const harness = runner ?? harnesses[0] ?? null;
|
|
988
|
+
// A snippet: no imports and no modules of its own, inside an environment (docs examples,
|
|
989
|
+
// scripts run by a harness that `include`s them under a computed path).
|
|
990
|
+
const snippet =
|
|
991
|
+
ownFacts.length === 1 &&
|
|
992
|
+
!top?.imports.length &&
|
|
993
|
+
upward(file).some((dir) => exists(join(dir, 'Project.toml')));
|
|
994
|
+
if (project && file === join(project.dir, 'src', `${project.name}.jl`)) return null;
|
|
995
|
+
if (project) {
|
|
996
|
+
// A package's test file is run by its test harness after `using Package`; a test file
|
|
997
|
+
// that does `using Package` itself is a script of its own. Elsewhere, any import of the
|
|
998
|
+
// package marks a script of its own.
|
|
999
|
+
const script = inTest
|
|
1000
|
+
? top?.imports.some((imp) => own(imp) && imp.wildcard && imp.kind === 'using')
|
|
1001
|
+
: top?.imports.some(own);
|
|
1002
|
+
// An unreached file under `src/` belongs inside the package module (or is stale): no
|
|
1003
|
+
// `using Package` is assumed for it.
|
|
1004
|
+
const inSrc = relative(project.dir, file).split(sep)[0] === 'src';
|
|
1005
|
+
// `using Package` is only assumed where the file's environment can load the package:
|
|
1006
|
+
// the package's own, a test environment, or one that lists it.
|
|
1007
|
+
const env = nearestProject(file);
|
|
1008
|
+
const loadable =
|
|
1009
|
+
inTest || !env || dirname(env) === project.dir || toml(env, track).deps.has(project.name);
|
|
1010
|
+
// A script a harness's loop also runs keeps its own imports' treatment: it gets the
|
|
1011
|
+
// harness's names, not the report-everything tier of `fragments: "report"`.
|
|
1012
|
+
if (!script || snippet || runner)
|
|
1013
|
+
return {
|
|
1014
|
+
project: inSrc || !loadable ? null : project,
|
|
1015
|
+
harness,
|
|
1016
|
+
script: Boolean(script && !snippet),
|
|
1017
|
+
};
|
|
1018
|
+
return null;
|
|
1019
|
+
}
|
|
1020
|
+
// Without a package, only a harness that could run it makes a snippet a fragment.
|
|
1021
|
+
return snippet && harness ? { project: null, harness } : null;
|
|
1022
|
+
}
|
|
1023
|
+
// The names a fragment's assumed context provides: its harness's top level, and the
|
|
1024
|
+
// exports of `using Package`.
|
|
1025
|
+
function fragmentContext(fragment, state) {
|
|
1026
|
+
const models = [];
|
|
1027
|
+
if (fragment.harness) {
|
|
1028
|
+
const harness = graphFor([fragment.harness]);
|
|
1029
|
+
solve(harness, { ...state, file: fragment.harness, active: new Set() });
|
|
1030
|
+
models.push(harness.root);
|
|
1031
|
+
}
|
|
1032
|
+
if (fragment.project) {
|
|
1033
|
+
const m = moduleModel('Main', null);
|
|
1034
|
+
m.imports.push({
|
|
1035
|
+
path: fragment.project.name,
|
|
1036
|
+
kind: 'using',
|
|
1037
|
+
wildcard: true,
|
|
1038
|
+
names: [],
|
|
1039
|
+
sourceNames: [],
|
|
1040
|
+
file: state.file,
|
|
1041
|
+
});
|
|
1042
|
+
const graph = { root: m, files: new Map(), versions: new Map(), entries: [] };
|
|
1043
|
+
solve(graph, { ...state, active: new Set() });
|
|
1044
|
+
models.push(m);
|
|
1045
|
+
}
|
|
1046
|
+
return models;
|
|
1047
|
+
}
|
|
1048
|
+
export function applyModuleResolution(manager, source, context = {}) {
|
|
1049
|
+
variant = resolverVariant(context.settings);
|
|
1050
|
+
environmentKey =
|
|
1051
|
+
resolverActive(context.settings) && !virtual(context.physicalFilename)
|
|
1052
|
+
? (nearestProject(resolve(context.physicalFilename)) ?? '')
|
|
1053
|
+
: '';
|
|
1054
|
+
currentMacros = context.settings?.julia?.macros;
|
|
1055
|
+
const file = virtual(context.physicalFilename) ? '<input>' : resolve(context.physicalFilename);
|
|
1056
|
+
const ownFacts = facts(source, file);
|
|
1057
|
+
const overlay = { file, facts: ownFacts, hash: hash(source.text) };
|
|
1058
|
+
const discovery = new Map();
|
|
1059
|
+
let graph, occurrences;
|
|
1060
|
+
if (!virtual(file))
|
|
1061
|
+
for (const entries of entryCandidates(file, context, discovery)) {
|
|
1062
|
+
let candidate = graphFor(entries);
|
|
1063
|
+
if (candidate.files.has(file) && factsCache.get(file)?.hash !== overlay.hash)
|
|
1064
|
+
candidate = graphFor(entries, overlay);
|
|
1065
|
+
if (candidate.files.has(file)) {
|
|
1066
|
+
graph = candidate;
|
|
1067
|
+
occurrences = candidate.files.get(file);
|
|
1068
|
+
break;
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
let fragment = false;
|
|
1072
|
+
if (!graph) {
|
|
1073
|
+
graph = virtual(file) ? graphFor([file], overlay) : graphFor([file]);
|
|
1074
|
+
if (!virtual(file) && factsCache.get(file)?.hash !== overlay.hash)
|
|
1075
|
+
graph = graphFor([file], overlay);
|
|
1076
|
+
occurrences = graph.files.get(file) ?? [];
|
|
1077
|
+
fragment = virtual(file) ? null : fragmentOf(file, ownFacts, discovery);
|
|
1078
|
+
}
|
|
1079
|
+
for (const [p, v] of discovery) graph.versions.set(p, v);
|
|
1080
|
+
let environment,
|
|
1081
|
+
asked = false;
|
|
1082
|
+
const settings = context.settings;
|
|
1083
|
+
const state = {
|
|
1084
|
+
active: new Set(),
|
|
1085
|
+
file,
|
|
1086
|
+
// Asked lazily: only a package that isn't installed locally starts the resolver.
|
|
1087
|
+
environment:
|
|
1088
|
+
virtual(file) || !settings?.julia
|
|
1089
|
+
? null
|
|
1090
|
+
: () => {
|
|
1091
|
+
if (!asked) environment = environmentFor(file, settings, graph.versions) ?? null;
|
|
1092
|
+
asked = true;
|
|
1093
|
+
return environment;
|
|
1094
|
+
},
|
|
1095
|
+
// Only a dependency of the linted file's environment (its nearest Project.toml's
|
|
1096
|
+
// `[deps]`, or a transitive one once resolved) is worth resolving.
|
|
1097
|
+
dependsOn: (name) => {
|
|
1098
|
+
for (const dir of upward(file)) {
|
|
1099
|
+
const project = join(dir, 'Project.toml');
|
|
1100
|
+
if (!exists(project)) continue;
|
|
1101
|
+
return (
|
|
1102
|
+
toml(project, graph.versions).deps.has(name) || Boolean(environment?.packages?.[name])
|
|
1103
|
+
);
|
|
1104
|
+
}
|
|
1105
|
+
return false;
|
|
1106
|
+
},
|
|
1107
|
+
};
|
|
1108
|
+
solve(graph, state);
|
|
1109
|
+
const assumed = fragment ? fragmentContext(fragment, state) : [];
|
|
1110
|
+
manager.moduleResolution = {
|
|
1111
|
+
entries: graph.entries,
|
|
1112
|
+
files: [...graph.files.keys()],
|
|
1113
|
+
scopes: new Map(),
|
|
1114
|
+
};
|
|
1115
|
+
for (const s of manager.scopes.filter((s) => s.kind === 'module')) {
|
|
1116
|
+
const id = s === manager.globalScope ? -1 : s.node.range[0];
|
|
1117
|
+
const models = [...new Set(occurrences.filter((o) => o.id === id).map((o) => o.module))];
|
|
1118
|
+
// A fragment's assumed context contributes names (and its unknown `using`s), not its
|
|
1119
|
+
// own flags or reads: other rules see only the file's own models.
|
|
1120
|
+
const context = s === manager.globalScope ? assumed : [];
|
|
1121
|
+
s.bareUsing = [...new Set([...models, ...context].flatMap((m) => [...m.unknown]))];
|
|
1122
|
+
s.dynamicIncludes = models.some((m) => m.dynamicIncludes);
|
|
1123
|
+
s.includeFragment = Boolean(fragment) && s === manager.globalScope;
|
|
1124
|
+
s.fragmentScript = Boolean(fragment?.script) && s === manager.globalScope;
|
|
1125
|
+
s.dynamicDefinitions = models.some((m) => m.dynamicDefinitions);
|
|
1126
|
+
s.generatedPrefixes = [
|
|
1127
|
+
...new Set([...models, ...context].flatMap((m) => [...(m.generatedPrefixes ?? [])])),
|
|
1128
|
+
];
|
|
1129
|
+
manager.moduleResolution.scopes.set(s, models);
|
|
1130
|
+
for (const m of [...models, ...context]) {
|
|
1131
|
+
if (s === manager.globalScope && m.parent) s.bare = m.bare;
|
|
1132
|
+
for (const [name, kind] of m.names) {
|
|
1133
|
+
if (kind === 'macro-binding') {
|
|
1134
|
+
// Declared by a macro somewhere in the module: suppresses reports only.
|
|
1135
|
+
s.macroBindings ??= new Map();
|
|
1136
|
+
if (!s.bindings.has(name) && !s.macroBindings.has(name))
|
|
1137
|
+
s.macroBindings.set(name, {
|
|
1138
|
+
name,
|
|
1139
|
+
kind,
|
|
1140
|
+
scope: s,
|
|
1141
|
+
definitions: [],
|
|
1142
|
+
references: [],
|
|
1143
|
+
writes: [],
|
|
1144
|
+
constant: false,
|
|
1145
|
+
macro: true,
|
|
1146
|
+
});
|
|
1147
|
+
continue;
|
|
1148
|
+
}
|
|
1149
|
+
if (!s.bindings.has(name))
|
|
1150
|
+
s.bindings.set(name, {
|
|
1151
|
+
name,
|
|
1152
|
+
kind: kind === 'import' ? 'import' : 'module-member',
|
|
1153
|
+
scope: s,
|
|
1154
|
+
definitions: [],
|
|
1155
|
+
references: [],
|
|
1156
|
+
writes: [],
|
|
1157
|
+
constant: false,
|
|
1158
|
+
external: true,
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
// `@eval M code`: the names `M` defines (null when they can't be listed).
|
|
1164
|
+
manager.moduleResolution.evalNames = new Map();
|
|
1165
|
+
for (const [code, { path, scope }] of manager.evalTargets ?? []) {
|
|
1166
|
+
const id = scope === manager.globalScope ? -1 : scope.node.range[0];
|
|
1167
|
+
const from = occurrences.find((o) => o.id === id)?.module ?? graph.root;
|
|
1168
|
+
// A submodule of the enclosing module (or of an outer one) by that name comes first.
|
|
1169
|
+
let local = null;
|
|
1170
|
+
if (!path.includes('.'))
|
|
1171
|
+
for (let at = from; at && !local; at = at.parent) local = at.children.get(path) ?? null;
|
|
1172
|
+
const target = local ? { module: local } : findModule(path, from, graph, state);
|
|
1173
|
+
const m = target?.module;
|
|
1174
|
+
const listed =
|
|
1175
|
+
m &&
|
|
1176
|
+
!m.indexed &&
|
|
1177
|
+
!m.unknownExports &&
|
|
1178
|
+
!m.unknown.size &&
|
|
1179
|
+
!m.dynamicDefinitions &&
|
|
1180
|
+
!m.dynamicIncludes &&
|
|
1181
|
+
!m.unresolvedIncludes;
|
|
1182
|
+
manager.moduleResolution.evalNames.set(code, {
|
|
1183
|
+
path,
|
|
1184
|
+
names: listed ? new Set([...m.names.keys(), ...m.children.keys(), m.name]) : null,
|
|
1185
|
+
});
|
|
1186
|
+
}
|
|
1187
|
+
}
|