]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview disallow unnecessary concatenation of literals or template literals | |
3 | * @author Henry Zhu | |
4 | */ | |
5 | "use strict"; | |
6 | ||
7 | //------------------------------------------------------------------------------ | |
8 | // Requirements | |
9 | //------------------------------------------------------------------------------ | |
10 | ||
11 | const rule = require("../../../lib/rules/no-useless-concat"), | |
12 | ||
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | ||
16 | //------------------------------------------------------------------------------ | |
17 | // Tests | |
18 | //------------------------------------------------------------------------------ | |
19 | ||
20 | const ruleTester = new RuleTester(); | |
21 | ||
22 | ruleTester.run("no-useless-concat", rule, { | |
23 | ||
24 | valid: [ | |
25 | "var a = 1 + 1;", | |
26 | "var a = 1 * '2';", | |
27 | "var a = 1 - 2;", | |
28 | "var a = foo + bar;", | |
29 | "var a = 'foo' + bar;", | |
30 | "var foo = 'foo' +\n 'bar';", | |
31 | ||
32 | // https://github.com/eslint/eslint/issues/3575 | |
33 | "var string = (number + 1) + 'px';", | |
34 | "'a' + 1", | |
35 | "1 + '1'", | |
36 | { code: "1 + `1`", parserOptions: { ecmaVersion: 6 } }, | |
37 | { code: "`1` + 1", parserOptions: { ecmaVersion: 6 } }, | |
38 | { code: "(1 + +2) + `b`", parserOptions: { ecmaVersion: 6 } } | |
39 | ], | |
40 | ||
41 | invalid: [ | |
42 | { | |
43 | code: "'a' + 'b'", | |
44 | errors: [ | |
56c4a2cb DC |
45 | { |
46 | messageId: "unexpectedConcat", | |
47 | line: 1, | |
48 | column: 5, | |
49 | endLine: 1, | |
50 | endColumn: 6 | |
51 | } | |
52 | ] | |
53 | }, | |
54 | { | |
55 | code: "'a' +\n'b' + 'c'", | |
56 | errors: [ | |
57 | { | |
58 | messageId: "unexpectedConcat", | |
59 | line: 2, | |
60 | column: 5, | |
61 | endLine: 2, | |
62 | endColumn: 6 | |
63 | } | |
eb39fafa DC |
64 | ] |
65 | }, | |
66 | { | |
67 | code: "foo + 'a' + 'b'", | |
68 | errors: [ | |
69 | { messageId: "unexpectedConcat" } | |
70 | ] | |
71 | }, | |
72 | { | |
73 | code: "'a' + 'b' + 'c'", | |
74 | errors: [ | |
75 | { | |
76 | messageId: "unexpectedConcat", | |
77 | line: 1, | |
56c4a2cb DC |
78 | column: 5, |
79 | endLine: 1, | |
80 | endColumn: 6 | |
eb39fafa DC |
81 | }, |
82 | { | |
83 | messageId: "unexpectedConcat", | |
84 | line: 1, | |
56c4a2cb DC |
85 | column: 11, |
86 | endLine: 1, | |
87 | endColumn: 12 | |
eb39fafa DC |
88 | } |
89 | ] | |
90 | }, | |
91 | { | |
92 | code: "(foo + 'a') + ('b' + 'c')", | |
93 | errors: [ | |
94 | { column: 13, messageId: "unexpectedConcat" }, | |
95 | { column: 20, messageId: "unexpectedConcat" } | |
96 | ] | |
97 | }, | |
98 | { | |
99 | code: "`a` + 'b'", | |
100 | parserOptions: { ecmaVersion: 6 }, | |
101 | errors: [ | |
102 | { messageId: "unexpectedConcat" } | |
103 | ] | |
104 | }, | |
105 | { | |
106 | code: "`a` + `b`", | |
107 | parserOptions: { ecmaVersion: 6 }, | |
108 | errors: [ | |
109 | { messageId: "unexpectedConcat" } | |
110 | ] | |
111 | }, | |
112 | { | |
113 | code: "foo + `a` + `b`", | |
114 | parserOptions: { ecmaVersion: 6 }, | |
115 | errors: [ | |
116 | { messageId: "unexpectedConcat" } | |
117 | ] | |
118 | } | |
119 | ] | |
120 | }); |