Skip to content

Commit

Permalink
Use injectInitialization to generate ts parameter properties (#9610)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed May 21, 2019
1 parent 58cf1a7 commit 87fb6c4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 27 deletions.
Expand Up @@ -21,7 +21,7 @@ import {

import pkg from "../package.json";

export { FEATURES };
export { FEATURES, injectInitialization };

// Note: Versions are represented as an integer. e.g. 7.1.5 is represented
// as 70000100005. This method is easier than using a semver-parsing
Expand Down
1 change: 1 addition & 0 deletions packages/babel-plugin-transform-typescript/package.json
Expand Up @@ -13,6 +13,7 @@
"typescript"
],
"dependencies": {
"@babel/helper-create-class-features-plugin": "^7.3.4",
"@babel/helper-plugin-utils": "^7.0.0",
"@babel/plugin-syntax-typescript": "^7.2.0"
},
Expand Down
33 changes: 7 additions & 26 deletions packages/babel-plugin-transform-typescript/src/index.js
@@ -1,6 +1,7 @@
import { declare } from "@babel/helper-plugin-utils";
import syntaxTypeScript from "@babel/plugin-syntax-typescript";
import { types as t } from "@babel/core";
import { types as t, template } from "@babel/core";
import { injectInitialization } from "@babel/helper-create-class-features-plugin";

import transpileEnum from "./enum";

Expand Down Expand Up @@ -240,41 +241,21 @@ export default declare((api, { jsxPragma = "React" }) => {

if (parameterProperties.length) {
const assigns = parameterProperties.map(p => {
let name;
let id;
if (t.isIdentifier(p)) {
name = p.name;
id = p;
} else if (t.isAssignmentPattern(p) && t.isIdentifier(p.left)) {
name = p.left.name;
id = p.left;
} else {
throw path.buildCodeFrameError(
"Parameter properties can not be destructuring patterns.",
);
}

const assign = t.assignmentExpression(
"=",
t.memberExpression(t.thisExpression(), t.identifier(name)),
t.identifier(name),
);
return t.expressionStatement(assign);
return template.statement.ast`this.${id} = ${id}`;
});

const statements = childNode.body.body;

const first = statements[0];

const startsWithSuperCall =
first !== undefined &&
t.isExpressionStatement(first) &&
t.isCallExpression(first.expression) &&
t.isSuper(first.expression.callee);

// Make sure to put parameter properties *after* the `super`
// call. TypeScript will enforce that a 'super()' call is the
// first statement when there are parameter properties.
childNode.body.body = startsWithSuperCall
? [first, ...assigns, ...statements.slice(1)]
: [...assigns, ...statements];
injectInitialization(path, child, assigns);
}
} else if (child.isClassProperty()) {
childNode.typeAnnotation = null;
Expand Down
@@ -0,0 +1,9 @@
class B extends A {
constructor(public p: string) {
console.log('anything before super');
if (p) super();
else {
super();
}
}
}
@@ -0,0 +1,3 @@
{
"plugins": ["transform-typescript"]
}
@@ -0,0 +1,14 @@
class B extends A {
constructor(p) {
console.log('anything before super');

if (p) {
super();
this.p = p;
} else {
super();
this.p = p;
}
}

}

0 comments on commit 87fb6c4

Please sign in to comment.