babel-helper-evaluate-path
Advanced tools
Comparing version 0.5.0-alpha.34c3efe9 to 0.5.0-alpha.540d75e7
@@ -11,6 +11,8 @@ "use strict"; | ||
const t = require("@babel/types"); | ||
module.exports = function evaluate(path, { | ||
tdz = false | ||
} = {}) { | ||
if (!tdz) { | ||
if (!tdz && !path.isReferencedIdentifier()) { | ||
return baseEvaluate(path); | ||
@@ -77,3 +79,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,3 +151,10 @@ | ||
if (declaration.parentPath.isIfStatement() || declaration.parentPath.isLoop() || declaration.parentPath.isSwitchCase()) { | ||
if (t.isIfStatement(declaration.parentPath) || t.isLoop(declaration.parentPath) || t.isSwitchCase(declaration.parentPath)) { | ||
if (declaration.parentPath.removed) { | ||
return { | ||
confident: true, | ||
value: void 0 | ||
}; | ||
} | ||
return { | ||
@@ -129,11 +165,13 @@ shouldDeopt: true | ||
const fnParent = (binding.path.scope.getFunctionParent() || binding.path.scope.getProgramParent()).path; | ||
let blockParent = binding.path.scope.getBlockParent().path; | ||
const fnParent = binding.path.getFunctionParent(); | ||
if (blockParent === fnParent) { | ||
if (!fnParent.isProgram()) blockParent = blockParent.get("body"); | ||
if (blockParent === fnParent && !fnParent.isProgram()) { | ||
blockParent = blockParent.get("body"); | ||
} // detect Usage Outside Init Scope | ||
if (!blockParent.get("body").some(stmt => stmt.isAncestor(refPath))) { | ||
const blockBody = blockParent.get("body"); | ||
if (Array.isArray(blockBody) && !blockBody.some(stmt => stmt.isAncestor(refPath))) { | ||
return { | ||
@@ -159,6 +197,2 @@ shouldDeopt: true | ||
} | ||
return { | ||
shouldDeopt: true | ||
}; | ||
} | ||
@@ -165,0 +199,0 @@ } else if (binding.kind === "let" || binding.kind === "const") { |
{ | ||
"name": "babel-helper-evaluate-path", | ||
"version": "0.5.0-alpha.34c3efe9", | ||
"version": "0.5.0-alpha.540d75e7", | ||
"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
9236
254