Skip to content

Commit

Permalink
Merge pull request #119 from loeffel-io/feature/loeffel-io/glob-ignor…
Browse files Browse the repository at this point in the history
…e-index

feat: add glob ignore index
  • Loading branch information
loeffel-io committed Jul 9, 2023
2 parents 8d82f42 + 964d67b commit 1234225
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bazel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ jobs:
credentials_json: ${{ secrets.GOOGLE_CREDENTIALS }}
- run: bazel run --remote_cache=$GOOGLE_BUCKET --google_default_credentials //deployments/github:ls_lint_publish
- run: bazel build --remote_cache=$GOOGLE_BUCKET --google_default_credentials //deployments/npm:ls_lint
- run: (cd bazel-bin/deployments/npm/ls_lint && NPM_CONFIG_USERCONFIG=${GITHUB_WORKSPACE}/deployments/npm/.npmrc npm publish --no-git-checks) # workaround: https://bazelbuild.slack.com/archives/CEZUUKQ6P/p1667995025343689 # --dry-run --tag beta
- run: (cd bazel-bin/deployments/npm/ls_lint && NPM_CONFIG_USERCONFIG=${GITHUB_WORKSPACE}/deployments/npm/.npmrc npm publish --no-git-checks --tag beta) # workaround: https://bazelbuild.slack.com/archives/CEZUUKQ6P/p1667995025343689 # --dry-run --tag beta
7 changes: 2 additions & 5 deletions .ls-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ ls:

ignore:
- .git
- .idea
- .github
- .idea
- genhtml
- bazel-bin
- bazel-out
- bazel-ls-lint
- bazel-testlogs
- bazel-*
- deployments/npm/pnpm-lock.yaml
2 changes: 1 addition & 1 deletion deployments/github/github.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set -euo pipefail
gh=$1
github_files=$2

$gh release create --generate-notes --latest ${STABLE_GIT_TAG} $github_files
$gh release create --generate-notes --latest ${STABLE_GIT_TAG} --draft $github_files
5 changes: 1 addition & 4 deletions internal/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ go_library(
srcs = ["config.go"],
importpath = "github.com/loeffel-io/ls-lint/v2/internal/config",
visibility = ["//:__subpackages__"],
deps = [
"//internal/rule",
"@com_github_bmatcuk_doublestar_v4//:doublestar",
],
deps = ["//internal/rule"],
)

go_test(
Expand Down
38 changes: 0 additions & 38 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package config

import (
"fmt"
"github.com/bmatcuk/doublestar/v4"
"github.com/loeffel-io/ls-lint/v2/internal/rule"
"io/fs"
"reflect"
"strings"
"sync"
Expand Down Expand Up @@ -93,42 +91,6 @@ func (config *Config) GetIndex(list Ls) (RuleIndex, error) {
return index, nil
}

func (config *Config) GlobIndex(filesystem fs.FS, index RuleIndex) (err error) {
for key, value := range index {
var matches []string

if !strings.ContainsAny(key, "*{}") {
continue
}

if matches, err = doublestar.Glob(filesystem, key); err != nil {
return err
}

if len(matches) == 0 {
delete(index, key)
continue
}

for _, match := range matches {
var matchInfo fs.FileInfo

if matchInfo, err = fs.Stat(filesystem, match); err != nil {
return err
}

if !matchInfo.IsDir() {
continue
}

index[match] = value
delete(index, key)
}
}

return nil
}

func (config *Config) walkIndex(index RuleIndex, key string, list Ls) error {
if index[key] == nil {
index[key] = make(map[string][]rule.Rule)
Expand Down
12 changes: 12 additions & 0 deletions internal/glob/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "glob",
srcs = ["glob.go"],
importpath = "github.com/loeffel-io/ls-lint/v2/internal/glob",
visibility = ["//:__subpackages__"],
deps = [
"//internal/rule",
"@com_github_bmatcuk_doublestar_v4//:doublestar",
],
)
44 changes: 44 additions & 0 deletions internal/glob/glob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package glob

import (
"github.com/bmatcuk/doublestar/v4"
"github.com/loeffel-io/ls-lint/v2/internal/rule"
"io/fs"
"strings"
)

func Index[IndexValue bool | map[string][]rule.Rule](filesystem fs.FS, index map[string]IndexValue, files bool) (err error) {
for key, value := range index {
var matches []string

if !strings.ContainsAny(key, "*{}") {
continue
}

if matches, err = doublestar.Glob(filesystem, key); err != nil {
return err
}

if len(matches) == 0 {
delete(index, key)
continue
}

for _, match := range matches {
var matchInfo fs.FileInfo

if matchInfo, err = fs.Stat(filesystem, match); err != nil {
return err
}

if !files && !matchInfo.IsDir() {
continue
}

index[match] = value
delete(index, key)
}
}

return nil
}
1 change: 1 addition & 0 deletions internal/linter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
deps = [
"//internal/config",
"//internal/debug",
"//internal/glob",
"//internal/rule",
"@org_golang_x_sync//errgroup",
],
Expand Down
8 changes: 7 additions & 1 deletion internal/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/loeffel-io/ls-lint/v2/internal/config"
"github.com/loeffel-io/ls-lint/v2/internal/debug"
"github.com/loeffel-io/ls-lint/v2/internal/glob"
"github.com/loeffel-io/ls-lint/v2/internal/rule"
"golang.org/x/sync/errgroup"
"io/fs"
Expand Down Expand Up @@ -173,7 +174,12 @@ func (linter *Linter) Run(filesystem fs.FS, debug bool, statistics bool) (err er
}

// glob index
if err = linter.config.GlobIndex(filesystem, index); err != nil {
if err = glob.Index(filesystem, index, false); err != nil {
return err
}

// glob ignore index
if err = glob.Index(filesystem, ignoreIndex, true); err != nil {
return err
}

Expand Down
61 changes: 61 additions & 0 deletions internal/linter/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,67 @@ func TestLinterRun(t *testing.T) {
},
},
},
{
description: "glob and glob ignore",
filesystem: fstest.MapFS{
"snake_case.png": &fstest.MapFile{Mode: fs.ModePerm},
"src/a/a": &fstest.MapFile{Mode: fs.ModeDir},
"src/a/a/kebab-case.png": &fstest.MapFile{Mode: fs.ModePerm},
"src/a/a/kebab-case.jpg": &fstest.MapFile{Mode: fs.ModePerm},
"src/b/b": &fstest.MapFile{Mode: fs.ModeDir},
"src/b/b/kebab-case.png": &fstest.MapFile{Mode: fs.ModePerm},
"src/b/b/kebab-case.jpg": &fstest.MapFile{Mode: fs.ModePerm},
"src/c/c": &fstest.MapFile{Mode: fs.ModeDir},
"src/c/c/PascalCase.png": &fstest.MapFile{Mode: fs.ModePerm},
"src/c/c/PascalCase.jpg": &fstest.MapFile{Mode: fs.ModePerm},
"src/c/c/ignore.png": &fstest.MapFile{Mode: fs.ModePerm},
"src/c/c/packages": &fstest.MapFile{Mode: fs.ModeDir},
"src/c/c/packages/snake_case.png": &fstest.MapFile{Mode: fs.ModePerm},
"src/c/d/snake_case.png": &fstest.MapFile{Mode: fs.ModePerm},
},
linter: NewLinter(
".",
config.NewConfig(
config.Ls{
".png": "snake_case",
".jpg": "kebab-case",
"src/**/c": config.Ls{
".png": "PascalCase",
"packages": config.Ls{
".png": "snake_case",
},
},
"src/{a,b}/*": config.Ls{
".png": "kebab-case",
},
},
[]string{
"src/c/c/ignore.png",
"src/c/*/*.jpg",
"src/c/d/*",
},
),
&debug.Statistic{
Start: start,
Files: 0,
FileSkips: 0,
Dirs: 0,
DirSkips: 0,
RWMutex: new(sync.RWMutex),
},
[]*rule.Error{},
),
expectedErr: nil,
expectedStatistic: &debug.Statistic{
Start: start,
Files: 7,
FileSkips: 3,
Dirs: 10,
DirSkips: 0,
RWMutex: new(sync.RWMutex),
},
expectedErrors: []*rule.Error{},
},
}

var i = 0
Expand Down

0 comments on commit 1234225

Please sign in to comment.