pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/pkgtools/pkglint pkgtools/pkglint: update to 19.4.0



details:   https://anonhg.NetBSD.org/pkgsrc/rev/0e683a683a33
branches:  trunk
changeset: 346234:0e683a683a33
user:      rillig <rillig%pkgsrc.org@localhost>
date:      Mon Dec 30 16:27:13 2019 +0000

description:
pkgtools/pkglint: update to 19.4.0

Changes since 19.3.19:

Empty PLIST files now generate an error instead of a warning since there
is no reason for having these empty files.

If a follow-up line in a Makefile is completely empty, there is no note
about trailing whitespace anymore since the warning about the misleading
empty line already covers this case.

The remaining code changes are only refactorings.

diffstat:

 pkgtools/pkglint/Makefile                      |    4 +-
 pkgtools/pkglint/files/autofix.go              |   94 +-----
 pkgtools/pkglint/files/autofix_test.go         |  198 ++++--------
 pkgtools/pkglint/files/category.go             |   24 +-
 pkgtools/pkglint/files/check_test.go           |    4 +
 pkgtools/pkglint/files/intqa/qa.go             |   31 +-
 pkgtools/pkglint/files/intqa/qa_test.go        |   54 +++-
 pkgtools/pkglint/files/linechecker.go          |   55 +-
 pkgtools/pkglint/files/linechecker_test.go     |   50 ++-
 pkgtools/pkglint/files/lines.go                |    2 +-
 pkgtools/pkglint/files/logging.go              |   86 +++--
 pkgtools/pkglint/files/logging_test.go         |   65 +++-
 pkgtools/pkglint/files/mkline_test.go          |   28 +
 pkgtools/pkglint/files/mklinechecker.go        |    5 +-
 pkgtools/pkglint/files/mklinechecker_test.go   |   61 ++++-
 pkgtools/pkglint/files/mklineparser.go         |    2 +-
 pkgtools/pkglint/files/mkvarusechecker.go      |    2 +-
 pkgtools/pkglint/files/mkvarusechecker_test.go |   18 +-
 pkgtools/pkglint/files/package_test.go         |   17 +
 pkgtools/pkglint/files/paragraph.go            |    3 -
 pkgtools/pkglint/files/plist.go                |    6 +-
 pkgtools/pkglint/files/plist_test.go           |   22 +-
 pkgtools/pkglint/files/redundantscope_test.go  |   18 +-
 pkgtools/pkglint/files/shell.go                |    8 +-
 pkgtools/pkglint/files/util.go                 |   56 +++-
 pkgtools/pkglint/files/util_test.go            |   26 +
 pkgtools/pkglint/files/varalignblock.go        |  251 +++-------------
 pkgtools/pkglint/files/varalignblock_test.go   |  369 ++++++++++++++++++------
 pkgtools/pkglint/files/vardefs.go              |    2 +-
 pkgtools/pkglint/files/vartype_test.go         |   17 +
 pkgtools/pkglint/files/vartypecheck.go         |    3 +-
 pkgtools/pkglint/files/vartypecheck_test.go    |   10 +
 32 files changed, 922 insertions(+), 669 deletions(-)

diffs (truncated from 2561 to 300 lines):

diff -r 4ed9d48d3e44 -r 0e683a683a33 pkgtools/pkglint/Makefile
--- a/pkgtools/pkglint/Makefile Mon Dec 30 13:58:59 2019 +0000
+++ b/pkgtools/pkglint/Makefile Mon Dec 30 16:27:13 2019 +0000
@@ -1,6 +1,6 @@
-# $NetBSD: Makefile,v 1.620 2019/12/16 17:06:05 rillig Exp $
+# $NetBSD: Makefile,v 1.621 2019/12/30 16:27:13 rillig Exp $
 
-PKGNAME=       pkglint-19.3.19
+PKGNAME=       pkglint-19.4.0
 CATEGORIES=    pkgtools
 DISTNAME=      tools
 MASTER_SITES=  ${MASTER_SITE_GITHUB:=golang/}
diff -r 4ed9d48d3e44 -r 0e683a683a33 pkgtools/pkglint/files/autofix.go
--- a/pkgtools/pkglint/files/autofix.go Mon Dec 30 13:58:59 2019 +0000
+++ b/pkgtools/pkglint/files/autofix.go Mon Dec 30 16:27:13 2019 +0000
@@ -1,7 +1,6 @@
 package pkglint
 
 import (
-       "netbsd.org/pkglint/regex"
        "os"
        "strconv"
        "strings"
@@ -117,8 +116,8 @@
        }
 
        for _, rawLine := range fix.line.raw {
-               replaced := strings.Replace(rawLine.textnl, prefixFrom, prefixTo, 1)
-               if replaced != rawLine.textnl {
+               ok, replaced := replaceOnce(rawLine.textnl, prefixFrom, prefixTo)
+               if ok {
                        if G.Logger.IsAutofix() {
                                rawLine.textnl = replaced
 
@@ -129,10 +128,7 @@
                                // TODO: Do this properly by parsing the whole line again,
                                //  and ideally everything that depends on the parsed line.
                                //  This probably requires a generic notification mechanism.
-                               //
-                               // FIXME: Only actually update fix.line.Text if the replacement
-                               //  has been done exactly once; see ReplaceAt.
-                               fix.line.Text = strings.Replace(fix.line.Text, prefixFrom, prefixTo, 1)
+                               _, fix.line.Text = replaceOnce(fix.line.Text, prefixFrom, prefixTo)
                        }
                        fix.Describef(rawLine.Lineno, "Replacing %q with %q.", from, to)
                        return
@@ -168,71 +164,11 @@
        // TODO: Do this properly by parsing the whole line again,
        //  and ideally everything that depends on the parsed line.
        //  This probably requires a generic notification mechanism.
-       if strings.Count(fix.line.Text, from) == 1 {
-               fix.line.Text = strings.Replace(fix.line.Text, from, to, 1)
-       }
+       _, fix.line.Text = replaceOnce(fix.line.Text, from, to)
 
        fix.Describef(rawLine.Lineno, "Replacing %q with %q.", from, to)
 }
 
-// ReplaceRegex replaces the first howOften or all occurrences (if negative)
-// of the `from` pattern with the fixed string `toText`.
-//
-// Placeholders like `$1` are _not_ expanded in the `toText`.
-// (If you know how to do the expansion correctly, feel free to implement it.)
-func (fix *Autofix) ReplaceRegex(from regex.Pattern, toText string, howOften int) {
-       fix.assertRealLine()
-       if fix.skip() {
-               return
-       }
-
-       done := 0
-       for _, rawLine := range fix.line.raw {
-               var froms []string // The strings that have actually changed
-
-               replace := func(fromText string) string {
-                       if howOften >= 0 && done >= howOften {
-                               return fromText
-                       }
-                       froms = append(froms, fromText)
-                       done++
-                       return toText
-               }
-
-               replaced := replaceAllFunc(rawLine.textnl, from, replace)
-               if replaced != rawLine.textnl {
-                       if G.Logger.IsAutofix() {
-                               rawLine.textnl = replaced
-                       }
-                       for _, fromText := range froms {
-                               fix.Describef(rawLine.Lineno, "Replacing %q with %q.", fromText, toText)
-                       }
-               }
-       }
-
-       // Fix the parsed text as well.
-       // This is only approximate and won't work in some edge cases
-       // that involve escaped comments or replacements across line breaks.
-       //
-       // TODO: Do this properly by parsing the whole line again,
-       //  and ideally everything that depends on the parsed line.
-       //  This probably requires a generic notification mechanism.
-       //
-       // FIXME: Only actually update fix.line.Text if the replacement
-       //  has been done exactly once.
-       done = 0
-       fix.line.Text = replaceAllFunc(
-               fix.line.Text,
-               from,
-               func(fromText string) string {
-                       if howOften >= 0 && done >= howOften {
-                               return fromText
-                       }
-                       done++
-                       return toText
-               })
-}
-
 // InsertBefore prepends a line before the current line.
 // The newline is added internally.
 func (fix *Autofix) InsertBefore(text string) {
@@ -241,9 +177,7 @@
                return
        }
 
-       if G.Logger.IsAutofix() {
-               fix.linesBefore = append(fix.linesBefore, text+"\n")
-       }
+       fix.linesBefore = append(fix.linesBefore, text+"\n")
        fix.Describef(fix.line.raw[0].Lineno, "Inserting a line %q before this line.", text)
 }
 
@@ -255,9 +189,7 @@
                return
        }
 
-       if G.Logger.IsAutofix() {
-               fix.linesAfter = append(fix.linesAfter, text+"\n")
-       }
+       fix.linesAfter = append(fix.linesAfter, text+"\n")
        fix.Describef(fix.line.raw[len(fix.line.raw)-1].Lineno, "Inserting a line %q after this line.", text)
 }
 
@@ -271,9 +203,7 @@
        }
 
        for _, line := range fix.line.raw {
-               if G.Logger.IsAutofix() {
-                       line.textnl = ""
-               }
+               line.textnl = ""
                fix.Describef(line.Lineno, "Deleting this line.")
        }
 }
@@ -332,6 +262,12 @@
 // SaveAutofixChanges needs to be called. For example, this is done by
 // MkLines.Check.
 func (fix *Autofix) Apply() {
+       // XXX: Make the following annotations actually do something.
+       // gobco:beforeCall:!G.Opts.ShowAutofix && !G.Opts.Autofix
+       // gobco:beforeCall:G.Opts.ShowAutofix
+       // gobco:beforeCall:G.Opts.Autofix
+       // See https://github.com/rillig/gobco
+
        line := fix.line
 
        // Each autofix must have a log level and a diagnostic.
@@ -366,7 +302,7 @@
                linenos := fix.affectedLinenos()
                msg := sprintf(fix.diagFormat, fix.diagArgs...)
                if !logFix && G.Logger.FirstTime(line.Filename, linenos, msg) {
-                       G.Logger.showSource(line)
+                       G.Logger.writeSource(line)
                }
                G.Logger.Logf(fix.level, line.Filename, linenos, fix.diagFormat, msg)
        }
@@ -379,7 +315,7 @@
                        }
                        G.Logger.Logf(AutofixLogLevel, line.Filename, lineno, autofixFormat, action.description)
                }
-               G.Logger.showSource(line)
+               G.Logger.writeSource(line)
        }
 
        if logDiagnostic && len(fix.explanation) > 0 {
diff -r 4ed9d48d3e44 -r 0e683a683a33 pkgtools/pkglint/files/autofix_test.go
--- a/pkgtools/pkglint/files/autofix_test.go    Mon Dec 30 13:58:59 2019 +0000
+++ b/pkgtools/pkglint/files/autofix_test.go    Mon Dec 30 16:27:13 2019 +0000
@@ -7,7 +7,7 @@
        "strings"
 )
 
-func (s *Suite) Test_Autofix__default_leaves_line_unchanged(c *check.C) {
+func (s *Suite) Test_Autofix__default_also_updates_line(c *check.C) {
        t := s.Init(c)
 
        t.SetUpCommandLine("--source")
@@ -19,15 +19,14 @@
        fix := line.Autofix()
        fix.Warnf("Row should be replaced with line.")
        fix.Replace("row", "line")
-       fix.ReplaceRegex(`row \d+`, "the above line", -1)
        fix.InsertBefore("above")
        fix.InsertAfter("below")
        fix.Delete()
        fix.Apply()
 
        t.CheckEquals(fix.RawText(), ""+
-               "# row 1 \\\n"+
-               "continuation of row 1\n")
+               "above\n"+
+               "below\n")
        t.CheckOutputLines(
                ">\t# row 1 \\",
                ">\tcontinuation of row 1",
@@ -47,7 +46,6 @@
        fix := line.Autofix()
        fix.Warnf("Row should be replaced with line.")
        fix.ReplaceAfter("", "# row", "# line")
-       fix.ReplaceRegex(`row \d+`, "the above line", -1)
        fix.InsertBefore("above")
        fix.InsertAfter("below")
        fix.Delete()
@@ -59,7 +57,6 @@
        t.CheckOutputLines(
                "WARN: ~/Makefile:1--2: Row should be replaced with line.",
                "AUTOFIX: ~/Makefile:1: Replacing \"# row\" with \"# line\".",
-               "AUTOFIX: ~/Makefile:2: Replacing \"row 1\" with \"the above line\".",
                "AUTOFIX: ~/Makefile:1: Inserting a line \"above\" before this line.",
                "AUTOFIX: ~/Makefile:2: Inserting a line \"below\" after this line.",
                "AUTOFIX: ~/Makefile:1: Deleting this line.",
@@ -84,7 +81,7 @@
        {
                fix := line.Autofix()
                fix.Warnf(SilentAutofixFormat)
-               fix.ReplaceRegex(`(.)(.*)(.)`, "lriginao", 1) // XXX: the replacement should be "$3$2$1"
+               fix.Replace("original", "lriginao")
                fix.Apply()
        }
 
@@ -300,7 +297,7 @@
 
        fix := lines.Lines[1].Autofix()
        fix.Warnf("Something's wrong here.")
-       fix.ReplaceRegex(`.....`, "XXX", 1)
+       fix.ReplaceAt(0, 0, "line2", "XXX")
        fix.Apply()
 
        fix.Warnf("Since XXX marks are usually not fixed, use TODO instead to draw attention.")
@@ -328,18 +325,20 @@
        line := t.NewLine("Makefile", 14, "Original text")
 
        fix := line.Autofix()
-       fix.Warnf("All-uppercase words should not be used at all.")
-       fix.ReplaceRegex(`\b[A-Z]{3,}\b`, "---censored---", -1)
+       fix.Warnf("The word ABC should not be used.")
+       fix.Replace("ABC", "---censored---")
        fix.Apply()
 
-       // This warning is wrong. This test therefore demonstrates that each
-       // autofix must be properly guarded to only apply when it actually
-       // does something.
+       // This warning is wrong since the actual line doesn't contain the
+       // word ABC.
+       //
+       // This test therefore demonstrates that each autofix must be properly
+       // guarded to only apply when it actually does something.
        //
        // As of November 2019 there is no Rollback method since it was not
        // needed yet.
        t.CheckOutputLines(
-               "WARN: Makefile:14: All-uppercase words should not be used at all.")
+               "WARN: Makefile:14: The word ABC should not be used.")
 }
 
 func (s *Suite) Test_Autofix_Warnf__duplicate(c *check.C) {
@@ -463,7 +462,7 @@
                "\tWhen inserting multiple lines, Apply must be called in-between.",
                "\tOtherwise the changes are not described to the human reader.",
                "")
-       t.CheckEquals(fix.RawText(), "Text\n")
+       t.CheckEquals(fix.RawText(), "before\nText\nafter\n")
 }
 
 func (s *Suite) Test_Autofix_ReplaceAfter__autofix_in_continuation_line(c *check.C) {
@@ -540,7 +539,7 @@
 
        fix.Notef("Replacing text.")
        fix.Replace("l", "L")
-       fix.ReplaceRegex(`i`, "I", 1)
+       fix.ReplaceAt(0, 0, "i", "I")
        fix.Apply()
 
        t.CheckOutputLines(
@@ -552,6 +551,59 @@
                "AUTOFIX: filename:5: Replacing \"i\" with \"I\".")
 }
 
+func (s *Suite) Test_Autofix_ReplaceAfter__replace_once(c *check.C) {
+       t := s.Init(c)
+
+       doTest := func(autofix bool) {



Home | Main Index | Thread Index | Old Index