-
-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathprefer-dom-node-remove.mjs
117 lines (109 loc) · 3.21 KB
/
prefer-dom-node-remove.mjs
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
import outdent from 'outdent';
import {getTester} from './utils/test.mjs';
import notDomNodeTypes from './utils/not-dom-node-types.mjs';
const {test} = getTester(import.meta);
test.snapshot({
valid: [
'foo.remove()',
'this.remove()',
'remove()',
'foo.parentNode.removeChild(\'bar\')',
'parentNode.removeChild(undefined)',
// Not `CallExpression`
'new parentNode.removeChild(bar);',
// Not `MemberExpression`
'removeChild(foo);',
// `callee.property` is not a `Identifier`
'parentNode[\'removeChild\'](bar);',
// Computed
'parentNode[removeChild](bar);',
// Not `removeChild`
'parentNode.foo(bar);',
// More or less argument(s)
'parentNode.removeChild(bar, extra);',
'parentNode.removeChild();',
'parentNode.removeChild(...argumentsArray)',
// Optional call
'parentNode.removeChild?.(foo)',
// `callee.object` is not a DOM Node,
...notDomNodeTypes.map(data => `(${data}).removeChild(foo)`),
// First argument is not a DOM Node,
...notDomNodeTypes.map(data => `foo.removeChild(${data})`),
],
invalid: [
// Auto fix
'parentNode.removeChild(foo)',
'parentNode.removeChild(this)',
'parentNode.removeChild(some.node)',
'parentNode.removeChild(getChild())',
'parentNode.removeChild(lib.getChild())',
'parentNode.removeChild((() => childNode)())',
// Need parenthesized
outdent`
async function foo () {
parentNode.removeChild(
await getChild()
);
}
`,
outdent`
async function foo () {
parentNode.removeChild(
(await getChild())
);
}
`,
'parentNode.removeChild((0, child))',
'parentNode.removeChild( ( (new Image)) )',
'parentNode.removeChild( new Audio )',
// Need semicolon
outdent`
const array = []
parentNode.removeChild([a, b, c].reduce(child => child, child))
`,
outdent`
async function foo () {
const array = []
parentNode.removeChild(
await getChild()
);
}
`,
outdent`
async function foo () {
const array = []
parentNode.removeChild(
(0, childNode)
);
}
`,
// Value of `parentNode.removeChild` call is use
'if (parentNode.removeChild(foo)) {}',
'var removed = parentNode.removeChild(child);',
'const foo = parentNode.removeChild(child);',
'foo.bar(parentNode.removeChild(child));',
'parentNode.removeChild(child) || "foo";',
'parentNode.removeChild(child) + 0;',
'+parentNode.removeChild(child);',
'parentNode.removeChild(child) ? "foo" : "bar";',
'if (parentNode.removeChild(child)) {}',
'const foo = [parentNode.removeChild(child)]',
'const foo = { bar: parentNode.removeChild(child) }',
'function foo() { return parentNode.removeChild(child); }',
'const foo = () => { return parentElement.removeChild(child); }',
'foo(bar = parentNode.removeChild(child))',
// `parentNode` has side effect
'foo().removeChild(child)',
'foo[doSomething()].removeChild(child)',
// Optional parent
'parentNode?.removeChild(foo)',
'foo?.parentNode.removeChild(foo)',
'foo.parentNode?.removeChild(foo)',
'foo?.parentNode?.removeChild(foo)',
'foo.bar?.parentNode.removeChild(foo.bar)',
'a.b?.c.parentNode.removeChild(foo)',
'a[b?.c].parentNode.removeChild(foo)',
// The suggestions are bad, since they will break code
'a?.b.parentNode.removeChild(a.b)',
],
});