inject-field diagnostics when defining LOVE callbacks.
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
- Ensure to have LOVE Definition addon.
- Create new
main.luawith content above.
Additional Notes
LOVE addon @class is not defined with (exact).
Log File
See my response on the cross post
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.
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
- In the above meta definition file, it defined
love = {}as a global variable with a---@class loveannotation, and returns it at the bottom - For what I know,
inject-fielddiagnostic is disabled on global variable - But once you used
local love = require "love", it becomes a local variable with---@type love(as returned by aforementioned meta definition file) =>inject-fielddiagnostic is triggered - The key difference here is local variable vs global variable. Hope this explains the behavior 🙂
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 😄