@@ -12,7 +12,18 @@ export default createRule('prefer-svelte-reactivity', {
12
12
category : 'Possible Errors' ,
13
13
recommended : true
14
14
} ,
15
- schema : [ ] ,
15
+ schema : [
16
+ {
17
+ type : 'object' ,
18
+ properties : {
19
+ ignoreEncapsulatedLocalVariables : {
20
+ type : 'boolean' ,
21
+ default : true
22
+ }
23
+ } ,
24
+ additionalProperties : false
25
+ }
26
+ ] ,
16
27
messages : {
17
28
mutableDateUsed :
18
29
'Found a mutable instance of the built-in Date class. Use SvelteDate instead.' ,
@@ -31,6 +42,8 @@ export default createRule('prefer-svelte-reactivity', {
31
42
]
32
43
} ,
33
44
create ( context ) {
45
+ const ignoreEncapsulatedLocalVariables =
46
+ context . options [ 0 ] ?. ignoreEncapsulatedLocalVariables ?? true ;
34
47
const returnedVariables : Map <
35
48
TSESTree . ArrowFunctionExpression | TSESTree . FunctionDeclaration ,
36
49
TSESTree . VariableDeclarator [ ]
@@ -136,6 +149,9 @@ export default createRule('prefer-svelte-reactivity', {
136
149
node
137
150
} ) ;
138
151
}
152
+ if ( ignoreEncapsulatedLocalVariables && isEncapsulated ( returnedVariables , node ) ) {
153
+ continue ;
154
+ }
139
155
if ( path [ 0 ] === 'Date' && isDateMutable ( referenceTracker , node as TSESTree . Expression ) ) {
140
156
context . report ( {
141
157
messageId : 'mutableDateUsed' ,
@@ -198,6 +214,22 @@ function findEnclosingFunction(
198
214
return findAncestorOfTypes ( node , [ 'ArrowFunctionExpression' , 'FunctionDeclaration' ] ) ;
199
215
}
200
216
217
+ function isEncapsulated (
218
+ returnedVariables : Map <
219
+ TSESTree . ArrowFunctionExpression | TSESTree . FunctionDeclaration ,
220
+ TSESTree . VariableDeclarator [ ]
221
+ > ,
222
+ node : TSESTree . Node
223
+ ) : boolean {
224
+ const enclosingFunction = findEnclosingFunction ( node ) ;
225
+ if ( enclosingFunction === null ) {
226
+ return false ;
227
+ }
228
+ return (
229
+ returnedVariables . get ( enclosingFunction ) ?. some ( ( variable ) => isIn ( node , variable ) ) !== true
230
+ ) ;
231
+ }
232
+
201
233
function isDateMutable ( referenceTracker : ReferenceTracker , ctorNode : TSESTree . Expression ) : boolean {
202
234
return ! referenceTracker
203
235
. iteratePropertyReferences ( ctorNode , {
0 commit comments