Index

no-useless-concat

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

❄️ Frozen

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

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

🌐 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

该规则旨在标记可以合并为单个字面量时的两个字面量连接。字面量可以是字符串或模板字面量。

🌐 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";

选项

🌐 Options

此规则没有选项。

🌐 This rule has no options.

何时不使用

🌐 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 中引入。

资源