-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathlint.lua
More file actions
426 lines (426 loc) · 11.7 KB
/
Copy pathlint.lua
File metadata and controls
426 lines (426 loc) · 11.7 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
local insert
insert = table.insert
local Set
Set = require("moonscript.data").Set
local Block
Block = require("moonscript.compile").Block
local ntype
ntype = require("moonscript.types").ntype
local mtype
mtype = require("moonscript.util").mtype
local default_whitelist = Set({
'_G',
'_VERSION',
'assert',
'bit32',
'collectgarbage',
'coroutine',
'debug',
'dofile',
'error',
'getfenv',
'getmetatable',
'io',
'ipairs',
'load',
'loadfile',
'loadstring',
'math',
'module',
'next',
'os',
'package',
'pairs',
'pcall',
'print',
'rawequal',
'rawget',
'rawlen',
'rawset',
'require',
'select',
'setfenv',
'setmetatable',
'string',
'table',
'tonumber',
'tostring',
'type',
'unpack',
'xpcall',
"nil",
"true",
"false"
})
local LINT_STAGES = {
"global_access",
"unused",
"constant_assign",
"import_overwrite"
}
local LinterBlock
do
local _class_0
local _parent_0 = Block
local _base_0 = {
lint_report = function(self, stage, msg, pos)
local root = self.root
if root.lint_stages and not root.lint_stages[stage] then
return
end
return insert(root.lint_errors, {
msg,
pos,
stage
})
end,
lint_mark_used = function(self, name)
if self.lint_unused_names and self.lint_unused_names[name] then
self.lint_unused_names[name] = false
return
end
if self.parent then
return self.parent:lint_mark_used(name)
end
end,
lint_check_unused = function(self)
if not (self.lint_unused_names and next(self.lint_unused_names)) then
return
end
local names_by_position = { }
for name, pos in pairs(self.lint_unused_names) do
local _continue_0 = false
repeat
if not (pos) then
_continue_0 = true
break
end
local _update_0 = pos
names_by_position[_update_0] = names_by_position[_update_0] or { }
insert(names_by_position[pos], name)
_continue_0 = true
until true
if not _continue_0 then
break
end
end
local tuples
do
local _accum_0 = { }
local _len_0 = 1
for pos, names in pairs(names_by_position) do
_accum_0[_len_0] = {
pos,
names
}
_len_0 = _len_0 + 1
end
tuples = _accum_0
end
table.sort(tuples, function(a, b)
return a[1] < b[1]
end)
for _index_0 = 1, #tuples do
local _des_0 = tuples[_index_0]
local pos, names
pos, names = _des_0[1], _des_0[2]
self.root:lint_report("unused", "assigned but unused " .. tostring(table.concat((function()
local _accum_0 = { }
local _len_0 = 1
for _index_1 = 1, #names do
local n = names[_index_1]
_accum_0[_len_0] = "`" .. tostring(n) .. "`"
_len_0 = _len_0 + 1
end
return _accum_0
end)(), ", ")), pos)
end
end,
render = function(self, ...)
self:lint_check_unused()
return _class_0.__parent.__base.render(self, ...)
end,
block = function(self, ...)
do
local child = _class_0.__parent.__base.block(self, ...)
child.block = self.block
child.render = self.render
child.lint_check_unused = self.lint_check_unused
child.lint_mark_used = self.lint_mark_used
child.value_compilers = self.value_compilers
child.statement_compilers = self.statement_compilers
child.lint_wrap_transform = self.lint_wrap_transform
self:lint_wrap_transform(child)
return child
end
end
}
_base_0.__index = _base_0
setmetatable(_base_0, _parent_0.__base)
_class_0 = setmetatable({
__init = function(self, whitelist_globals, stages, ...)
if whitelist_globals == nil then
whitelist_globals = default_whitelist
end
_class_0.__parent.__init(self, ...)
self.lint_errors = { }
if stages then
self.lint_stages = Set(stages)
end
self.lint_wrap_transform = function(self, block)
local inner = block.transform.statement
local checked_imports = setmetatable({ }, {
__mode = "k"
})
block.transform.statement = function(node, ...)
local import_node = node
while ntype(import_node) == "transform" do
import_node = import_node[2]
end
if ntype(import_node) == "import" and not checked_imports[import_node] then
checked_imports[import_node] = true
local _list_0 = import_node[2]
for _index_0 = 1, #_list_0 do
local name = _list_0[_index_0]
if ntype(name) == "colon" then
name = name[2]
end
local binding = block:binding_value(name)
if not (type(binding) == "table" and binding.const) then
if binding then
block.root:lint_report("import_overwrite", "import overwrites existing binding `" .. tostring(name) .. "`", import_node[-1])
end
end
end
end
return inner(node, ...)
end
return block
end
self:lint_wrap_transform(self)
local vc = self.value_compilers
self.value_compilers = setmetatable({
ref = function(block, val)
local name = val[2]
if not (block:has_name(name) or whitelist_globals[name] or name:match("%.")) then
self:lint_report("global_access", "accessing global `" .. tostring(name) .. "`", val[-1])
end
block:lint_mark_used(name)
return vc.ref(block, val)
end
}, {
__index = vc
})
local sc = self.statement_compilers
self.statement_compilers = setmetatable({
assign = function(block, node)
local names = node[2]
for _index_0 = 1, #names do
local _continue_0 = false
repeat
local name = names[_index_0]
if type(name) == "table" and name[1] == "temp_name" then
_continue_0 = true
break
end
local const_name
if type(name) == "string" then
const_name = name
elseif name[1] == "ref" then
const_name = name[2]
end
if const_name then
local binding = block:binding_value(const_name)
if type(binding) == "table" and binding.const then
self:lint_report("constant_assign", "assigning to constant `" .. tostring(const_name) .. "`", type(name) == "table" and name[-1] or node[-1] or block.root.last_pos)
end
end
local real_name, is_local = block:extract_assign_name(name)
if not (is_local or real_name and not block:has_name(real_name, true)) then
_continue_0 = true
break
end
if real_name == "_" then
_continue_0 = true
break
end
block.lint_unused_names = block.lint_unused_names or { }
block.lint_unused_names[real_name] = node[-1] or 0
_continue_0 = true
until true
if not _continue_0 then
break
end
end
return sc.assign(block, node)
end
}, {
__index = sc
})
end,
__base = _base_0,
__name = "LinterBlock",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil then
local parent = rawget(cls, "__parent")
if parent then
return parent[name]
end
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
LinterBlock = _class_0
end
local format_lint_compact
format_lint_compact = function(errors, code, header)
if not (next(errors)) then
return nil
end
local pos_to_line_col
pos_to_line_col = require("moonscript.util").pos_to_line_col
local formatted
do
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #errors do
local _des_0 = errors[_index_0]
local msg, pos, stage
msg, pos, stage = _des_0[1], _des_0[2], _des_0[3]
local location
if pos then
local line, col = pos_to_line_col(code, pos)
location = tostring(header) .. ":" .. tostring(line) .. ":" .. tostring(col)
else
location = header
end
local stage_suffix = stage and " [" .. tostring(stage) .. "]" or ""
local _value_0 = tostring(location) .. ": " .. tostring(msg) .. tostring(stage_suffix)
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
formatted = _accum_0
end
return table.concat(formatted, "\n")
end
local format_lint
format_lint = function(errors, code, header)
if not (next(errors)) then
return
end
local pos_to_line, get_line
do
local _obj_0 = require("moonscript.util")
pos_to_line, get_line = _obj_0.pos_to_line, _obj_0.get_line
end
local formatted
do
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #errors do
local _des_0 = errors[_index_0]
local msg, pos
msg, pos = _des_0[1], _des_0[2]
if pos then
local line = pos_to_line(code, pos)
msg = "line " .. tostring(line) .. ": " .. tostring(msg)
local line_text = "> " .. get_line(code, line)
local sep_len = math.max(#msg, #line_text)
_accum_0[_len_0] = table.concat({
msg,
("="):rep(sep_len),
line_text
}, "\n")
else
_accum_0[_len_0] = msg
end
_len_0 = _len_0 + 1
end
formatted = _accum_0
end
if header then
table.insert(formatted, 1, header)
end
return table.concat(formatted, "\n\n")
end
local whitelist_for_file
do
local lint_config
whitelist_for_file = function(fname)
if not (lint_config) then
lint_config = { }
pcall(function()
lint_config = require("lint_config")
end)
end
if not (lint_config.whitelist_globals) then
return default_whitelist
end
local final_list = { }
for pattern, list in pairs(lint_config.whitelist_globals) do
if fname:match(pattern) then
for _index_0 = 1, #list do
local item = list[_index_0]
insert(final_list, item)
end
end
end
return setmetatable(Set(final_list), {
__index = default_whitelist
})
end
end
local LINT_FORMATS = {
"default",
"compact"
}
local formatters = {
default = format_lint,
compact = format_lint_compact
}
local lint_code
lint_code = function(code, name, whitelist_globals, opts)
if name == nil then
name = "string input"
end
local parse = require("moonscript.parse")
local tree, err = parse.string(code)
if not (tree) then
return nil, err
end
local scope = LinterBlock(whitelist_globals, opts and opts.stages)
scope:stms(tree)
scope:lint_check_unused()
local formatter = formatters[opts and opts.format or "default"]
if not (formatter) then
error("unknown lint format: " .. tostring(opts.format))
end
return formatter(scope.lint_errors, code, name)
end
local lint_file
lint_file = function(fname, opts)
local f, err = io.open(fname)
if not (f) then
return nil, err
end
return lint_code(f:read("*a"), fname, whitelist_for_file(fname), opts)
end
return {
lint_code = lint_code,
lint_file = lint_file,
LINT_STAGES = LINT_STAGES,
LINT_FORMATS = LINT_FORMATS
}