babel-helper-evaluate-path
Advanced tools
Comparing version 0.5.0-alpha.7b176463 to 0.5.0-alpha.8b905991
"use strict"; | ||
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) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } | ||
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"); } } | ||
function _nonIterableRest() { 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 === void 0 ? false : _ref$tdz; | ||
function _iterableToArrayLimit(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 _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
module.exports = function evaluate(path, { | ||
tdz = false | ||
} = {}) { | ||
if (!tdz) { | ||
@@ -20,3 +22,3 @@ return baseEvaluate(path); | ||
var state = { | ||
const state = { | ||
confident: true | ||
@@ -31,7 +33,7 @@ }; // prepare | ||
ReferencedIdentifier(idPath) { | ||
var binding = idPath.scope.getBinding(idPath.node.name); // don't deopt globals | ||
const binding = idPath.scope.getBinding(idPath.node.name); // don't deopt globals | ||
// let babel take care of it | ||
if (!binding) return; | ||
var evalResult = evaluateIdentifier(idPath); | ||
const evalResult = evaluateIdentifier(idPath); | ||
@@ -72,4 +74,4 @@ if (!evalResult.confident) { | ||
var node = path.node; | ||
var binding = path.scope.getBinding(node.name); | ||
const node = path.node; | ||
const binding = path.scope.getBinding(node.name); | ||
@@ -91,3 +93,3 @@ if (!binding) { | ||
var flowEvalResult = evaluateBasedOnControlFlow(binding, path); | ||
const flowEvalResult = evaluateBasedOnControlFlow(binding, path); | ||
@@ -120,3 +122,3 @@ if (flowEvalResult.confident) { | ||
// early-exit | ||
var declaration = binding.path.parentPath; | ||
const declaration = binding.path.parentPath; | ||
@@ -129,4 +131,4 @@ if (declaration.parentPath.isIfStatement() || declaration.parentPath.isLoop() || declaration.parentPath.isSwitchCase()) { | ||
var blockParent = binding.path.scope.getBlockParent().path; | ||
var fnParent = binding.path.getFunctionParent(); | ||
let blockParent = binding.path.scope.getBlockParent().path; | ||
const fnParent = binding.path.getFunctionParent(); | ||
@@ -138,5 +140,3 @@ if (blockParent === fnParent) { | ||
if (!blockParent.get("body").some(function (stmt) { | ||
return stmt.isAncestor(refPath); | ||
})) { | ||
if (!blockParent.get("body").some(stmt => stmt.isAncestor(refPath))) { | ||
return { | ||
@@ -148,4 +148,4 @@ shouldDeopt: true | ||
var stmts = fnParent.isProgram() ? fnParent.get("body") : fnParent.get("body").get("body"); | ||
var compareResult = compareBindingAndReference({ | ||
const stmts = fnParent.isProgram() ? fnParent.get("body") : fnParent.get("body").get("body"); | ||
const compareResult = compareBindingAndReference({ | ||
binding, | ||
@@ -170,6 +170,6 @@ refPath, | ||
// binding.path is the declarator | ||
var declarator = binding.path; | ||
var _declaration = declarator.parentPath; | ||
const declarator = binding.path; | ||
const declaration = declarator.parentPath; | ||
if (_declaration.parentPath.isIfStatement() || _declaration.parentPath.isLoop() || _declaration.parentPath.isSwitchCase()) { | ||
if (declaration.parentPath.isIfStatement() || declaration.parentPath.isLoop() || declaration.parentPath.isSwitchCase()) { | ||
return { | ||
@@ -180,3 +180,3 @@ shouldDeopt: true | ||
var scopePath = declarator.scope.path; | ||
let scopePath = declarator.scope.path; | ||
@@ -188,16 +188,15 @@ if (scopePath.isFunction() || scopePath.isCatchClause()) { | ||
var _stmts = scopePath.get("body"); | ||
var _compareResult = compareBindingAndReference({ | ||
const stmts = scopePath.get("body"); | ||
const compareResult = compareBindingAndReference({ | ||
binding, | ||
refPath, | ||
stmts: _stmts | ||
stmts | ||
}); | ||
if (_compareResult.reference && _compareResult.binding) { | ||
if (_compareResult.reference.scope === "current" && _compareResult.reference.idx < _compareResult.binding.idx) { | ||
if (compareResult.reference && compareResult.binding) { | ||
if (compareResult.reference.scope === "current" && compareResult.reference.idx < compareResult.binding.idx) { | ||
throw new Error(`ReferenceError: Used ${refPath.node.name}: ` + `${binding.kind} binding before declaration`); | ||
} | ||
if (_compareResult.reference.scope === "other") { | ||
if (compareResult.reference.scope === "other") { | ||
return { | ||
@@ -216,7 +215,8 @@ shouldDeopt: true | ||
function compareBindingAndReference(_ref2) { | ||
var binding = _ref2.binding, | ||
refPath = _ref2.refPath, | ||
stmts = _ref2.stmts; | ||
var state = { | ||
function compareBindingAndReference({ | ||
binding, | ||
refPath, | ||
stmts | ||
}) { | ||
const state = { | ||
binding: null, | ||
@@ -231,12 +231,9 @@ reference: null | ||
for (var _iterator = stmts.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _ref5 = _step.value; | ||
const _step$value = _slicedToArray(_step.value, 2), | ||
idx = _step$value[0], | ||
stmt = _step$value[1]; | ||
var _ref4 = _slicedToArray(_ref5, 2); | ||
var _idx = _ref4[0]; | ||
var _stmt = _ref4[1]; | ||
if (_stmt.isAncestor(binding.path)) { | ||
if (stmt.isAncestor(binding.path)) { | ||
state.binding = { | ||
idx: _idx | ||
idx | ||
}; | ||
@@ -251,8 +248,8 @@ } | ||
for (var _iterator2 = binding.referencePaths[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var _ref6 = _step2.value; | ||
const ref = _step2.value; | ||
if (_ref6 === refPath && _stmt.isAncestor(_ref6)) { | ||
if (ref === refPath && stmt.isAncestor(ref)) { | ||
state.reference = { | ||
idx: _idx, | ||
scope: binding.path.scope === _ref6.scope ? "current" : "other" | ||
idx, | ||
scope: binding.path.scope === ref.scope ? "current" : "other" | ||
}; | ||
@@ -259,0 +256,0 @@ break; |
{ | ||
"name": "babel-helper-evaluate-path", | ||
"version": "0.5.0-alpha.7b176463", | ||
"version": "0.5.0-alpha.8b905991", | ||
"description": "path.evaluate wrapped in a try catch", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
8526
227