@zgeoff/oxlint-config 0.5.0 → 0.5.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/README.md +9 -0
- package/collect-refusing-edges.js +19 -0
- package/collect-zone-names.js +4 -0
- package/function-verb.js +56 -56
- package/{load-zone-options.js → get-zone-options.js} +3 -3
- package/import-zones-entry.d.ts +59 -0
- package/import-zones-entry.js +2 -0
- package/import-zones-schema.js +19 -19
- package/import-zones.js +121 -131
- package/is-edge-refusing.js +22 -0
- package/is-entry-applying.js +6 -0
- package/is-in-any-zone.js +3 -0
- package/load-workspace-manifests.js +6 -6
- package/max-consecutive-line-comments.js +36 -36
- package/no-abbreviated-names.js +30 -30
- package/no-comment-rule-override.js +30 -30
- package/no-jsdoc.js +15 -15
- package/package.json +14 -3
- package/plugin.js +18 -18
- package/resolve-import-target.js +131 -131
- package/resolve-repo-root.js +6 -6
- package/setup-test-only.js +10 -10
- package/test-beside-module.js +35 -33
- package/verb-contract.js +93 -93
package/README.md
CHANGED
|
@@ -294,6 +294,15 @@ Exempt a known violation with a next-line disable comment that names the ticket
|
|
|
294
294
|
unzoned report sits on the first statement, so its comment goes above that. Run lint with
|
|
295
295
|
`--report-unused-disable-directives-severity error` so a comment outlives no import.
|
|
296
296
|
|
|
297
|
+
The rule judges only the imports that a file holds. A repo check that judges something else, such as
|
|
298
|
+
a dependency that a manifest declares, imports the rule's own reading from
|
|
299
|
+
`@zgeoff/oxlint-config/import-zones`. That entry exports `importZonesSchema`, the JSON schema of the
|
|
300
|
+
options, with an `ImportZonesOptions` type, and
|
|
301
|
+
`collectRefusingEdges(options, { from, to, typeOnly })`. The function returns each edge in `options`
|
|
302
|
+
that refuses an import of `to` from `from`, both paths relative to the repo root. It skips module
|
|
303
|
+
resolution, so `to` is the path that the rule would resolve the specifier to. The rule judges each
|
|
304
|
+
edge through the same function.
|
|
305
|
+
|
|
297
306
|
## Notes
|
|
298
307
|
|
|
299
308
|
- The config enables the `typescript`, `unicorn`, `oxc`, `import`, and `promise` plugins and loads
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import collectZoneNames from './collect-zone-names.js';
|
|
2
|
+
import getZoneOptions from './get-zone-options.js';
|
|
3
|
+
import isEdgeRefusing from './is-edge-refusing.js';
|
|
4
|
+
|
|
5
|
+
// the edges in `options` that refuse an import from `edgeImport.from` of
|
|
6
|
+
// `edgeImport.to`, as the options list them
|
|
7
|
+
export default function collectRefusingEdges(options, edgeImport) {
|
|
8
|
+
const compiled = getZoneOptions(options);
|
|
9
|
+
|
|
10
|
+
const judged = {
|
|
11
|
+
from: edgeImport.from,
|
|
12
|
+
to: edgeImport.to,
|
|
13
|
+
fromZones: collectZoneNames(compiled.zones, edgeImport.from),
|
|
14
|
+
toZones: collectZoneNames(compiled.zones, edgeImport.to),
|
|
15
|
+
typeOnly: edgeImport.typeOnly === true,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
return (options.edges ?? []).filter((_, index) => isEdgeRefusing(compiled.edges[index], judged));
|
|
19
|
+
}
|
package/function-verb.js
CHANGED
|
@@ -1,62 +1,6 @@
|
|
|
1
1
|
import findFunctionName from './find-function-name.js';
|
|
2
2
|
import isVerbMatch from './is-verb-match.js';
|
|
3
3
|
|
|
4
|
-
// banned verb → the role-table verb to use instead; null means the verb is
|
|
5
|
-
// too vague to map and the name should say what the function does
|
|
6
|
-
const bannedVerbs = new Map([
|
|
7
|
-
['process', null],
|
|
8
|
-
['manage', null],
|
|
9
|
-
['do', null],
|
|
10
|
-
['perform', null],
|
|
11
|
-
['execute', 'run'],
|
|
12
|
-
['compute', 'build'],
|
|
13
|
-
['fetch', 'read'],
|
|
14
|
-
['save', 'write'],
|
|
15
|
-
['store', 'write'],
|
|
16
|
-
['delete', 'remove'],
|
|
17
|
-
['search', 'find'],
|
|
18
|
-
['lookup', 'find'],
|
|
19
|
-
]);
|
|
20
|
-
|
|
21
|
-
// the role table's template prefixes (`with<X>`, `handle<Event>`, …): each is
|
|
22
|
-
// only valid with a suffix, and a bare `handle` is the vague banned verb
|
|
23
|
-
const suffixRequiredVerbs = new Set(['to', 'with', 'make', 'use', 'on']);
|
|
24
|
-
|
|
25
|
-
function findBannedVerb(name) {
|
|
26
|
-
for (const [verb, replacement] of bannedVerbs) {
|
|
27
|
-
if (isVerbMatch(name, verb)) {
|
|
28
|
-
return { verb, replacement };
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return name === 'handle' ? { verb: 'handle', replacement: null } : null;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function planReport(name, exemptNames) {
|
|
36
|
-
if (exemptNames.has(name) || !/^[a-z]/u.test(name)) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (suffixRequiredVerbs.has(name)) {
|
|
41
|
-
return { messageId: 'bareTemplate', data: { name } };
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const banned = findBannedVerb(name);
|
|
45
|
-
|
|
46
|
-
if (banned === null) {
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (banned.replacement === null) {
|
|
51
|
-
return { messageId: 'vagueVerb', data: { name, verb: banned.verb } };
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return {
|
|
55
|
-
messageId: 'bannedVerb',
|
|
56
|
-
data: { name, verb: banned.verb, replacement: banned.replacement },
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
4
|
const functionVerb = {
|
|
61
5
|
meta: {
|
|
62
6
|
type: 'suggestion',
|
|
@@ -96,3 +40,59 @@ const functionVerb = {
|
|
|
96
40
|
};
|
|
97
41
|
|
|
98
42
|
export default functionVerb;
|
|
43
|
+
|
|
44
|
+
// the role table's template prefixes (`with<X>`, `handle<Event>`, …): each is
|
|
45
|
+
// only valid with a suffix, and a bare `handle` is the vague banned verb
|
|
46
|
+
const suffixRequiredVerbs = new Set(['to', 'with', 'make', 'use', 'on']);
|
|
47
|
+
|
|
48
|
+
function planReport(name, exemptNames) {
|
|
49
|
+
if (exemptNames.has(name) || !/^[a-z]/u.test(name)) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (suffixRequiredVerbs.has(name)) {
|
|
54
|
+
return { messageId: 'bareTemplate', data: { name } };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const banned = findBannedVerb(name);
|
|
58
|
+
|
|
59
|
+
if (banned === null) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (banned.replacement === null) {
|
|
64
|
+
return { messageId: 'vagueVerb', data: { name, verb: banned.verb } };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
messageId: 'bannedVerb',
|
|
69
|
+
data: { name, verb: banned.verb, replacement: banned.replacement },
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// banned verb → the role-table verb to use instead; null means the verb is
|
|
74
|
+
// too vague to map and the name should say what the function does
|
|
75
|
+
const bannedVerbs = new Map([
|
|
76
|
+
['process', null],
|
|
77
|
+
['manage', null],
|
|
78
|
+
['do', null],
|
|
79
|
+
['perform', null],
|
|
80
|
+
['execute', 'run'],
|
|
81
|
+
['compute', 'build'],
|
|
82
|
+
['fetch', 'read'],
|
|
83
|
+
['save', 'write'],
|
|
84
|
+
['store', 'write'],
|
|
85
|
+
['delete', 'remove'],
|
|
86
|
+
['search', 'find'],
|
|
87
|
+
['lookup', 'find'],
|
|
88
|
+
]);
|
|
89
|
+
|
|
90
|
+
function findBannedVerb(name) {
|
|
91
|
+
for (const [verb, replacement] of bannedVerbs) {
|
|
92
|
+
if (isVerbMatch(name, verb)) {
|
|
93
|
+
return { verb, replacement };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return name === 'handle' ? { verb: 'handle', replacement: null } : null;
|
|
98
|
+
}
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import makeGlobMatcher from './make-glob-matcher.js';
|
|
2
2
|
|
|
3
|
-
const defaultExtensions = ['.ts', '.tsx', '.mts', '.cts', '.js', '.jsx', '.mjs', '.cjs'];
|
|
4
|
-
|
|
5
3
|
const loadedOptions = new Map();
|
|
6
4
|
|
|
7
5
|
// oxlint hands each file its own copy of the options, so the compiled form is
|
|
8
6
|
// keyed by content
|
|
9
|
-
export default function
|
|
7
|
+
export default function getZoneOptions(options) {
|
|
10
8
|
const key = JSON.stringify(options);
|
|
11
9
|
let loaded = loadedOptions.get(key);
|
|
12
10
|
|
|
@@ -19,6 +17,8 @@ export default function loadZoneOptions(options) {
|
|
|
19
17
|
return loaded;
|
|
20
18
|
}
|
|
21
19
|
|
|
20
|
+
const defaultExtensions = ['.ts', '.tsx', '.mts', '.cts', '.js', '.jsx', '.mjs', '.cjs'];
|
|
21
|
+
|
|
22
22
|
function buildZoneOptions(options) {
|
|
23
23
|
const globsByZone = options.zones ?? {};
|
|
24
24
|
const zones = Object.keys(globsByZone).map((name) => ({
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
type Names = readonly string[];
|
|
2
|
+
|
|
3
|
+
export interface ImportZonesEntry {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly from?: Names;
|
|
6
|
+
readonly ignore?: Names;
|
|
7
|
+
readonly reason?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface ImportZonesEdgeBase extends ImportZonesEntry {
|
|
11
|
+
readonly from: Names;
|
|
12
|
+
readonly allowTypeOnly?: boolean | Names;
|
|
13
|
+
readonly allowWithin?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type ImportZonesEdge = ImportZonesEdgeBase &
|
|
17
|
+
({ readonly to: Names; readonly allow?: Names } | { readonly to?: Names; readonly allow: Names });
|
|
18
|
+
|
|
19
|
+
export interface ImportZonesModuleEntry extends ImportZonesEntry {
|
|
20
|
+
readonly modules: Names;
|
|
21
|
+
readonly allow: Names;
|
|
22
|
+
readonly allowTypeOnly?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ImportZonesGlobalEntry extends ImportZonesEntry {
|
|
26
|
+
readonly globals: Names;
|
|
27
|
+
readonly allow: Names;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ImportZonesUnzonedEntry extends ImportZonesEntry {
|
|
31
|
+
readonly from: Names;
|
|
32
|
+
readonly zones: Names;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ImportZonesOptions {
|
|
36
|
+
readonly zones?: Readonly<Record<string, Names>>;
|
|
37
|
+
readonly extensions?: Names;
|
|
38
|
+
readonly conditions?: Names;
|
|
39
|
+
readonly workspaces?: Names;
|
|
40
|
+
readonly edges?: readonly ImportZonesEdge[];
|
|
41
|
+
readonly modules?: readonly ImportZonesModuleEntry[];
|
|
42
|
+
readonly globals?: readonly ImportZonesGlobalEntry[];
|
|
43
|
+
readonly unzoned?: readonly ImportZonesUnzonedEntry[];
|
|
44
|
+
readonly unresolved?: readonly ImportZonesEntry[];
|
|
45
|
+
readonly selfImports?: readonly ImportZonesEntry[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface EdgeImport {
|
|
49
|
+
readonly from: string;
|
|
50
|
+
readonly to: string;
|
|
51
|
+
readonly typeOnly?: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export declare const importZonesSchema: Readonly<Record<string, unknown>>;
|
|
55
|
+
|
|
56
|
+
export declare function collectRefusingEdges(
|
|
57
|
+
options: ImportZonesOptions,
|
|
58
|
+
edgeImport: EdgeImport,
|
|
59
|
+
): ImportZonesEdge[];
|
package/import-zones-schema.js
CHANGED
|
@@ -1,25 +1,6 @@
|
|
|
1
1
|
// the options of zgeoff/import-zones; oxlint rejects a config that breaks it
|
|
2
2
|
const stringList = { type: 'array', items: { type: 'string' } };
|
|
3
3
|
|
|
4
|
-
function buildEntrySchema(properties, required, anyOf) {
|
|
5
|
-
return {
|
|
6
|
-
type: 'array',
|
|
7
|
-
items: {
|
|
8
|
-
type: 'object',
|
|
9
|
-
properties: {
|
|
10
|
-
name: { type: 'string' },
|
|
11
|
-
from: stringList,
|
|
12
|
-
ignore: stringList,
|
|
13
|
-
reason: { type: 'string' },
|
|
14
|
-
...properties,
|
|
15
|
-
},
|
|
16
|
-
required: ['name', ...required],
|
|
17
|
-
additionalProperties: false,
|
|
18
|
-
...(anyOf === undefined ? {} : { anyOf }),
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
|
|
23
4
|
const importZonesSchema = {
|
|
24
5
|
type: 'object',
|
|
25
6
|
properties: {
|
|
@@ -50,3 +31,22 @@ const importZonesSchema = {
|
|
|
50
31
|
};
|
|
51
32
|
|
|
52
33
|
export default importZonesSchema;
|
|
34
|
+
|
|
35
|
+
function buildEntrySchema(properties, required, anyOf) {
|
|
36
|
+
return {
|
|
37
|
+
type: 'array',
|
|
38
|
+
items: {
|
|
39
|
+
type: 'object',
|
|
40
|
+
properties: {
|
|
41
|
+
name: { type: 'string' },
|
|
42
|
+
from: stringList,
|
|
43
|
+
ignore: stringList,
|
|
44
|
+
reason: { type: 'string' },
|
|
45
|
+
...properties,
|
|
46
|
+
},
|
|
47
|
+
required: ['name', ...required],
|
|
48
|
+
additionalProperties: false,
|
|
49
|
+
...(anyOf === undefined ? {} : { anyOf }),
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
package/import-zones.js
CHANGED
|
@@ -1,16 +1,91 @@
|
|
|
1
1
|
import { isBuiltin } from 'node:module';
|
|
2
2
|
import { relative, sep } from 'node:path';
|
|
3
|
+
import collectZoneNames from './collect-zone-names.js';
|
|
4
|
+
import getZoneOptions from './get-zone-options.js';
|
|
3
5
|
import importZonesSchema from './import-zones-schema.js';
|
|
6
|
+
import isEdgeRefusing from './is-edge-refusing.js';
|
|
7
|
+
import isEntryApplying from './is-entry-applying.js';
|
|
8
|
+
import isInAnyZone from './is-in-any-zone.js';
|
|
4
9
|
import loadWorkspaceManifests from './load-workspace-manifests.js';
|
|
5
|
-
import loadZoneOptions from './load-zone-options.js';
|
|
6
10
|
import resolveImportTarget from './resolve-import-target.js';
|
|
7
11
|
import resolveRepoRoot from './resolve-repo-root.js';
|
|
8
12
|
|
|
13
|
+
const importZones = {
|
|
14
|
+
meta: {
|
|
15
|
+
type: 'problem',
|
|
16
|
+
messages: {
|
|
17
|
+
edge: '{{rule}}: {{from}} must not import {{target}} ({{to}}){{reason}}',
|
|
18
|
+
module: '{{rule}}: {{module}} is allowed only in {{allow}}{{reason}}',
|
|
19
|
+
global: '{{rule}}: {{global}}() is allowed only in {{allow}}{{reason}}',
|
|
20
|
+
unzoned: '{{rule}}: this file belongs to none of {{zones}}; add its path to one{{reason}}',
|
|
21
|
+
unresolved: '{{rule}}: cannot resolve {{specifier}}{{reason}}',
|
|
22
|
+
self: '{{rule}}: {{specifier}} names the package this file belongs to; import it by a relative path{{reason}}',
|
|
23
|
+
computed: '{{rule}}: cannot check a computed import specifier{{reason}}',
|
|
24
|
+
},
|
|
25
|
+
schema: [importZonesSchema],
|
|
26
|
+
},
|
|
27
|
+
create(context) {
|
|
28
|
+
const file = loadFileState(context);
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
Program(node) {
|
|
32
|
+
checkUnzoned(file, node);
|
|
33
|
+
},
|
|
34
|
+
ImportDeclaration(node) {
|
|
35
|
+
checkImport(file, { node, source: node.source, typeOnly: isTypeOnlyImport(node) });
|
|
36
|
+
},
|
|
37
|
+
ExportNamedDeclaration(node) {
|
|
38
|
+
if (node.source !== null) {
|
|
39
|
+
checkImport(file, { node, source: node.source, typeOnly: isTypeOnlyExport(node) });
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
ExportAllDeclaration(node) {
|
|
43
|
+
checkImport(file, { node, source: node.source, typeOnly: node.exportKind === 'type' });
|
|
44
|
+
},
|
|
45
|
+
ImportExpression(node) {
|
|
46
|
+
checkImport(file, { node, source: node.source, typeOnly: false });
|
|
47
|
+
},
|
|
48
|
+
TSImportType(node) {
|
|
49
|
+
checkImport(file, { node, source: node.source ?? node.argument?.literal, typeOnly: true });
|
|
50
|
+
},
|
|
51
|
+
TSImportEqualsDeclaration(node) {
|
|
52
|
+
const reference = node.moduleReference;
|
|
53
|
+
|
|
54
|
+
if (reference.type === 'TSExternalModuleReference') {
|
|
55
|
+
const typeOnly = node.importKind === 'type';
|
|
56
|
+
|
|
57
|
+
checkImport(file, { node, source: reference.expression, typeOnly });
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
CallExpression(node) {
|
|
61
|
+
checkGlobalCall(file, node);
|
|
62
|
+
|
|
63
|
+
if (
|
|
64
|
+
(node.callee.type === 'Identifier' && node.callee.name === 'require') ||
|
|
65
|
+
isImportMetaRequire(node.callee)
|
|
66
|
+
) {
|
|
67
|
+
checkImport(file, { node, source: node.arguments[0], typeOnly: false });
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
// Bun's import.meta.require used as a value, such as `const load =
|
|
72
|
+
// import.meta.require`, loads modules no specifier names
|
|
73
|
+
MemberExpression(node) {
|
|
74
|
+
if (isImportMetaRequire(node) && node.parent?.callee !== node) {
|
|
75
|
+
emitUnresolved(file, node, { messageId: 'computed', data: {} });
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export default importZones;
|
|
83
|
+
|
|
9
84
|
// Judges each import against named zones: path globs relative to the repo
|
|
10
85
|
// root, which may overlap. A file belongs to every zone its path matches.
|
|
11
86
|
|
|
12
87
|
function loadFileState(context) {
|
|
13
|
-
const options =
|
|
88
|
+
const options = getZoneOptions(context.options[0] ?? {});
|
|
14
89
|
const filename = context.physicalFilename;
|
|
15
90
|
const root = resolveRepoRoot(filename) ?? context.cwd;
|
|
16
91
|
const path = toRootPath(root, filename);
|
|
@@ -24,7 +99,7 @@ function loadFileState(context) {
|
|
|
24
99
|
root,
|
|
25
100
|
path,
|
|
26
101
|
workspaces,
|
|
27
|
-
zones:
|
|
102
|
+
zones: collectZoneNames(options.zones, path),
|
|
28
103
|
};
|
|
29
104
|
}
|
|
30
105
|
|
|
@@ -32,20 +107,24 @@ function toRootPath(root, path) {
|
|
|
32
107
|
return relative(root, path).split(sep).join('/');
|
|
33
108
|
}
|
|
34
109
|
|
|
35
|
-
function
|
|
36
|
-
|
|
37
|
-
|
|
110
|
+
function checkUnzoned(file, program) {
|
|
111
|
+
// anchor on the first statement, as test-beside-module does, so a disable
|
|
112
|
+
// comment at the top of the file covers the report
|
|
113
|
+
const start = program.body[0]?.loc.start ?? { line: 1, column: 0 };
|
|
38
114
|
|
|
39
|
-
|
|
40
|
-
|
|
115
|
+
for (const entry of collectEntries(file, file.options.unzoned)) {
|
|
116
|
+
if (!isInAnyZone(file.zones, entry.zones)) {
|
|
117
|
+
emitReport(file, entry, {
|
|
118
|
+
at: { loc: { start, end: start } },
|
|
119
|
+
messageId: 'unzoned',
|
|
120
|
+
data: { zones: formatZones(entry.zones) },
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
41
124
|
}
|
|
42
125
|
|
|
43
|
-
// an entry without `from` applies to every file
|
|
44
126
|
function collectEntries(file, entries) {
|
|
45
|
-
return entries.filter(
|
|
46
|
-
(entry) =>
|
|
47
|
-
(entry.from === undefined || isInAny(file.zones, entry.from)) && !entry.ignore(file.path),
|
|
48
|
-
);
|
|
127
|
+
return entries.filter((entry) => isEntryApplying(entry, file.path, file.zones));
|
|
49
128
|
}
|
|
50
129
|
|
|
51
130
|
// `at` is `{ node }` or `{ loc }`
|
|
@@ -121,7 +200,7 @@ function emitSelfImport(file, node, specifier) {
|
|
|
121
200
|
function checkModule(file, imported, specifier) {
|
|
122
201
|
for (const entry of collectEntries(file, file.options.modules)) {
|
|
123
202
|
const isAllowed =
|
|
124
|
-
|
|
203
|
+
isInAnyZone(file.zones, entry.allow) || (imported.typeOnly && entry.allowTypeOnly === true);
|
|
125
204
|
|
|
126
205
|
if (!isAllowed && isRestrictedModule(entry, specifier)) {
|
|
127
206
|
emitReport(file, entry, {
|
|
@@ -147,6 +226,10 @@ function isRestrictedModule(entry, specifier) {
|
|
|
147
226
|
});
|
|
148
227
|
}
|
|
149
228
|
|
|
229
|
+
function toModuleName(specifier) {
|
|
230
|
+
return specifier.replace(/^node:/u, '');
|
|
231
|
+
}
|
|
232
|
+
|
|
150
233
|
// `node:*` names every builtin, which a specifier may write without the prefix
|
|
151
234
|
function isPrefixMatch(prefix, specifier) {
|
|
152
235
|
if (specifier.startsWith(prefix)) {
|
|
@@ -156,58 +239,50 @@ function isPrefixMatch(prefix, specifier) {
|
|
|
156
239
|
return prefix === 'node:' && isBuiltin(specifier);
|
|
157
240
|
}
|
|
158
241
|
|
|
159
|
-
|
|
160
|
-
return specifier.replace(/^node:/u, '');
|
|
161
|
-
}
|
|
162
|
-
|
|
242
|
+
// collectRefusingEdges judges an edge the same way, through isEdgeRefusing
|
|
163
243
|
function checkEdge(file, imported, target) {
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
(entry.allowTypeOnly === true || entry.typeOnlyTargets?.(target) === true);
|
|
175
|
-
|
|
176
|
-
if (isForbidden && !isTypeExempt && entry.within?.(file.path, target) !== true) {
|
|
244
|
+
const edgeImport = {
|
|
245
|
+
from: file.path,
|
|
246
|
+
to: target,
|
|
247
|
+
fromZones: file.zones,
|
|
248
|
+
toZones: collectZoneNames(file.options.zones, target),
|
|
249
|
+
typeOnly: imported.typeOnly,
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
for (const entry of file.options.edges) {
|
|
253
|
+
if (isEdgeRefusing(entry, edgeImport)) {
|
|
177
254
|
emitReport(file, entry, {
|
|
178
255
|
at: { node: imported.node },
|
|
179
256
|
messageId: 'edge',
|
|
180
257
|
data: {
|
|
181
258
|
from: formatZones(entry.from.filter((name) => file.zones.includes(name))),
|
|
182
259
|
target,
|
|
183
|
-
to: formatZones(
|
|
260
|
+
to: formatZones(edgeImport.toZones),
|
|
184
261
|
},
|
|
185
262
|
});
|
|
186
263
|
}
|
|
187
264
|
}
|
|
188
265
|
}
|
|
189
266
|
|
|
190
|
-
function
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
267
|
+
function isTypeOnlyImport(node) {
|
|
268
|
+
return (
|
|
269
|
+
node.importKind === 'type' ||
|
|
270
|
+
(node.specifiers.length > 0 && node.specifiers.every((entry) => entry.importKind === 'type'))
|
|
271
|
+
);
|
|
272
|
+
}
|
|
194
273
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
data: { zones: formatZones(entry.zones) },
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
}
|
|
274
|
+
function isTypeOnlyExport(node) {
|
|
275
|
+
return (
|
|
276
|
+
node.exportKind === 'type' ||
|
|
277
|
+
(node.specifiers.length > 0 && node.specifiers.every((entry) => entry.exportKind === 'type'))
|
|
278
|
+
);
|
|
204
279
|
}
|
|
205
280
|
|
|
206
281
|
function checkGlobalCall(file, node) {
|
|
207
282
|
for (const entry of collectEntries(file, file.options.globals)) {
|
|
208
283
|
const global = findGlobalCallName(file, node, entry.globals);
|
|
209
284
|
|
|
210
|
-
if (global !== null && !
|
|
285
|
+
if (global !== null && !isInAnyZone(file.zones, entry.allow)) {
|
|
211
286
|
emitReport(file, entry, {
|
|
212
287
|
at: { node },
|
|
213
288
|
messageId: 'global',
|
|
@@ -257,20 +332,6 @@ function isLocalBinding(file, node, name) {
|
|
|
257
332
|
return false;
|
|
258
333
|
}
|
|
259
334
|
|
|
260
|
-
function isTypeOnlyImport(node) {
|
|
261
|
-
return (
|
|
262
|
-
node.importKind === 'type' ||
|
|
263
|
-
(node.specifiers.length > 0 && node.specifiers.every((entry) => entry.importKind === 'type'))
|
|
264
|
-
);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
function isTypeOnlyExport(node) {
|
|
268
|
-
return (
|
|
269
|
-
node.exportKind === 'type' ||
|
|
270
|
-
(node.specifiers.length > 0 && node.specifiers.every((entry) => entry.exportKind === 'type'))
|
|
271
|
-
);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
335
|
function isImportMetaRequire(node) {
|
|
275
336
|
return (
|
|
276
337
|
node.type === 'MemberExpression' &&
|
|
@@ -282,74 +343,3 @@ function isImportMetaRequire(node) {
|
|
|
282
343
|
node.property.name === 'require'
|
|
283
344
|
);
|
|
284
345
|
}
|
|
285
|
-
|
|
286
|
-
const importZones = {
|
|
287
|
-
meta: {
|
|
288
|
-
type: 'problem',
|
|
289
|
-
messages: {
|
|
290
|
-
edge: '{{rule}}: {{from}} must not import {{target}} ({{to}}){{reason}}',
|
|
291
|
-
module: '{{rule}}: {{module}} is allowed only in {{allow}}{{reason}}',
|
|
292
|
-
global: '{{rule}}: {{global}}() is allowed only in {{allow}}{{reason}}',
|
|
293
|
-
unzoned: '{{rule}}: this file belongs to none of {{zones}}; add its path to one{{reason}}',
|
|
294
|
-
unresolved: '{{rule}}: cannot resolve {{specifier}}{{reason}}',
|
|
295
|
-
self: '{{rule}}: {{specifier}} names the package this file belongs to; import it by a relative path{{reason}}',
|
|
296
|
-
computed: '{{rule}}: cannot check a computed import specifier{{reason}}',
|
|
297
|
-
},
|
|
298
|
-
schema: [importZonesSchema],
|
|
299
|
-
},
|
|
300
|
-
create(context) {
|
|
301
|
-
const file = loadFileState(context);
|
|
302
|
-
|
|
303
|
-
return {
|
|
304
|
-
Program(node) {
|
|
305
|
-
checkUnzoned(file, node);
|
|
306
|
-
},
|
|
307
|
-
ImportDeclaration(node) {
|
|
308
|
-
checkImport(file, { node, source: node.source, typeOnly: isTypeOnlyImport(node) });
|
|
309
|
-
},
|
|
310
|
-
ExportNamedDeclaration(node) {
|
|
311
|
-
if (node.source !== null) {
|
|
312
|
-
checkImport(file, { node, source: node.source, typeOnly: isTypeOnlyExport(node) });
|
|
313
|
-
}
|
|
314
|
-
},
|
|
315
|
-
ExportAllDeclaration(node) {
|
|
316
|
-
checkImport(file, { node, source: node.source, typeOnly: node.exportKind === 'type' });
|
|
317
|
-
},
|
|
318
|
-
ImportExpression(node) {
|
|
319
|
-
checkImport(file, { node, source: node.source, typeOnly: false });
|
|
320
|
-
},
|
|
321
|
-
TSImportType(node) {
|
|
322
|
-
checkImport(file, { node, source: node.source ?? node.argument?.literal, typeOnly: true });
|
|
323
|
-
},
|
|
324
|
-
TSImportEqualsDeclaration(node) {
|
|
325
|
-
const reference = node.moduleReference;
|
|
326
|
-
|
|
327
|
-
if (reference.type === 'TSExternalModuleReference') {
|
|
328
|
-
const typeOnly = node.importKind === 'type';
|
|
329
|
-
|
|
330
|
-
checkImport(file, { node, source: reference.expression, typeOnly });
|
|
331
|
-
}
|
|
332
|
-
},
|
|
333
|
-
CallExpression(node) {
|
|
334
|
-
checkGlobalCall(file, node);
|
|
335
|
-
|
|
336
|
-
if (
|
|
337
|
-
(node.callee.type === 'Identifier' && node.callee.name === 'require') ||
|
|
338
|
-
isImportMetaRequire(node.callee)
|
|
339
|
-
) {
|
|
340
|
-
checkImport(file, { node, source: node.arguments[0], typeOnly: false });
|
|
341
|
-
}
|
|
342
|
-
},
|
|
343
|
-
|
|
344
|
-
// Bun's import.meta.require used as a value, such as `const load =
|
|
345
|
-
// import.meta.require`, loads modules no specifier names
|
|
346
|
-
MemberExpression(node) {
|
|
347
|
-
if (isImportMetaRequire(node) && node.parent?.callee !== node) {
|
|
348
|
-
emitUnresolved(file, node, { messageId: 'computed', data: {} });
|
|
349
|
-
}
|
|
350
|
-
},
|
|
351
|
-
};
|
|
352
|
-
},
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
export default importZones;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import isEntryApplying from './is-entry-applying.js';
|
|
2
|
+
import isInAnyZone from './is-in-any-zone.js';
|
|
3
|
+
|
|
4
|
+
// `entry` is a compiled edge; `edgeImport` is `{ from, to, fromZones,
|
|
5
|
+
// toZones, typeOnly }`, both paths relative to the repo root
|
|
6
|
+
export default function isEdgeRefusing(entry, edgeImport) {
|
|
7
|
+
// `to` picks the targets judged, every target when absent; `allow` exempts
|
|
8
|
+
// some of them. The schema needs one of the two.
|
|
9
|
+
const isForbidden =
|
|
10
|
+
(entry.to === undefined || isInAnyZone(edgeImport.toZones, entry.to)) &&
|
|
11
|
+
(entry.allow === undefined || !isInAnyZone(edgeImport.toZones, entry.allow));
|
|
12
|
+
const isTypeExempt =
|
|
13
|
+
edgeImport.typeOnly &&
|
|
14
|
+
(entry.allowTypeOnly === true || entry.typeOnlyTargets?.(edgeImport.to) === true);
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
isEntryApplying(entry, edgeImport.from, edgeImport.fromZones) &&
|
|
18
|
+
isForbidden &&
|
|
19
|
+
!isTypeExempt &&
|
|
20
|
+
entry.within?.(edgeImport.from, edgeImport.to) !== true
|
|
21
|
+
);
|
|
22
|
+
}
|