Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assigning to fields not defined through annotations in classes leads to inject-field diagnostics #2303

Closed
Yeahyay opened this issue Sep 2, 2023 · 0 comments · Fixed by #2747

Comments

@Yeahyay
Copy link

Yeahyay commented Sep 2, 2023

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?

Annotations, Diagnostics/Syntax Checking

Expected Behaviour

Class definition.

 ---@class Class
 local Class = {
    foo = {
       bar = 1
    }
 }

Mutate instance values.

 ---@type Class
 local test

 test.foo.bar = 2 -- OK, defined in Class
 test.foo = {
    bar = 3
 } -- OK, defined in Class

 ---comment
 ---@param testParam Class
 local test2 = function(testParam)
    testParam.foo = {
       bar = 4
    } -- OK, defined in Class
 end

Actual Behaviour

Assigning to fields not defined through annotations in classes leads to inject-field diagnostics

Class is defined as having a table field foo.

---@class Class
local Class = {
   foo = {
      bar = 1
   }
}

Yet assigning to foo, even after specifying the type through @type or @param, causes an inject-fields diagnostic to occur.

 ---@type Class
 local test

 test.foo.bar = 2 -- OK
 test.foo = {
    bar = 3
 } -- Warning: inject-fields, use @class

 ---comment
 ---@param testParam Class
 local test2 = function(testParam)
    testParam.foo = {
       bar = 4
    } -- Warning: inject-fields, use @class
 end

image
image

Reproduction steps

Define a class through the @class annotation and table creation syntax.

---@class Class
local Class = {
   foo = {
      bar = 1
   }
}

Assign to fields that store table values.

 ---@type Class
 local test

 test.foo.bar = 2 -- OK
 test.foo = {
    bar = 3
 } -- Warning: inject-fields, use @class

 ---comment
 ---@param testParam Class
 local test2 = function(testParam)
    testParam.foo = {
       bar = 4
    } -- Warning: inject-fields, use @class
 end

Additional Notes

No response

Log File

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant