From 35ed194361f672a1469dcf27a97830fdd9098d02 Mon Sep 17 00:00:00 2001 From: b1ek Date: Sat, 7 Sep 2024 16:58:54 +1000 Subject: [PATCH] feat: ignore next line keyword --- README.md | 4 ++++ finder.go | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bba959f..93f1b65 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ tags # You can add/ignore multiple commands at once: #bshchk:add-cmd curl wget + +# To ignore next line: +#bshchk:ignore-next-line +curl # won't be checked ``` diff --git a/finder.go b/finder.go index 64b17cd..d865d45 100644 --- a/finder.go +++ b/finder.go @@ -6,13 +6,22 @@ import ( "mvdan.cc/sh/v3/syntax" ) +var ignored_lines = []uint{} + func get_ignored_and_deps(code string) ([]string, []string) { // source: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html var ignored []string = []string{"alias", "bind", "builtin", "caller", "command", "declare", "echo", "enable", "let", "local", "logout", "mapfile", "printf", "read", "readarray", "source", "type", "typeset", "ulimit", "unalias"} var deps []string - for _, line := range strings.Split(code, "\n") { + for i, line := range strings.Split(code, "\n") { splitted := strings.Split(line, " ") + + // comments w/ no arguments + if splitted[0] == "#bshchk:ignore-next-line" { + ignored_lines = append(ignored_lines, uint(i)+1) + } + + // comments with arguments if len(splitted) < 2 { continue } @@ -27,6 +36,15 @@ func get_ignored_and_deps(code string) ([]string, []string) { return ignored, deps } +func is_line_ignored(line uint) bool { + for _, ignored := range ignored_lines { + if line == ignored { + return true + } + } + return false +} + func find(code string) ([]string, error) { r := strings.NewReader(code) f, err := syntax.NewParser().Parse(r, "") @@ -50,6 +68,9 @@ func find(code string) ([]string, error) { syntax.Walk(f, func(node syntax.Node) bool { switch x := node.(type) { case *syntax.CallExpr: + if is_line_ignored(node.Pos().Line() - 1) { + return true + } for i := range x.Args { for _, part := range x.Args[i].Parts { switch xx := part.(type) {