@gosukiwi/eslint-plugin-colocate 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +197 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +12 -0
- package/dist/lib/derived.d.ts +4 -0
- package/dist/lib/derived.js +15 -0
- package/dist/lib/findings.d.ts +9 -0
- package/dist/lib/findings.js +96 -0
- package/dist/lib/fs-safe.d.ts +8 -0
- package/dist/lib/fs-safe.js +55 -0
- package/dist/lib/gates.d.ts +10 -0
- package/dist/lib/gates.js +46 -0
- package/dist/lib/graph-cache.d.ts +5 -0
- package/dist/lib/graph-cache.js +162 -0
- package/dist/lib/graph.d.ts +20 -0
- package/dist/lib/graph.js +109 -0
- package/dist/lib/named-door.d.ts +6 -0
- package/dist/lib/named-door.js +361 -0
- package/dist/lib/owners.d.ts +22 -0
- package/dist/lib/owners.js +386 -0
- package/dist/lib/parse.d.ts +4 -0
- package/dist/lib/parse.js +59 -0
- package/dist/lib/paths.d.ts +2 -0
- package/dist/lib/paths.js +8 -0
- package/dist/lib/require-binding.d.ts +5 -0
- package/dist/lib/require-binding.js +72 -0
- package/dist/lib/resolve.d.ts +9 -0
- package/dist/lib/resolve.js +161 -0
- package/dist/lib/root.d.ts +1 -0
- package/dist/lib/root.js +27 -0
- package/dist/lib/scope.d.ts +8 -0
- package/dist/lib/scope.js +58 -0
- package/dist/lib/subject.d.ts +16 -0
- package/dist/lib/subject.js +61 -0
- package/dist/lib/walk.d.ts +10 -0
- package/dist/lib/walk.js +97 -0
- package/dist/rules/entry.d.ts +3 -0
- package/dist/rules/entry.js +141 -0
- package/dist/rules/ownership.d.ts +3 -0
- package/dist/rules/ownership.js +45 -0
- package/package.json +45 -0
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { minimatch } from "minimatch";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
import { derivedFromGraph } from "./derived.js";
|
|
5
|
+
import { safeReadFile, safeReaddir, safeRealpath } from "./fs-safe.js";
|
|
6
|
+
import { canonicalGraphPath, getGraphResolutionSettings, graphFilesInDir, } from "./graph.js";
|
|
7
|
+
import { parseSourceFile } from "./parse.js";
|
|
8
|
+
import { isInsideDir } from "./paths.js";
|
|
9
|
+
import { resolveSpecifier } from "./resolve.js";
|
|
10
|
+
import { SKIP_DIRS } from "./scope.js";
|
|
11
|
+
const reExportsByGraph = derivedFromGraph(() => new Map());
|
|
12
|
+
function scanReExports(indexFile, dir, graph) {
|
|
13
|
+
const content = safeReadFile(indexFile);
|
|
14
|
+
const realDir = safeRealpath(dir);
|
|
15
|
+
const realIndex = safeRealpath(indexFile);
|
|
16
|
+
if (content === undefined || realDir === undefined) {
|
|
17
|
+
return [];
|
|
18
|
+
}
|
|
19
|
+
const sourceFile = parseSourceFile(indexFile, content);
|
|
20
|
+
const settings = getGraphResolutionSettings(graph);
|
|
21
|
+
const local = new Set();
|
|
22
|
+
const visit = (node) => {
|
|
23
|
+
if (ts.isExportDeclaration(node) &&
|
|
24
|
+
node.moduleSpecifier !== undefined &&
|
|
25
|
+
ts.isStringLiteral(node.moduleSpecifier)) {
|
|
26
|
+
const specifier = node.moduleSpecifier.text;
|
|
27
|
+
const resolved = resolveSpecifier(specifier, dir, settings);
|
|
28
|
+
const target = resolved === undefined
|
|
29
|
+
? undefined
|
|
30
|
+
: canonicalGraphPath(graph, resolved);
|
|
31
|
+
if (target !== undefined && target !== realIndex) {
|
|
32
|
+
if (path.dirname(target) === realDir) {
|
|
33
|
+
local.add(target);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
ts.forEachChild(node, visit);
|
|
38
|
+
};
|
|
39
|
+
visit(sourceFile);
|
|
40
|
+
return [...local];
|
|
41
|
+
}
|
|
42
|
+
export function collectReExports(indexFile, dir, graph) {
|
|
43
|
+
const key = indexFile + "\0" + dir;
|
|
44
|
+
const perGraph = reExportsByGraph(graph);
|
|
45
|
+
const cached = perGraph.get(key);
|
|
46
|
+
if (cached !== undefined) {
|
|
47
|
+
return cached;
|
|
48
|
+
}
|
|
49
|
+
const result = scanReExports(indexFile, dir, graph);
|
|
50
|
+
perGraph.set(key, result);
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
function isNamespaceBarrel(filePath, graph) {
|
|
54
|
+
const basename = path.basename(filePath, path.extname(filePath));
|
|
55
|
+
if (basename !== "index") {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
return (collectReExports(filePath, path.dirname(filePath), graph).length >= 2);
|
|
59
|
+
}
|
|
60
|
+
function isOwnerEntryFile(file, dir, graph) {
|
|
61
|
+
if (path.dirname(file) !== dir) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
const base = path.basename(file, path.extname(file));
|
|
65
|
+
if (base === path.basename(dir)) {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
if (base !== "index") {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
return (graph.importers.get(file) ?? []).some((importer) => path.dirname(importer) !== dir);
|
|
72
|
+
}
|
|
73
|
+
function directoryHasMatchingEntry(dir, graph) {
|
|
74
|
+
return graphFilesInDir(graph, dir).some((file) => isOwnerEntryFile(file, dir, graph));
|
|
75
|
+
}
|
|
76
|
+
export function getOwner(filePath, graph, rootDir) {
|
|
77
|
+
let dir = path.dirname(filePath);
|
|
78
|
+
const realRoot = safeRealpath(rootDir) ?? rootDir;
|
|
79
|
+
while (true) {
|
|
80
|
+
if (directoryHasMatchingEntry(dir, graph)) {
|
|
81
|
+
return { kind: "folder", path: dir };
|
|
82
|
+
}
|
|
83
|
+
if (dir === realRoot) {
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
const parent = path.dirname(dir);
|
|
87
|
+
if (parent === dir) {
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
dir = parent;
|
|
91
|
+
}
|
|
92
|
+
return { kind: "standalone", path: filePath };
|
|
93
|
+
}
|
|
94
|
+
function buildImports(graph) {
|
|
95
|
+
const imports = new Map();
|
|
96
|
+
for (const [target, importers] of graph.importers) {
|
|
97
|
+
for (const importer of importers) {
|
|
98
|
+
const existing = imports.get(importer);
|
|
99
|
+
if (existing === undefined) {
|
|
100
|
+
imports.set(importer, [target]);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
existing.push(target);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return imports;
|
|
108
|
+
}
|
|
109
|
+
function popComponent(stack, onStack, ids, id, root) {
|
|
110
|
+
while (true) {
|
|
111
|
+
const member = stack.pop();
|
|
112
|
+
if (member === undefined) {
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
onStack.delete(member);
|
|
116
|
+
ids.set(member, id);
|
|
117
|
+
if (member === root) {
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function stronglyConnectedIds(nodes, edgesOf) {
|
|
123
|
+
const index = new Map();
|
|
124
|
+
const low = new Map();
|
|
125
|
+
const onStack = new Set();
|
|
126
|
+
const stack = [];
|
|
127
|
+
const ids = new Map();
|
|
128
|
+
let counter = 0;
|
|
129
|
+
let nextId = 0;
|
|
130
|
+
const push = (node, frames) => {
|
|
131
|
+
index.set(node, counter);
|
|
132
|
+
low.set(node, counter);
|
|
133
|
+
counter += 1;
|
|
134
|
+
stack.push(node);
|
|
135
|
+
onStack.add(node);
|
|
136
|
+
frames.push({ node, edges: edgesOf(node), next: 0 });
|
|
137
|
+
};
|
|
138
|
+
const visitEdge = (frame, child, frames) => {
|
|
139
|
+
if (!index.has(child)) {
|
|
140
|
+
push(child, frames);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (onStack.has(child)) {
|
|
144
|
+
low.set(frame.node, Math.min(low.get(frame.node) ?? 0, index.get(child) ?? 0));
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
const finishFrame = (frame, frames) => {
|
|
148
|
+
frames.pop();
|
|
149
|
+
const parent = frames[frames.length - 1];
|
|
150
|
+
if (parent !== undefined) {
|
|
151
|
+
low.set(parent.node, Math.min(low.get(parent.node) ?? 0, low.get(frame.node) ?? 0));
|
|
152
|
+
}
|
|
153
|
+
if (low.get(frame.node) === index.get(frame.node)) {
|
|
154
|
+
const id = nextId;
|
|
155
|
+
nextId += 1;
|
|
156
|
+
popComponent(stack, onStack, ids, id, frame.node);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
for (const start of nodes) {
|
|
160
|
+
if (index.has(start)) {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
const frames = [];
|
|
164
|
+
push(start, frames);
|
|
165
|
+
while (frames.length > 0) {
|
|
166
|
+
const frame = frames[frames.length - 1];
|
|
167
|
+
if (frame.next < frame.edges.length) {
|
|
168
|
+
const child = frame.edges[frame.next];
|
|
169
|
+
frame.next += 1;
|
|
170
|
+
visitEdge(frame, child, frames);
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
finishFrame(frame, frames);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return ids;
|
|
177
|
+
}
|
|
178
|
+
function getRoots(graph) {
|
|
179
|
+
const imports = buildImports(graph);
|
|
180
|
+
const componentIds = stronglyConnectedIds(graph.files, (file) => imports.get(file) ?? []);
|
|
181
|
+
const importedFromOutside = new Set();
|
|
182
|
+
for (const file of graph.files) {
|
|
183
|
+
const component = componentIds.get(file);
|
|
184
|
+
for (const importer of graph.importers.get(file) ?? []) {
|
|
185
|
+
if (componentIds.get(importer) !== component && component !== undefined) {
|
|
186
|
+
importedFromOutside.add(component);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
const roots = new Set();
|
|
191
|
+
for (const file of graph.files) {
|
|
192
|
+
const component = componentIds.get(file);
|
|
193
|
+
if (component === undefined || !importedFromOutside.has(component)) {
|
|
194
|
+
roots.add(file);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return roots;
|
|
198
|
+
}
|
|
199
|
+
export const getShells = derivedFromGraph((graph) => {
|
|
200
|
+
const roots = getRoots(graph);
|
|
201
|
+
const shells = new Set(roots);
|
|
202
|
+
for (const file of graph.files) {
|
|
203
|
+
if (shells.has(file)) {
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
const importers = graph.importers.get(file) ?? [];
|
|
207
|
+
if (importers.length > 0 &&
|
|
208
|
+
importers.every((importer) => roots.has(importer))) {
|
|
209
|
+
shells.add(file);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return shells;
|
|
213
|
+
});
|
|
214
|
+
export function getColocationConsumers(filePath, graph, shells) {
|
|
215
|
+
const importers = graph.importers.get(filePath) ?? [];
|
|
216
|
+
return importers.filter((importer) => !shells.has(importer) && !isNamespaceBarrel(importer, graph));
|
|
217
|
+
}
|
|
218
|
+
function collectLayerDirs(dir, cwd, rootDir, layerGlobs, out) {
|
|
219
|
+
for (const entry of safeReaddir(dir)) {
|
|
220
|
+
if (!entry.isDirectory() || SKIP_DIRS.has(entry.name)) {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
const fullPath = path.join(dir, entry.name);
|
|
224
|
+
const candidates = [
|
|
225
|
+
path.relative(cwd, fullPath).split(path.sep).join("/"),
|
|
226
|
+
path.relative(rootDir, fullPath).split(path.sep).join("/"),
|
|
227
|
+
];
|
|
228
|
+
if (layerGlobs.some((glob) => candidates.some((candidate) => minimatch(candidate, glob)))) {
|
|
229
|
+
const realPath = safeRealpath(fullPath);
|
|
230
|
+
if (realPath !== undefined) {
|
|
231
|
+
out.push(realPath);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
collectLayerDirs(fullPath, cwd, rootDir, layerGlobs, out);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
export function collectLayerDirectories(cwd, layerGlobs, rootDir = cwd) {
|
|
238
|
+
if (layerGlobs.length === 0) {
|
|
239
|
+
return [];
|
|
240
|
+
}
|
|
241
|
+
const dirs = [];
|
|
242
|
+
collectLayerDirs(cwd, cwd, rootDir, layerGlobs, dirs);
|
|
243
|
+
return dirs;
|
|
244
|
+
}
|
|
245
|
+
const layerDirsByGraph = derivedFromGraph(() => new Map());
|
|
246
|
+
export function resolveLayerDirectories(graph, cwd, layerGlobs, rootDir = cwd) {
|
|
247
|
+
if (layerGlobs.length === 0) {
|
|
248
|
+
return [];
|
|
249
|
+
}
|
|
250
|
+
const key = cwd + "\0" + rootDir + "\0" + layerGlobs.join("\0");
|
|
251
|
+
const perGraph = layerDirsByGraph(graph);
|
|
252
|
+
const cached = perGraph.get(key);
|
|
253
|
+
if (cached !== undefined) {
|
|
254
|
+
return cached;
|
|
255
|
+
}
|
|
256
|
+
const dirs = collectLayerDirectories(cwd, layerGlobs, rootDir);
|
|
257
|
+
perGraph.set(key, dirs);
|
|
258
|
+
return dirs;
|
|
259
|
+
}
|
|
260
|
+
export function isLayerPublicModule(filePath, layerDirs) {
|
|
261
|
+
if (layerDirs.length === 0) {
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
const parent = path.dirname(filePath);
|
|
265
|
+
if (layerDirs.includes(parent)) {
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
const grandparent = path.dirname(parent);
|
|
269
|
+
if (!layerDirs.includes(grandparent)) {
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
const folderName = path.basename(parent);
|
|
273
|
+
const fileBase = path.basename(filePath, path.extname(filePath));
|
|
274
|
+
return fileBase === folderName || fileBase === "index";
|
|
275
|
+
}
|
|
276
|
+
export function shouldSkipColocation(filePath, ctx) {
|
|
277
|
+
const shells = getShells(ctx.graph);
|
|
278
|
+
if (shells.has(filePath)) {
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
if (isLayerPublicModule(filePath, ctx.layerDirs)) {
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
return getColocationConsumers(filePath, ctx.graph, shells).length === 0;
|
|
285
|
+
}
|
|
286
|
+
function collectConsumerOwners(filePath, ctx) {
|
|
287
|
+
const shells = getShells(ctx.graph);
|
|
288
|
+
const consumers = getColocationConsumers(filePath, ctx.graph, shells);
|
|
289
|
+
const owners = new Map();
|
|
290
|
+
for (const consumer of consumers) {
|
|
291
|
+
const owner = getOwner(consumer, ctx.graph, ctx.rootDir);
|
|
292
|
+
owners.set(owner.path, owner);
|
|
293
|
+
}
|
|
294
|
+
return owners;
|
|
295
|
+
}
|
|
296
|
+
export function isPrivateOutsideOwner(filePath, ctx) {
|
|
297
|
+
if (shouldSkipColocation(filePath, ctx)) {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
const owners = collectConsumerOwners(filePath, ctx);
|
|
301
|
+
if (owners.size !== 1) {
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
const owner = owners.values().next().value;
|
|
305
|
+
if (owner === undefined) {
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
if (owner.kind === "folder") {
|
|
309
|
+
return !isInsideDir(filePath, owner.path);
|
|
310
|
+
}
|
|
311
|
+
return filePath !== owner.path;
|
|
312
|
+
}
|
|
313
|
+
function ownerDir(owner) {
|
|
314
|
+
return owner.kind === "folder" ? owner.path : path.dirname(owner.path);
|
|
315
|
+
}
|
|
316
|
+
function longestCommonAncestor(dirs) {
|
|
317
|
+
if (dirs.length === 0) {
|
|
318
|
+
return "";
|
|
319
|
+
}
|
|
320
|
+
const segments = dirs.map((dir) => dir.split(path.sep));
|
|
321
|
+
const minLen = Math.min(...segments.map((parts) => parts.length));
|
|
322
|
+
const common = [];
|
|
323
|
+
for (let i = 0; i < minLen; i++) {
|
|
324
|
+
const part = segments[0][i];
|
|
325
|
+
if (segments.every((parts) => parts[i] === part)) {
|
|
326
|
+
common.push(part);
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return common.join(path.sep);
|
|
333
|
+
}
|
|
334
|
+
function folderOwnerAncestors(filePath, graph, rootDir) {
|
|
335
|
+
const realRoot = safeRealpath(rootDir) ?? rootDir;
|
|
336
|
+
const dirs = [];
|
|
337
|
+
let dir = path.dirname(filePath);
|
|
338
|
+
while (true) {
|
|
339
|
+
if (directoryHasMatchingEntry(dir, graph)) {
|
|
340
|
+
dirs.push(dir);
|
|
341
|
+
}
|
|
342
|
+
if (dir === realRoot) {
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
const parent = path.dirname(dir);
|
|
346
|
+
if (parent === dir) {
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
dir = parent;
|
|
350
|
+
}
|
|
351
|
+
return dirs;
|
|
352
|
+
}
|
|
353
|
+
export function getSharedColocationIssue(filePath, ctx) {
|
|
354
|
+
if (shouldSkipColocation(filePath, ctx)) {
|
|
355
|
+
return undefined;
|
|
356
|
+
}
|
|
357
|
+
const owners = collectConsumerOwners(filePath, ctx);
|
|
358
|
+
if (owners.size < 2) {
|
|
359
|
+
return undefined;
|
|
360
|
+
}
|
|
361
|
+
const ownerDirs = [...new Set([...owners.values()].map(ownerDir))];
|
|
362
|
+
const lca = longestCommonAncestor(ownerDirs);
|
|
363
|
+
const consumerFolderDirs = [...owners.values()]
|
|
364
|
+
.filter((owner) => owner.kind === "folder")
|
|
365
|
+
.map((owner) => owner.path);
|
|
366
|
+
const candidates = new Set([
|
|
367
|
+
...consumerFolderDirs,
|
|
368
|
+
...folderOwnerAncestors(filePath, ctx.graph, ctx.rootDir),
|
|
369
|
+
]);
|
|
370
|
+
const containing = [...candidates].filter((dir) => {
|
|
371
|
+
if (!isInsideDir(filePath, dir)) {
|
|
372
|
+
return false;
|
|
373
|
+
}
|
|
374
|
+
if (!isInsideDir(dir, lca)) {
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
return !isOwnerEntryFile(filePath, dir, ctx.graph);
|
|
378
|
+
});
|
|
379
|
+
if (containing.length > 0) {
|
|
380
|
+
return "sharedInsideOwner";
|
|
381
|
+
}
|
|
382
|
+
if (!isInsideDir(filePath, lca)) {
|
|
383
|
+
return "sharedTooHigh";
|
|
384
|
+
}
|
|
385
|
+
return undefined;
|
|
386
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
export declare function parseSourceFile(fileName: string, content: string): ts.SourceFile;
|
|
3
|
+
export declare function stringLiteralText(node: ts.Node | undefined): string | undefined;
|
|
4
|
+
export declare function extractSpecifiers(content: string, fileName: string): string[];
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { scopeBindsRequire } from "./require-binding.js";
|
|
3
|
+
function scriptKindFromFileName(fileName) {
|
|
4
|
+
if (fileName.endsWith(".tsx")) {
|
|
5
|
+
return ts.ScriptKind.TSX;
|
|
6
|
+
}
|
|
7
|
+
if (fileName.endsWith(".jsx")) {
|
|
8
|
+
return ts.ScriptKind.JSX;
|
|
9
|
+
}
|
|
10
|
+
if (fileName.endsWith(".js") ||
|
|
11
|
+
fileName.endsWith(".mjs") ||
|
|
12
|
+
fileName.endsWith(".cjs")) {
|
|
13
|
+
return ts.ScriptKind.JS;
|
|
14
|
+
}
|
|
15
|
+
return ts.ScriptKind.TS;
|
|
16
|
+
}
|
|
17
|
+
export function parseSourceFile(fileName, content) {
|
|
18
|
+
return ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, false, scriptKindFromFileName(fileName));
|
|
19
|
+
}
|
|
20
|
+
export function stringLiteralText(node) {
|
|
21
|
+
if (node !== undefined && ts.isStringLiteralLike(node)) {
|
|
22
|
+
return node.text;
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
function importedSpecifier(node, requireIsCjs) {
|
|
27
|
+
if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) {
|
|
28
|
+
return stringLiteralText(node.moduleSpecifier);
|
|
29
|
+
}
|
|
30
|
+
if (ts.isImportEqualsDeclaration(node) &&
|
|
31
|
+
ts.isExternalModuleReference(node.moduleReference)) {
|
|
32
|
+
return stringLiteralText(node.moduleReference.expression);
|
|
33
|
+
}
|
|
34
|
+
if (ts.isCallExpression(node) &&
|
|
35
|
+
(node.expression.kind === ts.SyntaxKind.ImportKeyword ||
|
|
36
|
+
(requireIsCjs &&
|
|
37
|
+
ts.isIdentifier(node.expression) &&
|
|
38
|
+
node.expression.text === "require"))) {
|
|
39
|
+
return stringLiteralText(node.arguments[0]);
|
|
40
|
+
}
|
|
41
|
+
if (ts.isImportTypeNode(node) && ts.isLiteralTypeNode(node.argument)) {
|
|
42
|
+
return stringLiteralText(node.argument.literal);
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
export function extractSpecifiers(content, fileName) {
|
|
47
|
+
const sourceFile = parseSourceFile(fileName, content);
|
|
48
|
+
const specifiers = [];
|
|
49
|
+
const visit = (node, shadowed) => {
|
|
50
|
+
const requireIsCjs = !(shadowed || scopeBindsRequire(node));
|
|
51
|
+
const specifier = importedSpecifier(node, requireIsCjs);
|
|
52
|
+
if (specifier !== undefined) {
|
|
53
|
+
specifiers.push(specifier);
|
|
54
|
+
}
|
|
55
|
+
ts.forEachChild(node, (child) => visit(child, !requireIsCjs));
|
|
56
|
+
};
|
|
57
|
+
visit(sourceFile, false);
|
|
58
|
+
return specifiers;
|
|
59
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
export function isInsideDir(filePath, dir) {
|
|
3
|
+
const prefix = dir.endsWith(path.sep) ? dir : dir + path.sep;
|
|
4
|
+
return filePath.startsWith(prefix);
|
|
5
|
+
}
|
|
6
|
+
export function isAtOrInsideDir(filePath, dir) {
|
|
7
|
+
return filePath === dir || isInsideDir(filePath, dir);
|
|
8
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { SourceCode } from "eslint";
|
|
2
|
+
import type * as ESTree from "estree";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
export declare function scopeBindsRequire(node: ts.Node): boolean;
|
|
5
|
+
export declare function requireIsShadowed(sourceCode: SourceCode, node: ESTree.Node): boolean;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
const CREATE_REQUIRE = "createRequire";
|
|
3
|
+
const REQUIRE = "require";
|
|
4
|
+
function tsIsCreateRequireCall(node) {
|
|
5
|
+
if (node === undefined || !ts.isCallExpression(node)) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
const callee = node.expression;
|
|
9
|
+
if (ts.isIdentifier(callee)) {
|
|
10
|
+
return callee.text === CREATE_REQUIRE;
|
|
11
|
+
}
|
|
12
|
+
return (ts.isPropertyAccessExpression(callee) && callee.name.text === CREATE_REQUIRE);
|
|
13
|
+
}
|
|
14
|
+
function estreeIsCreateRequireCall(init) {
|
|
15
|
+
if (init === null || init === undefined || init.type !== "CallExpression") {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
const callee = init.callee;
|
|
19
|
+
return ((callee.type === "Identifier" && callee.name === CREATE_REQUIRE) ||
|
|
20
|
+
(callee.type === "MemberExpression" &&
|
|
21
|
+
callee.property.type === "Identifier" &&
|
|
22
|
+
callee.property.name === CREATE_REQUIRE));
|
|
23
|
+
}
|
|
24
|
+
function bindsName(name, target) {
|
|
25
|
+
if (ts.isIdentifier(name)) {
|
|
26
|
+
return name.text === target;
|
|
27
|
+
}
|
|
28
|
+
return name.elements.some((element) => ts.isBindingElement(element) ? bindsName(element.name, target) : false);
|
|
29
|
+
}
|
|
30
|
+
function declarationBindsRequire(declaration) {
|
|
31
|
+
if (!bindsName(declaration.name, REQUIRE)) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
return !tsIsCreateRequireCall(declaration.initializer);
|
|
35
|
+
}
|
|
36
|
+
function statementBindsRequire(statement) {
|
|
37
|
+
if (ts.isVariableStatement(statement)) {
|
|
38
|
+
return statement.declarationList.declarations.some(declarationBindsRequire);
|
|
39
|
+
}
|
|
40
|
+
return (ts.isFunctionDeclaration(statement) && statement.name?.text === REQUIRE);
|
|
41
|
+
}
|
|
42
|
+
export function scopeBindsRequire(node) {
|
|
43
|
+
if (ts.isFunctionLike(node)) {
|
|
44
|
+
if (node.parameters.some((p) => bindsName(p.name, REQUIRE))) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const statements = ts.isSourceFile(node)
|
|
49
|
+
? node.statements
|
|
50
|
+
: ts.isBlock(node) || ts.isModuleBlock(node)
|
|
51
|
+
? node.statements
|
|
52
|
+
: undefined;
|
|
53
|
+
if (statements === undefined) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
return statements.some(statementBindsRequire);
|
|
57
|
+
}
|
|
58
|
+
export function requireIsShadowed(sourceCode, node) {
|
|
59
|
+
let scope = sourceCode.getScope(node);
|
|
60
|
+
while (scope !== null) {
|
|
61
|
+
const variable = scope.variables.find((entry) => entry.name === REQUIRE);
|
|
62
|
+
if (variable !== undefined && variable.defs.length > 0) {
|
|
63
|
+
return !variable.defs.every((def) => {
|
|
64
|
+
const declarator = def.node;
|
|
65
|
+
return (declarator.type === "VariableDeclarator" &&
|
|
66
|
+
estreeIsCreateRequireCall(declarator.init));
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
scope = scope.upper;
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
export interface ResolutionSettings {
|
|
3
|
+
options: ts.CompilerOptions;
|
|
4
|
+
cache: ts.ModuleResolutionCache;
|
|
5
|
+
configPaths: string[];
|
|
6
|
+
}
|
|
7
|
+
export declare function findTsconfig(rootDir: string): string | undefined;
|
|
8
|
+
export declare function createResolutionSettings(rootDir: string): ResolutionSettings;
|
|
9
|
+
export declare function resolveSpecifier(specifier: string, fromDir: string, settings?: ResolutionSettings): string | undefined;
|