|
1 | 1 | import type { AST } from 'svelte-eslint-parser'; |
2 | 2 | import { createRule } from '../utils'; |
3 | 3 | import type { Scope, Variable, Reference, Definition } from '@typescript-eslint/scope-manager'; |
| 4 | +import type { TSESTree } from '@typescript-eslint/types'; |
4 | 5 |
|
5 | 6 | export default createRule('no-immutable-reactive-statements', { |
6 | 7 | meta: { |
@@ -55,45 +56,79 @@ export default createRule('no-immutable-reactive-statements', { |
55 | 56 | // Global variables are assumed to be immutable. |
56 | 57 | return true; |
57 | 58 | } |
58 | | - const isMutable = variable.defs.some((def) => { |
59 | | - if (def.type === 'Variable') { |
60 | | - const parent = def.parent; |
61 | | - if (parent.kind === 'const') { |
62 | | - return false; |
63 | | - } |
64 | | - const pp = parent.parent; |
65 | | - if (pp && pp.type === 'ExportNamedDeclaration' && pp.declaration === parent) { |
66 | | - // Props |
67 | | - return true; |
68 | | - } |
69 | | - return hasWrite(variable); |
70 | | - } |
| 59 | + const isMutableDefine = variable.defs.some((def) => { |
71 | 60 | if (def.type === 'ImportBinding') { |
72 | 61 | return false; |
73 | 62 | } |
74 | | - |
75 | 63 | if (def.node.type === 'AssignmentExpression') { |
76 | 64 | // Reactive values |
77 | 65 | return true; |
78 | 66 | } |
| 67 | + if (def.type === 'Variable') { |
| 68 | + const parent = def.parent; |
| 69 | + if (parent.kind === 'const') { |
| 70 | + if ( |
| 71 | + def.node.init && |
| 72 | + (def.node.init.type === 'FunctionExpression' || |
| 73 | + def.node.init.type === 'ArrowFunctionExpression' || |
| 74 | + def.node.init.type === 'Literal') |
| 75 | + ) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + } else { |
| 79 | + const pp = parent.parent; |
| 80 | + if (pp && pp.type === 'ExportNamedDeclaration' && pp.declaration === parent) { |
| 81 | + // Props |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + return hasWrite(variable); |
| 86 | + } |
79 | 87 | return false; |
80 | 88 | }); |
81 | | - cacheMutableVariable.set(variable, isMutable); |
82 | | - return isMutable; |
| 89 | + cacheMutableVariable.set(variable, isMutableDefine); |
| 90 | + return isMutableDefine; |
83 | 91 | } |
84 | 92 |
|
85 | 93 | /** Checks whether the given variable has a write or reactive store reference or not. */ |
86 | 94 | function hasWrite(variable: Variable) { |
87 | 95 | const defIds = variable.defs.map((def: Definition) => def.name); |
88 | | - return variable.references.some( |
89 | | - (reference) => |
| 96 | + for (const reference of variable.references) { |
| 97 | + if ( |
90 | 98 | reference.isWrite() && |
91 | 99 | !defIds.some( |
92 | 100 | (defId) => |
93 | 101 | defId.range[0] <= reference.identifier.range[0] && |
94 | 102 | reference.identifier.range[1] <= defId.range[1] |
95 | 103 | ) |
96 | | - ); |
| 104 | + ) { |
| 105 | + return true; |
| 106 | + } |
| 107 | + if (isMutableMember(reference.identifier)) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + } |
| 111 | + return false; |
| 112 | + |
| 113 | + function isMutableMember( |
| 114 | + expr: TSESTree.Identifier | TSESTree.JSXIdentifier | TSESTree.MemberExpression |
| 115 | + ): boolean { |
| 116 | + if (expr.type === 'JSXIdentifier') return false; |
| 117 | + const parent = expr.parent; |
| 118 | + if (parent.type === 'AssignmentExpression') { |
| 119 | + return parent.left === expr; |
| 120 | + } |
| 121 | + if (parent.type === 'UpdateExpression') { |
| 122 | + return parent.argument === expr; |
| 123 | + } |
| 124 | + if (parent.type === 'UnaryExpression') { |
| 125 | + return parent.operator === 'delete' && parent.argument === expr; |
| 126 | + } |
| 127 | + if (parent.type === 'MemberExpression') { |
| 128 | + return parent.object === expr && isMutableMember(parent); |
| 129 | + } |
| 130 | + return false; |
| 131 | + } |
97 | 132 | } |
98 | 133 |
|
99 | 134 | /** |
|
0 commit comments