]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for no-unexpected-multiline rule. | |
3 | * @author Glen Mailer | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/no-unexpected-multiline"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | const ruleTester = new RuleTester(); | |
16 | ||
17 | ruleTester.run("no-unexpected-multiline", rule, { | |
18 | valid: [ | |
19 | "(x || y).aFunction()", | |
20 | "[a, b, c].forEach(doSomething)", | |
21 | "var a = b;\n(x || y).doSomething()", | |
22 | "var a = b\n;(x || y).doSomething()", | |
23 | "var a = b\nvoid (x || y).doSomething()", | |
24 | "var a = b;\n[1, 2, 3].forEach(console.log)", | |
25 | "var a = b\nvoid [1, 2, 3].forEach(console.log)", | |
26 | "\"abc\\\n(123)\"", | |
27 | "var a = (\n(123)\n)", | |
28 | "f(\n(x)\n)", | |
29 | "(\nfunction () {}\n)[1]", | |
30 | { | |
31 | code: "let x = function() {};\n `hello`", | |
32 | parserOptions: { ecmaVersion: 6 } | |
33 | }, | |
34 | { | |
35 | code: "let x = function() {}\nx `hello`", | |
36 | parserOptions: { ecmaVersion: 6 } | |
37 | }, | |
38 | { | |
39 | code: "String.raw `Hi\n${2+3}!`;", | |
40 | parserOptions: { ecmaVersion: 6 } | |
41 | }, | |
42 | { | |
43 | code: "x\n.y\nz `Valid Test Case`", | |
44 | parserOptions: { ecmaVersion: 6 } | |
45 | }, | |
56c4a2cb DC |
46 | { |
47 | code: "f(x\n)`Valid Test Case`", | |
48 | parserOptions: { ecmaVersion: 6 } | |
49 | }, | |
50 | { | |
51 | code: "x.\ny `Valid Test Case`", | |
52 | parserOptions: { ecmaVersion: 6 } | |
53 | }, | |
54 | { | |
55 | code: "(x\n)`Valid Test Case`", | |
56 | parserOptions: { ecmaVersion: 6 } | |
57 | }, | |
eb39fafa DC |
58 | ` |
59 | foo | |
60 | / bar /2 | |
61 | `, | |
62 | ` | |
63 | foo | |
64 | / bar / mgy | |
65 | `, | |
66 | ` | |
67 | foo | |
68 | / bar / | |
69 | gym | |
70 | `, | |
71 | ` | |
72 | foo | |
73 | / bar | |
74 | / ygm | |
75 | `, | |
76 | ` | |
77 | foo | |
78 | / bar /GYM | |
79 | `, | |
80 | ` | |
81 | foo | |
82 | / bar / baz | |
83 | `, | |
84 | "foo /bar/g", | |
85 | ` | |
86 | foo | |
87 | /denominator/ | |
88 | 2 | |
89 | `, | |
90 | ` | |
91 | foo | |
92 | / /abc/ | |
93 | `, | |
94 | ` | |
95 | 5 / (5 | |
96 | / 5) | |
97 | `, | |
98 | ||
99 | // https://github.com/eslint/eslint/issues/11650 | |
100 | { | |
101 | code: ` | |
102 | tag<generic>\` | |
103 | multiline | |
104 | \`; | |
105 | `, | |
106 | parser: require.resolve("../../fixtures/parsers/typescript-parsers/tagged-template-with-generic/tagged-template-with-generic-1") | |
107 | }, | |
108 | { | |
109 | code: ` | |
110 | tag< | |
111 | generic | |
112 | >\` | |
113 | multiline | |
114 | \`; | |
115 | `, | |
116 | parser: require.resolve("../../fixtures/parsers/typescript-parsers/tagged-template-with-generic/tagged-template-with-generic-2") | |
117 | }, | |
118 | { | |
119 | code: ` | |
120 | tag< | |
121 | generic | |
122 | >\`multiline\`; | |
123 | `, | |
124 | parser: require.resolve("../../fixtures/parsers/typescript-parsers/tagged-template-with-generic/tagged-template-with-generic-3") | |
125 | } | |
126 | ], | |
127 | invalid: [ | |
128 | { | |
129 | code: "var a = b\n(x || y).doSomething()", | |
130 | errors: [{ | |
131 | messageId: "function", | |
132 | line: 2, | |
56c4a2cb DC |
133 | column: 1, |
134 | endLine: 2, | |
135 | endColumn: 2 | |
eb39fafa DC |
136 | }] |
137 | }, | |
138 | { | |
139 | code: "var a = (a || b)\n(x || y).doSomething()", | |
140 | errors: [{ | |
141 | line: 2, | |
142 | column: 1, | |
56c4a2cb DC |
143 | endLine: 2, |
144 | endColumn: 2, | |
eb39fafa DC |
145 | messageId: "function" |
146 | }] | |
147 | }, | |
148 | { | |
149 | code: "var a = (a || b)\n(x).doSomething()", | |
150 | errors: [{ | |
151 | line: 2, | |
152 | column: 1, | |
56c4a2cb DC |
153 | endLine: 2, |
154 | endColumn: 2, | |
eb39fafa DC |
155 | messageId: "function" |
156 | }] | |
157 | }, | |
158 | { | |
159 | code: "var a = b\n[a, b, c].forEach(doSomething)", | |
160 | errors: [{ | |
161 | line: 2, | |
162 | column: 1, | |
56c4a2cb DC |
163 | endLine: 2, |
164 | endColumn: 2, | |
eb39fafa DC |
165 | messageId: "property" |
166 | }] | |
167 | }, | |
168 | { | |
169 | code: "var a = b\n (x || y).doSomething()", | |
170 | errors: [{ | |
171 | line: 2, | |
172 | column: 5, | |
56c4a2cb DC |
173 | endLine: 2, |
174 | endColumn: 6, | |
eb39fafa DC |
175 | messageId: "function" |
176 | }] | |
177 | }, | |
178 | { | |
179 | code: "var a = b\n [a, b, c].forEach(doSomething)", | |
180 | errors: [{ | |
181 | line: 2, | |
182 | column: 3, | |
56c4a2cb DC |
183 | endLine: 2, |
184 | endColumn: 4, | |
eb39fafa DC |
185 | messageId: "property" |
186 | }] | |
187 | }, | |
188 | { | |
189 | code: "let x = function() {}\n `hello`", | |
190 | parserOptions: { ecmaVersion: 6 }, | |
191 | errors: [{ | |
56c4a2cb DC |
192 | line: 2, |
193 | column: 2, | |
194 | endLine: 2, | |
195 | endColumn: 3, | |
eb39fafa DC |
196 | messageId: "taggedTemplate" |
197 | }] | |
198 | }, | |
199 | { | |
200 | code: "let x = function() {}\nx\n`hello`", | |
201 | parserOptions: { ecmaVersion: 6 }, | |
202 | errors: [{ | |
56c4a2cb | 203 | line: 3, |
eb39fafa | 204 | column: 1, |
56c4a2cb DC |
205 | endLine: 3, |
206 | endColumn: 2, | |
eb39fafa DC |
207 | messageId: "taggedTemplate" |
208 | }] | |
209 | }, | |
210 | { | |
211 | code: "x\n.y\nz\n`Invalid Test Case`", | |
212 | parserOptions: { ecmaVersion: 6 }, | |
213 | errors: [{ | |
56c4a2cb | 214 | line: 4, |
eb39fafa | 215 | column: 1, |
56c4a2cb DC |
216 | endLine: 4, |
217 | endColumn: 2, | |
eb39fafa DC |
218 | messageId: "taggedTemplate" |
219 | }] | |
220 | }, | |
221 | { | |
222 | code: ` | |
223 | foo | |
224 | / bar /gym | |
225 | `, | |
226 | errors: [{ | |
227 | line: 3, | |
228 | column: 17, | |
56c4a2cb DC |
229 | endLine: 3, |
230 | endColumn: 18, | |
eb39fafa DC |
231 | messageId: "division" |
232 | }] | |
233 | }, | |
234 | { | |
235 | code: ` | |
236 | foo | |
237 | / bar /g | |
238 | `, | |
239 | errors: [{ | |
240 | line: 3, | |
241 | column: 17, | |
56c4a2cb DC |
242 | endLine: 3, |
243 | endColumn: 18, | |
eb39fafa DC |
244 | messageId: "division" |
245 | }] | |
246 | }, | |
247 | { | |
248 | code: ` | |
249 | foo | |
250 | / bar /g.test(baz) | |
251 | `, | |
252 | errors: [{ | |
253 | line: 3, | |
254 | column: 17, | |
56c4a2cb DC |
255 | endLine: 3, |
256 | endColumn: 18, | |
eb39fafa DC |
257 | messageId: "division" |
258 | }] | |
259 | }, | |
260 | { | |
261 | code: ` | |
262 | foo | |
263 | /bar/gimuygimuygimuy.test(baz) | |
264 | `, | |
265 | errors: [{ | |
266 | line: 3, | |
267 | column: 17, | |
56c4a2cb DC |
268 | endLine: 3, |
269 | endColumn: 18, | |
eb39fafa DC |
270 | messageId: "division" |
271 | }] | |
272 | }, | |
273 | { | |
274 | code: ` | |
275 | foo | |
276 | /bar/s.test(baz) | |
277 | `, | |
278 | errors: [{ | |
279 | line: 3, | |
280 | column: 17, | |
56c4a2cb DC |
281 | endLine: 3, |
282 | endColumn: 18, | |
eb39fafa DC |
283 | messageId: "division" |
284 | }] | |
285 | }, | |
286 | ||
287 | // https://github.com/eslint/eslint/issues/11650 | |
288 | { | |
56c4a2cb DC |
289 | code: [ |
290 | "const x = aaaa<", | |
291 | " test", | |
292 | ">/*", | |
293 | "test", | |
294 | "*/`foo`" | |
295 | ].join("\n"), | |
eb39fafa DC |
296 | parser: require.resolve("../../fixtures/parsers/typescript-parsers/tagged-template-with-generic/tagged-template-with-generic-and-comment"), |
297 | errors: [ | |
298 | { | |
56c4a2cb DC |
299 | line: 5, |
300 | column: 3, | |
301 | endLine: 5, | |
302 | endColumn: 4, | |
eb39fafa DC |
303 | messageId: "taggedTemplate" |
304 | } | |
305 | ] | |
306 | } | |
307 | ] | |
308 | }); |