@babel/plugin-transform-parameters
Advanced tools
Comparing version 7.22.5 to 8.0.0-alpha.0
489
lib/index.js
@@ -1,21 +0,470 @@ | ||
"use strict"; | ||
import { declare } from '@babel/helper-plugin-utils'; | ||
import { types, template } from '@babel/core'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "convertFunctionParams", { | ||
enumerable: true, | ||
get: function () { | ||
return _params.default; | ||
const iifeVisitor = { | ||
"ReferencedIdentifier|BindingIdentifier"(path, state) { | ||
const { | ||
scope, | ||
node | ||
} = path; | ||
const { | ||
name | ||
} = node; | ||
if (name === "eval" || scope.getBinding(name) === state.scope.parent.getBinding(name) && state.scope.hasOwnBinding(name)) { | ||
state.needsOuterBinding = true; | ||
path.stop(); | ||
} | ||
}, | ||
"TypeAnnotation|TSTypeAnnotation|TypeParameterDeclaration|TSTypeParameterDeclaration": path => path.skip() | ||
}; | ||
function collectShadowedParamsNames(param, functionScope, shadowedParams) { | ||
for (const name of Object.keys(param.getBindingIdentifiers())) { | ||
const constantViolations = functionScope.bindings[name]?.constantViolations; | ||
if (constantViolations) { | ||
for (const redeclarator of constantViolations) { | ||
const node = redeclarator.node; | ||
switch (node.type) { | ||
case "VariableDeclarator": | ||
{ | ||
if (node.init === null) { | ||
const declaration = redeclarator.parentPath; | ||
if (!declaration.parentPath.isFor() || declaration.parentPath.get("body") === declaration) { | ||
redeclarator.remove(); | ||
break; | ||
} | ||
} | ||
shadowedParams.add(name); | ||
break; | ||
} | ||
case "FunctionDeclaration": | ||
shadowedParams.add(name); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
exports.default = void 0; | ||
var _helperPluginUtils = require("@babel/helper-plugin-utils"); | ||
var _params = require("./params"); | ||
var _rest = require("./rest"); | ||
var _default = (0, _helperPluginUtils.declare)((api, options) => { | ||
var _api$assumption, _api$assumption2; | ||
} | ||
function buildScopeIIFE(shadowedParams, body) { | ||
const args = []; | ||
const params = []; | ||
for (const name of shadowedParams) { | ||
args.push(types.identifier(name)); | ||
params.push(types.identifier(name)); | ||
} | ||
return types.returnStatement(types.callExpression(types.arrowFunctionExpression(params, body), args)); | ||
} | ||
const buildDefaultParam = template.statement(` | ||
let VARIABLE_NAME = | ||
arguments.length > ARGUMENT_KEY && arguments[ARGUMENT_KEY] !== undefined ? | ||
arguments[ARGUMENT_KEY] | ||
: | ||
DEFAULT_VALUE; | ||
`); | ||
const buildLooseDefaultParam = template.statement(` | ||
if (ASSIGNMENT_IDENTIFIER === UNDEFINED) { | ||
ASSIGNMENT_IDENTIFIER = DEFAULT_VALUE; | ||
} | ||
`); | ||
const buildLooseDestructuredDefaultParam = template.statement(` | ||
let ASSIGNMENT_IDENTIFIER = PARAMETER_NAME === UNDEFINED ? DEFAULT_VALUE : PARAMETER_NAME ; | ||
`); | ||
const buildSafeArgumentsAccess = template.statement(` | ||
let $0 = arguments.length > $1 ? arguments[$1] : undefined; | ||
`); | ||
function convertFunctionParams(path, ignoreFunctionLength, shouldTransformParam, replaceRestElement) { | ||
const params = path.get("params"); | ||
const isSimpleParameterList = params.every(param => param.isIdentifier()); | ||
if (isSimpleParameterList) return false; | ||
const { | ||
node, | ||
scope | ||
} = path; | ||
const body = []; | ||
const shadowedParams = new Set(); | ||
for (const param of params) { | ||
collectShadowedParamsNames(param, scope, shadowedParams); | ||
} | ||
const state = { | ||
needsOuterBinding: false, | ||
scope | ||
}; | ||
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]; | ||
if (shouldTransformParam && !shouldTransformParam(i)) { | ||
continue; | ||
} | ||
const transformedRestNodes = []; | ||
if (replaceRestElement) { | ||
replaceRestElement(path, param, transformedRestNodes); | ||
} | ||
const paramIsAssignmentPattern = param.isAssignmentPattern(); | ||
if (paramIsAssignmentPattern && (ignoreFunctionLength || types.isMethod(node, { | ||
kind: "set" | ||
}))) { | ||
const left = param.get("left"); | ||
const right = param.get("right"); | ||
const undefinedNode = scope.buildUndefinedNode(); | ||
if (left.isIdentifier()) { | ||
body.push(buildLooseDefaultParam({ | ||
ASSIGNMENT_IDENTIFIER: types.cloneNode(left.node), | ||
DEFAULT_VALUE: right.node, | ||
UNDEFINED: undefinedNode | ||
})); | ||
param.replaceWith(left.node); | ||
} else if (left.isObjectPattern() || left.isArrayPattern()) { | ||
const paramName = scope.generateUidIdentifier(); | ||
body.push(buildLooseDestructuredDefaultParam({ | ||
ASSIGNMENT_IDENTIFIER: left.node, | ||
DEFAULT_VALUE: right.node, | ||
PARAMETER_NAME: types.cloneNode(paramName), | ||
UNDEFINED: undefinedNode | ||
})); | ||
param.replaceWith(paramName); | ||
} | ||
} else if (paramIsAssignmentPattern) { | ||
if (firstOptionalIndex === null) firstOptionalIndex = i; | ||
const left = param.get("left"); | ||
const right = param.get("right"); | ||
const defNode = buildDefaultParam({ | ||
VARIABLE_NAME: left.node, | ||
DEFAULT_VALUE: right.node, | ||
ARGUMENT_KEY: types.numericLiteral(i) | ||
}); | ||
body.push(defNode); | ||
} else if (firstOptionalIndex !== null) { | ||
const defNode = buildSafeArgumentsAccess([param.node, types.numericLiteral(i)]); | ||
body.push(defNode); | ||
} else if (param.isObjectPattern() || param.isArrayPattern()) { | ||
const uid = path.scope.generateUidIdentifier("ref"); | ||
uid.typeAnnotation = param.node.typeAnnotation; | ||
const defNode = types.variableDeclaration("let", [types.variableDeclarator(param.node, uid)]); | ||
body.push(defNode); | ||
param.replaceWith(types.cloneNode(uid)); | ||
} | ||
if (transformedRestNodes) { | ||
for (const transformedNode of transformedRestNodes) { | ||
body.push(transformedNode); | ||
} | ||
} | ||
} | ||
if (firstOptionalIndex !== null) { | ||
node.params = node.params.slice(0, firstOptionalIndex); | ||
} | ||
path.ensureBlock(); | ||
const { | ||
async, | ||
generator | ||
} = node; | ||
if (generator || state.needsOuterBinding || shadowedParams.size > 0) { | ||
body.push(buildScopeIIFE(shadowedParams, path.node.body)); | ||
path.set("body", types.blockStatement(body)); | ||
const bodyPath = path.get("body.body"); | ||
const arrowPath = bodyPath[bodyPath.length - 1].get("argument.callee"); | ||
arrowPath.arrowFunctionToExpression(); | ||
arrowPath.node.generator = generator; | ||
arrowPath.node.async = async; | ||
node.generator = false; | ||
node.async = false; | ||
if (async) { | ||
path.node.body = template.statement.ast`{ | ||
try { | ||
${path.node.body.body} | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}`; | ||
} | ||
} else { | ||
path.get("body").unshiftContainer("body", body); | ||
} | ||
return true; | ||
} | ||
const buildRest = template.statement(` | ||
for (var LEN = ARGUMENTS.length, | ||
ARRAY = new Array(ARRAY_LEN), | ||
KEY = START; | ||
KEY < LEN; | ||
KEY++) { | ||
ARRAY[ARRAY_KEY] = ARGUMENTS[KEY]; | ||
} | ||
`); | ||
const restIndex = template.expression(` | ||
(INDEX < OFFSET || ARGUMENTS.length <= INDEX) ? undefined : ARGUMENTS[INDEX] | ||
`); | ||
const restIndexImpure = template.expression(` | ||
REF = INDEX, (REF < OFFSET || ARGUMENTS.length <= REF) ? undefined : ARGUMENTS[REF] | ||
`); | ||
const restLength = template.expression(` | ||
ARGUMENTS.length <= OFFSET ? 0 : ARGUMENTS.length - OFFSET | ||
`); | ||
function referencesRest(path, state) { | ||
if (path.node.name === state.name) { | ||
return path.scope.bindingIdentifierEquals(state.name, state.outerBinding); | ||
} | ||
return false; | ||
} | ||
const memberExpressionOptimisationVisitor = { | ||
Scope(path, state) { | ||
if (!path.scope.bindingIdentifierEquals(state.name, state.outerBinding)) { | ||
path.skip(); | ||
} | ||
}, | ||
Flow(path) { | ||
if (path.isTypeCastExpression()) return; | ||
path.skip(); | ||
}, | ||
Function(path, state) { | ||
const oldNoOptimise = state.noOptimise; | ||
state.noOptimise = true; | ||
path.traverse(memberExpressionOptimisationVisitor, state); | ||
state.noOptimise = oldNoOptimise; | ||
path.skip(); | ||
}, | ||
ReferencedIdentifier(path, state) { | ||
const { | ||
node | ||
} = path; | ||
if (node.name === "arguments") { | ||
state.deopted = true; | ||
} | ||
if (!referencesRest(path, state)) return; | ||
if (state.noOptimise) { | ||
state.deopted = true; | ||
} else { | ||
const { | ||
parentPath | ||
} = path; | ||
if (parentPath.listKey === "params" && parentPath.key < state.offset) { | ||
return; | ||
} | ||
if (parentPath.isMemberExpression({ | ||
object: node | ||
})) { | ||
const grandparentPath = parentPath.parentPath; | ||
const argsOptEligible = !state.deopted && !(grandparentPath.isAssignmentExpression() && parentPath.node === grandparentPath.node.left || grandparentPath.isLVal() || grandparentPath.isForXStatement() || grandparentPath.isUpdateExpression() || grandparentPath.isUnaryExpression({ | ||
operator: "delete" | ||
}) || (grandparentPath.isCallExpression() || grandparentPath.isNewExpression()) && parentPath.node === grandparentPath.node.callee); | ||
if (argsOptEligible) { | ||
if (parentPath.node.computed) { | ||
if (parentPath.get("property").isBaseType("number")) { | ||
state.candidates.push({ | ||
cause: "indexGetter", | ||
path | ||
}); | ||
return; | ||
} | ||
} else if (parentPath.node.property.name === "length") { | ||
state.candidates.push({ | ||
cause: "lengthGetter", | ||
path | ||
}); | ||
return; | ||
} | ||
} | ||
} | ||
if (state.offset === 0 && parentPath.isSpreadElement()) { | ||
const call = parentPath.parentPath; | ||
if (call.isCallExpression() && call.node.arguments.length === 1) { | ||
state.candidates.push({ | ||
cause: "argSpread", | ||
path | ||
}); | ||
return; | ||
} | ||
} | ||
state.references.push(path); | ||
} | ||
}, | ||
BindingIdentifier(path, state) { | ||
if (referencesRest(path, state)) { | ||
state.deopted = true; | ||
} | ||
} | ||
}; | ||
function getParamsCount(node) { | ||
let count = node.params.length; | ||
if (count > 0 && types.isIdentifier(node.params[0], { | ||
name: "this" | ||
})) { | ||
count -= 1; | ||
} | ||
return count; | ||
} | ||
function hasRest(node) { | ||
const length = node.params.length; | ||
return length > 0 && types.isRestElement(node.params[length - 1]); | ||
} | ||
function optimiseIndexGetter(path, argsId, offset) { | ||
const offsetLiteral = types.numericLiteral(offset); | ||
let index; | ||
const parent = path.parent; | ||
if (types.isNumericLiteral(parent.property)) { | ||
index = types.numericLiteral(parent.property.value + offset); | ||
} else if (offset === 0) { | ||
index = parent.property; | ||
} else { | ||
index = types.binaryExpression("+", parent.property, types.cloneNode(offsetLiteral)); | ||
} | ||
const { | ||
scope, | ||
parentPath | ||
} = path; | ||
if (!scope.isPure(index)) { | ||
const temp = scope.generateUidIdentifierBasedOnNode(index); | ||
scope.push({ | ||
id: temp, | ||
kind: "var" | ||
}); | ||
parentPath.replaceWith(restIndexImpure({ | ||
ARGUMENTS: argsId, | ||
OFFSET: offsetLiteral, | ||
INDEX: index, | ||
REF: types.cloneNode(temp) | ||
})); | ||
} else { | ||
parentPath.replaceWith(restIndex({ | ||
ARGUMENTS: argsId, | ||
OFFSET: offsetLiteral, | ||
INDEX: index | ||
})); | ||
const replacedParentPath = parentPath; | ||
const offsetTestPath = replacedParentPath.get("test"); | ||
const valRes = offsetTestPath.get("left").evaluate(); | ||
if (valRes.confident) { | ||
if (valRes.value === true) { | ||
replacedParentPath.replaceWith(scope.buildUndefinedNode()); | ||
} else { | ||
offsetTestPath.replaceWith(offsetTestPath.get("right")); | ||
} | ||
} | ||
} | ||
} | ||
function optimiseLengthGetter(path, argsId, offset) { | ||
if (offset) { | ||
path.parentPath.replaceWith(restLength({ | ||
ARGUMENTS: argsId, | ||
OFFSET: types.numericLiteral(offset) | ||
})); | ||
} else { | ||
path.replaceWith(argsId); | ||
} | ||
} | ||
function convertFunctionRest(path) { | ||
const { | ||
node, | ||
scope | ||
} = path; | ||
if (!hasRest(node)) return false; | ||
const restPath = path.get(`params.${node.params.length - 1}.argument`); | ||
if (!restPath.isIdentifier()) { | ||
const shadowedParams = new Set(); | ||
collectShadowedParamsNames(restPath, path.scope, shadowedParams); | ||
let needsIIFE = shadowedParams.size > 0; | ||
if (!needsIIFE) { | ||
const state = { | ||
needsOuterBinding: false, | ||
scope | ||
}; | ||
restPath.traverse(iifeVisitor, state); | ||
needsIIFE = state.needsOuterBinding; | ||
} | ||
if (needsIIFE) { | ||
path.ensureBlock(); | ||
path.set("body", types.blockStatement([buildScopeIIFE(shadowedParams, path.node.body)])); | ||
} | ||
} | ||
let rest = restPath.node; | ||
node.params.pop(); | ||
if (types.isPattern(rest)) { | ||
const pattern = rest; | ||
rest = scope.generateUidIdentifier("ref"); | ||
const declar = types.variableDeclaration("let", [types.variableDeclarator(pattern, rest)]); | ||
path.ensureBlock(); | ||
node.body.body.unshift(declar); | ||
} else if (rest.name === "arguments") { | ||
scope.rename(rest.name); | ||
} | ||
const argsId = types.identifier("arguments"); | ||
const paramsCount = getParamsCount(node); | ||
const state = { | ||
references: [], | ||
offset: paramsCount, | ||
argumentsNode: argsId, | ||
outerBinding: scope.getBindingIdentifier(rest.name), | ||
candidates: [], | ||
name: rest.name, | ||
deopted: false | ||
}; | ||
path.traverse(memberExpressionOptimisationVisitor, state); | ||
if (!state.deopted && !state.references.length) { | ||
for (const { | ||
path, | ||
cause | ||
} of state.candidates) { | ||
const clonedArgsId = types.cloneNode(argsId); | ||
switch (cause) { | ||
case "indexGetter": | ||
optimiseIndexGetter(path, clonedArgsId, state.offset); | ||
break; | ||
case "lengthGetter": | ||
optimiseLengthGetter(path, clonedArgsId, state.offset); | ||
break; | ||
default: | ||
path.replaceWith(clonedArgsId); | ||
} | ||
} | ||
return true; | ||
} | ||
state.references.push(...state.candidates.map(({ | ||
path | ||
}) => path)); | ||
const start = types.numericLiteral(paramsCount); | ||
const key = scope.generateUidIdentifier("key"); | ||
const len = scope.generateUidIdentifier("len"); | ||
let arrKey, arrLen; | ||
if (paramsCount) { | ||
arrKey = types.binaryExpression("-", types.cloneNode(key), types.cloneNode(start)); | ||
arrLen = types.conditionalExpression(types.binaryExpression(">", types.cloneNode(len), types.cloneNode(start)), types.binaryExpression("-", types.cloneNode(len), types.cloneNode(start)), types.numericLiteral(0)); | ||
} else { | ||
arrKey = types.identifier(key.name); | ||
arrLen = types.identifier(len.name); | ||
} | ||
const loop = buildRest({ | ||
ARGUMENTS: argsId, | ||
ARRAY_KEY: arrKey, | ||
ARRAY_LEN: arrLen, | ||
START: start, | ||
ARRAY: rest, | ||
KEY: key, | ||
LEN: len | ||
}); | ||
if (state.deopted) { | ||
node.body.body.unshift(loop); | ||
} else { | ||
let target = path.getEarliestCommonAncestorFrom(state.references).getStatementParent(); | ||
target.findParent(path => { | ||
if (path.isLoop()) { | ||
target = path; | ||
} else { | ||
return path.isFunction(); | ||
} | ||
}); | ||
target.insertBefore(loop); | ||
} | ||
return true; | ||
} | ||
var index = declare((api, options) => { | ||
api.assertVersion(7); | ||
const ignoreFunctionLength = (_api$assumption = api.assumption("ignoreFunctionLength")) != null ? _api$assumption : options.loose; | ||
const noNewArrows = (_api$assumption2 = api.assumption("noNewArrows")) != null ? _api$assumption2 : true; | ||
const ignoreFunctionLength = api.assumption("ignoreFunctionLength") ?? options.loose; | ||
const noNewArrows = api.assumption("noNewArrows") ?? true; | ||
return { | ||
@@ -32,4 +481,4 @@ name: "transform-parameters", | ||
} | ||
const convertedRest = (0, _rest.default)(path); | ||
const convertedParams = (0, _params.default)(path, ignoreFunctionLength); | ||
const convertedRest = convertFunctionRest(path); | ||
const convertedParams = convertFunctionParams(path, ignoreFunctionLength); | ||
if (convertedRest || convertedParams) { | ||
@@ -42,4 +491,4 @@ path.scope.crawl(); | ||
}); | ||
exports.default = _default; | ||
export { convertFunctionParams, index as default }; | ||
//# sourceMappingURL=index.js.map |
@@ -1,10 +0,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = convertFunctionParams; | ||
var _core = require("@babel/core"); | ||
var _shadowUtils = require("./shadow-utils"); | ||
const buildDefaultParam = _core.template.statement(` | ||
import { template, types as t } from "@babel/core"; | ||
import { iifeVisitor, collectShadowedParamsNames, buildScopeIIFE } from "./shadow-utils.js"; | ||
const buildDefaultParam = template.statement(` | ||
let VARIABLE_NAME = | ||
@@ -16,3 +10,3 @@ arguments.length > ARGUMENT_KEY && arguments[ARGUMENT_KEY] !== undefined ? | ||
`); | ||
const buildLooseDefaultParam = _core.template.statement(` | ||
const buildLooseDefaultParam = template.statement(` | ||
if (ASSIGNMENT_IDENTIFIER === UNDEFINED) { | ||
@@ -22,9 +16,9 @@ ASSIGNMENT_IDENTIFIER = DEFAULT_VALUE; | ||
`); | ||
const buildLooseDestructuredDefaultParam = _core.template.statement(` | ||
const buildLooseDestructuredDefaultParam = template.statement(` | ||
let ASSIGNMENT_IDENTIFIER = PARAMETER_NAME === UNDEFINED ? DEFAULT_VALUE : PARAMETER_NAME ; | ||
`); | ||
const buildSafeArgumentsAccess = _core.template.statement(` | ||
const buildSafeArgumentsAccess = template.statement(` | ||
let $0 = arguments.length > $1 ? arguments[$1] : undefined; | ||
`); | ||
function convertFunctionParams(path, ignoreFunctionLength, shouldTransformParam, replaceRestElement) { | ||
export default function convertFunctionParams(path, ignoreFunctionLength, shouldTransformParam, replaceRestElement) { | ||
const params = path.get("params"); | ||
@@ -40,3 +34,3 @@ const isSimpleParameterList = params.every(param => param.isIdentifier()); | ||
for (const param of params) { | ||
(0, _shadowUtils.collectShadowedParamsNames)(param, scope, shadowedParams); | ||
collectShadowedParamsNames(param, scope, shadowedParams); | ||
} | ||
@@ -49,3 +43,3 @@ const state = { | ||
for (const param of params) { | ||
if (!param.isIdentifier()) param.traverse(_shadowUtils.iifeVisitor, state); | ||
if (!param.isIdentifier()) param.traverse(iifeVisitor, state); | ||
if (state.needsOuterBinding) break; | ||
@@ -65,3 +59,3 @@ } | ||
const paramIsAssignmentPattern = param.isAssignmentPattern(); | ||
if (paramIsAssignmentPattern && (ignoreFunctionLength || _core.types.isMethod(node, { | ||
if (paramIsAssignmentPattern && (ignoreFunctionLength || t.isMethod(node, { | ||
kind: "set" | ||
@@ -74,3 +68,3 @@ }))) { | ||
body.push(buildLooseDefaultParam({ | ||
ASSIGNMENT_IDENTIFIER: _core.types.cloneNode(left.node), | ||
ASSIGNMENT_IDENTIFIER: t.cloneNode(left.node), | ||
DEFAULT_VALUE: right.node, | ||
@@ -85,3 +79,3 @@ UNDEFINED: undefinedNode | ||
DEFAULT_VALUE: right.node, | ||
PARAMETER_NAME: _core.types.cloneNode(paramName), | ||
PARAMETER_NAME: t.cloneNode(paramName), | ||
UNDEFINED: undefinedNode | ||
@@ -98,7 +92,7 @@ })); | ||
DEFAULT_VALUE: right.node, | ||
ARGUMENT_KEY: _core.types.numericLiteral(i) | ||
ARGUMENT_KEY: t.numericLiteral(i) | ||
}); | ||
body.push(defNode); | ||
} else if (firstOptionalIndex !== null) { | ||
const defNode = buildSafeArgumentsAccess([param.node, _core.types.numericLiteral(i)]); | ||
const defNode = buildSafeArgumentsAccess([param.node, t.numericLiteral(i)]); | ||
body.push(defNode); | ||
@@ -108,5 +102,5 @@ } else if (param.isObjectPattern() || param.isArrayPattern()) { | ||
uid.typeAnnotation = param.node.typeAnnotation; | ||
const defNode = _core.types.variableDeclaration("let", [_core.types.variableDeclarator(param.node, uid)]); | ||
const defNode = t.variableDeclaration("let", [t.variableDeclarator(param.node, uid)]); | ||
body.push(defNode); | ||
param.replaceWith(_core.types.cloneNode(uid)); | ||
param.replaceWith(t.cloneNode(uid)); | ||
} | ||
@@ -128,4 +122,4 @@ if (transformedRestNodes) { | ||
if (generator || state.needsOuterBinding || shadowedParams.size > 0) { | ||
body.push((0, _shadowUtils.buildScopeIIFE)(shadowedParams, path.node.body)); | ||
path.set("body", _core.types.blockStatement(body)); | ||
body.push(buildScopeIIFE(shadowedParams, path.node.body)); | ||
path.set("body", t.blockStatement(body)); | ||
const bodyPath = path.get("body.body"); | ||
@@ -139,3 +133,3 @@ const arrowPath = bodyPath[bodyPath.length - 1].get("argument.callee"); | ||
if (async) { | ||
path.node.body = _core.template.statement.ast`{ | ||
path.node.body = template.statement.ast`{ | ||
try { | ||
@@ -142,0 +136,0 @@ ${path.node.body.body} |
@@ -1,10 +0,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = convertFunctionRest; | ||
var _core = require("@babel/core"); | ||
var _shadowUtils = require("./shadow-utils"); | ||
const buildRest = _core.template.statement(` | ||
import { template, types as t } from "@babel/core"; | ||
import { iifeVisitor, collectShadowedParamsNames, buildScopeIIFE } from "./shadow-utils.js"; | ||
const buildRest = template.statement(` | ||
for (var LEN = ARGUMENTS.length, | ||
@@ -18,9 +12,9 @@ ARRAY = new Array(ARRAY_LEN), | ||
`); | ||
const restIndex = _core.template.expression(` | ||
const restIndex = template.expression(` | ||
(INDEX < OFFSET || ARGUMENTS.length <= INDEX) ? undefined : ARGUMENTS[INDEX] | ||
`); | ||
const restIndexImpure = _core.template.expression(` | ||
const restIndexImpure = template.expression(` | ||
REF = INDEX, (REF < OFFSET || ARGUMENTS.length <= REF) ? undefined : ARGUMENTS[REF] | ||
`); | ||
const restLength = _core.template.expression(` | ||
const restLength = template.expression(` | ||
ARGUMENTS.length <= OFFSET ? 0 : ARGUMENTS.length - OFFSET | ||
@@ -114,3 +108,3 @@ `); | ||
let count = node.params.length; | ||
if (count > 0 && _core.types.isIdentifier(node.params[0], { | ||
if (count > 0 && t.isIdentifier(node.params[0], { | ||
name: "this" | ||
@@ -124,14 +118,14 @@ })) { | ||
const length = node.params.length; | ||
return length > 0 && _core.types.isRestElement(node.params[length - 1]); | ||
return length > 0 && t.isRestElement(node.params[length - 1]); | ||
} | ||
function optimiseIndexGetter(path, argsId, offset) { | ||
const offsetLiteral = _core.types.numericLiteral(offset); | ||
const offsetLiteral = t.numericLiteral(offset); | ||
let index; | ||
const parent = path.parent; | ||
if (_core.types.isNumericLiteral(parent.property)) { | ||
index = _core.types.numericLiteral(parent.property.value + offset); | ||
if (t.isNumericLiteral(parent.property)) { | ||
index = t.numericLiteral(parent.property.value + offset); | ||
} else if (offset === 0) { | ||
index = parent.property; | ||
} else { | ||
index = _core.types.binaryExpression("+", parent.property, _core.types.cloneNode(offsetLiteral)); | ||
index = t.binaryExpression("+", parent.property, t.cloneNode(offsetLiteral)); | ||
} | ||
@@ -152,3 +146,3 @@ const { | ||
INDEX: index, | ||
REF: _core.types.cloneNode(temp) | ||
REF: t.cloneNode(temp) | ||
})); | ||
@@ -177,3 +171,3 @@ } else { | ||
ARGUMENTS: argsId, | ||
OFFSET: _core.types.numericLiteral(offset) | ||
OFFSET: t.numericLiteral(offset) | ||
})); | ||
@@ -184,3 +178,3 @@ } else { | ||
} | ||
function convertFunctionRest(path) { | ||
export default function convertFunctionRest(path) { | ||
const { | ||
@@ -194,3 +188,3 @@ node, | ||
const shadowedParams = new Set(); | ||
(0, _shadowUtils.collectShadowedParamsNames)(restPath, path.scope, shadowedParams); | ||
collectShadowedParamsNames(restPath, path.scope, shadowedParams); | ||
let needsIIFE = shadowedParams.size > 0; | ||
@@ -202,3 +196,3 @@ if (!needsIIFE) { | ||
}; | ||
restPath.traverse(_shadowUtils.iifeVisitor, state); | ||
restPath.traverse(iifeVisitor, state); | ||
needsIIFE = state.needsOuterBinding; | ||
@@ -208,3 +202,3 @@ } | ||
path.ensureBlock(); | ||
path.set("body", _core.types.blockStatement([(0, _shadowUtils.buildScopeIIFE)(shadowedParams, path.node.body)])); | ||
path.set("body", t.blockStatement([buildScopeIIFE(shadowedParams, path.node.body)])); | ||
} | ||
@@ -214,6 +208,6 @@ } | ||
node.params.pop(); | ||
if (_core.types.isPattern(rest)) { | ||
if (t.isPattern(rest)) { | ||
const pattern = rest; | ||
rest = scope.generateUidIdentifier("ref"); | ||
const declar = _core.types.variableDeclaration("let", [_core.types.variableDeclarator(pattern, rest)]); | ||
const declar = t.variableDeclaration("let", [t.variableDeclarator(pattern, rest)]); | ||
path.ensureBlock(); | ||
@@ -224,3 +218,3 @@ node.body.body.unshift(declar); | ||
} | ||
const argsId = _core.types.identifier("arguments"); | ||
const argsId = t.identifier("arguments"); | ||
const paramsCount = getParamsCount(node); | ||
@@ -242,3 +236,3 @@ const state = { | ||
} of state.candidates) { | ||
const clonedArgsId = _core.types.cloneNode(argsId); | ||
const clonedArgsId = t.cloneNode(argsId); | ||
switch (cause) { | ||
@@ -260,3 +254,3 @@ case "indexGetter": | ||
}) => path)); | ||
const start = _core.types.numericLiteral(paramsCount); | ||
const start = t.numericLiteral(paramsCount); | ||
const key = scope.generateUidIdentifier("key"); | ||
@@ -266,7 +260,7 @@ const len = scope.generateUidIdentifier("len"); | ||
if (paramsCount) { | ||
arrKey = _core.types.binaryExpression("-", _core.types.cloneNode(key), _core.types.cloneNode(start)); | ||
arrLen = _core.types.conditionalExpression(_core.types.binaryExpression(">", _core.types.cloneNode(len), _core.types.cloneNode(start)), _core.types.binaryExpression("-", _core.types.cloneNode(len), _core.types.cloneNode(start)), _core.types.numericLiteral(0)); | ||
arrKey = t.binaryExpression("-", t.cloneNode(key), t.cloneNode(start)); | ||
arrLen = t.conditionalExpression(t.binaryExpression(">", t.cloneNode(len), t.cloneNode(start)), t.binaryExpression("-", t.cloneNode(len), t.cloneNode(start)), t.numericLiteral(0)); | ||
} else { | ||
arrKey = _core.types.identifier(key.name); | ||
arrLen = _core.types.identifier(len.name); | ||
arrKey = t.identifier(key.name); | ||
arrLen = t.identifier(len.name); | ||
} | ||
@@ -273,0 +267,0 @@ const loop = buildRest({ |
@@ -1,11 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.buildScopeIIFE = buildScopeIIFE; | ||
exports.collectShadowedParamsNames = collectShadowedParamsNames; | ||
exports.iifeVisitor = void 0; | ||
var _core = require("@babel/core"); | ||
const iifeVisitor = { | ||
import { types as t } from "@babel/core"; | ||
export const iifeVisitor = { | ||
"ReferencedIdentifier|BindingIdentifier"(path, state) { | ||
@@ -26,7 +18,5 @@ const { | ||
}; | ||
exports.iifeVisitor = iifeVisitor; | ||
function collectShadowedParamsNames(param, functionScope, shadowedParams) { | ||
export function collectShadowedParamsNames(param, functionScope, shadowedParams) { | ||
for (const name of Object.keys(param.getBindingIdentifiers())) { | ||
var _functionScope$bindin; | ||
const constantViolations = (_functionScope$bindin = functionScope.bindings[name]) == null ? void 0 : _functionScope$bindin.constantViolations; | ||
const constantViolations = functionScope.bindings[name]?.constantViolations; | ||
if (constantViolations) { | ||
@@ -56,12 +46,12 @@ for (const redeclarator of constantViolations) { | ||
} | ||
function buildScopeIIFE(shadowedParams, body) { | ||
export 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)); | ||
args.push(t.identifier(name)); | ||
params.push(t.identifier(name)); | ||
} | ||
return _core.types.returnStatement(_core.types.callExpression(_core.types.arrowFunctionExpression(params, body), args)); | ||
return t.returnStatement(t.callExpression(t.arrowFunctionExpression(params, body), args)); | ||
} | ||
//# sourceMappingURL=shadow-utils.js.map |
{ | ||
"name": "@babel/plugin-transform-parameters", | ||
"version": "7.22.5", | ||
"version": "8.0.0-alpha.0", | ||
"description": "Compile ES2015 default and rest parameters to ES5", | ||
@@ -17,3 +17,3 @@ "repository": { | ||
"dependencies": { | ||
"@babel/helper-plugin-utils": "^7.22.5" | ||
"@babel/helper-plugin-utils": "^8.0.0-alpha.0" | ||
}, | ||
@@ -24,13 +24,17 @@ "keywords": [ | ||
"peerDependencies": { | ||
"@babel/core": "^7.0.0-0" | ||
"@babel/core": "^8.0.0-alpha.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.22.5", | ||
"@babel/helper-plugin-test-runner": "^7.22.5" | ||
"@babel/core": "^8.0.0-alpha.0", | ||
"@babel/helper-plugin-test-runner": "^8.0.0-alpha.0" | ||
}, | ||
"engines": { | ||
"node": ">=6.9.0" | ||
"node": "^16.20.0 || ^18.16.0 || >=20.0.0" | ||
}, | ||
"author": "The Babel Team (https://babel.dev/team)", | ||
"type": "commonjs" | ||
"exports": { | ||
".": "./lib/index.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"type": "module" | ||
} |
@@ -5,3 +5,3 @@ # @babel/plugin-transform-parameters | ||
See our website [@babel/plugin-transform-parameters](https://babeljs.io/docs/en/babel-plugin-transform-parameters) for more information. | ||
See our website [@babel/plugin-transform-parameters](https://babeljs.io/docs/babel-plugin-transform-parameters) for more information. | ||
@@ -8,0 +8,0 @@ ## Install |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
120802
956
Yes
1
+ Added@babel/code-frame@8.0.0-alpha.13(transitive)
+ Added@babel/compat-data@8.0.0-alpha.13(transitive)
+ Added@babel/core@8.0.0-alpha.13(transitive)
+ Added@babel/generator@8.0.0-alpha.13(transitive)
+ Added@babel/helper-compilation-targets@8.0.0-alpha.13(transitive)
+ Added@babel/helper-plugin-utils@8.0.0-alpha.13(transitive)
+ Added@babel/helper-string-parser@8.0.0-alpha.13(transitive)
+ Added@babel/helper-validator-identifier@8.0.0-alpha.13(transitive)
+ Added@babel/helper-validator-option@8.0.0-alpha.13(transitive)
+ Added@babel/helpers@8.0.0-alpha.13(transitive)
+ Added@babel/parser@8.0.0-alpha.13(transitive)
+ Added@babel/template@8.0.0-alpha.13(transitive)
+ Added@babel/traverse@8.0.0-alpha.13(transitive)
+ Added@babel/types@8.0.0-alpha.13(transitive)
+ Addedglobals@15.12.0(transitive)
+ Addedjs-tokens@8.0.3(transitive)
+ Addedlru-cache@7.18.3(transitive)
+ Addedsemver@7.6.3(transitive)
- Removed@babel/code-frame@7.26.2(transitive)
- Removed@babel/compat-data@7.26.2(transitive)
- Removed@babel/core@7.26.0(transitive)
- Removed@babel/generator@7.26.2(transitive)
- Removed@babel/helper-compilation-targets@7.25.9(transitive)
- Removed@babel/helper-module-imports@7.25.9(transitive)
- Removed@babel/helper-module-transforms@7.26.0(transitive)
- Removed@babel/helper-plugin-utils@7.25.9(transitive)
- Removed@babel/helper-string-parser@7.25.9(transitive)
- Removed@babel/helper-validator-identifier@7.25.9(transitive)
- Removed@babel/helper-validator-option@7.25.9(transitive)
- Removed@babel/helpers@7.26.0(transitive)
- Removed@babel/parser@7.26.2(transitive)
- Removed@babel/template@7.25.9(transitive)
- Removed@babel/traverse@7.25.9(transitive)
- Removed@babel/types@7.26.0(transitive)
- Removedglobals@11.12.0(transitive)
- Removedjs-tokens@4.0.0(transitive)
- Removedlru-cache@5.1.1(transitive)
- Removedsemver@6.3.1(transitive)
- Removedyallist@3.1.1(transitive)