mpeterv / luacheck

A tool for linting and static analysis of Lua code.
MIT License
1.92k stars 322 forks source link

Add higher level Lua interface that can load config #107

Open mpeterv opened 7 years ago

mpeterv commented 7 years ago

Currently functions in public luacheck module do not load config, which limits their usability when embedding luacheck in an editor with Lua interface (e.g. dit, far). Implement a new interface that uses config and possibly other features available from CLI but not current luacheck module: multithreading, caching.

mpeterv commented 6 years ago

This will be added in the next version (0.24.0), interface draft:

-- `options` may include `config`/`default_config` fields specifying how
-- the config should be loaded, and all the fields allowed in config
-- including `stds` and `files`.
local checker, err = luacheck.new_checker(options)

if not checker then
   -- Prints one of:
   -- `options_validation`, `config_location`,
   -- `config_load`, `config_validation`, `cache_read`, `cache_write`.
   print(err.type)
   -- Prints a readable message:
   print(err.message)
end

-- `inputs` - an array of tables.
-- Each table specifies an input:
-- * if `string` field is present, checks the string.
-- * if `path` field is present, checks file or directory with that path.
-- * if `file_handler` is present, reads the file handler with `:read("*a")`
--   and checks that.
-- Additional fields:
-- * `name`: file name used for applying per-path options from config instead of `path`.
-- * `label`: label used in formatted report for this input.
-- Returns report in the same format as current `luacheck.check_files`.
local report = checker:check(inputs)

-- Returns formatted report as a string.
local formatted_report = checker:format_report(report)
mpeterv commented 6 years ago

@Alloyed @johnd0e you may be interested in this