From 6224189ca2b29042c977a01ef98c347a43c9c111 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Wed, 12 Sep 2018 10:57:57 -0500 Subject: [PATCH] Adjust TSParameterProperty handling to work with transform-parameters --- .../babel-plugin-transform-typescript/src/index.js | 11 +++++++---- .../parameter-properties-with-parameters/input.mjs | 3 +++ .../parameter-properties-with-parameters/options.json | 3 +++ .../parameter-properties-with-parameters/output.mjs | 7 +++++++ 4 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-parameters/input.mjs create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-parameters/options.json create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-parameters/output.mjs diff --git a/packages/babel-plugin-transform-typescript/src/index.js b/packages/babel-plugin-transform-typescript/src/index.js index 51c99db8ed2c..515769e7d768 100644 --- a/packages/babel-plugin-transform-typescript/src/index.js +++ b/packages/babel-plugin-transform-typescript/src/index.js @@ -151,10 +151,6 @@ export default declare((api, { jsxPragma = "React" }) => { // Rest handled by Function visitor }, - TSParameterProperty(path) { - path.replaceWith(path.node.parameter); - }, - ClassProperty(path) { const { node } = path; @@ -208,6 +204,13 @@ export default declare((api, { jsxPragma = "React" }) => { if (p0 && t.isIdentifier(p0) && p0.name === "this") { node.params.shift(); } + + // We replace `TSParameterProperty` here so that transforms that + // rely on a `Function` visitor to deal with arguments, like + // `transform-parameters`, work properly. + node.params = node.params.map(p => { + return p.type === "TSParameterProperty" ? p.parameter : p; + }); }, TSModuleDeclaration(path) { diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-parameters/input.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-parameters/input.mjs new file mode 100644 index 000000000000..e21ba592b616 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-parameters/input.mjs @@ -0,0 +1,3 @@ +export default class Example { + constructor(public arg1 = null) { } +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-parameters/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-parameters/options.json new file mode 100644 index 000000000000..e4c16fc7dcbf --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-parameters/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["transform-typescript", "transform-parameters"] +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-parameters/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-parameters/output.mjs new file mode 100644 index 000000000000..c41b0e530880 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-parameters/output.mjs @@ -0,0 +1,7 @@ +export default class Example { + constructor() { + let arg1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; + this.arg1 = arg1; + } + +}