@babel/traverse
Advanced tools
Comparing version 7.0.0-beta.43 to 7.0.0-beta.44
@@ -10,5 +10,5 @@ "use strict"; | ||
exports.scope = exports.path = void 0; | ||
let path = new WeakMap(); | ||
var path = new WeakMap(); | ||
exports.path = path; | ||
let scope = new WeakMap(); | ||
var scope = new WeakMap(); | ||
exports.scope = scope; | ||
@@ -15,0 +15,0 @@ |
@@ -8,8 +8,8 @@ "use strict"; | ||
var _path = _interopRequireDefault(require("./path")); | ||
var _path2 = _interopRequireDefault(require("./path")); | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -25,6 +25,6 @@ }; | ||
const testing = process.env.NODE_ENV === "test"; | ||
var testing = process.env.NODE_ENV === "test"; | ||
class TraversalContext { | ||
constructor(scope, opts, state, parentPath) { | ||
var TraversalContext = function () { | ||
function TraversalContext(scope, opts, state, parentPath) { | ||
this.queue = null; | ||
@@ -37,10 +37,24 @@ this.parentPath = parentPath; | ||
shouldVisit(node) { | ||
const opts = this.opts; | ||
var _proto = TraversalContext.prototype; | ||
_proto.shouldVisit = function shouldVisit(node) { | ||
var opts = this.opts; | ||
if (opts.enter || opts.exit) return true; | ||
if (opts[node.type]) return true; | ||
const keys = t().VISITOR_KEYS[node.type]; | ||
var keys = t().VISITOR_KEYS[node.type]; | ||
if (!keys || !keys.length) return false; | ||
for (const key of keys) { | ||
for (var _iterator = keys, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var key = _ref; | ||
if (node[key]) return true; | ||
@@ -50,6 +64,6 @@ } | ||
return false; | ||
} | ||
}; | ||
create(node, obj, key, listKey) { | ||
return _path.default.get({ | ||
_proto.create = function create(node, obj, key, listKey) { | ||
return _path2.default.get({ | ||
parentPath: this.parentPath, | ||
@@ -59,7 +73,7 @@ parent: node, | ||
key: key, | ||
listKey | ||
listKey: listKey | ||
}); | ||
} | ||
}; | ||
maybeQueue(path, notPriority) { | ||
_proto.maybeQueue = function maybeQueue(path, notPriority) { | ||
if (this.trap) { | ||
@@ -76,10 +90,10 @@ throw new Error("Infinite cycle detected"); | ||
} | ||
} | ||
}; | ||
visitMultiple(container, parent, listKey) { | ||
_proto.visitMultiple = function visitMultiple(container, parent, listKey) { | ||
if (container.length === 0) return false; | ||
const queue = []; | ||
var queue = []; | ||
for (let key = 0; key < container.length; key++) { | ||
const node = container[key]; | ||
for (var key = 0; key < container.length; key++) { | ||
var node = container[key]; | ||
@@ -92,5 +106,5 @@ if (node && this.shouldVisit(node)) { | ||
return this.visitQueue(queue); | ||
} | ||
}; | ||
visitSingle(node, key) { | ||
_proto.visitSingle = function visitSingle(node, key) { | ||
if (this.shouldVisit(node[key])) { | ||
@@ -101,11 +115,23 @@ return this.visitQueue([this.create(node, node, key)]); | ||
} | ||
} | ||
}; | ||
visitQueue(queue) { | ||
_proto.visitQueue = function visitQueue(queue) { | ||
this.queue = queue; | ||
this.priorityQueue = []; | ||
const visited = []; | ||
let stop = false; | ||
var visited = []; | ||
var stop = false; | ||
for (const path of queue) { | ||
for (var _iterator2 = queue, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { | ||
var _ref2; | ||
if (_isArray2) { | ||
if (_i2 >= _iterator2.length) break; | ||
_ref2 = _iterator2[_i2++]; | ||
} else { | ||
_i2 = _iterator2.next(); | ||
if (_i2.done) break; | ||
_ref2 = _i2.value; | ||
} | ||
var path = _ref2; | ||
path.resync(); | ||
@@ -139,4 +165,17 @@ | ||
for (const path of queue) { | ||
path.popContext(); | ||
for (var _iterator3 = queue, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) { | ||
var _ref3; | ||
if (_isArray3) { | ||
if (_i3 >= _iterator3.length) break; | ||
_ref3 = _iterator3[_i3++]; | ||
} else { | ||
_i3 = _iterator3.next(); | ||
if (_i3.done) break; | ||
_ref3 = _i3.value; | ||
} | ||
var _path = _ref3; | ||
_path.popContext(); | ||
} | ||
@@ -146,6 +185,6 @@ | ||
return stop; | ||
} | ||
}; | ||
visit(node, key) { | ||
const nodes = node[key]; | ||
_proto.visit = function visit(node, key) { | ||
var nodes = node[key]; | ||
if (!nodes) return false; | ||
@@ -158,6 +197,7 @@ | ||
} | ||
} | ||
}; | ||
} | ||
return TraversalContext; | ||
}(); | ||
exports.default = TraversalContext; |
@@ -8,9 +8,6 @@ "use strict"; | ||
class Hub { | ||
constructor(file) { | ||
this.file = file; | ||
} | ||
var Hub = function Hub(file) { | ||
this.file = file; | ||
}; | ||
} | ||
exports.default = Hub; |
@@ -9,3 +9,3 @@ "use strict"; | ||
enumerable: true, | ||
get: function () { | ||
get: function get() { | ||
return _path.default; | ||
@@ -16,3 +16,3 @@ } | ||
enumerable: true, | ||
get: function () { | ||
get: function get() { | ||
return _scope.default; | ||
@@ -23,3 +23,3 @@ } | ||
enumerable: true, | ||
get: function () { | ||
get: function get() { | ||
return _hub.default; | ||
@@ -37,5 +37,5 @@ } | ||
function _includes() { | ||
const data = _interopRequireDefault(require("lodash/includes")); | ||
var data = _interopRequireDefault(require("lodash/includes")); | ||
_includes = function () { | ||
_includes = function _includes() { | ||
return data; | ||
@@ -48,5 +48,5 @@ }; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -76,3 +76,3 @@ }; | ||
if (parent.type !== "Program" && parent.type !== "File") { | ||
throw new Error("You must pass a scope and parentPath unless traversing a Program/File. " + `Instead of that you tried to traverse a ${parent.type} node without ` + "passing scope and parentPath."); | ||
throw new Error("You must pass a scope and parentPath unless traversing a Program/File. " + ("Instead of that you tried to traverse a " + parent.type + " node without ") + "passing scope and parentPath."); | ||
} | ||
@@ -94,7 +94,19 @@ } | ||
traverse.node = function (node, opts, scope, state, parentPath, skipKeys) { | ||
const keys = t().VISITOR_KEYS[node.type]; | ||
var keys = t().VISITOR_KEYS[node.type]; | ||
if (!keys) return; | ||
const context = new _context.default(scope, opts, state, parentPath); | ||
var context = new _context.default(scope, opts, state, parentPath); | ||
for (const key of keys) { | ||
for (var _iterator = keys, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var key = _ref; | ||
if (skipKeys && skipKeys[key]) continue; | ||
@@ -125,3 +137,3 @@ if (context.visit(node, key)) return; | ||
if (tree.type === type) return true; | ||
const state = { | ||
var state = { | ||
has: false, | ||
@@ -128,0 +140,0 @@ type: type |
@@ -18,5 +18,5 @@ "use strict"; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -35,3 +35,3 @@ }; | ||
function findParent(callback) { | ||
let path = this; | ||
var path = this; | ||
@@ -46,3 +46,3 @@ while (path = path.parentPath) { | ||
function find(callback) { | ||
let path = this; | ||
var path = this; | ||
@@ -57,7 +57,9 @@ do { | ||
function getFunctionParent() { | ||
return this.findParent(p => p.isFunction()); | ||
return this.findParent(function (p) { | ||
return p.isFunction(); | ||
}); | ||
} | ||
function getStatementParent() { | ||
let path = this; | ||
var path = this; | ||
@@ -81,7 +83,9 @@ do { | ||
return this.getDeepestCommonAncestorFrom(paths, function (deepest, i, ancestries) { | ||
let earliest; | ||
const keys = t().VISITOR_KEYS[deepest.type]; | ||
var earliest; | ||
var keys = t().VISITOR_KEYS[deepest.type]; | ||
var _arr = ancestries; | ||
for (const ancestry of ancestries) { | ||
const path = ancestry[i + 1]; | ||
for (var _i = 0; _i < _arr.length; _i++) { | ||
var ancestry = _arr[_i]; | ||
var path = ancestry[i + 1]; | ||
@@ -100,4 +104,4 @@ if (!earliest) { | ||
const earliestKeyIndex = keys.indexOf(earliest.parentKey); | ||
const currentKeyIndex = keys.indexOf(path.parentKey); | ||
var earliestKeyIndex = keys.indexOf(earliest.parentKey); | ||
var currentKeyIndex = keys.indexOf(path.parentKey); | ||
@@ -114,2 +118,4 @@ if (earliestKeyIndex > currentKeyIndex) { | ||
function getDeepestCommonAncestorFrom(paths, filter) { | ||
var _this = this; | ||
if (!paths.length) { | ||
@@ -123,10 +129,10 @@ return this; | ||
let minDepth = Infinity; | ||
let lastCommonIndex, lastCommon; | ||
const ancestries = paths.map(path => { | ||
const ancestry = []; | ||
var minDepth = Infinity; | ||
var lastCommonIndex, lastCommon; | ||
var ancestries = paths.map(function (path) { | ||
var ancestry = []; | ||
do { | ||
ancestry.unshift(path); | ||
} while ((path = path.parentPath) && path !== this); | ||
} while ((path = path.parentPath) && path !== _this); | ||
@@ -139,8 +145,11 @@ if (ancestry.length < minDepth) { | ||
}); | ||
const first = ancestries[0]; | ||
var first = ancestries[0]; | ||
depthLoop: for (let i = 0; i < minDepth; i++) { | ||
const shouldMatch = first[i]; | ||
depthLoop: for (var i = 0; i < minDepth; i++) { | ||
var shouldMatch = first[i]; | ||
var _arr2 = ancestries; | ||
for (const ancestry of ancestries) { | ||
for (var _i2 = 0; _i2 < _arr2.length; _i2++) { | ||
var ancestry = _arr2[_i2]; | ||
if (ancestry[i] !== shouldMatch) { | ||
@@ -167,4 +176,4 @@ break depthLoop; | ||
function getAncestry() { | ||
let path = this; | ||
const paths = []; | ||
var path = this; | ||
var paths = []; | ||
@@ -183,10 +192,15 @@ do { | ||
function isDescendant(maybeAncestor) { | ||
return !!this.findParent(parent => parent === maybeAncestor); | ||
return !!this.findParent(function (parent) { | ||
return parent === maybeAncestor; | ||
}); | ||
} | ||
function inType() { | ||
let path = this; | ||
var path = this; | ||
while (path) { | ||
for (const type of arguments) { | ||
var _arr3 = arguments; | ||
for (var _i3 = 0; _i3 < _arr3.length; _i3++) { | ||
var type = _arr3[_i3]; | ||
if (path.node.type === type) return true; | ||
@@ -193,0 +207,0 @@ } |
@@ -11,5 +11,5 @@ "use strict"; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -25,11 +25,11 @@ }; | ||
if (typeof this.key === "string") return; | ||
const node = this.node; | ||
var node = this.node; | ||
if (!node) return; | ||
const trailing = node.trailingComments; | ||
const leading = node.leadingComments; | ||
var trailing = node.trailingComments; | ||
var leading = node.leadingComments; | ||
if (!trailing && !leading) return; | ||
const prev = this.getSibling(this.key - 1); | ||
const next = this.getSibling(this.key + 1); | ||
const hasPrev = Boolean(prev.node); | ||
const hasNext = Boolean(next.node); | ||
var prev = this.getSibling(this.key - 1); | ||
var next = this.getSibling(this.key + 1); | ||
var hasPrev = Boolean(prev.node); | ||
var hasNext = Boolean(next.node); | ||
@@ -36,0 +36,0 @@ if (hasPrev && hasNext) {} else if (hasPrev) { |
@@ -32,3 +32,3 @@ "use strict"; | ||
function call(key) { | ||
const opts = this.opts; | ||
var opts = this.opts; | ||
this.debug(key); | ||
@@ -50,14 +50,26 @@ | ||
for (const fn of fns) { | ||
for (var _iterator = fns, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var fn = _ref; | ||
if (!fn) continue; | ||
const node = this.node; | ||
var node = this.node; | ||
if (!node) return true; | ||
const ret = fn.call(this.state, this, this.state); | ||
var ret = fn.call(this.state, this, this.state); | ||
if (ret && typeof ret === "object" && typeof ret.then === "function") { | ||
throw new Error(`You appear to be using a plugin with an async traversal visitor, ` + `which your current version of Babel does not support.` + `If you're using a published plugin, you may need to upgrade ` + `your @babel/core version.`); | ||
throw new Error("You appear to be using a plugin with an async traversal visitor, " + "which your current version of Babel does not support." + "If you're using a published plugin, you may need to upgrade " + "your @babel/core version."); | ||
} | ||
if (ret) { | ||
throw new Error(`Unexpected return value from visitor method ${fn}`); | ||
throw new Error("Unexpected return value from visitor method " + fn); | ||
} | ||
@@ -73,3 +85,3 @@ | ||
function isBlacklisted() { | ||
const blacklist = this.opts.blacklist; | ||
var blacklist = this.opts.blacklist; | ||
return blacklist && blacklist.indexOf(this.node.type) > -1; | ||
@@ -119,4 +131,4 @@ } | ||
if (this.opts && this.opts.noScope) return; | ||
let path = this.parentPath; | ||
let target; | ||
var path = this.parentPath; | ||
var target; | ||
@@ -170,3 +182,3 @@ while (path && !target) { | ||
if (Array.isArray(this.container)) { | ||
for (let i = 0; i < this.container.length; i++) { | ||
for (var i = 0; i < this.container.length; i++) { | ||
if (this.container[i] === this.node) { | ||
@@ -177,3 +189,3 @@ return this.setKey(i); | ||
} else { | ||
for (const key in this.container) { | ||
for (var key in this.container) { | ||
if (this.container[key] === this.node) { | ||
@@ -190,3 +202,3 @@ return this.setKey(key); | ||
if (!this.parent || !this.inList) return; | ||
const newContainer = this.parent[this.listKey]; | ||
var newContainer = this.parent[this.listKey]; | ||
if (this.container === newContainer) return; | ||
@@ -232,7 +244,23 @@ this.container = newContainer || null; | ||
function requeue(pathToQueue = this) { | ||
function requeue(pathToQueue) { | ||
if (pathToQueue === void 0) { | ||
pathToQueue = this; | ||
} | ||
if (pathToQueue.removed) return; | ||
const contexts = this.contexts; | ||
var contexts = this.contexts; | ||
for (const context of contexts) { | ||
for (var _iterator2 = contexts, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { | ||
var _ref2; | ||
if (_isArray2) { | ||
if (_i2 >= _iterator2.length) break; | ||
_ref2 = _iterator2[_i2++]; | ||
} else { | ||
_i2 = _iterator2.next(); | ||
if (_i2.done) break; | ||
_ref2 = _i2.value; | ||
} | ||
var context = _ref2; | ||
context.maybeQueue(pathToQueue); | ||
@@ -243,4 +271,4 @@ } | ||
function _getQueueContexts() { | ||
let path = this; | ||
let contexts = this.contexts; | ||
var path = this; | ||
var contexts = this.contexts; | ||
@@ -247,0 +275,0 @@ while (!contexts.length) { |
@@ -13,5 +13,5 @@ "use strict"; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -24,5 +24,5 @@ }; | ||
function _helperFunctionName() { | ||
const data = _interopRequireDefault(require("@babel/helper-function-name")); | ||
var data = _interopRequireDefault(require("@babel/helper-function-name")); | ||
_helperFunctionName = function () { | ||
_helperFunctionName = function _helperFunctionName() { | ||
return data; | ||
@@ -39,4 +39,4 @@ }; | ||
function toComputedKey() { | ||
const node = this.node; | ||
let key; | ||
var node = this.node; | ||
var key; | ||
@@ -59,4 +59,4 @@ if (this.isMemberExpression()) { | ||
function ensureBlock() { | ||
const body = this.get("body"); | ||
const bodyNode = body.node; | ||
var body = this.get("body"); | ||
var bodyNode = body.node; | ||
@@ -75,6 +75,6 @@ if (Array.isArray(body)) { | ||
const statements = []; | ||
let stringPath = "body"; | ||
let key; | ||
let listKey; | ||
var statements = []; | ||
var stringPath = "body"; | ||
var key; | ||
var listKey; | ||
@@ -98,3 +98,3 @@ if (body.isStatement()) { | ||
this.node.body = t().blockStatement(statements); | ||
const parentPath = this.get(stringPath); | ||
var parentPath = this.get(stringPath); | ||
body.setup(parentPath, listKey ? parentPath.node[listKey] : parentPath.node, listKey, key); | ||
@@ -117,6 +117,9 @@ return this.node; | ||
function arrowFunctionToExpression({ | ||
allowInsertArrow = true, | ||
specCompliant = false | ||
} = {}) { | ||
function arrowFunctionToExpression(_temp) { | ||
var _ref = _temp === void 0 ? {} : _temp, | ||
_ref$allowInsertArrow = _ref.allowInsertArrow, | ||
allowInsertArrow = _ref$allowInsertArrow === void 0 ? true : _ref$allowInsertArrow, | ||
_ref$specCompliant = _ref.specCompliant, | ||
specCompliant = _ref$specCompliant === void 0 ? false : _ref$specCompliant; | ||
if (!this.isArrowFunctionExpression()) { | ||
@@ -126,3 +129,3 @@ throw this.buildCodeFrameError("Cannot convert non-arrow function to a function expression."); | ||
const thisBinding = hoistFunctionEnvironment(this, specCompliant, allowInsertArrow); | ||
var thisBinding = hoistFunctionEnvironment(this, specCompliant, allowInsertArrow); | ||
this.ensureBlock(); | ||
@@ -132,3 +135,3 @@ this.node.type = "FunctionExpression"; | ||
if (specCompliant) { | ||
const checkBinding = thisBinding ? null : this.parentPath.scope.generateUidIdentifier("arrowCheckId"); | ||
var checkBinding = thisBinding ? null : this.parentPath.scope.generateUidIdentifier("arrowCheckId"); | ||
@@ -147,8 +150,18 @@ if (checkBinding) { | ||
function hoistFunctionEnvironment(fnPath, specCompliant = false, allowInsertArrow = true) { | ||
const thisEnvFn = fnPath.findParent(p => p.isFunction() && !p.isArrowFunctionExpression() || p.isProgram() || p.isClassProperty({ | ||
static: false | ||
})); | ||
const inConstructor = thisEnvFn && thisEnvFn.node.kind === "constructor"; | ||
function hoistFunctionEnvironment(fnPath, specCompliant, allowInsertArrow) { | ||
if (specCompliant === void 0) { | ||
specCompliant = false; | ||
} | ||
if (allowInsertArrow === void 0) { | ||
allowInsertArrow = true; | ||
} | ||
var thisEnvFn = fnPath.findParent(function (p) { | ||
return p.isFunction() && !p.isArrowFunctionExpression() || p.isProgram() || p.isClassProperty({ | ||
static: false | ||
}); | ||
}); | ||
var inConstructor = thisEnvFn && thisEnvFn.node.kind === "constructor"; | ||
if (thisEnvFn.isClassProperty()) { | ||
@@ -158,9 +171,8 @@ throw fnPath.buildCodeFrameError("Unable to transform arrow inside class property"); | ||
const { | ||
thisPaths, | ||
argumentsPaths, | ||
newTargetPaths, | ||
superProps, | ||
superCalls | ||
} = getScopeInformation(fnPath); | ||
var _getScopeInformation = getScopeInformation(fnPath), | ||
thisPaths = _getScopeInformation.thisPaths, | ||
argumentsPaths = _getScopeInformation.argumentsPaths, | ||
newTargetPaths = _getScopeInformation.newTargetPaths, | ||
superProps = _getScopeInformation.superProps, | ||
superCalls = _getScopeInformation.superCalls; | ||
@@ -172,23 +184,20 @@ if (inConstructor && superCalls.length > 0) { | ||
const allSuperCalls = []; | ||
var allSuperCalls = []; | ||
thisEnvFn.traverse({ | ||
Function(child) { | ||
Function: function Function(child) { | ||
if (child.isArrowFunctionExpression()) return; | ||
child.skip(); | ||
}, | ||
ClassProperty(child) { | ||
ClassProperty: function ClassProperty(child) { | ||
if (child.node.static) return; | ||
child.skip(); | ||
}, | ||
CallExpression(child) { | ||
CallExpression: function CallExpression(child) { | ||
if (!child.get("callee").isSuper()) return; | ||
allSuperCalls.push(child); | ||
} | ||
}); | ||
const superBinding = getSuperBinding(thisEnvFn); | ||
allSuperCalls.forEach(superCall => { | ||
const callee = t().identifier(superBinding); | ||
var superBinding = getSuperBinding(thisEnvFn); | ||
allSuperCalls.forEach(function (superCall) { | ||
var callee = t().identifier(superBinding); | ||
callee.loc = superCall.node.callee.loc; | ||
@@ -199,3 +208,3 @@ superCall.get("callee").replaceWith(callee); | ||
let thisBinding; | ||
var thisBinding; | ||
@@ -206,4 +215,4 @@ if (thisPaths.length > 0 || specCompliant) { | ||
if (!specCompliant || inConstructor && hasSuperClass(thisEnvFn)) { | ||
thisPaths.forEach(thisChild => { | ||
const thisRef = thisChild.isJSX() ? t().jsxIdentifier(thisBinding) : t().identifier(thisBinding); | ||
thisPaths.forEach(function (thisChild) { | ||
var thisRef = thisChild.isJSX() ? t().jsxIdentifier(thisBinding) : t().identifier(thisBinding); | ||
thisRef.loc = thisChild.node.loc; | ||
@@ -217,5 +226,7 @@ thisChild.replaceWith(thisRef); | ||
if (argumentsPaths.length > 0) { | ||
const argumentsBinding = getBinding(thisEnvFn, "arguments", () => t().identifier("arguments")); | ||
argumentsPaths.forEach(argumentsChild => { | ||
const argsRef = t().identifier(argumentsBinding); | ||
var argumentsBinding = getBinding(thisEnvFn, "arguments", function () { | ||
return t().identifier("arguments"); | ||
}); | ||
argumentsPaths.forEach(function (argumentsChild) { | ||
var argsRef = t().identifier(argumentsBinding); | ||
argsRef.loc = argumentsChild.node.loc; | ||
@@ -227,5 +238,7 @@ argumentsChild.replaceWith(argsRef); | ||
if (newTargetPaths.length > 0) { | ||
const newTargetBinding = getBinding(thisEnvFn, "newtarget", () => t().metaProperty(t().identifier("new"), t().identifier("target"))); | ||
newTargetPaths.forEach(targetChild => { | ||
const targetRef = t().identifier(newTargetBinding); | ||
var newTargetBinding = getBinding(thisEnvFn, "newtarget", function () { | ||
return t().metaProperty(t().identifier("new"), t().identifier("target")); | ||
}); | ||
newTargetPaths.forEach(function (targetChild) { | ||
var targetRef = t().identifier(newTargetBinding); | ||
targetRef.loc = targetChild.node.loc; | ||
@@ -241,5 +254,7 @@ targetChild.replaceWith(targetRef); | ||
const flatSuperProps = superProps.reduce((acc, superProp) => acc.concat(standardizeSuperProperty(superProp)), []); | ||
flatSuperProps.forEach(superProp => { | ||
const key = superProp.node.computed ? "" : superProp.get("property").node.name; | ||
var flatSuperProps = superProps.reduce(function (acc, superProp) { | ||
return acc.concat(standardizeSuperProperty(superProp)); | ||
}, []); | ||
flatSuperProps.forEach(function (superProp) { | ||
var key = superProp.node.computed ? "" : superProp.get("property").node.name; | ||
@@ -249,18 +264,20 @@ if (superProp.parentPath.isCallExpression({ | ||
})) { | ||
const superBinding = getSuperPropCallBinding(thisEnvFn, key); | ||
var _superBinding = getSuperPropCallBinding(thisEnvFn, key); | ||
if (superProp.node.computed) { | ||
const prop = superProp.get("property").node; | ||
superProp.replaceWith(t().identifier(superBinding)); | ||
var prop = superProp.get("property").node; | ||
superProp.replaceWith(t().identifier(_superBinding)); | ||
superProp.parentPath.node.arguments.unshift(prop); | ||
} else { | ||
superProp.replaceWith(t().identifier(superBinding)); | ||
superProp.replaceWith(t().identifier(_superBinding)); | ||
} | ||
} else { | ||
const isAssignment = superProp.parentPath.isAssignmentExpression({ | ||
var isAssignment = superProp.parentPath.isAssignmentExpression({ | ||
left: superProp.node | ||
}); | ||
const superBinding = getSuperPropBinding(thisEnvFn, isAssignment, key); | ||
const args = []; | ||
var _superBinding2 = getSuperPropBinding(thisEnvFn, isAssignment, key); | ||
var args = []; | ||
if (superProp.node.computed) { | ||
@@ -271,7 +288,7 @@ args.push(superProp.get("property").node); | ||
if (isAssignment) { | ||
const value = superProp.parentPath.node.right; | ||
var value = superProp.parentPath.node.right; | ||
args.push(value); | ||
superProp.parentPath.replaceWith(t().callExpression(t().identifier(superBinding), args)); | ||
superProp.parentPath.replaceWith(t().callExpression(t().identifier(_superBinding2), args)); | ||
} else { | ||
superProp.replaceWith(t().callExpression(t().identifier(superBinding), args)); | ||
superProp.replaceWith(t().callExpression(t().identifier(_superBinding2), args)); | ||
} | ||
@@ -287,9 +304,9 @@ } | ||
if (superProp.parentPath.isAssignmentExpression() && superProp.parentPath.node.operator !== "=") { | ||
const assignmentPath = superProp.parentPath; | ||
const op = assignmentPath.node.operator.slice(0, -1); | ||
const value = assignmentPath.node.right; | ||
var assignmentPath = superProp.parentPath; | ||
var op = assignmentPath.node.operator.slice(0, -1); | ||
var value = assignmentPath.node.right; | ||
assignmentPath.node.operator = "="; | ||
if (superProp.node.computed) { | ||
const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp"); | ||
var tmp = superProp.scope.generateDeclaredUidIdentifier("tmp"); | ||
assignmentPath.get("left").replaceWith(t().memberExpression(superProp.node.object, t().assignmentExpression("=", tmp, superProp.node.property), true)); | ||
@@ -304,14 +321,16 @@ assignmentPath.get("right").replaceWith(t().binaryExpression(op, t().memberExpression(superProp.node.object, t().identifier(tmp.name), true), value)); | ||
} else if (superProp.parentPath.isUpdateExpression()) { | ||
const updateExpr = superProp.parentPath; | ||
const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp"); | ||
const computedKey = superProp.node.computed ? superProp.scope.generateDeclaredUidIdentifier("prop") : null; | ||
const parts = [t().assignmentExpression("=", tmp, t().memberExpression(superProp.node.object, computedKey ? t().assignmentExpression("=", computedKey, superProp.node.property) : superProp.node.property, superProp.node.computed)), t().assignmentExpression("=", t().memberExpression(superProp.node.object, computedKey ? t().identifier(computedKey.name) : superProp.node.property, superProp.node.computed), t().binaryExpression("+", t().identifier(tmp.name), t().numericLiteral(1)))]; | ||
var updateExpr = superProp.parentPath; | ||
var _tmp = superProp.scope.generateDeclaredUidIdentifier("tmp"); | ||
var computedKey = superProp.node.computed ? superProp.scope.generateDeclaredUidIdentifier("prop") : null; | ||
var parts = [t().assignmentExpression("=", _tmp, t().memberExpression(superProp.node.object, computedKey ? t().assignmentExpression("=", computedKey, superProp.node.property) : superProp.node.property, superProp.node.computed)), t().assignmentExpression("=", t().memberExpression(superProp.node.object, computedKey ? t().identifier(computedKey.name) : superProp.node.property, superProp.node.computed), t().binaryExpression("+", t().identifier(_tmp.name), t().numericLiteral(1)))]; | ||
if (!superProp.parentPath.node.prefix) { | ||
parts.push(t().identifier(tmp.name)); | ||
parts.push(t().identifier(_tmp.name)); | ||
} | ||
updateExpr.replaceWith(t().sequenceExpression(parts)); | ||
const left = updateExpr.get("expressions.0.right"); | ||
const right = updateExpr.get("expressions.1.left"); | ||
var left = updateExpr.get("expressions.0.right"); | ||
var right = updateExpr.get("expressions.1.left"); | ||
return [left, right]; | ||
@@ -328,17 +347,15 @@ } | ||
function getThisBinding(thisEnvFn, inConstructor) { | ||
return getBinding(thisEnvFn, "this", thisBinding => { | ||
return getBinding(thisEnvFn, "this", function (thisBinding) { | ||
if (!inConstructor || !hasSuperClass(thisEnvFn)) return t().thisExpression(); | ||
const supers = new WeakSet(); | ||
var supers = new WeakSet(); | ||
thisEnvFn.traverse({ | ||
Function(child) { | ||
Function: function Function(child) { | ||
if (child.isArrowFunctionExpression()) return; | ||
child.skip(); | ||
}, | ||
ClassProperty(child) { | ||
ClassProperty: function ClassProperty(child) { | ||
if (child.node.static) return; | ||
child.skip(); | ||
}, | ||
CallExpression(child) { | ||
CallExpression: function CallExpression(child) { | ||
if (!child.get("callee").isSuper()) return; | ||
@@ -349,3 +366,2 @@ if (supers.has(child.node)) return; | ||
} | ||
}); | ||
@@ -356,4 +372,4 @@ }); | ||
function getSuperBinding(thisEnvFn) { | ||
return getBinding(thisEnvFn, "supercall", () => { | ||
const argsBinding = thisEnvFn.scope.generateUidIdentifier("args"); | ||
return getBinding(thisEnvFn, "supercall", function () { | ||
var argsBinding = thisEnvFn.scope.generateUidIdentifier("args"); | ||
return t().arrowFunctionExpression([t().restElement(argsBinding)], t().callExpression(t().super(), [t().spreadElement(t().identifier(argsBinding.name))])); | ||
@@ -364,6 +380,6 @@ }); | ||
function getSuperPropCallBinding(thisEnvFn, propName) { | ||
return getBinding(thisEnvFn, `superprop_call:${propName || ""}`, () => { | ||
const argsBinding = thisEnvFn.scope.generateUidIdentifier("args"); | ||
const argsList = [t().restElement(argsBinding)]; | ||
let fnBody; | ||
return getBinding(thisEnvFn, "superprop_call:" + (propName || ""), function () { | ||
var argsBinding = thisEnvFn.scope.generateUidIdentifier("args"); | ||
var argsList = [t().restElement(argsBinding)]; | ||
var fnBody; | ||
@@ -373,3 +389,3 @@ if (propName) { | ||
} else { | ||
const method = thisEnvFn.scope.generateUidIdentifier("prop"); | ||
var method = thisEnvFn.scope.generateUidIdentifier("prop"); | ||
argsList.unshift(method); | ||
@@ -384,6 +400,6 @@ fnBody = t().callExpression(t().memberExpression(t().super(), t().identifier(method.name), true), [t().spreadElement(t().identifier(argsBinding.name))]); | ||
function getSuperPropBinding(thisEnvFn, isAssignment, propName) { | ||
const op = isAssignment ? "set" : "get"; | ||
return getBinding(thisEnvFn, `superprop_${op}:${propName || ""}`, () => { | ||
const argsList = []; | ||
let fnBody; | ||
var op = isAssignment ? "set" : "get"; | ||
return getBinding(thisEnvFn, "superprop_" + op + ":" + (propName || ""), function () { | ||
var argsList = []; | ||
var fnBody; | ||
@@ -393,3 +409,3 @@ if (propName) { | ||
} else { | ||
const method = thisEnvFn.scope.generateUidIdentifier("prop"); | ||
var method = thisEnvFn.scope.generateUidIdentifier("prop"); | ||
argsList.unshift(method); | ||
@@ -400,3 +416,3 @@ fnBody = t().memberExpression(t().super(), t().identifier(method.name), true); | ||
if (isAssignment) { | ||
const valueIdent = thisEnvFn.scope.generateUidIdentifier("value"); | ||
var valueIdent = thisEnvFn.scope.generateUidIdentifier("value"); | ||
argsList.push(valueIdent); | ||
@@ -411,7 +427,7 @@ fnBody = t().assignmentExpression("=", fnBody, t().identifier(valueIdent.name)); | ||
function getBinding(thisEnvFn, key, init) { | ||
const cacheKey = "binding:" + key; | ||
let data = thisEnvFn.getData(cacheKey); | ||
var cacheKey = "binding:" + key; | ||
var data = thisEnvFn.getData(cacheKey); | ||
if (!data) { | ||
const id = thisEnvFn.scope.generateUidIdentifier(key); | ||
var id = thisEnvFn.scope.generateUidIdentifier(key); | ||
data = id.name; | ||
@@ -429,23 +445,20 @@ thisEnvFn.setData(cacheKey, data); | ||
function getScopeInformation(fnPath) { | ||
const thisPaths = []; | ||
const argumentsPaths = []; | ||
const newTargetPaths = []; | ||
const superProps = []; | ||
const superCalls = []; | ||
var thisPaths = []; | ||
var argumentsPaths = []; | ||
var newTargetPaths = []; | ||
var superProps = []; | ||
var superCalls = []; | ||
fnPath.traverse({ | ||
ClassProperty(child) { | ||
ClassProperty: function ClassProperty(child) { | ||
if (child.node.static) return; | ||
child.skip(); | ||
}, | ||
Function(child) { | ||
Function: function Function(child) { | ||
if (child.isArrowFunctionExpression()) return; | ||
child.skip(); | ||
}, | ||
ThisExpression(child) { | ||
ThisExpression: function ThisExpression(child) { | ||
thisPaths.push(child); | ||
}, | ||
JSXIdentifier(child) { | ||
JSXIdentifier: function JSXIdentifier(child) { | ||
if (child.node.name !== "this") return; | ||
@@ -463,17 +476,13 @@ | ||
}, | ||
CallExpression(child) { | ||
CallExpression: function CallExpression(child) { | ||
if (child.get("callee").isSuper()) superCalls.push(child); | ||
}, | ||
MemberExpression(child) { | ||
MemberExpression: function MemberExpression(child) { | ||
if (child.get("object").isSuper()) superProps.push(child); | ||
}, | ||
ReferencedIdentifier(child) { | ||
ReferencedIdentifier: function ReferencedIdentifier(child) { | ||
if (child.node.name !== "arguments") return; | ||
argumentsPaths.push(child); | ||
}, | ||
MetaProperty(child) { | ||
MetaProperty: function MetaProperty(child) { | ||
if (!child.get("meta").isIdentifier({ | ||
@@ -487,11 +496,10 @@ name: "new" | ||
} | ||
}); | ||
return { | ||
thisPaths, | ||
argumentsPaths, | ||
newTargetPaths, | ||
superProps, | ||
superCalls | ||
thisPaths: thisPaths, | ||
argumentsPaths: argumentsPaths, | ||
newTargetPaths: newTargetPaths, | ||
superProps: superProps, | ||
superCalls: superCalls | ||
}; | ||
} |
@@ -8,7 +8,7 @@ "use strict"; | ||
exports.evaluate = evaluate; | ||
const VALID_CALLEES = ["String", "Number", "Math"]; | ||
const INVALID_METHODS = ["random"]; | ||
var VALID_CALLEES = ["String", "Number", "Math"]; | ||
var INVALID_METHODS = ["random"]; | ||
function evaluateTruthy() { | ||
const res = this.evaluate(); | ||
var res = this.evaluate(); | ||
if (res.confident) return !!res.value; | ||
@@ -24,11 +24,7 @@ } | ||
function evaluateCached(path, state) { | ||
const { | ||
node | ||
} = path; | ||
const { | ||
seen | ||
} = state; | ||
var node = path.node; | ||
var seen = state.seen; | ||
if (seen.has(node)) { | ||
const existing = seen.get(node); | ||
var existing = seen.get(node); | ||
@@ -42,3 +38,3 @@ if (existing.resolved) { | ||
} else { | ||
const item = { | ||
var item = { | ||
resolved: false | ||
@@ -48,3 +44,3 @@ }; | ||
const val = _evaluate(path, state); | ||
var val = _evaluate(path, state); | ||
@@ -62,8 +58,6 @@ if (state.confident) { | ||
if (!state.confident) return; | ||
const { | ||
node | ||
} = path; | ||
var node = path.node; | ||
if (path.isSequenceExpression()) { | ||
const exprs = path.get("expressions"); | ||
var exprs = path.get("expressions"); | ||
return evaluateCached(exprs[exprs.length - 1], state); | ||
@@ -85,9 +79,5 @@ } | ||
if (path.isTaggedTemplateExpression() && path.get("tag").isMemberExpression()) { | ||
const object = path.get("tag.object"); | ||
const { | ||
node: { | ||
name | ||
} | ||
} = object; | ||
const property = path.get("tag.property"); | ||
var object = path.get("tag.object"); | ||
var name = object.node.name; | ||
var property = path.get("tag.property"); | ||
@@ -100,3 +90,3 @@ if (object.isIdentifier() && name === "String" && !path.scope.getBinding(name, true) && property.isIdentifier && property.node.name === "raw") { | ||
if (path.isConditionalExpression()) { | ||
const testResult = evaluateCached(path.get("test"), state); | ||
var testResult = evaluateCached(path.get("test"), state); | ||
if (!state.confident) return; | ||
@@ -118,11 +108,12 @@ | ||
})) { | ||
const property = path.get("property"); | ||
const object = path.get("object"); | ||
var _property = path.get("property"); | ||
if (object.isLiteral() && property.isIdentifier()) { | ||
const value = object.node.value; | ||
const type = typeof value; | ||
var _object = path.get("object"); | ||
if (_object.isLiteral() && _property.isIdentifier()) { | ||
var value = _object.node.value; | ||
var type = typeof value; | ||
if (type === "number" || type === "string") { | ||
return value[property.node.name]; | ||
return value[_property.node.name]; | ||
} | ||
@@ -133,3 +124,3 @@ } | ||
if (path.isReferencedIdentifier()) { | ||
const binding = path.scope.getBinding(node.name); | ||
var binding = path.scope.getBinding(node.name); | ||
@@ -155,3 +146,3 @@ if (binding && binding.constantViolations.length > 0) { | ||
const resolved = path.resolve(); | ||
var resolved = path.resolve(); | ||
@@ -173,3 +164,3 @@ if (resolved === path) { | ||
const argument = path.get("argument"); | ||
var argument = path.get("argument"); | ||
@@ -180,3 +171,3 @@ if (node.operator === "typeof" && (argument.isFunction() || argument.isClass())) { | ||
const arg = evaluateCached(argument, state); | ||
var arg = evaluateCached(argument, state); | ||
if (!state.confident) return; | ||
@@ -203,8 +194,20 @@ | ||
if (path.isArrayExpression()) { | ||
const arr = []; | ||
const elems = path.get("elements"); | ||
var arr = []; | ||
var elems = path.get("elements"); | ||
for (const elem of elems) { | ||
const elemValue = elem.evaluate(); | ||
for (var _iterator = elems, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var elem = _ref; | ||
var elemValue = elem.evaluate(); | ||
if (elemValue.confident) { | ||
@@ -221,6 +224,19 @@ arr.push(elemValue.value); | ||
if (path.isObjectExpression()) { | ||
const obj = {}; | ||
const props = path.get("properties"); | ||
var obj = {}; | ||
var props = path.get("properties"); | ||
for (const prop of props) { | ||
for (var _iterator2 = props, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { | ||
var _ref2; | ||
if (_isArray2) { | ||
if (_i2 >= _iterator2.length) break; | ||
_ref2 = _iterator2[_i2++]; | ||
} else { | ||
_i2 = _iterator2.next(); | ||
if (_i2.done) break; | ||
_ref2 = _i2.value; | ||
} | ||
var prop = _ref2; | ||
if (prop.isObjectMethod() || prop.isSpreadElement()) { | ||
@@ -230,4 +246,4 @@ return deopt(prop, state); | ||
const keyPath = prop.get("key"); | ||
let key = keyPath; | ||
var keyPath = prop.get("key"); | ||
var key = keyPath; | ||
@@ -248,11 +264,12 @@ if (prop.node.computed) { | ||
const valuePath = prop.get("value"); | ||
let value = valuePath.evaluate(); | ||
var valuePath = prop.get("value"); | ||
if (!value.confident) { | ||
var _value = valuePath.evaluate(); | ||
if (!_value.confident) { | ||
return deopt(valuePath, state); | ||
} | ||
value = value.value; | ||
obj[key] = value; | ||
_value = _value.value; | ||
obj[key] = _value; | ||
} | ||
@@ -264,8 +281,8 @@ | ||
if (path.isLogicalExpression()) { | ||
const wasConfident = state.confident; | ||
const left = evaluateCached(path.get("left"), state); | ||
const leftConfident = state.confident; | ||
var wasConfident = state.confident; | ||
var left = evaluateCached(path.get("left"), state); | ||
var leftConfident = state.confident; | ||
state.confident = wasConfident; | ||
const right = evaluateCached(path.get("right"), state); | ||
const rightConfident = state.confident; | ||
var right = evaluateCached(path.get("right"), state); | ||
var rightConfident = state.confident; | ||
state.confident = leftConfident && rightConfident; | ||
@@ -294,5 +311,8 @@ | ||
if (path.isBinaryExpression()) { | ||
const left = evaluateCached(path.get("left"), state); | ||
var _left = evaluateCached(path.get("left"), state); | ||
if (!state.confident) return; | ||
const right = evaluateCached(path.get("right"), state); | ||
var _right = evaluateCached(path.get("right"), state); | ||
if (!state.confident) return; | ||
@@ -302,60 +322,60 @@ | ||
case "-": | ||
return left - right; | ||
return _left - _right; | ||
case "+": | ||
return left + right; | ||
return _left + _right; | ||
case "/": | ||
return left / right; | ||
return _left / _right; | ||
case "*": | ||
return left * right; | ||
return _left * _right; | ||
case "%": | ||
return left % right; | ||
return _left % _right; | ||
case "**": | ||
return left ** right; | ||
return Math.pow(_left, _right); | ||
case "<": | ||
return left < right; | ||
return _left < _right; | ||
case ">": | ||
return left > right; | ||
return _left > _right; | ||
case "<=": | ||
return left <= right; | ||
return _left <= _right; | ||
case ">=": | ||
return left >= right; | ||
return _left >= _right; | ||
case "==": | ||
return left == right; | ||
return _left == _right; | ||
case "!=": | ||
return left != right; | ||
return _left != _right; | ||
case "===": | ||
return left === right; | ||
return _left === _right; | ||
case "!==": | ||
return left !== right; | ||
return _left !== _right; | ||
case "|": | ||
return left | right; | ||
return _left | _right; | ||
case "&": | ||
return left & right; | ||
return _left & _right; | ||
case "^": | ||
return left ^ right; | ||
return _left ^ _right; | ||
case "<<": | ||
return left << right; | ||
return _left << _right; | ||
case ">>": | ||
return left >> right; | ||
return _left >> _right; | ||
case ">>>": | ||
return left >>> right; | ||
return _left >>> _right; | ||
} | ||
@@ -365,5 +385,5 @@ } | ||
if (path.isCallExpression()) { | ||
const callee = path.get("callee"); | ||
let context; | ||
let func; | ||
var callee = path.get("callee"); | ||
var context; | ||
var func; | ||
@@ -375,16 +395,17 @@ if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name, true) && VALID_CALLEES.indexOf(callee.node.name) >= 0) { | ||
if (callee.isMemberExpression()) { | ||
const object = callee.get("object"); | ||
const property = callee.get("property"); | ||
var _object2 = callee.get("object"); | ||
if (object.isIdentifier() && property.isIdentifier() && VALID_CALLEES.indexOf(object.node.name) >= 0 && INVALID_METHODS.indexOf(property.node.name) < 0) { | ||
context = global[object.node.name]; | ||
func = context[property.node.name]; | ||
var _property2 = callee.get("property"); | ||
if (_object2.isIdentifier() && _property2.isIdentifier() && VALID_CALLEES.indexOf(_object2.node.name) >= 0 && INVALID_METHODS.indexOf(_property2.node.name) < 0) { | ||
context = global[_object2.node.name]; | ||
func = context[_property2.node.name]; | ||
} | ||
if (object.isLiteral() && property.isIdentifier()) { | ||
const type = typeof object.node.value; | ||
if (_object2.isLiteral() && _property2.isIdentifier()) { | ||
var _type = typeof _object2.node.value; | ||
if (type === "string" || type === "number") { | ||
context = object.node.value; | ||
func = context[property.node.name]; | ||
if (_type === "string" || _type === "number") { | ||
context = _object2.node.value; | ||
func = context[_property2.node.name]; | ||
} | ||
@@ -395,3 +416,5 @@ } | ||
if (func) { | ||
const args = path.get("arguments").map(arg => evaluateCached(arg, state)); | ||
var args = path.get("arguments").map(function (arg) { | ||
return evaluateCached(arg, state); | ||
}); | ||
if (!state.confident) return; | ||
@@ -405,11 +428,27 @@ return func.apply(context, args); | ||
function evaluateQuasis(path, quasis, state, raw = false) { | ||
let str = ""; | ||
let i = 0; | ||
const exprs = path.get("expressions"); | ||
function evaluateQuasis(path, quasis, state, raw) { | ||
if (raw === void 0) { | ||
raw = false; | ||
} | ||
for (const elem of quasis) { | ||
var str = ""; | ||
var i = 0; | ||
var exprs = path.get("expressions"); | ||
for (var _iterator3 = quasis, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) { | ||
var _ref3; | ||
if (_isArray3) { | ||
if (_i3 >= _iterator3.length) break; | ||
_ref3 = _iterator3[_i3++]; | ||
} else { | ||
_i3 = _iterator3.next(); | ||
if (_i3.done) break; | ||
_ref3 = _i3.value; | ||
} | ||
var elem = _ref3; | ||
if (!state.confident) break; | ||
str += raw ? elem.value.raw : elem.value.cooked; | ||
const expr = exprs[i++]; | ||
var expr = exprs[i++]; | ||
if (expr) str += String(evaluateCached(expr, state)); | ||
@@ -423,3 +462,3 @@ } | ||
function evaluate() { | ||
const state = { | ||
var state = { | ||
confident: true, | ||
@@ -429,3 +468,3 @@ deoptPath: null, | ||
}; | ||
let value = evaluateCached(this, state); | ||
var value = evaluateCached(this, state); | ||
if (!state.confident) value = undefined; | ||
@@ -432,0 +471,0 @@ return { |
@@ -24,5 +24,5 @@ "use strict"; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -52,3 +52,3 @@ }; | ||
function getCompletionRecords() { | ||
let paths = []; | ||
var paths = []; | ||
@@ -96,5 +96,5 @@ if (this.isIfStatement()) { | ||
function getAllNextSiblings() { | ||
let _key = this.key; | ||
let sibling = this.getSibling(++_key); | ||
const siblings = []; | ||
var _key = this.key; | ||
var sibling = this.getSibling(++_key); | ||
var siblings = []; | ||
@@ -110,5 +110,5 @@ while (sibling.node) { | ||
function getAllPrevSiblings() { | ||
let _key = this.key; | ||
let sibling = this.getSibling(--_key); | ||
const siblings = []; | ||
var _key = this.key; | ||
var sibling = this.getSibling(--_key); | ||
var siblings = []; | ||
@@ -125,3 +125,3 @@ while (sibling.node) { | ||
if (context === true) context = this.context; | ||
const parts = key.split("."); | ||
var parts = key.split("."); | ||
@@ -136,10 +136,12 @@ if (parts.length === 1) { | ||
function _getKey(key, context) { | ||
const node = this.node; | ||
const container = node[key]; | ||
var _this = this; | ||
var node = this.node; | ||
var container = node[key]; | ||
if (Array.isArray(container)) { | ||
return container.map((_, i) => { | ||
return container.map(function (_, i) { | ||
return _index.default.get({ | ||
listKey: key, | ||
parentPath: this, | ||
parentPath: _this, | ||
parent: node, | ||
@@ -161,5 +163,8 @@ container: container, | ||
function _getPattern(parts, context) { | ||
let path = this; | ||
var path = this; | ||
var _arr = parts; | ||
for (const part of parts) { | ||
for (var _i = 0; _i < _arr.length; _i++) { | ||
var part = _arr[_i]; | ||
if (part === ".") { | ||
@@ -187,16 +192,24 @@ path = path.parentPath; | ||
function getBindingIdentifierPaths(duplicates = false, outerOnly = false) { | ||
const path = this; | ||
let search = [].concat(path); | ||
const ids = Object.create(null); | ||
function getBindingIdentifierPaths(duplicates, outerOnly) { | ||
if (duplicates === void 0) { | ||
duplicates = false; | ||
} | ||
if (outerOnly === void 0) { | ||
outerOnly = false; | ||
} | ||
var path = this; | ||
var search = [].concat(path); | ||
var ids = Object.create(null); | ||
while (search.length) { | ||
const id = search.shift(); | ||
var id = search.shift(); | ||
if (!id) continue; | ||
if (!id.node) continue; | ||
const keys = t().getBindingIdentifiers.keys[id.node.type]; | ||
var keys = t().getBindingIdentifiers.keys[id.node.type]; | ||
if (id.isIdentifier()) { | ||
if (duplicates) { | ||
const _ids = ids[id.node.name] = ids[id.node.name] || []; | ||
var _ids = ids[id.node.name] = ids[id.node.name] || []; | ||
@@ -212,3 +225,3 @@ _ids.push(id); | ||
if (id.isExportDeclaration()) { | ||
const declaration = id.get("declaration"); | ||
var declaration = id.get("declaration"); | ||
@@ -234,5 +247,5 @@ if (declaration.isDeclaration()) { | ||
if (keys) { | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
const child = id.get(key); | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
var child = id.get(key); | ||
@@ -239,0 +252,0 @@ if (Array.isArray(child) || child.node) { |
@@ -10,6 +10,6 @@ "use strict"; | ||
function _debug() { | ||
const data = _interopRequireDefault(require("debug")); | ||
function _debug2() { | ||
var data = _interopRequireDefault(require("debug")); | ||
_debug = function () { | ||
_debug2 = function _debug2() { | ||
return data; | ||
@@ -22,5 +22,5 @@ }; | ||
function _invariant() { | ||
const data = _interopRequireDefault(require("invariant")); | ||
var data = _interopRequireDefault(require("invariant")); | ||
_invariant = function () { | ||
_invariant = function _invariant() { | ||
return data; | ||
@@ -37,5 +37,5 @@ }; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -50,5 +50,5 @@ }; | ||
function _generator() { | ||
const data = _interopRequireDefault(require("@babel/generator")); | ||
var data = _interopRequireDefault(require("@babel/generator")); | ||
_generator = function () { | ||
_generator = function _generator() { | ||
return data; | ||
@@ -86,6 +86,6 @@ }; | ||
const debug = (0, _debug().default)("babel"); | ||
var _debug = (0, _debug2().default)("babel"); | ||
class NodePath { | ||
constructor(hub, parent) { | ||
var NodePath = function () { | ||
function NodePath(hub, parent) { | ||
this.parent = parent; | ||
@@ -114,10 +114,10 @@ this.hub = hub; | ||
static get({ | ||
hub, | ||
parentPath, | ||
parent, | ||
container, | ||
listKey, | ||
key | ||
}) { | ||
NodePath.get = function get(_ref) { | ||
var hub = _ref.hub, | ||
parentPath = _ref.parentPath, | ||
parent = _ref.parent, | ||
container = _ref.container, | ||
listKey = _ref.listKey, | ||
key = _ref.key; | ||
if (!hub && parentPath) { | ||
@@ -128,4 +128,4 @@ hub = parentPath.hub; | ||
(0, _invariant().default)(parent, "To get a node path the parent needs to exist"); | ||
const targetNode = container[key]; | ||
const paths = _cache.path.get(parent) || []; | ||
var targetNode = container[key]; | ||
var paths = _cache.path.get(parent) || []; | ||
@@ -136,6 +136,6 @@ if (!_cache.path.has(parent)) { | ||
let path; | ||
var path; | ||
for (let i = 0; i < paths.length; i++) { | ||
const pathCheck = paths[i]; | ||
for (var i = 0; i < paths.length; i++) { | ||
var pathCheck = paths[i]; | ||
@@ -155,38 +155,44 @@ if (pathCheck.node === targetNode) { | ||
return path; | ||
} | ||
}; | ||
getScope(scope) { | ||
var _proto = NodePath.prototype; | ||
_proto.getScope = function getScope(scope) { | ||
return this.isScope() ? new _scope.default(this) : scope; | ||
} | ||
}; | ||
setData(key, val) { | ||
_proto.setData = function setData(key, val) { | ||
return this.data[key] = val; | ||
} | ||
}; | ||
getData(key, def) { | ||
let val = this.data[key]; | ||
_proto.getData = function getData(key, def) { | ||
var val = this.data[key]; | ||
if (!val && def) val = this.data[key] = def; | ||
return val; | ||
} | ||
}; | ||
buildCodeFrameError(msg, Error = SyntaxError) { | ||
_proto.buildCodeFrameError = function buildCodeFrameError(msg, Error) { | ||
if (Error === void 0) { | ||
Error = SyntaxError; | ||
} | ||
return this.hub.file.buildCodeFrameError(this.node, msg, Error); | ||
} | ||
}; | ||
traverse(visitor, state) { | ||
_proto.traverse = function traverse(visitor, state) { | ||
(0, _index.default)(this.node, visitor, this.scope, state, this); | ||
} | ||
}; | ||
set(key, node) { | ||
_proto.set = function set(key, node) { | ||
t().validate(this.node, key, node); | ||
this.node[key] = node; | ||
} | ||
}; | ||
getPathLocation() { | ||
const parts = []; | ||
let path = this; | ||
_proto.getPathLocation = function getPathLocation() { | ||
var parts = []; | ||
var path = this; | ||
do { | ||
let key = path.key; | ||
if (path.inList) key = `${path.listKey}[${key}]`; | ||
var key = path.key; | ||
if (path.inList) key = path.listKey + "[" + key + "]"; | ||
parts.unshift(key); | ||
@@ -196,20 +202,24 @@ } while (path = path.parentPath); | ||
return parts.join("."); | ||
} | ||
}; | ||
debug(message) { | ||
if (!debug.enabled) return; | ||
debug(`${this.getPathLocation()} ${this.type}: ${message}`); | ||
} | ||
_proto.debug = function debug(message) { | ||
if (!_debug.enabled) return; | ||
toString() { | ||
_debug(this.getPathLocation() + " " + this.type + ": " + message); | ||
}; | ||
_proto.toString = function toString() { | ||
return (0, _generator().default)(this.node).code; | ||
} | ||
}; | ||
} | ||
return NodePath; | ||
}(); | ||
exports.default = NodePath; | ||
Object.assign(NodePath.prototype, NodePath_ancestry, NodePath_inference, NodePath_replacement, NodePath_evaluation, NodePath_conversion, NodePath_introspection, NodePath_context, NodePath_removal, NodePath_modification, NodePath_family, NodePath_comments); | ||
var _arr = t().TYPES; | ||
for (const type of t().TYPES) { | ||
const typeKey = `is${type}`; | ||
var _loop2 = function _loop2() { | ||
var type = _arr[_i]; | ||
var typeKey = "is" + type; | ||
@@ -220,17 +230,27 @@ NodePath.prototype[typeKey] = function (opts) { | ||
NodePath.prototype[`assert${type}`] = function (opts) { | ||
NodePath.prototype["assert" + type] = function (opts) { | ||
if (!this[typeKey](opts)) { | ||
throw new TypeError(`Expected node path of type ${type}`); | ||
throw new TypeError("Expected node path of type " + type); | ||
} | ||
}; | ||
}; | ||
for (var _i = 0; _i < _arr.length; _i++) { | ||
_loop2(); | ||
} | ||
for (const type in virtualTypes) { | ||
if (type[0] === "_") continue; | ||
var _loop = function _loop(type) { | ||
if (type[0] === "_") return "continue"; | ||
if (t().TYPES.indexOf(type) < 0) t().TYPES.push(type); | ||
const virtualType = virtualTypes[type]; | ||
var virtualType = virtualTypes[type]; | ||
NodePath.prototype[`is${type}`] = function (opts) { | ||
NodePath.prototype["is" + type] = function (opts) { | ||
return virtualType.checkPath(this, opts); | ||
}; | ||
}; | ||
for (var type in virtualTypes) { | ||
var _ret = _loop(type); | ||
if (_ret === "continue") continue; | ||
} |
@@ -16,5 +16,5 @@ "use strict"; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -30,3 +30,3 @@ }; | ||
if (this.typeAnnotation) return this.typeAnnotation; | ||
let type = this._getTypeAnnotation() || t().anyTypeAnnotation(); | ||
var type = this._getTypeAnnotation() || t().anyTypeAnnotation(); | ||
if (t().isTypeAnnotation(type)) type = type.typeAnnotation; | ||
@@ -37,8 +37,8 @@ return this.typeAnnotation = type; | ||
function _getTypeAnnotation() { | ||
const node = this.node; | ||
var node = this.node; | ||
if (!node) { | ||
if (this.key === "init" && this.parentPath.isVariableDeclarator()) { | ||
const declar = this.parentPath.parentPath; | ||
const declarParent = declar.parentPath; | ||
var declar = this.parentPath.parentPath; | ||
var declarParent = declar.parentPath; | ||
@@ -63,3 +63,3 @@ if (declar.key === "left" && declarParent.isForInStatement()) { | ||
let inferer = inferers[node.type]; | ||
var inferer = inferers[node.type]; | ||
@@ -100,3 +100,3 @@ if (inferer) { | ||
} else { | ||
throw new Error(`Unknown base type ${baseName}`); | ||
throw new Error("Unknown base type " + baseName); | ||
} | ||
@@ -107,7 +107,11 @@ } | ||
function couldBeBaseType(name) { | ||
const type = this.getTypeAnnotation(); | ||
var type = this.getTypeAnnotation(); | ||
if (t().isAnyTypeAnnotation(type)) return true; | ||
if (t().isUnionTypeAnnotation(type)) { | ||
for (const type2 of type.types) { | ||
var _arr = type.types; | ||
for (var _i = 0; _i < _arr.length; _i++) { | ||
var type2 = _arr[_i]; | ||
if (t().isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) { | ||
@@ -125,3 +129,3 @@ return true; | ||
function baseTypeStrictlyMatches(right) { | ||
const left = this.getTypeAnnotation(); | ||
var left = this.getTypeAnnotation(); | ||
right = right.getTypeAnnotation(); | ||
@@ -135,3 +139,3 @@ | ||
function isGenericType(genericName) { | ||
const type = this.getTypeAnnotation(); | ||
var type = this.getTypeAnnotation(); | ||
return t().isGenericTypeAnnotation(type) && t().isIdentifier(type.id, { | ||
@@ -138,0 +142,0 @@ name: genericName |
@@ -9,5 +9,5 @@ "use strict"; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -23,3 +23,3 @@ }; | ||
if (!this.isReferenced()) return; | ||
const binding = this.scope.getBinding(node.name); | ||
var binding = this.scope.getBinding(node.name); | ||
@@ -42,10 +42,12 @@ if (binding) { | ||
function getTypeAnnotationBindingConstantViolations(binding, path, name) { | ||
const types = []; | ||
const functionConstantViolations = []; | ||
let constantViolations = getConstantViolationsBefore(binding, path, functionConstantViolations); | ||
const testType = getConditionalAnnotation(binding, path, name); | ||
var types = []; | ||
var functionConstantViolations = []; | ||
var constantViolations = getConstantViolationsBefore(binding, path, functionConstantViolations); | ||
var testType = getConditionalAnnotation(binding, path, name); | ||
if (testType) { | ||
const testConstantViolations = getConstantViolationsBefore(binding, testType.ifStatement); | ||
constantViolations = constantViolations.filter(path => testConstantViolations.indexOf(path) < 0); | ||
var testConstantViolations = getConstantViolationsBefore(binding, testType.ifStatement); | ||
constantViolations = constantViolations.filter(function (path) { | ||
return testConstantViolations.indexOf(path) < 0; | ||
}); | ||
types.push(testType.typeAnnotation); | ||
@@ -56,4 +58,6 @@ } | ||
constantViolations = constantViolations.concat(functionConstantViolations); | ||
var _arr = constantViolations; | ||
for (const violation of constantViolations) { | ||
for (var _i = 0; _i < _arr.length; _i++) { | ||
var violation = _arr[_i]; | ||
types.push(violation.getTypeAnnotation()); | ||
@@ -69,8 +73,8 @@ } | ||
function getConstantViolationsBefore(binding, path, functions) { | ||
const violations = binding.constantViolations.slice(); | ||
var violations = binding.constantViolations.slice(); | ||
violations.unshift(binding.path); | ||
return violations.filter(violation => { | ||
return violations.filter(function (violation) { | ||
violation = violation.resolve(); | ||
const status = violation._guessExecutionStatusRelativeTo(path); | ||
var status = violation._guessExecutionStatusRelativeTo(path); | ||
@@ -83,13 +87,13 @@ if (functions && status === "function") functions.push(violation); | ||
function inferAnnotationFromBinaryExpression(name, path) { | ||
const operator = path.node.operator; | ||
const right = path.get("right").resolve(); | ||
const left = path.get("left").resolve(); | ||
let target; | ||
var operator = path.node.operator; | ||
var right = path.get("right").resolve(); | ||
var left = path.get("left").resolve(); | ||
var target; | ||
if (left.isIdentifier({ | ||
name | ||
name: name | ||
})) { | ||
target = right; | ||
} else if (right.isIdentifier({ | ||
name | ||
name: name | ||
})) { | ||
@@ -112,4 +116,4 @@ target = left; | ||
if (operator !== "===" && operator !== "==") return; | ||
let typeofPath; | ||
let typePath; | ||
var typeofPath; | ||
var typePath; | ||
@@ -130,7 +134,7 @@ if (left.isUnaryExpression({ | ||
if (!typeofPath.get("argument").isIdentifier({ | ||
name | ||
name: name | ||
})) return; | ||
typePath = typePath.resolve(); | ||
if (!typePath.isLiteral()) return; | ||
const typeValue = typePath.node.value; | ||
var typeValue = typePath.node.value; | ||
if (typeof typeValue !== "string") return; | ||
@@ -141,3 +145,3 @@ return t().createTypeAnnotationBasedOnTypeof(typeValue); | ||
function getParentConditionalPath(binding, path, name) { | ||
let parentPath; | ||
var parentPath; | ||
@@ -162,18 +166,18 @@ while (parentPath = path.parentPath) { | ||
function getConditionalAnnotation(binding, path, name) { | ||
const ifStatement = getParentConditionalPath(binding, path, name); | ||
var ifStatement = getParentConditionalPath(binding, path, name); | ||
if (!ifStatement) return; | ||
const test = ifStatement.get("test"); | ||
const paths = [test]; | ||
const types = []; | ||
var test = ifStatement.get("test"); | ||
var paths = [test]; | ||
var types = []; | ||
for (let i = 0; i < paths.length; i++) { | ||
const path = paths[i]; | ||
for (var i = 0; i < paths.length; i++) { | ||
var _path = paths[i]; | ||
if (path.isLogicalExpression()) { | ||
if (path.node.operator === "&&") { | ||
paths.push(path.get("left")); | ||
paths.push(path.get("right")); | ||
if (_path.isLogicalExpression()) { | ||
if (_path.node.operator === "&&") { | ||
paths.push(_path.get("left")); | ||
paths.push(_path.get("right")); | ||
} | ||
} else if (path.isBinaryExpression()) { | ||
const type = inferAnnotationFromBinaryExpression(name, path); | ||
} else if (_path.isBinaryExpression()) { | ||
var type = inferAnnotationFromBinaryExpression(name, _path); | ||
if (type) types.push(type); | ||
@@ -186,3 +190,3 @@ } | ||
typeAnnotation: t().createUnionTypeAnnotation(types), | ||
ifStatement | ||
ifStatement: ifStatement | ||
}; | ||
@@ -189,0 +193,0 @@ } |
@@ -30,3 +30,3 @@ "use strict"; | ||
enumerable: true, | ||
get: function () { | ||
get: function get() { | ||
return _infererReference.default; | ||
@@ -37,5 +37,5 @@ } | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -54,6 +54,6 @@ }; | ||
function VariableDeclarator() { | ||
const id = this.get("id"); | ||
var id = this.get("id"); | ||
if (!id.isIdentifier()) return; | ||
const init = this.get("init"); | ||
let type = init.getTypeAnnotation(); | ||
var init = this.get("init"); | ||
var type = init.getTypeAnnotation(); | ||
@@ -88,3 +88,3 @@ if (type && type.type === "AnyTypeAnnotation") { | ||
function UnaryExpression(node) { | ||
const operator = node.operator; | ||
var operator = node.operator; | ||
@@ -103,3 +103,3 @@ if (operator === "void") { | ||
function BinaryExpression(node) { | ||
const operator = node.operator; | ||
var operator = node.operator; | ||
@@ -111,4 +111,4 @@ if (t().NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) { | ||
} else if (operator === "+") { | ||
const right = this.get("right"); | ||
const left = this.get("left"); | ||
var right = this.get("right"); | ||
var left = this.get("left"); | ||
@@ -142,3 +142,3 @@ if (left.isBaseType("number") && right.isBaseType("number")) { | ||
function UpdateExpression(node) { | ||
const operator = node.operator; | ||
var operator = node.operator; | ||
@@ -188,11 +188,9 @@ if (operator === "++" || operator === "--") { | ||
const isArrayFrom = t().buildMatchMemberExpression("Array.from"); | ||
const isObjectKeys = t().buildMatchMemberExpression("Object.keys"); | ||
const isObjectValues = t().buildMatchMemberExpression("Object.values"); | ||
const isObjectEntries = t().buildMatchMemberExpression("Object.entries"); | ||
var isArrayFrom = t().buildMatchMemberExpression("Array.from"); | ||
var isObjectKeys = t().buildMatchMemberExpression("Object.keys"); | ||
var isObjectValues = t().buildMatchMemberExpression("Object.values"); | ||
var isObjectEntries = t().buildMatchMemberExpression("Object.entries"); | ||
function CallExpression() { | ||
const { | ||
callee | ||
} = this.node; | ||
var callee = this.node.callee; | ||
@@ -199,0 +197,0 @@ if (isObjectKeys(callee)) { |
@@ -27,5 +27,5 @@ "use strict"; | ||
function _includes() { | ||
const data = _interopRequireDefault(require("lodash/includes")); | ||
var data = _interopRequireDefault(require("lodash/includes")); | ||
_includes = function () { | ||
_includes = function _includes() { | ||
return data; | ||
@@ -38,5 +38,5 @@ }; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -57,3 +57,3 @@ }; | ||
function has(key) { | ||
const val = this.node && this.node[key]; | ||
var val = this.node && this.node[key]; | ||
@@ -71,3 +71,3 @@ if (val && Array.isArray(val)) { | ||
const is = has; | ||
var is = has; | ||
exports.is = is; | ||
@@ -106,7 +106,7 @@ | ||
function isCompletionRecord(allowInsideFunction) { | ||
let path = this; | ||
let first = true; | ||
var path = this; | ||
var first = true; | ||
do { | ||
const container = path.container; | ||
var container = path.container; | ||
@@ -137,6 +137,6 @@ if (path.isFunction() && !first) { | ||
if (!this.isReferencedIdentifier()) return false; | ||
const binding = this.scope.getBinding(this.node.name); | ||
var binding = this.scope.getBinding(this.node.name); | ||
if (!binding || binding.kind !== "module") return false; | ||
const path = binding.path; | ||
const parent = path.parentPath; | ||
var path = binding.path; | ||
var parent = path.parentPath; | ||
if (!parent.isImportDeclaration()) return false; | ||
@@ -166,3 +166,3 @@ | ||
function getSource() { | ||
const node = this.node; | ||
var node = this.node; | ||
@@ -181,7 +181,7 @@ if (node.end) { | ||
function _guessExecutionStatusRelativeTo(target) { | ||
const targetFuncParent = target.scope.getFunctionParent() || target.scope.getProgramParent(); | ||
const selfFuncParent = this.scope.getFunctionParent() || target.scope.getProgramParent(); | ||
var targetFuncParent = target.scope.getFunctionParent() || target.scope.getProgramParent(); | ||
var selfFuncParent = this.scope.getFunctionParent() || target.scope.getProgramParent(); | ||
if (targetFuncParent.node !== selfFuncParent.node) { | ||
const status = this._guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent); | ||
var status = this._guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent); | ||
@@ -195,11 +195,11 @@ if (status) { | ||
const targetPaths = target.getAncestry(); | ||
var targetPaths = target.getAncestry(); | ||
if (targetPaths.indexOf(this) >= 0) return "after"; | ||
const selfPaths = this.getAncestry(); | ||
let commonPath; | ||
let targetIndex; | ||
let selfIndex; | ||
var selfPaths = this.getAncestry(); | ||
var commonPath; | ||
var targetIndex; | ||
var selfIndex; | ||
for (selfIndex = 0; selfIndex < selfPaths.length; selfIndex++) { | ||
const selfPath = selfPaths[selfIndex]; | ||
var selfPath = selfPaths[selfIndex]; | ||
targetIndex = targetPaths.indexOf(selfPath); | ||
@@ -217,4 +217,4 @@ | ||
const targetRelationship = targetPaths[targetIndex - 1]; | ||
const selfRelationship = selfPaths[selfIndex - 1]; | ||
var targetRelationship = targetPaths[targetIndex - 1]; | ||
var selfRelationship = selfPaths[selfIndex - 1]; | ||
@@ -229,5 +229,5 @@ if (!targetRelationship || !selfRelationship) { | ||
const keys = t().VISITOR_KEYS[commonPath.type]; | ||
const targetKeyPosition = keys.indexOf(targetRelationship.key); | ||
const selfKeyPosition = keys.indexOf(selfRelationship.key); | ||
var keys = t().VISITOR_KEYS[commonPath.type]; | ||
var targetKeyPosition = keys.indexOf(targetRelationship.key); | ||
var selfKeyPosition = keys.indexOf(selfRelationship.key); | ||
return targetKeyPosition > selfKeyPosition ? "before" : "after"; | ||
@@ -237,9 +237,22 @@ } | ||
function _guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent) { | ||
const targetFuncPath = targetFuncParent.path; | ||
var targetFuncPath = targetFuncParent.path; | ||
if (!targetFuncPath.isFunctionDeclaration()) return; | ||
const binding = targetFuncPath.scope.getBinding(targetFuncPath.node.id.name); | ||
var binding = targetFuncPath.scope.getBinding(targetFuncPath.node.id.name); | ||
if (!binding.references) return "before"; | ||
const referencePaths = binding.referencePaths; | ||
var referencePaths = binding.referencePaths; | ||
for (const path of referencePaths) { | ||
for (var _iterator = referencePaths, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var path = _ref; | ||
if (path.key !== "callee" || !path.parentPath.isCallExpression()) { | ||
@@ -250,9 +263,23 @@ return; | ||
let allStatus; | ||
var allStatus; | ||
for (const path of referencePaths) { | ||
const childOfFunction = !!path.find(path => path.node === targetFuncPath.node); | ||
for (var _iterator2 = referencePaths, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { | ||
var _ref2; | ||
if (_isArray2) { | ||
if (_i2 >= _iterator2.length) break; | ||
_ref2 = _iterator2[_i2++]; | ||
} else { | ||
_i2 = _iterator2.next(); | ||
if (_i2.done) break; | ||
_ref2 = _i2.value; | ||
} | ||
var _path = _ref2; | ||
var childOfFunction = !!_path.find(function (path) { | ||
return path.node === targetFuncPath.node; | ||
}); | ||
if (childOfFunction) continue; | ||
const status = this._guessExecutionStatusRelativeTo(path); | ||
var status = this._guessExecutionStatusRelativeTo(_path); | ||
@@ -283,3 +310,3 @@ if (allStatus) { | ||
} else if (this.isReferencedIdentifier()) { | ||
const binding = this.scope.getBinding(this.node.name); | ||
var binding = this.scope.getBinding(this.node.name); | ||
if (!binding) return; | ||
@@ -290,4 +317,6 @@ if (!binding.constant) return; | ||
if (binding.path !== this) { | ||
const ret = binding.path.resolve(dangerous, resolved); | ||
if (this.find(parent => parent.node === ret.node)) return; | ||
var ret = binding.path.resolve(dangerous, resolved); | ||
if (this.find(function (parent) { | ||
return parent.node === ret.node; | ||
})) return; | ||
return ret; | ||
@@ -298,14 +327,16 @@ } | ||
} else if (dangerous && this.isMemberExpression()) { | ||
const targetKey = this.toComputedKey(); | ||
var targetKey = this.toComputedKey(); | ||
if (!t().isLiteral(targetKey)) return; | ||
const targetName = targetKey.value; | ||
const target = this.get("object").resolve(dangerous, resolved); | ||
var targetName = targetKey.value; | ||
var target = this.get("object").resolve(dangerous, resolved); | ||
if (target.isObjectExpression()) { | ||
const props = target.get("properties"); | ||
var props = target.get("properties"); | ||
var _arr = props; | ||
for (const prop of props) { | ||
for (var _i3 = 0; _i3 < _arr.length; _i3++) { | ||
var prop = _arr[_i3]; | ||
if (!prop.isProperty()) continue; | ||
const key = prop.get("key"); | ||
let match = prop.isnt("computed") && key.isIdentifier({ | ||
var key = prop.get("key"); | ||
var match = prop.isnt("computed") && key.isIdentifier({ | ||
name: targetName | ||
@@ -319,4 +350,4 @@ }); | ||
} else if (target.isArrayExpression() && !isNaN(+targetName)) { | ||
const elems = target.get("elements"); | ||
const elem = elems[targetName]; | ||
var elems = target.get("elements"); | ||
var elem = elems[targetName]; | ||
if (elem) return elem.resolve(dangerous, resolved); | ||
@@ -329,3 +360,3 @@ } | ||
if (this.isIdentifier()) { | ||
const binding = this.scope.getBinding(this.node.name); | ||
var binding = this.scope.getBinding(this.node.name); | ||
@@ -345,3 +376,5 @@ if (!binding) { | ||
if (this.isTemplateLiteral()) { | ||
return this.get("expressions").every(expression => expression.isConstantExpression()); | ||
return this.get("expressions").every(function (expression) { | ||
return expression.isConstantExpression(); | ||
}); | ||
} | ||
@@ -348,0 +381,0 @@ |
@@ -9,5 +9,5 @@ "use strict"; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -21,4 +21,4 @@ }; | ||
const referenceVisitor = { | ||
ReferencedIdentifier(path, state) { | ||
var referenceVisitor = { | ||
ReferencedIdentifier: function ReferencedIdentifier(path, state) { | ||
if (path.isJSXIdentifier() && t().react.isCompatTag(path.node.name) && !path.parentPath.isJSXMemberExpression()) { | ||
@@ -29,3 +29,3 @@ return; | ||
if (path.node.name === "this") { | ||
let scope = path.scope; | ||
var scope = path.scope; | ||
@@ -41,3 +41,3 @@ do { | ||
const binding = path.scope.getBinding(path.node.name); | ||
var binding = path.scope.getBinding(path.node.name); | ||
if (!binding) return; | ||
@@ -47,7 +47,6 @@ if (binding !== state.scope.getBinding(path.node.name)) return; | ||
} | ||
}; | ||
class PathHoister { | ||
constructor(path, scope) { | ||
var PathHoister = function () { | ||
function PathHoister(path, scope) { | ||
this.breakOnScopePaths = []; | ||
@@ -61,6 +60,8 @@ this.bindings = {}; | ||
isCompatibleScope(scope) { | ||
for (const key in this.bindings) { | ||
const binding = this.bindings[key]; | ||
var _proto = PathHoister.prototype; | ||
_proto.isCompatibleScope = function isCompatibleScope(scope) { | ||
for (var key in this.bindings) { | ||
var binding = this.bindings[key]; | ||
if (!scope.bindingIdentifierEquals(key, binding.identifier)) { | ||
@@ -72,6 +73,6 @@ return false; | ||
return true; | ||
} | ||
}; | ||
getCompatibleScopes() { | ||
let scope = this.path.scope; | ||
_proto.getCompatibleScopes = function getCompatibleScopes() { | ||
var scope = this.path.scope; | ||
@@ -89,9 +90,9 @@ do { | ||
} while (scope = scope.parent); | ||
} | ||
}; | ||
getAttachmentPath() { | ||
let path = this._getAttachmentPath(); | ||
_proto.getAttachmentPath = function getAttachmentPath() { | ||
var path = this._getAttachmentPath(); | ||
if (!path) return; | ||
let targetScope = path.scope; | ||
var targetScope = path.scope; | ||
@@ -103,5 +104,5 @@ if (targetScope.path === path) { | ||
if (targetScope.path.isProgram() || targetScope.path.isFunction()) { | ||
for (const name in this.bindings) { | ||
for (var name in this.bindings) { | ||
if (!targetScope.hasOwnBinding(name)) continue; | ||
const binding = this.bindings[name]; | ||
var binding = this.bindings[name]; | ||
@@ -112,3 +113,3 @@ if (binding.kind === "param" || binding.path.parentKey === "params") { | ||
const bindingParentPath = this.getAttachmentParentForPath(binding.path); | ||
var bindingParentPath = this.getAttachmentParentForPath(binding.path); | ||
@@ -118,4 +119,7 @@ if (bindingParentPath.key >= path.key) { | ||
path = binding.path; | ||
var _arr = binding.constantViolations; | ||
for (const violationPath of binding.constantViolations) { | ||
for (var _i = 0; _i < _arr.length; _i++) { | ||
var violationPath = _arr[_i]; | ||
if (this.getAttachmentParentForPath(violationPath).key > path.key) { | ||
@@ -130,7 +134,7 @@ path = violationPath; | ||
return path; | ||
} | ||
}; | ||
_getAttachmentPath() { | ||
const scopes = this.scopes; | ||
const scope = scopes.pop(); | ||
_proto._getAttachmentPath = function _getAttachmentPath() { | ||
var scopes = this.scopes; | ||
var scope = scopes.pop(); | ||
if (!scope) return; | ||
@@ -141,5 +145,5 @@ | ||
if (this.scope === scope) return; | ||
const bodies = scope.path.get("body").get("body"); | ||
var bodies = scope.path.get("body").get("body"); | ||
for (let i = 0; i < bodies.length; i++) { | ||
for (var i = 0; i < bodies.length; i++) { | ||
if (bodies[i].node._blockHoist) continue; | ||
@@ -154,10 +158,10 @@ return bodies[i]; | ||
} | ||
} | ||
}; | ||
getNextScopeAttachmentParent() { | ||
const scope = this.scopes.pop(); | ||
_proto.getNextScopeAttachmentParent = function getNextScopeAttachmentParent() { | ||
var scope = this.scopes.pop(); | ||
if (scope) return this.getAttachmentParentForPath(scope.path); | ||
} | ||
}; | ||
getAttachmentParentForPath(path) { | ||
_proto.getAttachmentParentForPath = function getAttachmentParentForPath(path) { | ||
do { | ||
@@ -168,8 +172,8 @@ if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) { | ||
} while (path = path.parentPath); | ||
} | ||
}; | ||
hasOwnParamBindings(scope) { | ||
for (const name in this.bindings) { | ||
_proto.hasOwnParamBindings = function hasOwnParamBindings(scope) { | ||
for (var name in this.bindings) { | ||
if (!scope.hasOwnBinding(name)) continue; | ||
const binding = this.bindings[name]; | ||
var binding = this.bindings[name]; | ||
if (binding.kind === "param" && binding.constant) return true; | ||
@@ -179,16 +183,19 @@ } | ||
return false; | ||
} | ||
}; | ||
run() { | ||
_proto.run = function run() { | ||
this.path.traverse(referenceVisitor, this); | ||
this.getCompatibleScopes(); | ||
const attachTo = this.getAttachmentPath(); | ||
var attachTo = this.getAttachmentPath(); | ||
if (!attachTo) return; | ||
if (attachTo.getFunctionParent() === this.path.getFunctionParent()) return; | ||
let uid = attachTo.scope.generateUidIdentifier("ref"); | ||
const declarator = t().variableDeclarator(uid, this.path.node); | ||
const insertFn = this.attachAfter ? "insertAfter" : "insertBefore"; | ||
const [attached] = attachTo[insertFn]([attachTo.isVariableDeclarator() ? declarator : t().variableDeclaration("var", [declarator])]); | ||
const parent = this.path.parentPath; | ||
var uid = attachTo.scope.generateUidIdentifier("ref"); | ||
var declarator = t().variableDeclarator(uid, this.path.node); | ||
var insertFn = this.attachAfter ? "insertAfter" : "insertBefore"; | ||
var _attachTo$insertFn = attachTo[insertFn]([attachTo.isVariableDeclarator() ? declarator : t().variableDeclaration("var", [declarator])]), | ||
attached = _attachTo$insertFn[0]; | ||
var parent = this.path.parentPath; | ||
if (parent.isJSXElement() && this.path.container === parent.node.children) { | ||
@@ -200,6 +207,7 @@ uid = t().JSXExpressionContainer(uid); | ||
return attachTo.isVariableDeclarator() ? attached.get("init") : attached.get("declarations.0.init"); | ||
} | ||
}; | ||
} | ||
return PathHoister; | ||
}(); | ||
exports.default = PathHoister; |
@@ -7,4 +7,4 @@ "use strict"; | ||
exports.hooks = void 0; | ||
const hooks = [function (self, parent) { | ||
const removeParent = self.key === "test" && (parent.isWhile() || parent.isSwitchCase()) || self.key === "declaration" && parent.isExportDeclaration() || self.key === "body" && parent.isLabeledStatement() || self.listKey === "declarations" && parent.isVariableDeclaration() && parent.node.declarations.length === 1 || self.key === "expression" && parent.isExpressionStatement(); | ||
var hooks = [function (self, parent) { | ||
var removeParent = self.key === "test" && (parent.isWhile() || parent.isSwitchCase()) || self.key === "declaration" && parent.isExportDeclaration() || self.key === "body" && parent.isLabeledStatement() || self.listKey === "declarations" && parent.isVariableDeclaration() && parent.node.declarations.length === 1 || self.key === "expression" && parent.isExpressionStatement(); | ||
@@ -11,0 +11,0 @@ if (removeParent) { |
@@ -9,5 +9,5 @@ "use strict"; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -21,9 +21,8 @@ }; | ||
const ReferencedIdentifier = { | ||
var ReferencedIdentifier = { | ||
types: ["Identifier", "JSXIdentifier"], | ||
checkPath: function checkPath(_ref, opts) { | ||
var node = _ref.node, | ||
parent = _ref.parent; | ||
checkPath({ | ||
node, | ||
parent | ||
}, opts) { | ||
if (!t().isIdentifier(node, opts) && !t().isJSXMemberExpression(parent, opts)) { | ||
@@ -39,36 +38,28 @@ if (t().isJSXIdentifier(node, opts)) { | ||
} | ||
}; | ||
exports.ReferencedIdentifier = ReferencedIdentifier; | ||
const ReferencedMemberExpression = { | ||
var ReferencedMemberExpression = { | ||
types: ["MemberExpression"], | ||
checkPath({ | ||
node, | ||
parent | ||
}) { | ||
checkPath: function checkPath(_ref2) { | ||
var node = _ref2.node, | ||
parent = _ref2.parent; | ||
return t().isMemberExpression(node) && t().isReferenced(node, parent); | ||
} | ||
}; | ||
exports.ReferencedMemberExpression = ReferencedMemberExpression; | ||
const BindingIdentifier = { | ||
var BindingIdentifier = { | ||
types: ["Identifier"], | ||
checkPath({ | ||
node, | ||
parent | ||
}) { | ||
checkPath: function checkPath(_ref3) { | ||
var node = _ref3.node, | ||
parent = _ref3.parent; | ||
return t().isIdentifier(node) && t().isBinding(node, parent); | ||
} | ||
}; | ||
exports.BindingIdentifier = BindingIdentifier; | ||
const Statement = { | ||
var Statement = { | ||
types: ["Statement"], | ||
checkPath: function checkPath(_ref4) { | ||
var node = _ref4.node, | ||
parent = _ref4.parent; | ||
checkPath({ | ||
node, | ||
parent | ||
}) { | ||
if (t().isStatement(node)) { | ||
@@ -89,9 +80,7 @@ if (t().isVariableDeclaration(node)) { | ||
} | ||
}; | ||
exports.Statement = Statement; | ||
const Expression = { | ||
var Expression = { | ||
types: ["Expression"], | ||
checkPath(path) { | ||
checkPath: function checkPath(path) { | ||
if (path.isIdentifier()) { | ||
@@ -103,64 +92,53 @@ return path.isReferencedIdentifier(); | ||
} | ||
}; | ||
exports.Expression = Expression; | ||
const Scope = { | ||
var Scope = { | ||
types: ["Scopable"], | ||
checkPath(path) { | ||
checkPath: function checkPath(path) { | ||
return t().isScope(path.node, path.parent); | ||
} | ||
}; | ||
exports.Scope = Scope; | ||
const Referenced = { | ||
checkPath(path) { | ||
var Referenced = { | ||
checkPath: function checkPath(path) { | ||
return t().isReferenced(path.node, path.parent); | ||
} | ||
}; | ||
exports.Referenced = Referenced; | ||
const BlockScoped = { | ||
checkPath(path) { | ||
var BlockScoped = { | ||
checkPath: function checkPath(path) { | ||
return t().isBlockScoped(path.node); | ||
} | ||
}; | ||
exports.BlockScoped = BlockScoped; | ||
const Var = { | ||
var Var = { | ||
types: ["VariableDeclaration"], | ||
checkPath(path) { | ||
checkPath: function checkPath(path) { | ||
return t().isVar(path.node); | ||
} | ||
}; | ||
exports.Var = Var; | ||
const User = { | ||
checkPath(path) { | ||
var User = { | ||
checkPath: function checkPath(path) { | ||
return path.node && !!path.node.loc; | ||
} | ||
}; | ||
exports.User = User; | ||
const Generated = { | ||
checkPath(path) { | ||
var Generated = { | ||
checkPath: function checkPath(path) { | ||
return !path.isUser(); | ||
} | ||
}; | ||
exports.Generated = Generated; | ||
const Pure = { | ||
checkPath(path, opts) { | ||
var Pure = { | ||
checkPath: function checkPath(path, opts) { | ||
return path.scope.isPure(path.node, opts); | ||
} | ||
}; | ||
exports.Pure = Pure; | ||
const Flow = { | ||
var Flow = { | ||
types: ["Flow", "ImportDeclaration", "ExportDeclaration", "ImportSpecifier"], | ||
checkPath: function checkPath(_ref5) { | ||
var node = _ref5.node; | ||
checkPath({ | ||
node | ||
}) { | ||
if (t().isFlow(node)) { | ||
@@ -178,41 +156,33 @@ return true; | ||
} | ||
}; | ||
exports.Flow = Flow; | ||
const RestProperty = { | ||
var RestProperty = { | ||
types: ["RestElement"], | ||
checkPath(path) { | ||
checkPath: function checkPath(path) { | ||
return path.parentPath && path.parentPath.isObjectPattern(); | ||
} | ||
}; | ||
exports.RestProperty = RestProperty; | ||
const SpreadProperty = { | ||
var SpreadProperty = { | ||
types: ["RestElement"], | ||
checkPath(path) { | ||
checkPath: function checkPath(path) { | ||
return path.parentPath && path.parentPath.isObjectExpression(); | ||
} | ||
}; | ||
exports.SpreadProperty = SpreadProperty; | ||
const ExistentialTypeParam = { | ||
var ExistentialTypeParam = { | ||
types: ["ExistsTypeAnnotation"] | ||
}; | ||
exports.ExistentialTypeParam = ExistentialTypeParam; | ||
const NumericLiteralTypeAnnotation = { | ||
var NumericLiteralTypeAnnotation = { | ||
types: ["NumberLiteralTypeAnnotation"] | ||
}; | ||
exports.NumericLiteralTypeAnnotation = NumericLiteralTypeAnnotation; | ||
const ForAwaitStatement = { | ||
var ForAwaitStatement = { | ||
types: ["ForOfStatement"], | ||
checkPath({ | ||
node | ||
}) { | ||
checkPath: function checkPath(_ref6) { | ||
var node = _ref6.node; | ||
return node.await === true; | ||
} | ||
}; | ||
exports.ForAwaitStatement = ForAwaitStatement; |
@@ -24,5 +24,5 @@ "use strict"; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -51,3 +51,3 @@ }; | ||
} else if (this.isStatementOrBlock()) { | ||
const shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null); | ||
var shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null); | ||
this.replaceWith(t().blockStatement(shouldInsertCurrentNode ? [this.node] : [])); | ||
@@ -61,9 +61,12 @@ return this.unshiftContainer("body", nodes); | ||
function _containerInsert(from, nodes) { | ||
var _container; | ||
this.updateSiblingKeys(from, nodes.length); | ||
const paths = []; | ||
this.container.splice(from, 0, ...nodes); | ||
var paths = []; | ||
for (let i = 0; i < nodes.length; i++) { | ||
const to = from + i; | ||
const path = this.getSibling(to); | ||
(_container = this.container).splice.apply(_container, [from, 0].concat(nodes)); | ||
for (var i = 0; i < nodes.length; i++) { | ||
var to = from + i; | ||
var path = this.getSibling(to); | ||
paths.push(path); | ||
@@ -76,10 +79,25 @@ | ||
const contexts = this._getQueueContexts(); | ||
var contexts = this._getQueueContexts(); | ||
for (const path of paths) { | ||
path.setScope(); | ||
path.debug("Inserted."); | ||
for (var _i = 0; _i < paths.length; _i++) { | ||
var _path = paths[_i]; | ||
for (const context of contexts) { | ||
context.maybeQueue(path, true); | ||
_path.setScope(); | ||
_path.debug("Inserted."); | ||
for (var _iterator = contexts, _isArray = Array.isArray(_iterator), _i2 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i2 >= _iterator.length) break; | ||
_ref = _iterator[_i2++]; | ||
} else { | ||
_i2 = _iterator.next(); | ||
if (_i2.done) break; | ||
_ref = _i2.value; | ||
} | ||
var context = _ref; | ||
context.maybeQueue(_path, true); | ||
} | ||
@@ -108,3 +126,3 @@ } | ||
if (this.node) { | ||
const temp = this.scope.generateDeclaredUidIdentifier(); | ||
var temp = this.scope.generateDeclaredUidIdentifier(); | ||
nodes.unshift(t().expressionStatement(t().assignmentExpression("=", t().cloneNode(temp), this.node))); | ||
@@ -118,3 +136,3 @@ nodes.push(t().expressionStatement(t().cloneNode(temp))); | ||
} else if (this.isStatementOrBlock()) { | ||
const shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null); | ||
var shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null); | ||
this.replaceWith(t().blockStatement(shouldInsertCurrentNode ? [this.node] : [])); | ||
@@ -130,6 +148,6 @@ return this.pushContainer("body", nodes); | ||
const paths = _cache.path.get(this.parent); | ||
var paths = _cache.path.get(this.parent); | ||
for (let i = 0; i < paths.length; i++) { | ||
const path = paths[i]; | ||
for (var i = 0; i < paths.length; i++) { | ||
var path = paths[i]; | ||
@@ -151,5 +169,5 @@ if (path.key >= fromIndex) { | ||
for (let i = 0; i < nodes.length; i++) { | ||
const node = nodes[i]; | ||
let msg; | ||
for (var i = 0; i < nodes.length; i++) { | ||
var node = nodes[i]; | ||
var msg = void 0; | ||
@@ -167,4 +185,4 @@ if (!node) { | ||
if (msg) { | ||
const type = Array.isArray(node) ? "array" : typeof node; | ||
throw new Error(`Node list ${msg} with the index of ${i} and type of ${type}`); | ||
var type = Array.isArray(node) ? "array" : typeof node; | ||
throw new Error("Node list " + msg + " with the index of " + i + " and type of " + type); | ||
} | ||
@@ -181,7 +199,7 @@ } | ||
const path = _index.default.get({ | ||
var path = _index.default.get({ | ||
parentPath: this, | ||
parent: this.node, | ||
container: this.node[listKey], | ||
listKey, | ||
listKey: listKey, | ||
key: 0 | ||
@@ -197,9 +215,9 @@ }); | ||
nodes = this._verifyNodeList(nodes); | ||
const container = this.node[listKey]; | ||
var container = this.node[listKey]; | ||
const path = _index.default.get({ | ||
var path = _index.default.get({ | ||
parentPath: this, | ||
parent: this.node, | ||
container: container, | ||
listKey, | ||
listKey: listKey, | ||
key: container.length | ||
@@ -211,5 +229,9 @@ }); | ||
function hoist(scope = this.scope) { | ||
const hoister = new _hoister.default(this, scope); | ||
function hoist(scope) { | ||
if (scope === void 0) { | ||
scope = this.scope; | ||
} | ||
var hoister = new _hoister.default(this, scope); | ||
return hoister.run(); | ||
} |
@@ -36,8 +36,15 @@ "use strict"; | ||
function _removeFromScope() { | ||
const bindings = this.getBindingIdentifiers(); | ||
Object.keys(bindings).forEach(name => this.scope.removeBinding(name)); | ||
var _this = this; | ||
var bindings = this.getBindingIdentifiers(); | ||
Object.keys(bindings).forEach(function (name) { | ||
return _this.scope.removeBinding(name); | ||
}); | ||
} | ||
function _callRemovalHooks() { | ||
for (const fn of _removalHooks.hooks) { | ||
var _arr = _removalHooks.hooks; | ||
for (var _i = 0; _i < _arr.length; _i++) { | ||
var fn = _arr[_i]; | ||
if (fn(this, this.parentPath)) return true; | ||
@@ -44,0 +51,0 @@ } |
@@ -14,5 +14,5 @@ "use strict"; | ||
function _codeFrame() { | ||
const data = require("@babel/code-frame"); | ||
var data = require("@babel/code-frame"); | ||
_codeFrame = function () { | ||
_codeFrame = function _codeFrame() { | ||
return data; | ||
@@ -29,5 +29,5 @@ }; | ||
function _babylon() { | ||
const data = require("babylon"); | ||
var data = require("babylon"); | ||
_babylon = function () { | ||
_babylon = function _babylon() { | ||
return data; | ||
@@ -40,5 +40,5 @@ }; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -54,12 +54,11 @@ }; | ||
const hoistVariablesVisitor = { | ||
Function(path) { | ||
var hoistVariablesVisitor = { | ||
Function: function Function(path) { | ||
path.skip(); | ||
}, | ||
VariableDeclaration(path) { | ||
VariableDeclaration: function VariableDeclaration(path) { | ||
if (path.node.kind !== "var") return; | ||
const bindings = path.getBindingIdentifiers(); | ||
var bindings = path.getBindingIdentifiers(); | ||
for (const key in bindings) { | ||
for (var key in bindings) { | ||
path.scope.push({ | ||
@@ -70,5 +69,8 @@ id: bindings[key] | ||
const exprs = []; | ||
var exprs = []; | ||
var _arr = path.node.declarations; | ||
for (const declar of path.node.declarations) { | ||
for (var _i = 0; _i < _arr.length; _i++) { | ||
var declar = _arr[_i]; | ||
if (declar.init) { | ||
@@ -81,3 +83,2 @@ exprs.push(t().expressionStatement(t().assignmentExpression("=", declar.id, declar.init))); | ||
} | ||
}; | ||
@@ -91,3 +92,3 @@ | ||
this.node = this.container[this.key] = null; | ||
const paths = this.insertAfter(nodes); | ||
var paths = this.insertAfter(nodes); | ||
@@ -107,6 +108,6 @@ if (this.node) { | ||
try { | ||
replacement = `(${replacement})`; | ||
replacement = "(" + replacement + ")"; | ||
replacement = (0, _babylon().parse)(replacement); | ||
} catch (err) { | ||
const loc = err.loc; | ||
var loc = err.loc; | ||
@@ -164,3 +165,3 @@ if (loc) { | ||
let nodePath = ""; | ||
var nodePath = ""; | ||
@@ -180,3 +181,3 @@ if (this.isNodeType("Statement") && t().isExpression(replacement)) { | ||
const oldNode = this.node; | ||
var oldNode = this.node; | ||
@@ -207,3 +208,3 @@ if (oldNode) { | ||
this.debug(`Replace with ${node && node.type}`); | ||
this.debug("Replace with " + (node && node.type)); | ||
this.node = this.container[this.key] = node; | ||
@@ -214,3 +215,3 @@ } | ||
this.resync(); | ||
const toSequenceExpression = t().toSequenceExpression(nodes, this.scope); | ||
var toSequenceExpression = t().toSequenceExpression(nodes, this.scope); | ||
@@ -221,18 +222,35 @@ if (toSequenceExpression) { | ||
const container = t().arrowFunctionExpression([], t().blockStatement(nodes)); | ||
var container = t().arrowFunctionExpression([], t().blockStatement(nodes)); | ||
this.replaceWith(t().callExpression(container, [])); | ||
this.traverse(hoistVariablesVisitor); | ||
const completionRecords = this.get("callee").getCompletionRecords(); | ||
var completionRecords = this.get("callee").getCompletionRecords(); | ||
for (const path of completionRecords) { | ||
for (var _iterator = completionRecords, _isArray = Array.isArray(_iterator), _i2 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i2 >= _iterator.length) break; | ||
_ref = _iterator[_i2++]; | ||
} else { | ||
_i2 = _iterator.next(); | ||
if (_i2.done) break; | ||
_ref = _i2.value; | ||
} | ||
var path = _ref; | ||
if (!path.isExpressionStatement()) continue; | ||
const loop = path.findParent(path => path.isLoop()); | ||
var loop = path.findParent(function (path) { | ||
return path.isLoop(); | ||
}); | ||
if (loop) { | ||
let uid = loop.getData("expressionReplacementReturnUid"); | ||
var uid = loop.getData("expressionReplacementReturnUid"); | ||
if (!uid) { | ||
const callee = this.get("callee"); | ||
uid = callee.scope.generateDeclaredUidIdentifier("ret"); | ||
callee.get("body").pushContainer("body", t().returnStatement(t().cloneNode(uid))); | ||
var _callee = this.get("callee"); | ||
uid = _callee.scope.generateDeclaredUidIdentifier("ret"); | ||
_callee.get("body").pushContainer("body", t().returnStatement(t().cloneNode(uid))); | ||
loop.setData("expressionReplacementReturnUid", uid); | ||
@@ -249,3 +267,3 @@ } else { | ||
const callee = this.get("callee"); | ||
var callee = this.get("callee"); | ||
callee.arrowFunctionToExpression(); | ||
@@ -262,3 +280,3 @@ return callee.get("body.body"); | ||
const paths = this._containerInsertAfter(nodes); | ||
var paths = this._containerInsertAfter(nodes); | ||
@@ -265,0 +283,0 @@ this.remove(); |
@@ -8,9 +8,8 @@ "use strict"; | ||
class Binding { | ||
constructor({ | ||
identifier, | ||
scope, | ||
path, | ||
kind | ||
}) { | ||
var Binding = function () { | ||
function Binding(_ref) { | ||
var identifier = _ref.identifier, | ||
scope = _ref.scope, | ||
path = _ref.path, | ||
kind = _ref.kind; | ||
this.identifier = identifier; | ||
@@ -28,20 +27,22 @@ this.scope = scope; | ||
deoptValue() { | ||
var _proto = Binding.prototype; | ||
_proto.deoptValue = function deoptValue() { | ||
this.clearValue(); | ||
this.hasDeoptedValue = true; | ||
} | ||
}; | ||
setValue(value) { | ||
_proto.setValue = function setValue(value) { | ||
if (this.hasDeoptedValue) return; | ||
this.hasValue = true; | ||
this.value = value; | ||
} | ||
}; | ||
clearValue() { | ||
_proto.clearValue = function clearValue() { | ||
this.hasDeoptedValue = false; | ||
this.hasValue = false; | ||
this.value = null; | ||
} | ||
}; | ||
reassign(path) { | ||
_proto.reassign = function reassign(path) { | ||
this.constant = false; | ||
@@ -54,5 +55,5 @@ | ||
this.constantViolations.push(path); | ||
} | ||
}; | ||
reference(path) { | ||
_proto.reference = function reference(path) { | ||
if (this.referencePaths.indexOf(path) !== -1) { | ||
@@ -65,11 +66,12 @@ return; | ||
this.referencePaths.push(path); | ||
} | ||
}; | ||
dereference() { | ||
_proto.dereference = function dereference() { | ||
this.references--; | ||
this.referenced = !!this.references; | ||
} | ||
}; | ||
} | ||
return Binding; | ||
}(); | ||
exports.default = Binding; |
@@ -9,5 +9,5 @@ "use strict"; | ||
function _includes() { | ||
const data = _interopRequireDefault(require("lodash/includes")); | ||
var data = _interopRequireDefault(require("lodash/includes")); | ||
_includes = function () { | ||
_includes = function _includes() { | ||
return data; | ||
@@ -20,5 +20,5 @@ }; | ||
function _repeat() { | ||
const data = _interopRequireDefault(require("lodash/repeat")); | ||
var data = _interopRequireDefault(require("lodash/repeat")); | ||
_repeat = function () { | ||
_repeat = function _repeat() { | ||
return data; | ||
@@ -35,5 +35,5 @@ }; | ||
function _defaults() { | ||
const data = _interopRequireDefault(require("lodash/defaults")); | ||
var data = _interopRequireDefault(require("lodash/defaults")); | ||
_defaults = function () { | ||
_defaults = function _defaults() { | ||
return data; | ||
@@ -45,8 +45,8 @@ }; | ||
var _binding = _interopRequireDefault(require("./binding")); | ||
var _binding2 = _interopRequireDefault(require("./binding")); | ||
function _globals() { | ||
const data = _interopRequireDefault(require("globals")); | ||
var data = _interopRequireDefault(require("globals")); | ||
_globals = function () { | ||
_globals = function _globals() { | ||
return data; | ||
@@ -59,5 +59,5 @@ }; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -75,2 +75,6 @@ }; | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function gatherNodeParts(node, parts) { | ||
@@ -81,3 +85,6 @@ if (t().isModuleDeclaration(node)) { | ||
} else if (node.specifiers && node.specifiers.length) { | ||
for (const specifier of node.specifiers) { | ||
var _arr = node.specifiers; | ||
for (var _i = 0; _i < _arr.length; _i++) { | ||
var specifier = _arr[_i]; | ||
gatherNodeParts(specifier, parts); | ||
@@ -100,3 +107,6 @@ } | ||
} else if (t().isObjectExpression(node) || t().isObjectPattern(node)) { | ||
for (const prop of node.properties) { | ||
var _arr2 = node.properties; | ||
for (var _i2 = 0; _i2 < _arr2.length; _i2++) { | ||
var prop = _arr2[_i2]; | ||
gatherNodeParts(prop.key || prop.argument, parts); | ||
@@ -107,9 +117,12 @@ } | ||
const collectorVisitor = { | ||
For(path) { | ||
for (const key of t().FOR_INIT_KEYS) { | ||
const declar = path.get(key); | ||
var collectorVisitor = { | ||
For: function For(path) { | ||
var _arr3 = t().FOR_INIT_KEYS; | ||
for (var _i3 = 0; _i3 < _arr3.length; _i3++) { | ||
var key = _arr3[_i3]; | ||
var declar = path.get(key); | ||
if (declar.isVar()) { | ||
const parentScope = path.scope.getFunctionParent() || path.scope.getProgramParent(); | ||
var parentScope = path.scope.getFunctionParent() || path.scope.getProgramParent(); | ||
parentScope.registerBinding("var", declar); | ||
@@ -119,4 +132,3 @@ } | ||
}, | ||
Declaration(path) { | ||
Declaration: function Declaration(path) { | ||
if (path.isBlockScoped()) return; | ||
@@ -128,13 +140,11 @@ | ||
const parent = path.scope.getFunctionParent() || path.scope.getProgramParent(); | ||
var parent = path.scope.getFunctionParent() || path.scope.getProgramParent(); | ||
parent.registerDeclaration(path); | ||
}, | ||
ReferencedIdentifier(path, state) { | ||
ReferencedIdentifier: function ReferencedIdentifier(path, state) { | ||
state.references.push(path); | ||
}, | ||
ForXStatement: function ForXStatement(path, state) { | ||
var left = path.get("left"); | ||
ForXStatement(path, state) { | ||
const left = path.get("left"); | ||
if (left.isPattern() || left.isIdentifier()) { | ||
@@ -144,23 +154,24 @@ state.constantViolations.push(path); | ||
}, | ||
ExportDeclaration: { | ||
exit(path) { | ||
const { | ||
node, | ||
scope | ||
} = path; | ||
const declar = node.declaration; | ||
exit: function exit(path) { | ||
var node = path.node, | ||
scope = path.scope; | ||
var declar = node.declaration; | ||
if (t().isClassDeclaration(declar) || t().isFunctionDeclaration(declar)) { | ||
const id = declar.id; | ||
if (!id) return; | ||
const binding = scope.getBinding(id.name); | ||
var _id = declar.id; | ||
if (!_id) return; | ||
var binding = scope.getBinding(_id.name); | ||
if (binding) binding.reference(path); | ||
} else if (t().isVariableDeclaration(declar)) { | ||
for (const decl of declar.declarations) { | ||
const ids = t().getBindingIdentifiers(decl); | ||
var _arr4 = declar.declarations; | ||
for (const name in ids) { | ||
const binding = scope.getBinding(name); | ||
if (binding) binding.reference(path); | ||
for (var _i4 = 0; _i4 < _arr4.length; _i4++) { | ||
var decl = _arr4[_i4]; | ||
var ids = t().getBindingIdentifiers(decl); | ||
for (var name in ids) { | ||
var _binding = scope.getBinding(name); | ||
if (_binding) _binding.reference(path); | ||
} | ||
@@ -170,19 +181,14 @@ } | ||
} | ||
}, | ||
LabeledStatement(path) { | ||
LabeledStatement: function LabeledStatement(path) { | ||
path.scope.getProgramParent().addGlobal(path.node); | ||
path.scope.getBlockParent().registerDeclaration(path); | ||
}, | ||
AssignmentExpression(path, state) { | ||
AssignmentExpression: function AssignmentExpression(path, state) { | ||
state.assignments.push(path); | ||
}, | ||
UpdateExpression(path, state) { | ||
UpdateExpression: function UpdateExpression(path, state) { | ||
state.constantViolations.push(path); | ||
}, | ||
UnaryExpression(path, state) { | ||
UnaryExpression: function UnaryExpression(path, state) { | ||
if (path.node.operator === "delete") { | ||
@@ -192,20 +198,20 @@ state.constantViolations.push(path); | ||
}, | ||
BlockScoped(path) { | ||
let scope = path.scope; | ||
BlockScoped: function BlockScoped(path) { | ||
var scope = path.scope; | ||
if (scope.path === path) scope = scope.parent; | ||
scope.getBlockParent().registerDeclaration(path); | ||
}, | ||
ClassDeclaration(path) { | ||
const id = path.node.id; | ||
ClassDeclaration: function ClassDeclaration(path) { | ||
var id = path.node.id; | ||
if (!id) return; | ||
const name = id.name; | ||
var name = id.name; | ||
path.scope.bindings[name] = path.scope.getBinding(name); | ||
}, | ||
Block: function Block(path) { | ||
var paths = path.get("body"); | ||
var _arr5 = paths; | ||
Block(path) { | ||
const paths = path.get("body"); | ||
for (var _i5 = 0; _i5 < _arr5.length; _i5++) { | ||
var bodyPath = _arr5[_i5]; | ||
for (const bodyPath of paths) { | ||
if (bodyPath.isFunctionDeclaration()) { | ||
@@ -216,13 +222,10 @@ path.scope.getBlockParent().registerDeclaration(bodyPath); | ||
} | ||
}; | ||
let uid = 0; | ||
var uid = 0; | ||
class Scope { | ||
constructor(path) { | ||
const { | ||
node | ||
} = path; | ||
var Scope = function () { | ||
function Scope(path) { | ||
var node = path.node; | ||
const cached = _cache.scope.get(node); | ||
var cached = _cache.scope.get(node); | ||
@@ -241,35 +244,28 @@ if (cached && cached.path === path) { | ||
get parent() { | ||
const parent = this.path.findParent(p => p.isScope()); | ||
return parent && parent.scope; | ||
} | ||
var _proto = Scope.prototype; | ||
get parentBlock() { | ||
return this.path.parent; | ||
} | ||
get hub() { | ||
return this.path.hub; | ||
} | ||
traverse(node, opts, state) { | ||
_proto.traverse = function traverse(node, opts, state) { | ||
(0, _index.default)(node, opts, this, state, this.path); | ||
} | ||
}; | ||
generateDeclaredUidIdentifier(name) { | ||
const id = this.generateUidIdentifier(name); | ||
_proto.generateDeclaredUidIdentifier = function generateDeclaredUidIdentifier(name) { | ||
var id = this.generateUidIdentifier(name); | ||
this.push({ | ||
id | ||
id: id | ||
}); | ||
return t().cloneNode(id); | ||
} | ||
}; | ||
generateUidIdentifier(name) { | ||
_proto.generateUidIdentifier = function generateUidIdentifier(name) { | ||
return t().identifier(this.generateUid(name)); | ||
} | ||
}; | ||
generateUid(name = "temp") { | ||
_proto.generateUid = function generateUid(name) { | ||
if (name === void 0) { | ||
name = "temp"; | ||
} | ||
name = t().toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, ""); | ||
let uid; | ||
let i = 0; | ||
var uid; | ||
var i = 0; | ||
@@ -281,16 +277,16 @@ do { | ||
const program = this.getProgramParent(); | ||
var program = this.getProgramParent(); | ||
program.references[uid] = true; | ||
program.uids[uid] = true; | ||
return uid; | ||
} | ||
}; | ||
_generateUid(name, i) { | ||
let id = name; | ||
_proto._generateUid = function _generateUid(name, i) { | ||
var id = name; | ||
if (i > 1) id += i; | ||
return `_${id}`; | ||
} | ||
return "_" + id; | ||
}; | ||
generateUidBasedOnNode(parent, defaultName) { | ||
let node = parent; | ||
_proto.generateUidBasedOnNode = function generateUidBasedOnNode(parent, defaultName) { | ||
var node = parent; | ||
@@ -305,14 +301,14 @@ if (t().isAssignmentExpression(parent)) { | ||
const parts = []; | ||
var parts = []; | ||
gatherNodeParts(node, parts); | ||
let id = parts.join("$"); | ||
var id = parts.join("$"); | ||
id = id.replace(/^_/, "") || defaultName || "ref"; | ||
return this.generateUid(id.slice(0, 20)); | ||
} | ||
}; | ||
generateUidIdentifierBasedOnNode(parent, defaultName) { | ||
_proto.generateUidIdentifierBasedOnNode = function generateUidIdentifierBasedOnNode(parent, defaultName) { | ||
return t().identifier(this.generateUidBasedOnNode(parent, defaultName)); | ||
} | ||
}; | ||
isStatic(node) { | ||
_proto.isStatic = function isStatic(node) { | ||
if (t().isThisExpression(node) || t().isSuper(node)) { | ||
@@ -323,3 +319,3 @@ return true; | ||
if (t().isIdentifier(node)) { | ||
const binding = this.getBinding(node.name); | ||
var binding = this.getBinding(node.name); | ||
@@ -334,34 +330,34 @@ if (binding) { | ||
return false; | ||
} | ||
}; | ||
maybeGenerateMemoised(node, dontPush) { | ||
_proto.maybeGenerateMemoised = function maybeGenerateMemoised(node, dontPush) { | ||
if (this.isStatic(node)) { | ||
return null; | ||
} else { | ||
const id = this.generateUidIdentifierBasedOnNode(node); | ||
var _id2 = this.generateUidIdentifierBasedOnNode(node); | ||
if (!dontPush) { | ||
this.push({ | ||
id | ||
id: _id2 | ||
}); | ||
return t().cloneNode(id); | ||
return t().cloneNode(_id2); | ||
} | ||
return id; | ||
return _id2; | ||
} | ||
} | ||
}; | ||
checkBlockScopedCollisions(local, kind, name, id) { | ||
_proto.checkBlockScopedCollisions = function checkBlockScopedCollisions(local, kind, name, id) { | ||
if (kind === "param") return; | ||
if (local.kind === "local") return; | ||
if (kind === "hoisted" && local.kind === "let") return; | ||
const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && (kind === "let" || kind === "const"); | ||
var duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && (kind === "let" || kind === "const"); | ||
if (duplicate) { | ||
throw this.hub.file.buildCodeFrameError(id, `Duplicate declaration "${name}"`, TypeError); | ||
throw this.hub.file.buildCodeFrameError(id, "Duplicate declaration \"" + name + "\"", TypeError); | ||
} | ||
} | ||
}; | ||
rename(oldName, newName, block) { | ||
const binding = this.getBinding(oldName); | ||
_proto.rename = function rename(oldName, newName, block) { | ||
var binding = this.getBinding(oldName); | ||
@@ -372,5 +368,5 @@ if (binding) { | ||
} | ||
} | ||
}; | ||
_renameFromMap(map, oldName, newName, value) { | ||
_proto._renameFromMap = function _renameFromMap(map, oldName, newName, value) { | ||
if (map[oldName]) { | ||
@@ -380,8 +376,8 @@ map[newName] = value; | ||
} | ||
} | ||
}; | ||
dump() { | ||
const sep = (0, _repeat().default)("-", 60); | ||
_proto.dump = function dump() { | ||
var sep = (0, _repeat().default)("-", 60); | ||
console.log(sep); | ||
let scope = this; | ||
var scope = this; | ||
@@ -391,4 +387,4 @@ do { | ||
for (const name in scope.bindings) { | ||
const binding = scope.bindings[name]; | ||
for (var name in scope.bindings) { | ||
var binding = scope.bindings[name]; | ||
console.log(" -", name, { | ||
@@ -404,9 +400,9 @@ constant: binding.constant, | ||
console.log(sep); | ||
} | ||
}; | ||
toArray(node, i) { | ||
const file = this.hub.file; | ||
_proto.toArray = function toArray(node, i) { | ||
var file = this.hub.file; | ||
if (t().isIdentifier(node)) { | ||
const binding = this.getBinding(node.name); | ||
var binding = this.getBinding(node.name); | ||
@@ -428,4 +424,4 @@ if (binding && binding.constant && binding.path.isGenericType("Array")) { | ||
let helperName; | ||
const args = [node]; | ||
var helperName; | ||
var args = [node]; | ||
@@ -442,17 +438,17 @@ if (i === true) { | ||
return t().callExpression(file.addHelper(helperName), args); | ||
} | ||
}; | ||
hasLabel(name) { | ||
_proto.hasLabel = function hasLabel(name) { | ||
return !!this.getLabel(name); | ||
} | ||
}; | ||
getLabel(name) { | ||
_proto.getLabel = function getLabel(name) { | ||
return this.labels.get(name); | ||
} | ||
}; | ||
registerLabel(path) { | ||
_proto.registerLabel = function registerLabel(path) { | ||
this.labels.set(path.node.label.name, path); | ||
} | ||
}; | ||
registerDeclaration(path) { | ||
_proto.registerDeclaration = function registerDeclaration(path) { | ||
if (path.isFlow()) return; | ||
@@ -465,5 +461,7 @@ | ||
} else if (path.isVariableDeclaration()) { | ||
const declarations = path.get("declarations"); | ||
var declarations = path.get("declarations"); | ||
var _arr6 = declarations; | ||
for (const declar of declarations) { | ||
for (var _i6 = 0; _i6 < _arr6.length; _i6++) { | ||
var declar = _arr6[_i6]; | ||
this.registerBinding(path.node.kind, declar); | ||
@@ -474,12 +472,14 @@ } | ||
} else if (path.isImportDeclaration()) { | ||
const specifiers = path.get("specifiers"); | ||
var specifiers = path.get("specifiers"); | ||
var _arr7 = specifiers; | ||
for (const specifier of specifiers) { | ||
for (var _i7 = 0; _i7 < _arr7.length; _i7++) { | ||
var specifier = _arr7[_i7]; | ||
this.registerBinding("module", specifier); | ||
} | ||
} else if (path.isExportDeclaration()) { | ||
const declar = path.get("declaration"); | ||
var _declar = path.get("declaration"); | ||
if (declar.isClassDeclaration() || declar.isFunctionDeclaration() || declar.isVariableDeclaration()) { | ||
this.registerDeclaration(declar); | ||
if (_declar.isClassDeclaration() || _declar.isFunctionDeclaration() || _declar.isVariableDeclaration()) { | ||
this.registerDeclaration(_declar); | ||
} | ||
@@ -489,5 +489,5 @@ } else { | ||
} | ||
} | ||
}; | ||
buildUndefinedNode() { | ||
_proto.buildUndefinedNode = function buildUndefinedNode() { | ||
if (this.hasBinding("undefined")) { | ||
@@ -498,20 +498,36 @@ return t().unaryExpression("void", t().numericLiteral(0), true); | ||
} | ||
} | ||
}; | ||
registerConstantViolation(path) { | ||
const ids = path.getBindingIdentifiers(); | ||
_proto.registerConstantViolation = function registerConstantViolation(path) { | ||
var ids = path.getBindingIdentifiers(); | ||
for (const name in ids) { | ||
const binding = this.getBinding(name); | ||
for (var name in ids) { | ||
var binding = this.getBinding(name); | ||
if (binding) binding.reassign(path); | ||
} | ||
} | ||
}; | ||
registerBinding(kind, path, bindingPath = path) { | ||
_proto.registerBinding = function registerBinding(kind, path, bindingPath) { | ||
if (bindingPath === void 0) { | ||
bindingPath = path; | ||
} | ||
if (!kind) throw new ReferenceError("no `kind`"); | ||
if (path.isVariableDeclaration()) { | ||
const declarators = path.get("declarations"); | ||
var declarators = path.get("declarations"); | ||
for (const declar of declarators) { | ||
for (var _iterator = declarators, _isArray = Array.isArray(_iterator), _i8 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i8 >= _iterator.length) break; | ||
_ref = _iterator[_i8++]; | ||
} else { | ||
_i8 = _iterator.next(); | ||
if (_i8.done) break; | ||
_ref = _i8.value; | ||
} | ||
var declar = _ref; | ||
this.registerBinding(kind, declar); | ||
@@ -523,12 +539,15 @@ } | ||
const parent = this.getProgramParent(); | ||
const ids = path.getBindingIdentifiers(true); | ||
var parent = this.getProgramParent(); | ||
var ids = path.getBindingIdentifiers(true); | ||
for (const name in ids) { | ||
for (const id of ids[name]) { | ||
const local = this.getOwnBinding(name); | ||
for (var name in ids) { | ||
var _arr8 = ids[name]; | ||
for (var _i9 = 0; _i9 < _arr8.length; _i9++) { | ||
var _id3 = _arr8[_i9]; | ||
var local = this.getOwnBinding(name); | ||
if (local) { | ||
if (local.identifier === id) continue; | ||
this.checkBlockScopedCollisions(local, kind, name, id); | ||
if (local.identifier === _id3) continue; | ||
this.checkBlockScopedCollisions(local, kind, name, _id3); | ||
} | ||
@@ -541,4 +560,4 @@ | ||
} else { | ||
this.bindings[name] = new _binding.default({ | ||
identifier: id, | ||
this.bindings[name] = new _binding2.default({ | ||
identifier: _id3, | ||
scope: this, | ||
@@ -551,10 +570,10 @@ path: bindingPath, | ||
} | ||
} | ||
}; | ||
addGlobal(node) { | ||
_proto.addGlobal = function addGlobal(node) { | ||
this.globals[node.name] = node; | ||
} | ||
}; | ||
hasUid(name) { | ||
let scope = this; | ||
_proto.hasUid = function hasUid(name) { | ||
var scope = this; | ||
@@ -566,6 +585,6 @@ do { | ||
return false; | ||
} | ||
}; | ||
hasGlobal(name) { | ||
let scope = this; | ||
_proto.hasGlobal = function hasGlobal(name) { | ||
var scope = this; | ||
@@ -577,6 +596,6 @@ do { | ||
return false; | ||
} | ||
}; | ||
hasReference(name) { | ||
let scope = this; | ||
_proto.hasReference = function hasReference(name) { | ||
var scope = this; | ||
@@ -588,7 +607,7 @@ do { | ||
return false; | ||
} | ||
}; | ||
isPure(node, constantsOnly) { | ||
_proto.isPure = function isPure(node, constantsOnly) { | ||
if (t().isIdentifier(node)) { | ||
const binding = this.getBinding(node.name); | ||
var binding = this.getBinding(node.name); | ||
if (!binding) return false; | ||
@@ -604,3 +623,15 @@ if (constantsOnly) return binding.constant; | ||
} else if (t().isClassBody(node)) { | ||
for (const method of node.body) { | ||
for (var _iterator2 = node.body, _isArray2 = Array.isArray(_iterator2), _i10 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { | ||
var _ref2; | ||
if (_isArray2) { | ||
if (_i10 >= _iterator2.length) break; | ||
_ref2 = _iterator2[_i10++]; | ||
} else { | ||
_i10 = _iterator2.next(); | ||
if (_i10.done) break; | ||
_ref2 = _i10.value; | ||
} | ||
var method = _ref2; | ||
if (!this.isPure(method, constantsOnly)) return false; | ||
@@ -613,3 +644,6 @@ } | ||
} else if (t().isArrayExpression(node)) { | ||
for (const elem of node.elements) { | ||
var _arr9 = node.elements; | ||
for (var _i11 = 0; _i11 < _arr9.length; _i11++) { | ||
var elem = _arr9[_i11]; | ||
if (!this.isPure(elem, constantsOnly)) return false; | ||
@@ -620,3 +654,6 @@ } | ||
} else if (t().isObjectExpression(node)) { | ||
for (const prop of node.properties) { | ||
var _arr10 = node.properties; | ||
for (var _i12 = 0; _i12 < _arr10.length; _i12++) { | ||
var prop = _arr10[_i12]; | ||
if (!this.isPure(prop, constantsOnly)) return false; | ||
@@ -638,3 +675,6 @@ } | ||
} else if (t().isTemplateLiteral(node)) { | ||
for (const expression of node.expressions) { | ||
var _arr11 = node.expressions; | ||
for (var _i13 = 0; _i13 < _arr11.length; _i13++) { | ||
var expression = _arr11[_i13]; | ||
if (!this.isPure(expression, constantsOnly)) return false; | ||
@@ -647,32 +687,32 @@ } | ||
} | ||
} | ||
}; | ||
setData(key, val) { | ||
_proto.setData = function setData(key, val) { | ||
return this.data[key] = val; | ||
} | ||
}; | ||
getData(key) { | ||
let scope = this; | ||
_proto.getData = function getData(key) { | ||
var scope = this; | ||
do { | ||
const data = scope.data[key]; | ||
var data = scope.data[key]; | ||
if (data != null) return data; | ||
} while (scope = scope.parent); | ||
} | ||
}; | ||
removeData(key) { | ||
let scope = this; | ||
_proto.removeData = function removeData(key) { | ||
var scope = this; | ||
do { | ||
const data = scope.data[key]; | ||
var data = scope.data[key]; | ||
if (data != null) scope.data[key] = null; | ||
} while (scope = scope.parent); | ||
} | ||
}; | ||
init() { | ||
_proto.init = function init() { | ||
if (!this.references) this.crawl(); | ||
} | ||
}; | ||
crawl() { | ||
const path = this.path; | ||
_proto.crawl = function crawl() { | ||
var path = this.path; | ||
this.references = Object.create(null); | ||
@@ -685,4 +725,7 @@ this.bindings = Object.create(null); | ||
if (path.isLoop()) { | ||
for (const key of t().FOR_INIT_KEYS) { | ||
const node = path.get(key); | ||
var _arr12 = t().FOR_INIT_KEYS; | ||
for (var _i14 = 0; _i14 < _arr12.length; _i14++) { | ||
var key = _arr12[_i14]; | ||
var node = path.get(key); | ||
if (node.isBlockScoped()) this.registerBinding(node.node.kind, node); | ||
@@ -705,5 +748,17 @@ } | ||
if (path.isFunction()) { | ||
const params = path.get("params"); | ||
var params = path.get("params"); | ||
for (const param of params) { | ||
for (var _iterator3 = params, _isArray3 = Array.isArray(_iterator3), _i15 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) { | ||
var _ref3; | ||
if (_isArray3) { | ||
if (_i15 >= _iterator3.length) break; | ||
_ref3 = _iterator3[_i15++]; | ||
} else { | ||
_i15 = _iterator3.next(); | ||
if (_i15.done) break; | ||
_ref3 = _i15.value; | ||
} | ||
var param = _ref3; | ||
this.registerBinding("param", param); | ||
@@ -717,5 +772,5 @@ } | ||
const parent = this.getProgramParent(); | ||
var parent = this.getProgramParent(); | ||
if (parent.crawling) return; | ||
const state = { | ||
var state = { | ||
references: [], | ||
@@ -729,18 +784,44 @@ constantViolations: [], | ||
for (const path of state.assignments) { | ||
const ids = path.getBindingIdentifiers(); | ||
let programParent; | ||
for (var _iterator4 = state.assignments, _isArray4 = Array.isArray(_iterator4), _i16 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) { | ||
var _ref4; | ||
for (const name in ids) { | ||
if (path.scope.getBinding(name)) continue; | ||
programParent = programParent || path.scope.getProgramParent(); | ||
if (_isArray4) { | ||
if (_i16 >= _iterator4.length) break; | ||
_ref4 = _iterator4[_i16++]; | ||
} else { | ||
_i16 = _iterator4.next(); | ||
if (_i16.done) break; | ||
_ref4 = _i16.value; | ||
} | ||
var _path = _ref4; | ||
var ids = _path.getBindingIdentifiers(); | ||
var programParent = void 0; | ||
for (var name in ids) { | ||
if (_path.scope.getBinding(name)) continue; | ||
programParent = programParent || _path.scope.getProgramParent(); | ||
programParent.addGlobal(ids[name]); | ||
} | ||
path.scope.registerConstantViolation(path); | ||
_path.scope.registerConstantViolation(_path); | ||
} | ||
for (const ref of state.references) { | ||
const binding = ref.scope.getBinding(ref.node.name); | ||
for (var _iterator5 = state.references, _isArray5 = Array.isArray(_iterator5), _i17 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) { | ||
var _ref5; | ||
if (_isArray5) { | ||
if (_i17 >= _iterator5.length) break; | ||
_ref5 = _iterator5[_i17++]; | ||
} else { | ||
_i17 = _iterator5.next(); | ||
if (_i17.done) break; | ||
_ref5 = _i17.value; | ||
} | ||
var ref = _ref5; | ||
var binding = ref.scope.getBinding(ref.node.name); | ||
if (binding) { | ||
@@ -753,9 +834,22 @@ binding.reference(ref); | ||
for (const path of state.constantViolations) { | ||
path.scope.registerConstantViolation(path); | ||
for (var _iterator6 = state.constantViolations, _isArray6 = Array.isArray(_iterator6), _i18 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) { | ||
var _ref6; | ||
if (_isArray6) { | ||
if (_i18 >= _iterator6.length) break; | ||
_ref6 = _iterator6[_i18++]; | ||
} else { | ||
_i18 = _iterator6.next(); | ||
if (_i18.done) break; | ||
_ref6 = _i18.value; | ||
} | ||
var _path2 = _ref6; | ||
_path2.scope.registerConstantViolation(_path2); | ||
} | ||
} | ||
}; | ||
push(opts) { | ||
let path = this.path; | ||
_proto.push = function push(opts) { | ||
var path = this.path; | ||
@@ -775,22 +869,25 @@ if (!path.isBlockStatement() && !path.isProgram()) { | ||
const unique = opts.unique; | ||
const kind = opts.kind || "var"; | ||
const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist; | ||
const dataKey = `declaration:${kind}:${blockHoist}`; | ||
let declarPath = !unique && path.getData(dataKey); | ||
var unique = opts.unique; | ||
var kind = opts.kind || "var"; | ||
var blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist; | ||
var dataKey = "declaration:" + kind + ":" + blockHoist; | ||
var declarPath = !unique && path.getData(dataKey); | ||
if (!declarPath) { | ||
const declar = t().variableDeclaration(kind, []); | ||
var declar = t().variableDeclaration(kind, []); | ||
declar._blockHoist = blockHoist; | ||
[declarPath] = path.unshiftContainer("body", [declar]); | ||
var _path$unshiftContaine = path.unshiftContainer("body", [declar]); | ||
declarPath = _path$unshiftContaine[0]; | ||
if (!unique) path.setData(dataKey, declarPath); | ||
} | ||
const declarator = t().variableDeclarator(opts.id, opts.init); | ||
var declarator = t().variableDeclarator(opts.id, opts.init); | ||
declarPath.node.declarations.push(declarator); | ||
this.registerBinding(kind, declarPath.get("declarations").pop()); | ||
} | ||
}; | ||
getProgramParent() { | ||
let scope = this; | ||
_proto.getProgramParent = function getProgramParent() { | ||
var scope = this; | ||
@@ -804,6 +901,6 @@ do { | ||
throw new Error("Couldn't find a Program"); | ||
} | ||
}; | ||
getFunctionParent() { | ||
let scope = this; | ||
_proto.getFunctionParent = function getFunctionParent() { | ||
var scope = this; | ||
@@ -817,6 +914,6 @@ do { | ||
return null; | ||
} | ||
}; | ||
getBlockParent() { | ||
let scope = this; | ||
_proto.getBlockParent = function getBlockParent() { | ||
var scope = this; | ||
@@ -830,7 +927,7 @@ do { | ||
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program..."); | ||
} | ||
}; | ||
getAllBindings() { | ||
const ids = Object.create(null); | ||
let scope = this; | ||
_proto.getAllBindings = function getAllBindings() { | ||
var ids = Object.create(null); | ||
var scope = this; | ||
@@ -843,13 +940,15 @@ do { | ||
return ids; | ||
} | ||
}; | ||
getAllBindingsOfKind() { | ||
const ids = Object.create(null); | ||
_proto.getAllBindingsOfKind = function getAllBindingsOfKind() { | ||
var ids = Object.create(null); | ||
var _arr13 = arguments; | ||
for (const kind of arguments) { | ||
let scope = this; | ||
for (var _i19 = 0; _i19 < _arr13.length; _i19++) { | ||
var kind = _arr13[_i19]; | ||
var scope = this; | ||
do { | ||
for (const name in scope.bindings) { | ||
const binding = scope.bindings[name]; | ||
for (var name in scope.bindings) { | ||
var binding = scope.bindings[name]; | ||
if (binding.kind === kind) ids[name] = binding; | ||
@@ -863,36 +962,36 @@ } | ||
return ids; | ||
} | ||
}; | ||
bindingIdentifierEquals(name, node) { | ||
_proto.bindingIdentifierEquals = function bindingIdentifierEquals(name, node) { | ||
return this.getBindingIdentifier(name) === node; | ||
} | ||
}; | ||
getBinding(name) { | ||
let scope = this; | ||
_proto.getBinding = function getBinding(name) { | ||
var scope = this; | ||
do { | ||
const binding = scope.getOwnBinding(name); | ||
var binding = scope.getOwnBinding(name); | ||
if (binding) return binding; | ||
} while (scope = scope.parent); | ||
} | ||
}; | ||
getOwnBinding(name) { | ||
_proto.getOwnBinding = function getOwnBinding(name) { | ||
return this.bindings[name]; | ||
} | ||
}; | ||
getBindingIdentifier(name) { | ||
const info = this.getBinding(name); | ||
_proto.getBindingIdentifier = function getBindingIdentifier(name) { | ||
var info = this.getBinding(name); | ||
return info && info.identifier; | ||
} | ||
}; | ||
getOwnBindingIdentifier(name) { | ||
const binding = this.bindings[name]; | ||
_proto.getOwnBindingIdentifier = function getOwnBindingIdentifier(name) { | ||
var binding = this.bindings[name]; | ||
return binding && binding.identifier; | ||
} | ||
}; | ||
hasOwnBinding(name) { | ||
_proto.hasOwnBinding = function hasOwnBinding(name) { | ||
return !!this.getOwnBinding(name); | ||
} | ||
}; | ||
hasBinding(name, noGlobals) { | ||
_proto.hasBinding = function hasBinding(name, noGlobals) { | ||
if (!name) return false; | ||
@@ -905,10 +1004,10 @@ if (this.hasOwnBinding(name)) return true; | ||
return false; | ||
} | ||
}; | ||
parentHasBinding(name, noGlobals) { | ||
_proto.parentHasBinding = function parentHasBinding(name, noGlobals) { | ||
return this.parent && this.parent.hasBinding(name, noGlobals); | ||
} | ||
}; | ||
moveBindingTo(name, scope) { | ||
const info = this.getBinding(name); | ||
_proto.moveBindingTo = function moveBindingTo(name, scope) { | ||
var info = this.getBinding(name); | ||
@@ -920,10 +1019,10 @@ if (info) { | ||
} | ||
} | ||
}; | ||
removeOwnBinding(name) { | ||
_proto.removeOwnBinding = function removeOwnBinding(name) { | ||
delete this.bindings[name]; | ||
} | ||
}; | ||
removeBinding(name) { | ||
const info = this.getBinding(name); | ||
_proto.removeBinding = function removeBinding(name) { | ||
var info = this.getBinding(name); | ||
@@ -934,3 +1033,3 @@ if (info) { | ||
let scope = this; | ||
var scope = this; | ||
@@ -942,8 +1041,29 @@ do { | ||
} while (scope = scope.parent); | ||
} | ||
}; | ||
} | ||
_createClass(Scope, [{ | ||
key: "parent", | ||
get: function get() { | ||
var parent = this.path.findParent(function (p) { | ||
return p.isScope(); | ||
}); | ||
return parent && parent.scope; | ||
} | ||
}, { | ||
key: "parentBlock", | ||
get: function get() { | ||
return this.path.parent; | ||
} | ||
}, { | ||
key: "hub", | ||
get: function get() { | ||
return this.path.hub; | ||
} | ||
}]); | ||
return Scope; | ||
}(); | ||
exports.default = Scope; | ||
Scope.globals = Object.keys(_globals().default.builtin); | ||
Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"]; |
@@ -11,5 +11,5 @@ "use strict"; | ||
function _helperSplitExportDeclaration() { | ||
const data = _interopRequireDefault(require("@babel/helper-split-export-declaration")); | ||
var data = _interopRequireDefault(require("@babel/helper-split-export-declaration")); | ||
_helperSplitExportDeclaration = function () { | ||
_helperSplitExportDeclaration = function _helperSplitExportDeclaration() { | ||
return data; | ||
@@ -22,5 +22,5 @@ }; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -36,6 +36,6 @@ }; | ||
const renameVisitor = { | ||
ReferencedIdentifier({ | ||
node | ||
}, state) { | ||
var renameVisitor = { | ||
ReferencedIdentifier: function ReferencedIdentifier(_ref, state) { | ||
var node = _ref.node; | ||
if (node.name === state.oldName) { | ||
@@ -45,4 +45,3 @@ node.name = state.newName; | ||
}, | ||
Scope(path, state) { | ||
Scope: function Scope(path, state) { | ||
if (!path.scope.bindingIdentifierEquals(state.oldName, state.binding.identifier)) { | ||
@@ -52,15 +51,13 @@ path.skip(); | ||
}, | ||
"AssignmentExpression|Declaration": function AssignmentExpressionDeclaration(path, state) { | ||
var ids = path.getOuterBindingIdentifiers(); | ||
"AssignmentExpression|Declaration"(path, state) { | ||
const ids = path.getOuterBindingIdentifiers(); | ||
for (const name in ids) { | ||
for (var name in ids) { | ||
if (name === state.oldName) ids[name].name = state.newName; | ||
} | ||
} | ||
}; | ||
class Renamer { | ||
constructor(binding, oldName, newName) { | ||
var Renamer = function () { | ||
function Renamer(binding, oldName, newName) { | ||
this.newName = newName; | ||
@@ -71,5 +68,7 @@ this.oldName = oldName; | ||
maybeConvertFromExportDeclaration(parentDeclar) { | ||
const maybeExportDeclar = parentDeclar.parentPath; | ||
var _proto = Renamer.prototype; | ||
_proto.maybeConvertFromExportDeclaration = function maybeConvertFromExportDeclaration(parentDeclar) { | ||
var maybeExportDeclar = parentDeclar.parentPath; | ||
if (!maybeExportDeclar.isExportDeclaration()) { | ||
@@ -84,5 +83,5 @@ return; | ||
(0, _helperSplitExportDeclaration().default)(maybeExportDeclar); | ||
} | ||
}; | ||
maybeConvertFromClassFunctionDeclaration(path) { | ||
_proto.maybeConvertFromClassFunctionDeclaration = function maybeConvertFromClassFunctionDeclaration(path) { | ||
return; | ||
@@ -94,5 +93,5 @@ if (!path.isFunctionDeclaration() && !path.isClassDeclaration()) return; | ||
path.replaceWith(t().variableDeclaration("let", [t().variableDeclarator(t().identifier(this.newName), t().toExpression(path.node))])); | ||
} | ||
}; | ||
maybeConvertFromClassFunctionExpression(path) { | ||
_proto.maybeConvertFromClassFunctionExpression = function maybeConvertFromClassFunctionExpression(path) { | ||
return; | ||
@@ -106,15 +105,13 @@ if (!path.isFunctionExpression() && !path.isClassExpression()) return; | ||
path.replaceWith(t().assignmentExpression("=", t().identifier(this.newName), path.node)); | ||
} | ||
}; | ||
rename(block) { | ||
const { | ||
binding, | ||
oldName, | ||
newName | ||
} = this; | ||
const { | ||
scope, | ||
path | ||
} = binding; | ||
const parentDeclar = path.find(path => path.isDeclaration() || path.isFunctionExpression() || path.isClassExpression()); | ||
_proto.rename = function rename(block) { | ||
var binding = this.binding, | ||
oldName = this.oldName, | ||
newName = this.newName; | ||
var scope = binding.scope, | ||
path = binding.path; | ||
var parentDeclar = path.find(function (path) { | ||
return path.isDeclaration() || path.isFunctionExpression() || path.isClassExpression(); | ||
}); | ||
@@ -139,6 +136,7 @@ if (parentDeclar) { | ||
} | ||
} | ||
}; | ||
} | ||
return Renamer; | ||
}(); | ||
exports.default = Renamer; |
@@ -13,5 +13,5 @@ "use strict"; | ||
function t() { | ||
const data = _interopRequireWildcard(require("@babel/types")); | ||
var data = _interopRequireWildcard(require("@babel/types")); | ||
t = function () { | ||
t = function t() { | ||
return data; | ||
@@ -24,5 +24,5 @@ }; | ||
function _clone() { | ||
const data = _interopRequireDefault(require("lodash/clone")); | ||
var data = _interopRequireDefault(require("lodash/clone")); | ||
_clone = function () { | ||
_clone = function _clone() { | ||
return data; | ||
@@ -42,10 +42,22 @@ }; | ||
for (const nodeType in visitor) { | ||
for (var nodeType in visitor) { | ||
if (shouldIgnoreKey(nodeType)) continue; | ||
const parts = nodeType.split("|"); | ||
var parts = nodeType.split("|"); | ||
if (parts.length === 1) continue; | ||
const fns = visitor[nodeType]; | ||
var fns = visitor[nodeType]; | ||
delete visitor[nodeType]; | ||
for (const part of parts) { | ||
for (var _iterator = parts, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var part = _ref; | ||
visitor[part] = fns; | ||
@@ -60,35 +72,44 @@ } | ||
for (const nodeType of Object.keys(visitor)) { | ||
if (shouldIgnoreKey(nodeType)) continue; | ||
const wrapper = virtualTypes[nodeType]; | ||
var _arr = Object.keys(visitor); | ||
for (var _i2 = 0; _i2 < _arr.length; _i2++) { | ||
var _nodeType3 = _arr[_i2]; | ||
if (shouldIgnoreKey(_nodeType3)) continue; | ||
var wrapper = virtualTypes[_nodeType3]; | ||
if (!wrapper) continue; | ||
const fns = visitor[nodeType]; | ||
var _fns2 = visitor[_nodeType3]; | ||
for (const type in fns) { | ||
fns[type] = wrapCheck(wrapper, fns[type]); | ||
for (var type in _fns2) { | ||
_fns2[type] = wrapCheck(wrapper, _fns2[type]); | ||
} | ||
delete visitor[nodeType]; | ||
delete visitor[_nodeType3]; | ||
if (wrapper.types) { | ||
for (const type of wrapper.types) { | ||
if (visitor[type]) { | ||
mergePair(visitor[type], fns); | ||
var _arr2 = wrapper.types; | ||
for (var _i4 = 0; _i4 < _arr2.length; _i4++) { | ||
var _type = _arr2[_i4]; | ||
if (visitor[_type]) { | ||
mergePair(visitor[_type], _fns2); | ||
} else { | ||
visitor[type] = fns; | ||
visitor[_type] = _fns2; | ||
} | ||
} | ||
} else { | ||
mergePair(visitor, fns); | ||
mergePair(visitor, _fns2); | ||
} | ||
} | ||
for (const nodeType in visitor) { | ||
if (shouldIgnoreKey(nodeType)) continue; | ||
const fns = visitor[nodeType]; | ||
let aliases = t().FLIPPED_ALIAS_KEYS[nodeType]; | ||
const deprecratedKey = t().DEPRECATED_KEYS[nodeType]; | ||
for (var _nodeType in visitor) { | ||
if (shouldIgnoreKey(_nodeType)) continue; | ||
var _fns = visitor[_nodeType]; | ||
var aliases = t().FLIPPED_ALIAS_KEYS[_nodeType]; | ||
var deprecratedKey = t().DEPRECATED_KEYS[_nodeType]; | ||
if (deprecratedKey) { | ||
console.trace(`Visitor defined for ${nodeType} but it has been renamed to ${deprecratedKey}`); | ||
console.trace("Visitor defined for " + _nodeType + " but it has been renamed to " + deprecratedKey); | ||
aliases = [deprecratedKey]; | ||
@@ -98,11 +119,23 @@ } | ||
if (!aliases) continue; | ||
delete visitor[nodeType]; | ||
delete visitor[_nodeType]; | ||
for (const alias of aliases) { | ||
const existing = visitor[alias]; | ||
for (var _iterator2 = aliases, _isArray2 = Array.isArray(_iterator2), _i3 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { | ||
var _ref2; | ||
if (_isArray2) { | ||
if (_i3 >= _iterator2.length) break; | ||
_ref2 = _iterator2[_i3++]; | ||
} else { | ||
_i3 = _iterator2.next(); | ||
if (_i3.done) break; | ||
_ref2 = _i3.value; | ||
} | ||
var alias = _ref2; | ||
var existing = visitor[alias]; | ||
if (existing) { | ||
mergePair(existing, fns); | ||
mergePair(existing, _fns); | ||
} else { | ||
visitor[alias] = (0, _clone().default)(fns); | ||
visitor[alias] = (0, _clone().default)(_fns); | ||
} | ||
@@ -112,5 +145,5 @@ } | ||
for (const nodeType in visitor) { | ||
if (shouldIgnoreKey(nodeType)) continue; | ||
ensureCallbackArrays(visitor[nodeType]); | ||
for (var _nodeType2 in visitor) { | ||
if (shouldIgnoreKey(_nodeType2)) continue; | ||
ensureCallbackArrays(visitor[_nodeType2]); | ||
} | ||
@@ -128,3 +161,3 @@ | ||
for (const nodeType in visitor) { | ||
for (var nodeType in visitor) { | ||
if (nodeType === "enter" || nodeType === "exit") { | ||
@@ -137,13 +170,13 @@ validateVisitorMethods(nodeType, visitor[nodeType]); | ||
if (t().TYPES.indexOf(nodeType) < 0) { | ||
throw new Error(`You gave us a visitor for the node type ${nodeType} but it's not a valid type`); | ||
throw new Error("You gave us a visitor for the node type " + nodeType + " but it's not a valid type"); | ||
} | ||
const visitors = visitor[nodeType]; | ||
var visitors = visitor[nodeType]; | ||
if (typeof visitors === "object") { | ||
for (const visitorKey in visitors) { | ||
for (var visitorKey in visitors) { | ||
if (visitorKey === "enter" || visitorKey === "exit") { | ||
validateVisitorMethods(`${nodeType}.${visitorKey}`, visitors[visitorKey]); | ||
validateVisitorMethods(nodeType + "." + visitorKey, visitors[visitorKey]); | ||
} else { | ||
throw new Error("You passed `traverse()` a visitor object with the property " + `${nodeType} that has the invalid property ${visitorKey}`); | ||
throw new Error("You passed `traverse()` a visitor object with the property " + (nodeType + " that has the invalid property " + visitorKey)); | ||
} | ||
@@ -158,7 +191,20 @@ } | ||
function validateVisitorMethods(path, val) { | ||
const fns = [].concat(val); | ||
var fns = [].concat(val); | ||
for (const fn of fns) { | ||
for (var _iterator3 = fns, _isArray3 = Array.isArray(_iterator3), _i5 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) { | ||
var _ref3; | ||
if (_isArray3) { | ||
if (_i5 >= _iterator3.length) break; | ||
_ref3 = _iterator3[_i5++]; | ||
} else { | ||
_i5 = _iterator3.next(); | ||
if (_i5.done) break; | ||
_ref3 = _i5.value; | ||
} | ||
var fn = _ref3; | ||
if (typeof fn !== "function") { | ||
throw new TypeError(`Non-function found defined in ${path} with type ${typeof fn}`); | ||
throw new TypeError("Non-function found defined in " + path + " with type " + typeof fn); | ||
} | ||
@@ -168,12 +214,16 @@ } | ||
function merge(visitors, states = [], wrapper) { | ||
const rootVisitor = {}; | ||
function merge(visitors, states, wrapper) { | ||
if (states === void 0) { | ||
states = []; | ||
} | ||
for (let i = 0; i < visitors.length; i++) { | ||
const visitor = visitors[i]; | ||
const state = states[i]; | ||
var rootVisitor = {}; | ||
for (var i = 0; i < visitors.length; i++) { | ||
var visitor = visitors[i]; | ||
var state = states[i]; | ||
explode(visitor); | ||
for (const type in visitor) { | ||
let visitorType = visitor[type]; | ||
for (var type in visitor) { | ||
var visitorType = visitor[type]; | ||
@@ -184,3 +234,3 @@ if (state || wrapper) { | ||
const nodeVisitor = rootVisitor[type] = rootVisitor[type] || {}; | ||
var nodeVisitor = rootVisitor[type] = rootVisitor[type] || {}; | ||
mergePair(nodeVisitor, visitorType); | ||
@@ -194,12 +244,12 @@ } | ||
function wrapWithStateOrWrapper(oldVisitor, state, wrapper) { | ||
const newVisitor = {}; | ||
var newVisitor = {}; | ||
for (const key in oldVisitor) { | ||
let fns = oldVisitor[key]; | ||
if (!Array.isArray(fns)) continue; | ||
var _loop = function _loop(key) { | ||
var fns = oldVisitor[key]; | ||
if (!Array.isArray(fns)) return "continue"; | ||
fns = fns.map(function (fn) { | ||
let newFn = fn; | ||
var newFn = fn; | ||
if (state) { | ||
newFn = function (path) { | ||
newFn = function newFn(path) { | ||
return fn.call(state, path, state); | ||
@@ -216,2 +266,8 @@ }; | ||
newVisitor[key] = fns; | ||
}; | ||
for (var key in oldVisitor) { | ||
var _ret = _loop(key); | ||
if (_ret === "continue") continue; | ||
} | ||
@@ -223,5 +279,5 @@ | ||
function ensureEntranceObjects(obj) { | ||
for (const key in obj) { | ||
for (var key in obj) { | ||
if (shouldIgnoreKey(key)) continue; | ||
const fns = obj[key]; | ||
var fns = obj[key]; | ||
@@ -242,3 +298,3 @@ if (typeof fns === "function") { | ||
function wrapCheck(wrapper, fn) { | ||
const newFn = function (path) { | ||
var newFn = function newFn(path) { | ||
if (wrapper.checkPath(path)) { | ||
@@ -249,3 +305,5 @@ return fn.apply(this, arguments); | ||
newFn.toString = () => fn.toString(); | ||
newFn.toString = function () { | ||
return fn.toString(); | ||
}; | ||
@@ -267,5 +325,5 @@ return newFn; | ||
function mergePair(dest, src) { | ||
for (const key in src) { | ||
for (var key in src) { | ||
dest[key] = [].concat(dest[key] || [], src[key]); | ||
} | ||
} |
{ | ||
"name": "@babel/traverse", | ||
"version": "7.0.0-beta.43", | ||
"version": "7.0.0-beta.44", | ||
"description": "The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes", | ||
@@ -11,8 +11,8 @@ "author": "Sebastian McKenzie <sebmck@gmail.com>", | ||
"dependencies": { | ||
"@babel/code-frame": "7.0.0-beta.43", | ||
"@babel/generator": "7.0.0-beta.43", | ||
"@babel/helper-function-name": "7.0.0-beta.43", | ||
"@babel/helper-split-export-declaration": "7.0.0-beta.43", | ||
"@babel/types": "7.0.0-beta.43", | ||
"babylon": "7.0.0-beta.43", | ||
"@babel/code-frame": "7.0.0-beta.44", | ||
"@babel/generator": "7.0.0-beta.44", | ||
"@babel/helper-function-name": "7.0.0-beta.44", | ||
"@babel/helper-split-export-declaration": "7.0.0-beta.44", | ||
"@babel/types": "7.0.0-beta.44", | ||
"babylon": "7.0.0-beta.44", | ||
"debug": "^3.1.0", | ||
@@ -24,4 +24,4 @@ "globals": "^11.1.0", | ||
"devDependencies": { | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.43" | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.44" | ||
} | ||
} |
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
159540
4584
+ Added@babel/code-frame@7.0.0-beta.44(transitive)
+ Added@babel/generator@7.0.0-beta.44(transitive)
+ Added@babel/helper-function-name@7.0.0-beta.44(transitive)
+ Added@babel/helper-get-function-arity@7.0.0-beta.44(transitive)
+ Added@babel/helper-split-export-declaration@7.0.0-beta.44(transitive)
+ Added@babel/highlight@7.0.0-beta.44(transitive)
+ Added@babel/template@7.0.0-beta.44(transitive)
+ Added@babel/types@7.0.0-beta.44(transitive)
+ Addedbabylon@7.0.0-beta.44(transitive)
- Removed@babel/code-frame@7.0.0-beta.43(transitive)
- Removed@babel/generator@7.0.0-beta.43(transitive)
- Removed@babel/helper-function-name@7.0.0-beta.43(transitive)
- Removed@babel/helper-get-function-arity@7.0.0-beta.43(transitive)
- Removed@babel/helper-split-export-declaration@7.0.0-beta.43(transitive)
- Removed@babel/highlight@7.0.0-beta.43(transitive)
- Removed@babel/template@7.0.0-beta.43(transitive)
- Removed@babel/types@7.0.0-beta.43(transitive)
- Removedbabylon@7.0.0-beta.43(transitive)
Updated@babel/types@7.0.0-beta.44
Updatedbabylon@7.0.0-beta.44