From 6801d0d3d9e014246fad08dfd77ee4bac3b78c98 Mon Sep 17 00:00:00 2001 From: XeroOl Date: Sun, 7 Jul 2024 23:53:54 -0500 Subject: [PATCH] unknown module field lint works on definitions --- src/fennel-ls.fnl | 4 ++-- src/fennel-ls/compiler.fnl | 2 +- src/fennel-ls/handlers.fnl | 2 +- src/fennel-ls/lint.fnl | 25 +++++++++++++++++-------- test/lint.fnl | 3 ++- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/fennel-ls.fnl b/src/fennel-ls.fnl index 9c92aa8..4453418 100644 --- a/src/fennel-ls.fnl +++ b/src/fennel-ls.fnl @@ -2,7 +2,7 @@ (local dispatch (require :fennel-ls.dispatch)) (local json-rpc (require :fennel-ls.json-rpc)) -(λ check [filenames] +(λ lint [filenames] "non-interactive mode that gets executed from CLI with --lint. runs lints on each file, then formats and prints them" (local files (require :fennel-ls.files)) @@ -38,7 +38,7 @@ (λ main [] (case arg - ["--lint" & filenames] (check filenames) + ["--lint" & filenames] (lint filenames) (where (or ["--server"] [nil])) (main-loop (io.input) (io.output)) _args (do (io.stderr:write "USAGE: fennel-ls [--lint file] [--server]\n") diff --git a/src/fennel-ls/compiler.fnl b/src/fennel-ls/compiler.fnl index f4b13d3..b7f2222 100644 --- a/src/fennel-ls/compiler.fnl +++ b/src/fennel-ls/compiler.fnl @@ -61,7 +61,7 @@ identifiers are declared / referenced in which places." "Compile the file, and record all the useful information from the compiler into the file object" ;; The useful information being recorded: (let [definitions-by-scope (doto {} (setmetatable has-tables-mt)) - definitions {} ; symbol -> definition + definitions {} ; symbol -> binding diagnostics {} ; [diagnostic] references {} ; symbol -> references macro-refs {} ; symbol -> macro diff --git a/src/fennel-ls/handlers.fnl b/src/fennel-ls/handlers.fnl index 4c0930f..b23e06a 100644 --- a/src/fennel-ls/handlers.fnl +++ b/src/fennel-ls/handlers.fnl @@ -273,7 +273,7 @@ Every time the client sends a message, it gets handled by a function in the corr (send (message.diagnostics file)) (set file.open? true)) -(λ notifications.textDocument/didSave [server send {: uri}] +(λ notifications.textDocument/didSave [server _send {: uri}] (when (utils.endswith uri "flsproject.fnl") (config.reload server)) diff --git a/src/fennel-ls/lint.fnl b/src/fennel-ls/lint.fnl index 8c00783..80c4764 100644 --- a/src/fennel-ls/lint.fnl +++ b/src/fennel-ls/lint.fnl @@ -42,18 +42,27 @@ the `file.diagnostics` field, filling it with diagnostics." #[{:range (message.ast->range server file symbol) :newText (.. "_" (tostring symbol))}]))) +(fn module-field-helper [server file symbol ?ast stack] + "if ?ast is a module field that isn't known, return a diagnostic" + (let [opts {} + item (analyzer.search-ast server file ?ast stack opts)] + (if (and (not item) opts.searched-through-require-with-stack-size-1) + {:range (message.ast->range server file symbol) + :message (.. "unknown field: " (tostring symbol)) + :severity message.severity.WARN + :code 302 + :codeDescription "unknown-module-field"}))) + (λ unknown-module-field [server file] "any multisym whose definition can't be found through a (require) call" (icollect [symbol (pairs file.references) &into file.diagnostics] (if (. (utils.multi-sym-split symbol) 2) - (let [opts {} - item (analyzer.search-ast server file symbol [] opts)] - (if (and (not item) opts.searched-through-require-with-stack-size-1) - {:range (message.ast->range server file symbol) - :message (.. "unknown field: " (tostring symbol)) - :severity message.severity.WARN - :code 302 - :codeDescription "unknown-module-field"}))))) + (module-field-helper server file symbol symbol []))) + + (icollect [symbol binding (pairs file.definitions) &into file.diagnostics] + (if binding.keys + (module-field-helper server file symbol binding.definition (fcollect [i (length binding.keys) 1 -1] + (. binding.keys i)))))) (λ unnecessary-method [server file colon call] "a call to the : builtin that could just be a multisym" diff --git a/test/lint.fnl b/test/lint.fnl index ccda4a0..1d93ee3 100644 --- a/test/lint.fnl +++ b/test/lint.fnl @@ -92,7 +92,8 @@ :main.fnl "(local {: a : c &as guy} (require :the-guy-they-tell-you-not-to-worry-about)) (print guy.b guy.d)"} - [{:code 302 :message "unknown field: guy.d"}] + [{:code 302 :message "unknown field: c"} + {:code 302 :message "unknown field: guy.d"}] [{:code 302 :message "unknown field: a"} {:code 302 :message "unknown field: b"}]) (check "table.insert2 table.insert"