-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathno-immutable-reactive-statements.ts
187 lines (178 loc) · 5.22 KB
/
no-immutable-reactive-statements.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import type { AST } from 'svelte-eslint-parser';
import { createRule } from '../utils';
import type { Scope, Variable, Reference, Definition } from '@typescript-eslint/scope-manager';
import type { TSESTree } from '@typescript-eslint/types';
export default createRule('no-immutable-reactive-statements', {
meta: {
docs: {
description: "disallow reactive statements that don't reference reactive values.",
category: 'Best Practices',
// TODO Switch to recommended in the major version.
recommended: false
},
schema: [],
messages: {
immutable:
'This statement is not reactive because all variables referenced in the reactive statement are immutable.'
},
type: 'suggestion'
},
create(context) {
const scopeManager = context.getSourceCode().scopeManager;
const globalScope = scopeManager.globalScope;
const toplevelScope =
globalScope?.childScopes.find((scope) => scope.type === 'module') || globalScope;
if (!globalScope || !toplevelScope) {
return {};
}
const cacheMutableVariable = new WeakMap<Variable, boolean>();
/**
* Checks whether the given reference is a mutable variable or not.
*/
function isMutableVariableReference(reference: Reference) {
if (reference.identifier.name.startsWith('$')) {
// It is reactive store reference.
return true;
}
if (!reference.resolved) {
// Unknown variable
return true;
}
return isMutableVariable(reference.resolved);
}
/**
* Checks whether the given variable is a mutable variable or not.
*/
function isMutableVariable(variable: Variable) {
const cache = cacheMutableVariable.get(variable);
if (cache != null) {
return cache;
}
if (variable.defs.length === 0) {
// Global variables are assumed to be immutable.
return true;
}
const isMutableDefine = variable.defs.some((def) => {
if (def.type === 'ImportBinding') {
return false;
}
if (def.node.type === 'AssignmentExpression') {
// Reactive values
return true;
}
if (def.type === 'Variable') {
const parent = def.parent;
if (parent.kind === 'const') {
if (
def.node.init &&
(def.node.init.type === 'FunctionExpression' ||
def.node.init.type === 'ArrowFunctionExpression' ||
def.node.init.type === 'Literal')
) {
return false;
}
} else {
const pp = parent.parent;
if (pp && pp.type === 'ExportNamedDeclaration' && pp.declaration === parent) {
// Props
return true;
}
}
return hasWrite(variable);
}
return false;
});
cacheMutableVariable.set(variable, isMutableDefine);
return isMutableDefine;
}
/** Checks whether the given variable has a write or reactive store reference or not. */
function hasWrite(variable: Variable) {
const defIds = variable.defs.map((def: Definition) => def.name);
for (const reference of variable.references) {
if (
reference.isWrite() &&
!defIds.some(
(defId) =>
defId.range[0] <= reference.identifier.range[0] &&
reference.identifier.range[1] <= defId.range[1]
)
) {
return true;
}
if (isMutableMember(reference.identifier)) {
return true;
}
}
return false;
function isMutableMember(
expr: TSESTree.Identifier | TSESTree.JSXIdentifier | TSESTree.MemberExpression
): boolean {
if (expr.type === 'JSXIdentifier') return false;
const parent = expr.parent;
if (parent.type === 'AssignmentExpression') {
return parent.left === expr;
}
if (parent.type === 'UpdateExpression') {
return parent.argument === expr;
}
if (parent.type === 'UnaryExpression') {
return parent.operator === 'delete' && parent.argument === expr;
}
if (parent.type === 'MemberExpression') {
return parent.object === expr && isMutableMember(parent);
}
return false;
}
}
/**
* Iterates through references to top-level variables in the given range.
*/
function* iterateRangeReferences(scope: Scope, range: [number, number]) {
for (const variable of scope.variables) {
for (const reference of variable.references) {
if (
range[0] <= reference.identifier.range[0] &&
reference.identifier.range[1] <= range[1]
) {
yield reference;
}
}
}
}
return {
SvelteReactiveStatement(node: AST.SvelteReactiveStatement) {
for (const reference of iterateRangeReferences(toplevelScope, node.range)) {
if (reference.isWriteOnly()) {
continue;
}
if (isMutableVariableReference(reference)) {
return;
}
}
for (const through of toplevelScope.through.filter(
(reference) =>
node.range[0] <= reference.identifier.range[0] &&
reference.identifier.range[1] <= node.range[1]
)) {
if (through.identifier.name.startsWith('$$')) {
// Builtin `$$` vars
return;
}
if (through.resolved == null) {
// Do not report if there are missing references.
return;
}
}
context.report({
node:
node.body.type === 'ExpressionStatement' &&
node.body.expression.type === 'AssignmentExpression' &&
node.body.expression.operator === '='
? node.body.expression.right
: node.body,
messageId: 'immutable'
});
}
};
}
});