-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove the module.exports from the proxy code
- Loading branch information
1 parent
5b4397c
commit dac339c
Showing
2 changed files
with
88 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,89 @@ | ||
const looksLike = require('./looks-like'); | ||
const {looksLike, isModuleExports} = require('./looks-like'); | ||
const t = require('@babel/types'); | ||
const { default: traverse } = require('@babel/traverse'); | ||
const {default: traverse} = require('@babel/traverse'); | ||
const parser = require('@babel/parser'); | ||
const fs = require('fs'); | ||
const proxyCode = fs.readFileSync(`${__dirname}/proxy.js`, 'utf8'); | ||
|
||
const proxyCode = fs.readFileSync(`${__dirname}/proxy.js`, 'utf8'); | ||
const proxyAST = parser.parse(proxyCode); | ||
const proxyBodyAST = proxyAST.program.body.filter(node => !isModuleExports(node)); | ||
|
||
/** | ||
* The "Map" of if the "./proxy.js" code should be injected to the top of the Program | ||
*/ | ||
const Programs = new WeakMap(); | ||
|
||
module.exports = () => { | ||
return { | ||
name: 'willMutate', | ||
visitor: { | ||
/** | ||
* We need to traverse the "Program" so we can know if we need to inject | ||
* the Proxy handler or not. | ||
*/ | ||
Program: { | ||
enter(programPath) { | ||
traverse(programPath.node, { | ||
noScope: true, | ||
enter(path) { | ||
/** | ||
* Look for all $shouldNotMutate functions | ||
*/ | ||
if (path.node.type === 'ExpressionStatement') { | ||
const isFunc = looksLike(path, { | ||
node: { | ||
type: 'ExpressionStatement', | ||
expression: { | ||
callee: { | ||
name: '$shouldNotMutate', | ||
}, | ||
}, | ||
}, | ||
}); | ||
return { | ||
name: 'willMutate', | ||
visitor: { | ||
/** | ||
* We need to traverse the "Program" so we can know if we need to inject | ||
* the Proxy handler or not. | ||
*/ | ||
Program: { | ||
enter(programPath) { | ||
traverse(programPath.node, { | ||
noScope: true, | ||
enter(path) { | ||
/** | ||
* Look for all $shouldNotMutate functions | ||
*/ | ||
if (path.node.type === 'ExpressionStatement') { | ||
const isFunc = looksLike(path, { | ||
node: { | ||
type: 'ExpressionStatement', | ||
expression: { | ||
callee: { | ||
name: '$shouldNotMutate', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
if (!isFunc) return; | ||
|
||
if (!isFunc) return; | ||
const notMutateArgs = path.node.expression.arguments; | ||
|
||
const notMutateArgs = path.node.expression.arguments; | ||
|
||
let inBodyArgsToProxy = []; | ||
if (notMutateArgs.length) { | ||
// We expect the first item in the array to be an array of strings | ||
inBodyArgsToProxy = notMutateArgs[0].elements.map(node => node.value) | ||
} | ||
let inBodyArgsToProxy = []; | ||
if (notMutateArgs.length) { | ||
// We expect the first item in the array to be an array of strings | ||
inBodyArgsToProxy = notMutateArgs[0].elements.map(node => node.value) | ||
} | ||
|
||
debugger; | ||
debugger; | ||
|
||
Programs.set(programPath.node, true); | ||
Programs.set(programPath.node, true); | ||
|
||
// Treat the function as a "decorator" for a function. AKA get the function immediately | ||
// After the function itself | ||
const functionNodePath = path.getSibling(path.key + 1); | ||
// Treat the function as a "decorator" for a function. AKA get the function immediately | ||
// After the function itself | ||
const functionNodePath = path.getSibling(path.key + 1); | ||
|
||
// Traverse the path to get the "BlockStatement" so we can mutate the code on it's own | ||
traverse(functionNodePath.node, { | ||
noScope: true, | ||
enter(path) { | ||
if (path.node.type === 'BlockStatement') { | ||
console.log('WELCOME TO THE FUNCTION BODY OF THE $shouldNotMutate "Decorator'); | ||
// debugger; | ||
} | ||
}, | ||
}); | ||
} | ||
}, | ||
}); | ||
}, | ||
exit(programPath) { | ||
const val = Programs.get(programPath.node); | ||
/** | ||
* If the Program has any utilization of the $shouldNotMutate, then we'll inject the code from the | ||
* proxy.js file to then be able to use that function in the AST | ||
*/ | ||
if (val) { | ||
programPath.node.body = [...proxyAST.program.body, ...programPath.node.body]; | ||
} | ||
}, | ||
}, | ||
}, | ||
}; | ||
// Traverse the path to get the "BlockStatement" so we can mutate the code on it's own | ||
traverse(functionNodePath.node, { | ||
noScope: true, | ||
enter(path) { | ||
if (path.node.type === 'BlockStatement') { | ||
console.log('WELCOME TO THE FUNCTION BODY OF THE $shouldNotMutate "Decorator'); | ||
// debugger; | ||
} | ||
}, | ||
}); | ||
} | ||
}, | ||
}); | ||
}, | ||
exit(programPath) { | ||
const val = Programs.get(programPath.node); | ||
/** | ||
* If the Program has any utilization of the $shouldNotMutate, then we'll inject the code from the | ||
* proxy.js file to then be able to use that function in the AST | ||
*/ | ||
if (val) { | ||
programPath.node.body = [...proxyBodyAST, ...programPath.node.body]; | ||
} | ||
}, | ||
}, | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters