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