Skip to content

Commit

Permalink
fix: make get_types report all inherited interface fields (#857)
Browse files Browse the repository at this point in the history
Fixes #852.
  • Loading branch information
hishamhm authored Nov 16, 2024
1 parent 7e1bc90 commit 6e54e66
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
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

0 comments on commit 6e54e66

Please sign in to comment.