Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ jobs:
with:
go-version: '1.22.1'

- name: Run linter
uses: golangci/golangci-lint-action@v8
with:
version: v2.12.2
args: ./...

- name: Run Tests
# Tests must be run sequentially because they create temporary files that can cause issues.
# Therefore, the "-p 1" argument is required.
Expand Down
65 changes: 42 additions & 23 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
# This file holds the configs for the linters, and it should be placed in the current working directory.
version: "2"
run:
timeout: 5m
tests: true
go: 1.22.1
modules-download-mode: readonly
tests: true
allow-parallel-runners: true
go: '1.22.1'
linters:
enable:
# Reference doc with available linters and description for the linters below: https://golangci-lint.run/usage/linters/
# Default
- gosimple
- govet
- ineffassign
- staticcheck
- unused

# Bug linters
- errorlint
- gosec
- gosmopolitan
- nilerr
- testifylint

# Comment linters
- godot

# Complexity linters
- cyclop
- funlen
- gocognit
- gocyclo
- nestif
# Format linters
- gofmt
- goimports

# Meta linters
- gocritic
- revive
- staticcheck

# Style linters
- copyloopvar
- dogsled
Expand All @@ -53,36 +54,54 @@ linters:
- nilnil
- nlreturn
- nonamedreturns
- stylecheck
- unconvert
- unparam
- usestdlibvars
- varnamelen
- wastedassign
- whitespace

# Test linters
- ginkgolinter
- tenv
- testableexamples
- testpackage
fast: false
settings:
lll:
line-length: 100
exclusions:
generated: lax
presets:
- comments
- common-false-positives
- legacy
- std-error-handling
rules:
- linters:
- revive
path: _test\.go
text: 'dot-imports: should not use dot imports'
- linters:
- goconst
path: _test\.go
paths:
- third_party$
- builtin$
- examples$
issues:
# Default values allow for the showing of only 50 issues.
# Setting it to 0 disables the default config and shows all issues at once.
max-issues-per-linter: 0
# Default values allow showing only 3 repeating issues.
# Setting it to 0 disables the default config and shows all repeating issues at once.
max-same-issues: 0
exclude-rules:
# As we use external test libs, tests will be hard to read if we have package reference everywhere.
- linters:
- revive
text: "dot-imports: should not use dot imports"
path: _test\.go
# We may have repeating lines in tests and it should be valid.
- linters:
- goconst
path: _test\.go
linters-settings:
lll:
line-length: 100
formatters:
enable:
# Format linters.
- gofmt
- goimports
exclusions:
generated: lax
paths:
- third_party$
- builtin$
- examples$
1 change: 1 addition & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func BuildEmbedCodeConfiguration(userArgs Config) []configuration.Configuration
))
embedCodeConfigs = append(embedCodeConfigs, configFromEmbedding(embedding))
}

return embedCodeConfigs
}

Expand Down
3 changes: 2 additions & 1 deletion cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ var _ = Describe("CLI validation", func() {
"`code-path` and `docs-path` cannot be set when `embeddings` are set"))
})

It("should fail validation when embeddings and root optional params are set at the same time", func() {
It("should reject embeddings with root optional params", func() {
invalidConfig := cli.Config{
Mode: cli.ModeCheck,
DocIncludes: []string{"**/*.md"},
Expand Down Expand Up @@ -263,6 +263,7 @@ func baseCliConfig() cli.Config {

func baseEmbeddingConfig() cli.EmbeddingConfig {
baseConfig := baseCliConfig()

return cli.EmbeddingConfig{
Name: "docs",
CodePaths: baseConfig.BaseCodePaths,
Expand Down
17 changes: 11 additions & 6 deletions cli/cli_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,13 @@ func findEmbeddingNameDuplications(embeddings []EmbeddingConfig) error {

if len(errLines) > 0 {
slices.Sort(errLines)

return fmt.Errorf(
"duplicate embedding names detected:\n%s",
strings.Join(errLines, "\n"),
)
}

return nil
}

Expand Down Expand Up @@ -296,6 +298,7 @@ func validatePaths(paths _type.NamedPathList) (bool, error) {
allPathsSet = false
}
}

return allPathsSet, nil
}

Expand All @@ -307,18 +310,18 @@ func validateCodeSources(paths _type.NamedPathList) error {
unnamedCount := 0
hasNamed := false

for _, p := range paths {
if isEmpty(p.Path) {
for _, pathEntry := range paths {
if isEmpty(pathEntry.Path) {
continue
}
if isEmpty(p.Name) {
if isEmpty(pathEntry.Name) {
unnamedCount++
} else {
hasNamed = true
nameCount[p.Name]++
nameCount[pathEntry.Name]++
}
pathCount[p.Path]++
pathNames[p.Path] = append(pathNames[p.Path], p.Name)
pathCount[pathEntry.Path]++
pathNames[pathEntry.Path] = append(pathNames[pathEntry.Path], pathEntry.Name)
}

if err := verifyCodeSourceNames(nameCount); err != nil {
Expand Down Expand Up @@ -347,11 +350,13 @@ func verifyCodeSourceNames(nameCount map[string]int) error {

if len(errLines) > 0 {
slices.Sort(errLines)

return fmt.Errorf(
"duplicate source code path names detected:\n%s",
strings.Join(errLines, "\n"),
)
}

return nil
}

Expand Down
29 changes: 18 additions & 11 deletions embedding/commentfilter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@

package commentfilter

const (
cStyleBlockCommentStart = "/*"
cStyleBlockCommentEnd = "*/"
cStyleDocCommentStart = "/**"
jsQuoteChars = "\"'`"
)

// filtersByExtension is a mapping of the file extension to its comment filter.
var filtersByExtension = map[string]filterEntry{
// Java/Kotlin
Expand Down Expand Up @@ -83,51 +90,51 @@ type filterEntry struct {
var javaSyntax = CommentMarker{
Inline: []string{"//"},
Block: []BlockMarker{
{Start: "/*", End: "*/"},
{Start: cStyleBlockCommentStart, End: cStyleBlockCommentEnd},
},
Documentation: DocumentationMarker{
Block: []BlockMarker{{Start: "/**", End: "*/"}},
Block: []BlockMarker{{Start: cStyleDocCommentStart, End: cStyleBlockCommentEnd}},
},
QuoteChars: "\"'",
}

var jsSyntax = CommentMarker{
Inline: []string{"//"},
Block: []BlockMarker{
{Start: "/*", End: "*/"},
{Start: cStyleBlockCommentStart, End: cStyleBlockCommentEnd},
},
Documentation: DocumentationMarker{
Block: []BlockMarker{{Start: "/**", End: "*/"}},
Block: []BlockMarker{{Start: cStyleDocCommentStart, End: cStyleBlockCommentEnd}},
},
QuoteChars: "\"'`",
QuoteChars: jsQuoteChars,
}

var csharpSyntax = CommentMarker{
Inline: []string{"//"},
Block: []BlockMarker{
{Start: "/*", End: "*/"},
{Start: cStyleBlockCommentStart, End: cStyleBlockCommentEnd},
},
Documentation: DocumentationMarker{
Inline: []string{"///"},
Block: []BlockMarker{{Start: "/**", End: "*/"}},
Block: []BlockMarker{{Start: cStyleDocCommentStart, End: cStyleBlockCommentEnd}},
},
QuoteChars: "\"'`",
QuoteChars: jsQuoteChars,
}

var cStyleSyntax = CommentMarker{
Inline: []string{"//"},
Block: []BlockMarker{
{Start: "/*", End: "*/"},
{Start: cStyleBlockCommentStart, End: cStyleBlockCommentEnd},
},
QuoteChars: "\"'",
}

var goSyntax = CommentMarker{
Inline: []string{"//"},
Block: []BlockMarker{
{Start: "/*", End: "*/"},
{Start: cStyleBlockCommentStart, End: cStyleBlockCommentEnd},
},
QuoteChars: "\"'`",
QuoteChars: jsQuoteChars,
}

var hashLineSyntax = CommentMarker{
Expand Down
21 changes: 14 additions & 7 deletions embedding/commentfilter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func (f EmbeddingCommentFilter) Filter(lines []string, mode Mode) []string {
if mode == RetainAll {
return lines
}
filter, found := filterFor(f.filePath, mode, f.embeddingDocPath, f.embeddingLine)
entry, found := filterFor(f.filePath, mode, f.embeddingDocPath, f.embeddingLine)
if !found {
return lines
}

return filter.Filter(lines, mode)
return entry.filter.Filter(lines, mode)
}

// filterFor returns the comment filter registered for the given file path and warns on odd modes.
Expand All @@ -74,18 +74,25 @@ func filterFor(
mode Mode,
embeddingDocPath string,
embeddingLine int,
) (CommentFilter, bool) {
) (filterEntry, bool) {
extension := normalizeExtension(filepath.Ext(filePath))
entry, found := filtersByExtension[extension]
if !found {
warnUnsupportedFileType(filePath, mode, embeddingDocPath, embeddingLine)
return nil, false

return filterEntry{}, false
}
if warnUnsupportedCommentsMode(filePath, mode, embeddingDocPath, embeddingLine, entry.supportedModes) {
return nil, false
if warnUnsupportedCommentsMode(
filePath,
mode,
embeddingDocPath,
embeddingLine,
entry.supportedModes,
) {
return filterEntry{}, false
}

return entry.filter, true
return entry, true
}

// normalizeExtension returns a lowercase file extension with a leading dot.
Expand Down
4 changes: 3 additions & 1 deletion embedding/commentfilter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package commentfilter
package commentfilter_test

import (
"bytes"
"log/slog"
"testing"

. "embed-code/embed-code-go/embedding/commentfilter"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand Down
Loading
Loading