Repository navigation
Breaking changes in 3.X #980
Description
Introduce
I'm working on the 3.X version, the purpose is to add a more complete type system, and use this to implement features such as type checking.
In previous versions I tried to keep the type system compatible with EmmyLua, but I will make some breaking changes after version 3.X, so the type system may not be suitable to continue to be called EmmyLua in the future, but I haven't thought about it yet, I will call it .LuaDoc for now
Breaking changes
Always trace local set
local x
x = 1
x = true
print(x) --> x is `integer|boolean`---@type string
local x
x = 1
x = true
print(x) --> x is `string`local x = 'str'
x = 1
x = true
print(x) --> x is `string`Dose not trace field inject
local x = {}
local y = x
y.field = 1
print(x.field) --> undefined field `field`use ---@class instead
---@class MyClass
local x = {}
---@class MyClass
local y = x
y.field = 1
print(x.field) --> `x.field` is `1`Finding definition does not recurse
local t = {}
t.x = 1
t.y = t.x
print(t.y) --> t.y is `1`Find definition of t.y will not find out t.x
---@class C
local mt = {}
---@type C
local oFind definition of o will not find out mt
Type unknown
local t = {}
print(t.x) --> t.x is `unknown` instead of `any`Types can be used in doc-table keys
---@type { [number]: string }
local t
print(t[1]) --> t[1] is `string`
print(t.x) --> t.x is `unknown`boolean type true and false
You can use ---@type { [string]: true } now.
class and alias support generic inheritance
---@class table<K, V>: { [K]: V } --> this is builtin
---@alias mark<K> { [K]: true } table<integer, string> can not convert to string[]
table<integer, string> can not convert to string[]Default value of unrary and binary
---@type unknown
local x
local y = - x --> y is `number` instead of `unknown`unknown can form union types with other types
function f()
if x then
return y
else
return nil
end
end
local n = f() --> n is `unknown|nil`Integer type
---@type 1|2|3|4|5
local vdoc-enum must be a string
---@param x 'aaa'|'bbb'
function f(x)
print(x) --> x is `"aaa"` or `"bbb"`
endThis means you can no longer use ---@param x '1024'|'true'|'function () end' to represent a specific number, boolean or function. Use ---@param x 1024|true|fun() instead.
For compatibility, I will continue to recognize '"xxx"' as the string xxx instead of "xxx".
integer is a class in Lua 5.1 and Lua 5.2
---@type integer
local v
print(v) --> v is `integer` instead of `number`Although Lua 5.1 and Lua 5.2 runtimes do not have integer, many API parameters require a number that can be converted to an integer, so adding integer to the type system can help you handle this part.
Not supported for now
search metatable in this case
setmetatable(object, { __index = {
default = 1
} })
print(object.default) --> undefined field `default`This turns the reference into a definition, and checking the reference in order to search for a definition is too expensive.
Please use ---@class or use this:
object = setmetatable(object, { __index = {
default = 1
} })
print(object.default) --> this is OKMaybe can be resolved by indexing
setmetable
Activity
XeroOl commentedon Mar 29, 2022
I think
LuaDocname is taken by https://keplerproject.github.io/luadoc/sumneko commentedon Mar 30, 2022
Maybe I can continue to call it
Emmylua, since the original author is not active, then I can grab it 🙃.sumneko commentedon Apr 8, 2022
I have merged a
3.0branch toMaster, if you find a major problem, you can return to the label2.6.8faimin commentedon Apr 10, 2022
The CPU usage of the latest plugin version (
v3.0.0) inVSCodeis full.Silent-zzz commentedon Apr 10, 2022
多个文件声明类的方法该怎么写呢
Silent-zzz commentedon Apr 10, 2022
例如

sumneko commentedon Apr 10, 2022
请单独开个issue并提供日志
sumneko commentedon Apr 10, 2022
你可以多次使用
---@class unitfiras-assaad commentedon Apr 10, 2022
Thanks for the great server/extension!
With the latest version of the VS Code extension (3.0.0) I get a lot of undefined field errors in classes. e.g.
I actually use middleclass for classes, but that used to work fine in previous versions. Is this the same unsupported case as "find definition in methods"?
As I use classes heavily, this is raising a lot of warnings. Is there a workaround or do you recommend that I revert to a previous 2.X version?
sumneko commentedon Apr 10, 2022
You can choose:
---@field field booleanunder---@class x#980
sumneko commentedon Apr 17, 2022
Supported finding definition of
self.xxxin version 3.1.0Arxareon commentedon May 24, 2022
I think this issue we found all links to here: #1158
I'd very much like to have the old functionality from 2.6.8 back. There are fringe cases in WoW addon development (addon namespace tables as an example) where field references being pulled through tables and subtables are very much needed. I tried adding
@classand@fielddefinitions to rectify the issue, but I wasn't able to fix annotations and types in all cases. Also, I wouldn't like adding more unnecessary lines to the already long files just to have annotations working between all files of a project.See the above mentioned issue and further quoted issue from Ketho's WoW API extension repo (also mentioned within the above mentioned issue).
Thank you for working on this! :)
If you need details on how we use it specifically for WoW or have tips for us to change our workflow, I'd be happy to get back to you on that.
SalavatR commentedon Oct 31, 2022
Hello!
is any update for this issue?
in some cases we have more about 100 variables in self, and it is not comfortable to declare all as ---@field
Arxareon commentedon Nov 1, 2022
@SalavatR
I've reverted to using version 2.6.8 until this gets resolved without needing to use excessive documentation for references to be recognized. It works fine for me even though I wish I had some of the new features available as well (like the recognition of aliases - but I can always collapse them manually now) - so unless you need something new, I suggest reverting the extension back to 2.6.8 for the time being (not sure if sumneko is planning to revert any of these changes in one way or another, so it might be indefinitely).
sumneko commentedon Nov 2, 2022
You can use
This is to prevent fields from being declared unexpectedly.
SalavatR commentedon Nov 2, 2022
Thanks!
What about performance of this?
And will LLS analize all of usings of param?
sumneko commentedon Nov 2, 2022
@SalavatR LLS only analize fields in
---@class, this is both strict and performance oriented.Iyadriel commentedon Nov 14, 2022
I was able to make references across namespaces work in the latest version by simply using the following syntax in every file:
Does this work for you?
Edit -- Never mind, this only seems to work for a depth of one level.
bandaloo commentedon Jan 28, 2023
it would be really nice to have a
@namespaceinstead of having to use@classfor simple modules. it is a little funky to mark a table as@classwhen you're not interested in using it as a type or instantiating it, and it makes no reference toself. if you don't use@class, it's easy to make a typo and access a field that doesn't exist; it will be treated asunknown.@classdoes the job though! it's just slightly more feature-rich than what i want most of the time.gynt commentedon Apr 7, 2025
Will this break compatibility with
sphinx-lua? https://github.com/boolangery/sphinx-lua