no-useless-concat
禁止不必要的字面或模板字面串联
没有必要将两个字符串连接在一起,例如:
¥It’s unnecessary to concatenate two strings together, such as:
var 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:
var 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"*/
var a = `some` + `string`;
// these are the same as "10"
var a = '1' + '0';
var a = '1' + `0`;
var a = `1` + '0';
var a = `1` + `0`;
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-useless-concat: "error"*/
// when a non string is included
var c = a + b;
var c = '1' + a;
var a = 1 + '1';
var c = 1 - 2;
// when the string concatenation is multiline
var c = "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 中引入。