Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use injectInitialization to generate ts parameter properties #9610

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -175,41 +176,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;
}
}

}