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

Register binding when transforming TSParameterProperty #12796

Merged
merged 3 commits into from Feb 16, 2021
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 @@ -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;