From ad206d730dfe33aeab7c3fa9a49d632c5951fac1 Mon Sep 17 00:00:00 2001 From: hazzard993 Date: Sat, 18 Jan 2020 11:41:26 +1000 Subject: [PATCH 1/2] Stop prefixing identifiers from global augs --- src/transformation/visitors/identifier.ts | 6 +++++- test/translation/__snapshots__/transformation.spec.ts.snap | 5 +++++ test/translation/transformation/globalAugmentation.ts | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 test/translation/transformation/globalAugmentation.ts diff --git a/src/transformation/visitors/identifier.ts b/src/transformation/visitors/identifier.ts index 565c0625b..b883269bc 100644 --- a/src/transformation/visitors/identifier.ts +++ b/src/transformation/visitors/identifier.ts @@ -26,12 +26,16 @@ export function transformIdentifier(context: TransformationContext, identifier: return lua.createIdentifier(text, identifier, symbolId, identifier.text); } +function isGlobalAugmentation(module: ts.ModuleDeclaration): boolean { + return (module.flags & ts.NodeFlags.GlobalAugmentation) !== 0; +} + export const transformIdentifierExpression: FunctionVisitor = (node, context) => { // TODO: Move below to avoid extra transforms? const identifier = transformIdentifier(context, node); const exportScope = getIdentifierExportScope(context, identifier); - if (exportScope) { + if (exportScope && !(ts.isModuleDeclaration(exportScope) && isGlobalAugmentation(exportScope))) { return createExportedIdentifier(context, identifier, exportScope); } diff --git a/test/translation/__snapshots__/transformation.spec.ts.snap b/test/translation/__snapshots__/transformation.spec.ts.snap index f4e2ce661..dda4cea2f 100644 --- a/test/translation/__snapshots__/transformation.spec.ts.snap +++ b/test/translation/__snapshots__/transformation.spec.ts.snap @@ -82,6 +82,11 @@ end return ____exports" `; +exports[`Transformation (globalAugmentation) 1`] = ` +"local ____exports = globalVariable +return ____exports" +`; + exports[`Transformation (methodRestArguments) 1`] = ` "require(\\"lualib_bundle\\"); MyClass = __TS__Class() diff --git a/test/translation/transformation/globalAugmentation.ts b/test/translation/transformation/globalAugmentation.ts new file mode 100644 index 000000000..03f2c5af2 --- /dev/null +++ b/test/translation/transformation/globalAugmentation.ts @@ -0,0 +1,5 @@ +declare global { + export const globalVariable: number; +} + +export = globalVariable; From 15176a73b7a2ee7b1e39c1b1025091ea0f8e5bbc Mon Sep 17 00:00:00 2001 From: hazzard993 Date: Sun, 19 Jan 2020 14:24:04 +1000 Subject: [PATCH 2/2] Check for GlobalAug inside getSymbolExportScope --- src/transformation/utils/export.ts | 8 ++++++++ src/transformation/visitors/identifier.ts | 6 +----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/transformation/utils/export.ts b/src/transformation/utils/export.ts index d509504d2..5922a9fc5 100644 --- a/src/transformation/utils/export.ts +++ b/src/transformation/utils/export.ts @@ -47,6 +47,10 @@ export function getIdentifierExportScope( return getSymbolExportScope(context, symbol); } +function isGlobalAugmentation(module: ts.ModuleDeclaration): boolean { + return (module.flags & ts.NodeFlags.GlobalAugmentation) !== 0; +} + export function getSymbolExportScope( context: TransformationContext, symbol: ts.Symbol @@ -64,6 +68,10 @@ export function getSymbolExportScope( return undefined; } + if (ts.isModuleDeclaration(scope) && isGlobalAugmentation(scope)) { + return undefined; + } + if (!isSymbolExportedFromScope(context, symbol, scope)) { return undefined; } diff --git a/src/transformation/visitors/identifier.ts b/src/transformation/visitors/identifier.ts index b883269bc..565c0625b 100644 --- a/src/transformation/visitors/identifier.ts +++ b/src/transformation/visitors/identifier.ts @@ -26,16 +26,12 @@ export function transformIdentifier(context: TransformationContext, identifier: return lua.createIdentifier(text, identifier, symbolId, identifier.text); } -function isGlobalAugmentation(module: ts.ModuleDeclaration): boolean { - return (module.flags & ts.NodeFlags.GlobalAugmentation) !== 0; -} - export const transformIdentifierExpression: FunctionVisitor = (node, context) => { // TODO: Move below to avoid extra transforms? const identifier = transformIdentifier(context, node); const exportScope = getIdentifierExportScope(context, identifier); - if (exportScope && !(ts.isModuleDeclaration(exportScope) && isGlobalAugmentation(exportScope))) { + if (exportScope) { return createExportedIdentifier(context, identifier, exportScope); }