From 1bb94b45c35e6f6ac34e08315a77523355e5221d Mon Sep 17 00:00:00 2001 From: Richardas Kuchinskas Date: Fri, 19 Jul 2024 15:30:34 +0300 Subject: [PATCH 1/3] solution prototype --- src/linter/root.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/linter/root.go b/src/linter/root.go index 261c85fa..de73d57a 100644 --- a/src/linter/root.go +++ b/src/linter/root.go @@ -123,6 +123,7 @@ func (d *rootWalker) File() *workspace.File { // EnterNode is invoked at every node in hierarchy. func (d *rootWalker) EnterNode(n ir.Node) (res bool) { res = true + //d.checker.currentClassNodeStack.Push(n) for _, c := range d.custom { c.BeforeEnterNode(n) @@ -172,6 +173,9 @@ func (d *rootWalker) EnterNode(n ir.Node) (res bool) { if !strings.HasSuffix(n.InterfaceName.Value, "able") { d.checker.CheckIdentMisspellings(n.InterfaceName) } + case *ir.IfStmt: + d.currentClassNodeStack.Push(n) + case *ir.ClassStmt: d.currentClassNodeStack.Push(n) @@ -225,6 +229,8 @@ func (d *rootWalker) EnterNode(n ir.Node) (res bool) { d.scope().AddVar(v, solver.ExprTypeLocal(d.scope(), d.ctx.st, n.Expr), "global variable", meta.VarAlwaysDefined) case *ir.FunctionStmt: + d.currentClassNodeStack.Push(n) + if d.metaInfo().IsIndexingComplete() { res = d.checker.CheckFunction(n) } else { @@ -1746,12 +1752,43 @@ func (d *rootWalker) renderRuleMessage(msg string, n ir.Node, m phpgrep.MatchDat return msg } +func (d *rootWalker) isSuppressed(n ir.Node, checkName string) bool { + if containLinterSuppress(n, checkName) { + return true + } + + // We go up the tree in search of a comment that disables this checker. + for i := 0; d.checker.currentClassNodeStack.NthParent(i) != nil; i++ { + parent := d.checker.currentClassNodeStack.NthParent(i) + if containLinterSuppress(parent, checkName) { + return true + } + } + + return false +} + func (d *rootWalker) runRule(n ir.Node, sc *meta.Scope, rule *rules.Rule) bool { m, ok := rule.Matcher.Match(n) + if !ok { return false } + /* switch n:= n.(type) { + + case *ir.IfStmt: + n. + } + */ + if d.isSuppressed(n, rule.Name) { + println() + } + + if containLinterSuppress(n, rule.Name) { + return false + } + matched := false if len(rule.Filters) == 0 { matched = true From d6700d67854d3715298f39501e4f8e77e3821702 Mon Sep 17 00:00:00 2001 From: Richardas Kuchinskas Date: Sun, 21 Jul 2024 20:41:37 +0300 Subject: [PATCH 2/3] rewrote logic + tests --- src/linter/root.go | 18 +----- src/tests/checkers/linter_suppress_test.go | 64 ++++++++++++++++++++++ 2 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/linter/root.go b/src/linter/root.go index de73d57a..5849f15b 100644 --- a/src/linter/root.go +++ b/src/linter/root.go @@ -123,7 +123,6 @@ func (d *rootWalker) File() *workspace.File { // EnterNode is invoked at every node in hierarchy. func (d *rootWalker) EnterNode(n ir.Node) (res bool) { res = true - //d.checker.currentClassNodeStack.Push(n) for _, c := range d.custom { c.BeforeEnterNode(n) @@ -173,8 +172,6 @@ func (d *rootWalker) EnterNode(n ir.Node) (res bool) { if !strings.HasSuffix(n.InterfaceName.Value, "able") { d.checker.CheckIdentMisspellings(n.InterfaceName) } - case *ir.IfStmt: - d.currentClassNodeStack.Push(n) case *ir.ClassStmt: d.currentClassNodeStack.Push(n) @@ -1758,8 +1755,8 @@ func (d *rootWalker) isSuppressed(n ir.Node, checkName string) bool { } // We go up the tree in search of a comment that disables this checker. - for i := 0; d.checker.currentClassNodeStack.NthParent(i) != nil; i++ { - parent := d.checker.currentClassNodeStack.NthParent(i) + for i := 0; d.currentClassNodeStack.NthParent(i) != nil; i++ { + parent := d.currentClassNodeStack.NthParent(i) if containLinterSuppress(parent, checkName) { return true } @@ -1770,22 +1767,11 @@ func (d *rootWalker) isSuppressed(n ir.Node, checkName string) bool { func (d *rootWalker) runRule(n ir.Node, sc *meta.Scope, rule *rules.Rule) bool { m, ok := rule.Matcher.Match(n) - if !ok { return false } - /* switch n:= n.(type) { - - case *ir.IfStmt: - n. - } - */ if d.isSuppressed(n, rule.Name) { - println() - } - - if containLinterSuppress(n, rule.Name) { return false } diff --git a/src/tests/checkers/linter_suppress_test.go b/src/tests/checkers/linter_suppress_test.go index 9e1aa6ba..954e2eb0 100644 --- a/src/tests/checkers/linter_suppress_test.go +++ b/src/tests/checkers/linter_suppress_test.go @@ -146,3 +146,67 @@ function g($b) { `) test.RunAndMatch() } + +func TestLinterSuppressInsideFunction(t *testing.T) { + rfile := ` Date: Mon, 22 Jul 2024 13:05:07 +0300 Subject: [PATCH 3/3] test renaming --- src/tests/checkers/linter_suppress_test.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/tests/checkers/linter_suppress_test.go b/src/tests/checkers/linter_suppress_test.go index 954e2eb0..33356709 100644 --- a/src/tests/checkers/linter_suppress_test.go +++ b/src/tests/checkers/linter_suppress_test.go @@ -147,7 +147,7 @@ function g($b) { test.RunAndMatch() } -func TestLinterSuppressInsideFunction(t *testing.T) { +func TestLinterSuppressDNRInsideFunction(t *testing.T) { rfile := `