Skip to content

noUselessStringConcat

¥Summary

¥How to configure

biome.json
{
"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