-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
empty-brace-spaces.js
84 lines (80 loc) · 2.03 KB
/
empty-brace-spaces.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {outdent} from 'outdent';
import {test} from './utils/test';
const SPACES_PLACEHOLDER = '/* */';
const cases = [
'{/* */}',
'function foo(){/* */}',
'if(foo) {/* */}',
'if(foo) {} else if (bar) {/* */}',
'if(foo) {} else {/* */}',
'for(;;){/* */}',
'for(foo in bar){/* */}',
'for(foo of bar){/* */}',
'switch (foo) {case bar: {/* */}}',
'switch (foo) {default: {/* */}}',
'try {/* */} catch(foo){}',
'try {} catch(foo){/* */}',
'try {} catch(foo){} finally {/* */}',
'do {/* */} while (foo)',
'while (foo){/* */}',
'foo = () => {/* */}',
'foo = function (){/* */}',
'function foo(){/* */}',
'foo = {/* */}',
'class Foo {bar() {/* */}}',
'foo = class {bar() {/* */}}'
];
const classBodyCases = [
'class Foo {/* */}',
'foo = class {/* */}',
];
const allCases = [...cases, ...classBodyCases];
const casesIgnored = [
'switch (foo) {/* */}',
'const {/* */} = foo',
'import {/* */} from "foo"'
];
test({
valid: [
// We don't check these cases
...casesIgnored.map(code => code.replace(SPACES_PLACEHOLDER, ' ')),
// No space
...allCases.map(code => code.replace(SPACES_PLACEHOLDER, '')),
// Comments
...allCases.map(code => code.replace(SPACES_PLACEHOLDER, '/* comment */')),
...allCases.map(code => code.replace(SPACES_PLACEHOLDER, '\n\t// comment \n')),
// Not empty
...cases.map(code => code.replace(SPACES_PLACEHOLDER, 'unicorn')),
...classBodyCases.map(code => code.replace(SPACES_PLACEHOLDER, 'bar() {}')),
// `with`
{
code: 'with (foo) {}',
parserOptions: {ecmaVersion: 5, sourceType: 'script'}
}
],
invalid: [
...[' ', '\t', ' \t \t ', '\n\n', '\r\n'].map(space =>
allCases.map(code => ({
code: code.replace(SPACES_PLACEHOLDER, space),
output: code.replace(SPACES_PLACEHOLDER, ''),
errors: 1
}))
).flat(),
// `with`
{
code: 'with (foo) { }',
output: 'with (foo) {}',
errors: 1,
parserOptions: {ecmaVersion: 5, sourceType: 'script'}
}
]
});
test.visualize([
outdent`
try {
foo();
} catch (error) {
\u0020\u0020\u0020\u0020\u0020\u0020\u0020
}
`
]);