|
| 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