forked from mpeterv/luacheck
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a rule to detect useless computed property key
- Loading branch information
Showing
6 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
local helper = require "spec.helper" | ||
|
||
local function assert_warnings(warnings, src) | ||
assert.same(warnings, helper.get_stage_warnings("detect_useless_computed_key", src)) | ||
end | ||
|
||
describe("useless computed property key detection", function() | ||
it("does not detect anything wrong key is a keyword", function() | ||
assert_warnings({}, [[ | ||
aTable["and"] = 0 | ||
]]) | ||
end) | ||
|
||
it("does not detect anything wrong key is not a string", function() | ||
assert_warnings({}, [[ | ||
aTable[{}] = 0 | ||
]]) | ||
end) | ||
|
||
it("does not detect anything wrong key start with a number", function() | ||
assert_warnings({}, [[ | ||
aTable["1key"] = 0 | ||
]]) | ||
end) | ||
|
||
|
||
it("detects useless computed key in table creation", function() | ||
assert_warnings({ | ||
{code = "701", line = 2, column = 5, end_column = 11, name = "aKey1"}, | ||
}, [[ | ||
local aTable = { | ||
["aKey1"] = 0 | ||
} | ||
]]) | ||
end) | ||
|
||
it("detects useless computed key when affecting a value", function() | ||
assert_warnings({ | ||
{code = "701", line = 1, column = 8, end_column = 14, name = "aKey2"}, | ||
}, [[ | ||
aTable["aKey2"] = 0 | ||
]]) | ||
end) | ||
|
||
it("detects useless computed key when accessing a value", function() | ||
assert_warnings({ | ||
{code = "701", line = 1, column = 14, end_column = 20, name = "aKey3"}, | ||
}, [[ | ||
print(aTable["aKey3"]) | ||
]]) | ||
end) | ||
|
||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- warn, could be written simply with "aKey = 0" | ||
local aTable = { | ||
["aKey"] = 0 | ||
} | ||
|
||
-- warn, could be written simply with "aTable.aKey = 1" | ||
aTable["aKey"] = 1 | ||
|
||
-- no warn, "and" is a keyword | ||
aTable["and"] = 0 | ||
|
||
-- no warn, "1key" is not a valid name for key | ||
aTable["1key"] = 0 | ||
|
||
print(aTable) |
55 changes: 55 additions & 0 deletions
55
src/luacheck/stages/detect_function_call_witout_parentheses.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
local utils = require "luacheck.utils" | ||
|
||
local stage = {} | ||
|
||
local function useless_computed_key_message_format() | ||
return "It's unnecessary to use computed properties with literals such as {name!}" | ||
end | ||
|
||
stage.warnings = { | ||
["701"] = {message_format = useless_computed_key_message_format, | ||
fields = {"name"}} | ||
} | ||
|
||
local function warn_useless_computed_key(chstate, node, symbol) | ||
chstate:warn_range("701", node, { | ||
name = symbol, | ||
}) | ||
end | ||
|
||
local keywords = utils.array_to_set({ | ||
"and", "break", "do", "else", "elseif", "end", "false", "for", "function", "goto", "if", "in", | ||
"local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while"}) | ||
|
||
local function check_computed_key(chstate, key_node) | ||
if key_node.tag == "String" then | ||
local symbol = key_node[1] | ||
if (key_node.end_offset - key_node.offset + 1) > #symbol then | ||
if string.gmatch(symbol, "[%a_][%a%w_]*$")() == symbol and not keywords[symbol] then | ||
warn_useless_computed_key(chstate, key_node, symbol) | ||
end | ||
end | ||
end | ||
end | ||
|
||
local function check_nodes(chstate, nodes) | ||
for _, node in ipairs(nodes) do | ||
if type(node) == "table" then | ||
if node.tag == "Pair" then | ||
local key_node = node[1] | ||
check_computed_key(chstate, key_node) | ||
elseif node.tag == "Index" then | ||
local key_node = node[2] | ||
check_computed_key(chstate, key_node) | ||
end | ||
|
||
check_nodes(chstate, node) | ||
end | ||
end | ||
end | ||
|
||
function stage.run(chstate) | ||
check_nodes(chstate, chstate.ast) | ||
end | ||
|
||
return stage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
local utils = require "luacheck.utils" | ||
|
||
local stage = {} | ||
|
||
local function useless_computed_key_message_format() | ||
return "It's unnecessary to use computed properties with literals such as {name!}" | ||
end | ||
|
||
stage.warnings = { | ||
["701"] = {message_format = useless_computed_key_message_format, | ||
fields = {"name"}} | ||
} | ||
|
||
local function warn_useless_computed_key(chstate, node, symbol) | ||
chstate:warn_range("701", node, { | ||
name = symbol, | ||
}) | ||
end | ||
|
||
local keywords = utils.array_to_set({ | ||
"and", "break", "do", "else", "elseif", "end", "false", "for", "function", "goto", "if", "in", | ||
"local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while"}) | ||
|
||
local function check_computed_key(chstate, key_node) | ||
if key_node.tag == "String" then | ||
local symbol = key_node[1] | ||
if (key_node.end_offset - key_node.offset + 1) > #symbol then | ||
if string.gmatch(symbol, "[%a_][%a%w_]*$")() == symbol and not keywords[symbol] then | ||
warn_useless_computed_key(chstate, key_node, symbol) | ||
end | ||
end | ||
end | ||
end | ||
|
||
local function check_nodes(chstate, nodes) | ||
for _, node in ipairs(nodes) do | ||
if type(node) == "table" then | ||
if node.tag == "Pair" then | ||
local key_node = node[1] | ||
check_computed_key(chstate, key_node) | ||
elseif node.tag == "Index" then | ||
local key_node = node[2] | ||
check_computed_key(chstate, key_node) | ||
end | ||
|
||
check_nodes(chstate, node) | ||
end | ||
end | ||
end | ||
|
||
function stage.run(chstate) | ||
check_nodes(chstate, chstate.ast) | ||
end | ||
|
||
return stage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters