From 4ab708ed16fdbb45da4a9ef90517c9752d9c1afd Mon Sep 17 00:00:00 2001 From: hazzard993 Date: Sun, 1 Dec 2019 11:33:02 +1000 Subject: [PATCH 1/5] Avoid exporting anonymous identifiers --- src/LuaAST.ts | 3 +++ src/LuaTransformer.ts | 4 ++++ test/translation/__snapshots__/transformation.spec.ts.snap | 6 ++++++ .../transformation/variableStatementExportAnonymous.ts | 1 + 4 files changed, 14 insertions(+) create mode 100644 test/translation/transformation/variableStatementExportAnonymous.ts diff --git a/src/LuaAST.ts b/src/LuaAST.ts index d88dbef09..7fcd4a74b 100644 --- a/src/LuaAST.ts +++ b/src/LuaAST.ts @@ -829,6 +829,7 @@ export function createMethodCallExpression( export interface Identifier extends Expression { kind: SyntaxKind.Identifier; + exportable: boolean; text: string; originalName?: string; symbolId?: SymbolId; @@ -846,6 +847,7 @@ export function createIdentifier( parent?: Node ): Identifier { const expression = createNode(SyntaxKind.Identifier, tsOriginal, parent) as Identifier; + expression.exportable = true; expression.text = text; expression.symbolId = symbolId; expression.originalName = originalName; @@ -858,6 +860,7 @@ export function cloneIdentifier(identifier: Identifier, tsOriginal?: ts.Node): I export function createAnonymousIdentifier(tsOriginal?: ts.Node, parent?: Node): Identifier { const expression = createNode(SyntaxKind.Identifier, tsOriginal, parent) as Identifier; + expression.exportable = false; expression.text = "____"; return expression; } diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index 5479fc889..8287ebb08 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -5480,6 +5480,10 @@ export class LuaTransformer { identifier: tstl.Identifier, exportScope?: ts.SourceFile | ts.ModuleDeclaration ): tstl.AssignmentLeftHandSideExpression { + if (!identifier.exportable) { + return identifier; + } + const exportTable = exportScope && ts.isModuleDeclaration(exportScope) ? this.createModuleLocalNameIdentifier(exportScope) diff --git a/test/translation/__snapshots__/transformation.spec.ts.snap b/test/translation/__snapshots__/transformation.spec.ts.snap index 044e1bf82..314936c69 100644 --- a/test/translation/__snapshots__/transformation.spec.ts.snap +++ b/test/translation/__snapshots__/transformation.spec.ts.snap @@ -279,3 +279,9 @@ exports[`Transformation (unusedDefaultWithNamespaceImport) 1`] = ` "local x = require(\\"module\\") local ____ = x" `; + +exports[`Transformation (variableStatementExportAnonymous) 1`] = ` +"local ____exports = {} +____exports.x, ____, ____exports.y = 1, 2, 3 +return ____exports" +`; diff --git a/test/translation/transformation/variableStatementExportAnonymous.ts b/test/translation/transformation/variableStatementExportAnonymous.ts new file mode 100644 index 000000000..2f4222229 --- /dev/null +++ b/test/translation/transformation/variableStatementExportAnonymous.ts @@ -0,0 +1 @@ +export const [x, , y] = [1, 2, 3]; From 50ece7f11f79c97f060146d481e54a34dec5353b Mon Sep 17 00:00:00 2001 From: hazzard993 Date: Tue, 3 Dec 2019 16:43:02 +1000 Subject: [PATCH 2/5] Use existing test for anonymous destructuring --- test/translation/__snapshots__/transformation.spec.ts.snap | 6 ------ .../transformation/variableStatementExportAnonymous.ts | 1 - test/unit/destructuring.spec.ts | 3 +-- 3 files changed, 1 insertion(+), 9 deletions(-) delete mode 100644 test/translation/transformation/variableStatementExportAnonymous.ts diff --git a/test/translation/__snapshots__/transformation.spec.ts.snap b/test/translation/__snapshots__/transformation.spec.ts.snap index 314936c69..044e1bf82 100644 --- a/test/translation/__snapshots__/transformation.spec.ts.snap +++ b/test/translation/__snapshots__/transformation.spec.ts.snap @@ -279,9 +279,3 @@ exports[`Transformation (unusedDefaultWithNamespaceImport) 1`] = ` "local x = require(\\"module\\") local ____ = x" `; - -exports[`Transformation (variableStatementExportAnonymous) 1`] = ` -"local ____exports = {} -____exports.x, ____, ____exports.y = 1, 2, 3 -return ____exports" -`; diff --git a/test/translation/transformation/variableStatementExportAnonymous.ts b/test/translation/transformation/variableStatementExportAnonymous.ts deleted file mode 100644 index 2f4222229..000000000 --- a/test/translation/transformation/variableStatementExportAnonymous.ts +++ /dev/null @@ -1 +0,0 @@ -export const [x, , y] = [1, 2, 3]; diff --git a/test/unit/destructuring.spec.ts b/test/unit/destructuring.spec.ts index 6572d8b6b..185c3cc54 100644 --- a/test/unit/destructuring.spec.ts +++ b/test/unit/destructuring.spec.ts @@ -51,9 +51,8 @@ test.each(testCases)("in variable declaration (%p)", ({ binding, value }) => { `.expectToMatchJsResult(); }); -// TODO: https://github.com/TypeScriptToLua/TypeScriptToLua/issues/695 // TODO: https://github.com/microsoft/TypeScript/issues/32656 -test.each(testCases.filter(x => x.binding !== "[x, , y]" && x.binding !== "{ x, ...rest }"))( +test.each(testCases.filter(x => x.binding !== "{ x, ...rest }"))( "in exported variable declaration (%p)", ({ binding, value }) => { util.testModule` From 811a1c0050bbd1b9bd13a1547959b87dabeb8603 Mon Sep 17 00:00:00 2001 From: hazzard993 Date: Tue, 3 Dec 2019 16:47:18 +1000 Subject: [PATCH 3/5] Remove TODO for issue 32656 with test filter --- test/unit/destructuring.spec.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/test/unit/destructuring.spec.ts b/test/unit/destructuring.spec.ts index 185c3cc54..1b6af50db 100644 --- a/test/unit/destructuring.spec.ts +++ b/test/unit/destructuring.spec.ts @@ -51,15 +51,11 @@ test.each(testCases)("in variable declaration (%p)", ({ binding, value }) => { `.expectToMatchJsResult(); }); -// TODO: https://github.com/microsoft/TypeScript/issues/32656 -test.each(testCases.filter(x => x.binding !== "{ x, ...rest }"))( - "in exported variable declaration (%p)", - ({ binding, value }) => { - util.testModule` +test.each(testCases)("in exported variable declaration (%p)", ({ binding, value }) => { + util.testModule` export const ${binding} = ${value}; `.expectToMatchJsResult(); - } -); +}); const assignmentTestCases = [ ...testCases, From ddac23fd118db09fea2064f2063cd0f1dffdbcae Mon Sep 17 00:00:00 2001 From: hazzard993 Date: Tue, 3 Dec 2019 17:03:02 +1000 Subject: [PATCH 4/5] Update test/unit/destructuring.spec.ts Co-Authored-By: ark120202 --- test/unit/destructuring.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/destructuring.spec.ts b/test/unit/destructuring.spec.ts index 1b6af50db..9fe43e12e 100644 --- a/test/unit/destructuring.spec.ts +++ b/test/unit/destructuring.spec.ts @@ -54,7 +54,7 @@ test.each(testCases)("in variable declaration (%p)", ({ binding, value }) => { test.each(testCases)("in exported variable declaration (%p)", ({ binding, value }) => { util.testModule` export const ${binding} = ${value}; - `.expectToMatchJsResult(); + `.expectToMatchJsResult(); }); const assignmentTestCases = [ From 1f3ac33a98519f8bcc709f50f444cac3c5e0718e Mon Sep 17 00:00:00 2001 From: hazzard993 Date: Tue, 3 Dec 2019 17:06:44 +1000 Subject: [PATCH 5/5] Update test/unit/destructuring.spec.ts Co-Authored-By: ark120202 --- test/unit/destructuring.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/destructuring.spec.ts b/test/unit/destructuring.spec.ts index 9fe43e12e..ed6d5219d 100644 --- a/test/unit/destructuring.spec.ts +++ b/test/unit/destructuring.spec.ts @@ -53,7 +53,7 @@ test.each(testCases)("in variable declaration (%p)", ({ binding, value }) => { test.each(testCases)("in exported variable declaration (%p)", ({ binding, value }) => { util.testModule` - export const ${binding} = ${value}; + export const ${binding} = ${value}; `.expectToMatchJsResult(); });