eslint-rule-forge 0.3.2 → 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 +47 -40
- package/dist/bindings.d.mts +4 -5
- package/dist/bindings.d.mts.map +1 -1
- package/dist/bindings.mjs +0 -1
- package/dist/bindings.mjs.map +1 -1
- package/dist/comments.d.mts +10 -0
- package/dist/comments.d.mts.map +1 -0
- package/dist/comments.mjs +11 -0
- package/dist/comments.mjs.map +1 -0
- package/dist/imports.d.mts +68 -8
- package/dist/imports.d.mts.map +1 -1
- package/dist/imports.mjs +202 -6
- package/dist/imports.mjs.map +1 -1
- package/dist/index.d.mts +2 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -1
- package/dist/negation.d.mts +8 -9
- package/dist/negation.d.mts.map +1 -1
- package/dist/negation.mjs +18 -18
- package/dist/negation.mjs.map +1 -1
- package/dist/rules/prefer-array-is-array.d.mts.map +1 -1
- package/dist/rules/prefer-array-is-array.mjs +13 -19
- package/dist/rules/prefer-array-is-array.mjs.map +1 -1
- package/dist/rules/prefer-is-non-null-object.d.mts.map +1 -1
- package/dist/rules/prefer-is-non-null-object.mjs +75 -76
- package/dist/rules/prefer-is-non-null-object.mjs.map +1 -1
- package/dist/rules/prefer-is-record-and-has-key.d.mts.map +1 -1
- package/dist/rules/prefer-is-record-and-has-key.mjs +32 -44
- package/dist/rules/prefer-is-record-and-has-key.mjs.map +1 -1
- package/dist/rules/prefer-safe-parse.d.mts +10 -7
- package/dist/rules/prefer-safe-parse.d.mts.map +1 -1
- package/dist/rules/prefer-safe-parse.mjs +50 -34
- package/dist/rules/prefer-safe-parse.mjs.map +1 -1
- package/dist/statements.d.mts +12 -0
- package/dist/statements.d.mts.map +1 -0
- package/dist/statements.mjs +39 -0
- package/dist/statements.mjs.map +1 -0
- package/dist/type-wrappers.d.mts +3 -3
- package/package.json +5 -5
- package/src/bindings.mts +4 -5
- package/src/comments.mts +23 -0
- package/src/imports.mts +363 -19
- package/src/index.mts +2 -0
- package/src/negation.mts +36 -37
- package/src/rules/prefer-array-is-array.mts +23 -36
- package/src/rules/prefer-is-non-null-object.mts +107 -112
- package/src/rules/prefer-is-record-and-has-key.mts +47 -71
- package/src/rules/prefer-safe-parse.mts +92 -77
- package/src/statements.mts +57 -0
- package/src/type-wrappers.mts +3 -3
package/src/imports.mts
CHANGED
|
@@ -61,8 +61,8 @@ export type ResolvedCallee = Readonly<{
|
|
|
61
61
|
canonicalName: string;
|
|
62
62
|
|
|
63
63
|
/**
|
|
64
|
-
* The identifier to rewrite to change what is called: the callee itself for
|
|
65
|
-
*
|
|
64
|
+
* The identifier to rewrite to change what is called: the callee itself for a
|
|
65
|
+
* named import, the property for a namespace import.
|
|
66
66
|
*/
|
|
67
67
|
propertyNode: TSESTree.Identifier;
|
|
68
68
|
|
|
@@ -140,33 +140,377 @@ export const buildCalleeResolver = (
|
|
|
140
140
|
|
|
141
141
|
/**
|
|
142
142
|
* A fix that inserts `import { <specifiers> } from '<moduleName>';` at the top
|
|
143
|
-
* of the file,
|
|
143
|
+
* of the file, after its directive prologue (`'use client';` stays a
|
|
144
|
+
* directive), or no fix when `specifiers` is empty. Each specifier is written
|
|
144
145
|
* as given, so `type T` imports a type.
|
|
145
146
|
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
147
|
+
* Use it only when the file does not import from `moduleName` by name;
|
|
148
|
+
* {@link bindValueImports} adds to an existing declaration instead, which keeps
|
|
149
|
+
* `import-x/no-duplicates` satisfied, and falls back to this. An import sorter
|
|
150
|
+
* orders the declaration afterwards. Put every name one report needs into a
|
|
151
|
+
* single call: ESLint merges the fixes of one report into one edit, and two
|
|
152
|
+
* fixes inserting at the same place conflict, so that one of them is dropped.
|
|
151
153
|
*/
|
|
152
154
|
export const buildNamedImportFix = (
|
|
153
155
|
fixer: TSESLint.RuleFixer,
|
|
154
156
|
program: TSESTree.Program,
|
|
155
157
|
moduleName: string,
|
|
156
158
|
specifiers: readonly string[],
|
|
157
|
-
): readonly TSESLint.RuleFix[] =>
|
|
158
|
-
specifiers.length === 0
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
159
|
+
): readonly TSESLint.RuleFix[] => {
|
|
160
|
+
if (specifiers.length === 0) {
|
|
161
|
+
return [] as const;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const declaration =
|
|
165
|
+
`import { ${specifiers.join(', ')} } from '${moduleName}';` as const;
|
|
166
|
+
|
|
167
|
+
const lastDirective = program.body
|
|
168
|
+
.filter(
|
|
169
|
+
(statement): statement is TSESTree.ExpressionStatement =>
|
|
170
|
+
statement.type === AST_NODE_TYPES.ExpressionStatement &&
|
|
171
|
+
statement.directive !== undefined,
|
|
172
|
+
)
|
|
173
|
+
.at(-1);
|
|
174
|
+
|
|
175
|
+
if (lastDirective === undefined) {
|
|
176
|
+
return [fixer.insertTextBefore(program, `${declaration}\n`)] as const;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// After the comments that start on the directive's last line as well: one
|
|
180
|
+
// such as `// eslint-disable-line` is about that line.
|
|
181
|
+
const anchor =
|
|
182
|
+
(program.comments ?? [])
|
|
183
|
+
.filter(
|
|
184
|
+
(comment) =>
|
|
185
|
+
comment.range[0] >= lastDirective.range[1] &&
|
|
186
|
+
comment.loc.start.line === lastDirective.loc.end.line,
|
|
187
|
+
)
|
|
188
|
+
.at(-1) ?? lastDirective;
|
|
189
|
+
|
|
190
|
+
return [fixer.insertTextAfter(anchor, `\n${declaration}`)] as const;
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The local names `moduleName`'s exports `exportNames` are bound to as values
|
|
195
|
+
* once `edits` are made, in the same order, or `undefined` when one of them
|
|
196
|
+
* cannot be bound.
|
|
197
|
+
*
|
|
198
|
+
* - Imported as a value: that local name, and no edit.
|
|
199
|
+
* - Imported by `import { type x }`: the edit drops `type`, keeping a comment
|
|
200
|
+
* after it.
|
|
201
|
+
* - Not imported: the edit adds it to the braces of the first import from the
|
|
202
|
+
* module that has a value specifier, or else of the first other one with
|
|
203
|
+
* braces that is not `import type`, empty braces included (`import {}`,
|
|
204
|
+
* `import D, {}`).
|
|
205
|
+
* Adding it there rather than in a new declaration keeps
|
|
206
|
+
* `import-x/no-duplicates` satisfied. Failing that, it goes into new braces
|
|
207
|
+
* after a lone default import (`import D, { x }`), and when the module has
|
|
208
|
+
* neither, a new declaration goes after the directive prologue
|
|
209
|
+
* ({@link buildNamedImportFix}).
|
|
210
|
+
* - Refused when an import already binds the name to something else
|
|
211
|
+
* (`import type { x }`, `import { y as x }`, `import { x } from 'other'`),
|
|
212
|
+
* where adding it would be a duplicate binding.
|
|
213
|
+
*
|
|
214
|
+
* ESLint's default parser leaves `importKind` out, which counts as a value
|
|
215
|
+
* import. Every name a fix writes is bound by that fix's own edits rather than
|
|
216
|
+
* left to another report's: an editor applies one fix at a time, and a fix that
|
|
217
|
+
* loses a conflict with another in the same pass is dropped. Fixes that all add
|
|
218
|
+
* a name overlap, so `--fix` applies one per pass, and the rest in the next, by
|
|
219
|
+
* when the file imports it. Whether something other than an import binds the
|
|
220
|
+
* name where it is written is the caller's to check
|
|
221
|
+
* ({@link hasForeignBinding}).
|
|
222
|
+
*/
|
|
223
|
+
export const bindValueImports = (
|
|
224
|
+
sourceCode: TSESLint.SourceCode,
|
|
225
|
+
moduleName: string,
|
|
226
|
+
exportNames: readonly string[],
|
|
227
|
+
): ValueImportBinding | undefined => {
|
|
228
|
+
const program = sourceCode.ast;
|
|
229
|
+
|
|
230
|
+
const allImports = program.body.filter(
|
|
231
|
+
(node): node is TSESTree.ImportDeclaration =>
|
|
232
|
+
node.type === AST_NODE_TYPES.ImportDeclaration,
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
const declarations = getImportDeclarations(program, moduleName);
|
|
236
|
+
|
|
237
|
+
const named = declarations.flatMap((declaration) =>
|
|
238
|
+
declaration.specifiers.flatMap((specifier) =>
|
|
239
|
+
specifier.type === AST_NODE_TYPES.ImportSpecifier
|
|
240
|
+
? [{ declaration, specifier }]
|
|
241
|
+
: [],
|
|
242
|
+
),
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
const bindings = exportNames.map((exportName) =>
|
|
246
|
+
bindingOf(sourceCode, allImports, named, exportName),
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
if (bindings.includes(undefined)) {
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const defined = bindings.filter((b) => b !== undefined);
|
|
254
|
+
|
|
255
|
+
const toAdd = defined.flatMap((b) => (b.isAdded ? [b.localName] : []));
|
|
256
|
+
|
|
257
|
+
const hosts = hostsFor(declarations);
|
|
258
|
+
|
|
259
|
+
return {
|
|
260
|
+
localNames: defined.map((b) => b.localName),
|
|
261
|
+
edits: (fixer) => [
|
|
262
|
+
...defined.flatMap((b) => b.edits(fixer)),
|
|
263
|
+
...(toAdd.length === 0
|
|
264
|
+
? []
|
|
265
|
+
: addToFirstHost(fixer, sourceCode, moduleName, hosts, toAdd)),
|
|
266
|
+
],
|
|
267
|
+
};
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* The edits that import `moduleName`'s types `names` the file does not import
|
|
272
|
+
* yet: added to an existing `import type { … }` from the module (`, X`), else
|
|
273
|
+
* as `type X` to another import from it, chosen as {@link bindValueImports}
|
|
274
|
+
* chooses one, and only failing those as a new
|
|
275
|
+
* `import { type X } from '<module>'` after the directive prologue. Adding to
|
|
276
|
+
* an existing declaration keeps `import-x/no-duplicates` satisfied. Whether the
|
|
277
|
+
* file binds a name to something else is the caller's to check.
|
|
278
|
+
*/
|
|
279
|
+
export const addTypeImports = (
|
|
280
|
+
fixer: TSESLint.RuleFixer,
|
|
281
|
+
sourceCode: TSESLint.SourceCode,
|
|
282
|
+
moduleName: string,
|
|
283
|
+
names: readonly string[],
|
|
284
|
+
): readonly TSESLint.RuleFix[] => {
|
|
285
|
+
if (names.length === 0) {
|
|
286
|
+
return [];
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const declarations = getImportDeclarations(sourceCode.ast, moduleName);
|
|
290
|
+
|
|
291
|
+
const typeOnlyFix = firstFix(
|
|
292
|
+
declarations.filter(isTypeOnlyDeclaration),
|
|
293
|
+
(declaration) => addToImportBraces(fixer, sourceCode, declaration, names),
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
return typeOnlyFix !== undefined
|
|
297
|
+
? [typeOnlyFix]
|
|
298
|
+
: addToFirstHost(
|
|
299
|
+
fixer,
|
|
300
|
+
sourceCode,
|
|
301
|
+
moduleName,
|
|
302
|
+
hostsFor(declarations),
|
|
303
|
+
names.map((name) => `type ${name}`),
|
|
304
|
+
);
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Adds `specifiers` to the `{ … }` of `declaration`: after its last named
|
|
309
|
+
* specifier, or inside empty braces (`import d, {} from …`). `undefined` when
|
|
310
|
+
* the declaration has no braces (a default or namespace import alone) or there
|
|
311
|
+
* is nothing to add. The braces of import attributes (`with { … }`) are not
|
|
312
|
+
* taken for them.
|
|
313
|
+
*/
|
|
314
|
+
export const addToImportBraces = (
|
|
315
|
+
fixer: TSESLint.RuleFixer,
|
|
316
|
+
sourceCode: TSESLint.SourceCode,
|
|
317
|
+
declaration: TSESTree.ImportDeclaration,
|
|
318
|
+
specifiers: readonly string[],
|
|
319
|
+
): TSESLint.RuleFix | undefined => {
|
|
320
|
+
if (specifiers.length === 0) {
|
|
321
|
+
return undefined;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const lastNamed = declaration.specifiers
|
|
325
|
+
.filter(
|
|
326
|
+
(specifier): specifier is TSESTree.ImportSpecifier =>
|
|
327
|
+
specifier.type === AST_NODE_TYPES.ImportSpecifier,
|
|
328
|
+
)
|
|
329
|
+
.at(-1);
|
|
330
|
+
|
|
331
|
+
if (lastNamed !== undefined) {
|
|
332
|
+
return fixer.insertTextAfter(lastNamed, `, ${specifiers.join(', ')}`);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const openingBrace = sourceCode.getFirstToken(declaration, {
|
|
336
|
+
filter: (token) =>
|
|
337
|
+
token.value === '{' && token.range[1] <= declaration.source.range[0],
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
return openingBrace === null
|
|
341
|
+
? undefined
|
|
342
|
+
: fixer.insertTextAfter(openingBrace, ` ${specifiers.join(', ')} `);
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
/** The exported name `specifier` imports: `x` for `import { x as y }`. */
|
|
346
|
+
export const importedNameOf = (
|
|
168
347
|
specifier: DeepReadonly<TSESTree.ImportSpecifier>,
|
|
169
348
|
): string =>
|
|
170
349
|
specifier.imported.type === AST_NODE_TYPES.Identifier
|
|
171
350
|
? specifier.imported.name
|
|
172
351
|
: specifier.imported.value;
|
|
352
|
+
|
|
353
|
+
/** The first fix `toFix` gives for `declarations`, trying them in order. */
|
|
354
|
+
const firstFix = (
|
|
355
|
+
declarations: readonly TSESTree.ImportDeclaration[],
|
|
356
|
+
toFix: (
|
|
357
|
+
declaration: TSESTree.ImportDeclaration,
|
|
358
|
+
) => TSESLint.RuleFix | undefined,
|
|
359
|
+
): TSESLint.RuleFix | undefined => {
|
|
360
|
+
for (const declaration of declarations) {
|
|
361
|
+
const fix = toFix(declaration);
|
|
362
|
+
|
|
363
|
+
if (fix !== undefined) {
|
|
364
|
+
return fix;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return undefined;
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Adds `specifiers` to the braces of the first of `hosts` that has them, else
|
|
373
|
+
* puts braces after a lone default import among them (`import D, { x }`): a
|
|
374
|
+
* second declaration beside it would be a duplicate. Failing both, a new
|
|
375
|
+
* declaration importing them from `moduleName`.
|
|
376
|
+
*/
|
|
377
|
+
const addToFirstHost = (
|
|
378
|
+
fixer: TSESLint.RuleFixer,
|
|
379
|
+
sourceCode: TSESLint.SourceCode,
|
|
380
|
+
moduleName: string,
|
|
381
|
+
hosts: readonly TSESTree.ImportDeclaration[],
|
|
382
|
+
specifiers: readonly string[],
|
|
383
|
+
): readonly TSESLint.RuleFix[] => {
|
|
384
|
+
const intoBraces = firstFix(hosts, (declaration) =>
|
|
385
|
+
addToImportBraces(fixer, sourceCode, declaration, specifiers),
|
|
386
|
+
);
|
|
387
|
+
|
|
388
|
+
if (intoBraces !== undefined) {
|
|
389
|
+
return [intoBraces];
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const defaultHost = hosts.find(
|
|
393
|
+
(declaration) =>
|
|
394
|
+
declaration.specifiers.length === 1 &&
|
|
395
|
+
declaration.specifiers[0]?.type === AST_NODE_TYPES.ImportDefaultSpecifier,
|
|
396
|
+
)?.specifiers[0];
|
|
397
|
+
|
|
398
|
+
return defaultHost !== undefined
|
|
399
|
+
? [fixer.insertTextAfter(defaultHost, `, { ${specifiers.join(', ')} }`)]
|
|
400
|
+
: buildNamedImportFix(fixer, sourceCode.ast, moduleName, specifiers);
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
/** What {@link bindValueImports} gives. */
|
|
404
|
+
export type ValueImportBinding = Readonly<{
|
|
405
|
+
/** The names to write, in the order of the export names asked for. */
|
|
406
|
+
localNames: readonly string[];
|
|
407
|
+
|
|
408
|
+
/** The edits that bring every one of them into scope. */
|
|
409
|
+
edits: (fixer: TSESLint.RuleFixer) => readonly TSESLint.RuleFix[];
|
|
410
|
+
}>;
|
|
411
|
+
|
|
412
|
+
type NamedSpecifier = Readonly<{
|
|
413
|
+
declaration: TSESTree.ImportDeclaration;
|
|
414
|
+
specifier: TSESTree.ImportSpecifier;
|
|
415
|
+
}>;
|
|
416
|
+
|
|
417
|
+
/** `import type …`. */
|
|
418
|
+
const isTypeOnlyDeclaration = (
|
|
419
|
+
declaration: TSESTree.ImportDeclaration,
|
|
420
|
+
): boolean => declaration.importKind === 'type';
|
|
421
|
+
|
|
422
|
+
/** The declaration is not `import type`. */
|
|
423
|
+
const isNotTypeOnly = ({ declaration }: NamedSpecifier): boolean =>
|
|
424
|
+
!isTypeOnlyDeclaration(declaration);
|
|
425
|
+
|
|
426
|
+
/** Neither `specifier` nor its declaration says `type`. */
|
|
427
|
+
const isValueImport = ({ declaration, specifier }: NamedSpecifier): boolean =>
|
|
428
|
+
isNotTypeOnly({ declaration, specifier }) && specifier.importKind !== 'type';
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* The declarations a name can be added to, in the order to try them: those that
|
|
432
|
+
* are not `import type`, the ones with a value specifier first, since one
|
|
433
|
+
* holding only `type` specifiers is erased (and flagged by
|
|
434
|
+
* `no-import-type-side-effects`) unless a value joins it.
|
|
435
|
+
*/
|
|
436
|
+
const hostsFor = (
|
|
437
|
+
declarations: readonly TSESTree.ImportDeclaration[],
|
|
438
|
+
): readonly TSESTree.ImportDeclaration[] => {
|
|
439
|
+
const notTypeOnly = declarations.filter(
|
|
440
|
+
(declaration) => !isTypeOnlyDeclaration(declaration),
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
const hasValueSpecifier = (
|
|
444
|
+
declaration: TSESTree.ImportDeclaration,
|
|
445
|
+
): boolean =>
|
|
446
|
+
declaration.specifiers.some(
|
|
447
|
+
(specifier) =>
|
|
448
|
+
specifier.type === AST_NODE_TYPES.ImportSpecifier &&
|
|
449
|
+
isValueImport({ declaration, specifier }),
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
return [
|
|
453
|
+
...notTypeOnly.filter(hasValueSpecifier),
|
|
454
|
+
...notTypeOnly.filter((declaration) => !hasValueSpecifier(declaration)),
|
|
455
|
+
];
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
const bindingOf = (
|
|
459
|
+
sourceCode: TSESLint.SourceCode,
|
|
460
|
+
allImports: readonly TSESTree.ImportDeclaration[],
|
|
461
|
+
named: readonly NamedSpecifier[],
|
|
462
|
+
exportName: string,
|
|
463
|
+
):
|
|
464
|
+
| Readonly<{
|
|
465
|
+
localName: string;
|
|
466
|
+
isAdded: boolean;
|
|
467
|
+
edits: (fixer: TSESLint.RuleFixer) => readonly TSESLint.RuleFix[];
|
|
468
|
+
}>
|
|
469
|
+
| undefined => {
|
|
470
|
+
const matching = named.filter(
|
|
471
|
+
({ specifier }) => importedNameOf(specifier) === exportName,
|
|
472
|
+
);
|
|
473
|
+
|
|
474
|
+
const value = matching.find(isValueImport);
|
|
475
|
+
|
|
476
|
+
if (value !== undefined) {
|
|
477
|
+
return {
|
|
478
|
+
localName: value.specifier.local.name,
|
|
479
|
+
isAdded: false,
|
|
480
|
+
edits: () => [],
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const inlineType = matching.find(isNotTypeOnly)?.specifier;
|
|
485
|
+
|
|
486
|
+
if (inlineType !== undefined) {
|
|
487
|
+
const typeKeyword = sourceCode.getFirstToken(inlineType);
|
|
488
|
+
|
|
489
|
+
// Up to whatever follows `type`, a comment included, so that the comment
|
|
490
|
+
// stays.
|
|
491
|
+
const next =
|
|
492
|
+
typeKeyword === null
|
|
493
|
+
? null
|
|
494
|
+
: sourceCode.getTokenAfter(typeKeyword, { includeComments: true });
|
|
495
|
+
|
|
496
|
+
return typeKeyword === null || next === null
|
|
497
|
+
? undefined
|
|
498
|
+
: {
|
|
499
|
+
localName: inlineType.local.name,
|
|
500
|
+
isAdded: false,
|
|
501
|
+
edits: (fixer) => [
|
|
502
|
+
fixer.removeRange([typeKeyword.range[0], next.range[0]]),
|
|
503
|
+
],
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const isTaken = allImports.some((declaration) =>
|
|
508
|
+
declaration.specifiers.some(
|
|
509
|
+
(specifier) => specifier.local.name === exportName,
|
|
510
|
+
),
|
|
511
|
+
);
|
|
512
|
+
|
|
513
|
+
return isTaken
|
|
514
|
+
? undefined
|
|
515
|
+
: { localName: exportName, isAdded: true, edits: () => [] };
|
|
516
|
+
};
|
package/src/index.mts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export * from './bindings.mjs';
|
|
2
|
+
export * from './comments.mjs';
|
|
2
3
|
export * from './imports.mjs';
|
|
3
4
|
export * from './negation.mjs';
|
|
4
5
|
export * from './parentheses.mjs';
|
|
5
6
|
export * from './rules/index.mjs';
|
|
7
|
+
export * from './statements.mjs';
|
|
6
8
|
export * from './type-wrappers.mjs';
|
package/src/negation.mts
CHANGED
|
@@ -137,10 +137,9 @@ export type NegationContext = Readonly<{
|
|
|
137
137
|
checker: ts.TypeChecker;
|
|
138
138
|
getLayerTypes: GetLayerTypes;
|
|
139
139
|
/**
|
|
140
|
-
* Whether `node`'s value is a boolean, so that the negation of `!node` may
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
* node.
|
|
140
|
+
* Whether `node`'s value is a boolean, so that the negation of `!node` may be
|
|
141
|
+
* `node` itself. Where only the truthiness of the negation is read — the test
|
|
142
|
+
* of an `if` — any value will do, and this may answer `true` for every node.
|
|
144
143
|
*/
|
|
145
144
|
isBoolean: (node: DeepReadonly<TSESTree.Expression>) => boolean;
|
|
146
145
|
}>;
|
|
@@ -159,12 +158,12 @@ export type NegationContext = Readonly<{
|
|
|
159
158
|
* | anything else | `!a`, `!(a)` | always |
|
|
160
159
|
*
|
|
161
160
|
* `undefined` when the first two rows' condition does not hold: then the only
|
|
162
|
-
* exact negation is `!!a` or `!(a < b)`, and a caller decides whether to
|
|
163
|
-
*
|
|
164
|
-
*
|
|
161
|
+
* exact negation is `!!a` or `!(a < b)`, and a caller decides whether to write
|
|
162
|
+
* it (with {@link prefixedNegationOf}) or to give up. Inside `&&` and `||`, a
|
|
163
|
+
* side with no short negation is written with `!`.
|
|
165
164
|
*
|
|
166
|
-
* The node is taken as written: a comparison under a type wrapper is not
|
|
167
|
-
*
|
|
165
|
+
* The node is taken as written: a comparison under a type wrapper is not looked
|
|
166
|
+
* into, since inverting it would drop the wrapper.
|
|
168
167
|
*/
|
|
169
168
|
export const negationOf = (
|
|
170
169
|
node: DeepReadonly<TSESTree.Expression>,
|
|
@@ -210,6 +209,34 @@ type Negation = Readonly<{
|
|
|
210
209
|
precedence: Precedence;
|
|
211
210
|
}>;
|
|
212
211
|
|
|
212
|
+
const isLooserThan = (precedence: Precedence, operator: '&&' | '||'): boolean =>
|
|
213
|
+
precedence === 'looser' || (operator === '&&' && precedence === '||');
|
|
214
|
+
|
|
215
|
+
const asOperand = (
|
|
216
|
+
negation: Negation,
|
|
217
|
+
operator: '&&' | '||' | undefined,
|
|
218
|
+
): string =>
|
|
219
|
+
operator !== undefined && isLooserThan(negation.precedence, operator)
|
|
220
|
+
? (`(${negation.text})` as const)
|
|
221
|
+
: negation.text;
|
|
222
|
+
|
|
223
|
+
const precedenceOf = (node: DeepReadonly<TSESTree.Node>): Precedence => {
|
|
224
|
+
if (node.type === AST_NODE_TYPES.LogicalExpression) {
|
|
225
|
+
// `??` cannot be mixed with `&&` or `||` without parentheses.
|
|
226
|
+
return node.operator === '??' ? 'looser' : node.operator;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return LOOSER_THAN_LOGICAL.has(node.type) ? 'looser' : 'tighter';
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* The parser's node, which the source code and the token store take as mutable,
|
|
234
|
+
* for one that this package only reads.
|
|
235
|
+
*/
|
|
236
|
+
const asNode = <T extends TSESTree.Node>(node: DeepReadonly<T>): T =>
|
|
237
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
238
|
+
node as T;
|
|
239
|
+
|
|
213
240
|
const shortNegationOf = (
|
|
214
241
|
node: DeepReadonly<TSESTree.Expression>,
|
|
215
242
|
context: NegationContext,
|
|
@@ -259,26 +286,6 @@ const shortNegationOf = (
|
|
|
259
286
|
};
|
|
260
287
|
};
|
|
261
288
|
|
|
262
|
-
const asOperand = (
|
|
263
|
-
negation: Negation,
|
|
264
|
-
operator: '&&' | '||' | undefined,
|
|
265
|
-
): string =>
|
|
266
|
-
operator !== undefined && isLooserThan(negation.precedence, operator)
|
|
267
|
-
? (`(${negation.text})` as const)
|
|
268
|
-
: negation.text;
|
|
269
|
-
|
|
270
|
-
const isLooserThan = (precedence: Precedence, operator: '&&' | '||'): boolean =>
|
|
271
|
-
precedence === 'looser' || (operator === '&&' && precedence === '||');
|
|
272
|
-
|
|
273
|
-
const precedenceOf = (node: DeepReadonly<TSESTree.Node>): Precedence => {
|
|
274
|
-
if (node.type === AST_NODE_TYPES.LogicalExpression) {
|
|
275
|
-
// `??` cannot be mixed with `&&` or `||` without parentheses.
|
|
276
|
-
return node.operator === '??' ? 'looser' : node.operator;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
return LOOSER_THAN_LOGICAL.has(node.type) ? 'looser' : 'tighter';
|
|
280
|
-
};
|
|
281
|
-
|
|
282
289
|
const LOOSER_THAN_LOGICAL: ReadonlySet<AST_NODE_TYPES> =
|
|
283
290
|
new Set<AST_NODE_TYPES>([
|
|
284
291
|
AST_NODE_TYPES.ArrowFunctionExpression,
|
|
@@ -348,11 +355,3 @@ const isNeverNaN = (type: ts.Type, checker: ts.TypeChecker): boolean => {
|
|
|
348
355
|
checker.typeToString(brandType) === 'false'
|
|
349
356
|
);
|
|
350
357
|
};
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
* The parser's node, which the source code and the token store take as
|
|
354
|
-
* mutable, for one that this package only reads.
|
|
355
|
-
*/
|
|
356
|
-
const asNode = <T extends TSESTree.Node>(node: DeepReadonly<T>): T =>
|
|
357
|
-
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
358
|
-
node as T;
|
|
@@ -3,11 +3,8 @@ import {
|
|
|
3
3
|
type TSESLint,
|
|
4
4
|
type TSESTree,
|
|
5
5
|
} from '@typescript-eslint/utils';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
getImportDeclarations,
|
|
9
|
-
getNamedImports,
|
|
10
|
-
} from '../imports.mjs';
|
|
6
|
+
import { hasForeignBinding } from '../bindings.mjs';
|
|
7
|
+
import { bindValueImports } from '../imports.mjs';
|
|
11
8
|
import { getRangeWithParens } from '../parentheses.mjs';
|
|
12
9
|
import { isIdentifierNamed, skipTypeWrappers } from '../type-wrappers.mjs';
|
|
13
10
|
|
|
@@ -44,10 +41,6 @@ export const createPreferArrayIsArray = ({
|
|
|
44
41
|
create: (context) => {
|
|
45
42
|
const sourceCode = context.sourceCode;
|
|
46
43
|
|
|
47
|
-
const program = sourceCode.ast;
|
|
48
|
-
|
|
49
|
-
const moduleImports = getImportDeclarations(program, moduleName);
|
|
50
|
-
|
|
51
44
|
const mut_nodesToFix: TSESTree.CallExpression[] = [];
|
|
52
45
|
|
|
53
46
|
return {
|
|
@@ -59,37 +52,31 @@ export const createPreferArrayIsArray = ({
|
|
|
59
52
|
mut_nodesToFix.push(node);
|
|
60
53
|
},
|
|
61
54
|
'Program:exit': () => {
|
|
62
|
-
const
|
|
63
|
-
getNamedImports(moduleImports).includes(namespace);
|
|
55
|
+
const binding = bindValueImports(sourceCode, moduleName, [namespace]);
|
|
64
56
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
//
|
|
68
|
-
//
|
|
69
|
-
|
|
70
|
-
for (const [index, node] of mut_nodesToFix.entries()) {
|
|
57
|
+
const localName = binding?.localNames[0];
|
|
58
|
+
|
|
59
|
+
// Every fix brings the namespace into scope itself, so that one
|
|
60
|
+
// applied alone (an editor's quick fix) is complete.
|
|
61
|
+
for (const node of mut_nodesToFix) {
|
|
71
62
|
context.report({
|
|
72
63
|
node,
|
|
73
64
|
messageId: 'useIsArray',
|
|
74
|
-
fix:
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
replacement,
|
|
90
|
-
),
|
|
91
|
-
];
|
|
92
|
-
},
|
|
65
|
+
fix:
|
|
66
|
+
binding === undefined ||
|
|
67
|
+
localName === undefined ||
|
|
68
|
+
hasForeignBinding(sourceCode, node, localName, moduleName)
|
|
69
|
+
? null
|
|
70
|
+
: (fixer) => [
|
|
71
|
+
...binding.edits(fixer),
|
|
72
|
+
// Only the callee is replaced, type wrappers included,
|
|
73
|
+
// along with the parentheses around it, which its range
|
|
74
|
+
// leaves out; the arguments stay as written.
|
|
75
|
+
fixer.replaceTextRange(
|
|
76
|
+
getRangeWithParens(sourceCode, node.callee),
|
|
77
|
+
`${localName}.isArray`,
|
|
78
|
+
),
|
|
79
|
+
],
|
|
93
80
|
});
|
|
94
81
|
}
|
|
95
82
|
},
|