/
prefer-node-append.js
128 lines (125 loc) · 2.85 KB
/
prefer-node-append.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import {outdent} from 'outdent';
import rule from '../rules/prefer-node-append';
const ruleTester = avaRuleTester(test, {
env: {
es6: true
}
});
const error = {
message: 'Prefer `Node#append()` over `Node#appendChild()`.'
};
ruleTester.run('prefer-node-append', rule, {
valid: [
'parent.append(child);',
'document.body.append(child, \'text\');',
'node.append()',
'node.append(null)'
],
invalid: [
{
code: 'node.appendChild(child);',
output: 'node.append(child);',
errors: [error]
},
{
code: 'document.body.appendChild(child);',
output: 'document.body.append(child);',
errors: [error]
},
{
code: 'node.appendChild()',
output: 'node.append()',
errors: [error]
},
{
code: 'node.appendChild(null)',
output: 'node.append(null)',
errors: [error]
},
{
code: outdent`
function foo() {
node.appendChild(null);
}
`,
output: outdent`
function foo() {
node.append(null);
}
`,
errors: [error]
},
{
code: 'const foo = node.appendChild(child);',
output: 'const foo = node.appendChild(child);',
errors: [error]
},
{
code: 'console.log(node.appendChild(child));',
output: 'console.log(node.appendChild(child));',
errors: [error]
},
{
code: 'node.appendChild(child).appendChild(grandchild);',
output: 'node.appendChild(child).append(grandchild);',
errors: [error, error]
},
{
code: 'node.appendChild(child) || "foo";',
output: 'node.appendChild(child) || "foo";',
errors: [error]
},
{
code: 'node.appendChild(child) + 0;',
output: 'node.appendChild(child) + 0;',
errors: [error]
},
{
code: 'node.appendChild(child) + 0;',
output: 'node.appendChild(child) + 0;',
errors: [error]
},
{
code: '+node.appendChild(child);',
output: '+node.appendChild(child);',
errors: [error]
},
{
code: 'node.appendChild(child) ? "foo" : "bar";',
output: 'node.appendChild(child) ? "foo" : "bar";',
errors: [error]
},
{
code: 'if (node.appendChild(child)) {}',
output: 'if (node.appendChild(child)) {}',
errors: [error]
},
{
code: 'const foo = [node.appendChild(child)]',
output: 'const foo = [node.appendChild(child)]',
errors: [error]
},
{
code: 'const foo = { bar: node.appendChild(child) }',
output: 'const foo = { bar: node.appendChild(child) }',
errors: [error]
},
{
code: 'function foo() { return node.appendChild(child); }',
output: 'function foo() { return node.appendChild(child); }',
errors: [error]
},
{
code: 'const foo = () => { return node.appendChild(child); }',
output: 'const foo = () => { return node.appendChild(child); }',
errors: [error]
},
{
code: 'foo(bar = node.appendChild(child))',
output: 'foo(bar = node.appendChild(child))',
errors: [error]
}
]
});