版本

no-useless-concat

禁止不必要的字面量或模板字面量连接

❄️ 已冻结

此规则目前已冻结,不接受功能请求。

将两个字符串连接在一起是不必要的,例如

const foo = "a" + "b";

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

const foo = "ab";

规则详情

此规则旨在标记当可以将 2 个字面量组合成单个字面量时的连接。字面量可以是字符串或模板字面量。

此规则的错误代码示例

在 Playground 中打开
/*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`;

此规则的正确代码示例

在 Playground 中打开
/*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";

何时不使用它

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

版本

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

资源

更改语言