eslint-plugin-react-x 5.30.0-beta.3 → 5.30.0-beta.4
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/dist/index.js +109 -45
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DEFAULT_ESLINT_REACT_SETTINGS, getSettingsFromContext, toRegExp } from "@eslint-react/shared";
|
|
2
2
|
import * as ESLintUtils from "@typescript-eslint/utils/eslint-utils";
|
|
3
|
-
import { Check, Collect, Compare, Extract, Traverse } from "@eslint-react/ast";
|
|
3
|
+
import { Analyze, Check, Collect, Compare, Extract, Traverse } from "@eslint-react/ast";
|
|
4
4
|
import * as core from "@eslint-react/core";
|
|
5
5
|
import { getConstrainedTypeAtLocation, getText, merge, report } from "@eslint-react/eslint";
|
|
6
6
|
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
@@ -130,7 +130,7 @@ const rules$6 = {
|
|
|
130
130
|
};
|
|
131
131
|
// package.json
|
|
132
132
|
var name$6 = "eslint-plugin-react-x";
|
|
133
|
-
var version = "5.30.0-beta.
|
|
133
|
+
var version = "5.30.0-beta.4";
|
|
134
134
|
// src/utils/create-rule.ts
|
|
135
135
|
function getDocsUrl(ruleName) {
|
|
136
136
|
return `https://eslint-react.xyz/docs/rules/${ruleName}`;
|
|
@@ -1997,7 +1997,7 @@ function memoize(f) {
|
|
|
1997
1997
|
function not(predicate) {
|
|
1998
1998
|
return (data) => !predicate(data);
|
|
1999
1999
|
}
|
|
2000
|
-
|
|
2000
|
+
dual(2, (a, b) => (data) => a(data) && b(data));
|
|
2001
2001
|
const or = dual(2, (a, b) => (data) => a(data) || b(data));
|
|
2002
2002
|
dual(2, (a, b) => (data) => a(data) !== b(data));
|
|
2003
2003
|
dual(2, (a, b) => (data) => a(data) === b(data));
|
|
@@ -4292,18 +4292,6 @@ function getGuardDisposition(node, getNullBranch) {
|
|
|
4292
4292
|
}
|
|
4293
4293
|
return null;
|
|
4294
4294
|
}
|
|
4295
|
-
function isUnconditionallyTerminating(statement) {
|
|
4296
|
-
switch (statement.type) {
|
|
4297
|
-
case AST_NODE_TYPES.ReturnStatement:
|
|
4298
|
-
case AST_NODE_TYPES.ThrowStatement: return true;
|
|
4299
|
-
case AST_NODE_TYPES.BlockStatement: {
|
|
4300
|
-
const last = statement.body.at(-1);
|
|
4301
|
-
return last != null && isUnconditionallyTerminating(last);
|
|
4302
|
-
}
|
|
4303
|
-
case AST_NODE_TYPES.IfStatement: return statement.alternate != null && isUnconditionallyTerminating(statement.consequent) && isUnconditionallyTerminating(statement.alternate);
|
|
4304
|
-
default: return false;
|
|
4305
|
-
}
|
|
4306
|
-
}
|
|
4307
4295
|
function isAfterTerminatingNonNullGuard(node, getNullBranch) {
|
|
4308
4296
|
let statement = node;
|
|
4309
4297
|
while (statement.parent?.type !== AST_NODE_TYPES.BlockStatement) {
|
|
@@ -4318,7 +4306,7 @@ function isAfterTerminatingNonNullGuard(node, getNullBranch) {
|
|
|
4318
4306
|
const nullBranch = getNullBranch(sibling.test);
|
|
4319
4307
|
if (nullBranch == null) continue;
|
|
4320
4308
|
const nonNullBranch = nullBranch === "alternate" ? sibling.consequent : sibling.alternate;
|
|
4321
|
-
return nonNullBranch != null && isUnconditionallyTerminating(nonNullBranch);
|
|
4309
|
+
return nonNullBranch != null && Analyze.isUnconditionallyTerminating(nonNullBranch);
|
|
4322
4310
|
}
|
|
4323
4311
|
return false;
|
|
4324
4312
|
}
|
|
@@ -6374,10 +6362,11 @@ function createFactCollector$3(additionalStateHooks, additionalEffectHooks) {
|
|
|
6374
6362
|
};
|
|
6375
6363
|
}
|
|
6376
6364
|
// src/rules/set-state-in-effect/helpers.ts
|
|
6377
|
-
function
|
|
6378
|
-
|
|
6379
|
-
|
|
6380
|
-
return
|
|
6365
|
+
function getImmediateInvocation(fn) {
|
|
6366
|
+
let parent = fn.parent;
|
|
6367
|
+
while (Check.isTypeOrChainExpression(parent)) parent = parent.parent;
|
|
6368
|
+
if (parent.type !== AST_NODE_TYPES.CallExpression) return null;
|
|
6369
|
+
return Extract.unwrap(parent.callee) === fn ? parent : null;
|
|
6381
6370
|
}
|
|
6382
6371
|
function getConditionalBranch(node) {
|
|
6383
6372
|
let child = node;
|
|
@@ -6541,6 +6530,47 @@ function createSetStateResolver$1(context, facts = EMPTY_FACTS) {
|
|
|
6541
6530
|
}
|
|
6542
6531
|
return refParams;
|
|
6543
6532
|
}
|
|
6533
|
+
function isDeferredInvocation(invocation) {
|
|
6534
|
+
let current = invocation.parent;
|
|
6535
|
+
while (current != null) {
|
|
6536
|
+
if (Check.isFunction(current)) return false;
|
|
6537
|
+
switch (current.type) {
|
|
6538
|
+
case AST_NODE_TYPES.AssignmentPattern: return true;
|
|
6539
|
+
case AST_NODE_TYPES.PropertyDefinition: {
|
|
6540
|
+
if (current.static) return false;
|
|
6541
|
+
const value = current.value;
|
|
6542
|
+
return value != null && (value === invocation || Traverse.hasParent(invocation, (n) => n === value));
|
|
6543
|
+
}
|
|
6544
|
+
default: current = current.parent;
|
|
6545
|
+
}
|
|
6546
|
+
}
|
|
6547
|
+
return false;
|
|
6548
|
+
}
|
|
6549
|
+
function getVisibleWriteLocation(value, id) {
|
|
6550
|
+
let branch = getConditionalBranch(value);
|
|
6551
|
+
let node = value;
|
|
6552
|
+
let order = 1;
|
|
6553
|
+
let boundary = Traverse.findParent(value, Check.isFunction);
|
|
6554
|
+
while (boundary != null) {
|
|
6555
|
+
if (Traverse.hasParent(id, (n) => n === boundary)) return {
|
|
6556
|
+
branch,
|
|
6557
|
+
node,
|
|
6558
|
+
order
|
|
6559
|
+
};
|
|
6560
|
+
if (boundary.async || boundary.generator) return null;
|
|
6561
|
+
const invocation = getImmediateInvocation(boundary);
|
|
6562
|
+
if (invocation == null || isDeferredInvocation(invocation)) return null;
|
|
6563
|
+
branch ??= getConditionalBranch(invocation);
|
|
6564
|
+
node = invocation;
|
|
6565
|
+
order = 0;
|
|
6566
|
+
boundary = Traverse.findParent(invocation, Check.isFunction);
|
|
6567
|
+
}
|
|
6568
|
+
return {
|
|
6569
|
+
branch,
|
|
6570
|
+
node,
|
|
6571
|
+
order
|
|
6572
|
+
};
|
|
6573
|
+
}
|
|
6544
6574
|
function isInitializedFromRef(id, refParams, seen = /* @__PURE__ */ new Set()) {
|
|
6545
6575
|
const variable = findVariable(context.sourceCode.getScope(id), id);
|
|
6546
6576
|
if (variable == null) return false;
|
|
@@ -6549,11 +6579,16 @@ function createSetStateResolver$1(context, facts = EMPTY_FACTS) {
|
|
|
6549
6579
|
seen.add(key);
|
|
6550
6580
|
if (isStateHeldDomNode(variable)) return true;
|
|
6551
6581
|
const candidates = [];
|
|
6552
|
-
const addCandidate = (value,
|
|
6553
|
-
|
|
6582
|
+
const addCandidate = (value, visibility = {
|
|
6583
|
+
branch: getConditionalBranch(value),
|
|
6584
|
+
node: value,
|
|
6585
|
+
order: 1
|
|
6586
|
+
}, previous) => {
|
|
6587
|
+
if (visibility.node.range[1] > id.range[0] || candidates.some((c) => c.value === value)) return;
|
|
6554
6588
|
candidates.push({
|
|
6555
|
-
branch:
|
|
6556
|
-
|
|
6589
|
+
branch: visibility.branch,
|
|
6590
|
+
order: visibility.order,
|
|
6591
|
+
position: visibility.node.range[1],
|
|
6557
6592
|
previous,
|
|
6558
6593
|
value
|
|
6559
6594
|
});
|
|
@@ -6566,12 +6601,13 @@ function createSetStateResolver$1(context, facts = EMPTY_FACTS) {
|
|
|
6566
6601
|
if (!reference.isWrite() || reference.writeExpr == null) continue;
|
|
6567
6602
|
if (reference.init === true && variable.defs.some((d) => d.type === DefinitionType.Parameter && refParams?.has(d.name) === true)) continue;
|
|
6568
6603
|
const writeExpr = reference.writeExpr;
|
|
6569
|
-
|
|
6604
|
+
const visibility = getVisibleWriteLocation(writeExpr, id);
|
|
6605
|
+
if (visibility == null) continue;
|
|
6570
6606
|
const parent = reference.identifier.parent;
|
|
6571
|
-
addCandidate(writeExpr, parent.type === AST_NODE_TYPES.AssignmentExpression && parent.operator !== "=" && Extract.unwrap(parent.left) === reference.identifier ? reference.identifier : void 0);
|
|
6607
|
+
addCandidate(writeExpr, visibility, parent.type === AST_NODE_TYPES.AssignmentExpression && parent.operator !== "=" && Extract.unwrap(parent.left) === reference.identifier ? reference.identifier : void 0);
|
|
6572
6608
|
}
|
|
6573
6609
|
const survivingCandidates = [];
|
|
6574
|
-
for (const candidate of [...candidates].sort((a, b) => a.position - b.position)) {
|
|
6610
|
+
for (const candidate of [...candidates].sort((a, b) => a.position - b.position || a.order - b.order)) {
|
|
6575
6611
|
if (candidate.branch == null) survivingCandidates.length = 0;
|
|
6576
6612
|
else {
|
|
6577
6613
|
const previousIndex = survivingCandidates.findIndex((o) => o.branch === candidate.branch);
|
|
@@ -6621,7 +6657,11 @@ function createSetStateResolver$1(context, facts = EMPTY_FACTS) {
|
|
|
6621
6657
|
case AST_NODE_TYPES.LogicalExpression: return isRefValue(expression.left, refParams, seen, mode) || isRefValue(expression.right, refParams, seen, mode);
|
|
6622
6658
|
case AST_NODE_TYPES.UnaryExpression:
|
|
6623
6659
|
case AST_NODE_TYPES.UpdateExpression: return isRefValue(expression.argument, refParams, seen, mode);
|
|
6624
|
-
case AST_NODE_TYPES.ConditionalExpression: return
|
|
6660
|
+
case AST_NODE_TYPES.ConditionalExpression: return [
|
|
6661
|
+
mode === "initializer" ? expression.test : null,
|
|
6662
|
+
expression.consequent,
|
|
6663
|
+
expression.alternate
|
|
6664
|
+
].some((e) => e != null && isRefValue(e, refParams, seen, mode));
|
|
6625
6665
|
case AST_NODE_TYPES.SequenceExpression: return expression.expressions.some((p) => isRefValue(p, refParams, seen, mode));
|
|
6626
6666
|
case AST_NODE_TYPES.TemplateLiteral: return expression.expressions.some((p) => isRefValue(p, refParams, seen, mode));
|
|
6627
6667
|
case AST_NODE_TYPES.TaggedTemplateExpression: return isRefValue(expression.tag, refParams, seen, mode) || isRefValue(expression.quasi, refParams, seen, mode);
|
|
@@ -6644,6 +6684,29 @@ function createSetStateResolver$1(context, facts = EMPTY_FACTS) {
|
|
|
6644
6684
|
function isRefInExpression(node, refParams) {
|
|
6645
6685
|
return Collect.getIdentifiers(node).some((id) => isInitializedFromRef(id, refParams));
|
|
6646
6686
|
}
|
|
6687
|
+
function hasRefGatedTerminator(statements, refParams) {
|
|
6688
|
+
return statements.some((statement) => isRefGatedTerminator(statement, refParams));
|
|
6689
|
+
}
|
|
6690
|
+
function isRefGatedTerminator(statement, refParams) {
|
|
6691
|
+
switch (statement.type) {
|
|
6692
|
+
case AST_NODE_TYPES.IfStatement: return isRefInExpression(statement.test, refParams) && (Analyze.isTerminatingStatement(statement.consequent) || statement.alternate != null && Analyze.isTerminatingStatement(statement.alternate));
|
|
6693
|
+
case AST_NODE_TYPES.BlockStatement: return hasRefGatedTerminator(statement.body, refParams);
|
|
6694
|
+
case AST_NODE_TYPES.TryStatement:
|
|
6695
|
+
if (statement.finalizer != null && hasRefGatedTerminator(statement.finalizer.body, refParams)) return true;
|
|
6696
|
+
if (statement.handler != null && !Analyze.isTerminatingStatement(statement.handler.body)) return false;
|
|
6697
|
+
return hasRefGatedTerminator(statement.block.body, refParams);
|
|
6698
|
+
default: return false;
|
|
6699
|
+
}
|
|
6700
|
+
}
|
|
6701
|
+
function hasRefGatedEarlyExit(statements, child, refParams) {
|
|
6702
|
+
const index = statements.findIndex((statement) => statement === child);
|
|
6703
|
+
if (index < 0) return false;
|
|
6704
|
+
for (let i = 0; i < index; i++) {
|
|
6705
|
+
const statement = statements[i];
|
|
6706
|
+
if (statement != null && isRefGatedTerminator(statement, refParams)) return true;
|
|
6707
|
+
}
|
|
6708
|
+
return false;
|
|
6709
|
+
}
|
|
6647
6710
|
function isRefGatedContext(node, refParams) {
|
|
6648
6711
|
let child = node;
|
|
6649
6712
|
let current = node.parent;
|
|
@@ -6657,15 +6720,16 @@ function createSetStateResolver$1(context, facts = EMPTY_FACTS) {
|
|
|
6657
6720
|
case AST_NODE_TYPES.LogicalExpression:
|
|
6658
6721
|
if (child === current.right && isRefInExpression(current.left, refParams)) return true;
|
|
6659
6722
|
break;
|
|
6660
|
-
case AST_NODE_TYPES.
|
|
6661
|
-
|
|
6662
|
-
for (const statement of current.body.slice(0, Math.max(0, index))) {
|
|
6663
|
-
if (statement.type !== AST_NODE_TYPES.IfStatement) continue;
|
|
6664
|
-
if (!isRefInExpression(statement.test, refParams)) continue;
|
|
6665
|
-
if (isTerminatingStatement(statement.consequent)) return true;
|
|
6666
|
-
}
|
|
6723
|
+
case AST_NODE_TYPES.WhileStatement:
|
|
6724
|
+
if (child === current.body && isRefInExpression(current.test, refParams)) return true;
|
|
6667
6725
|
break;
|
|
6668
|
-
|
|
6726
|
+
case AST_NODE_TYPES.ForStatement:
|
|
6727
|
+
if ((child === current.body || child === current.update) && current.test != null && isRefInExpression(current.test, refParams)) return true;
|
|
6728
|
+
break;
|
|
6729
|
+
case AST_NODE_TYPES.BlockStatement:
|
|
6730
|
+
if (hasRefGatedEarlyExit(current.body, child, refParams)) return true;
|
|
6731
|
+
break;
|
|
6732
|
+
case AST_NODE_TYPES.SwitchCase: if (hasRefGatedEarlyExit(current.consequent, child, refParams)) return true;
|
|
6669
6733
|
}
|
|
6670
6734
|
child = current;
|
|
6671
6735
|
current = current.parent;
|
|
@@ -6704,24 +6768,24 @@ function inferViolations$2(context, facts) {
|
|
|
6704
6768
|
visited.add(key);
|
|
6705
6769
|
queue.push(state);
|
|
6706
6770
|
};
|
|
6707
|
-
const report = (
|
|
6708
|
-
|
|
6709
|
-
|
|
6710
|
-
|
|
6771
|
+
const report = (setter, reportNode) => {
|
|
6772
|
+
const name = resolver.getSetterName(setter.node);
|
|
6773
|
+
const key = `${String(reportNode.range[0])}:${String(reportNode.range[1])}:${name}`;
|
|
6774
|
+
getOrInsertComputed(violations, key, () => ({
|
|
6775
|
+
name,
|
|
6711
6776
|
node: reportNode
|
|
6712
|
-
});
|
|
6777
|
+
}));
|
|
6713
6778
|
};
|
|
6714
6779
|
const isExemptCall = (call, state) => {
|
|
6715
6780
|
if (state.inheritedRefGate || resolver.isRefGatedContext(call, state.refParams)) return true;
|
|
6716
|
-
|
|
6717
|
-
return argument != null && resolver.isArgumentUsingRefValue(argument, state.refParams);
|
|
6781
|
+
return call.arguments.some((argument) => resolver.isArgumentUsingRefValue(argument, state.refParams));
|
|
6718
6782
|
};
|
|
6719
6783
|
for (const root of [...facts.effectRoots].sort((a, b) => a.node.range[0] - b.node.range[0])) {
|
|
6720
6784
|
const setup = root.setup;
|
|
6721
6785
|
const unwrappedSetup = Extract.unwrap(setup);
|
|
6722
6786
|
for (const target of resolver.resolveValues(setup)) {
|
|
6723
6787
|
if (target.kind === "setter") {
|
|
6724
|
-
if (!resolver.isRefGatedContext(setup)) report(target
|
|
6788
|
+
if (!resolver.isRefGatedContext(setup)) report(target, setup);
|
|
6725
6789
|
continue;
|
|
6726
6790
|
}
|
|
6727
6791
|
const reportNode = unwrappedSetup === target.node ? null : setup;
|
|
@@ -6739,7 +6803,7 @@ function inferViolations$2(context, facts) {
|
|
|
6739
6803
|
const calls = callsByFunction.get(state.fn) ?? [];
|
|
6740
6804
|
for (const call of calls) for (const target of resolver.resolveValues(call.callee)) {
|
|
6741
6805
|
if (target.kind === "setter") {
|
|
6742
|
-
if (!isExemptCall(call, state)) report(
|
|
6806
|
+
if (!isExemptCall(call, state)) report(target, state.reportNode ?? call);
|
|
6743
6807
|
continue;
|
|
6744
6808
|
}
|
|
6745
6809
|
enqueue({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-react-x",
|
|
3
|
-
"version": "5.30.0-beta.
|
|
3
|
+
"version": "5.30.0-beta.4",
|
|
4
4
|
"description": "A set of composable ESLint rules for libraries and frameworks that use React as a UI runtime.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -36,12 +36,12 @@
|
|
|
36
36
|
"dist"
|
|
37
37
|
],
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@eslint-react/ast": "5.30.0-beta.
|
|
40
|
-
"@eslint-react/core": "5.30.0-beta.
|
|
41
|
-
"@eslint-react/eslint": "5.30.0-beta.
|
|
42
|
-
"@eslint-react/jsx": "5.30.0-beta.
|
|
43
|
-
"@eslint-react/shared": "5.30.0-beta.
|
|
44
|
-
"@eslint-react/var": "5.30.0-beta.
|
|
39
|
+
"@eslint-react/ast": "5.30.0-beta.4",
|
|
40
|
+
"@eslint-react/core": "5.30.0-beta.4",
|
|
41
|
+
"@eslint-react/eslint": "5.30.0-beta.4",
|
|
42
|
+
"@eslint-react/jsx": "5.30.0-beta.4",
|
|
43
|
+
"@eslint-react/shared": "5.30.0-beta.4",
|
|
44
|
+
"@eslint-react/var": "5.30.0-beta.4",
|
|
45
45
|
"@typescript-eslint/scope-manager": "^8.72.0",
|
|
46
46
|
"@typescript-eslint/types": "^8.72.0",
|
|
47
47
|
"@typescript-eslint/utils": "^8.72.0",
|