@murky-web/typebuddy 2.1.0 → 3.0.1
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 +184 -72
- package/dist/collections.js +1 -2
- package/dist/empty.js +34 -13
- package/dist/guards.js +33 -9
- package/dist/ids.js +6 -6
- package/dist/oxlint.js +7 -7
- package/dist/parse.js +39 -15
- package/dist/result.js +6 -6
- package/dist/rules/async_rule.js +39 -25
- package/dist/rules/deliberate_throw.js +5 -6
- package/dist/rules/maybe_promise_rule.js +99 -40
- package/dist/rules/maybe_rule.js +7 -5
- package/dist/rules/nullable_rule.js +7 -5
- package/dist/rules/optional_rule.js +8 -4
- package/dist/rules/own_subtree.js +1 -2
- package/dist/rules/test_file.js +63 -0
- package/dist/rules/typebuddy_import.js +242 -35
- package/dist/rules/use_server.js +1 -2
- package/globals.d.ts +49 -44
- package/jsr.json +24 -23
- package/oxlint/index.ts +9 -7
- package/package.json +92 -91
- package/rules/async_rule.ts +91 -75
- package/rules/deliberate_throw.ts +11 -11
- package/rules/maybe_promise_rule.ts +238 -111
- package/rules/maybe_rule.ts +76 -49
- package/rules/nullable_rule.ts +77 -52
- package/rules/optional_rule.ts +79 -53
- package/rules/own_subtree.ts +33 -33
- package/rules/test_file.ts +114 -0
- package/rules/typebuddy_import.ts +479 -87
- package/rules/use_server.ts +5 -4
- package/src/collections.ts +1 -1
- package/src/empty.ts +58 -20
- package/src/guards.ts +100 -34
- package/src/ids.ts +10 -4
- package/src/index.ts +1 -18
- package/src/parse.ts +108 -29
- package/src/public_api.ts +22 -0
- package/src/result.ts +8 -7
- package/src/types/globals.ts +11 -11
- package/src/types/index.ts +21 -21
- package/src/types/json.ts +48 -11
- package/src/types/maybe_promise.ts +17 -12
- package/src/types/nullable.ts +2 -2
- package/src/types/optional.ts +6 -6
|
@@ -1,134 +1,526 @@
|
|
|
1
1
|
type AstNode = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
type: string;
|
|
3
|
+
parent?: AstNode;
|
|
4
|
+
[key: string]: unknown;
|
|
5
5
|
};
|
|
6
6
|
|
|
7
7
|
const PACKAGE_NAME = "@murky-web/typebuddy";
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
/**
|
|
10
|
+
* The entries that export `ok` and `err`. The result helpers live in their own
|
|
11
|
+
* entry so a caller that only needs them does not pull the guards along, and a
|
|
12
|
+
* file that imports them from there already has what the fix is about to add.
|
|
13
|
+
*/
|
|
14
|
+
const RESULT_HELPER_SOURCES = new Set([PACKAGE_NAME, `${PACKAGE_NAME}/result`]);
|
|
15
|
+
const TYPE_SOURCES = new Set([PACKAGE_NAME]);
|
|
12
16
|
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
type ImportKind = "type" | "value";
|
|
18
|
+
|
|
19
|
+
/** What a rule needs from `context.getSourceCode()` to plan an import. */
|
|
20
|
+
type ImportSourceCode = {
|
|
21
|
+
getScope(node: unknown): unknown;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The outcome of looking for a name before a fix uses it:
|
|
26
|
+
*
|
|
27
|
+
* - `present`: the file already imports it from the package, nothing to add.
|
|
28
|
+
* - `global`: no import can be added here (a script file), so a type is written
|
|
29
|
+
* by its global name, as before.
|
|
30
|
+
* - `conflict`: the name is something else at that spot (a catch parameter, a
|
|
31
|
+
* local, another library's import), or it is a value in a place that cannot
|
|
32
|
+
* import. Using it would call or type the wrong thing, so the fix must not be
|
|
33
|
+
* offered.
|
|
34
|
+
* - `insert`: nothing of that name is in scope; add this edit. It is a new import
|
|
35
|
+
* line, or the name appended to an import of the package that is already
|
|
36
|
+
* there.
|
|
37
|
+
*/
|
|
38
|
+
type ImportPlan =
|
|
39
|
+
| { readonly kind: "present" }
|
|
40
|
+
| { readonly kind: "global" }
|
|
41
|
+
| { readonly kind: "conflict" }
|
|
42
|
+
| {
|
|
43
|
+
readonly kind: "insert";
|
|
44
|
+
readonly range: [number, number];
|
|
45
|
+
readonly text: string;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
function isNode(value: unknown): value is AstNode {
|
|
49
|
+
return typeof value === "object" && value !== null && "type" in value;
|
|
15
50
|
}
|
|
16
51
|
|
|
17
52
|
function isImportDeclaration(node: unknown): node is AstNode {
|
|
18
|
-
|
|
53
|
+
return isNode(node) && node.type === "ImportDeclaration";
|
|
19
54
|
}
|
|
20
55
|
|
|
21
56
|
function getStringLiteralValue(node: unknown): string | null {
|
|
22
|
-
|
|
57
|
+
if (!isNode(node)) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (
|
|
62
|
+
(node.type === "Literal" || node.type === "StringLiteral") &&
|
|
63
|
+
typeof node["value"] === "string"
|
|
64
|
+
) {
|
|
65
|
+
return node["value"];
|
|
66
|
+
}
|
|
67
|
+
|
|
23
68
|
return null;
|
|
24
|
-
|
|
69
|
+
}
|
|
25
70
|
|
|
26
|
-
|
|
27
|
-
(node
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return node["value"];
|
|
31
|
-
}
|
|
71
|
+
function getModuleExportName(node: unknown): string | null {
|
|
72
|
+
if (!isNode(node)) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
32
75
|
|
|
33
|
-
|
|
76
|
+
if (node.type === "Identifier" && typeof node["name"] === "string") {
|
|
77
|
+
return node["name"];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return getStringLiteralValue(node);
|
|
34
81
|
}
|
|
35
82
|
|
|
36
83
|
function getProgram(node: unknown): AstNode | null {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
84
|
+
if (!isNode(node)) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
40
87
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
88
|
+
let current: AstNode | undefined = node;
|
|
89
|
+
while (current?.parent) {
|
|
90
|
+
current = current.parent;
|
|
91
|
+
}
|
|
45
92
|
|
|
46
|
-
|
|
93
|
+
return current?.type === "Program" ? current : null;
|
|
47
94
|
}
|
|
48
95
|
|
|
49
96
|
function getProgramBody(program: AstNode): AstNode[] {
|
|
50
|
-
|
|
97
|
+
if (!Array.isArray(program["body"])) {
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return program["body"].filter(isNode);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** `"use client"`, `"use cache"`, `"use strict"`: the directive prologue. */
|
|
105
|
+
function isDirective(statement: AstNode): boolean {
|
|
106
|
+
return (
|
|
107
|
+
statement.type === "ExpressionStatement" &&
|
|
108
|
+
typeof statement["directive"] === "string"
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const MODULE_STATEMENTS = new Set([
|
|
113
|
+
"ExportAllDeclaration",
|
|
114
|
+
"ExportDefaultDeclaration",
|
|
115
|
+
"ExportNamedDeclaration",
|
|
116
|
+
"ImportDeclaration",
|
|
117
|
+
"TSExportAssignment"
|
|
118
|
+
]);
|
|
119
|
+
|
|
120
|
+
function isModuleStatement(statement: AstNode): boolean {
|
|
121
|
+
if (MODULE_STATEMENTS.has(statement.type)) {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// `import fs = require("fs")` makes a file a module, `import a = b.c` is
|
|
126
|
+
// only an alias.
|
|
127
|
+
if (statement.type !== "TSImportEqualsDeclaration") {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
const reference = statement["moduleReference"];
|
|
131
|
+
return isNode(reference) && reference.type === "TSExternalModuleReference";
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Whether the file is a module. TypeScript decides this by its statements, not
|
|
136
|
+
* by the extension: a file without an `import` or `export` is a script, its
|
|
137
|
+
* top-level names are global, and adding an `import` would turn it into a
|
|
138
|
+
* module and take its globals away (`vite-env.d.ts` with `interface
|
|
139
|
+
* ImportMetaEnv`, `interface Window`, `declare var`).
|
|
140
|
+
*/
|
|
141
|
+
function isModule(program: AstNode): boolean {
|
|
142
|
+
return getProgramBody(program).some(isModuleStatement);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Whether the file the node is in can take an import without changing what it
|
|
147
|
+
* is: it already is a module. A script file cannot, and `ok`/`err` have no
|
|
148
|
+
* global name to fall back on, so a rule that needs them there must not offer a
|
|
149
|
+
* fix.
|
|
150
|
+
*
|
|
151
|
+
* @param {unknown} node - Any node of the file.
|
|
152
|
+
* @returns {boolean} True when an import can be added to the file.
|
|
153
|
+
*/
|
|
154
|
+
function canAddImport(node: unknown): boolean {
|
|
155
|
+
const program = getProgram(node);
|
|
156
|
+
return program !== null && isModule(program);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
type ScopeLike = {
|
|
160
|
+
upper: ScopeLike | null;
|
|
161
|
+
set: Map<string, unknown>;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
function isScopeLike(value: unknown): value is ScopeLike {
|
|
165
|
+
return (
|
|
166
|
+
typeof value === "object" &&
|
|
167
|
+
value !== null &&
|
|
168
|
+
"set" in value &&
|
|
169
|
+
value.set instanceof Map
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function getUpperScope(scope: ScopeLike): ScopeLike | null {
|
|
174
|
+
const upper: unknown = scope.upper;
|
|
175
|
+
return isScopeLike(upper) ? upper : null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** The first binding of `name` visible from `node`, innermost scope first. */
|
|
179
|
+
function findBinding(
|
|
180
|
+
sourceCode: ImportSourceCode,
|
|
181
|
+
node: unknown,
|
|
182
|
+
name: string
|
|
183
|
+
): unknown {
|
|
184
|
+
const start: unknown = sourceCode.getScope(node);
|
|
185
|
+
let scope: ScopeLike | null = isScopeLike(start) ? start : null;
|
|
186
|
+
|
|
187
|
+
while (scope) {
|
|
188
|
+
const variable = scope.set.get(name);
|
|
189
|
+
if (variable !== undefined) {
|
|
190
|
+
return variable;
|
|
191
|
+
}
|
|
192
|
+
scope = getUpperScope(scope);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function getDefinitions(variable: unknown): unknown[] {
|
|
199
|
+
if (
|
|
200
|
+
typeof variable === "object" &&
|
|
201
|
+
variable !== null &&
|
|
202
|
+
"defs" in variable &&
|
|
203
|
+
Array.isArray(variable.defs)
|
|
204
|
+
) {
|
|
205
|
+
return variable.defs;
|
|
206
|
+
}
|
|
207
|
+
|
|
51
208
|
return [];
|
|
52
|
-
|
|
209
|
+
}
|
|
53
210
|
|
|
54
|
-
|
|
211
|
+
function sourcesFor(kind: ImportKind): ReadonlySet<string> {
|
|
212
|
+
return kind === "value" ? RESULT_HELPER_SOURCES : TYPE_SOURCES;
|
|
55
213
|
}
|
|
56
214
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
215
|
+
/**
|
|
216
|
+
* Whether one definition is `import { name } from "<package>"`, imported under
|
|
217
|
+
* its own name. A renamed import (`import { ok as okay }`), a default import
|
|
218
|
+
* and an import from another module are different bindings that happen to share
|
|
219
|
+
* the name. For a value, a type-only import does not count either: it has no
|
|
220
|
+
* runtime binding to call.
|
|
221
|
+
*/
|
|
222
|
+
function isPackageImport(
|
|
223
|
+
definition: unknown,
|
|
224
|
+
name: string,
|
|
225
|
+
kind: ImportKind
|
|
226
|
+
): boolean {
|
|
227
|
+
if (
|
|
228
|
+
typeof definition !== "object" ||
|
|
229
|
+
definition === null ||
|
|
230
|
+
!("type" in definition) ||
|
|
231
|
+
definition.type !== "ImportBinding" ||
|
|
232
|
+
!("node" in definition) ||
|
|
233
|
+
!("parent" in definition)
|
|
234
|
+
) {
|
|
235
|
+
return false;
|
|
61
236
|
}
|
|
62
237
|
|
|
63
|
-
|
|
64
|
-
|
|
238
|
+
const specifier = definition.node;
|
|
239
|
+
const declaration = definition.parent;
|
|
240
|
+
if (
|
|
241
|
+
!isNode(specifier) ||
|
|
242
|
+
specifier.type !== "ImportSpecifier" ||
|
|
243
|
+
!isImportDeclaration(declaration)
|
|
244
|
+
) {
|
|
245
|
+
return false;
|
|
65
246
|
}
|
|
66
247
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
248
|
+
const source = getStringLiteralValue(declaration["source"]);
|
|
249
|
+
if (source === null || !sourcesFor(kind).has(source)) {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
if (getModuleExportName(specifier["imported"]) !== name) {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
if (kind === "value") {
|
|
256
|
+
return (
|
|
257
|
+
declaration["importKind"] !== "type" &&
|
|
258
|
+
specifier["importKind"] !== "type"
|
|
259
|
+
);
|
|
71
260
|
}
|
|
72
261
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
: [];
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
76
264
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
isNode
|
|
80
|
-
|
|
81
|
-
isIdentifierNamed(specifier["local"], helperName)
|
|
82
|
-
);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
265
|
+
function getSpecifiers(declaration: AstNode): AstNode[] {
|
|
266
|
+
return Array.isArray(declaration["specifiers"])
|
|
267
|
+
? declaration["specifiers"].filter(isNode)
|
|
268
|
+
: [];
|
|
85
269
|
}
|
|
86
270
|
|
|
271
|
+
type MergeTarget = {
|
|
272
|
+
/** Where the new specifier goes: right after the last named one. */
|
|
273
|
+
readonly at: number;
|
|
274
|
+
/**
|
|
275
|
+
* The specifier needs its own `type` modifier (a type joining a value
|
|
276
|
+
* import).
|
|
277
|
+
*/
|
|
278
|
+
readonly inlineType: boolean;
|
|
279
|
+
};
|
|
280
|
+
|
|
87
281
|
/**
|
|
88
|
-
*
|
|
282
|
+
* The import of the package that the name can join, or `null` when there is
|
|
283
|
+
* none. A value joins `import { … }`. A type joins `import type { … }`, or else
|
|
284
|
+
* an `import { … }` of the package as `type Name` (an inline type specifier),
|
|
285
|
+
* so the file does not end up with two imports from one module. An import with
|
|
286
|
+
* a namespace specifier, or with no named specifier, is left alone.
|
|
287
|
+
*/
|
|
288
|
+
function findMergeTarget(
|
|
289
|
+
imports: readonly AstNode[],
|
|
290
|
+
kind: ImportKind
|
|
291
|
+
): MergeTarget | null {
|
|
292
|
+
const sources = sourcesFor(kind);
|
|
293
|
+
let inlineTarget: MergeTarget | null = null;
|
|
294
|
+
|
|
295
|
+
for (const declaration of imports) {
|
|
296
|
+
const source = getStringLiteralValue(declaration["source"]);
|
|
297
|
+
const isTypeImport = declaration["importKind"] === "type";
|
|
298
|
+
if (source === null || !sources.has(source)) {
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if (kind === "value" && isTypeImport) {
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const specifiers = getSpecifiers(declaration);
|
|
306
|
+
if (
|
|
307
|
+
specifiers.some((specifier) => {
|
|
308
|
+
return specifier.type === "ImportNamespaceSpecifier";
|
|
309
|
+
})
|
|
310
|
+
) {
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const named = specifiers.filter((specifier) => {
|
|
315
|
+
return specifier.type === "ImportSpecifier";
|
|
316
|
+
});
|
|
317
|
+
const range = named.at(-1)?.["range"];
|
|
318
|
+
if (!Array.isArray(range) || typeof range[1] !== "number") {
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (kind === "value" || isTypeImport) {
|
|
323
|
+
return { at: range[1], inlineType: false };
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
inlineTarget ??= { at: range[1], inlineType: true };
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return inlineTarget;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Where and what to insert so `name` is imported from the package, and whether
|
|
334
|
+
* it has to be at all.
|
|
89
335
|
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* zero. A leading comment moved the first statement off zero, the wrong branch
|
|
95
|
-
* won, and the fix emitted a leading newline instead of a trailing one:
|
|
336
|
+
* The answer comes from the scope at the place the fix is about to use the
|
|
337
|
+
* name, not from the list of imports. A catch parameter called `err`, a local
|
|
338
|
+
* called `ok`, or an unrelated library's `Maybe` all make a name mean something
|
|
339
|
+
* else there, and adding an import for it would not change that.
|
|
96
340
|
*
|
|
97
|
-
*
|
|
341
|
+
* Where the import goes: appended to an existing import of the same kind from
|
|
342
|
+
* the package; otherwise a new line after the last import; with no import,
|
|
343
|
+
* after the directive prologue (`"use client"` has to stay the first statement
|
|
344
|
+
* of the file or it stops being a directive); with neither, before the first
|
|
345
|
+
* statement.
|
|
98
346
|
*
|
|
99
|
-
*
|
|
347
|
+
* A script file (no `import` or `export`) gets no import: that would turn it
|
|
348
|
+
* into a module and take its globals away. A type is then written by its global
|
|
349
|
+
* name, as it was before this rule added imports; a value, which has no global
|
|
350
|
+
* name, gets no fix. Inside a module, `declare global` and `declare module`
|
|
351
|
+
* blocks get the import like any other code: the file is a module already, and
|
|
352
|
+
* the global name would break a project that does not load the globals entry.
|
|
100
353
|
*
|
|
101
|
-
* @param {
|
|
102
|
-
* @param {
|
|
103
|
-
* @
|
|
104
|
-
*
|
|
105
|
-
*
|
|
354
|
+
* @param {ImportSourceCode} sourceCode - Source code of the file, for scopes.
|
|
355
|
+
* @param {unknown} node - The node the fix uses `name` at.
|
|
356
|
+
* @param {string} name - The named export to import, e.g. `"err"`.
|
|
357
|
+
* @param {ImportKind} kind - `"value"` for a helper that is called, `"type"`
|
|
358
|
+
* for a type that is only named.
|
|
359
|
+
* @returns {ImportPlan} Whether to insert, with the zero-width insertion to
|
|
360
|
+
* apply.
|
|
106
361
|
*/
|
|
107
|
-
function
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
362
|
+
function planTypeBuddyImport(
|
|
363
|
+
sourceCode: ImportSourceCode,
|
|
364
|
+
node: unknown,
|
|
365
|
+
name: string,
|
|
366
|
+
kind: ImportKind
|
|
367
|
+
): ImportPlan {
|
|
368
|
+
const program = getProgram(node);
|
|
369
|
+
if (!program || !isNode(node)) {
|
|
370
|
+
return { kind: "conflict" };
|
|
371
|
+
}
|
|
115
372
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
373
|
+
const binding = findBinding(sourceCode, node, name);
|
|
374
|
+
if (binding !== undefined) {
|
|
375
|
+
const definitions = getDefinitions(binding);
|
|
376
|
+
const isOurs =
|
|
377
|
+
definitions.length > 0 &&
|
|
378
|
+
definitions.every((definition) => {
|
|
379
|
+
return isPackageImport(definition, name, kind);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
return { kind: isOurs ? "present" : "conflict" };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (!isModule(program)) {
|
|
386
|
+
return { kind: kind === "type" ? "global" : "conflict" };
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const body = getProgramBody(program);
|
|
390
|
+
const imports = body.filter(isImportDeclaration);
|
|
391
|
+
|
|
392
|
+
const merge = findMergeTarget(imports, kind);
|
|
393
|
+
if (merge !== null) {
|
|
394
|
+
return {
|
|
395
|
+
kind: "insert",
|
|
396
|
+
range: [merge.at, merge.at],
|
|
397
|
+
text: `, ${merge.inlineType ? "type " : ""}${name}`
|
|
398
|
+
};
|
|
399
|
+
}
|
|
123
400
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
401
|
+
const statement =
|
|
402
|
+
kind === "type"
|
|
403
|
+
? `import type { ${name} } from "${PACKAGE_NAME}";`
|
|
404
|
+
: `import { ${name} } from "${PACKAGE_NAME}";`;
|
|
129
405
|
|
|
130
|
-
|
|
131
|
-
|
|
406
|
+
const lastImport = imports.at(-1);
|
|
407
|
+
if (lastImport) {
|
|
408
|
+
const range = lastImport["range"];
|
|
409
|
+
if (!Array.isArray(range) || range.length < 2) {
|
|
410
|
+
return { kind: "conflict" };
|
|
411
|
+
}
|
|
412
|
+
const end: number = range[1];
|
|
413
|
+
return { kind: "insert", range: [end, end], text: `\n${statement}` };
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const lastDirective = body.filter(isDirective).at(-1);
|
|
417
|
+
if (lastDirective) {
|
|
418
|
+
const range = lastDirective["range"];
|
|
419
|
+
if (!Array.isArray(range) || range.length < 2) {
|
|
420
|
+
return { kind: "conflict" };
|
|
421
|
+
}
|
|
422
|
+
const end: number = range[1];
|
|
423
|
+
return { kind: "insert", range: [end, end], text: `\n${statement}` };
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const anchor = body[0] ?? program;
|
|
427
|
+
const range = anchor["range"];
|
|
428
|
+
if (!Array.isArray(range) || range.length < 2) {
|
|
429
|
+
return { kind: "conflict" };
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const start: number = range[0];
|
|
433
|
+
return { kind: "insert", range: [start, start], text: `${statement}\n` };
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Where the first non-directive statement of a body starts, for a fix that
|
|
438
|
+
* wraps the body. `"use cache"` and `"use server"` only count as directives at
|
|
439
|
+
* the very top of the body; wrapping them in a `try` turns them into expression
|
|
440
|
+
* statements that do nothing.
|
|
441
|
+
*
|
|
442
|
+
* @param {unknown} body - A block statement.
|
|
443
|
+
* @returns {number | null} The offset right after the directive prologue, or
|
|
444
|
+
* the offset right after the opening brace when there is none; null when the
|
|
445
|
+
* range is unusable.
|
|
446
|
+
*/
|
|
447
|
+
function getBodyStartAfterDirectives(body: unknown): number | null {
|
|
448
|
+
if (!isNode(body) || !Array.isArray(body["range"])) {
|
|
449
|
+
return null;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const range: unknown[] = body["range"];
|
|
453
|
+
const opening = range[0];
|
|
454
|
+
if (typeof opening !== "number") {
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const statements = Array.isArray(body["body"])
|
|
459
|
+
? body["body"].filter(isNode)
|
|
460
|
+
: [];
|
|
461
|
+
const lastDirective = statements.filter(isDirective).at(-1);
|
|
462
|
+
if (lastDirective) {
|
|
463
|
+
const directiveRange: unknown = lastDirective["range"];
|
|
464
|
+
if (
|
|
465
|
+
Array.isArray(directiveRange) &&
|
|
466
|
+
typeof directiveRange[1] === "number"
|
|
467
|
+
) {
|
|
468
|
+
return directiveRange[1];
|
|
469
|
+
}
|
|
470
|
+
return null;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
return opening + 1;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
type ImportFixer = {
|
|
477
|
+
insertTextBeforeRange(range: [number, number], text: string): unknown;
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Turns import plans into the fixes a rule returns, for one file.
|
|
482
|
+
*
|
|
483
|
+
* `fixes` returns `null` when the name is something else at that spot (or a
|
|
484
|
+
* value cannot be imported there), and a rule then offers no fix at all: a fix
|
|
485
|
+
* that calls the caught `err` or a different library's `Maybe` is worse than
|
|
486
|
+
* none.
|
|
487
|
+
*
|
|
488
|
+
* Every fix that needs an import carries it itself. oxlint applies a fix as a
|
|
489
|
+
* whole or not at all, and drops a fix that overlaps one it applied earlier in
|
|
490
|
+
* the same pass; an import attached to only the first fix of a file would be
|
|
491
|
+
* lost with that fix while the later fixes, which no longer overlap, applied
|
|
492
|
+
* without it. The price is that fixes needing the same import share one
|
|
493
|
+
* insertion point, so one `--fix` pass applies one of them; the next pass finds
|
|
494
|
+
* the import present and applies the rest.
|
|
495
|
+
*
|
|
496
|
+
* @param {ImportSourceCode} sourceCode - Source code of the file, for scopes.
|
|
497
|
+
* @returns {{ fixes: Function }} The planner.
|
|
498
|
+
*/
|
|
499
|
+
function createImportPlanner(sourceCode: ImportSourceCode) {
|
|
500
|
+
return {
|
|
501
|
+
fixes(
|
|
502
|
+
node: unknown,
|
|
503
|
+
fixer: ImportFixer,
|
|
504
|
+
name: string,
|
|
505
|
+
kind: ImportKind
|
|
506
|
+
): unknown[] | null {
|
|
507
|
+
const plan = planTypeBuddyImport(sourceCode, node, name, kind);
|
|
508
|
+
if (plan.kind === "conflict") {
|
|
509
|
+
return null;
|
|
510
|
+
}
|
|
511
|
+
if (plan.kind === "insert") {
|
|
512
|
+
return [fixer.insertTextBeforeRange(plan.range, plan.text)];
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
return [];
|
|
516
|
+
}
|
|
517
|
+
};
|
|
132
518
|
}
|
|
133
519
|
|
|
134
|
-
export {
|
|
520
|
+
export {
|
|
521
|
+
canAddImport,
|
|
522
|
+
createImportPlanner,
|
|
523
|
+
getBodyStartAfterDirectives,
|
|
524
|
+
planTypeBuddyImport
|
|
525
|
+
};
|
|
526
|
+
export type { ImportFixer, ImportKind, ImportPlan, ImportSourceCode };
|
package/rules/use_server.ts
CHANGED
|
@@ -9,7 +9,7 @@ const USE_SERVER = "use server";
|
|
|
9
9
|
const FUNCTION_TYPES = new Set([
|
|
10
10
|
"ArrowFunctionExpression",
|
|
11
11
|
"FunctionDeclaration",
|
|
12
|
-
"FunctionExpression"
|
|
12
|
+
"FunctionExpression"
|
|
13
13
|
]);
|
|
14
14
|
|
|
15
15
|
function isUseServerNode(value: unknown): value is UseServerNode {
|
|
@@ -62,7 +62,8 @@ function isUseServerFunction(node: UseServerNode): boolean {
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
return (
|
|
65
|
-
node.body.type === "BlockStatement" &&
|
|
65
|
+
node.body.type === "BlockStatement" &&
|
|
66
|
+
startsWithUseServer(node.body.body)
|
|
66
67
|
);
|
|
67
68
|
}
|
|
68
69
|
|
|
@@ -100,7 +101,7 @@ function createUseServerGuard() {
|
|
|
100
101
|
serverModule = startsWithUseServer(node.body);
|
|
101
102
|
},
|
|
102
103
|
guard<Node extends UseServerNode>(
|
|
103
|
-
visitor: (node: Node) => void
|
|
104
|
+
visitor: (node: Node) => void
|
|
104
105
|
): (node: Node) => void {
|
|
105
106
|
return (node) => {
|
|
106
107
|
if (serverModule || isInsideUseServerFunction(node)) {
|
|
@@ -109,7 +110,7 @@ function createUseServerGuard() {
|
|
|
109
110
|
|
|
110
111
|
visitor(node);
|
|
111
112
|
};
|
|
112
|
-
}
|
|
113
|
+
}
|
|
113
114
|
};
|
|
114
115
|
}
|
|
115
116
|
|
package/src/collections.ts
CHANGED
|
@@ -21,7 +21,7 @@ function getKeys<T extends Record<string, unknown>>(object: T): (keyof T)[] {
|
|
|
21
21
|
*/
|
|
22
22
|
function arrayContainsCommonValue<T>(
|
|
23
23
|
array1: readonly T[],
|
|
24
|
-
array2: readonly T[]
|
|
24
|
+
array2: readonly T[]
|
|
25
25
|
): boolean {
|
|
26
26
|
if (!isArray(array1) || !isArray(array2)) {
|
|
27
27
|
return false;
|