Skip to content

[BUG] Adding a decorator to a class inside a namespace causes assignation to undefined ___exports #1634

Closed
@SirWrexes

Description

@SirWrexes

Description of the issue

In my current project, I created a namespace that is split across different files with the goal of containing declarations of classes that I later declare as global, and make actually global using a decorator, as described in the docs.
However, using a decorator produces, in the compiled files, an assignation to ___exports as the last instruction of the namespace definition.

Reproduction

Aside from the declaration and definition of a simple decorator (aptly named Decorator), I've made two files which both add a class to a namespace (Repro).
WithoutDecorator.ts

namespace Repro {
    export class  WithoutDecorator {}
}

WithDecorator.ts

namespace Repro {
  @Decorator
  export class WithDecorator {}
}

Here's the produced lua for those files:
WithoutDecorator.lua

local ____lualib = require("lualib_bundle")
local __TS__Class = ____lualib.__TS__Class
Repro = Repro or ({})
do
    Repro.WithoutDecorator = __TS__Class()
    local WithoutDecorator = Repro.WithoutDecorator
    WithoutDecorator.name = "WithoutDecorator"
    function WithoutDecorator.prototype.____constructor(self)
    end
end

WithDecorator.lua

local ____lualib = require("lualib_bundle")
local __TS__Class = ____lualib.__TS__Class
local __TS__Decorate = ____lualib.__TS__Decorate
Repro = Repro or ({})
do
    Repro.WithDecorator = __TS__Class()
    local WithDecorator = Repro.WithDecorator
    WithDecorator.name = "WithDecorator"
    function WithDecorator.prototype.____constructor(self)
    end
    WithDecorator = __TS__Decorate(WithDecorator, WithDecorator, {Decorator}, {kind = "class", name = "WithDecorator"})
    ____exports.WithDecorator = WithDecorator -- [editor's note]: throws "attempt to index global '____exports' (a nil value)"
end

To test it for yourself, you can download this minimal reproduction repo: https://github.com/SirWrexes/tstl-decorator-bug.
A run.lua file is provided to set the package.path to see results of compilation (inside ./lua/) and require the two compiled sources.

npm install
npx tstl
lua ./run.lua

Expected behaviour

This assignation should not happen. Behaviour should not be different with and without a decorator on the classes inside a namespace.
The namespace itself is global, and it is not exported from its file, so the source rightly doesn't have a local named ___exports to assign to.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions