Skip to content

Commit

Permalink
Merge pull request #223 from loeffel-io/feature/loeffel-io/#30-exists
Browse files Browse the repository at this point in the history
feat: exists
  • Loading branch information
loeffel-io committed Jul 11, 2024
2 parents 16c31a0 + 17f81f8 commit 888a537
Show file tree
Hide file tree
Showing 37 changed files with 971 additions and 159 deletions.
17 changes: 16 additions & 1 deletion .ls-lint.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
ls:
.dir: snake_case
.dir: snake_case # | exists:1
.json: exists:2
.bzl: snake_case
.sh: snake_case
.bazel: SCREAMING_SNAKE_CASE
.go: snake_case
.yaml: snake_case
.js: snake_case

deployments:
.dir: exists:1
.json: exists:1

peter:
.dir: exists:2 | PascalCase
.*: PascalCase | exists:2
.js: exists:3 | snake_case
.vue: exists:1

'*':
.dir: exists:1
.vue: snake_case

ignore:
- .git
- .github
Expand Down
18 changes: 18 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,26 @@ load("@bazel_gazelle//:def.bzl", "gazelle")
load("@io_bazel_rules_go//go:def.bzl", "TOOLS_NOGO", "nogo")

# gazelle:prefix github.com/loeffel-io/ls-lint/v2
# gazelle:exclude vendor
# gazelle:exclude .idea
gazelle(name = "gazelle")

gazelle(
name = "gazelle_update_repos",
args = [
"--from_file=go.mod",
"--to_macro=repositories.bzl%go_repositories",
"--prune",
],
command = "update-repos",
)

gazelle(
name = "gazelle_fix_diff",
command = "fix",
extra_args = ["--mode=diff"],
)

config_setting(
name = "darwin_arm64",
constraint_values = [
Expand Down
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ http_archive(
url = "https://github.com/aspect-build/rules_js/releases/download/v1.41.2/rules_js-v1.41.2.tar.gz",
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

############################################################
# custom repositories ######################################
Expand All @@ -55,7 +55,7 @@ go_repositories()

go_rules_dependencies()

go_register_toolchains(version = "1.22.1")
go_register_toolchains(version = "1.22.4")

############################################################
# gazelle ##################################################
Expand Down
56 changes: 33 additions & 23 deletions cmd/ls_lint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import (
"github.com/loeffel-io/ls-lint/v2/internal/linter"
"github.com/loeffel-io/ls-lint/v2/internal/rule"
"gopkg.in/yaml.v3"
"io/fs"
"log"
"maps"
"os"
"runtime"
"slices"
"strings"
"testing/fstest"
)

var Version = "dev"
Expand Down Expand Up @@ -61,22 +59,13 @@ func main() {
flagConfig = _flag.Config{".ls-lint.yml"}
}

var args = flags.Args()
var filesystem fs.FS
switch len(args) {
case 0:
filesystem = os.DirFS(*flagWorkdir)
default:
var mapFilesystem = make(fstest.MapFS, len(args))
for _, file := range args {
var fileInfo os.FileInfo
if fileInfo, err = os.Stat(fmt.Sprintf("%s/%s", *flagWorkdir, file)); err != nil {
log.Fatal(err)
}

mapFilesystem[file] = &fstest.MapFile{Mode: fileInfo.Mode()}
var filesystem = os.DirFS(*flagWorkdir)
var paths map[string]struct{}
if len(flags.Args()[0:]) > 0 {
paths = make(map[string]struct{}, len(flags.Args()[0:]))
for _, path := range flags.Args()[0:] {
paths[path] = struct{}{}
}
filesystem = mapFilesystem
}

var lslintConfig = config.NewConfig(make(config.Ls), make([]string, 0))
Expand Down Expand Up @@ -105,7 +94,7 @@ func main() {
make([]*rule.Error, 0),
)

if err = lslintLinter.Run(filesystem, *flagDebug); err != nil {
if err = lslintLinter.Run(filesystem, paths, *flagDebug); err != nil {
log.Fatal(err)
}

Expand All @@ -122,11 +111,23 @@ func main() {

switch *flagErrorOutputFormat {
case "json":
var errIndex = make(map[string][]string, len(lslintLinter.GetErrors()))
var errIndex = make(map[string]map[string][]string, len(lslintLinter.GetErrors()))
for _, ruleErr := range lslintLinter.GetErrors() {
errIndex[ruleErr.GetPath()] = make([]string, len(ruleErr.GetRules()))
for i, ruleErrMessages := range ruleErr.GetRules() {
errIndex[ruleErr.GetPath()][i] = ruleErrMessages.GetErrorMessage()
path := ruleErr.GetPath()
if path == "" {
path = "."
}

if _, ok := errIndex[path]; !ok {
errIndex[path] = make(map[string][]string)
}

for _, errRule := range ruleErr.GetRules() {
if !ruleErr.IsDir() && errRule.GetName() == "exists" {
continue
}

errIndex[path][ruleErr.GetExt()] = append(errIndex[path][ruleErr.GetExt()], errRule.GetErrorMessage())
}
}

Expand All @@ -142,11 +143,20 @@ func main() {
for _, ruleErr := range lslintLinter.GetErrors() {
var ruleMessages []string

path := ruleErr.GetPath()
if path == "" {
path = "."
}

for _, errRule := range ruleErr.GetRules() {
if !ruleErr.IsDir() && errRule.GetName() == "exists" {
continue
}

ruleMessages = append(ruleMessages, errRule.GetErrorMessage())
}

if _, err = fmt.Fprintf(writer, "%s failed for rules: %s\n", ruleErr.GetPath(), strings.Join(ruleMessages, "|")); err != nil {
if _, err = fmt.Fprintf(writer, "%s failed for `%s` rules: %s\n", path, ruleErr.GetExt(), strings.Join(ruleMessages, " | ")); err != nil {
log.Fatal(err)
}
}
Expand Down
Empty file added deployments/test.json
Empty file.
11 changes: 7 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,17 @@ func (config *Config) ShouldIgnore(ignoreIndex map[string]bool, path string) boo
return false
}

func (config *Config) GetConfig(index RuleIndex, path string) map[string][]rule.Rule {
func (config *Config) GetConfig(index RuleIndex, path string) (string, map[string][]rule.Rule) {
dirs := strings.Split(path, sep)

for i := len(dirs); i >= 0; i-- {
if find, exists := index[strings.Join(dirs[:i], sep)]; exists {
return find
var dir = strings.Join(dirs[:i], sep)
if find, exists := index[dir]; exists {
return dir, find
}
}

return nil
return "", nil
}

func (config *Config) GetIndex(list Ls) (RuleIndex, error) {
Expand Down Expand Up @@ -144,6 +145,8 @@ func (config *Config) copyRule(r rule.Rule) rule.Rule {
switch r.GetName() {
case "regex":
return new(rule.Regex).Init()
case "exists":
return new(rule.Exists).Init()
}

return r
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestGetConfig(t *testing.T) {

var i = 0
for _, test := range tests {
res := test.config.GetConfig(test.index, test.path)
_, res := test.config.GetConfig(test.index, test.path)

if !reflect.DeepEqual(res, test.expected) {
t.Errorf("Test %d failed with unmatched return value - %+v", i, res)
Expand Down
Loading

0 comments on commit 888a537

Please sign in to comment.