noSubstr
¥Summary
-
规则生效日期:
v1.8.2¥Rule available since:
v1.8.2 -
诊断类别:
lint/style/noSubstr¥Diagnostic Category:
lint/style/noSubstr -
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
unicorn/prefer-string-slice相同¥Same as
unicorn/prefer-string-slice
-
¥How to configure
{ "linter": { "rules": { "style": { "noSubstr": "error" } } }}¥Description
强制使用 String.slice() 而不是 String.substr() 和 String.substring()。
¥Enforce the use of String.slice() over String.substr() and String.substring().
String.slice() 优于 String.substr() 和 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.
请注意,传递参数时,String.substr、String.substring 和 String.slice 并不相同。有关详细差异,请参阅 MDN 文档:
¥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
¥Invalid
foo.substr();code-block.js:1:5 lint/style/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 │
foo.substring();code-block.js:1:5 lint/style/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 │
¥Valid
foo.slice(beginIndex, endIndex);¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号