-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from oslabs-beta/ast-implementation
early implementation for lazy-loading import statements
- Loading branch information
Showing
6 changed files
with
148 additions
and
76 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,34 +1,53 @@ | ||
const acorn = require('acorn'); | ||
const jsx = require('acorn-jsx'); | ||
const JSXParser = acorn.Parser.extend(jsx()); | ||
|
||
// Helper function to recursively traverse through the user's codebase | ||
// INSERT HERE | ||
const JSXParser = acorn.Parser.extend(jsx()); | ||
|
||
module.exports = file => { | ||
// Helper function to recursively traverse AST of a specified component for all hook declarations | ||
function getHookNames(ast) { | ||
// Initialize empty object to store the setters and getter | ||
const hookState = {}; | ||
const ast = JSXParser.parse(file).body; | ||
// Iterate through AST of every function declaration | ||
// Check within each function declaration if there are hook declarations | ||
ast.forEach(func => { | ||
const { body } = func.body; | ||
const statements = []; | ||
// Traverse through the function's funcDecs and Expression Statements | ||
body.forEach(program => { | ||
if (program.type === 'VariableDeclaration') { | ||
program.declarations.forEach(dec => { | ||
statements.push(dec.id.name); | ||
}); | ||
// All module exports will always start off as a single 'FunctionDeclaration' type | ||
while (Object.hasOwnProperty.call(ast, 'body')) { | ||
// Traverse down .body once before invoking parsing logic and will loop through any .body after | ||
ast = ast.body; | ||
// Iterate through AST of every function declaration | ||
// Check within each function declaration if there are hook declarations | ||
ast.forEach(functionDec => { | ||
const { body } = functionDec.body; | ||
const statements = []; | ||
// Traverse through the function's funcDecs and Expression Statements | ||
body.forEach(program => { | ||
// Hook Declarations will only be under 'VariableDeclaration' type | ||
if (program.type === 'VariableDeclaration') { | ||
program.declarations.forEach(dec => { | ||
statements.push(dec.id.name); | ||
}); | ||
} | ||
}); | ||
// Iterate through the array and determine getter/setters based on pattern | ||
for (let i = 0; i < statements.length; i += 1) { | ||
if (statements[i].match(/_use/)) { | ||
hookState[statements[i]] = statements[i + 2]; | ||
} | ||
} | ||
}); | ||
// Iterate through the array and determine getter/setters based on pattern | ||
for (let i = 0; i < statements.length; i += 1) { | ||
if (statements[i].match(/_use/)) { | ||
hookState[statements[i]] = statements[i + 2]; | ||
} | ||
} | ||
}); | ||
// Return the object with setters and getters | ||
} | ||
return hookState; | ||
} | ||
|
||
module.exports = file => { | ||
// Create an empty object to allow all invocations of getHookNames to consolidate | ||
let allHookNames = {}; | ||
const ast = JSXParser.parse(file); | ||
// console.log('Original File', file.toString()); | ||
// console.log('Original AST', ast); | ||
// Upsert any new/updated {_hookType#: hookName} pairs | ||
allHookNames = { | ||
...allHookNames, | ||
...getHookNames(ast), | ||
}; | ||
|
||
// Return the object with setters and getters | ||
return allHookNames; | ||
}; |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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