-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependencies.go
43 lines (35 loc) · 954 Bytes
/
dependencies.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
package main
import (
"bytes"
"regexp"
"strings"
"unsafe"
"github.com/dlw93/cvbuild/util"
)
type DependencyScanner struct {
text []byte
}
var re *regexp.Regexp
func init() {
res := util.Map([]rune{'\'', '"', '`'}, func(q rune) string {
s := string(q)
return s + "[^" + s + "]*" + s
})
re = regexp.MustCompile(`(?m)\bnew\s+URL\s*\(\s*(` + strings.Join(res, "|") + `)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)`)
}
func NewDependencyScanner(text []byte) *DependencyScanner {
return &DependencyScanner{text}
}
func (s *DependencyScanner) String() string {
return unsafe.String(&s.text[0], len(s.text))
}
func (s *DependencyScanner) Scan() []string {
if !bytes.Contains(s.text, []byte("import.meta.url")) {
return nil
}
matches := re.FindAllSubmatchIndex(s.text, -1)
return util.Map(matches, func(match []int) string {
beg, end := match[2]+1, match[3]-1 // +1/-1 to remove quotes
return unsafe.String(&s.text[beg], end-beg)
})
}