@babel/plugin-transform-computed-properties
Advanced tools
Comparing version 7.18.9 to 7.20.7
110
lib/index.js
@@ -7,19 +7,37 @@ "use strict"; | ||
exports.default = void 0; | ||
var _core = require("@babel/core"); | ||
var _helperPluginUtils = require("@babel/helper-plugin-utils"); | ||
var _core = require("@babel/core"); | ||
var _template = require("@babel/template"); | ||
const DefineAccessorHelper = _template.default.expression.ast` | ||
function (type, obj, key, fn) { | ||
var desc = { configurable: true, enumerable: true }; | ||
desc[type] = fn; | ||
return Object.defineProperty(obj, key, desc); | ||
}`; | ||
DefineAccessorHelper._compact = true; | ||
var _default = (0, _helperPluginUtils.declare)((api, options) => { | ||
var _api$assumption; | ||
api.assertVersion(7); | ||
const setComputedProperties = (_api$assumption = api.assumption("setComputedProperties")) != null ? _api$assumption : options.loose; | ||
const pushComputedProps = setComputedProperties ? pushComputedPropsLoose : pushComputedPropsSpec; | ||
function buildDefineAccessor(state, type, obj, key, fn) { | ||
let helper; | ||
if (state.availableHelper("defineAccessor")) { | ||
helper = state.addHelper("defineAccessor"); | ||
} else { | ||
const file = state.file; | ||
helper = file.get("fallbackDefineAccessorHelper"); | ||
if (!helper) { | ||
const id = file.scope.generateUidIdentifier("defineAccessor"); | ||
file.scope.push({ | ||
id, | ||
init: DefineAccessorHelper | ||
}); | ||
file.set("fallbackDefineAccessorHelper", helper = id); | ||
} | ||
helper = _core.types.cloneNode(helper); | ||
} | ||
return _core.types.callExpression(helper, [_core.types.stringLiteral(type), obj, key, fn]); | ||
} | ||
const buildMutatorMapAssign = _core.template.statements(` | ||
MUTATOR_MAP_REF[KEY] = MUTATOR_MAP_REF[KEY] || {}; | ||
MUTATOR_MAP_REF[KEY].KIND = VALUE; | ||
`); | ||
function getValue(prop) { | ||
@@ -32,32 +50,26 @@ if (_core.types.isObjectProperty(prop)) { | ||
} | ||
function pushAssign(objId, prop, body) { | ||
body.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.cloneNode(objId), prop.key, prop.computed || _core.types.isLiteral(prop.key)), getValue(prop)))); | ||
} | ||
function pushMutatorDefine({ | ||
function pushAccessorDefine({ | ||
body, | ||
getMutatorId, | ||
scope | ||
computedProps, | ||
initPropExpression, | ||
objId, | ||
state | ||
}, prop) { | ||
let key = !prop.computed && _core.types.isIdentifier(prop.key) ? _core.types.stringLiteral(prop.key.name) : prop.key; | ||
const maybeMemoise = scope.maybeGenerateMemoised(key); | ||
if (maybeMemoise) { | ||
body.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", maybeMemoise, key))); | ||
key = maybeMemoise; | ||
const kind = prop.kind; | ||
const key = !prop.computed && _core.types.isIdentifier(prop.key) ? _core.types.stringLiteral(prop.key.name) : prop.key; | ||
const value = getValue(prop); | ||
if (computedProps.length === 1) { | ||
return buildDefineAccessor(state, kind, initPropExpression, key, value); | ||
} else { | ||
body.push(_core.types.expressionStatement(buildDefineAccessor(state, kind, _core.types.cloneNode(objId), key, value))); | ||
} | ||
body.push(...buildMutatorMapAssign({ | ||
MUTATOR_MAP_REF: getMutatorId(), | ||
KEY: _core.types.cloneNode(key), | ||
VALUE: getValue(prop), | ||
KIND: _core.types.identifier(prop.kind) | ||
})); | ||
} | ||
function pushComputedPropsLoose(info) { | ||
for (const prop of info.computedProps) { | ||
if (_core.types.isObjectMethod(prop) && (prop.kind === "get" || prop.kind === "set")) { | ||
pushMutatorDefine(info, prop); | ||
const single = pushAccessorDefine(info, prop); | ||
if (single) return single; | ||
} else { | ||
@@ -68,3 +80,2 @@ pushAssign(_core.types.cloneNode(info.objId), prop, info.body); | ||
} | ||
function pushComputedPropsSpec(info) { | ||
@@ -77,11 +88,9 @@ const { | ||
} = info; | ||
for (const prop of computedProps) { | ||
const key = _core.types.toComputedKey(prop); | ||
if (_core.types.isObjectMethod(prop) && (prop.kind === "get" || prop.kind === "set")) { | ||
pushMutatorDefine(info, prop); | ||
const single = pushAccessorDefine(info, prop); | ||
if (single) return single; | ||
} else { | ||
const value = getValue(prop); | ||
if (computedProps.length === 1) { | ||
@@ -95,3 +104,2 @@ return _core.types.callExpression(state.addHelper("defineProperty"), [info.initPropExpression, key, value]); | ||
} | ||
return { | ||
@@ -108,3 +116,2 @@ name: "transform-computed-properties", | ||
let hasComputed = false; | ||
for (const prop of node.properties) { | ||
@@ -114,8 +121,7 @@ hasComputed = prop.computed === true; | ||
} | ||
if (!hasComputed) return; | ||
if (!hasComputed) return; | ||
const initProps = []; | ||
const computedProps = []; | ||
let foundComputed = false; | ||
for (const prop of node.properties) { | ||
@@ -125,7 +131,5 @@ if (_core.types.isSpreadElement(prop)) { | ||
} | ||
if (prop.computed) { | ||
foundComputed = true; | ||
} | ||
if (foundComputed) { | ||
@@ -137,20 +141,6 @@ computedProps.push(prop); | ||
} | ||
const objId = scope.generateUidIdentifierBasedOnNode(parent); | ||
const initPropExpression = _core.types.objectExpression(initProps); | ||
const body = []; | ||
body.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(objId, initPropExpression)])); | ||
let mutatorRef; | ||
const getMutatorId = function () { | ||
if (!mutatorRef) { | ||
mutatorRef = scope.generateUidIdentifier("mutatorMap"); | ||
body.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(mutatorRef, _core.types.objectExpression([]))])); | ||
} | ||
return _core.types.cloneNode(mutatorRef); | ||
}; | ||
const single = pushComputedProps({ | ||
@@ -162,10 +152,4 @@ scope, | ||
initPropExpression, | ||
getMutatorId, | ||
state | ||
}); | ||
if (mutatorRef) { | ||
body.push(_core.types.expressionStatement(_core.types.callExpression(state.addHelper("defineEnumerableProperties"), [_core.types.cloneNode(objId), _core.types.cloneNode(mutatorRef)]))); | ||
} | ||
if (single) { | ||
@@ -178,3 +162,2 @@ path.replaceWith(single); | ||
} | ||
} | ||
@@ -184,3 +167,4 @@ } | ||
}); | ||
exports.default = _default; | ||
exports.default = _default; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@babel/plugin-transform-computed-properties", | ||
"version": "7.18.9", | ||
"version": "7.20.7", | ||
"description": "Compile ES2015 computed properties to ES5", | ||
@@ -20,3 +20,4 @@ "repository": { | ||
"dependencies": { | ||
"@babel/helper-plugin-utils": "^7.18.9" | ||
"@babel/helper-plugin-utils": "^7.20.2", | ||
"@babel/template": "^7.20.7" | ||
}, | ||
@@ -27,3 +28,3 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"@babel/core": "^7.18.9", | ||
"@babel/core": "^7.20.7", | ||
"@babel/helper-plugin-test-runner": "^7.18.6" | ||
@@ -30,0 +31,0 @@ }, |
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
21599
5
154
77
1
1
1
14
3