Made detector.detect unexported, updated docstrings

This commit is contained in:
Giuseppe Guerra
2023-06-16 12:01:41 +02:00
parent 97121bdede
commit 2a648b7ed7
4 changed files with 18 additions and 17 deletions
@@ -14,10 +14,10 @@ var (
_ detectorsProvider = sequenceDetectorsProvider{}
)
// detector implements a check to see if a plugin uses Angular.
// detector implements a check to see if a js file is using angular APIs.
type detector interface {
// Detect takes the content of a moduleJs file and returns true if the plugin is using Angular.
Detect(moduleJs []byte) bool
// detect takes the content of a js file and returns true if the plugin is using Angular.
detect(js []byte) bool
}
// containsBytesDetector is a detector that returns true if module.js contains the "pattern" string.
@@ -25,8 +25,8 @@ type containsBytesDetector struct {
pattern []byte
}
// Detect returns true if moduleJs contains the byte slice d.pattern.
func (d *containsBytesDetector) Detect(moduleJs []byte) bool {
// detect returns true if moduleJs contains the byte slice d.pattern.
func (d *containsBytesDetector) detect(moduleJs []byte) bool {
return bytes.Contains(moduleJs, d.pattern)
}
@@ -36,16 +36,17 @@ type regexDetector struct {
}
// Detect returns true if moduleJs matches the regular expression d.regex.
func (d *regexDetector) Detect(moduleJs []byte) bool {
func (d *regexDetector) detect(moduleJs []byte) bool {
return d.regex.Match(moduleJs)
}
// detectorsProvider returns a slice of detectors.
// detectorsProvider can provide multiple detectors used for Angular detection.
type detectorsProvider interface {
// provideDetectors returns a slice of detectors.
provideDetectors(ctx context.Context) []detector
}
// staticDetectorsProvider is a detectorsProvider that always returns the provided detectors.
// staticDetectorsProvider is a detectorsProvider that always returns a pre-defined slice of detectors.
type staticDetectorsProvider struct {
detectors []detector
}
@@ -54,8 +55,8 @@ func (p *staticDetectorsProvider) provideDetectors(_ context.Context) []detector
return p.detectors
}
// sequenceDetectorsProvider is a detectorsProvider that wraps a slice of detectorsProvider and returns the first
// provider result that isn't empty.
// sequenceDetectorsProvider is a detectorsProvider that wraps a slice of other detectorsProvider, and returns the first
// provided result that isn't empty.
type sequenceDetectorsProvider []detectorsProvider
func (p sequenceDetectorsProvider) provideDetectors(ctx context.Context) []detector {
@@ -11,10 +11,10 @@ import (
func TestContainsBytesDetector(t *testing.T) {
detector := &containsBytesDetector{pattern: []byte("needle")}
t.Run("contains", func(t *testing.T) {
require.True(t, detector.Detect([]byte("lorem needle ipsum haystack")))
require.True(t, detector.detect([]byte("lorem needle ipsum haystack")))
})
t.Run("not contains", func(t *testing.T) {
require.False(t, detector.Detect([]byte("ippif")))
require.False(t, detector.detect([]byte("ippif")))
})
}
@@ -31,7 +31,7 @@ func TestRegexDetector(t *testing.T) {
{name: "no match", s: "bla bla hello you reading this test code", exp: false},
} {
t.Run(tc.s, func(t *testing.T) {
r := detector.Detect([]byte(tc.s))
r := detector.detect([]byte(tc.s))
require.Equal(t, tc.exp, r, "detector result should be correct")
})
}
@@ -11,11 +11,11 @@ import (
// Inspector can inspect a plugin and determine if it's an Angular plugin or not.
type Inspector interface {
// Inspect takes a plugin and checks if the plugin is using Angular.
// It returns true if module.js matches against any of the detectors in angularDetectors.
Inspect(ctx context.Context, p *plugins.Plugin) (bool, error)
}
// PatternsListInspector matches module.js against all the patterns returned by the detectorsProvider, in sequence.
// PatternsListInspector is an Inspector that matches a plugin's module.js against all the patterns returned by
// the detectorsProvider, in sequence.
type PatternsListInspector struct {
// detectorsProvider returns the detectors that will be used by Inspect.
detectorsProvider detectorsProvider
@@ -36,7 +36,7 @@ func (i *PatternsListInspector) Inspect(ctx context.Context, p *plugins.Plugin)
return false, fmt.Errorf("module.js readall: %w", err)
}
for _, d := range i.detectorsProvider.provideDetectors(ctx) {
if d.Detect(b) {
if d.detect(b) {
isAngular = true
break
}
@@ -14,7 +14,7 @@ type fakeDetector struct {
returns bool
}
func (d *fakeDetector) Detect(_ []byte) bool {
func (d *fakeDetector) detect(_ []byte) bool {
d.calls += 1
return d.returns
}