From 9f0fd735f46b5ed2d30a047339842d1e400ce8d1 Mon Sep 17 00:00:00 2001 From: Richardas Kuchinskas <36065987+Hidanio@users.noreply.github.com> Date: Thu, 19 Dec 2024 02:49:13 +0300 Subject: [PATCH] linter: suppress in dynamic rules (#1225) --- src/linter/root.go | 23 +++++++ src/tests/checkers/linter_suppress_test.go | 74 ++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/src/linter/root.go b/src/linter/root.go index 261c85fa2..5849f15b9 100644 --- a/src/linter/root.go +++ b/src/linter/root.go @@ -172,6 +172,7 @@ func (d *rootWalker) EnterNode(n ir.Node) (res bool) { if !strings.HasSuffix(n.InterfaceName.Value, "able") { d.checker.CheckIdentMisspellings(n.InterfaceName) } + case *ir.ClassStmt: d.currentClassNodeStack.Push(n) @@ -225,6 +226,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 +1749,32 @@ 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.currentClassNodeStack.NthParent(i) != nil; i++ { + parent := d.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 } + if d.isSuppressed(n, rule.Name) { + return false + } + matched := false if len(rule.Filters) == 0 { matched = true diff --git a/src/tests/checkers/linter_suppress_test.go b/src/tests/checkers/linter_suppress_test.go index 9e1aa6ba5..333567091 100644 --- a/src/tests/checkers/linter_suppress_test.go +++ b/src/tests/checkers/linter_suppress_test.go @@ -146,3 +146,77 @@ function g($b) { `) test.RunAndMatch() } + +func TestLinterSuppressDNRInsideFunction(t *testing.T) { + rfile := `