LuaLS / lua-language-server

A language server that offers Lua language support - programmed in Lua
https://luals.github.io
MIT License
3.35k stars 320 forks source link

inject-field diagnostics when defining LOVE callbacks. #2316

Open MikuAuahDark opened 1 year ago

MikuAuahDark commented 1 year ago

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, Libraries

Expected Behaviour

This should be clean.

local love = require("love")

function love.load() -- no diagnostics here
end

Actual Behaviour

local love = require("love")

function love.load() -- Fields cannot be injected into the reference of `love` for `load`. To do so, use `---@class` for `love`. (inject-field)
end

Reproduction steps

  1. Ensure to have LOVE Definition addon.
  2. Create new main.lua with content above.

Additional Notes

LOVE addon @class is not defined with (exact).

Log File

file_d%3A_Data_Development_love-11.3-win64_inochi2d.log

JJSax commented 3 months ago

See my response on the cross post

MikuAuahDark commented 3 months ago

If the global variable love and require("love") is exactly the same, why the different diagnostic result? Shouldn't it be same, either giving inject-field warning or no warning at all on both?

Aside that, IMO having require("love") makes it clear to user who reads the source code that in the specific file, LOVE is being used.

tomlau10 commented 3 months ago

If the global variable love and require("love") is exactly the same, why the different diagnostic result?

It seems that you have configured your project to use the love2d meta definition as library, i.e. in the .luarc.json you may have setup something like this:

    "workspace.library": [
        "${3rd}/love2d/library"
    ]

=> this will include the built-in definition files for love2d https://github.com/LuaCATS/love2d/blob/dad72a7eae31f35bf4c6529e5b81f6187b5b7377/library/love.lua


IMO having require("love") makes it clear to user who reads the source code that in the specific file, LOVE is being used.

If you really wants to write require("love") in the file, I suggest writing it as:

love = love or require("love")

function love.load() -- no more warning now
end

=> this makes use of the fact that inject-field diagnostic is disabled on global variable 😄