-
Notifications
You must be signed in to change notification settings - Fork 1
/
codemod:doc-scripts-header-replace-querySelectorAll.js
50 lines (41 loc) · 1.31 KB
/
codemod:doc-scripts-header-replace-querySelectorAll.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
import find from '../lib/code/find.js';
import replace from '../lib/code/replace.js';
export const description =
'Replace querySelectorAll(...)[0] by querySelector(...) in docs scripts.';
export const commit = {
type: 'docs',
subject: 'Simplify scripts.',
};
const paths = ['doc/scripts/header.js'];
const filter = (node, {is, n}) => {
if (!is(node, n.MemberExpression)) return false;
const {object, property} = node;
if (!is(object, n.CallExpression)) return false;
if (!is(property, n.Literal)) return false;
if (property.value !== 0) return false;
const callee = object.callee;
return (
is(callee, n.MemberExpression) &&
callee.object.name === 'document' &&
callee.property.name === 'querySelectorAll'
);
};
const map = ({object: {callee, arguments: args}}, {b}) => {
return b.callExpression(
b.memberExpression(callee.object, b.identifier('querySelector')),
args,
);
};
export async function postcondition({read, assert}) {
const found = await find([{filter}], paths, {read});
assert(!found);
}
export async function precondition({read, assert}) {
const found = await find([{filter}], paths, {read});
assert(found);
}
export async function apply({read, write, fixSources}) {
await replace([{filter, map}], paths, {read, write});
await fixSources();
}
export const dependencies = ['sources:lint-setup'];