diff options
Diffstat (limited to 'node_modules/babel-helper-evaluate-path')
| -rw-r--r-- | node_modules/babel-helper-evaluate-path/LICENSE | 22 | ||||
| -rw-r--r-- | node_modules/babel-helper-evaluate-path/README.md | 35 | ||||
| -rw-r--r-- | node_modules/babel-helper-evaluate-path/lib/index.js | 337 | ||||
| -rw-r--r-- | node_modules/babel-helper-evaluate-path/package.json | 16 |
4 files changed, 410 insertions, 0 deletions
diff --git a/node_modules/babel-helper-evaluate-path/LICENSE b/node_modules/babel-helper-evaluate-path/LICENSE new file mode 100644 index 00000000..f9f0a7f3 --- /dev/null +++ b/node_modules/babel-helper-evaluate-path/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2015-2016 Amjad Masad <amjad.masad@gmail.com> + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/node_modules/babel-helper-evaluate-path/README.md b/node_modules/babel-helper-evaluate-path/README.md new file mode 100644 index 00000000..7f32eb10 --- /dev/null +++ b/node_modules/babel-helper-evaluate-path/README.md @@ -0,0 +1,35 @@ +# babel-helper-evaluate-path + +A wrapper for babel's `path.evaluate` + +Fixes / Features: + ++ Detect usages before initialization / declaration + +```js +function foo() { + console.log(b); // ReferenceError + let b = 1; +} + +function bar() { + console.log(a); // a is void 0 + var a = 5; + console.log(a); // 5 +} +``` + ++ Detect usages in scopes outside of initialization for vars (hoisted) + +```js +function foo() { + if (a) var x = 5; + console.log(x); // cannot determine +} +``` + +## Installation + +```sh +npm install babel-helper-evaluate-path --save-dev +``` diff --git a/node_modules/babel-helper-evaluate-path/lib/index.js b/node_modules/babel-helper-evaluate-path/lib/index.js new file mode 100644 index 00000000..d7fe0a11 --- /dev/null +++ b/node_modules/babel-helper-evaluate-path/lib/index.js @@ -0,0 +1,337 @@ +"use strict"; + +function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } + +function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } + +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 && !path.isReferencedIdentifier()) { + return baseEvaluate(path); + } + + if (path.isReferencedIdentifier()) { + return evaluateIdentifier(path); + } + + const state = { + confident: true + }; // prepare + + path.traverse({ + Scope(scopePath) { + scopePath.skip(); + }, + + ReferencedIdentifier(idPath) { + const binding = idPath.scope.getBinding(idPath.node.name); // don't deopt globals + // let babel take care of it + + if (!binding) return; + const evalResult = evaluateIdentifier(idPath); + + if (!evalResult.confident) { + state.confident = evalResult.confident; + state.deoptPath = evalResult.deoptPath; + } + } + + }); + + if (!state.confident) { + return state; + } + + return baseEvaluate(path); +}; + +function baseEvaluate(path) { + try { + return path.evaluate(); + } catch (e) { + return { + confident: false, + error: e + }; + } +} // Original Source: +// https://github.com/babel/babel/blob/master/packages/babel-traverse/src/path/evaluation.js +// modified for Babel-minify use + + +function evaluateIdentifier(path) { + if (!path.isReferencedIdentifier()) { + throw new Error(`Expected ReferencedIdentifier. Got ${path.type}`); + } + + const node = path.node; + const binding = path.scope.getBinding(node.name); + + if (!binding) { + 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); + } + } + + if (binding.constantViolations.length > 0) { + return deopt(binding.path); + } // 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 + + + const flowEvalResult = evaluateBasedOnControlFlow(binding, path); + + if (flowEvalResult.confident) { + return flowEvalResult; + } + + if (flowEvalResult.shouldDeopt) { + return deopt(path); + } + + return path.evaluate(); +} // 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) { + if (binding.scope.path.isProgram() && refPath.scope !== binding.scope) { + return true; + } + + return false; +} + +function evaluateBasedOnControlFlow(binding, refPath) { + if (binding.kind === "var") { + // early-exit + const declaration = binding.path.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) { + return { + confident: true, + value: void 0 + }; + } + + 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 blockParentPath = binding.path.scope.getBlockParent().path; + let blockParent = blockParentPath.node; + + if (blockParentPath === fnParent && !fnParent.isProgram()) { + blockParent = blockParent.body; + } // detect Usage Outside Init Scope + + + const blockBody = blockParent.body; + + if (Array.isArray(blockBody) && !blockBody.some(stmt => isAncestor(stmt, refPath))) { + return { + shouldDeopt: true + }; + } // Detect usage before init + + + const stmts = fnParent.isProgram() ? fnParent.node.body : fnParent.node.body.body; + const compareResult = compareBindingAndReference({ + binding, + refPath, + stmts + }); + + if (compareResult.reference && compareResult.binding) { + if (compareResult.reference.scope === "current" && compareResult.reference.idx < compareResult.binding.idx) { + return { + confident: true, + value: void 0 + }; + } + } + } else if (binding.kind === "let" || binding.kind === "const") { + // binding.path is the declarator + const declarator = binding.path; + const declaration = declarator.parentPath; + + if (declaration.parentPath && (declaration.parentPath.isIfStatement() || declaration.parentPath.isLoop() || declaration.parentPath.isSwitchCase())) { + return { + shouldDeopt: true + }; + } + + const scopePath = declarator.scope.path; + let scopeNode = scopePath.node; + + if (scopePath.isFunction() || scopePath.isCatchClause()) { + scopeNode = scopeNode.body; + } // Detect Usage before Init + + + let stmts = scopeNode.body; + + if (!Array.isArray(stmts)) { + stmts = [stmts]; + } + + const compareResult = compareBindingAndReference({ + binding, + refPath, + stmts + }); + + 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") { + return { + shouldDeopt: true + }; + } + } + } + + return { + confident: false, + shouldDeopt: false + }; +} + +function compareBindingAndReference({ + binding, + refPath, + stmts +}) { + const state = { + binding: null, + reference: null + }; + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; + + try { + for (var _iterator = stmts.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + const _step$value = _slicedToArray(_step.value, 2), + idx = _step$value[0], + stmt = _step$value[1]; + + if (isAncestor(stmt, binding.path)) { + state.binding = { + idx + }; + } + + var _iteratorNormalCompletion2 = true; + var _didIteratorError2 = false; + var _iteratorError2 = undefined; + + try { + for (var _iterator2 = binding.referencePaths[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { + const ref = _step2.value; + + if (ref === refPath && isAncestor(stmt, ref)) { + state.reference = { + idx, + scope: binding.path.scope === ref.scope ? "current" : "other" + }; + break; + } + } + } catch (err) { + _didIteratorError2 = true; + _iteratorError2 = err; + } finally { + try { + if (!_iteratorNormalCompletion2 && _iterator2.return != null) { + _iterator2.return(); + } + } finally { + if (_didIteratorError2) { + throw _iteratorError2; + } + } + } + } + } catch (err) { + _didIteratorError = true; + _iteratorError = err; + } finally { + try { + if (!_iteratorNormalCompletion && _iterator.return != null) { + _iterator.return(); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } + } + } + + return state; +} + +function deopt(deoptPath) { + return { + confident: false, + deoptPath + }; +} +/** + * is nodeParent an ancestor of path + */ + + +function isAncestor(nodeParent, path) { + return !!path.findParent(parent => parent.node === nodeParent); +}
\ No newline at end of file diff --git a/node_modules/babel-helper-evaluate-path/package.json b/node_modules/babel-helper-evaluate-path/package.json new file mode 100644 index 00000000..9805298e --- /dev/null +++ b/node_modules/babel-helper-evaluate-path/package.json @@ -0,0 +1,16 @@ +{ + "name": "babel-helper-evaluate-path", + "version": "0.5.0", + "description": "path.evaluate wrapped in a try catch", + "keywords": [ + "babel-minify", + "babel-plugin" + ], + "homepage": "https://github.com/babel/minify#readme", + "bugs": "https://github.com/babel/minify/issues", + "license": "MIT", + "author": "boopathi", + "main": "lib/index.js", + "repository": "https://github.com/babel/minify/tree/master/packages/babel-helper-evaluate-path", + "gitHead": "4de390008da4a486b37819109d2021a0957ad405" +} |
