Skip to content

Commit

Permalink
initial code 2 fix the issue #2025
Browse files Browse the repository at this point in the history
  • Loading branch information
wentout committed Sep 28, 2020
1 parent a080c82 commit a054296
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
10 changes: 7 additions & 3 deletions packages/babel-plugin-transform-classes/src/index.js
Expand Up @@ -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);
Expand Down
56 changes: 54 additions & 2 deletions packages/babel-plugin-transform-classes/src/transformClass.js
Expand Up @@ -681,17 +681,69 @@ 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)));
const container = t.arrowFunctionExpression(
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);
Expand Down

0 comments on commit a054296

Please sign in to comment.