babel-helper-evaluate-path
Advanced tools
Comparing version 0.4.0-alpha.6546ad11 to 0.4.0-alpha.caaefb4c
117
lib/index.js
"use strict"; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } } | ||
module.exports = function evaluate(path) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
_ref$tdz = _ref.tdz, | ||
tdz = _ref$tdz === undefined ? false : _ref$tdz; | ||
tdz = _ref$tdz === void 0 ? false : _ref$tdz; | ||
@@ -20,5 +22,4 @@ if (!tdz) { | ||
confident: true | ||
}; | ||
}; // prepare | ||
// prepare | ||
path.traverse({ | ||
@@ -28,9 +29,10 @@ Scope(scopePath) { | ||
}, | ||
ReferencedIdentifier(idPath) { | ||
var binding = idPath.scope.getBinding(idPath.node.name); | ||
// don't deopt globals | ||
var binding = idPath.scope.getBinding(idPath.node.name); // don't deopt globals | ||
// let babel take care of it | ||
if (!binding) return; | ||
var evalResult = evaluateIdentifier(idPath); | ||
var evalResult = evaluateIdentifier(idPath); | ||
if (!evalResult.confident) { | ||
@@ -41,2 +43,3 @@ state.confident = evalResult.confident; | ||
} | ||
}); | ||
@@ -60,7 +63,7 @@ | ||
} | ||
} | ||
// Original Source: | ||
} // Original Source: | ||
// https://github.com/babel/babel/blob/master/packages/babel-traverse/src/path/evaluation.js | ||
// modified for Babel-minify use | ||
function evaluateIdentifier(path) { | ||
@@ -72,4 +75,2 @@ if (!path.isReferencedIdentifier()) { | ||
var node = path.node; | ||
var binding = path.scope.getBinding(node.name); | ||
@@ -83,11 +84,11 @@ | ||
return deopt(binding.path); | ||
} | ||
} // referenced in a different scope - deopt | ||
// referenced in a different scope - deopt | ||
if (shouldDeoptBasedOnScope(binding, path)) { | ||
return deopt(path); | ||
} | ||
} // let/var/const referenced before init | ||
// or "var" referenced in an outer scope | ||
// let/var/const referenced before init | ||
// or "var" referenced in an outer scope | ||
var flowEvalResult = evaluateBasedOnControlFlow(binding, path); | ||
@@ -104,8 +105,8 @@ | ||
return path.evaluate(); | ||
} | ||
// check if referenced in a different fn scope | ||
} // check if referenced in a different fn scope | ||
// we can't determine if this function is called sync or async | ||
// if the binding is in program scope | ||
// all it's references inside a different function should be deopted | ||
function shouldDeoptBasedOnScope(binding, refPath) { | ||
@@ -115,2 +116,3 @@ if (binding.scope.path.isProgram() && refPath.scope !== binding.scope) { | ||
} | ||
return false; | ||
@@ -123,4 +125,7 @@ } | ||
var declaration = binding.path.parentPath; | ||
if (declaration.parentPath.isIfStatement() || declaration.parentPath.isLoop() || declaration.parentPath.isSwitchCase()) { | ||
return { shouldDeopt: true }; | ||
return { | ||
shouldDeopt: true | ||
}; | ||
} | ||
@@ -133,14 +138,15 @@ | ||
if (!fnParent.isProgram()) blockParent = blockParent.get("body"); | ||
} | ||
} // detect Usage Outside Init Scope | ||
// detect Usage Outside Init Scope | ||
if (!blockParent.get("body").some(function (stmt) { | ||
return stmt.isAncestor(refPath); | ||
})) { | ||
return { shouldDeopt: true }; | ||
} | ||
return { | ||
shouldDeopt: true | ||
}; | ||
} // Detect usage before init | ||
// Detect usage before init | ||
var stmts = fnParent.isProgram() ? fnParent.get("body") : fnParent.get("body").get("body"); | ||
var compareResult = compareBindingAndReference({ | ||
@@ -154,6 +160,11 @@ binding, | ||
if (compareResult.reference.scope === "current" && compareResult.reference.idx < compareResult.binding.idx) { | ||
return { confident: true, value: void 0 }; | ||
return { | ||
confident: true, | ||
value: void 0 | ||
}; | ||
} | ||
return { shouldDeopt: true }; | ||
return { | ||
shouldDeopt: true | ||
}; | ||
} | ||
@@ -166,11 +177,14 @@ } else if (binding.kind === "let" || binding.kind === "const") { | ||
if (_declaration.parentPath.isIfStatement() || _declaration.parentPath.isLoop() || _declaration.parentPath.isSwitchCase()) { | ||
return { shouldDeopt: true }; | ||
return { | ||
shouldDeopt: true | ||
}; | ||
} | ||
var scopePath = declarator.scope.path; | ||
if (scopePath.isFunction() || scopePath.isCatchClause()) { | ||
scopePath = scopePath.get("body"); | ||
} | ||
} // Detect Usage before Init | ||
// Detect Usage before Init | ||
var _stmts = scopePath.get("body"); | ||
@@ -188,4 +202,7 @@ | ||
} | ||
if (_compareResult.reference.scope === "other") { | ||
return { shouldDeopt: true }; | ||
return { | ||
shouldDeopt: true | ||
}; | ||
} | ||
@@ -195,3 +212,6 @@ } | ||
return { confident: false, shouldDeopt: false }; | ||
return { | ||
confident: false, | ||
shouldDeopt: false | ||
}; | ||
} | ||
@@ -203,3 +223,2 @@ | ||
stmts = _ref2.stmts; | ||
var state = { | ||
@@ -209,3 +228,2 @@ binding: null, | ||
}; | ||
var _iteratorNormalCompletion = true; | ||
@@ -217,12 +235,15 @@ var _didIteratorError = false; | ||
for (var _iterator = stmts.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _ref3 = _step.value; | ||
var _ref5 = _step.value; | ||
var _ref4 = _slicedToArray(_ref3, 2); | ||
var _ref4 = _slicedToArray(_ref5, 2); | ||
var idx = _ref4[0]; | ||
var stmt = _ref4[1]; | ||
var _idx = _ref4[0]; | ||
var _stmt = _ref4[1]; | ||
if (stmt.isAncestor(binding.path)) { | ||
state.binding = { idx }; | ||
if (_stmt.isAncestor(binding.path)) { | ||
state.binding = { | ||
idx: _idx | ||
}; | ||
} | ||
var _iteratorNormalCompletion2 = true; | ||
@@ -234,8 +255,8 @@ var _didIteratorError2 = false; | ||
for (var _iterator2 = binding.referencePaths[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var ref = _step2.value; | ||
var _ref6 = _step2.value; | ||
if (ref === refPath && stmt.isAncestor(ref)) { | ||
if (_ref6 === refPath && _stmt.isAncestor(_ref6)) { | ||
state.reference = { | ||
idx, | ||
scope: binding.path.scope === ref.scope ? "current" : "other" | ||
idx: _idx, | ||
scope: binding.path.scope === _ref6.scope ? "current" : "other" | ||
}; | ||
@@ -250,3 +271,3 @@ break; | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return != null) { | ||
_iterator2.return(); | ||
@@ -266,3 +287,3 @@ } | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
@@ -269,0 +290,0 @@ } |
{ | ||
"name": "babel-helper-evaluate-path", | ||
"version": "0.4.0-alpha.6546ad11", | ||
"version": "0.4.0-alpha.caaefb4c", | ||
"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
8704
228