Comments (4)
I would expect that inject-field error only show up if exact is provided the class annotation
Nope. This is the normal behavior.
Basically You can declare class fields in 2 ways:
- directly use
---@field
- add fields (or methods) to the variable annotated by
---@class
- or use combination of the above
- In your example
local Foo
is annotated by---@class
, so you can add fields/methods to this variable and LuaLS will merges added fields into the classFoo
:
---@class Foo
---@field a string -- method1: you declared a string field `a` by `---@field`
local Foo = {}
---@return Foo
function Foo.new() -- method2: you are adding a `new()` function field to the class `Foo`
return {a = 'abc'} -- Pretend this make a proper new object or smt
end
- On the other hand,
---@class (exact)
means that you can only use method 1 (---@field
) to declare fields/function/methods of a class. That is if you write---@class (exact) Foo
, yourfunction Foo.new
will already throw thisinject-field
warning.
A few ways to tackle this warning:
- declare the
bar
field in---@class Foo
- Add
---@class
forfoo
(as stated by the warning)
local function doSomething()
---@class Foo
local foo = Foo.new()
...
end
- disable the
inject-field
warning
The last one is probably not a good idea, because in this case, LuaLS actually DOES NOT KNOW that Foo
class has the field bar
, and you can't do those goto defintion thing for this field.
from lua-language-server.
Then the docs probably needs to be updated as they state
Marking the class as (exact) means fields cannot be injected after the definition.
With the example used as
---@class (exact) Point
---@field x number
---@field y number
local Point = {}
Point.x = 1 -- OK
Point.y = 2 -- OK
Point.z = 3 -- Warning
from lua-language-server.
I think the example that you quoted from wiki is perfectly fine?
It strictly follows the rule "fields cannot be injected after the definition"
- there exist
x
andy
fields by using the---@field
annotation - therefore the following
Point.x
andPoint.y
=> OK - but the last one
Point.z
triggers theinject-field
warning
In my own opinion, there is indeed a difference between a class
and an instance
.
- when you annotate the variable using
---@class
=> this is a class variable and newly injected fields should be merged to class definition - while in your first example
local foo = Foo.new()
=> this just returns aninstance
of theFoo
class, and generally no extra fields should be allowed to inject into an instance ofFoo
class, which will make this variable different from the definition of exitingFoo
class. Unless you explicitly specify that the new variable acts as a class variable. 🤔
from lua-language-server.
Alright, fair.
I still think the documentation should be clarified
from lua-language-server.
Related Issues (20)
- Using `diagnostics.severity` setting in `.luarc.json` seems to break the severity level filtering in generated report HOT 1
- Type checking does not work at all with `` (backtick) literals
- False negative for param-type-mismatch when using a variable as argument
- Missing fields not working for inherited classes HOT 1
- Error message after update to version 3.9.3 HOT 3
- "Save as" can cause "duplicate defined alias"
- bad Diagnostics. (inject-field) HOT 12
- support `+T` `+?` `-T` `-?` to edit types in `@as` like `@cast`
- lua-language-server --check Fails to Detect Issues in v3.8.0+ HOT 2
- the visibility of the fields of a class is not correctly evaluated when they are defined directly in the object.
- The `set` pattern for `single range char` is not working in glob HOT 1
- cant annotate string literal `'`, `"` HOT 1
- Function overloads with function args not narrowing inner function params HOT 4
- support expanding `var++` as snippet HOT 6
- How to document any type but nil? HOT 4
- `---@cast` annotation doesn't respect scope rules HOT 3
- completion returns wrong and according to the spec invalid results HOT 11
- Regular comments (i.e. `-- comment`) shouldn't be included in documentation HOT 17
- Using 'then' or 'do' in string literals incorrectly causes indentation HOT 4
- More Unreachable Code Undetected HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lua-language-server.