no-useless-concat
禁止不必要的字面或模板字面串联
没有必要将两个字符串连接在一起,例如:
var foo = "a" + "b";
此代码可能是从串联中删除变量的重构结果(例如 "a" + b + "b"
)。在这种情况下,串联并不重要,代码可以重写为:
var foo = "ab";
规则详情
此规则旨在标记 2 个字面的连接,当它们可以组合成一个字面时。字面可以是字符串或模板字面。
此规则的错误代码示例:
/*eslint no-useless-concat: "error"*/
/*eslint-env es6*/
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`;
DzO9VpSFHJiTjGCgt1KSooNhAIn1o64th84QTOygwWSy7fBgj4TGxtI1vaW0J9Ta
/*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";
何时不使用
7I3t+ntWvdCkAFy4U1wYe8EPcjlsPzN9QiyGYe5HIdUwynl9tOMjodBeCpshmwBd2dbN6UrAnya7vG9jkBQ8FZQcnKi2/QZZlgJdHO57FjVfcD/UsFx4oIjee3UVpdhVFICG2neEolN9JZ9KxZydVQ==