@zgeoff/oxlint-config 0.3.0 → 0.5.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 +72 -16
- package/build-module-order-fix.js +262 -0
- package/collect-module-entries.js +213 -0
- package/find-function-name.js +28 -0
- package/function-verb.js +21 -146
- package/import-zones.js +4 -4
- package/is-static-value.js +116 -0
- package/is-verb-match.js +5 -0
- package/module-order.js +307 -0
- package/no-comment-rule-override.js +2 -2
- package/no-file-snapshot.js +2 -2
- package/oxlintrc.json +2 -0
- package/package.json +9 -2
- package/plugin.js +4 -0
- package/resolve-import-target.js +7 -7
- package/{find-repo-root.js → resolve-repo-root.js} +1 -1
- package/test-beside-module.js +6 -6
- package/verb-contract.js +609 -0
package/README.md
CHANGED
|
@@ -6,13 +6,17 @@ restriction-rule cherry-picks, comment-style enforcement via `@stylistic`, and a
|
|
|
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
8
|
`switch` statement, every JSDoc block, any run of more than three line comments, and a repo config
|
|
9
|
-
that loosens either comment rule.
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
`zgeoff/
|
|
14
|
-
`zgeoff/
|
|
15
|
-
|
|
9
|
+
that loosens either comment rule.
|
|
10
|
+
|
|
11
|
+
The plugin's other rules each check one convention. `zgeoff/function-verb` bans the vague and
|
|
12
|
+
near-synonym verbs of the shared function-naming rules, `zgeoff/verb-contract` checks the promise
|
|
13
|
+
each role-table verb makes, `zgeoff/module-order` keeps a file in the shared module order,
|
|
14
|
+
`zgeoff/test-beside-module` keeps each test file beside the module it tests, `zgeoff/import-zones`
|
|
15
|
+
fails an import that crosses a module boundary the repo declares, `zgeoff/no-restricted-words` keeps
|
|
16
|
+
the words in its `words` option out of a path's text, `zgeoff/no-abbreviated-names` reports each
|
|
17
|
+
abbreviated value name it lists with its full word, `zgeoff/no-file-snapshot` bans file snapshots,
|
|
18
|
+
`zgeoff/setup-test-only` keeps a test file's module level to `setupTest()` and its tests, and
|
|
19
|
+
`zgeoff/no-class` bans every class but a custom error.
|
|
16
20
|
|
|
17
21
|
## Usage
|
|
18
22
|
|
|
@@ -43,23 +47,75 @@ The plugin is addressable on its own at `@zgeoff/oxlint-config/plugin` for confi
|
|
|
43
47
|
|
|
44
48
|
## `zgeoff/function-verb`
|
|
45
49
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
`
|
|
49
|
-
|
|
50
|
-
Exempt an algorithm-native name (`walk`, `backtrack`) with a disable comment in the module
|
|
51
|
-
implementing that algorithm.
|
|
52
|
-
|
|
53
|
-
Options extend the shipped set per repo:
|
|
50
|
+
The rule reports a function, method, or function-valued variable or property whose name starts with
|
|
51
|
+
a banned verb, and it names the role-table verb to use instead (`fetchUser` → use `read`). It also
|
|
52
|
+
reports a bare template prefix, such as a function named `on`. A name that does not start with a
|
|
53
|
+
lowercase letter passes. The `exemptNames` option lists names that a library contract fixes:
|
|
54
54
|
|
|
55
55
|
```jsonc
|
|
56
56
|
{
|
|
57
57
|
"rules": {
|
|
58
|
-
"zgeoff/function-verb": ["error", { "
|
|
58
|
+
"zgeoff/function-verb": ["error", { "exemptNames": ["execute"] }],
|
|
59
59
|
},
|
|
60
60
|
}
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
+
## `zgeoff/verb-contract`
|
|
64
|
+
|
|
65
|
+
The rule reads each named function's verb and reports a body that breaks the verb's promise. It
|
|
66
|
+
reads declared return types, since it runs without type information. It runs three checks:
|
|
67
|
+
|
|
68
|
+
- **No I/O behind a no-I/O verb.** A function named with `is`, `has`, `can`, `should`, `build`,
|
|
69
|
+
`parse`, `format`, `render`, `pick`, `find`, `get`, `collect` or `to` must not be async or await.
|
|
70
|
+
It must not call `fs`, `child_process`, `Bun.spawn`, `Bun.file`, `Bun.write`, Bun's `$` shell
|
|
71
|
+
under any import form, `fetch` or a Kysely `execute`, and must not read `process.env` or
|
|
72
|
+
`Bun.env`. It must not call a function whose verb is on the rule's effectful list, such as
|
|
73
|
+
`readConfig` or `ctx.writeText`. That check skips a callee imported from a package, a global, a
|
|
74
|
+
bare method name such as `map.set`, and a method on a value the function declares itself. I/O
|
|
75
|
+
inside an unnamed callback counts against the named function around it. A local alias of an I/O
|
|
76
|
+
source, such as `const read = fs.readFileSync` or `const { spawn } = Bun`, counts as that source,
|
|
77
|
+
and a local alias of a package's callee is skipped as the callee is.
|
|
78
|
+
- **Predicates return a boolean.** A function named with `is`, `has`, `can`, `should` or `probe`
|
|
79
|
+
that declares a return type declares `boolean`, a type predicate, or a promise of one. A function
|
|
80
|
+
that declares `boolean` and does no I/O starts with one of the four no-I/O predicate verbs. That
|
|
81
|
+
check skips an effectful verb or `try`, whose function may return a success flag, and the
|
|
82
|
+
framework prefixes `use`, `on` and `handle`.
|
|
83
|
+
- **`find` can miss, and `get` cannot.** A declared `find` return type holds `null`, `undefined`,
|
|
84
|
+
`void`, `unknown` or `any`. A `get` function neither declares `null` or `undefined` nor returns
|
|
85
|
+
either one as a literal or a bare `return`. Every check follows the file's own type aliases, and a
|
|
86
|
+
type the rule cannot see, such as an import, a type parameter or an indexed access, never fails a
|
|
87
|
+
check.
|
|
88
|
+
|
|
89
|
+
A test in this package reads the role table in the shared code-style skill and fails when the
|
|
90
|
+
skill's no-I/O verbs and the rule's differ in either direction.
|
|
91
|
+
|
|
92
|
+
## `zgeoff/module-order`
|
|
93
|
+
|
|
94
|
+
The rule checks the module order of the shared code-style skill in each file with a primary export.
|
|
95
|
+
The primary export is the default export, else the export whose name the file name kebab-cases, else
|
|
96
|
+
the file's only exported function or const. A file without one, such as a test file, passes. The
|
|
97
|
+
rule reports three breaks:
|
|
98
|
+
|
|
99
|
+
- **A helper first.** A private function sits above the primary export.
|
|
100
|
+
- **A helper out of composition order.** The helpers below the primary export follow it depth-first,
|
|
101
|
+
each in the order its first reference appears.
|
|
102
|
+
- **A declaration away from its first use.** A const, type alias, interface or enum sits below its
|
|
103
|
+
first use, or a helper sits between it and that use. A declaration the primary export uses may sit
|
|
104
|
+
just above it.
|
|
105
|
+
|
|
106
|
+
The config's `import/first` rule reports an import after other code, so this rule leaves imports
|
|
107
|
+
alone. The rule leaves a const unchecked when a helper that runs as the module loads reads it,
|
|
108
|
+
because the const has to sit above the code that calls that helper.
|
|
109
|
+
|
|
110
|
+
`--fix` moves each declaration it can move without a change in behaviour, with the comments above
|
|
111
|
+
it, in one pass. A function declaration is hoisted and a type has no value, so both always move. A
|
|
112
|
+
const moves only when every use sits in a function body that runs after the module loads, and its
|
|
113
|
+
value is static. A static value runs no code and reads nothing that load-time code can change: a
|
|
114
|
+
literal, a function, an array, object, `new Set`, `new Map` or `new RegExp` over static values, or
|
|
115
|
+
an operator or template over primitive values, with an import only as a copied value. Any other
|
|
116
|
+
const stays where it is, and the rule still reports it. The fix makes no change when a move would
|
|
117
|
+
put a statement that ends without a semicolon before one that opens with a bracket or an operator.
|
|
118
|
+
|
|
63
119
|
## `zgeoff/no-jsdoc` and `zgeoff/max-consecutive-line-comments`
|
|
64
120
|
|
|
65
121
|
A comment holds only what neither the code, a type, a test name, nor the subsystem doc can hold: the
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import isStaticValue from './is-static-value.js';
|
|
2
|
+
|
|
3
|
+
// one fix that moves every safe declaration the reports name, since oxlint
|
|
4
|
+
// fixes a file in one pass; `moved` holds the reported entries it moves, and
|
|
5
|
+
// `fix` is null when nothing can move
|
|
6
|
+
export default function buildModuleOrderFix(context, layout) {
|
|
7
|
+
const names = collectNames(context);
|
|
8
|
+
const reported = new Set(layout.reports.map((report) => report.entry));
|
|
9
|
+
const stacks = new Map();
|
|
10
|
+
const helpers = [];
|
|
11
|
+
const declarations = [];
|
|
12
|
+
|
|
13
|
+
for (const report of layout.reports) {
|
|
14
|
+
const entry = report.entry;
|
|
15
|
+
|
|
16
|
+
if (entry.kind === 'supporting' && isSafeToMove(entry, names)) {
|
|
17
|
+
declarations.push(entry);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (entry.kind === 'function' && layout.ranks.has(entry) && !entry.isOverloaded) {
|
|
21
|
+
stacks.set(entry, collectStack(entry, layout, { reported, names }));
|
|
22
|
+
helpers.push(entry);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const order = buildOrder(layout, { helpers, declarations, stacks });
|
|
27
|
+
const isUnchanged = order.every((entry, index) => entry === layout.entries[index]);
|
|
28
|
+
|
|
29
|
+
if (isUnchanged || isJoiningStatements(context.sourceCode.text, order)) {
|
|
30
|
+
return { fix: null, moved: new Set() };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const [range, text] = renderOrder(context, layout.entries, order);
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
fix: (fixer) => fixer.replaceTextRange(range, text),
|
|
37
|
+
moved: new Set([...helpers, ...declarations]),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// every name the file binds at its top level, so that a value can tell an
|
|
42
|
+
// import or a global from a local
|
|
43
|
+
function collectNames(context) {
|
|
44
|
+
const programScope = context.sourceCode.getScope(context.sourceCode.ast);
|
|
45
|
+
const scope = programScope.childScopes.find((child) => child.type === 'module') ?? programScope;
|
|
46
|
+
const imports = new Set();
|
|
47
|
+
const locals = new Set();
|
|
48
|
+
|
|
49
|
+
for (const variable of scope.variables) {
|
|
50
|
+
const isImport = variable.defs.some((definition) => definition.type === 'ImportBinding');
|
|
51
|
+
|
|
52
|
+
(isImport ? imports : locals).add(variable.name);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return { imports, locals };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// a type always moves safely; a const moves only when every use sits in a
|
|
59
|
+
// function body that runs after the module loads, and its value is static
|
|
60
|
+
function isSafeToMove(entry, names) {
|
|
61
|
+
const type = entry.declaration.type;
|
|
62
|
+
|
|
63
|
+
if (type === 'TSTypeAliasDeclaration' || type === 'TSInterfaceDeclaration') {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (type !== 'VariableDeclaration') {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const isUsedAfterLoad = entry.references.every(
|
|
72
|
+
(reference) =>
|
|
73
|
+
reference.source === entry ||
|
|
74
|
+
(reference.source !== null && !reference.source.isLive && !reference.isLoadTime),
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
return isUsedAfterLoad && isStaticValue(entry.declaration.declarations[0].init, names);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// the unreported consts and types whose home is the helper, which sit above
|
|
81
|
+
// it and move with it when they move safely; a function declaration is
|
|
82
|
+
// hoisted, so the helper itself always moves
|
|
83
|
+
function collectStack(helper, layout, context) {
|
|
84
|
+
return layout.entries.filter(
|
|
85
|
+
(entry) =>
|
|
86
|
+
entry.kind === 'supporting' &&
|
|
87
|
+
layout.homes.get(entry) === helper &&
|
|
88
|
+
!context.reported.has(entry) &&
|
|
89
|
+
isSafeToMove(entry, context.names),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// each moving helper, with its stack, goes directly after the entry before it
|
|
94
|
+
// in the main order; then each moving const or type goes directly above the
|
|
95
|
+
// first entry in its home's block that uses it
|
|
96
|
+
function buildOrder(layout, moves) {
|
|
97
|
+
const moving = new Set([
|
|
98
|
+
...moves.helpers.flatMap((helper) => [...moves.stacks.get(helper), helper]),
|
|
99
|
+
...moves.declarations,
|
|
100
|
+
]);
|
|
101
|
+
const order = layout.entries.filter((entry) => !moving.has(entry));
|
|
102
|
+
const helpers = moves.helpers.toSorted(
|
|
103
|
+
(first, second) => layout.ranks.get(first) - layout.ranks.get(second),
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
for (const helper of helpers) {
|
|
107
|
+
const previous = layout.mainOrder[layout.ranks.get(helper) - 1];
|
|
108
|
+
|
|
109
|
+
order.splice(findInsertAfter(order, previous), 0, ...moves.stacks.get(helper), helper);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const placed = new Set();
|
|
113
|
+
|
|
114
|
+
const place = (entry) => {
|
|
115
|
+
if (placed.has(entry)) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
placed.add(entry);
|
|
120
|
+
|
|
121
|
+
const users = layout.collectBlockUsers(entry);
|
|
122
|
+
|
|
123
|
+
for (const user of users) {
|
|
124
|
+
if (moves.declarations.includes(user)) {
|
|
125
|
+
place(user);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const positions = users.map((user) => order.indexOf(user)).filter((index) => index !== -1);
|
|
130
|
+
|
|
131
|
+
order.splice(Math.min(...positions), 0, entry);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
for (const entry of moves.declarations) {
|
|
135
|
+
place(entry);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return order;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// the position directly after `previous`, past an `export default` that
|
|
142
|
+
// names it
|
|
143
|
+
function findInsertAfter(order, previous) {
|
|
144
|
+
const at = order.indexOf(previous) + 1;
|
|
145
|
+
const next = order[at];
|
|
146
|
+
const isExportOfPrevious =
|
|
147
|
+
next !== undefined &&
|
|
148
|
+
next.statement.type === 'ExportDefaultDeclaration' &&
|
|
149
|
+
next.declaration.type === 'Identifier' &&
|
|
150
|
+
next.declaration.name === previous.name;
|
|
151
|
+
|
|
152
|
+
return isExportOfPrevious ? at + 1 : at;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// a statement that ends without a semicolon runs on into a next statement
|
|
156
|
+
// that opens with a bracket or an operator, so the fix never puts one there
|
|
157
|
+
function isJoiningStatements(text, order) {
|
|
158
|
+
return order.some(
|
|
159
|
+
(entry, index) =>
|
|
160
|
+
index > 0 &&
|
|
161
|
+
order[index - 1].index !== entry.index - 1 &&
|
|
162
|
+
!isTerminated(text, order[index - 1]) &&
|
|
163
|
+
/^[([`+\-/<]/u.test(text[entry.statement.range[0]]),
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const blockDeclarationTypes = new Set([
|
|
168
|
+
'FunctionDeclaration',
|
|
169
|
+
'ClassDeclaration',
|
|
170
|
+
'TSInterfaceDeclaration',
|
|
171
|
+
'TSEnumDeclaration',
|
|
172
|
+
'TSModuleDeclaration',
|
|
173
|
+
]);
|
|
174
|
+
|
|
175
|
+
function isTerminated(text, entry) {
|
|
176
|
+
return (
|
|
177
|
+
text[entry.statement.range[1] - 1] === ';' || blockDeclarationTypes.has(entry.declaration.type)
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// the range from the first entry that moves to the last, and its new text;
|
|
182
|
+
// an entry keeps the blank lines before it while the entry before it stays
|
|
183
|
+
function renderOrder(context, entries, order) {
|
|
184
|
+
const text = context.sourceCode.text;
|
|
185
|
+
const chunks = collectChunks(context, entries);
|
|
186
|
+
const first = entries.findIndex((entry, index) => order[index] !== entry);
|
|
187
|
+
const last = entries.findLastIndex((entry, index) => order[index] !== entry);
|
|
188
|
+
const parts = [];
|
|
189
|
+
|
|
190
|
+
for (let index = first; index <= last; index++) {
|
|
191
|
+
const chunk = chunks[order[index].index];
|
|
192
|
+
|
|
193
|
+
if (index > first) {
|
|
194
|
+
const isSamePrevious = order[index - 1].index === order[index].index - 1;
|
|
195
|
+
const gap = isSamePrevious
|
|
196
|
+
? text.slice(chunks[order[index].index - 1].end, chunk.start)
|
|
197
|
+
: '\n\n';
|
|
198
|
+
|
|
199
|
+
parts.push(gap);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
parts.push(text.slice(chunk.start, chunk.end));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return [[chunks[first].start, chunks[last].end], parts.join('')];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// each statement's text, with the comments on the lines above it and a
|
|
209
|
+
// comment that ends its last line; the first statement leaves a file header
|
|
210
|
+
// that a blank line parts from it, such as a disable directive, in place
|
|
211
|
+
function collectChunks(context, entries) {
|
|
212
|
+
const text = context.sourceCode.text;
|
|
213
|
+
const comments = context.sourceCode.getAllComments();
|
|
214
|
+
|
|
215
|
+
return entries.map((entry, index) => {
|
|
216
|
+
const [statementStart, statementEnd] = entry.statement.range;
|
|
217
|
+
const nextStart = entries[index + 1]?.statement.range[0] ?? text.length;
|
|
218
|
+
const trailing = comments.findLast(
|
|
219
|
+
(comment) =>
|
|
220
|
+
comment.range[0] >= statementEnd &&
|
|
221
|
+
comment.range[1] <= nextStart &&
|
|
222
|
+
!text.slice(statementEnd, comment.range[0]).includes('\n'),
|
|
223
|
+
);
|
|
224
|
+
const end = trailing?.range[1] ?? statementEnd;
|
|
225
|
+
|
|
226
|
+
if (index === 0) {
|
|
227
|
+
return { start: findAdjacentCommentsStart(text, comments, statementStart), end };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const [, previousEnd] = entries[index - 1].statement.range;
|
|
231
|
+
const leading = comments.find(
|
|
232
|
+
(comment) =>
|
|
233
|
+
comment.range[0] >= previousEnd &&
|
|
234
|
+
comment.range[1] <= statementStart &&
|
|
235
|
+
text.slice(previousEnd, comment.range[0]).includes('\n'),
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
return { start: leading?.range[0] ?? statementStart, end };
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// the start of the run of comments directly above `start`, with no blank
|
|
243
|
+
// line inside it
|
|
244
|
+
function findAdjacentCommentsStart(text, comments, start) {
|
|
245
|
+
let current = start;
|
|
246
|
+
|
|
247
|
+
for (const comment of comments.toReversed()) {
|
|
248
|
+
const gap = text.slice(comment.range[1], current);
|
|
249
|
+
|
|
250
|
+
if (comment.range[1] > current) {
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (gap.trim() !== '' || gap.split('\n').length > 2) {
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
current = comment.range[0];
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return current;
|
|
262
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// one entry per top-level statement, linked to the entries it references;
|
|
2
|
+
// `kind` is 'import', 'function' for a function declaration, 'supporting'
|
|
3
|
+
// for a const, type alias, interface or enum, and 'other' for the rest
|
|
4
|
+
export default function collectModuleEntries(context, program) {
|
|
5
|
+
const overloaded = new Set();
|
|
6
|
+
const entries = program.body.map((statement, index) => {
|
|
7
|
+
const exportKind = pickExportKind(statement);
|
|
8
|
+
const declaration = exportKind === null ? statement : statement.declaration;
|
|
9
|
+
|
|
10
|
+
if (declaration.type === 'TSDeclareFunction' && declaration.id !== null) {
|
|
11
|
+
overloaded.add(declaration.id.name);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return { index, statement, declaration, exportKind, ...pickKind(statement, declaration) };
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const listed = new Set(
|
|
18
|
+
program.body
|
|
19
|
+
.filter((statement) => statement.type === 'ExportNamedDeclaration' && isExportOnly(statement))
|
|
20
|
+
.flatMap((statement) => statement.specifiers.map((specifier) => specifier.local.name)),
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
for (const entry of entries) {
|
|
24
|
+
entry.isOverloaded = entry.kind === 'function' && overloaded.has(entry.name);
|
|
25
|
+
|
|
26
|
+
if (entry.exportKind === null && listed.has(entry.name)) {
|
|
27
|
+
entry.exportKind = 'named';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
linkReferences(context, program, entries);
|
|
32
|
+
markLiveEntries(entries);
|
|
33
|
+
|
|
34
|
+
return entries;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function pickExportKind(statement) {
|
|
38
|
+
if (statement.type === 'ExportDefaultDeclaration') {
|
|
39
|
+
return 'default';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return statement.type === 'ExportNamedDeclaration' && statement.declaration !== null
|
|
43
|
+
? 'named'
|
|
44
|
+
: null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function pickKind(statement, declaration) {
|
|
48
|
+
const links = { uses: [], users: new Set(), references: [] };
|
|
49
|
+
|
|
50
|
+
if (statement.type === 'ImportDeclaration') {
|
|
51
|
+
return { kind: 'import', name: null, nameNode: statement, ...links };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (declaration.type === 'FunctionDeclaration' && declaration.id === null) {
|
|
55
|
+
return { kind: 'function', name: 'default', nameNode: statement, ...links };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// a default export of an expression, such as an object, binds no name
|
|
59
|
+
if (statement.type === 'ExportDefaultDeclaration' && declaration.type !== 'FunctionDeclaration') {
|
|
60
|
+
return { kind: 'other', name: 'default', nameNode: statement, ...links };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const name = findDeclarationName(declaration);
|
|
64
|
+
|
|
65
|
+
if (name === null) {
|
|
66
|
+
return { kind: 'other', name: null, nameNode: statement, ...links };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const kind = declaration.type === 'FunctionDeclaration' ? 'function' : 'supporting';
|
|
70
|
+
|
|
71
|
+
return { kind, name: name.name, nameNode: name, ...links };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// the name a declaration binds: a function, a const with one plain binding,
|
|
75
|
+
// a type alias, an interface or an enum
|
|
76
|
+
function findDeclarationName(declaration) {
|
|
77
|
+
if (
|
|
78
|
+
declaration.type === 'FunctionDeclaration' ||
|
|
79
|
+
declaration.type === 'TSTypeAliasDeclaration' ||
|
|
80
|
+
declaration.type === 'TSInterfaceDeclaration' ||
|
|
81
|
+
declaration.type === 'TSEnumDeclaration'
|
|
82
|
+
) {
|
|
83
|
+
return declaration.id;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (declaration.type !== 'VariableDeclaration' || declaration.kind !== 'const') {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const [declarator, ...rest] = declaration.declarations;
|
|
91
|
+
const isPlain =
|
|
92
|
+
rest.length === 0 && declarator.id.type === 'Identifier' && declarator.init !== null;
|
|
93
|
+
|
|
94
|
+
return isPlain ? declarator.id : null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function isExportOnly(statement) {
|
|
98
|
+
return (
|
|
99
|
+
(statement.type === 'ExportDefaultDeclaration' &&
|
|
100
|
+
statement.declaration.type === 'Identifier') ||
|
|
101
|
+
(statement.type === 'ExportNamedDeclaration' &&
|
|
102
|
+
statement.declaration === null &&
|
|
103
|
+
statement.source === null)
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// fills each entry's `uses` and `users` from the file's top-level scope, and
|
|
108
|
+
// its `references`: each identifier that names it, the entry that holds it,
|
|
109
|
+
// and whether it runs as the module loads
|
|
110
|
+
function linkReferences(context, program, entries) {
|
|
111
|
+
const programScope = context.sourceCode.getScope(program);
|
|
112
|
+
const scope = programScope.childScopes.find((child) => child.type === 'module') ?? programScope;
|
|
113
|
+
const findEntry = (position) =>
|
|
114
|
+
entries.find(
|
|
115
|
+
(entry) => entry.statement.range[0] <= position && position < entry.statement.range[1],
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
for (const variable of scope.variables) {
|
|
119
|
+
const [definition] = variable.defs;
|
|
120
|
+
const target = definition === undefined ? undefined : findEntry(definition.name.range[0]);
|
|
121
|
+
|
|
122
|
+
if (target === undefined || target.kind === 'import') {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
for (const reference of variable.references) {
|
|
127
|
+
const identifier = reference.identifier;
|
|
128
|
+
const source = findEntry(identifier.range[0]) ?? null;
|
|
129
|
+
|
|
130
|
+
target.references.push({ identifier, source, isLoadTime: isLoadTime(identifier) });
|
|
131
|
+
|
|
132
|
+
if (source !== null && source !== target) {
|
|
133
|
+
source.uses.push({ target, position: identifier.range[0] });
|
|
134
|
+
target.users.add(source);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
for (const entry of entries) {
|
|
140
|
+
entry.uses.sort((first, second) => first.position - second.position);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// a reference runs as the module loads unless a function body wraps it; a
|
|
145
|
+
// function with a call above it, such as a `.map` callback or the function in
|
|
146
|
+
// `(() => value).call()`, may run at once, so it wraps nothing
|
|
147
|
+
function isLoadTime(identifier) {
|
|
148
|
+
const ancestors = [];
|
|
149
|
+
|
|
150
|
+
for (let node = identifier.parent; node.type !== 'Program'; node = node.parent) {
|
|
151
|
+
ancestors.push(node);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return !ancestors.some(
|
|
155
|
+
(node, index) =>
|
|
156
|
+
isFunctionNode(node) &&
|
|
157
|
+
!ancestors.slice(index + 1).some((ancestor) => isInvocation(ancestor)),
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function isFunctionNode(node) {
|
|
162
|
+
return (
|
|
163
|
+
node.type === 'FunctionDeclaration' ||
|
|
164
|
+
node.type === 'FunctionExpression' ||
|
|
165
|
+
node.type === 'ArrowFunctionExpression'
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function isInvocation(node) {
|
|
170
|
+
return (
|
|
171
|
+
node.type === 'CallExpression' ||
|
|
172
|
+
node.type === 'NewExpression' ||
|
|
173
|
+
node.type === 'TaggedTemplateExpression'
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// an entry is live when code that runs as the module loads may call into it;
|
|
178
|
+
// a load-time reference outside an export list makes its target live, and a
|
|
179
|
+
// live entry makes everything it references live
|
|
180
|
+
function markLiveEntries(entries) {
|
|
181
|
+
const live = new Set();
|
|
182
|
+
const pending = [];
|
|
183
|
+
|
|
184
|
+
for (const target of entries) {
|
|
185
|
+
const isSeed = target.references.some(
|
|
186
|
+
(reference) =>
|
|
187
|
+
reference.isLoadTime &&
|
|
188
|
+
reference.source !== null &&
|
|
189
|
+
reference.source !== target &&
|
|
190
|
+
!isExportOnly(reference.source.statement),
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
if (isSeed) {
|
|
194
|
+
live.add(target);
|
|
195
|
+
pending.push(target);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
while (pending.length > 0) {
|
|
200
|
+
const entry = pending.pop();
|
|
201
|
+
|
|
202
|
+
for (const use of entry.uses) {
|
|
203
|
+
if (!live.has(use.target)) {
|
|
204
|
+
live.add(use.target);
|
|
205
|
+
pending.push(use.target);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
for (const entry of entries) {
|
|
211
|
+
entry.isLive = live.has(entry);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// a declaration's own name, the variable or key a function is assigned to,
|
|
2
|
+
// or else a function expression's own name; getters, setters, computed and
|
|
3
|
+
// quoted keys name nothing
|
|
4
|
+
export default function findFunctionName(node) {
|
|
5
|
+
if (node.type === 'FunctionDeclaration') {
|
|
6
|
+
return node.id;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const parent = node.parent;
|
|
10
|
+
|
|
11
|
+
if (parent.type === 'VariableDeclarator') {
|
|
12
|
+
return parent.init === node && parent.id.type === 'Identifier' ? parent.id : null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (parent.type === 'MethodDefinition') {
|
|
16
|
+
return parent.kind === 'method' ? findKeyName(parent) : null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (parent.type === 'Property' && parent.parent.type === 'ObjectExpression') {
|
|
20
|
+
return parent.kind === 'init' && parent.value === node ? findKeyName(parent) : null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return node.id ?? null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function findKeyName(member) {
|
|
27
|
+
return !member.computed && member.key.type === 'Identifier' ? member.key : null;
|
|
28
|
+
}
|