noUselessStringConcat
¥Summary
-
规则生效日期:
v1.8.0¥Rule available since:
v1.8.0 -
诊断类别:
lint/complexity/noUselessStringConcat¥Diagnostic Category:
lint/complexity/noUselessStringConcat -
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
no-useless-concat相同¥Same as
no-useless-concat
-
¥How to configure
{ "linter": { "rules": { "complexity": { "noUselessStringConcat": "error" } } }}¥Description
禁止不必要的字符串或模板文字连接。
¥Disallow unnecessary concatenation of string or template literals.
此规则旨在标记字符串或模板字面量在可以合并为单个字面量时的连接操作。值得注意的是,这也包括将字符串与数字连接(与 ESLint 的衍生规则不同)。
¥This rule aims to flag concatenation of string or template literals when they could be combined into a single literal. Notably, this also includes concatenating a string with a number (unlike the derivative ESLint rule).
允许连接多个字符串以处理多行字符串(例如用于防止超过最大行宽的字符串)。
¥Concatenation of multiple strings is allowed for multi-line strings (such as ones used to prevent exceeding the maximum line width).
¥Examples
¥Invalid
const a = "a" + "b";code-block.js:1:11 lint/complexity/noUselessStringConcat FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Useless string concatenation.
> 1 │ const a = “a” + “b”;
│ ^^^^^^^^^
2 │
ℹ Consider turning the expression into a single string to improve readability and runtime performance.
ℹ Safe fix: Remove the useless concatenation
1 │ - const·a·=·“a”·+·“b”;
1 │ + const·a·=·“ab”;
2 2 │
const foo = "string" + 123;code-block.js:1:13 lint/complexity/noUselessStringConcat FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Useless string concatenation.
> 1 │ const foo = “string” + 123;
│ ^^^^^^^^^^^^^^
2 │
ℹ Consider turning the expression into a single string to improve readability and runtime performance.
ℹ Safe fix: Remove the useless concatenation
1 │ - const·foo·=·“string”·+·123;
1 │ + const·foo·=·“string123”;
2 2 │
const a = "a" + "b" + "c";code-block.js:1:11 lint/complexity/noUselessStringConcat FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Useless string concatenation.
> 1 │ const a = “a” + “b” + “c”;
│ ^^^^^^^^^^^^^^^
2 │
ℹ Consider turning the expression into a single string to improve readability and runtime performance.
ℹ Safe fix: Remove the useless concatenation
1 │ - const·a·=·“a”·+·“b”·+·“c”;
1 │ + const·a·=·“abc”;
2 2 │
const a = (foo + "a") + ("b" + "c");code-block.js:1:26 lint/complexity/noUselessStringConcat FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Useless string concatenation.
> 1 │ const a = (foo + “a”) + (“b” + “c”);
│ ^^^^^^^^^
2 │
ℹ Consider turning the expression into a single string to improve readability and runtime performance.
ℹ Safe fix: Remove the useless concatenation
1 │ - const·a·=·(foo·+·“a”)·+·(“b”·+·“c”);
1 │ + const·a·=·(foo·+·“a”)·+·(“bc”);
2 2 │
¥Valid
const a = 1 + 1;const a = 1 * '2';const a = 1 - 2;const a = foo + bar;const a = 'foo' + bar;忽略多行字符串:
¥Multi-line strings are ignored:
const multiline = 'foo' + // formatting 'bar'const alsoMultiline = 'foo' + 'bar' + `baz`¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号