babel-helper-evaluate-path
Advanced tools
Comparing version 0.5.0-alpha.9e986477 to 0.5.0-alpha.a28b6b00
@@ -14,3 +14,3 @@ "use strict"; | ||
} = {}) { | ||
if (!tdz) { | ||
if (!tdz && !path.isReferencedIdentifier()) { | ||
return baseEvaluate(path); | ||
@@ -77,3 +77,30 @@ } | ||
if (!binding) { | ||
return deopt(path); | ||
const name = node.name; | ||
if (!name) { | ||
return deopt(path); | ||
} | ||
switch (name) { | ||
case "undefined": | ||
return { | ||
confident: true, | ||
value: undefined | ||
}; | ||
case "NaN": | ||
return { | ||
confident: true, | ||
value: NaN | ||
}; | ||
case "Infinity": | ||
return { | ||
confident: true, | ||
value: Infinity | ||
}; | ||
default: | ||
return deopt(path); | ||
} | ||
} | ||
@@ -122,17 +149,34 @@ | ||
if (declaration.parentPath.isIfStatement() || declaration.parentPath.isLoop() || declaration.parentPath.isSwitchCase()) { | ||
return { | ||
shouldDeopt: true | ||
}; | ||
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) { | ||
return { | ||
confident: true, | ||
value: void 0 | ||
}; | ||
} | ||
if (declaration.parentPath.isIfStatement() || declaration.parentPath.isLoop() || declaration.parentPath.isSwitchCase()) { | ||
return { | ||
shouldDeopt: true | ||
}; | ||
} | ||
} | ||
let blockParent = binding.path.scope.getBlockParent().path; | ||
const fnParent = binding.path.getFunctionParent(); | ||
const fnParent = (binding.path.scope.getFunctionParent() || binding.path.scope.getProgramParent()).path; | ||
let blockParentPath = binding.path.scope.getBlockParent().path; | ||
let blockParent = blockParentPath.node; | ||
if (blockParent === fnParent) { | ||
if (!fnParent.isProgram()) blockParent = blockParent.get("body"); | ||
if (blockParentPath === fnParent && !fnParent.isProgram()) { | ||
blockParent = blockParent.body; | ||
} // detect Usage Outside Init Scope | ||
if (!blockParent.get("body").some(stmt => stmt.isAncestor(refPath))) { | ||
const blockBody = blockParent.body; | ||
if (Array.isArray(blockBody) && !blockBody.some(stmt => isAncestor(stmt, refPath))) { | ||
return { | ||
@@ -144,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({ | ||
@@ -159,6 +203,2 @@ binding, | ||
} | ||
return { | ||
shouldDeopt: true | ||
}; | ||
} | ||
@@ -170,3 +210,3 @@ } else if (binding.kind === "let" || binding.kind === "const") { | ||
if (declaration.parentPath.isIfStatement() || declaration.parentPath.isLoop() || declaration.parentPath.isSwitchCase()) { | ||
if (declaration.parentPath && (declaration.parentPath.isIfStatement() || declaration.parentPath.isLoop() || declaration.parentPath.isSwitchCase())) { | ||
return { | ||
@@ -177,10 +217,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({ | ||
@@ -230,3 +276,3 @@ binding, | ||
if (stmt.isAncestor(binding.path)) { | ||
if (isAncestor(stmt, binding.path)) { | ||
state.binding = { | ||
@@ -245,3 +291,3 @@ idx | ||
if (ref === refPath && stmt.isAncestor(ref)) { | ||
if (ref === refPath && isAncestor(stmt, ref)) { | ||
state.reference = { | ||
@@ -292,2 +338,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.9e986477", | ||
"version": "0.5.0-alpha.a28b6b00", | ||
"description": "path.evaluate wrapped in a try catch", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -34,3 +34,3 @@ # babel-helper-evaluate-path | ||
```sh | ||
npm install babel-helper-evaluate-path | ||
npm install babel-helper-evaluate-path --save-dev | ||
``` |
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