-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
empty-brace-spaces.js
88 lines (84 loc) · 1.93 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
85
86
87
88
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 ignoredCases = [
'switch (foo) {/* */}',
'const {/* */} = foo',
'import {/* */} from "foo"'
];
test({
valid: [
...[
'',
'/* comment */',
'\n\t// comment \n'
].map(body => allCases.map(code => code.replace(SPACES_PLACEHOLDER, body))).flat(),
// 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'}
},
// We don't check these cases
...ignoredCases.map(code => code.replace(SPACES_PLACEHOLDER, ' '))
],
invalid: [
...[
' ',
'\t',
' \t \t ',
'\n\n',
'\r\n'
].map(spaces => allCases.map(code => ({
code: code.replace(SPACES_PLACEHOLDER, spaces),
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
}
`
]);