babel-helper-evaluate-path
Advanced tools
Comparing version 0.5.0-alpha.9939bca9 to 0.5.0-alpha.9e447f6d
@@ -11,4 +11,2 @@ "use strict"; | ||
const t = require("@babel/types"); | ||
module.exports = function evaluate(path, { | ||
@@ -150,3 +148,8 @@ tdz = false | ||
if (t.isIfStatement(declaration.parentPath) || t.isLoop(declaration.parentPath) || t.isSwitchCase(declaration.parentPath)) { | ||
if (declaration.parentPath) { | ||
/** | ||
* Handle when binding is created inside a parent block and | ||
* the corresponding parent is removed by other plugins | ||
* if (false) { var a } -> var a | ||
*/ | ||
if (declaration.parentPath.removed) { | ||
@@ -159,18 +162,21 @@ return { | ||
return { | ||
shouldDeopt: true | ||
}; | ||
if (declaration.parentPath.isIfStatement() || declaration.parentPath.isLoop() || declaration.parentPath.isSwitchCase()) { | ||
return { | ||
shouldDeopt: true | ||
}; | ||
} | ||
} | ||
const fnParent = (binding.path.scope.getFunctionParent() || binding.path.scope.getProgramParent()).path; | ||
let blockParent = binding.path.scope.getBlockParent().path; | ||
let blockParentPath = binding.path.scope.getBlockParent().path; | ||
let blockParent = blockParentPath.node; | ||
if (blockParent === fnParent && !fnParent.isProgram()) { | ||
blockParent = blockParent.get("body"); | ||
if (blockParentPath === fnParent && !fnParent.isProgram()) { | ||
blockParent = blockParent.body; | ||
} // detect Usage Outside Init Scope | ||
const blockBody = blockParent.get("body"); | ||
const blockBody = blockParent.body; | ||
if (Array.isArray(blockBody) && !blockBody.some(stmt => stmt.isAncestor(refPath))) { | ||
if (Array.isArray(blockBody) && !blockBody.some(stmt => isAncestor(stmt, refPath))) { | ||
return { | ||
@@ -182,3 +188,3 @@ shouldDeopt: true | ||
const stmts = fnParent.isProgram() ? fnParent.get("body") : fnParent.get("body").get("body"); | ||
const stmts = fnParent.isProgram() ? fnParent.node.body : fnParent.node.body.body; | ||
const compareResult = compareBindingAndReference({ | ||
@@ -203,3 +209,3 @@ binding, | ||
if (declaration.parentPath.isIfStatement() || declaration.parentPath.isLoop() || declaration.parentPath.isSwitchCase()) { | ||
if (declaration.parentPath && (declaration.parentPath.isIfStatement() || declaration.parentPath.isLoop() || declaration.parentPath.isSwitchCase())) { | ||
return { | ||
@@ -210,10 +216,16 @@ shouldDeopt: true | ||
let scopePath = declarator.scope.path; | ||
const scopePath = declarator.scope.path; | ||
let scopeNode = scopePath.node; | ||
if (scopePath.isFunction() || scopePath.isCatchClause()) { | ||
scopePath = scopePath.get("body"); | ||
scopeNode = scopeNode.body; | ||
} // Detect Usage before Init | ||
const stmts = scopePath.get("body"); | ||
let stmts = scopeNode.body; | ||
if (!Array.isArray(stmts)) { | ||
stmts = [stmts]; | ||
} | ||
const compareResult = compareBindingAndReference({ | ||
@@ -263,3 +275,3 @@ binding, | ||
if (stmt.isAncestor(binding.path)) { | ||
if (isAncestor(stmt, binding.path)) { | ||
state.binding = { | ||
@@ -278,3 +290,3 @@ idx | ||
if (ref === refPath && stmt.isAncestor(ref)) { | ||
if (ref === refPath && isAncestor(stmt, ref)) { | ||
state.reference = { | ||
@@ -325,2 +337,10 @@ idx, | ||
}; | ||
} | ||
/** | ||
* is nodeParent an ancestor of path | ||
*/ | ||
function isAncestor(nodeParent, path) { | ||
return !!path.findParent(parent => parent.node === nodeParent); | ||
} |
{ | ||
"name": "babel-helper-evaluate-path", | ||
"version": "0.5.0-alpha.9939bca9", | ||
"version": "0.5.0-alpha.9e447f6d", | ||
"description": "path.evaluate wrapped in a try catch", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9733
271