/usr/share/lua/5.1/luacheck/check.lua is in lua-check 0.17.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | local parse = require "luacheck.parser"
local linearize = require "luacheck.linearize"
local analyze = require "luacheck.analyze"
local reachability = require "luacheck.reachability"
local handle_inline_options = require "luacheck.inline_options"
local core_utils = require "luacheck.core_utils"
local utils = require "luacheck.utils"
local check_whitespace = require "luacheck.whitespace"
local function is_secondary(value)
return value.secondaries and value.secondaries.used
end
local ChState = utils.class()
function ChState:__init()
self.warnings = {}
end
function ChState:warn(warning, implicit_self)
if not warning.end_column then
warning.end_column = implicit_self and warning.column or (warning.column + #warning.name - 1)
end
table.insert(self.warnings, warning)
end
local action_codes = {
set = "1",
mutate = "2",
access = "3"
}
local type_codes = {
var = "1",
func = "1",
arg = "2",
loop = "3",
loopi = "3"
}
function ChState:warn_global(node, action, is_top)
self:warn({
code = "11" .. action_codes[action],
name = node[1],
line = node.location.line,
column = node.location.column,
top = is_top and (action == "set") or nil
})
end
-- W12* (read-only global) and W131 (unused global) are patched in during filtering.
function ChState:warn_unused_variable(value, recursive, self_recursive, useless)
self:warn({
code = "21" .. type_codes[value.var.type],
name = value.var.name,
line = value.location.line,
column = value.location.column,
secondary = is_secondary(value) or nil,
func = (value.type == "func") or nil,
mutually_recursive = not self_recursive and recursive or nil,
recursive = self_recursive,
self = value.var.self,
useless = value.var.name == "_" and useless or nil
}, value.var.self)
end
function ChState:warn_unset(var)
self:warn({
code = "221",
name = var.name,
line = var.location.line,
column = var.location.column
})
end
function ChState:warn_unaccessed(var, mutated)
-- Mark as secondary if all assigned values are secondary.
-- It is guaranteed that there are at least two values.
local secondary = true
for _, value in ipairs(var.values) do
if not value.empty and not is_secondary(value) then
secondary = nil
break
end
end
self:warn({
code = "2" .. (mutated and "4" or "3") .. type_codes[var.type],
name = var.name,
line = var.location.line,
column = var.location.column,
secondary = secondary
}, var.self)
end
function ChState:warn_unused_value(value, mutated)
self:warn({
code = "3" .. (mutated and "3" or "1") .. type_codes[value.type],
name = value.var.name,
line = value.location.line,
column = value.location.column,
secondary = is_secondary(value) or nil,
}, value.type == "arg" and value.var.self)
end
function ChState:warn_unused_field_value(node)
self:warn({
code = "314",
field = node.field,
index = node.is_index,
line = node.location.line,
column = node.location.column,
end_column = node.location.column + #node.first_token - 1
})
end
function ChState:warn_uninit(node, mutation)
self:warn({
code = mutation and "341" or "321",
name = node[1],
line = node.location.line,
column = node.location.column
})
end
function ChState:warn_redefined(var, prev_var, same_scope)
if var.name ~= "..." then
self:warn({
code = "4" .. (same_scope and "1" or (var.line == prev_var.line and "2" or "3")) .. type_codes[prev_var.type],
name = var.name,
line = var.location.line,
column = var.location.column,
self = var.self and prev_var.self,
prev_line = prev_var.location.line,
prev_column = prev_var.location.column
}, var.self)
end
end
function ChState:warn_unreachable(location, unrepeatable, token)
self:warn({
code = "51" .. (unrepeatable and "2" or "1"),
line = location.line,
column = location.column,
end_column = location.column + #token - 1
})
end
function ChState:warn_unused_label(label)
self:warn({
code = "521",
label = label.name,
line = label.location.line,
column = label.location.column,
end_column = label.end_column
})
end
function ChState:warn_unbalanced(location, shorter_lhs)
-- Location points to `=`.
self:warn({
code = "53" .. (shorter_lhs and "1" or "2"),
line = location.line,
column = location.column,
end_column = location.column
})
end
function ChState:warn_empty_block(location, do_end)
-- Location points to `do`, `then` or `else`.
self:warn({
code = "54" .. (do_end and "1" or "2"),
line = location.line,
column = location.column,
end_column = location.column + (do_end and 1 or 3)
})
end
function ChState:warn_empty_statement(location)
self:warn({
code = "551",
line = location.line,
column = location.column,
end_column = location.column
})
end
local function check_or_throw(src)
local ast, comments, code_lines, semicolons = parse(src)
local chstate = ChState()
local line = linearize(chstate, ast)
for _, location in ipairs(semicolons) do
chstate:warn_empty_statement(location)
end
check_whitespace(chstate, src)
analyze(chstate, line)
reachability(chstate, line)
handle_inline_options(ast, comments, code_lines, chstate.warnings)
core_utils.sort_by_location(chstate.warnings)
return chstate.warnings
end
--- Checks source.
-- Returns an array of warnings and errors. Codes for errors start with "0".
-- Syntax errors (with code "011") have message stored in .msg field.
local function check(src)
local warnings, err = utils.pcall(check_or_throw, src)
if warnings then
return warnings
else
local syntax_error = {
code = "011",
line = err.line,
column = err.column,
end_column = err.end_column,
msg = err.msg
}
return {syntax_error}
end
end
return check
|