-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
69 lines (57 loc) · 2.35 KB
/
index.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
// matches any hook-like (the default)
const isHook = /^use[A-Z]/;
// matches only built-in hooks provided by React et al
const isBuiltInHook = /^use(?:Callback|Context|DebugValue|Effect|ImperativeHandle|LayoutEffect|Memo|Reducer|Ref|State)$/;
/**
* @typedef Options
* @property {boolean|string} [lib=false] If specified, only hook functions imported from this module are processed. Pass `true` to include 'react' and 'preact/hooks'.
* @property {boolean} [onlyBuiltIns=false] Only optimize known built-in hooks from react/preact.
*/
/**
* @param {import('@babel/core')} api
* @param {Options} [options]
* @returns {import('@babel/core').PluginObj}
*/
module.exports = ({ types: t }, /** @type {Options} */ options = {}) => {
const libs = options.lib && (options.lib === true ? ['react', 'preact/hooks'] : [options.lib]);
const onlyBuiltIns = options.onlyBuiltIns || false;
/** @type {import('@babel/core').Visitor} */
const visitor = {
CallExpression(path) {
// skip function calls that are not the init of a variable declaration:
if (!t.isVariableDeclarator(path.parent)) return;
// skip function calls where the return value is not Array-destructured:
if (!t.isArrayPattern(path.parent.id)) return;
// name of the (hook) function being called:
if (!t.isIdentifier(path.node.callee)) return;
const hookName = path.node.callee.name;
if (libs) {
const binding = path.scope.getBinding(hookName);
// not an import
if (!binding || binding.kind !== 'module' || !t.isImportDeclaration(binding.path.parent)) return;
const specifier = binding.path.parent.source.value;
// not a match
if (!libs.includes(specifier)) return;
}
// only match function calls with names that look like a hook
if (!(onlyBuiltIns ? isBuiltInHook : isHook).test(hookName)) return;
path.parent.id = t.objectPattern(
path.parent.id.elements.reduce((acc, element, i) => {
if (element) {
acc.push(t.objectProperty(t.numericLiteral(i), t.clone(element)));
}
return acc;
}, [])
);
}
};
return {
name: 'optimize-hook-destructuring',
visitor: {
// this is a workaround to run before preset-env destroys destructured assignments
Program(path) {
path.traverse(visitor);
}
}
};
};