1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
local helper = require "spec.helper"
local function assert_warnings(warnings, src)
assert.same(warnings, helper.get_stage_warnings("detect_globals", src))
end
describe("global detection", function()
it("detects global set", function()
assert_warnings({
{code = "111", name = "foo", line = 1, column = 1, end_column = 3, top = true}
}, [[
foo = {}
]])
end)
it("detects global set in nested functions", function()
assert_warnings({
{code = "111", name = "foo", line = 2, column = 4, end_column = 6}
}, [[
local function bar()
foo = {}
end
bar()
]])
end)
it("detects global access in multi-assignments", function()
assert_warnings({
{code = "111", name = "y", line = 2, column = 4, end_column = 4, top = true},
{code = "113", name = "print", line = 3, column = 1, end_column = 5}
}, [[
local x
x, y = 1
print(x)
]])
end)
it("detects global access in self swap", function()
assert_warnings({
{code = "113", name = "a", line = 1, column = 11, end_column = 11},
{code = "113", name = "print", line = 2, column = 1, end_column = 5}
}, [[
local a = a
print(a)
]])
end)
it("detects global mutation", function()
assert_warnings({
{code = "112", name = "a", indexing = {false}, line = 1, column = 1, end_column = 1}
}, [[
a[1] = 6
]])
end)
it("detects indirect global field access", function()
assert_warnings({
{
code = "113",
name = "b",
indexing = {false},
line = 2,
column = 15,
end_column = 15
}, {
code = "113",
name = "b",
indexing = {false, false, "foo"},
previous_indexing_len = 2,
line = 3,
column = 8,
end_column = 12,
indirect = true
}
}, [[
local c = "foo"
local alias = b[1]
return alias[2][c]
]])
end)
it("detects indirect global field mutation", function()
assert_warnings({
{
code = "113",
name = "b",
indexing = {false},
line = 2,
column = 15,
end_column = 15
}, {
code = "112",
name = "b",
indexing = {false, false, "foo"},
previous_indexing_len = 2,
line = 3,
column = 1,
end_column = 5,
indirect = true
}
}, [[
local c = "foo"
local alias = b[1]
alias[2][c] = c
]])
end)
it("provides indexing information for warnings related to global fields", function()
assert_warnings({
{
code = "113",
name = "global",
line = 2,
column = 11,
end_column = 16
}, {
code = "113",
name = "global",
indexing = {"foo", "bar", false},
indirect = true,
previous_indexing_len = 1,
line = 3,
column = 15,
end_column = 15
}, {
code = "113",
name = "global",
indexing = {"foo", "bar", false, true},
indirect = true,
previous_indexing_len = 4,
line = 5,
column = 8,
end_column = 13
}
}, [[
local c = "foo"
local g = global
local alias = g[c].bar[1]
local alias2 = alias
return alias2[...]
]])
end)
end)
|