@@ -12,7 +12,18 @@ export default createRule('prefer-svelte-reactivity', {
1212 category : 'Possible Errors' ,
1313 recommended : true
1414 } ,
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+ ] ,
1627 messages : {
1728 mutableDateUsed :
1829 'Found a mutable instance of the built-in Date class. Use SvelteDate instead.' ,
@@ -31,6 +42,8 @@ export default createRule('prefer-svelte-reactivity', {
3142 ]
3243 } ,
3344 create ( context ) {
45+ const ignoreEncapsulatedLocalVariables =
46+ context . options [ 0 ] ?. ignoreEncapsulatedLocalVariables ?? true ;
3447 const returnedVariables : Map <
3548 TSESTree . ArrowFunctionExpression | TSESTree . FunctionDeclaration ,
3649 TSESTree . VariableDeclarator [ ]
@@ -136,6 +149,9 @@ export default createRule('prefer-svelte-reactivity', {
136149 node
137150 } ) ;
138151 }
152+ if ( ignoreEncapsulatedLocalVariables && isEncapsulated ( returnedVariables , node ) ) {
153+ continue ;
154+ }
139155 if ( path [ 0 ] === 'Date' && isDateMutable ( referenceTracker , node as TSESTree . Expression ) ) {
140156 context . report ( {
141157 messageId : 'mutableDateUsed' ,
@@ -198,6 +214,22 @@ function findEnclosingFunction(
198214 return findAncestorOfTypes ( node , [ 'ArrowFunctionExpression' , 'FunctionDeclaration' ] ) ;
199215}
200216
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+
201233function isDateMutable ( referenceTracker : ReferenceTracker , ctorNode : TSESTree . Expression ) : boolean {
202234 return ! referenceTracker
203235 . iteratePropertyReferences ( ctorNode , {
0 commit comments