@babel/plugin-transform-parameters
Advanced tools
Comparing version 7.8.8 to 7.9.3
@@ -8,8 +8,4 @@ "use strict"; | ||
var _helperCallDelegate = _interopRequireDefault(require("@babel/helper-call-delegate")); | ||
var _core = require("@babel/core"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const buildDefaultParam = (0, _core.template)(` | ||
@@ -33,13 +29,4 @@ let VARIABLE_NAME = | ||
`); | ||
function isSafeBinding(scope, node) { | ||
if (!scope.hasOwnBinding(node.name)) return true; | ||
const { | ||
kind | ||
} = scope.getOwnBinding(node.name); | ||
return kind === "param" || kind === "local"; | ||
} | ||
const iifeVisitor = { | ||
ReferencedIdentifier(path, state) { | ||
"ReferencedIdentifier|BindingIdentifier"(path, state) { | ||
const { | ||
@@ -49,11 +36,10 @@ scope, | ||
} = path; | ||
const { | ||
name | ||
} = node; | ||
if (node.name === "eval" || !isSafeBinding(scope, node) || !isSafeBinding(state.scope, node)) { | ||
state.iife = true; | ||
if (name === "eval" || scope.getBinding(name) === state.scope.parent.getBinding(name) && state.scope.hasOwnBinding(name)) { | ||
state.needsOuterBinding = true; | ||
path.stop(); | ||
} | ||
}, | ||
Scope(path) { | ||
path.skip(); | ||
} | ||
@@ -64,2 +50,5 @@ | ||
function convertFunctionParams(path, loose) { | ||
const params = path.get("params"); | ||
const isSimpleParameterList = params.every(param => param.isIdentifier()); | ||
if (isSimpleParameterList) return false; | ||
const { | ||
@@ -70,12 +59,10 @@ node, | ||
const state = { | ||
iife: false, | ||
scope: scope | ||
stop: false, | ||
needsOuterBinding: false, | ||
scope | ||
}; | ||
const body = []; | ||
const params = path.get("params"); | ||
let firstOptionalIndex = null; | ||
const shadowedParams = new Set(); | ||
for (let i = 0; i < params.length; i++) { | ||
const param = params[i]; | ||
for (const param of params) { | ||
for (const name of Object.keys(param.getBindingIdentifiers())) { | ||
@@ -92,13 +79,18 @@ var _scope$bindings$name; | ||
case "VariableDeclarator": | ||
if (node.init === null) { | ||
const declaration = redeclarator.parentPath; | ||
{ | ||
if (node.init === null) { | ||
const declaration = redeclarator.parentPath; | ||
if (!declaration.parentPath.isFor() || declaration.parentPath.get("body") === declaration) { | ||
redeclarator.remove(); | ||
break; | ||
if (!declaration.parentPath.isFor() || declaration.parentPath.get("body") === declaration) { | ||
redeclarator.remove(); | ||
break; | ||
} | ||
} | ||
shadowedParams.add(name); | ||
break; | ||
} | ||
case "FunctionDeclaration": | ||
state.iife = true; | ||
shadowedParams.add(name); | ||
break; | ||
@@ -109,3 +101,15 @@ } | ||
} | ||
} | ||
if (shadowedParams.size === 0) { | ||
for (const param of params) { | ||
if (!param.isIdentifier()) param.traverse(iifeVisitor, state); | ||
if (state.needsOuterBinding) break; | ||
} | ||
} | ||
let firstOptionalIndex = null; | ||
for (let i = 0; i < params.length; i++) { | ||
const param = params[i]; | ||
const paramIsAssignmentPattern = param.isAssignmentPattern(); | ||
@@ -139,11 +143,2 @@ | ||
const right = param.get("right"); | ||
if (!state.iife) { | ||
if (right.isIdentifier() && !isSafeBinding(scope, right.node)) { | ||
state.iife = true; | ||
} else { | ||
right.traverse(iifeVisitor, state); | ||
} | ||
} | ||
const defNode = buildDefaultParam({ | ||
@@ -166,10 +161,4 @@ VARIABLE_NAME: left.node, | ||
} | ||
if (!state.iife && !param.isIdentifier()) { | ||
param.traverse(iifeVisitor, state); | ||
} | ||
} | ||
if (body.length === 0) return false; | ||
if (firstOptionalIndex !== null) { | ||
@@ -181,5 +170,8 @@ node.params = node.params.slice(0, firstOptionalIndex); | ||
if (state.iife) { | ||
body.push((0, _helperCallDelegate.default)(path, scope, false)); | ||
if (state.needsOuterBinding || shadowedParams.size > 0) { | ||
body.push(buildScopeIIFE(shadowedParams, path.get("body").node)); | ||
path.set("body", _core.types.blockStatement(body)); | ||
const bodyPath = path.get("body.body"); | ||
const arrowPath = bodyPath[bodyPath.length - 1].get("argument.callee"); | ||
arrowPath.arrowFunctionToExpression(); | ||
} else { | ||
@@ -190,2 +182,14 @@ path.get("body").unshiftContainer("body", body); | ||
return true; | ||
} | ||
function buildScopeIIFE(shadowedParams, body) { | ||
const args = []; | ||
const params = []; | ||
for (const name of shadowedParams) { | ||
args.push(_core.types.identifier(name)); | ||
params.push(_core.types.identifier(name)); | ||
} | ||
return _core.types.returnStatement(_core.types.callExpression(_core.types.arrowFunctionExpression(params, body), params)); | ||
} |
{ | ||
"name": "@babel/plugin-transform-parameters", | ||
"version": "7.8.8", | ||
"version": "7.9.3", | ||
"description": "Compile ES2015 default and rest parameters to ES5", | ||
@@ -12,3 +12,2 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-parameters", | ||
"dependencies": { | ||
"@babel/helper-call-delegate": "^7.8.7", | ||
"@babel/helper-get-function-arity": "^7.8.3", | ||
@@ -27,3 +26,3 @@ "@babel/helper-plugin-utils": "^7.8.3" | ||
}, | ||
"gitHead": "c831a2450dbf252c75750a455c63e1016c2f2244" | ||
"gitHead": "1ae85560a7e1c24e80aadd04a44e0dfa3e2699fc" | ||
} |
16876
3
442
- Removed@babel/helper-call-delegate@^7.8.7
- Removed@babel/helper-call-delegate@7.12.13(transitive)