From dac339cda3207d8c591b6952aa0259908d8f1802 Mon Sep 17 00:00:00 2001 From: Corbin Crutchley Date: Sun, 3 May 2020 12:49:10 -0700 Subject: [PATCH] Remove the module.exports from the proxy code --- plugin/index.js | 139 ++++++++++++++++++++++--------------------- plugin/looks-like.js | 19 +++++- 2 files changed, 88 insertions(+), 70 deletions(-) diff --git a/plugin/index.js b/plugin/index.js index c841c4e..2cf781f 100644 --- a/plugin/index.js +++ b/plugin/index.js @@ -1,11 +1,12 @@ -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 @@ -13,76 +14,76 @@ const proxyAST = parser.parse(proxyCode); 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]; + } + }, + }, + }, + }; }; diff --git a/plugin/looks-like.js b/plugin/looks-like.js index 16c3914..39d425a 100644 --- a/plugin/looks-like.js +++ b/plugin/looks-like.js @@ -16,4 +16,21 @@ const looksLike = (a, b) => { const isPrimitive = (val) => val == null || /^[sbn]/.test(typeof val); -module.exports = looksLike; +const isModuleExports = node => looksLike(node, { + type: 'ExpressionStatement', + expression: { + left: { + object: { + name: 'module' + }, + property: { + name: "exports" + } + } + } +}) + +module.exports = { + looksLike, + isModuleExports +};