ast-walker-scope
Advanced tools
+1
-3
@@ -29,7 +29,5 @@ import * as _babel_parser from '@babel/parser'; | ||
| declare const walk: (code: string, walkHooks: WalkerHooks, { filename, parserPlugins }?: ParseOptions) => { | ||
| ast: _babel_parser.ParseResult<_babel_types.File>; | ||
| }; | ||
| declare const walk: (code: string, walkHooks: WalkerHooks, { filename, parserPlugins }?: ParseOptions) => _babel_parser.ParseResult<_babel_types.File>; | ||
| declare const walkAST: (ast: Program, { enter, leave }: WalkerHooks) => void; | ||
| export { HookContext, ParseOptions, Scope, WalkerContext, WalkerHooks, walk, walkAST }; |
+23
-19
@@ -141,6 +141,13 @@ "use strict"; | ||
| // src/index.ts | ||
| var import_types2 = require("@babel/types"); | ||
| // src/utils/babel.ts | ||
| var isFunctionType = (node) => { | ||
| return /Function(?:Expression|Declaration)$|Method$/.test(node.type); | ||
| }; | ||
| var import_types = require("@babel/types"); | ||
| var NEW_SCOPE = [ | ||
| "CatchClause", | ||
| "ForInStatement", | ||
| "ForOfStatement" | ||
| ]; | ||
| var isNewScope = (node) => NEW_SCOPE.includes(node.type) || (0, import_types.isFunction)(node); | ||
| function walkFunctionParams(node, onIdent) { | ||
@@ -192,7 +199,2 @@ for (const p of node.params) { | ||
| // src/index.ts | ||
| var ADVANCED_SCOPE = [ | ||
| "CatchClause", | ||
| "ForInStatement", | ||
| "ForOfStatement" | ||
| ]; | ||
| var walk2 = (code, walkHooks, { filename, parserPlugins } = {}) => { | ||
@@ -211,3 +213,3 @@ const plugins = parserPlugins || []; | ||
| walkAST(ast.program, walkHooks); | ||
| return { ast }; | ||
| return ast; | ||
| }; | ||
@@ -240,14 +242,16 @@ var walkAST = (ast, { enter, leave }) => { | ||
| function enterNode(node, parent) { | ||
| if (isFunctionType(node)) { | ||
| if (isNewScope(node) || node.type === "BlockStatement" && !isNewScope(parent)) | ||
| scopeStack.push(currentScope = {}); | ||
| if ((0, import_types2.isFunction)(node)) { | ||
| walkFunctionParams(node, registerBinding); | ||
| } else if (node.type === "CatchClause") { | ||
| scopeStack.push(currentScope = {}); | ||
| if (node.param && node.param.type === "Identifier") | ||
| registerBinding(node.param); | ||
| return; | ||
| } else if (node.type === "ForOfStatement" || node.type === "ForInStatement") { | ||
| scopeStack.push(currentScope = {}); | ||
| } else if (node.type === "BlockStatement" && !isFunctionType(parent) && !ADVANCED_SCOPE.includes(parent.type)) { | ||
| scopeStack.push(currentScope = {}); | ||
| } else if (node.type === "CatchClause" && node.param && node.param.type === "Identifier") | ||
| registerBinding(node.param); | ||
| if (node.type === "BlockStatement" || node.type === "Program") { | ||
| for (const stmt of node.body) { | ||
| if (stmt.type === "VariableDeclaration" && stmt.kind === "var") { | ||
| walkVariableDeclaration(stmt); | ||
| } else if (stmt.type === "FunctionDeclaration" && stmt.id) { | ||
| registerBinding(stmt.id); | ||
| } | ||
| } | ||
| } | ||
@@ -254,0 +258,0 @@ } |
+23
-19
@@ -116,6 +116,13 @@ // src/index.ts | ||
| // src/index.ts | ||
| import { isFunction as isFunction2 } from "@babel/types"; | ||
| // src/utils/babel.ts | ||
| var isFunctionType = (node) => { | ||
| return /Function(?:Expression|Declaration)$|Method$/.test(node.type); | ||
| }; | ||
| import { isFunction } from "@babel/types"; | ||
| var NEW_SCOPE = [ | ||
| "CatchClause", | ||
| "ForInStatement", | ||
| "ForOfStatement" | ||
| ]; | ||
| var isNewScope = (node) => NEW_SCOPE.includes(node.type) || isFunction(node); | ||
| function walkFunctionParams(node, onIdent) { | ||
@@ -167,7 +174,2 @@ for (const p of node.params) { | ||
| // src/index.ts | ||
| var ADVANCED_SCOPE = [ | ||
| "CatchClause", | ||
| "ForInStatement", | ||
| "ForOfStatement" | ||
| ]; | ||
| var walk2 = (code, walkHooks, { filename, parserPlugins } = {}) => { | ||
@@ -186,3 +188,3 @@ const plugins = parserPlugins || []; | ||
| walkAST(ast.program, walkHooks); | ||
| return { ast }; | ||
| return ast; | ||
| }; | ||
@@ -215,14 +217,16 @@ var walkAST = (ast, { enter, leave }) => { | ||
| function enterNode(node, parent) { | ||
| if (isFunctionType(node)) { | ||
| if (isNewScope(node) || node.type === "BlockStatement" && !isNewScope(parent)) | ||
| scopeStack.push(currentScope = {}); | ||
| if (isFunction2(node)) { | ||
| walkFunctionParams(node, registerBinding); | ||
| } else if (node.type === "CatchClause") { | ||
| scopeStack.push(currentScope = {}); | ||
| if (node.param && node.param.type === "Identifier") | ||
| registerBinding(node.param); | ||
| return; | ||
| } else if (node.type === "ForOfStatement" || node.type === "ForInStatement") { | ||
| scopeStack.push(currentScope = {}); | ||
| } else if (node.type === "BlockStatement" && !isFunctionType(parent) && !ADVANCED_SCOPE.includes(parent.type)) { | ||
| scopeStack.push(currentScope = {}); | ||
| } else if (node.type === "CatchClause" && node.param && node.param.type === "Identifier") | ||
| registerBinding(node.param); | ||
| if (node.type === "BlockStatement" || node.type === "Program") { | ||
| for (const stmt of node.body) { | ||
| if (stmt.type === "VariableDeclaration" && stmt.kind === "var") { | ||
| walkVariableDeclaration(stmt); | ||
| } else if (stmt.type === "FunctionDeclaration" && stmt.id) { | ||
| registerBinding(stmt.id); | ||
| } | ||
| } | ||
| } | ||
@@ -229,0 +233,0 @@ } |
+1
-1
| { | ||
| "name": "ast-walker-scope", | ||
| "version": "0.1.0", | ||
| "version": "0.2.0", | ||
| "packageManager": "pnpm@7.6.0", | ||
@@ -5,0 +5,0 @@ "description": "Traverse Babel AST with scope information.", |
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
22405
1.42%586
0.69%0
-100%