Skip to content

Commit

Permalink
Register binding when transforming TSParameterProperty (#12796)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Feb 16, 2021
1 parent 0bb5700 commit 99130bc
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
Expand Up @@ -177,14 +177,14 @@ const rewriteReferencesVisitor = {

const localName = path.node.name;

const localBinding = path.scope.getBinding(localName);
const rootBinding = scope.getBinding(localName);

// redeclared in this scope
if (rootBinding !== localBinding) return;

const importData = imported.get(localName);
if (importData) {
const localBinding = path.scope.getBinding(localName);
const rootBinding = scope.getBinding(localName);

// redeclared in this scope
if (rootBinding !== localBinding) return;

const ref = buildImportReference(importData, path.node);

// Preserve the binding location so that sourcemaps are nicer.
Expand Down
19 changes: 12 additions & 7 deletions packages/babel-plugin-transform-typescript/src/index.js
Expand Up @@ -384,21 +384,26 @@ export default declare((api, opts) => {
});
},

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

const p0 = node.params[0];
if (p0 && t.isIdentifier(p0) && p0.name === "this") {
node.params.shift();
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.
node.params = node.params.map(p => {
return p.type === "TSParameterProperty" ? p.parameter : p;
});
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
@@ -0,0 +1,8 @@
import { messaging } from 'firebase-admin'

export class Something {
constructor(
public messaging: messaging.Messaging
) {
}
}
@@ -0,0 +1,3 @@
{
"plugins":["transform-typescript", "transform-classes", "transform-modules-commonjs"]
}
@@ -0,0 +1,18 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Something = void 0;

var _firebaseAdmin = require("firebase-admin");

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

let Something = function Something(messaging) {
_classCallCheck(this, Something);

this.messaging = messaging;
};

exports.Something = Something;

0 comments on commit 99130bc

Please sign in to comment.