From 5d2b8665183de74bb4b23ee4e7d6c5a05e854299 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Mon, 7 Sep 2020 12:07:17 +0200 Subject: ci: check capture names in queries --- scripts/check-queries.lua | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'scripts/check-queries.lua') diff --git a/scripts/check-queries.lua b/scripts/check-queries.lua index f7e282c37..aee0089e7 100755 --- a/scripts/check-queries.lua +++ b/scripts/check-queries.lua @@ -1,13 +1,47 @@ -- Execute as `nvim --headless -c "luafile ./scripts/check-queries.lua"` + +local function extract_captures() + local lines = vim.fn.readfile("CONTRIBUTING.md") + local captures = {} + local current_query + + for _, line in ipairs(lines) do + if vim.startswith(line, "### ") then + current_query = vim.fn.tolower(line:sub(5)) + elseif vim.startswith(line, "@") and current_query then + if not captures[current_query] then + captures[current_query] = {} + end + + table.insert(captures[current_query], vim.split(line:sub(2), " ", true)[1]) + end + end + + return captures +end + local function do_check() local parsers = require 'nvim-treesitter.parsers'.available_parsers() local queries = require 'nvim-treesitter.query' local query_types = queries.built_in_query_groups + local captures = extract_captures() + for _, lang in pairs(parsers) do for _, query_type in pairs(query_types) do print('Checking '..lang..' '..query_type) - queries.get_query(lang, query_type) + local query = queries.get_query(lang, query_type) + + if query then + for _, capture in ipairs(query.captures) do + if not vim.startswith(capture, "_") -- We ignore things like _helper + and captures[query_type] + and not capture:find("^[A-Z]") -- Highlight groups + and not vim.tbl_contains(captures[query_type], capture) then + error(string.format("Invalid capture @%s in %s for %s.", capture, query_type, lang)) + end + end + end end end end -- cgit v1.2.3-70-g09d2