From 3f394d7adbfdb696f02f37d98745cd2866f43ce6 Mon Sep 17 00:00:00 2001 From: aleclarson Date: Fri, 11 Jan 2019 14:20:51 -0500 Subject: [PATCH 1/3] fix: CJSImportTransformer#processAssignment In the presence of a type token, there's nothing to do. Fixes #401 --- src/transformers/CJSImportTransformer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/CJSImportTransformer.ts b/src/transformers/CJSImportTransformer.ts index bceee7ba..2fc833d6 100644 --- a/src/transformers/CJSImportTransformer.ts +++ b/src/transformers/CJSImportTransformer.ts @@ -274,7 +274,7 @@ export default class CJSImportTransformer extends Transformer { private processAssignment(): boolean { const index = this.tokens.currentIndex(); const identifierToken = this.tokens.tokens[index - 1]; - if (identifierToken.type !== tt.name) { + if (identifierToken.isType || identifierToken.type !== tt.name) { return false; } if (index >= 2 && this.tokens.matches1AtIndex(index - 2, tt.dot)) { From 2f64ac78083b5afcd2be3bb32a70ae28fd10f369 Mon Sep 17 00:00:00 2001 From: aleclarson Date: Fri, 11 Jan 2019 14:24:23 -0500 Subject: [PATCH 2/3] test: issue 401 --- test/typescript-test.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/typescript-test.ts b/test/typescript-test.ts index fbce761c..27e4b917 100644 --- a/test/typescript-test.ts +++ b/test/typescript-test.ts @@ -1356,4 +1356,17 @@ describe("typescript transform", () => { `, ); }); + + it("fixes issue #401", () => { + assertTypeScriptResult( + ` + export class Foo {} + let foo: Foo = new Foo(); + `, + `"use strict";${ESMODULE_PREFIX} + class Foo {} exports.Foo = Foo; + let foo = new Foo(); + `, + ); + }); }); From bdfbd98ded52298c62c4e8328c7a6d095e4ce0ca Mon Sep 17 00:00:00 2001 From: Alan Pierce Date: Sat, 12 Jan 2019 13:16:54 -0500 Subject: [PATCH 3/3] Apply suggestions from code review Co-Authored-By: aleclarson --- src/transformers/CJSImportTransformer.ts | 2 ++ test/typescript-test.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/transformers/CJSImportTransformer.ts b/src/transformers/CJSImportTransformer.ts index 2fc833d6..de9db4c0 100644 --- a/src/transformers/CJSImportTransformer.ts +++ b/src/transformers/CJSImportTransformer.ts @@ -274,6 +274,8 @@ export default class CJSImportTransformer extends Transformer { private processAssignment(): boolean { const index = this.tokens.currentIndex(); const identifierToken = this.tokens.tokens[index - 1]; + // If the LHS is a type identifier, this must be a declaration like `let a: b = c;`, + // with `b` as the identifier, so nothing needs to be done in that case. if (identifierToken.isType || identifierToken.type !== tt.name) { return false; } diff --git a/test/typescript-test.ts b/test/typescript-test.ts index 27e4b917..0518976f 100644 --- a/test/typescript-test.ts +++ b/test/typescript-test.ts @@ -1357,7 +1357,7 @@ describe("typescript transform", () => { ); }); - it("fixes issue #401", () => { + it("properly handles a declaration that looks like an assignment to an export (#401)", () => { assertTypeScriptResult( ` export class Foo {}