|
1 | | -const looksLike = require('./looks-like'); |
| 1 | +const {looksLike, isModuleExports} = require('./looks-like'); |
2 | 2 | const t = require('@babel/types'); |
3 | | -const { default: traverse } = require('@babel/traverse'); |
| 3 | +const {default: traverse} = require('@babel/traverse'); |
4 | 4 | const parser = require('@babel/parser'); |
5 | 5 | const fs = require('fs'); |
6 | | -const proxyCode = fs.readFileSync(`${__dirname}/proxy.js`, 'utf8'); |
7 | 6 |
|
| 7 | +const proxyCode = fs.readFileSync(`${__dirname}/proxy.js`, 'utf8'); |
8 | 8 | const proxyAST = parser.parse(proxyCode); |
| 9 | +const proxyBodyAST = proxyAST.program.body.filter(node => !isModuleExports(node)); |
9 | 10 |
|
10 | 11 | /** |
11 | 12 | * The "Map" of if the "./proxy.js" code should be injected to the top of the Program |
12 | 13 | */ |
13 | 14 | const Programs = new WeakMap(); |
14 | 15 |
|
15 | 16 | module.exports = () => { |
16 | | - return { |
17 | | - name: 'willMutate', |
18 | | - visitor: { |
19 | | - /** |
20 | | - * We need to traverse the "Program" so we can know if we need to inject |
21 | | - * the Proxy handler or not. |
22 | | - */ |
23 | | - Program: { |
24 | | - enter(programPath) { |
25 | | - traverse(programPath.node, { |
26 | | - noScope: true, |
27 | | - enter(path) { |
28 | | - /** |
29 | | - * Look for all $shouldNotMutate functions |
30 | | - */ |
31 | | - if (path.node.type === 'ExpressionStatement') { |
32 | | - const isFunc = looksLike(path, { |
33 | | - node: { |
34 | | - type: 'ExpressionStatement', |
35 | | - expression: { |
36 | | - callee: { |
37 | | - name: '$shouldNotMutate', |
38 | | - }, |
39 | | - }, |
40 | | - }, |
41 | | - }); |
| 17 | + return { |
| 18 | + name: 'willMutate', |
| 19 | + visitor: { |
| 20 | + /** |
| 21 | + * We need to traverse the "Program" so we can know if we need to inject |
| 22 | + * the Proxy handler or not. |
| 23 | + */ |
| 24 | + Program: { |
| 25 | + enter(programPath) { |
| 26 | + traverse(programPath.node, { |
| 27 | + noScope: true, |
| 28 | + enter(path) { |
| 29 | + /** |
| 30 | + * Look for all $shouldNotMutate functions |
| 31 | + */ |
| 32 | + if (path.node.type === 'ExpressionStatement') { |
| 33 | + const isFunc = looksLike(path, { |
| 34 | + node: { |
| 35 | + type: 'ExpressionStatement', |
| 36 | + expression: { |
| 37 | + callee: { |
| 38 | + name: '$shouldNotMutate', |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + if (!isFunc) return; |
42 | 45 |
|
43 | | - if (!isFunc) return; |
| 46 | + const notMutateArgs = path.node.expression.arguments; |
44 | 47 |
|
45 | | - const notMutateArgs = path.node.expression.arguments; |
46 | | - |
47 | | - let inBodyArgsToProxy = []; |
48 | | - if (notMutateArgs.length) { |
49 | | - // We expect the first item in the array to be an array of strings |
50 | | - inBodyArgsToProxy = notMutateArgs[0].elements.map(node => node.value) |
51 | | - } |
| 48 | + let inBodyArgsToProxy = []; |
| 49 | + if (notMutateArgs.length) { |
| 50 | + // We expect the first item in the array to be an array of strings |
| 51 | + inBodyArgsToProxy = notMutateArgs[0].elements.map(node => node.value) |
| 52 | + } |
52 | 53 |
|
53 | | - debugger; |
| 54 | + debugger; |
54 | 55 |
|
55 | | - Programs.set(programPath.node, true); |
| 56 | + Programs.set(programPath.node, true); |
56 | 57 |
|
57 | | - // Treat the function as a "decorator" for a function. AKA get the function immediately |
58 | | - // After the function itself |
59 | | - const functionNodePath = path.getSibling(path.key + 1); |
| 58 | + // Treat the function as a "decorator" for a function. AKA get the function immediately |
| 59 | + // After the function itself |
| 60 | + const functionNodePath = path.getSibling(path.key + 1); |
60 | 61 |
|
61 | | - // Traverse the path to get the "BlockStatement" so we can mutate the code on it's own |
62 | | - traverse(functionNodePath.node, { |
63 | | - noScope: true, |
64 | | - enter(path) { |
65 | | - if (path.node.type === 'BlockStatement') { |
66 | | - console.log('WELCOME TO THE FUNCTION BODY OF THE $shouldNotMutate "Decorator'); |
67 | | - // debugger; |
68 | | - } |
69 | | - }, |
70 | | - }); |
71 | | - } |
72 | | - }, |
73 | | - }); |
74 | | - }, |
75 | | - exit(programPath) { |
76 | | - const val = Programs.get(programPath.node); |
77 | | - /** |
78 | | - * If the Program has any utilization of the $shouldNotMutate, then we'll inject the code from the |
79 | | - * proxy.js file to then be able to use that function in the AST |
80 | | - */ |
81 | | - if (val) { |
82 | | - programPath.node.body = [...proxyAST.program.body, ...programPath.node.body]; |
83 | | - } |
84 | | - }, |
85 | | - }, |
86 | | - }, |
87 | | - }; |
| 62 | + // Traverse the path to get the "BlockStatement" so we can mutate the code on it's own |
| 63 | + traverse(functionNodePath.node, { |
| 64 | + noScope: true, |
| 65 | + enter(path) { |
| 66 | + if (path.node.type === 'BlockStatement') { |
| 67 | + console.log('WELCOME TO THE FUNCTION BODY OF THE $shouldNotMutate "Decorator'); |
| 68 | + // debugger; |
| 69 | + } |
| 70 | + }, |
| 71 | + }); |
| 72 | + } |
| 73 | + }, |
| 74 | + }); |
| 75 | + }, |
| 76 | + exit(programPath) { |
| 77 | + const val = Programs.get(programPath.node); |
| 78 | + /** |
| 79 | + * If the Program has any utilization of the $shouldNotMutate, then we'll inject the code from the |
| 80 | + * proxy.js file to then be able to use that function in the AST |
| 81 | + */ |
| 82 | + if (val) { |
| 83 | + programPath.node.body = [...proxyBodyAST, ...programPath.node.body]; |
| 84 | + } |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }; |
88 | 89 | }; |
0 commit comments