File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed
tests/rules/immutable-data/ts/array Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -165,7 +165,7 @@ const objectConstructorMutatorFunctions = new Set([
165
165
] ) ;
166
166
167
167
/**
168
- * Object constructor functions that return a new array .
168
+ * Object constructor functions that return new objects .
169
169
*/
170
170
const objectConstructorNewObjectReturningMethods = [
171
171
"create" ,
@@ -180,6 +180,13 @@ const objectConstructorNewObjectReturningMethods = [
180
180
"values" ,
181
181
] ;
182
182
183
+ /**
184
+ * String constructor functions that return new objects.
185
+ *
186
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Methods
187
+ */
188
+ const stringConstructorNewObjectReturningMethods = [ "split" ] ;
189
+
183
190
/**
184
191
* Check if the given assignment expression violates this rule.
185
192
*/
@@ -393,6 +400,16 @@ function isInChainCallAndFollowsNew(
393
400
) {
394
401
return true ;
395
402
}
403
+
404
+ // Check for: "".split("")
405
+ if (
406
+ stringConstructorNewObjectReturningMethods . some (
407
+ isExpected ( node . object . callee . property . name ) ,
408
+ ) &&
409
+ getTypeOfNode ( node . object . callee . object , context ) . isStringLiteral ( )
410
+ ) {
411
+ return true ;
412
+ }
396
413
}
397
414
398
415
return false ;
Original file line number Diff line number Diff line change @@ -330,6 +330,14 @@ const tests: Array<ValidTestCaseSet<OptionsOf<typeof rule>>> = [
330
330
` ,
331
331
optionsSet : [ [ { ignoreImmediateMutation : true } ] ] ,
332
332
} ,
333
+ {
334
+ code : dedent `
335
+ "foo".split("").sort();
336
+ const bar = "bar";
337
+ bar.split("").sort();
338
+ ` ,
339
+ optionsSet : [ [ { ignoreImmediateMutation : true } ] ] ,
340
+ } ,
333
341
] ;
334
342
335
343
export default tests ;
You can’t perform that action at this time.
0 commit comments