bshchk/finder.go

105 lines
2.2 KiB
Go
Raw Normal View History

2024-06-02 11:32:17 +02:00
package main
import (
"strings"
"mvdan.cc/sh/v3/syntax"
)
2024-09-07 08:58:54 +02:00
var ignored_lines = []uint{}
2024-06-02 13:56:48 +02:00
func get_ignored_and_deps(code string) ([]string, []string) {
2024-06-13 12:51:01 +02:00
// source: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html
2024-06-02 13:56:48 +02:00
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
2024-09-07 08:58:54 +02:00
for i, line := range strings.Split(code, "\n") {
2024-06-02 13:56:48 +02:00
splitted := strings.Split(line, " ")
2024-09-07 08:58:54 +02:00
// comments w/ no arguments
if splitted[0] == "#bshchk:ignore-next-line" {
ignored_lines = append(ignored_lines, uint(i)+1)
}
// comments with arguments
2024-06-02 13:56:48 +02:00
if len(splitted) < 2 {
continue
}
if splitted[0] == "#bshchk:ignore-cmd" {
ignored = append(ignored, splitted[1:]...)
}
if splitted[0] == "#bshchk:add-cmd" {
deps = append(deps, splitted[1:]...)
}
}
return ignored, deps
}
2024-09-07 08:58:54 +02:00
func is_line_ignored(line uint) bool {
for _, ignored := range ignored_lines {
if line == ignored {
return true
}
}
return false
}
2024-06-02 11:32:17 +02:00
func find(code string) ([]string, error) {
r := strings.NewReader(code)
f, err := syntax.NewParser().Parse(r, "")
if err != nil {
return make([]string, 0), err
}
2024-06-02 13:56:48 +02:00
ignored, deps := get_ignored_and_deps(code)
2024-06-07 14:25:37 +02:00
// 1. find function declarations
syntax.Walk(f, func(node syntax.Node) bool {
switch x := node.(type) {
case *syntax.FuncDecl:
ignored = append(ignored, x.Name.Value)
}
return true
})
// 2. collect all commands
2024-06-02 11:32:17 +02:00
syntax.Walk(f, func(node syntax.Node) bool {
switch x := node.(type) {
case *syntax.CallExpr:
2024-09-07 08:58:54 +02:00
if is_line_ignored(node.Pos().Line() - 1) {
return true
}
2024-06-02 11:32:17 +02:00
for i := range x.Args {
for _, part := range x.Args[i].Parts {
switch xx := part.(type) {
case *syntax.Lit:
deps = append(deps, xx.Value)
}
return true
}
}
}
return true
})
var finished_deps []string
for _, dep := range deps {
is_builtin := false
2024-06-02 13:56:48 +02:00
for _, builtin := range ignored {
2024-06-02 11:32:17 +02:00
if dep == builtin {
is_builtin = true
}
}
if !is_builtin {
finished_deps = append(finished_deps, dep)
}
}
return finished_deps, nil
}