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

fix: make get_types report all inherited interface fields #857

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions spec/api/get_types_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,138 @@ describe("tl.get_types", function()
assert.same(fields, {"init", "x", "y"})
end)

it("reports inherited interface fields in record field list, case 1 (#852)", function()
local env = tl.init_env()
env.report_types = true
local result = assert(tl.check_string([[
local interface IFoo
bar: function(self)
end

local record Foo is IFoo
-- Uncommenting this causes 'bar' to be hidden from fields of Foo
qux:function(Foo)

-- Using this style doesn't have this problem
-- qux:function(self)
end

function Foo:bar()
end

function Foo:qux()
end

local record Runner
foo: Foo
end

function Runner:run()
-- self.foo.
end
]], env))

local tr, trenv = tl.get_types(result)
local y = 5
local x = 10
local type_at_y_x = tr.by_pos[""][y][x]
assert(tr.types[type_at_y_x].str == "Foo")
local fields = {}
for k, _ in pairs(tr.types[type_at_y_x].fields) do
table.insert(fields, k)
end
table.sort(fields)
assert.same(fields, {"bar", "qux"})
end)

it("reports inherited interface fields in record field list, case 2 (#852)", function()
local env = tl.init_env()
env.report_types = true
local result = assert(tl.check_string([[
local interface IFoo
bar: function(self)
end

local record Foo is IFoo
-- Uncommenting this causes 'bar' to be hidden from fields of Foo
-- qux:function(Foo)

-- Using this style doesn't have this problem
qux:function(self)
end

function Foo:bar()
end

function Foo:qux()
end

local record Runner
foo: Foo
end

function Runner:run()
-- self.foo.
end
]], env))

local tr, trenv = tl.get_types(result)
local y = 5
local x = 10
local type_at_y_x = tr.by_pos[""][y][x]
assert(tr.types[type_at_y_x].str == "Foo")
local fields = {}
for k, _ in pairs(tr.types[type_at_y_x].fields) do
table.insert(fields, k)
end
table.sort(fields)
assert.same(fields, {"bar", "qux"})
end)

it("reports inherited interface fields in record field list, case 3 (#852)", function()
local env = tl.init_env()
env.report_types = true
local result = assert(tl.check_string([[
local interface IFoo
bar: function(self)
end

local record Foo is IFoo
-- Uncommenting this causes 'bar' to be hidden from fields of Foo
-- qux:function(Foo)

-- Using this style doesn't have this problem
-- qux:function(self)
end

function Foo:bar()
end

function Foo:qux()
end

local record Runner
foo: Foo
end

function Runner:run()
-- self.foo.
end
]], env))

local tr, trenv = tl.get_types(result)
local y = 5
local x = 10
local type_at_y_x = tr.by_pos[""][y][x]
assert(tr.types[type_at_y_x].str == "Foo")
local fields = {}
for k, _ in pairs(tr.types[type_at_y_x].fields) do
table.insert(fields, k)
end
table.sort(fields)
assert.same(fields, {"bar", "qux"})
end)

it("reports reference of a nominal type", function()
local env = tl.init_env()
env.report_types = true
Expand Down
6 changes: 6 additions & 0 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13043,6 +13043,12 @@ self:expand_type(node, values, elements) })

if typ.interface_list then
self:expand_interfaces(typ)

if self.collector then
for fname, ftype in fields_of(typ) do
self.env.reporter:add_field(typ, fname, ftype)
end
end
end

if fmacros then
Expand Down
6 changes: 6 additions & 0 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -13043,6 +13043,12 @@ do

if typ.interface_list then
self:expand_interfaces(typ)

if self.collector then
for fname, ftype in fields_of(typ) do
self.env.reporter:add_field(typ, fname, ftype)
end
end
end

if fmacros then
Expand Down
Loading