load a lint config

This commit is contained in:
leaf corcoran 2013-12-27 15:30:21 -08:00
parent 53cf5dd649
commit db439f880a
2 changed files with 64 additions and 13 deletions

View File

@ -13,7 +13,7 @@ do
local _obj_0 = require("moonscript.compile") local _obj_0 = require("moonscript.compile")
Block = _obj_0.Block Block = _obj_0.Block
end end
local whitelist_globals = Set({ local default_whitelist = Set({
'loadstring', 'loadstring',
'select', 'select',
'_VERSION', '_VERSION',
@ -71,12 +71,12 @@ do
_base_0.__index = _base_0 _base_0.__index = _base_0
setmetatable(_base_0, _parent_0.__base) setmetatable(_base_0, _parent_0.__base)
local _class_0 = setmetatable({ local _class_0 = setmetatable({
__init = function(self, lint_errors, ...) __init = function(self, whitelist_globals, ...)
if lint_errors == nil then if whitelist_globals == nil then
lint_errors = { } whitelist_globals = default_whitelist
end end
self.lint_errors = lint_errors
_parent_0.__init(self, ...) _parent_0.__init(self, ...)
self.lint_errors = { }
local vc = self.value_compilers local vc = self.value_compilers
self.value_compilers = setmetatable({ self.value_compilers = setmetatable({
ref = function(block, val) ref = function(block, val)
@ -157,8 +157,35 @@ format_lint = function(errors, code, header)
end end
return table.concat(formatted, "\n\n") return table.concat(formatted, "\n\n")
end 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_code local lint_code
lint_code = function(code, name) lint_code = function(code, name, whitelist_globals)
if name == nil then if name == nil then
name = "string input" name = "string input"
end end
@ -167,7 +194,7 @@ lint_code = function(code, name)
if not (tree) then if not (tree) then
return nil, err return nil, err
end end
local scope = LinterBlock() local scope = LinterBlock(whitelist_globals)
scope:stms(tree) scope:stms(tree)
return format_lint(scope.lint_errors, code, name) return format_lint(scope.lint_errors, code, name)
end end
@ -177,7 +204,7 @@ lint_file = function(fname)
if not (f) then if not (f) then
return nil, err return nil, err
end end
return lint_code(f:read("*a"), fname) return lint_code(f:read("*a"), fname, whitelist_for_file(fname))
end end
return { return {
lint_code = lint_code, lint_code = lint_code,

View File

@ -4,7 +4,7 @@ import Set from require "moonscript.data"
import Block from require "moonscript.compile" import Block from require "moonscript.compile"
-- globals allowed to be referenced -- globals allowed to be referenced
whitelist_globals = Set { default_whitelist = Set {
'loadstring' 'loadstring'
'select' 'select'
'_VERSION' '_VERSION'
@ -49,8 +49,9 @@ whitelist_globals = Set {
} }
class LinterBlock extends Block class LinterBlock extends Block
new: (@lint_errors={}, ...) => new: (whitelist_globals=default_whitelist, ...) =>
super ... super ...
@lint_errors = {}
vc = @value_compilers vc = @value_compilers
@value_compilers = setmetatable { @value_compilers = setmetatable {
@ -94,19 +95,42 @@ format_lint = (errors, code, header) ->
table.concat formatted, "\n\n" table.concat formatted, "\n\n"
lint_code = (code, name="string input") -> -- {
-- whitelist_globals: {
-- ["some_file_pattern"]: {
-- "some_var", "another_var"
-- }
-- }
-- }
whitelist_for_file = do
local lint_config
(fname) ->
unless lint_config
lint_config = {}
pcall -> lint_config = require "lint_config"
return default_whitelist unless lint_config.whitelist_globals
final_list = {}
for pattern, list in pairs lint_config.whitelist_globals
if fname\match(pattern)
for item in *list
insert final_list, item
setmetatable Set(final_list), __index: default_whitelist
lint_code = (code, name="string input", whitelist_globals) ->
parse = require "moonscript.parse" parse = require "moonscript.parse"
tree, err = parse.string code tree, err = parse.string code
return nil, err unless tree return nil, err unless tree
scope = LinterBlock! scope = LinterBlock whitelist_globals
scope\stms tree scope\stms tree
format_lint scope.lint_errors, code, name format_lint scope.lint_errors, code, name
lint_file = (fname) -> lint_file = (fname) ->
f, err = io.open fname f, err = io.open fname
return nil, err unless f return nil, err unless f
lint_code f\read("*a"), fname lint_code f\read("*a"), fname, whitelist_for_file fname
{ :lint_code, :lint_file } { :lint_code, :lint_file }