Skip to content

Commit c16ff36

Browse files
committed
only mark leaves of tables as covered
fixes function bodies within tables getting marked as being covered
1 parent 027ae09 commit c16ff36

2 files changed

Lines changed: 39 additions & 34 deletions

File tree

test/helpers/coverage.lua

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ function coverage.Preprocess(code, key)
1010

1111
local function inject_call_expression(parser, node, start, stop)
1212
if node.environment == "typesystem" or node.type_call then return node end
13-
14-
13+
1514
if node.kind == "function" then
1615
-- don't mark the funciton body as being called
1716
start, stop = node.tokens["function"].start, node.tokens["function"].stop
1817
end
19-
18+
if node.kind == "table" then
19+
-- don't mark the funciton body as being called
20+
start, stop = node.tokens["{"].start, node.tokens["{"].stop
21+
end
22+
2023
local call_expression = parser:ParseString(" " .. FUNC_NAME .. "(" .. start .. "," .. stop .. ",x)").statements[1].value
2124

2225
if node.kind == "postfix_call" and not node.tokens["call("] then
@@ -62,7 +65,7 @@ function coverage.Preprocess(code, key)
6265
node:GetStatement().kind == "function" or
6366
(
6467
node.kind == "binary_operator" and
65-
node.value.value == ":"
68+
node.value.value == ":"
6669
)
6770
or
6871
(
@@ -161,34 +164,35 @@ local function normalizeRanges(ranges)
161164
end
162165

163166
local function normalizeRanges(ranges)
164-
local map = {}
165-
local minVal = math.huge
166-
local maxVal = -math.huge
167-
168-
for _, range in ipairs(ranges) do
169-
local start, finish, count = range[1], range[2], range[3]
170-
minVal = math.min(minVal, start)
171-
maxVal = math.max(maxVal, finish)
172-
173-
for i = start, finish do
174-
map[i] = count
175-
end
176-
end
177-
178-
local result = {}
179-
local currentStart = minVal
180-
local currentCount = map[minVal] or 0
181-
182-
for i = minVal + 1, maxVal + 1 do
183-
local nextCount = map[i] or 0
184-
if nextCount ~= currentCount then
185-
table.insert(result, {currentStart, i - 1, currentCount})
186-
currentStart = i
187-
currentCount = nextCount
188-
end
189-
end
190-
191-
return result
167+
local map = {}
168+
local minVal = math.huge
169+
local maxVal = -math.huge
170+
171+
for _, range in ipairs(ranges) do
172+
local start, finish, count = range[1], range[2], range[3]
173+
minVal = math.min(minVal, start)
174+
maxVal = math.max(maxVal, finish)
175+
176+
for i = start, finish do
177+
map[i] = count
178+
end
179+
end
180+
181+
local result = {}
182+
local currentStart = minVal
183+
local currentCount = map[minVal] or 0
184+
185+
for i = minVal + 1, maxVal + 1 do
186+
local nextCount = map[i] or 0
187+
188+
if nextCount ~= currentCount then
189+
table.insert(result, {currentStart, i - 1, currentCount})
190+
currentStart = i
191+
currentCount = nextCount
192+
end
193+
end
194+
195+
return result
192196
end
193197

194198
function coverage.Collect(key)

test/tests/coverage.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ equal(annotate("local x = 1"), "1")
297297
equal(annotate("do return end"), "return")
298298
equal(
299299
annotate("local x = {foo={bar={baz=true}}} local y = x.foo.bar.baz"),
300-
"{foo={bar={baz=true}}}x.foo.bar.baz"
300+
"{{{truex.foo.bar.baz"
301301
)
302302
equal(
303303
annotate("if true then local x = 1 else local y = 1 end"),
@@ -308,4 +308,5 @@ equal(
308308
equal(get_count_string("local x = 1"), "1")
309309
equal(get_count_string("local x = package.loaded.table"), "11111111111111111111")
310310
equal(get_count_string("for i = 1+0, 8+0 do local x = 1 if i > 5 then local y = i end if i > 15 then local y = 1 end local x = 1 end"), "111001110000000000000080000888880000000000000000300000000888888000000000000000000000000000000008")
311-
equal(get_count_string("local x = function() if false then local x = true end local x = 1 end x()"), "111111110000001111100000000000000000000000000000000000100000111")
311+
equal(get_count_string("local x = function() if false then local x = true end local x = 1 end x()"), "111111110000001111100000000000000000000000000000000000100000111")
312+
equal(get_count_string("local x = {x = function() local y = 1 end}"), "1000011111111")

0 commit comments

Comments
 (0)