-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinder.go
83 lines (68 loc) · 1.78 KB
/
finder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"strings"
"mvdan.cc/sh/v3/syntax"
)
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") {
splitted := strings.Split(line, " ")
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
}
func find(code string) ([]string, error) {
r := strings.NewReader(code)
f, err := syntax.NewParser().Parse(r, "")
if err != nil {
return make([]string, 0), err
}
ignored, deps := get_ignored_and_deps(code)
// 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
syntax.Walk(f, func(node syntax.Node) bool {
switch x := node.(type) {
case *syntax.CallExpr:
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
for _, builtin := range ignored {
if dep == builtin {
is_builtin = true
}
}
if !is_builtin {
finished_deps = append(finished_deps, dep)
}
}
return finished_deps, nil
}