@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/function-verb.js
CHANGED
|
@@ -1,103 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
// the drift-guard test
|
|
4
|
-
const taxonomyVerbs = [
|
|
5
|
-
'is',
|
|
6
|
-
'has',
|
|
7
|
-
'can',
|
|
8
|
-
'should',
|
|
9
|
-
'needs',
|
|
10
|
-
'build',
|
|
11
|
-
'define',
|
|
12
|
-
'parse',
|
|
13
|
-
'encode',
|
|
14
|
-
'decode',
|
|
15
|
-
'derive',
|
|
16
|
-
'plan',
|
|
17
|
-
'pick',
|
|
18
|
-
'find',
|
|
19
|
-
'get',
|
|
20
|
-
'collect',
|
|
21
|
-
'count',
|
|
22
|
-
'split',
|
|
23
|
-
'merge',
|
|
24
|
-
'sort',
|
|
25
|
-
'format',
|
|
26
|
-
'render',
|
|
27
|
-
'normalize',
|
|
28
|
-
'sanitize',
|
|
29
|
-
'resolve',
|
|
30
|
-
'expand',
|
|
31
|
-
'compress',
|
|
32
|
-
'decompress',
|
|
33
|
-
'to',
|
|
34
|
-
'transform',
|
|
35
|
-
'apply',
|
|
36
|
-
'create',
|
|
37
|
-
'mint',
|
|
38
|
-
'open',
|
|
39
|
-
'dispose',
|
|
40
|
-
'claim',
|
|
41
|
-
'release',
|
|
42
|
-
'revoke',
|
|
43
|
-
'read',
|
|
44
|
-
'load',
|
|
45
|
-
'write',
|
|
46
|
-
'remove',
|
|
47
|
-
'update',
|
|
48
|
-
'refresh',
|
|
49
|
-
'upsert',
|
|
50
|
-
'set',
|
|
51
|
-
'toggle',
|
|
52
|
-
'reset',
|
|
53
|
-
'restore',
|
|
54
|
-
'print',
|
|
55
|
-
'record',
|
|
56
|
-
'run',
|
|
57
|
-
'spawn',
|
|
58
|
-
'schedule',
|
|
59
|
-
'check',
|
|
60
|
-
'try',
|
|
61
|
-
'register',
|
|
62
|
-
'subscribe',
|
|
63
|
-
'unsubscribe',
|
|
64
|
-
'assert',
|
|
65
|
-
'require',
|
|
66
|
-
'verify',
|
|
67
|
-
'emit',
|
|
68
|
-
'send',
|
|
69
|
-
'wait',
|
|
70
|
-
'setup',
|
|
71
|
-
'teardown',
|
|
72
|
-
'start',
|
|
73
|
-
'stop',
|
|
74
|
-
'restart',
|
|
75
|
-
'advance',
|
|
76
|
-
'drain',
|
|
77
|
-
'with',
|
|
78
|
-
'make',
|
|
79
|
-
'use',
|
|
80
|
-
'on',
|
|
81
|
-
'handle',
|
|
82
|
-
];
|
|
1
|
+
import findFunctionName from './find-function-name.js';
|
|
2
|
+
import isVerbMatch from './is-verb-match.js';
|
|
83
3
|
|
|
84
|
-
// the
|
|
85
|
-
//
|
|
86
|
-
const suffixRequiredVerbs = new Set([
|
|
87
|
-
'build',
|
|
88
|
-
'define',
|
|
89
|
-
'to',
|
|
90
|
-
'toggle',
|
|
91
|
-
'try',
|
|
92
|
-
'with',
|
|
93
|
-
'make',
|
|
94
|
-
'use',
|
|
95
|
-
'on',
|
|
96
|
-
'handle',
|
|
97
|
-
]);
|
|
98
|
-
|
|
99
|
-
// banned verb → the taxonomy verb to use instead; null means the verb is too
|
|
100
|
-
// vague to map and the name should say what the function does
|
|
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
|
|
101
6
|
const bannedVerbs = new Map([
|
|
102
7
|
['process', null],
|
|
103
8
|
['manage', null],
|
|
@@ -113,42 +18,33 @@ const bannedVerbs = new Map([
|
|
|
113
18
|
['lookup', 'find'],
|
|
114
19
|
]);
|
|
115
20
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// a digit also ends the verb: acronyms like 2FA start camelCase segments
|
|
122
|
-
return name.startsWith(verb) && /[A-Z0-9]/u.test(name.charAt(verb.length));
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function findAllowedVerb(name, verbs) {
|
|
126
|
-
return verbs.find((verb) => isVerbMatch(name, verb, !suffixRequiredVerbs.has(verb))) ?? null;
|
|
127
|
-
}
|
|
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']);
|
|
128
24
|
|
|
129
25
|
function findBannedVerb(name) {
|
|
130
26
|
for (const [verb, replacement] of bannedVerbs) {
|
|
131
|
-
if (isVerbMatch(name, verb
|
|
27
|
+
if (isVerbMatch(name, verb)) {
|
|
132
28
|
return { verb, replacement };
|
|
133
29
|
}
|
|
134
30
|
}
|
|
135
31
|
|
|
136
|
-
return null;
|
|
32
|
+
return name === 'handle' ? { verb: 'handle', replacement: null } : null;
|
|
137
33
|
}
|
|
138
34
|
|
|
139
|
-
function planReport(name,
|
|
35
|
+
function planReport(name, exemptNames) {
|
|
140
36
|
if (exemptNames.has(name) || !/^[a-z]/u.test(name)) {
|
|
141
37
|
return null;
|
|
142
38
|
}
|
|
143
39
|
|
|
144
|
-
if (
|
|
145
|
-
return
|
|
40
|
+
if (suffixRequiredVerbs.has(name)) {
|
|
41
|
+
return { messageId: 'bareTemplate', data: { name } };
|
|
146
42
|
}
|
|
147
43
|
|
|
148
44
|
const banned = findBannedVerb(name);
|
|
149
45
|
|
|
150
46
|
if (banned === null) {
|
|
151
|
-
return
|
|
47
|
+
return null;
|
|
152
48
|
}
|
|
153
49
|
|
|
154
50
|
if (banned.replacement === null) {
|
|
@@ -165,16 +61,14 @@ const functionVerb = {
|
|
|
165
61
|
meta: {
|
|
166
62
|
type: 'suggestion',
|
|
167
63
|
messages: {
|
|
168
|
-
unknownVerb:
|
|
169
|
-
"'{{name}}' does not start with a taxonomy verb — pick one, or extend the taxonomy and the `verbs` option in the same PR.",
|
|
170
64
|
bannedVerb: "'{{name}}' starts with the banned verb '{{verb}}' — use `{{replacement}}`.",
|
|
171
65
|
vagueVerb: "'{{name}}' starts with the banned verb '{{verb}}' — name what the function does.",
|
|
66
|
+
bareTemplate: "'{{name}}' is a bare prefix — add what it applies to, as in `{{name}}Thing`.",
|
|
172
67
|
},
|
|
173
68
|
schema: [
|
|
174
69
|
{
|
|
175
70
|
type: 'object',
|
|
176
71
|
properties: {
|
|
177
|
-
verbs: { type: 'array', items: { type: 'string' }, uniqueItems: true },
|
|
178
72
|
exemptNames: { type: 'array', items: { type: 'string' }, uniqueItems: true },
|
|
179
73
|
},
|
|
180
74
|
additionalProperties: false,
|
|
@@ -182,13 +76,11 @@ const functionVerb = {
|
|
|
182
76
|
],
|
|
183
77
|
},
|
|
184
78
|
create(context) {
|
|
185
|
-
const
|
|
186
|
-
const verbs =
|
|
187
|
-
options.verbs === undefined ? taxonomyVerbs : [...taxonomyVerbs, ...options.verbs];
|
|
188
|
-
const exemptNames = new Set(options.exemptNames);
|
|
79
|
+
const exemptNames = new Set(context.options[0]?.exemptNames);
|
|
189
80
|
|
|
190
|
-
const
|
|
191
|
-
const
|
|
81
|
+
const checkFunction = (node) => {
|
|
82
|
+
const id = findFunctionName(node);
|
|
83
|
+
const report = id === null ? null : planReport(id.name, exemptNames);
|
|
192
84
|
|
|
193
85
|
if (report !== null) {
|
|
194
86
|
context.report({ node: id, messageId: report.messageId, data: report.data });
|
|
@@ -196,26 +88,9 @@ const functionVerb = {
|
|
|
196
88
|
};
|
|
197
89
|
|
|
198
90
|
return {
|
|
199
|
-
FunctionDeclaration
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
},
|
|
204
|
-
VariableDeclarator(node) {
|
|
205
|
-
const isFunctionInit =
|
|
206
|
-
node.init !== null &&
|
|
207
|
-
node.init !== undefined &&
|
|
208
|
-
(node.init.type === 'ArrowFunctionExpression' || node.init.type === 'FunctionExpression');
|
|
209
|
-
|
|
210
|
-
if (isFunctionInit && node.id.type === 'Identifier') {
|
|
211
|
-
checkName(node.id);
|
|
212
|
-
}
|
|
213
|
-
},
|
|
214
|
-
MethodDefinition(node) {
|
|
215
|
-
if (node.kind === 'method' && !node.computed && node.key.type === 'Identifier') {
|
|
216
|
-
checkName(node.key);
|
|
217
|
-
}
|
|
218
|
-
},
|
|
91
|
+
FunctionDeclaration: checkFunction,
|
|
92
|
+
FunctionExpression: checkFunction,
|
|
93
|
+
ArrowFunctionExpression: checkFunction,
|
|
219
94
|
};
|
|
220
95
|
},
|
|
221
96
|
};
|
package/import-zones.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { isBuiltin } from 'node:module';
|
|
2
2
|
import { relative, sep } from 'node:path';
|
|
3
|
-
import findRepoRoot from './find-repo-root.js';
|
|
4
3
|
import importZonesSchema from './import-zones-schema.js';
|
|
5
4
|
import loadWorkspaceManifests from './load-workspace-manifests.js';
|
|
6
5
|
import loadZoneOptions from './load-zone-options.js';
|
|
7
6
|
import resolveImportTarget from './resolve-import-target.js';
|
|
7
|
+
import resolveRepoRoot from './resolve-repo-root.js';
|
|
8
8
|
|
|
9
9
|
// Judges each import against named zones: path globs relative to the repo
|
|
10
10
|
// root, which may overlap. A file belongs to every zone its path matches.
|
|
11
11
|
|
|
12
|
-
function
|
|
12
|
+
function loadFileState(context) {
|
|
13
13
|
const options = loadZoneOptions(context.options[0] ?? {});
|
|
14
14
|
const filename = context.physicalFilename;
|
|
15
|
-
const root =
|
|
15
|
+
const root = resolveRepoRoot(filename) ?? context.cwd;
|
|
16
16
|
const path = toRootPath(root, filename);
|
|
17
17
|
|
|
18
18
|
const workspaces = loadWorkspaceManifests(root, options.workspaces);
|
|
@@ -298,7 +298,7 @@ const importZones = {
|
|
|
298
298
|
schema: [importZonesSchema],
|
|
299
299
|
},
|
|
300
300
|
create(context) {
|
|
301
|
-
const file =
|
|
301
|
+
const file = loadFileState(context);
|
|
302
302
|
|
|
303
303
|
return {
|
|
304
304
|
Program(node) {
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
const valueGlobals = new Set(['undefined', 'NaN', 'Infinity']);
|
|
2
|
+
|
|
3
|
+
const valueConstructors = new Set(['Set', 'Map', 'WeakSet', 'WeakMap', 'RegExp']);
|
|
4
|
+
|
|
5
|
+
// a function value is made, not run, so its body never counts
|
|
6
|
+
const checks = {
|
|
7
|
+
Literal: () => true,
|
|
8
|
+
FunctionExpression: () => true,
|
|
9
|
+
ArrowFunctionExpression: () => true,
|
|
10
|
+
Identifier: (value, names, canCopyImport) =>
|
|
11
|
+
(valueGlobals.has(value.name) && !names.locals.has(value.name)) ||
|
|
12
|
+
(canCopyImport && names.imports.has(value.name)),
|
|
13
|
+
TemplateLiteral: (value, names) =>
|
|
14
|
+
value.expressions.every((expression) => isPrimitiveValue(expression, names)),
|
|
15
|
+
ArrayExpression: (value, names) =>
|
|
16
|
+
value.elements.every((element) => element === null || isStaticElement(element, names)),
|
|
17
|
+
ObjectExpression: (value, names) =>
|
|
18
|
+
value.properties.every((property) => isStaticProperty(property, names)),
|
|
19
|
+
UnaryExpression: (value, names) =>
|
|
20
|
+
value.operator !== 'delete' && isPrimitiveValue(value.argument, names),
|
|
21
|
+
BinaryExpression: (value, names) =>
|
|
22
|
+
isPrimitiveValue(value.left, names) && isPrimitiveValue(value.right, names),
|
|
23
|
+
LogicalExpression: (value, names, canCopyImport) =>
|
|
24
|
+
isStaticValue(value.left, names, canCopyImport) &&
|
|
25
|
+
isStaticValue(value.right, names, canCopyImport),
|
|
26
|
+
ConditionalExpression: (value, names, canCopyImport) =>
|
|
27
|
+
isStaticValue(value.test, names, false) &&
|
|
28
|
+
isStaticValue(value.consequent, names, canCopyImport) &&
|
|
29
|
+
isStaticValue(value.alternate, names, canCopyImport),
|
|
30
|
+
NewExpression: (value, names) =>
|
|
31
|
+
value.callee.type === 'Identifier' &&
|
|
32
|
+
valueConstructors.has(value.callee.name) &&
|
|
33
|
+
!names.locals.has(value.callee.name) &&
|
|
34
|
+
value.arguments.every((argument) => isStaticArgument(argument, names)),
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// a value that runs no code and reads nothing that load-time code can change,
|
|
38
|
+
// so it is the same wherever it sits; an import stands only where its value
|
|
39
|
+
// is copied, such as an array element, and never where it is read through
|
|
40
|
+
export default function isStaticValue(node, names, canCopyImport = true) {
|
|
41
|
+
const value = unwrapExpression(node);
|
|
42
|
+
const check = checks[value.type];
|
|
43
|
+
|
|
44
|
+
return check === undefined ? false : check(value, names, canCopyImport);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const wrapperTypes = new Set([
|
|
48
|
+
'TSAsExpression',
|
|
49
|
+
'TSSatisfiesExpression',
|
|
50
|
+
'TSNonNullExpression',
|
|
51
|
+
'TSTypeAssertion',
|
|
52
|
+
'ParenthesizedExpression',
|
|
53
|
+
]);
|
|
54
|
+
|
|
55
|
+
// a type assertion or parentheses leave the value as it is
|
|
56
|
+
function unwrapExpression(node) {
|
|
57
|
+
let current = node;
|
|
58
|
+
|
|
59
|
+
while (wrapperTypes.has(current.type)) {
|
|
60
|
+
current = current.expression;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return current;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const primitiveTypes = new Set([
|
|
67
|
+
'Literal',
|
|
68
|
+
'Identifier',
|
|
69
|
+
'TemplateLiteral',
|
|
70
|
+
'UnaryExpression',
|
|
71
|
+
'BinaryExpression',
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
// an operator or a template turns an object into a primitive through its
|
|
75
|
+
// `valueOf` or `toString`, which can read anything, so it takes only values
|
|
76
|
+
// that are primitive already
|
|
77
|
+
function isPrimitiveValue(node, names) {
|
|
78
|
+
const value = unwrapExpression(node);
|
|
79
|
+
|
|
80
|
+
return primitiveTypes.has(value.type) && isStaticValue(value, names, false);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function isStaticElement(element, names) {
|
|
84
|
+
return element.type === 'SpreadElement'
|
|
85
|
+
? isLiteralSpread(element, names)
|
|
86
|
+
: isStaticValue(element, names);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// a spread runs an iterator or reads properties, so it spreads only a literal
|
|
90
|
+
function isLiteralSpread(element, names) {
|
|
91
|
+
const argument = element.type === 'SpreadElement' ? unwrapExpression(element.argument) : null;
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
argument !== null &&
|
|
95
|
+
(argument.type === 'ArrayExpression' || argument.type === 'ObjectExpression') &&
|
|
96
|
+
isStaticValue(argument, names)
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function isStaticProperty(property, names) {
|
|
101
|
+
if (property.type === 'SpreadElement') {
|
|
102
|
+
return isLiteralSpread(property, names);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return (
|
|
106
|
+
(!property.computed || isStaticValue(property.key, names, false)) &&
|
|
107
|
+
isStaticValue(property.value, names)
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// a constructor reads through its arguments, so an import never stands there
|
|
112
|
+
function isStaticArgument(argument, names) {
|
|
113
|
+
return argument.type === 'SpreadElement'
|
|
114
|
+
? isLiteralSpread(argument, names)
|
|
115
|
+
: isStaticValue(argument, names, false);
|
|
116
|
+
}
|
package/is-verb-match.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// a name starts with a verb when the verb ends at a camelCase boundary; a
|
|
2
|
+
// digit also ends it, since acronyms like 2FA start camelCase segments
|
|
3
|
+
export default function isVerbMatch(name, verb) {
|
|
4
|
+
return name === verb || (name.startsWith(verb) && /[A-Z0-9]/u.test(name.charAt(verb.length)));
|
|
5
|
+
}
|
package/module-order.js
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { basename } from 'node:path';
|
|
2
|
+
import buildModuleOrderFix from './build-module-order-fix.js';
|
|
3
|
+
import collectModuleEntries from './collect-module-entries.js';
|
|
4
|
+
|
|
5
|
+
// the code-style module order: the primary export, then its private helpers
|
|
6
|
+
// depth-first, with each const, type or enum directly above its first use
|
|
7
|
+
const moduleOrder = {
|
|
8
|
+
meta: {
|
|
9
|
+
type: 'suggestion',
|
|
10
|
+
fixable: 'code',
|
|
11
|
+
messages: {
|
|
12
|
+
helperFirst:
|
|
13
|
+
"'{{name}}' sits above the primary export '{{primary}}' — move private helpers below it.",
|
|
14
|
+
helperOrder:
|
|
15
|
+
"'{{name}}' breaks composition order — helpers follow the primary export depth-first, so it belongs directly after '{{after}}'.",
|
|
16
|
+
declarationBelowUse:
|
|
17
|
+
"'{{name}}' sits below its first use in '{{user}}' — move it directly above '{{user}}'.",
|
|
18
|
+
declarationAway:
|
|
19
|
+
"'{{name}}' sits away from its first use in '{{user}}' — move it directly above '{{user}}'.",
|
|
20
|
+
declarationLeads:
|
|
21
|
+
"'{{name}}' leads the file, but the primary export does not use it — move it directly above its first use in '{{user}}'.",
|
|
22
|
+
},
|
|
23
|
+
schema: [],
|
|
24
|
+
},
|
|
25
|
+
create(context) {
|
|
26
|
+
return {
|
|
27
|
+
'Program:exit'(program) {
|
|
28
|
+
checkModule(context, program);
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export default moduleOrder;
|
|
35
|
+
|
|
36
|
+
function checkModule(context, program) {
|
|
37
|
+
const entries = collectModuleEntries(context, program);
|
|
38
|
+
const primary = findPrimaryExport(entries, context.physicalFilename);
|
|
39
|
+
|
|
40
|
+
if (primary === null) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const mainOrder = collectMainOrder(primary);
|
|
45
|
+
const ranks = new Map(mainOrder.map((entry, index) => [entry, index]));
|
|
46
|
+
const homes = buildHomes(entries, ranks);
|
|
47
|
+
const layout = {
|
|
48
|
+
entries,
|
|
49
|
+
primary,
|
|
50
|
+
mainOrder,
|
|
51
|
+
ranks,
|
|
52
|
+
homes,
|
|
53
|
+
collectBlockUsers: (entry) => collectBlockUsers(entry, homes),
|
|
54
|
+
};
|
|
55
|
+
const helperReports = collectHelperReports(layout);
|
|
56
|
+
const reports = [...helperReports, ...collectDeclarationReports(layout, helperReports)];
|
|
57
|
+
|
|
58
|
+
if (reports.length === 0) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const plan = buildModuleOrderFix(context, { ...layout, reports });
|
|
63
|
+
|
|
64
|
+
for (const report of reports) {
|
|
65
|
+
const fix = plan.moved.has(report.entry) ? { fix: plan.fix } : {};
|
|
66
|
+
|
|
67
|
+
context.report({
|
|
68
|
+
node: report.entry.nameNode,
|
|
69
|
+
messageId: report.messageId,
|
|
70
|
+
data: { name: report.entry.name, ...report.data },
|
|
71
|
+
...fix,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// the default export, else the export the file is named for, else the only
|
|
77
|
+
// value the file exports; `export default name` names the declaration of
|
|
78
|
+
// `name`, and a default export of any other expression is the statement
|
|
79
|
+
function findPrimaryExport(entries, filename) {
|
|
80
|
+
const defaultEntry = entries.find((entry) => entry.exportKind === 'default') ?? null;
|
|
81
|
+
const defaultName =
|
|
82
|
+
defaultEntry?.declaration.type === 'Identifier' ? defaultEntry.declaration.name : null;
|
|
83
|
+
const values = entries.filter(
|
|
84
|
+
(entry) =>
|
|
85
|
+
entry.name !== null &&
|
|
86
|
+
(entry.exportKind !== null || entry.name === defaultName) &&
|
|
87
|
+
(entry.kind === 'function' || entry.declaration.type === 'VariableDeclaration'),
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
if (defaultEntry !== null) {
|
|
91
|
+
return values.find((entry) => entry.name === defaultName) ?? defaultEntry;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const [fileBase] = basename(filename).split('.');
|
|
95
|
+
const named = values.find((entry) => toKebabCase(entry.name) === fileBase);
|
|
96
|
+
const only = values.length === 1 ? values[0] : null;
|
|
97
|
+
|
|
98
|
+
return named ?? only;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// `parseCLIArgs` becomes `parse-cli-args`, as the code-style file names read
|
|
102
|
+
function toKebabCase(name) {
|
|
103
|
+
return name
|
|
104
|
+
.replaceAll(/(?<before>[a-z\d])(?<after>[A-Z])/gu, '$<before>-$<after>')
|
|
105
|
+
.replaceAll(/(?<before>[A-Z]+)(?<after>[A-Z][a-z])/gu, '$<before>-$<after>')
|
|
106
|
+
.toLowerCase();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// the primary export, then each private helper it reaches, depth-first in the
|
|
110
|
+
// order each one is first referenced; a const or type passes its references
|
|
111
|
+
// through, so a helper that only a const names still has a place
|
|
112
|
+
function collectMainOrder(primary) {
|
|
113
|
+
const order = [primary];
|
|
114
|
+
const seen = new Set([primary]);
|
|
115
|
+
|
|
116
|
+
const visit = (entry) => {
|
|
117
|
+
for (const use of entry.uses) {
|
|
118
|
+
if (seen.has(use.target)) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
seen.add(use.target);
|
|
123
|
+
|
|
124
|
+
if (isHelper(use.target)) {
|
|
125
|
+
order.push(use.target);
|
|
126
|
+
visit(use.target);
|
|
127
|
+
} else if (use.target.kind === 'supporting') {
|
|
128
|
+
visit(use.target);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
visit(primary);
|
|
134
|
+
|
|
135
|
+
return order;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function isHelper(entry) {
|
|
139
|
+
return entry.kind === 'function' && entry.exportKind === null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// the main-order entry each const or type belongs above: the earliest one
|
|
143
|
+
// that uses it, directly or through another const or type
|
|
144
|
+
function buildHomes(entries, ranks) {
|
|
145
|
+
const homes = new Map();
|
|
146
|
+
const visiting = new Set();
|
|
147
|
+
|
|
148
|
+
const findHome = (entry) => {
|
|
149
|
+
if (homes.has(entry) || visiting.has(entry)) {
|
|
150
|
+
return homes.get(entry) ?? null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
visiting.add(entry);
|
|
154
|
+
|
|
155
|
+
let home = null;
|
|
156
|
+
|
|
157
|
+
for (const user of entry.users) {
|
|
158
|
+
const candidate = ranks.has(user) ? user : findSupportingHome(user);
|
|
159
|
+
|
|
160
|
+
if (candidate !== null && (home === null || ranks.get(candidate) < ranks.get(home))) {
|
|
161
|
+
home = candidate;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
visiting.delete(entry);
|
|
166
|
+
homes.set(entry, home);
|
|
167
|
+
|
|
168
|
+
return home;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const findSupportingHome = (user) => (user.kind === 'supporting' ? findHome(user) : null);
|
|
172
|
+
|
|
173
|
+
for (const entry of entries) {
|
|
174
|
+
if (entry.kind === 'supporting' && !ranks.has(entry)) {
|
|
175
|
+
findHome(entry);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return homes;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// the users of `entry` in its home's block: the home itself and each const
|
|
183
|
+
// or type with the same home
|
|
184
|
+
function collectBlockUsers(entry, homes) {
|
|
185
|
+
const home = homes.get(entry);
|
|
186
|
+
|
|
187
|
+
return [...entry.users].filter((user) => user === home || homes.get(user) === home);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// a helper above the primary export, and a helper below it that breaks the
|
|
191
|
+
// depth-first order; the longest run already in order stays, so each report
|
|
192
|
+
// names one helper to move
|
|
193
|
+
function collectHelperReports(layout) {
|
|
194
|
+
const primary = layout.primary;
|
|
195
|
+
const reports = [];
|
|
196
|
+
|
|
197
|
+
for (const entry of layout.entries) {
|
|
198
|
+
if (isHelper(entry) && entry.index < primary.index) {
|
|
199
|
+
reports.push({ entry, messageId: 'helperFirst', data: { primary: primary.name } });
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const mainOrder = layout.mainOrder;
|
|
204
|
+
const ranks = layout.ranks;
|
|
205
|
+
const below = mainOrder
|
|
206
|
+
.filter((entry) => entry.index > primary.index)
|
|
207
|
+
.toSorted((first, second) => first.index - second.index);
|
|
208
|
+
const kept = collectLongestRun(below.map((entry) => ranks.get(entry)));
|
|
209
|
+
|
|
210
|
+
for (const [position, entry] of below.entries()) {
|
|
211
|
+
if (!kept.has(position)) {
|
|
212
|
+
const after = mainOrder[ranks.get(entry) - 1].name;
|
|
213
|
+
|
|
214
|
+
reports.push({ entry, messageId: 'helperOrder', data: { after } });
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return reports;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// the positions of one longest increasing run in `values`
|
|
222
|
+
function collectLongestRun(values) {
|
|
223
|
+
const lengths = values.map(() => 1);
|
|
224
|
+
const previous = values.map(() => -1);
|
|
225
|
+
|
|
226
|
+
for (let current = 0; current < values.length; current++) {
|
|
227
|
+
for (let earlier = 0; earlier < current; earlier++) {
|
|
228
|
+
if (values[earlier] < values[current] && lengths[earlier] + 1 > lengths[current]) {
|
|
229
|
+
lengths[current] = lengths[earlier] + 1;
|
|
230
|
+
previous[current] = earlier;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const kept = new Set();
|
|
236
|
+
let position = lengths.indexOf(Math.max(...lengths));
|
|
237
|
+
|
|
238
|
+
while (position !== -1) {
|
|
239
|
+
kept.add(position);
|
|
240
|
+
position = previous[position];
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return kept;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// a const or type sits above every use in its home's block, with no helper
|
|
247
|
+
// that stays between it and its home; a const that a live function reads
|
|
248
|
+
// stays unchecked, since below the primary export it could be read unset
|
|
249
|
+
function collectDeclarationReports(layout, helperReports) {
|
|
250
|
+
const homes = layout.homes;
|
|
251
|
+
const moving = new Set(helperReports.map((report) => report.entry));
|
|
252
|
+
const staying = layout.mainOrder.filter((entry) => !moving.has(entry));
|
|
253
|
+
const reports = [];
|
|
254
|
+
|
|
255
|
+
for (const entry of layout.entries) {
|
|
256
|
+
const home = homes.get(entry) ?? null;
|
|
257
|
+
|
|
258
|
+
if (home === null || (hasValue(entry) && isReadByLiveFunction(entry))) {
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const users = collectBlockUsers(entry, homes);
|
|
263
|
+
const usersAbove = [...users, home].filter((user) => user.index < entry.index);
|
|
264
|
+
const isAbove = usersAbove.length === 0;
|
|
265
|
+
const named = isAbove ? users : usersAbove;
|
|
266
|
+
const firstUser = named.reduce((first, user) => (user.index < first.index ? user : first));
|
|
267
|
+
const hasHelperBetween = staying.some(
|
|
268
|
+
(main) => main.index > entry.index && main.index < home.index,
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
if (!isAbove || hasHelperBetween) {
|
|
272
|
+
const messageId = pickDeclarationMessage({ entry, home, primary: layout.primary, isAbove });
|
|
273
|
+
|
|
274
|
+
reports.push({ entry, messageId, data: { user: firstUser.name } });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return reports;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// a type has no value to read unset, so it is always checked
|
|
282
|
+
function hasValue(entry) {
|
|
283
|
+
const type = entry.declaration.type;
|
|
284
|
+
|
|
285
|
+
return type !== 'TSTypeAliasDeclaration' && type !== 'TSInterfaceDeclaration';
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function isReadByLiveFunction(entry) {
|
|
289
|
+
return entry.references.some(
|
|
290
|
+
(reference) =>
|
|
291
|
+
reference.source !== null &&
|
|
292
|
+
reference.source !== entry &&
|
|
293
|
+
reference.source.isLive &&
|
|
294
|
+
!reference.isLoadTime,
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function pickDeclarationMessage(placement) {
|
|
299
|
+
if (!placement.isAbove) {
|
|
300
|
+
return 'declarationBelowUse';
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const isLeading =
|
|
304
|
+
placement.home !== placement.primary && placement.entry.index < placement.primary.index;
|
|
305
|
+
|
|
306
|
+
return isLeading ? 'declarationLeads' : 'declarationAway';
|
|
307
|
+
}
|