]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Disallow unnecessary concatenation of strings (no-useless-concat) |
2 | ||
3 | It's unnecessary to concatenate two strings together, such as: | |
4 | ||
5 | ```js | |
6 | var foo = "a" + "b"; | |
7 | ``` | |
8 | ||
9 | 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: | |
10 | ||
11 | ```js | |
12 | var foo = "ab"; | |
13 | ``` | |
14 | ||
15 | ## Rule Details | |
16 | ||
17 | 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. | |
18 | ||
19 | Examples of **incorrect** code for this rule: | |
20 | ||
21 | ```js | |
22 | /*eslint no-useless-concat: "error"*/ | |
23 | /*eslint-env es6*/ | |
24 | ||
25 | var a = `some` + `string`; | |
26 | ||
27 | // these are the same as "10" | |
28 | var a = '1' + '0'; | |
29 | var a = '1' + `0`; | |
30 | var a = `1` + '0'; | |
31 | var a = `1` + `0`; | |
32 | ``` | |
33 | ||
34 | Examples of **correct** code for this rule: | |
35 | ||
36 | ```js | |
37 | /*eslint no-useless-concat: "error"*/ | |
38 | ||
39 | // when a non string is included | |
40 | var c = a + b; | |
41 | var c = '1' + a; | |
42 | var a = 1 + '1'; | |
43 | var c = 1 - 2; | |
44 | // when the string concatenation is multiline | |
45 | var c = "foo" + | |
46 | "bar"; | |
47 | ``` | |
48 | ||
49 | ## When Not To Use It | |
50 | ||
51 | If you don't want to be notified about unnecessary string concatenation, you can safely disable this rule. |