Skip to content

Commit dac339c

Browse files
committed
Remove the module.exports from the proxy code
1 parent 5b4397c commit dac339c

File tree

2 files changed

+88
-70
lines changed

2 files changed

+88
-70
lines changed

plugin/index.js

Lines changed: 70 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,89 @@
1-
const looksLike = require('./looks-like');
1+
const {looksLike, isModuleExports} = require('./looks-like');
22
const t = require('@babel/types');
3-
const { default: traverse } = require('@babel/traverse');
3+
const {default: traverse} = require('@babel/traverse');
44
const parser = require('@babel/parser');
55
const fs = require('fs');
6-
const proxyCode = fs.readFileSync(`${__dirname}/proxy.js`, 'utf8');
76

7+
const proxyCode = fs.readFileSync(`${__dirname}/proxy.js`, 'utf8');
88
const proxyAST = parser.parse(proxyCode);
9+
const proxyBodyAST = proxyAST.program.body.filter(node => !isModuleExports(node));
910

1011
/**
1112
* The "Map" of if the "./proxy.js" code should be injected to the top of the Program
1213
*/
1314
const Programs = new WeakMap();
1415

1516
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;
4245

43-
if (!isFunc) return;
46+
const notMutateArgs = path.node.expression.arguments;
4447

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+
}
5253

53-
debugger;
54+
debugger;
5455

55-
Programs.set(programPath.node, true);
56+
Programs.set(programPath.node, true);
5657

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);
6061

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+
};
8889
};

plugin/looks-like.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,21 @@ const looksLike = (a, b) => {
1616

1717
const isPrimitive = (val) => val == null || /^[sbn]/.test(typeof val);
1818

19-
module.exports = looksLike;
19+
const isModuleExports = node => looksLike(node, {
20+
type: 'ExpressionStatement',
21+
expression: {
22+
left: {
23+
object: {
24+
name: 'module'
25+
},
26+
property: {
27+
name: "exports"
28+
}
29+
}
30+
}
31+
})
32+
33+
module.exports = {
34+
looksLike,
35+
isModuleExports
36+
};

0 commit comments

Comments
 (0)