Skip to content

noUselessStringConcat

诊断类别:lint/complexity/noUselessStringConcat

¥Diagnostic Category: lint/complexity/noUselessStringConcat

自从:v1.8.0

¥Since: v1.8.0

来源:

¥Sources:

禁止不必要的字符串或模板文字连接。

¥Disallow unnecessary concatenation of string or template literals.

此规则旨在标记 2 个文字的连接,当它们可以组合成单个文字时。文字可以是字符串或模板文字。不检查对象和函数访问等复杂情况。

¥This rule aims to flag the concatenation of 2 literals when they could be combined into a single literal. Literals can be strings or template literals. Concatenation of multiple strings is allowed when the strings are spread over multiple lines in order 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.

Unsafe fix: Remove the useless concatenation

1 - const·a·=·a·+·b;
1+ const·a·=·ab;
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.

Unsafe 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.

Unsafe 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;
const a = 'foo' +
'bar'

¥Related links