From 7f15f64e2239283d0b602929611cf28a3f2c26fa Mon Sep 17 00:00:00 2001 From: XeroOl Date: Sat, 28 Dec 2024 14:34:44 -0600 Subject: [PATCH] tset lint should only apply when rewriting as sym is valid --- src/fennel-ls/lint.fnl | 14 +++++++++----- test/lint.fnl | 5 +++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/fennel-ls/lint.fnl b/src/fennel-ls/lint.fnl index 21c55ad..71d591c 100644 --- a/src/fennel-ls/lint.fnl +++ b/src/fennel-ls/lint.fnl @@ -25,6 +25,10 @@ the `file.diagnostics` field, filling it with diagnostics." (. ops (tostring item)) item)) +(fn could-be-rewritten-as-sym? [str] + (and (= :string (type str)) + (not (str:find "[^!$%*+/0-9<=>?A-Z\\^_a-z|\128-\255-]")))) + (λ unused-definition [server file symbol definition] "local variable that is defined but not used" (if (not (or (= "_" (: (tostring symbol) :sub 1 1)) @@ -72,9 +76,7 @@ the `file.diagnostics` field, filling it with diagnostics." (sym? (. call 2)) (. file.lexical call)) (let [method (. call 3)] - (if (and (= :string (type method)) - (not (method:find "^[0-9]")) - (not (method:find "[^!$%*+-/0-9<=>?A-Z\\^_a-z|\128-\255]"))) + (if (could-be-rewritten-as-sym? method) {:range (message.ast->range server file call) :message (.. "unnecessary : call: use (" (tostring (. call 2)) ":" method ")") :severity message.severity.WARN @@ -82,8 +84,10 @@ the `file.diagnostics` field, filling it with diagnostics." :codeDescription "unnecessary-method"})))) (λ unnecessary-tset [server file head call] - (if (and (sym? head :tset) (sym? (. call 2)) - (= :string (type (. call 3))) (. file.lexical call)) + (if (and (sym? head :tset) + (sym? (. call 2)) + (could-be-rewritten-as-sym? (. call 3)) + (. file.lexical call)) (diagnostic {:range (message.ast->range server file call) :message (string.format "unnecessary %s" head) :severity message.severity.WARN diff --git a/test/lint.fnl b/test/lint.fnl index df41437..b82ebbc 100644 --- a/test/lint.fnl +++ b/test/lint.fnl @@ -177,6 +177,11 @@ :message "unnecessary tset" :range {:start {:character 15 :line 0} :end {:character 32 :line 0}}}]) + ;; Lint only triggers on keys that can be written as a sym + (check "(local tbl {}) (tset tbl \"hello-world\" 249)" [{:code 309}]) + (check "(local tbl {}) (tset tbl \"01234567\" 249)" [{:code 309}]) + (assert-ok "(local tbl {}) (tset tbl \"hello world\" 1)") + (assert-ok "(local tbl {}) (tset tbl \"0123.4567\" 1)") nil) (fn test-unnecessary-do []