@babel/helper-create-class-features-plugin
Advanced tools
Comparing version 7.2.1 to 7.2.2
@@ -6,6 +6,161 @@ "use strict"; | ||
}); | ||
exports.hasOwnDecorators = hasOwnDecorators; | ||
exports.hasDecorators = hasDecorators; | ||
exports.buildDecoratedClass = buildDecoratedClass; | ||
function hasDecorators(path) { | ||
return !!(path.node.decorators && path.node.decorators.length); | ||
function _core() { | ||
const data = require("@babel/core"); | ||
_core = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _helperReplaceSupers() { | ||
const data = _interopRequireDefault(require("@babel/helper-replace-supers")); | ||
_helperReplaceSupers = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function hasOwnDecorators(node) { | ||
return !!(node.decorators && node.decorators.length); | ||
} | ||
function hasDecorators(node) { | ||
return hasOwnDecorators(node) || node.body.body.some(hasOwnDecorators); | ||
} | ||
function prop(key, value) { | ||
if (!value) return null; | ||
return _core().types.objectProperty(_core().types.identifier(key), value); | ||
} | ||
function value(body, params = [], async, generator) { | ||
const method = _core().types.objectMethod("method", _core().types.identifier("value"), params, body); | ||
method.async = !!async; | ||
method.generator = !!generator; | ||
return method; | ||
} | ||
function takeDecorators(node) { | ||
let result; | ||
if (node.decorators && node.decorators.length > 0) { | ||
result = _core().types.arrayExpression(node.decorators.map(decorator => decorator.expression)); | ||
} | ||
node.decorators = undefined; | ||
return result; | ||
} | ||
function getKey(node) { | ||
if (node.computed) { | ||
return node.key; | ||
} else if (_core().types.isIdentifier(node.key)) { | ||
return _core().types.stringLiteral(node.key.name); | ||
} else { | ||
return _core().types.stringLiteral(String(node.key.value)); | ||
} | ||
} | ||
function extractElementDescriptor(classRef, superRef, path) { | ||
const { | ||
node, | ||
scope | ||
} = path; | ||
const isMethod = path.isClassMethod(); | ||
if (path.isPrivate()) { | ||
throw path.buildCodeFrameError(`Private ${isMethod ? "methods" : "fields"} in decorated classes are not supported yet.`); | ||
} | ||
new (_helperReplaceSupers().default)({ | ||
methodPath: path, | ||
methodNode: node, | ||
objectRef: classRef, | ||
isStatic: node.static, | ||
superRef, | ||
scope, | ||
file: this | ||
}, true).replace(); | ||
const properties = [prop("kind", _core().types.stringLiteral(isMethod ? node.kind : "field")), prop("decorators", takeDecorators(node)), prop("static", node.static && _core().types.booleanLiteral(true)), prop("key", getKey(node)), isMethod ? value(node.body, node.params, node.async, node.generator) : node.value ? value(_core().template.ast`{ return ${node.value} }`) : prop("value", scope.buildUndefinedNode())].filter(Boolean); | ||
path.remove(); | ||
return _core().types.objectExpression(properties); | ||
} | ||
function addDecorateHelper(file) { | ||
try { | ||
return file.addHelper("decorate"); | ||
} catch (err) { | ||
if (err.code === "BABEL_HELPER_UNKNOWN") { | ||
err.message += "\n '@babel/plugin-transform-decorators' in non-legacy mode" + " requires '@babel/core' version ^7.0.2 and you appear to be using" + " an older version."; | ||
} | ||
throw err; | ||
} | ||
} | ||
function buildDecoratedClass(ref, path, elements, file) { | ||
const { | ||
node, | ||
scope | ||
} = path; | ||
const initializeId = scope.generateUidIdentifier("initialize"); | ||
const isDeclaration = node.id && path.isDeclaration(); | ||
const isStrict = path.isInStrictMode(); | ||
const { | ||
superClass | ||
} = node; | ||
node.type = "ClassDeclaration"; | ||
if (!node.id) node.id = _core().types.cloneNode(ref); | ||
let superId; | ||
if (superClass) { | ||
superId = scope.generateUidIdentifierBasedOnNode(node.superClass, "super"); | ||
node.superClass = superId; | ||
} | ||
const classDecorators = takeDecorators(node); | ||
const definitions = _core().types.arrayExpression(elements.map(extractElementDescriptor.bind(file, node.id, superId))); | ||
let replacement = _core().template.expression.ast` | ||
${addDecorateHelper(file)}( | ||
${classDecorators || _core().types.nullLiteral()}, | ||
function (${initializeId}, ${superClass ? superId : null}) { | ||
${node} | ||
return { F: ${_core().types.cloneNode(node.id)}, d: ${definitions} }; | ||
}, | ||
${superClass} | ||
) | ||
`; | ||
let classPathDesc = "arguments.1.body.body.0"; | ||
if (!isStrict) { | ||
replacement.arguments[1].body.directives.push(_core().types.directive(_core().types.directiveLiteral("use strict"))); | ||
} | ||
if (isDeclaration) { | ||
replacement = _core().template.ast`let ${ref} = ${replacement}`; | ||
classPathDesc = "declarations.0.init." + classPathDesc; | ||
} | ||
return { | ||
instanceNodes: [_core().template.statement.ast`${initializeId}(this)`], | ||
wrapClass(path) { | ||
path.replaceWith(replacement); | ||
return path.get(classPathDesc); | ||
} | ||
}; | ||
} |
@@ -38,8 +38,10 @@ "use strict"; | ||
function verifyUsedFeatures(path, file) { | ||
if ((0, _decorators.hasDecorators)(path) && !hasFeature(file, FEATURES.decorators)) { | ||
throw path.buildCodeFrameError("Decorators are not enabled."); | ||
} | ||
if ((0, _decorators.hasOwnDecorators)(path)) { | ||
if (!hasFeature(file, FEATURES.decorators)) { | ||
throw path.buildCodeFrameError("Decorators are not enabled."); | ||
} | ||
if (hasFeature(file, FEATURES.decorators)) { | ||
throw new Error("@babel/plugin-class-features doesn't support decorators yet."); | ||
if (path.isPrivate()) { | ||
throw path.buildCodeFrameError(`Private ${path.isClassMethod() ? "methods" : "fields"} in decorated classes are not supported yet.`); | ||
} | ||
} | ||
@@ -46,0 +48,0 @@ |
@@ -359,2 +359,3 @@ "use strict"; | ||
const instanceNodes = []; | ||
let needsClassRef = false; | ||
@@ -371,2 +372,3 @@ for (const prop of props) { | ||
case isStatic && isPrivate && isField && loose: | ||
needsClassRef = true; | ||
staticNodes.push(buildPrivateFieldInitLoose(_core().types.cloneNode(ref), prop, privateNamesMap)); | ||
@@ -376,2 +378,3 @@ break; | ||
case isStatic && isPrivate && isField && !loose: | ||
needsClassRef = true; | ||
staticNodes.push(buildPrivateStaticFieldInitSpec(prop, privateNamesMap)); | ||
@@ -381,2 +384,3 @@ break; | ||
case isStatic && isPublic && isField && loose: | ||
needsClassRef = true; | ||
staticNodes.push(buildPublicFieldInitLoose(_core().types.cloneNode(ref), prop)); | ||
@@ -386,2 +390,3 @@ break; | ||
case isStatic && isPublic && isField && !loose: | ||
needsClassRef = true; | ||
staticNodes.push(buildPublicFieldInitSpec(_core().types.cloneNode(ref), prop, state)); | ||
@@ -423,4 +428,24 @@ break; | ||
staticNodes, | ||
instanceNodes | ||
instanceNodes, | ||
wrapClass(path) { | ||
for (const prop of props) { | ||
prop.remove(); | ||
} | ||
if (!needsClassRef) return path; | ||
if (path.isClassExpression()) { | ||
path.scope.push({ | ||
id: ref | ||
}); | ||
path.replaceWith(_core().types.assignmentExpression("=", _core().types.cloneNode(ref), path.node)); | ||
} else if (!path.node.id) { | ||
path.node.id = ref; | ||
} | ||
return path; | ||
} | ||
}; | ||
} |
@@ -24,6 +24,6 @@ "use strict"; | ||
function _core() { | ||
const data = require("@babel/core"); | ||
function _helperSplitExportDeclaration() { | ||
const data = _interopRequireDefault(require("@babel/helper-split-export-declaration")); | ||
_core = function () { | ||
_helperSplitExportDeclaration = function () { | ||
return data; | ||
@@ -37,2 +37,4 @@ }; | ||
var _decorators = require("./decorators"); | ||
var _misc = require("./misc"); | ||
@@ -74,3 +76,5 @@ | ||
let constructor; | ||
let isDecorated = (0, _decorators.hasOwnDecorators)(path.node); | ||
const props = []; | ||
const elements = []; | ||
const computedPaths = []; | ||
@@ -99,12 +103,18 @@ const privateNames = new Set(); | ||
if (path.isProperty() || path.isPrivate()) { | ||
props.push(path); | ||
} else if (path.isClassMethod({ | ||
if (path.isClassMethod({ | ||
kind: "constructor" | ||
})) { | ||
constructor = path; | ||
} else { | ||
elements.push(path); | ||
if (path.isProperty() || path.isPrivate()) { | ||
props.push(path); | ||
} | ||
} | ||
if (!isDecorated) isDecorated = (0, _decorators.hasOwnDecorators)(path.node); | ||
} | ||
if (!props.length) return; | ||
if (!props.length && !isDecorated) return; | ||
let ref; | ||
@@ -119,13 +129,26 @@ | ||
const keysNodes = (0, _misc.extractComputedKeys)(ref, path, computedPaths, this.file); | ||
const privateNamesMap = (0, _fields.buildPrivateNamesMap)(props); | ||
const privateNamesNodes = (0, _fields.buildPrivateNamesNodes)(privateNamesMap, loose, state); | ||
(0, _fields.transformPrivateNamesUsage)(ref, path, privateNamesMap, loose, state); | ||
const { | ||
staticNodes, | ||
instanceNodes | ||
} = (0, _fields.buildFieldsInitNodes)(ref, props, privateNamesMap, state, loose); | ||
let keysNodes, staticNodes, instanceNodes, wrapClass; | ||
if (isDecorated) { | ||
staticNodes = keysNodes = []; | ||
({ | ||
instanceNodes, | ||
wrapClass | ||
} = (0, _decorators.buildDecoratedClass)(ref, path, elements, this.file)); | ||
} else { | ||
keysNodes = (0, _misc.extractComputedKeys)(ref, path, computedPaths, this.file); | ||
({ | ||
staticNodes, | ||
instanceNodes, | ||
wrapClass | ||
} = (0, _fields.buildFieldsInitNodes)(ref, props, privateNamesMap, state, loose)); | ||
} | ||
if (instanceNodes.length > 0) { | ||
(0, _misc.injectInitialization)(path, constructor, instanceNodes, (referenceVisitor, state) => { | ||
if (isDecorated) return; | ||
for (const prop of props) { | ||
@@ -138,19 +161,3 @@ if (prop.node.static) continue; | ||
for (const prop of props) { | ||
prop.remove(); | ||
} | ||
if (keysNodes.length === 0 && staticNodes.length === 0 && privateNamesNodes.length === 0) { | ||
return; | ||
} | ||
if (path.isClassExpression()) { | ||
path.scope.push({ | ||
id: ref | ||
}); | ||
path.replaceWith(_core().types.assignmentExpression("=", _core().types.cloneNode(ref), path.node)); | ||
} else if (!path.node.id) { | ||
path.node.id = ref; | ||
} | ||
path = wrapClass(path); | ||
path.insertBefore(keysNodes); | ||
@@ -163,2 +170,15 @@ path.insertAfter([...privateNamesNodes, ...staticNodes]); | ||
throw path.buildCodeFrameError(`Unknown PrivateName "${path}"`); | ||
}, | ||
ExportDefaultDeclaration(path) { | ||
if (this.file.get(versionKey) !== version) return; | ||
const decl = path.get("declaration"); | ||
if (decl.isClassDeclaration() && (0, _decorators.hasDecorators)(decl.node)) { | ||
if (decl.node.id) { | ||
(0, _helperSplitExportDeclaration().default)(path); | ||
} else { | ||
decl.node.type = "ClassExpression"; | ||
} | ||
} | ||
} | ||
@@ -165,0 +185,0 @@ |
{ | ||
"name": "@babel/helper-create-class-features-plugin", | ||
"version": "7.2.1", | ||
"version": "7.2.2", | ||
"author": "The Babel Team (https://babeljs.io/team)", | ||
"license": "MIT", | ||
"description": "Compile class public and private fields, private methods and decorators to ES6", | ||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-class-features", | ||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-helper-create-class-features-plugin", | ||
"main": "lib/index.js", | ||
@@ -27,5 +27,5 @@ "publishConfig": { | ||
"devDependencies": { | ||
"@babel/core": "^7.2.0", | ||
"@babel/core": "^7.2.2", | ||
"@babel/helper-plugin-test-runner": "^7.0.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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
30138
800