@blumintinc/eslint-plugin-blumint 1.20.90 → 1.20.92
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/lib/index.js
CHANGED
|
@@ -203,6 +203,15 @@ exports.preferMapOverConditionalDispatch = (0, createRule_1.createRule)({
|
|
|
203
203
|
* Resolve a case-test / equality-test node to a literal key. Handles inline
|
|
204
204
|
* literals directly and constant references (e.g. `THIS_DEVICE_STATUS.active`)
|
|
205
205
|
* via the checker.
|
|
206
|
+
*
|
|
207
|
+
* A test the checker had to resolve carries its source text along with the
|
|
208
|
+
* value: the value proves the key is a literal, but emitting the value as
|
|
209
|
+
* the key destroys the expression it came from. For a constant reference
|
|
210
|
+
* that orphans the constant (an imported one then trips `no-unused-vars`)
|
|
211
|
+
* and bakes its value into the call site, discarding the single source of
|
|
212
|
+
* truth the constant exists to provide; for `case -1:` the resolved value
|
|
213
|
+
* does not even print as a legal object key (`{ -1: v }` does not parse).
|
|
214
|
+
* The fixer emits a computed key from the source text instead.
|
|
206
215
|
*/
|
|
207
216
|
function resolveLiteralKey(node) {
|
|
208
217
|
if (node.type === utils_1.AST_NODE_TYPES.Literal) {
|
|
@@ -218,7 +227,11 @@ exports.preferMapOverConditionalDispatch = (0, createRule_1.createRule)({
|
|
|
218
227
|
if (!type) {
|
|
219
228
|
return null;
|
|
220
229
|
}
|
|
221
|
-
|
|
230
|
+
const literal = literalValueOf(type);
|
|
231
|
+
if (!literal) {
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
return { ...literal, sourceText: sourceCode.getText(node) };
|
|
222
235
|
}
|
|
223
236
|
function computeValueTypeText(exprs) {
|
|
224
237
|
const seen = new Set();
|
|
@@ -589,6 +602,13 @@ exports.preferMapOverConditionalDispatch = (0, createRule_1.createRule)({
|
|
|
589
602
|
}
|
|
590
603
|
// ---- Fix construction ---------------------------------------------------
|
|
591
604
|
function formatKey(key) {
|
|
605
|
+
if (key.sourceText !== undefined) {
|
|
606
|
+
// Computed key preserving the constant reference the case tested on.
|
|
607
|
+
// Type-safe by construction: the fix is reached only after the checker
|
|
608
|
+
// resolved this expression to a string/number *literal* type, so the
|
|
609
|
+
// computed key is a literal key and the Record stays exhaustive.
|
|
610
|
+
return `[${key.sourceText}]`;
|
|
611
|
+
}
|
|
592
612
|
if (key.kind === 'number') {
|
|
593
613
|
return String(key.value);
|
|
594
614
|
}
|
|
@@ -5,6 +5,8 @@ const utils_1 = require("@typescript-eslint/utils");
|
|
|
5
5
|
const createRule_1 = require("../utils/createRule");
|
|
6
6
|
const ASTHelpers_1 = require("../utils/ASTHelpers");
|
|
7
7
|
const importInsertion_1 = require("../utils/importInsertion");
|
|
8
|
+
const importRemoval_1 = require("../utils/importRemoval");
|
|
9
|
+
const disableDirectives_1 = require("../utils/disableDirectives");
|
|
8
10
|
const DEEP_COMPARE_MODULE = '@blumintinc/use-deep-compare';
|
|
9
11
|
const DEEP_COMPARE_HOOK = 'useDeepCompareMemo';
|
|
10
12
|
// Consider these as memoizing hooks producing stable references
|
|
@@ -199,6 +201,50 @@ function bindsHookImport(variable, hookImport) {
|
|
|
199
201
|
variable.defs.length > 0 &&
|
|
200
202
|
variable.defs.every((def) => def.node === hookImport));
|
|
201
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* Whether the name the rewrite spells resolves, at this call site, to nothing or
|
|
206
|
+
* to the very import this fix would insert. Any other binding makes the edit
|
|
207
|
+
* wrong twice over: the inserted import declares the name a second time
|
|
208
|
+
* (TS2440/TS2300), and a shadowing parameter or local silently routes the call
|
|
209
|
+
* to the wrong value with no diagnostic at all. Such a call site keeps its
|
|
210
|
+
* report and stays out of the batch, so the author migrates it deliberately —
|
|
211
|
+
* and, still spelling `useMemo`, it holds the specifier the batch would
|
|
212
|
+
* otherwise unbind.
|
|
213
|
+
*/
|
|
214
|
+
function emitsResolvableHook(call, hookImport) {
|
|
215
|
+
const existing = ASTHelpers_1.ASTHelpers.findVariableInScope(call.scope, DEEP_COMPARE_HOOK);
|
|
216
|
+
return !existing || bindsHookImport(existing, hookImport);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* The single fix that converts every call in `calls`, imports the hook once, and
|
|
220
|
+
* unbinds whatever the conversions stop reading.
|
|
221
|
+
*
|
|
222
|
+
* Batching is what makes the unbinding reachable at all. Judged one call at a
|
|
223
|
+
* time, a file with two convertible calls never sees either as the specifier's
|
|
224
|
+
* sole remaining reference, and once both are rewritten the rule no longer
|
|
225
|
+
* reports — so nothing revisits the stranded import. The batch is sound only
|
|
226
|
+
* because it contains exactly the calls this one fix rewrites: siblings the
|
|
227
|
+
* caller has already dropped for being suppressed or unfixable are absent, so no
|
|
228
|
+
* unbinding is ever claimed on the strength of an edit that does not happen.
|
|
229
|
+
*/
|
|
230
|
+
function convertCallsFixes(context, fixer, calls) {
|
|
231
|
+
// The callees are the text this fix deletes, so they are also the text
|
|
232
|
+
// whatever carried the hook stops being read from: `useMemo` for a bare call,
|
|
233
|
+
// `React` for a member call. A binding left with no reference at all is
|
|
234
|
+
// unbound here, in this same fix — stripping its last use and keeping the
|
|
235
|
+
// declaration trades this rule's report for an unused-import one, and nothing
|
|
236
|
+
// re-reports that debt once the rewrite has resolved the original violation.
|
|
237
|
+
const importRemoval = (0, importRemoval_1.planOrphanedImportRemoval)(context.sourceCode, calls.map((call) => call.node.callee.range));
|
|
238
|
+
// No plan means a binding is orphaned yet cannot be unbound safely, so the
|
|
239
|
+
// rewrite stays too: the report without a fixer is the lesser damage.
|
|
240
|
+
if (!importRemoval)
|
|
241
|
+
return null;
|
|
242
|
+
return [
|
|
243
|
+
...calls.map((call) => fixer.replaceText(call.node.callee, DEEP_COMPARE_HOOK)),
|
|
244
|
+
...ensureDeepCompareImportFixes(context, fixer),
|
|
245
|
+
...importRemoval.map((range) => fixer.removeRange([range[0], range[1]])),
|
|
246
|
+
];
|
|
247
|
+
}
|
|
202
248
|
function ensureDeepCompareImportFixes(context, fixer) {
|
|
203
249
|
const sourceCode = context.sourceCode;
|
|
204
250
|
const program = sourceCode.ast;
|
|
@@ -218,49 +264,6 @@ function ensureDeepCompareImportFixes(context, fixer) {
|
|
|
218
264
|
(0, importInsertion_1.insertAtImportAnchor)(sourceCode, fixer, (0, importInsertion_1.importAnchorLineStart)(sourceCode, anchor), importText),
|
|
219
265
|
];
|
|
220
266
|
}
|
|
221
|
-
function findVariableInScope(scope, name) {
|
|
222
|
-
if (!scope)
|
|
223
|
-
return null;
|
|
224
|
-
const found = scope.variables?.find((v) => v.name === name);
|
|
225
|
-
if (found)
|
|
226
|
-
return found;
|
|
227
|
-
if (Array.isArray(scope.childScopes)) {
|
|
228
|
-
for (const child of scope.childScopes) {
|
|
229
|
-
const v = findVariableInScope(child, name);
|
|
230
|
-
if (v)
|
|
231
|
-
return v;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
return null;
|
|
235
|
-
}
|
|
236
|
-
function isIdentifierReferenced(sourceCode, name) {
|
|
237
|
-
const scope = sourceCode.scopeManager?.globalScope;
|
|
238
|
-
if (!scope)
|
|
239
|
-
return true;
|
|
240
|
-
const variable = findVariableInScope(scope, name);
|
|
241
|
-
if (!variable)
|
|
242
|
-
return false;
|
|
243
|
-
return variable.references.some((ref) => ref.identifier && ref.identifier.name === name);
|
|
244
|
-
}
|
|
245
|
-
function removeImportSpecifierFixes(sourceCode, fixer, importDecl, specifier) {
|
|
246
|
-
const fixes = [];
|
|
247
|
-
if (importDecl.specifiers.length === 1) {
|
|
248
|
-
fixes.push(fixer.remove(importDecl));
|
|
249
|
-
return fixes;
|
|
250
|
-
}
|
|
251
|
-
const tokenAfter = sourceCode.getTokenAfter(specifier);
|
|
252
|
-
const tokenBefore = sourceCode.getTokenBefore(specifier);
|
|
253
|
-
if (tokenAfter && tokenAfter.value === ',') {
|
|
254
|
-
fixes.push(fixer.removeRange([specifier.range[0], tokenAfter.range[1]]));
|
|
255
|
-
}
|
|
256
|
-
else if (tokenBefore && tokenBefore.value === ',') {
|
|
257
|
-
fixes.push(fixer.removeRange([tokenBefore.range[0], specifier.range[1]]));
|
|
258
|
-
}
|
|
259
|
-
else {
|
|
260
|
-
fixes.push(fixer.remove(specifier));
|
|
261
|
-
}
|
|
262
|
-
return fixes;
|
|
263
|
-
}
|
|
264
267
|
function isImportedIdentifier(context, name) {
|
|
265
268
|
const sourceCode = context.sourceCode;
|
|
266
269
|
const program = sourceCode.ast;
|
|
@@ -354,6 +357,18 @@ exports.preferUseDeepCompareMemo = (0, createRule_1.createRule)({
|
|
|
354
357
|
defaultOptions: [],
|
|
355
358
|
create(context) {
|
|
356
359
|
const memoizedIds = collectMemoizedIdentifiers(context);
|
|
360
|
+
// Reporting is deferred to Program:exit because the import rewrite depends
|
|
361
|
+
// on knowing every conversion in the file: the `useMemo` specifier may only
|
|
362
|
+
// be unbound once no reference to it survives the fix, and a file where two
|
|
363
|
+
// call sites convert in the same pass has no later pass to notice that.
|
|
364
|
+
const calls = [];
|
|
365
|
+
/**
|
|
366
|
+
* A suppressed report is discarded together with its fix, yet its
|
|
367
|
+
* `useMemo(...)` call stays in the file. Counting such a call as converted
|
|
368
|
+
* would unbind an import the surviving text still spells — trading an unused
|
|
369
|
+
* import for a dangling reference, a lint warning for a compile error.
|
|
370
|
+
*/
|
|
371
|
+
const isReportSuppressed = (0, disableDirectives_1.createSuppressionChecker)(context);
|
|
357
372
|
return {
|
|
358
373
|
CallExpression(node) {
|
|
359
374
|
if (!isUseMemoCallee(node.callee))
|
|
@@ -412,63 +427,35 @@ exports.preferUseDeepCompareMemo = (0, createRule_1.createRule)({
|
|
|
412
427
|
}
|
|
413
428
|
if (!hasUnmemoizedNonPrimitive)
|
|
414
429
|
return;
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
else if (node.callee.type === utils_1.AST_NODE_TYPES.MemberExpression) {
|
|
445
|
-
fixes.push(fixer.replaceText(node.callee, 'useDeepCompareMemo'));
|
|
446
|
-
}
|
|
447
|
-
// Ensure import exists
|
|
448
|
-
fixes.push(...ensureDeepCompareImportFixes(context, fixer));
|
|
449
|
-
// Clean up now-unused React/useMemo imports if safe
|
|
450
|
-
const sourceCode = context.sourceCode;
|
|
451
|
-
const program = sourceCode.ast;
|
|
452
|
-
const reactImport = program.body.find((n) => n.type === utils_1.AST_NODE_TYPES.ImportDeclaration &&
|
|
453
|
-
n.source.value === 'react');
|
|
454
|
-
if (reactImport) {
|
|
455
|
-
// remove named useMemo if unused
|
|
456
|
-
const useMemoSpec = reactImport.specifiers.find((s) => s.type === utils_1.AST_NODE_TYPES.ImportSpecifier &&
|
|
457
|
-
s.imported.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
458
|
-
s.imported.name === 'useMemo');
|
|
459
|
-
if (useMemoSpec &&
|
|
460
|
-
!isIdentifierReferenced(sourceCode, useMemoSpec.local.name)) {
|
|
461
|
-
fixes.push(...removeImportSpecifierFixes(sourceCode, fixer, reactImport, useMemoSpec));
|
|
462
|
-
}
|
|
463
|
-
const defaultSpec = reactImport.specifiers.find((s) => s.type === utils_1.AST_NODE_TYPES.ImportDefaultSpecifier);
|
|
464
|
-
if (defaultSpec &&
|
|
465
|
-
!isIdentifierReferenced(sourceCode, defaultSpec.local.name)) {
|
|
466
|
-
fixes.push(...removeImportSpecifierFixes(sourceCode, fixer, reactImport, defaultSpec));
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
return fixes;
|
|
470
|
-
},
|
|
471
|
-
});
|
|
430
|
+
calls.push({ node, scope: ASTHelpers_1.ASTHelpers.getScope(context, node) });
|
|
431
|
+
},
|
|
432
|
+
'Program:exit'() {
|
|
433
|
+
if (calls.length === 0)
|
|
434
|
+
return;
|
|
435
|
+
const hookImport = findDeepCompareMemoImport(context.sourceCode.ast);
|
|
436
|
+
// Exactly the calls the carrier's fix rewrites: a suppressed report
|
|
437
|
+
// loses its fix, and one whose scope binds the hook name to something
|
|
438
|
+
// else must not be rewritten at all.
|
|
439
|
+
const converted = calls.filter((call) => !isReportSuppressed(call.node) &&
|
|
440
|
+
emitsResolvableHook(call, hookImport));
|
|
441
|
+
// The carrier is the first violation whose fix actually survives, so a
|
|
442
|
+
// suppressed or unfixable leading violation cannot take the batch down
|
|
443
|
+
// with it. Every other report emits without a fixer: the carrier's one
|
|
444
|
+
// pass already resolves them, and a second fixer would either duplicate
|
|
445
|
+
// its edits or contradict them.
|
|
446
|
+
const [carrier] = converted;
|
|
447
|
+
for (const call of calls) {
|
|
448
|
+
context.report({
|
|
449
|
+
node: call.node,
|
|
450
|
+
messageId: 'preferUseDeepCompareMemo',
|
|
451
|
+
data: {
|
|
452
|
+
hook: 'useMemo',
|
|
453
|
+
},
|
|
454
|
+
fix: call === carrier
|
|
455
|
+
? (fixer) => convertCallsFixes(context, fixer, converted)
|
|
456
|
+
: null,
|
|
457
|
+
});
|
|
458
|
+
}
|
|
472
459
|
},
|
|
473
460
|
};
|
|
474
461
|
},
|
package/package.json
CHANGED
package/release-manifest.json
CHANGED
|
@@ -1,4 +1,40 @@
|
|
|
1
1
|
[
|
|
2
|
+
{
|
|
3
|
+
"version": "1.20.92",
|
|
4
|
+
"date": "2026-08-03T19:30:56.799Z",
|
|
5
|
+
"rules": [
|
|
6
|
+
{
|
|
7
|
+
"name": "prefer-map-over-conditional-dispatch",
|
|
8
|
+
"changeType": "fix",
|
|
9
|
+
"issues": [
|
|
10
|
+
1663
|
|
11
|
+
],
|
|
12
|
+
"summary": "emit computed keys for resolved case tests (closes #1663)"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"name": "prefer-use-deep-compare-memo",
|
|
16
|
+
"changeType": "fix",
|
|
17
|
+
"issues": [
|
|
18
|
+
1662
|
|
19
|
+
],
|
|
20
|
+
"summary": "convert every call site in one edit (closes #1662)"
|
|
21
|
+
}
|
|
22
|
+
]
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"version": "1.20.91",
|
|
26
|
+
"date": "2026-08-03T15:15:42.831Z",
|
|
27
|
+
"rules": [
|
|
28
|
+
{
|
|
29
|
+
"name": "prefer-use-deep-compare-memo",
|
|
30
|
+
"changeType": "fix",
|
|
31
|
+
"issues": [
|
|
32
|
+
1661
|
|
33
|
+
],
|
|
34
|
+
"summary": "unbind the orphaned useMemo import (closes #1661)"
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
},
|
|
2
38
|
{
|
|
3
39
|
"version": "1.20.90",
|
|
4
40
|
"date": "2026-08-03T13:37:18.271Z",
|