Skip to content

Commit

Permalink
Pass state while walking node tree in unwrap_parens
Browse files Browse the repository at this point in the history
Trying to stash the state outside of the function and access it from any
other scope creates problems for multi-threaded runs. Passing it around
in the function calls keeps it local to the current scope even when/if
more than one may be running.
  • Loading branch information
alerque committed Mar 24, 2022
1 parent d752ed3 commit bfbc70d
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/luacheck/stages/unwrap_parens.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ local replacements = {
le = ">",
}

local chstate

-- Mutates an array of nodes and non-tables, unwrapping Paren nodes.
-- If list_start is given, tail Paren is not unwrapped if it's unpacking and past list_start index.
local function handle_nodes(nodes, list_start)
local function handle_nodes(chstate, nodes, list_start)
local num_nodes = #nodes

for index = 1, num_nodes do
Expand All @@ -40,21 +38,21 @@ local function handle_nodes(nodes, list_start)
local tag = node.tag

if tag == "Table" or tag == "Return" then
handle_nodes(node, 1)
handle_nodes(chstate, node, 1)
elseif tag == "Call" then
handle_nodes(node, 2)
handle_nodes(chstate, node, 2)
elseif tag == "Invoke" then
handle_nodes(node, 3)
handle_nodes(chstate, node, 3)
elseif tag == "Forin" then
handle_nodes(node[2], 1)
handle_nodes(node[3])
handle_nodes(chstate, node[2], 1)
handle_nodes(chstate, node[3])
elseif tag == "Local" then
if node[2] then
handle_nodes(node[2])
handle_nodes(chstate, node[2])
end
elseif tag == "Set" then
handle_nodes(node[1])
handle_nodes(node[2], 1)
handle_nodes(chstate, node[1])
handle_nodes(chstate, node[2], 1)
else
-- warn that not x == y means (not x) == y
if tag ~= "Paren"
Expand All @@ -66,11 +64,11 @@ local function handle_nodes(nodes, list_start)
chstate:warn_range("582", node[1])
end

handle_nodes(node)
handle_nodes(chstate, node)

-- warn that not (x == y) can become x ~= y
if tag == "Op" and node[1] == "not" and node[2].tag == "Op" and relational_operators[node[2][1]] then
chstate:warn_range("581", node, {
chstate:warn_range("581", node, {
operator = relational_operators[node[2][1]],
replacement_operator = replacements[node[2][1]]
})
Expand All @@ -91,9 +89,8 @@ end
-- Mutates AST, unwrapping Paren nodes.
-- Paren nodes are preserved only when they matter:
-- at the ends of expression lists with potentially multi-value inner expressions.
function stage.run(check_state)
chstate = check_state
handle_nodes(check_state.ast)
function stage.run(chstate)
handle_nodes(chstate, chstate.ast)
end

return stage

0 comments on commit bfbc70d

Please sign in to comment.