noSubstr
此内容尚不支持你的语言。
Diagnostic Category: lint/nursery/noSubstr
Since: v1.8.2
Sources:
- Same as:
unicorn/prefer-string-slice
Enforce the use of String.slice()
over String.substr()
and String.substring()
.
String.slice()
is preferred over String.substr()
and String.substring()
because it is a more popular option with clearer behavior,
and it has a consistent counterpart in arrays.
Note that String.substr
, String.substring
and String.slice
are not identical when arguments are passed.
For detailed differences, refer to the MDN documentation:
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.js:1:5 lint/nursery/noSubstr FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Avoid using substr and consider using slice instead.
> 1 │ foo.substr();
│ ^^^^^^
2 │
ℹ slice is more commonly used and has a less surprising behavior.
ℹ See MDN web docs for more details.
ℹ Unsafe fix: Use .slice() instead.
1 │ - foo.substr();
1 │ + foo.slice();
2 2 │
code-block.js:1:5 lint/nursery/noSubstr FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Avoid using substring and consider using slice instead.
> 1 │ foo.substring();
│ ^^^^^^^^^
2 │
ℹ slice is more commonly used and has a less surprising behavior.
ℹ See MDN web docs for more details.
ℹ Unsafe fix: Use .slice() instead.
1 │ - foo.substring();
1 │ + foo.slice();
2 2 │