no-useless-concat

禁止不必要的字面或模板字面串联

❄️ Frozen

This rule is currently frozen and is not accepting feature requests.

没有必要将两个字符串连接在一起,例如:

¥It’s unnecessary to concatenate two strings together, such as:

const foo = "a" + "b";

此代码可能是从串联中删除变量的重构结果(例如 "a" + b + "b")。在这种情况下,串联并不重要,代码可以重写为:

¥This code is likely the result of refactoring where a variable was removed from the concatenation (such as "a" + b + "b"). In such a case, the concatenation isn’t important and the code can be rewritten as:

const foo = "ab";

规则详情

¥Rule Details

此规则旨在标记 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.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-useless-concat: "error"*/

const a = `some` + `string`;

// these are the same as "10"
const b = '1' + '0';
const c = '1' + `0`;
const d = `1` + '0';
const e = `1` + `0`;

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-useless-concat: "error"*/

// when a non string is included
const a = a + b;
const b = '1' + a;
const c = 1 + '1';
const d = 1 - 2;
// when the string concatenation is multiline
const e = "foo" +
    "bar";

何时不使用

¥When Not To Use It

如果你不想收到有关不必要的字符串连接的通知,你可以安全地禁用此规则。

¥If you don’t want to be notified about unnecessary string concatenation, you can safely disable this rule.

版本

此规则是在 ESLint v1.3.0 中引入。

资源