Skip to content

Commit d608a29

Browse files
committed
Add a source module for whitespace checking
1 parent dc905f4 commit d608a29

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

install.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ for _, filename in ipairs {
117117
"fs.lua",
118118
"globbing.lua",
119119
"utils.lua",
120-
"argparse.lua"} do
120+
"argparse.lua",
121+
"whitespace.lua"} do
121122
copy("src" .. dirsep .. "luacheck" .. dirsep .. filename, luacheck_lib_dir)
122123
end
123124

luacheck-scm-1.rockspec

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ build = {
4040
["luacheck.fs"] = "src/luacheck/fs.lua",
4141
["luacheck.globbing"] = "src/luacheck/globbing.lua",
4242
["luacheck.utils"] = "src/luacheck/utils.lua",
43-
["luacheck.argparse"] = "src/luacheck/argparse.lua"
43+
["luacheck.argparse"] = "src/luacheck/argparse.lua",
44+
["luacheck.whitespace"] = "src/luacheck/whitespace.lua"
4445
},
4546
install = {
4647
bin = {

src/luacheck/whitespace.lua

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
local utils = require "luacheck.utils"
2+
3+
local function check_whitespace(chstate, src --[[, code_lines]])
4+
for lineno, line in ipairs(utils.split(src, "\n")) do
5+
line = string.gsub(line, "\r$", "") -- strip any remaining CR line ending
6+
if line ~= "" then
7+
local from, to = line:find("%s+$")
8+
if from then
9+
if from == 1 then
10+
-- line contains only whitespace (thus never considered "code")
11+
chstate:warn({code = "611",
12+
line = lineno, column = from, end_column = to})
13+
--[[ (This needs to be reworked.)
14+
elseif code_lines[lineno] then
15+
-- trailing whitespace on code line
16+
chstate:warn({code = "612",
17+
line = lineno, column = from, end_column = to})
18+
else
19+
-- trailing whitespace on non-code line
20+
chstate:warn({code = "613",
21+
line = lineno, column = from, end_column = to})
22+
end
23+
--]]
24+
else
25+
-- line contains trailing whitespace
26+
chstate:warn({code = "612",
27+
line = lineno, column = from, end_column = to})
28+
end
29+
else
30+
from, to = line:find("^%s+")
31+
if from and string.find(line:sub(1, to), " \t", 1, true) then
32+
-- inconsistent leading whitespace (SPACE followed by TAB)
33+
chstate:warn({code = "621",
34+
line = lineno, column = from, end_column = to})
35+
end
36+
end
37+
end
38+
end
39+
end
40+
41+
return check_whitespace

0 commit comments

Comments
 (0)