Skip to content

Commit 62068c0

Browse files
committed
Added crossfile testcase
Improved eq error message in test/code_action/init.lua Uses core.diagnostics.undefined-global
1 parent 82743b1 commit 62068c0

File tree

3 files changed

+82
-30
lines changed

3 files changed

+82
-30
lines changed

script/core/code-action.lua

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local converter = require 'proto.converter'
77
local autoreq = require 'core.completion.auto-require'
88
local rpath = require 'workspace.require-path'
99
local furi = require 'file-uri'
10+
local undefined = require 'core.diagnostics.undefined-global'
1011

1112
---@param uri uri
1213
---@param row integer
@@ -687,11 +688,7 @@ local function findRequireTargets(visiblePaths)
687688
return targets
688689
end
689690

690-
local function checkMissingRequire(results, uri, start, finish, diagnostics)
691-
if not diagnostics then
692-
return
693-
end
694-
691+
local function checkMissingRequire(results, uri, start, finish)
695692
local state = files.getState(uri)
696693
local text = files.getText(uri)
697694
if not state or not text then
@@ -705,14 +702,14 @@ local function checkMissingRequire(results, uri, start, finish, diagnostics)
705702
end
706703
end)
707704

708-
local function addPotentialRequires(global)
709-
autoreq.check(state, global.name, global.endpos, function(moduleFile, stemname, targetSource)
705+
local function addPotentialRequires(global, endpos)
706+
autoreq.check(state, global, endpos, function(moduleFile, stemname, targetSource)
710707
local visiblePaths = rpath.getVisiblePath(uri, furi.decode(moduleFile))
711708
if not visiblePaths or #visiblePaths == 0 then return end
712709

713710
for _, target in ipairs(findRequireTargets(visiblePaths)) do
714711
results[#results+1] = {
715-
title = lang.script('ACTION_AUTOREQUIRE', global.name, target),
712+
title = lang.script('ACTION_AUTOREQUIRE', global, target),
716713
kind = 'refactor.rewrite',
717714
command = {
718715
title = 'autoRequire',
@@ -721,7 +718,7 @@ local function checkMissingRequire(results, uri, start, finish, diagnostics)
721718
{
722719
uri = guide.getUri(state.ast),
723720
target = moduleFile,
724-
name = global.name,
721+
name = global,
725722
requireName = target
726723
},
727724
},
@@ -731,16 +728,11 @@ local function checkMissingRequire(results, uri, start, finish, diagnostics)
731728
end)
732729
end
733730

734-
-- TODO: Is there a better way to detect undefined-global?
735-
for _, diag in ipairs(diagnostics) do
736-
if diag.code == 'undefined-global' then
737-
for _, potglobal in ipairs(potentialGlobals) do
738-
if diag.message:find(potglobal.name) then
739-
addPotentialRequires(potglobal)
740-
end
741-
end
731+
undefined(uri, function(foundUndefined)
732+
if foundUndefined.start <= start and start <= foundUndefined.finish and foundUndefined.start <= finish and finish <= foundUndefined.finish then
733+
addPotentialRequires(foundUndefined.undefinedGlobal, foundUndefined.finish)
742734
end
743-
end
735+
end)
744736
end
745737

746738
return function (uri, start, finish, diagnostics)

script/core/diagnostics/undefined-global.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ return function (uri, callback)
5555
start = src.start,
5656
finish = src.finish,
5757
message = message,
58+
undefinedGlobal = src[1]
5859
}
5960
end)
6061
end

test/code_action/init.lua

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,40 @@ local core = require 'core.code-action'
22
local files = require 'files'
33
local lang = require 'language'
44
local catch = require 'catch'
5+
local furi = require 'file-uri'
56

67
rawset(_G, 'TEST', true)
78

89
local EXISTS = {}
910

10-
local function eq(a, b)
11-
if a == EXISTS and b ~= nil then
11+
local function eq(expected, result)
12+
if expected == EXISTS and result ~= nil then
1213
return true
1314
end
14-
if b == EXISTS and a ~= nil then
15+
if result == EXISTS and expected ~= nil then
1516
return true
1617
end
17-
local tp1, tp2 = type(a), type(b)
18+
local tp1, tp2 = type(expected), type(result)
1819
if tp1 ~= tp2 then
19-
return false
20+
return false, string.format(": expected type %s, got %s for %s", tp1, tp2)
2021
end
2122
if tp1 == 'table' then
2223
local mark = {}
23-
for k in pairs(a) do
24-
if not eq(a[k], b[k]) then
25-
return false
24+
for k in pairs(expected) do
25+
local ok, err = eq(expected[k], result[k])
26+
if not ok then
27+
return false, string.format(".%s%s", k, err)
2628
end
2729
mark[k] = true
2830
end
29-
for k in pairs(b) do
31+
for k in pairs(result) do
3032
if not mark[k] then
31-
return false
33+
return false, string.format(".%s: missing key in result", k)
3234
end
3335
end
3436
return true
3537
end
36-
return a == b
38+
return expected == result, string.format(": expected %s, got %s", expected, result)
3739
end
3840

3941
function TEST(script)
@@ -47,6 +49,32 @@ function TEST(script)
4749
end
4850
end
4951

52+
local function TEST_CROSSFILE(testfiles)
53+
local mainscript = table.remove(testfiles, 1)
54+
return function(expected)
55+
for _, data in ipairs(testfiles) do
56+
local uri = furi.encode(data.path)
57+
files.setText(uri, data.content)
58+
files.compileState(uri)
59+
end
60+
61+
local newScript, catched = catch(mainscript, '?')
62+
files.setText(TESTURI, newScript)
63+
files.compileState(TESTURI)
64+
65+
local _ <close> = function ()
66+
for _, info in ipairs(testfiles) do
67+
files.remove(furi.encode(info.path))
68+
end
69+
files.remove(TESTURI)
70+
end
71+
72+
local results = core(TESTURI, catched['?'][1][1], catched['?'][1][2])
73+
assert(results)
74+
assert(eq(expected, results))
75+
end
76+
end
77+
5078
TEST [[
5179
print(<?a?>, b, c)
5280
]]
@@ -155,4 +183,35 @@ local t = {
155183
-- },
156184
--}
157185

158-
-- TODO: Add some tests for ACTION_AUTOREQUIRE
186+
-- TODO: Add some tests for ACTION_AUTOREQUIRE
187+
188+
TEST_CROSSFILE {
189+
[[
190+
<?unrequiredModule?>.myFunction()
191+
]],
192+
{
193+
path = 'unrequiredModule.lua',
194+
content = [[
195+
local m = {}
196+
m.myFunction = print
197+
return m
198+
]]
199+
}
200+
} {
201+
{
202+
title = lang.script('ACTION_AUTOREQUIRE', 'unrequiredModule', 'unrequiredModule'),
203+
kind = 'refactor.rewrite',
204+
command = {
205+
title = 'autoRequire',
206+
command = 'lua.autoRequire',
207+
arguments = {
208+
{
209+
uri = TESTURI,
210+
target = furi.encode 'unrequiredModule.lua',
211+
name = 'unrequiredModule',
212+
requireName = 'unrequiredModule'
213+
},
214+
},
215+
}
216+
}
217+
}

0 commit comments

Comments
 (0)