diff --git a/packages/babel-plugin-transform-classes/src/index.js b/packages/babel-plugin-transform-classes/src/index.js index 8f0ecfc202ca..bbe0e58bdf98 100644 --- a/packages/babel-plugin-transform-classes/src/index.js +++ b/packages/babel-plugin-transform-classes/src/index.js @@ -57,9 +57,13 @@ export default declare((api, options) => { node[VISITED] = true; - path.replaceWith( - transformClass(path, state.file, builtinClasses, loose), - ); + const result = transformClass(path, state.file, builtinClasses, loose); + if (result.classAddition) { + path.replaceWith(result.classCode); + path.parentPath.parentPath.insertAfter(result.classAddition); + } else { + path.replaceWith(result); + } if (path.isCallExpression()) { annotateAsPure(path); diff --git a/packages/babel-plugin-transform-classes/src/transformClass.js b/packages/babel-plugin-transform-classes/src/transformClass.js index 5b8f5f5171b1..d1522bd631ca 100644 --- a/packages/babel-plugin-transform-classes/src/transformClass.js +++ b/packages/babel-plugin-transform-classes/src/transformClass.js @@ -681,9 +681,56 @@ export default function transformClass( directives.push(t.directive(t.directiveLiteral("use strict"))); } + const classAddition = t.expressionStatement( + t.callExpression( + { + type: "MemberExpression", + object: { + type: "Identifier", + name: "Object", + }, + property: { + type: "Identifier", + name: "defineProperty", + }, + }, + [ + t.cloneNode(classState.classRef), + { + type: "StringLiteral", + value: "prototype", + }, + { + type: "ObjectExpression", + properties: [ + { + type: "ObjectProperty", + method: false, + key: { + type: "Identifier", + name: "writable", + }, + computed: false, + shorthand: false, + value: { + type: "BooleanLiteral", + value: false, + }, + }, + ], + }, + ], + ), + ); + if (constructorOnly) { // named class with only a constructor - return t.toExpression(body[0]); + const answer = { + classCode: t.toExpression(body[0]), + classAddition, + }; + return answer; + // return t.toExpression(body[0]); } body.push(t.returnStatement(t.cloneNode(classState.classRef))); @@ -691,7 +738,12 @@ export default function transformClass( closureParams, t.blockStatement(body, directives), ); - return t.callExpression(container, closureArgs); + const answer = { + classCode: t.callExpression(container, closureArgs), + classAddition, + }; + return answer; + // return t.callExpression(container, closureArgs); } return classTransformer(path, file, builtinClasses, isLoose);