@zgeoff/oxlint-config 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -2
- 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 +40 -31
- package/no-hidden-cast.js +71 -0
- package/no-jsdoc.js +15 -15
- package/oxlintrc.json +8 -2
- package/package.json +15 -3
- package/plugin.js +20 -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 +32 -32
- package/verb-contract.js +93 -93
package/README.md
CHANGED
|
@@ -5,8 +5,8 @@ restriction-rule cherry-picks, comment-style enforcement via `@stylistic`, and a
|
|
|
5
5
|
(`zgeoff/*` rules) banning shapes no native oxlint rule can express — top-level arrows, nested
|
|
6
6
|
function declarations, bare named exports, destructured and inline-typed parameters, ternary and
|
|
7
7
|
await arguments, awaits hidden inside a control-flow condition or a `&&`/`||`/`??` chain, every
|
|
8
|
-
`switch` statement, every JSDoc block, any run of more than three line comments,
|
|
9
|
-
that loosens
|
|
8
|
+
`switch` statement, every JSDoc block, any run of more than three line comments, a type claim with
|
|
9
|
+
no `as`, and a repo config that loosens a comment rule or a type-assertion rule.
|
|
10
10
|
|
|
11
11
|
The plugin's other rules each check one convention. `zgeoff/function-verb` bans the vague and
|
|
12
12
|
near-synonym verbs of the shared function-naming rules, `zgeoff/verb-contract` checks the promise
|
|
@@ -45,6 +45,37 @@ import config from '@zgeoff/oxlint-config';
|
|
|
45
45
|
The plugin is addressable on its own at `@zgeoff/oxlint-config/plugin` for configs that want the
|
|
46
46
|
`zgeoff/*` rules without the rest.
|
|
47
47
|
|
|
48
|
+
## Type assertions
|
|
49
|
+
|
|
50
|
+
`typescript/consistent-type-assertions` runs with `assertionStyle: "never"`, so every type assertion
|
|
51
|
+
fails: `x as T`, `<T>x` and `x as unknown as T`. `as const` and `satisfies` pass, because neither
|
|
52
|
+
claims a type the checker cannot prove. `typescript/no-unsafe-type-assertion` is off, because the
|
|
53
|
+
ban reports every assertion that rule checks.
|
|
54
|
+
|
|
55
|
+
Replace an assertion with a type guard, a type annotation, `satisfies`, or a typed helper from
|
|
56
|
+
`@zgeoff/std`. An assertion that survives carries a disable comment on the line above it, with its
|
|
57
|
+
approval and reason after `--`:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
// oxlint-disable-next-line typescript/consistent-type-assertions -- approved <issue>: <reason>
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`zgeoff/no-hidden-cast` reports a claim of a type that has no `as` for the ban to see:
|
|
64
|
+
|
|
65
|
+
- a type argument on a `sql` tagged template, such as Kysely's `sql<T>`;
|
|
66
|
+
- a call to Kysely's `$castTo()`, `$narrowType()` or `$assertType()`;
|
|
67
|
+
- a type argument on a `query()` or `prepare()` call, such as bun:sqlite's;
|
|
68
|
+
- a type argument on Pulumi `Config`'s `getObject()`, `requireObject()`, `getSecretObject()` or
|
|
69
|
+
`requireSecretObject()`.
|
|
70
|
+
|
|
71
|
+
The rule needs no type information, so it matches by name: a method of another library with the same
|
|
72
|
+
name and a type argument is reported too. Read such a value as `unknown` and parse it with zod, or
|
|
73
|
+
query through Kysely over declared table types.
|
|
74
|
+
|
|
75
|
+
Every repo runs both rules as this config ships them. `zgeoff/no-comment-rule-override` reports a
|
|
76
|
+
repo-root `.oxlintrc*.json` that sets either rule under any plugin prefix, so a disable comment on
|
|
77
|
+
the line is the only way to keep an assertion.
|
|
78
|
+
|
|
48
79
|
## `zgeoff/function-verb`
|
|
49
80
|
|
|
50
81
|
The rule reports a function, method, or function-valued variable or property whose name starts with
|
|
@@ -294,6 +325,15 @@ Exempt a known violation with a next-line disable comment that names the ticket
|
|
|
294
325
|
unzoned report sits on the first statement, so its comment goes above that. Run lint with
|
|
295
326
|
`--report-unused-disable-directives-severity error` so a comment outlives no import.
|
|
296
327
|
|
|
328
|
+
The rule judges only the imports that a file holds. A repo check that judges something else, such as
|
|
329
|
+
a dependency that a manifest declares, imports the rule's own reading from
|
|
330
|
+
`@zgeoff/oxlint-config/import-zones`. That entry exports `importZonesSchema`, the JSON schema of the
|
|
331
|
+
options, with an `ImportZonesOptions` type, and
|
|
332
|
+
`collectRefusingEdges(options, { from, to, typeOnly })`. The function returns each edge in `options`
|
|
333
|
+
that refuses an import of `to` from `from`, both paths relative to the repo root. It skips module
|
|
334
|
+
resolution, so `to` is the path that the rule would resolve the specifier to. The rule judges each
|
|
335
|
+
edge through the same function.
|
|
336
|
+
|
|
297
337
|
## Notes
|
|
298
338
|
|
|
299
339
|
- 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;
|