]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Disallow mixed spaces and tabs for indentation | |
3 | * @author Jary Niebur | |
4 | */ | |
5 | "use strict"; | |
6 | ||
7 | //------------------------------------------------------------------------------ | |
8 | // Requirements | |
9 | //------------------------------------------------------------------------------ | |
10 | ||
11 | const rule = require("../../../lib/rules/no-mixed-spaces-and-tabs"), | |
12 | { RuleTester } = require("../../../lib/rule-tester"); | |
13 | ||
14 | //------------------------------------------------------------------------------ | |
15 | // Tests | |
16 | //------------------------------------------------------------------------------ | |
17 | ||
18 | const ruleTester = new RuleTester(); | |
19 | ||
20 | ruleTester.run("no-mixed-spaces-and-tabs", rule, { | |
21 | ||
22 | valid: [ | |
d3726936 TL |
23 | "foo", |
24 | "foo \t", | |
25 | "foo\t ", | |
eb39fafa | 26 | "\tvar x = 5;", |
d3726936 TL |
27 | "\t\tvar x = 5;", |
28 | " var x = 5;", | |
eb39fafa | 29 | " var x = 5;", |
d3726936 TL |
30 | " foo\t", |
31 | "\tfoo ", | |
eb39fafa DC |
32 | "\t/*\n\t * Hello\n\t */", |
33 | "// foo\n\t/**\n\t * Hello\n\t */", | |
34 | "/*\n\n \t \n*/", | |
35 | "/*\t */ //", | |
36 | "/*\n \t*/ //", | |
37 | "/*\n\t *//*\n \t*/", | |
38 | "// \t", | |
39 | "/*\n*/\t ", | |
40 | "/* \t\n\t \n \t\n\t */ \t", | |
41 | { | |
42 | code: "\tvar x = 5,\n\t y = 2;", | |
43 | options: [true] | |
44 | }, | |
45 | { | |
46 | code: "/*\n\t */`\n\t `;", | |
47 | env: { es6: true } | |
48 | }, | |
49 | { | |
50 | code: "/*\n\t */var a = `\n\t `, b = `\n\t `/*\t \n\t \n*/;", | |
51 | env: { es6: true } | |
52 | }, | |
53 | { | |
54 | code: "/*\t `template inside comment` */", | |
55 | env: { es6: true } | |
56 | }, | |
57 | { | |
58 | code: "var foo = `\t /* comment inside template\t */`;", | |
59 | env: { es6: true } | |
60 | }, | |
61 | { | |
62 | code: "`\n\t `;", | |
63 | env: { es6: true } | |
64 | }, | |
65 | { | |
66 | code: "`\n\t \n`;", | |
67 | env: { es6: true } | |
68 | }, | |
69 | { | |
70 | code: "`\t `;", | |
71 | env: { es6: true } | |
72 | }, | |
73 | { | |
74 | code: "const foo = `${console}\n\t foo`;", | |
75 | env: { es6: true } | |
76 | }, | |
77 | { | |
78 | code: "`\t `;` \t`", | |
79 | env: { es6: true } | |
80 | }, | |
81 | { | |
82 | code: "`foo${ 5 }\t `;", | |
83 | env: { es6: true } | |
84 | }, | |
85 | "' \t\\\n\t multiline string';", | |
86 | "'\t \\\n \tmultiline string';", | |
87 | { | |
88 | code: "\tvar x = 5,\n\t y = 2;", | |
89 | options: ["smart-tabs"] | |
d3726936 TL |
90 | }, |
91 | { | |
92 | code: "\t\t\t foo", | |
93 | options: ["smart-tabs"] | |
94 | }, | |
95 | { | |
96 | code: "foo", | |
97 | options: ["smart-tabs"] | |
98 | }, | |
99 | { | |
100 | code: "foo \t", | |
101 | options: ["smart-tabs"] | |
102 | }, | |
103 | { | |
104 | code: "foo\t ", | |
105 | options: ["smart-tabs"] | |
106 | }, | |
107 | { | |
108 | code: "\tfoo \t", | |
109 | options: ["smart-tabs"] | |
110 | }, | |
111 | { | |
112 | code: "\tvar x = 5;", | |
113 | options: ["smart-tabs"] | |
114 | }, | |
115 | { | |
116 | code: "\t\tvar x = 5;", | |
117 | options: ["smart-tabs"] | |
118 | }, | |
119 | { | |
120 | code: " var x = 5;", | |
121 | options: ["smart-tabs"] | |
122 | }, | |
123 | { | |
124 | code: " var x = 5;", | |
125 | options: ["smart-tabs"] | |
126 | }, | |
127 | { | |
128 | code: " foo\t", | |
129 | options: ["smart-tabs"] | |
130 | }, | |
131 | { | |
132 | code: "\tfoo ", | |
133 | options: ["smart-tabs"] | |
eb39fafa DC |
134 | } |
135 | ], | |
136 | ||
137 | invalid: [ | |
138 | { | |
139 | code: "function add(x, y) {\n\t return x + y;\n}", | |
140 | errors: [ | |
141 | { | |
142 | messageId: "mixedSpacesAndTabs", | |
143 | type: "Program", | |
d3726936 TL |
144 | line: 2, |
145 | column: 1, | |
146 | endLine: 2, | |
147 | endColumn: 3 | |
eb39fafa DC |
148 | } |
149 | ] | |
150 | }, | |
151 | { | |
152 | code: "\t ;\n/*\n\t * Hello\n\t */", | |
153 | errors: [ | |
154 | { | |
155 | messageId: "mixedSpacesAndTabs", | |
156 | type: "Program", | |
d3726936 TL |
157 | line: 1, |
158 | column: 1, | |
159 | endLine: 1, | |
160 | endColumn: 3 | |
eb39fafa DC |
161 | } |
162 | ] | |
163 | }, | |
164 | { | |
165 | code: " \t/* comment */", | |
166 | errors: [ | |
167 | { | |
168 | messageId: "mixedSpacesAndTabs", | |
169 | type: "Program", | |
d3726936 TL |
170 | line: 1, |
171 | column: 1, | |
172 | endLine: 1, | |
173 | endColumn: 3 | |
eb39fafa DC |
174 | } |
175 | ] | |
176 | }, | |
177 | { | |
178 | code: "\t // comment", | |
179 | errors: [ | |
180 | { | |
181 | messageId: "mixedSpacesAndTabs", | |
182 | type: "Program", | |
d3726936 TL |
183 | line: 1, |
184 | column: 1, | |
185 | endLine: 1, | |
186 | endColumn: 3 | |
eb39fafa DC |
187 | } |
188 | ] | |
189 | }, | |
190 | { | |
191 | code: "\t var a /* comment */ = 1;", | |
192 | errors: [ | |
193 | { | |
194 | messageId: "mixedSpacesAndTabs", | |
195 | type: "Program", | |
d3726936 TL |
196 | line: 1, |
197 | column: 1, | |
198 | endLine: 1, | |
199 | endColumn: 3 | |
eb39fafa DC |
200 | } |
201 | ] | |
202 | }, | |
203 | { | |
204 | code: " \tvar b = 1; // comment", | |
205 | errors: [ | |
206 | { | |
207 | messageId: "mixedSpacesAndTabs", | |
208 | type: "Program", | |
d3726936 TL |
209 | line: 1, |
210 | column: 1, | |
211 | endLine: 1, | |
212 | endColumn: 3 | |
eb39fafa DC |
213 | } |
214 | ] | |
215 | }, | |
216 | { | |
217 | code: "/**/\n \t/*\n \t*/", | |
218 | errors: [ | |
219 | { | |
220 | messageId: "mixedSpacesAndTabs", | |
221 | type: "Program", | |
d3726936 TL |
222 | line: 2, |
223 | column: 1, | |
224 | endLine: 2, | |
225 | endColumn: 3 | |
eb39fafa DC |
226 | } |
227 | ] | |
228 | }, | |
229 | { | |
230 | code: "\t var x = 5, y = 2, z = 5;\n\n\t \tvar j =\t x + y;\nz *= j;", | |
231 | errors: [ | |
232 | { | |
233 | messageId: "mixedSpacesAndTabs", | |
234 | type: "Program", | |
d3726936 TL |
235 | line: 1, |
236 | column: 1, | |
237 | endLine: 1, | |
238 | endColumn: 3 | |
eb39fafa DC |
239 | }, |
240 | { | |
241 | messageId: "mixedSpacesAndTabs", | |
242 | type: "Program", | |
d3726936 TL |
243 | line: 3, |
244 | column: 1, | |
245 | endLine: 3, | |
246 | endColumn: 3 | |
eb39fafa DC |
247 | } |
248 | ] | |
249 | }, | |
250 | { | |
251 | code: "\tvar x = 5,\n \t y = 2;", | |
252 | options: [true], | |
253 | errors: [ | |
254 | { | |
255 | messageId: "mixedSpacesAndTabs", | |
256 | type: "Program", | |
d3726936 TL |
257 | line: 2, |
258 | column: 2, | |
259 | endLine: 2, | |
260 | endColumn: 4 | |
eb39fafa DC |
261 | } |
262 | ] | |
263 | }, | |
264 | { | |
265 | code: "\tvar x = 5,\n \t y = 2;", | |
266 | options: ["smart-tabs"], | |
267 | errors: [ | |
268 | { | |
269 | messageId: "mixedSpacesAndTabs", | |
270 | type: "Program", | |
d3726936 TL |
271 | line: 2, |
272 | column: 2, | |
273 | endLine: 2, | |
274 | endColumn: 4 | |
eb39fafa DC |
275 | } |
276 | ] | |
277 | }, | |
278 | { | |
279 | code: "`foo${\n \t 5 }bar`;", | |
280 | options: ["smart-tabs"], | |
281 | env: { es6: true }, | |
282 | errors: [ | |
283 | { | |
284 | messageId: "mixedSpacesAndTabs", | |
285 | type: "Program", | |
286 | line: 2, | |
d3726936 TL |
287 | column: 1, |
288 | endLine: 2, | |
289 | endColumn: 3 | |
eb39fafa DC |
290 | } |
291 | ] | |
292 | }, | |
293 | { | |
294 | code: "`foo${\n\t 5 }bar`;", | |
295 | env: { es6: true }, | |
296 | errors: [ | |
297 | { | |
298 | messageId: "mixedSpacesAndTabs", | |
299 | type: "Program", | |
300 | line: 2, | |
d3726936 TL |
301 | column: 1, |
302 | endLine: 2, | |
303 | endColumn: 3 | |
eb39fafa DC |
304 | } |
305 | ] | |
306 | }, | |
307 | { | |
308 | code: " \t'';", | |
309 | errors: [ | |
310 | { | |
311 | messageId: "mixedSpacesAndTabs", | |
312 | type: "Program", | |
d3726936 TL |
313 | line: 1, |
314 | column: 2, | |
315 | endLine: 1, | |
316 | endColumn: 4 | |
eb39fafa DC |
317 | } |
318 | ] | |
319 | }, | |
320 | { | |
321 | code: "''\n\t ", | |
322 | errors: [ | |
323 | { | |
324 | messageId: "mixedSpacesAndTabs", | |
325 | type: "Program", | |
d3726936 TL |
326 | line: 2, |
327 | column: 1, | |
328 | endLine: 2, | |
329 | endColumn: 3 | |
330 | } | |
331 | ] | |
332 | }, | |
333 | { | |
334 | code: " \tfoo", | |
335 | errors: [ | |
336 | { | |
337 | messageId: "mixedSpacesAndTabs", | |
338 | type: "Program", | |
339 | line: 1, | |
340 | column: 3, | |
341 | endLine: 1, | |
342 | endColumn: 5 | |
343 | } | |
344 | ] | |
345 | }, | |
346 | { | |
347 | code: "\t\t\t foo", | |
348 | errors: [ | |
349 | { | |
350 | messageId: "mixedSpacesAndTabs", | |
351 | type: "Program", | |
352 | line: 1, | |
353 | column: 3, | |
354 | endLine: 1, | |
355 | endColumn: 5 | |
356 | } | |
357 | ] | |
358 | }, | |
359 | { | |
360 | code: "\t \tfoo", | |
361 | options: ["smart-tabs"], | |
362 | errors: [ | |
363 | { | |
364 | messageId: "mixedSpacesAndTabs", | |
365 | type: "Program", | |
366 | line: 1, | |
367 | column: 2, | |
368 | endLine: 1, | |
369 | endColumn: 4 | |
370 | } | |
371 | ] | |
372 | }, | |
373 | { | |
374 | code: "\t\t\t \tfoo", | |
375 | options: ["smart-tabs"], | |
376 | errors: [ | |
377 | { | |
378 | messageId: "mixedSpacesAndTabs", | |
379 | type: "Program", | |
380 | line: 1, | |
381 | column: 6, | |
382 | endLine: 1, | |
383 | endColumn: 8 | |
eb39fafa DC |
384 | } |
385 | ] | |
386 | } | |
387 | ] | |
388 | }); |