Skip to content

Commit

Permalink
review fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio committed Oct 22, 2024
1 parent b1fe2ef commit 0bb03d1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
48 changes: 10 additions & 38 deletions src/linter/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,21 @@ type Config struct {

type RuleNode struct {
Children map[string]*RuleNode // Child nodes (subdirectories and files)
Enable map[string]bool // Rules enabled at this level
Disable map[string]bool // Disabled rules at this level
Enabled map[string]bool // Rules enabled at this level
Disabled map[string]bool // Disabled rules at this level
}

func NewRuleNode() *RuleNode {
return &RuleNode{
Children: make(map[string]*RuleNode),
Enable: make(map[string]bool),
Disable: make(map[string]bool),
Enabled: make(map[string]bool),
Disabled: make(map[string]bool),
}
}

type PathRuleSet struct {
Enable map[string]bool // Rules that are enabled for this path
Disable map[string]bool // Rules that are disabled for this path
Enabled map[string]bool // Rules that are enabled for this path
Disabled map[string]bool // Rules that are disabled for this path
}

func BuildRuleTree(pathRules map[string]*PathRuleSet) *RuleNode {
Expand All @@ -112,45 +112,17 @@ func BuildRuleTree(pathRules map[string]*PathRuleSet) *RuleNode {
currentNode = currentNode.Children[part]
}

for rule := range ruleSet.Enable {
currentNode.Enable[rule] = true
for rule := range ruleSet.Enabled {
currentNode.Enabled[rule] = true
}
for rule := range ruleSet.Disable {
currentNode.Disable[rule] = true
for rule := range ruleSet.Disabled {
currentNode.Disabled[rule] = true
}
}

return root
}

func IsRuleEnabled(root *RuleNode, filePath string, checkRule string) bool {
normalizedPath := filepath.ToSlash(filepath.Clean(filePath))
parts := strings.Split(normalizedPath, "/")
currentNode := root

// Starting with global state. We have guarantee while parsing config that rule is `on` and exist
ruleState := true

for _, part := range parts {
if part == "" {
continue
}
if node, exists := currentNode.Children[part]; exists {
if node.Disable[checkRule] {
ruleState = false // Disable on this path
}
if node.Enable[checkRule] {
ruleState = true // Enable on this path
}
currentNode = node
} else {
break
}
}

return ruleState
}

func NewConfig(ver string) *Config {
reg := &CheckersRegistry{
info: map[string]CheckerInfo{},
Expand Down
34 changes: 31 additions & 3 deletions src/linter/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,34 @@ func (d *rootWalker) ReportPHPDoc(phpDocLocation PHPDocLocation, level int, chec
d.ReportLocation(loc, level, checkName, msg, args...)
}

func IsRuleEnabledForPath(root *RuleNode, filePath string, checkRule string) bool {
normalizedPath := filepath.ToSlash(filepath.Clean(filePath))
parts := strings.Split(normalizedPath, "/")
currentNode := root

// Starting with global state. We have guarantee while parsing config that rule is `on` and exist
ruleState := true

for _, part := range parts {
if part == "" {
continue
}
if node, exists := currentNode.Children[part]; exists {
if node.Disable[checkRule] {

Check failure on line 1525 in src/linter/root.go

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

node.Disable undefined (type *RuleNode has no field or method Disable)

Check failure on line 1525 in src/linter/root.go

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

node.Disable undefined (type *RuleNode has no field or method Disable)

Check failure on line 1525 in src/linter/root.go

View workflow job for this annotation

GitHub Actions / linter

node.Disable undefined (type *RuleNode has no field or method Disable) (typecheck)

Check failure on line 1525 in src/linter/root.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

node.Disable undefined (type *RuleNode has no field or method Disable)

Check failure on line 1525 in src/linter/root.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

node.Disable undefined (type *RuleNode has no field or method Disable)

Check failure on line 1525 in src/linter/root.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

node.Disable undefined (type *RuleNode has no field or method Disable)

Check failure on line 1525 in src/linter/root.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

node.Disable undefined (type *RuleNode has no field or method Disable)
ruleState = false // Disable on this path
}
if node.Enable[checkRule] {

Check failure on line 1528 in src/linter/root.go

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

node.Enable undefined (type *RuleNode has no field or method Enable)

Check failure on line 1528 in src/linter/root.go

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

node.Enable undefined (type *RuleNode has no field or method Enable)

Check failure on line 1528 in src/linter/root.go

View workflow job for this annotation

GitHub Actions / linter

node.Enable undefined (type *RuleNode has no field or method Enable) (typecheck)

Check failure on line 1528 in src/linter/root.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

node.Enable undefined (type *RuleNode has no field or method Enable)

Check failure on line 1528 in src/linter/root.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

node.Enable undefined (type *RuleNode has no field or method Enable)

Check failure on line 1528 in src/linter/root.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

node.Enable undefined (type *RuleNode has no field or method Enable)

Check failure on line 1528 in src/linter/root.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

node.Enable undefined (type *RuleNode has no field or method Enable)
ruleState = true // Enable on this path
}
currentNode = node
} else {
break
}
}

return ruleState
}

func (d *rootWalker) Report(n ir.Node, level int, checkName, msg string, args ...interface{}) {
var pos position.Position

Expand Down Expand Up @@ -1556,8 +1584,6 @@ func (d *rootWalker) Report(n ir.Node, level int, checkName, msg string, args ..
}
}

var rootNode = d.config.PathRules

filePath := d.file.Name()

if d.config.ProjectPath != "" {
Expand All @@ -1571,7 +1597,9 @@ func (d *rootWalker) Report(n ir.Node, level int, checkName, msg string, args ..
filePath = relativePath
}

if !IsRuleEnabled(rootNode, filePath, checkName) {
rootNode := d.config.PathRules

if !IsRuleEnabledForPath(rootNode, filePath, checkName) {
return
}

Expand Down

0 comments on commit 0bb03d1

Please sign in to comment.