Skip to content

Local reference to enum defined in an object from a different file, same workspace #3328

@erunehtar

Description

@erunehtar

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Diagnostics/Syntax Checking

Expected Behaviour

file1.lua

local module = ...

---@enum Module.Color
module.Color = {
    White = "ffffffff",
    Black = "ff000000",
    Yellow = etc...
}

file2.lua

local module = ...

---@type Module.Color
local Color = module.Color

---@param color Module.Color
local function SetColor(color)
    -- code ...
end

---@param text string
local function Print(text)
    SetColor(Color.Blue) -- no diagnostic errors
end

Actual Behaviour

file1.lua

local module = ...

---@enum Module.Color
module.Color = {
    White = "ffffffff",
    Black = "ff000000",
    Yellow = etc...
}

file2.lua

local module = ...

---@type Module.Color
local Color = module.Color

---@param color Module.Color
local function SetColor(color)
    -- code ...
end

---@param text string
local function Print(text)
    SetColor(Color.Blue) -- Undefined field `Blue`. Lua Diagnostics.(undefined-field)
end

Reproduction steps

Simply make a local reference to an enum defined in a table, try to use it.

Additional Notes

I want to share an enum across lua files and have a reference on it so that I can type Color.Blue instead of module.Color.Blue. Lua allows me to have a local reference, doing local Color = module.Color compile and works just fine, but the diagnostic is not happy, marking the local reference as ---@type Module.Color doesn't seem enough.

I feel like I might be doing this wrong, is there a syntax to achieve what I am trying to do? I cannot put the enum Color in global space, I have to put it in the module object, so that it does not clash with other modules.

Log File

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions