babel-plugin-transform-remove-undefined
Advanced tools
Comparing version 0.5.0-alpha.55276695 to 0.5.0-alpha.01eac1c3
107
lib/index.js
"use strict"; | ||
var evaluate = require("babel-helper-evaluate-path"); | ||
const evaluate = require("babel-helper-evaluate-path"); | ||
function isPureAndUndefined(rval) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
tdz = _ref.tdz, | ||
_ref$scope = _ref.scope, | ||
scope = _ref$scope === void 0 ? { | ||
hasBinding: function hasBinding() { | ||
return false; | ||
} | ||
} : _ref$scope; | ||
function isPureAndUndefined(rval, { | ||
tdz, | ||
scope = { | ||
hasBinding: () => false | ||
} | ||
} = {}) { | ||
if (rval.isIdentifier() && rval.node.name === "undefined") { | ||
@@ -30,3 +26,3 @@ // deopt right away if undefined is a local binding | ||
var evaluation = evaluate(rval, { | ||
const evaluation = evaluate(rval, { | ||
tdz | ||
@@ -38,5 +34,3 @@ }); | ||
function getLoopParent(path, scopeParent) { | ||
var parent = path.findParent(function (p) { | ||
return p.isLoop() || p === scopeParent; | ||
}); // don't traverse higher than the function the var is defined in. | ||
const parent = path.findParent(p => p.isLoop() || p === scopeParent); // don't traverse higher than the function the var is defined in. | ||
@@ -47,5 +41,3 @@ return parent === scopeParent ? null : parent; | ||
function getFunctionParent(path, scopeParent) { | ||
var parent = path.findParent(function (p) { | ||
return p.isFunction(); | ||
}); // don't traverse higher than the function the var is defined in. | ||
const parent = path.findParent(p => p.isFunction()); // don't traverse higher than the function the var is defined in. | ||
@@ -55,9 +47,7 @@ return parent === scopeParent ? null : parent; | ||
function getFunctionReferences(path, scopeParent) { | ||
var references = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Set(); | ||
function getFunctionReferences(path, scopeParent, references = new Set()) { | ||
for (let func = getFunctionParent(path, scopeParent); func; func = getFunctionParent(func, scopeParent)) { | ||
const id = func.node.id; | ||
const binding = id && func.scope.getBinding(id.name); | ||
for (var func = getFunctionParent(path, scopeParent); func; func = getFunctionParent(func, scopeParent)) { | ||
var id = func.node.id; | ||
var binding = id && func.scope.getBinding(id.name); | ||
if (!binding) { | ||
@@ -67,3 +57,3 @@ continue; | ||
binding.referencePaths.forEach(function (path) { | ||
binding.referencePaths.forEach(path => { | ||
if (!references.has(path)) { | ||
@@ -80,3 +70,3 @@ references.add(path); | ||
function hasViolation(declarator, scope, start) { | ||
var binding = scope.getBinding(declarator.node.id.name); | ||
const binding = scope.getBinding(declarator.node.id.name); | ||
@@ -87,4 +77,4 @@ if (!binding) { | ||
var scopeParent = declarator.getFunctionParent(); | ||
var violation = binding.constantViolations.some(function (v) { | ||
const scopeParent = declarator.getFunctionParent(); | ||
const violation = binding.constantViolations.some(v => { | ||
// https://github.com/babel/minify/issues/630 | ||
@@ -97,3 +87,3 @@ if (!v.node) { | ||
var violationStart = v.node.start; | ||
const violationStart = v.node.start; | ||
@@ -104,3 +94,3 @@ if (violationStart === undefined || violationStart < start) { | ||
var references = getFunctionReferences(v, scopeParent); | ||
const references = getFunctionReferences(v, scopeParent); | ||
var _iteratorNormalCompletion = true; | ||
@@ -112,5 +102,5 @@ var _didIteratorError = false; | ||
for (var _iterator = references[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _ref2 = _step.value; | ||
const ref = _step.value; | ||
if (_ref2.node.start === undefined || _ref2.node.start < start) { | ||
if (ref.node.start === undefined || ref.node.start < start) { | ||
return true; | ||
@@ -134,3 +124,3 @@ } | ||
for (var loop = getLoopParent(declarator, scopeParent); loop; loop = getLoopParent(loop, scopeParent)) { | ||
for (let loop = getLoopParent(declarator, scopeParent); loop; loop = getLoopParent(loop, scopeParent)) { | ||
if (loop.node.end === undefined || loop.node.end > violationStart) { | ||
@@ -148,10 +138,11 @@ return true; | ||
visitor: { | ||
SequenceExpression(path, _ref3) { | ||
var _ref3$opts = _ref3.opts; | ||
_ref3$opts = _ref3$opts === void 0 ? {} : _ref3$opts; | ||
var tdz = _ref3$opts.tdz; | ||
var expressions = path.get("expressions"); | ||
SequenceExpression(path, { | ||
opts: { | ||
tdz | ||
} = {} | ||
}) { | ||
const expressions = path.get("expressions"); | ||
for (var i = 0; i < expressions.length; i++) { | ||
var expr = expressions[i]; | ||
for (let i = 0; i < expressions.length; i++) { | ||
const expr = expressions[i]; | ||
if (!isPureAndUndefined(expr, { | ||
@@ -172,7 +163,7 @@ tdz, | ||
ReturnStatement(path, _ref4) { | ||
var _ref4$opts = _ref4.opts; | ||
_ref4$opts = _ref4$opts === void 0 ? {} : _ref4$opts; | ||
var tdz = _ref4$opts.tdz; | ||
ReturnStatement(path, { | ||
opts: { | ||
tdz | ||
} = {} | ||
}) { | ||
if (path.node.argument !== null) { | ||
@@ -188,7 +179,7 @@ if (isPureAndUndefined(path.get("argument"), { | ||
VariableDeclaration(path, _ref5) { | ||
var _ref5$opts = _ref5.opts; | ||
_ref5$opts = _ref5$opts === void 0 ? {} : _ref5$opts; | ||
var tdz = _ref5$opts.tdz; | ||
VariableDeclaration(path, { | ||
opts: { | ||
tdz | ||
} = {} | ||
}) { | ||
switch (path.node.kind) { | ||
@@ -205,8 +196,8 @@ case "const": | ||
for (var _iterator2 = path.get("declarations")[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var _declarator = _step2.value; | ||
const declarator = _step2.value; | ||
if (isPureAndUndefined(_declarator.get("init"), { | ||
if (isPureAndUndefined(declarator.get("init"), { | ||
tdz | ||
})) { | ||
_declarator.node.init = null; | ||
declarator.node.init = null; | ||
} | ||
@@ -232,3 +223,3 @@ } | ||
case "var": | ||
var start = path.node.start; | ||
const start = path.node.start; | ||
@@ -240,3 +231,3 @@ if (start === undefined) { | ||
var scope = path.scope; | ||
const scope = path.scope; | ||
var _iteratorNormalCompletion3 = true; | ||
@@ -248,6 +239,6 @@ var _didIteratorError3 = false; | ||
for (var _iterator3 = path.get("declarations")[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var _declarator3 = _step3.value; | ||
const declarator = _step3.value; | ||
if (isPureAndUndefined(_declarator3.get("init")) && !hasViolation(_declarator3, scope, start)) { | ||
_declarator3.node.init = null; | ||
if (isPureAndUndefined(declarator.get("init")) && !hasViolation(declarator, scope, start)) { | ||
declarator.node.init = null; | ||
} | ||
@@ -254,0 +245,0 @@ } |
{ | ||
"name": "babel-plugin-transform-remove-undefined", | ||
"version": "0.5.0-alpha.55276695", | ||
"version": "0.5.0-alpha.01eac1c3", | ||
"description": "This removes rvals that are equivalent to undefined wherever possible", | ||
@@ -15,4 +15,4 @@ "keywords": [ | ||
"dependencies": { | ||
"babel-helper-evaluate-path": "^0.5.0-alpha.55276695" | ||
"babel-helper-evaluate-path": "^0.5.0-alpha.01eac1c3" | ||
} | ||
} |
@@ -32,3 +32,3 @@ # babel-plugin-transform-remove-undefined | ||
```sh | ||
npm install babel-plugin-transform-remove-undefined | ||
npm install babel-plugin-transform-remove-undefined --save-dev | ||
``` | ||
@@ -35,0 +35,0 @@ |
8393
209