Skip to content

Commit

Permalink
refactor: remove parameter property in constructor visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Mar 7, 2022
1 parent 0a1f1d6 commit fe10f62
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 43 deletions.
58 changes: 23 additions & 35 deletions packages/babel-plugin-transform-typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,36 +172,35 @@ export default declare((api: ConfigAPI, opts: Options): Plugin => {
// property is only added once. This is necessary for cases like
// using `transform-classes`, which causes this visitor to run
// twice.
const parameterProperties = [];
for (const param of path.node.params) {
if (
param.type === "TSParameterProperty" &&
!PARSED_PARAMS.has(param.parameter)
) {
PARSED_PARAMS.add(param.parameter);
parameterProperties.push(param.parameter);
}
}

if (parameterProperties.length) {
const assigns = parameterProperties.map(p => {
const assigns = [];
const { scope } = path;
for (const paramPath of path.get("params")) {
const param = paramPath.node;
if (param.type === "TSParameterProperty") {
const parameter = param.parameter;
if (PARSED_PARAMS.has(parameter)) continue;
PARSED_PARAMS.add(parameter);
let id;
if (t.isIdentifier(p)) {
id = p;
} else if (t.isAssignmentPattern(p) && t.isIdentifier(p.left)) {
id = p.left;
if (t.isIdentifier(parameter)) {
id = parameter;
} else if (
t.isAssignmentPattern(parameter) &&
t.isIdentifier(parameter.left)
) {
id = parameter.left;
} else {
throw path.buildCodeFrameError(
throw paramPath.buildCodeFrameError(
"Parameter properties can not be destructuring patterns.",
);
}
assigns.push(template.statement.ast`
this.${t.cloneNode(id)} = ${t.cloneNode(id)}`);

return template.statement.ast`
this.${t.cloneNode(id)} = ${t.cloneNode(id)}`;
});

injectInitialization(classPath, path, assigns);
paramPath.replaceWith(paramPath.get("parameter"));
scope.registerBinding("param", paramPath);
}
}
injectInitialization(classPath, path, assigns);
},
};

Expand Down Expand Up @@ -508,25 +507,14 @@ export default declare((api: ConfigAPI, opts: Options): Plugin => {
},

Function(path) {
const { node, scope } = path;
const { node } = path;
if (node.typeParameters) node.typeParameters = null;
if (node.returnType) node.returnType = null;

const params = node.params;
if (params.length > 0 && t.isIdentifier(params[0], { name: "this" })) {
params.shift();
}

// We replace `TSParameterProperty` here so that transforms that
// rely on a `Function` visitor to deal with arguments, like
// `transform-parameters`, work properly.
const paramsPath = path.get("params");
for (const p of paramsPath) {
if (p.type === "TSParameterProperty") {
p.replaceWith(p.get("parameter"));
scope.registerBinding("param", p);
}
}
},

TSModuleDeclaration(path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
let Person = /*#__PURE__*/function () {
let Person = /*#__PURE__*/babelHelpers.createClass(function Person(name) {
"use strict";

function Person(name) {
babelHelpers.classCallCheck(this, Person);
this.name = name;
}

return babelHelpers.createClass(Person);
}();
babelHelpers.classCallCheck(this, Person);
this.name = name;
});

0 comments on commit fe10f62

Please sign in to comment.