errorformat

package module
v0.0.0-...-b91f67e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2019 License: MIT Imports: 8 Imported by: 0

README ΒΆ

errorformat - Vim 'errorformat' implementation in Go

Travis Build Status codecov Go Report Card LICENSE GoDoc

errorformat is Vim's quickfix errorformat implementation in golang.

errorformat provides default errorformats for major tools. You can see defined errorformats here. Also, it's easy to add new errorformat in a similar way to Vim's errorformat.

Note that it's highly compatible with Vim implementation, but it doesn't support Vim regex.

Usage

import "github.com/reviewdog/errorformat"

Example

Code:
in := `
golint.new.go:3:5: exported var V should have comment or be unexported
golint.new.go:5:5: exported var NewError1 should have comment or be unexported
golint.new.go:7:1: comment on exported function F should be of the form "F ..."
golint.new.go:11:1: comment on exported function F2 should be of the form "F2 ..."
`
efm, _ := errorformat.NewErrorformat([]string{`%f:%l:%c: %m`, `%-G%.%#`})
s := efm.NewScanner(strings.NewReader(in))
for s.Scan() {
    fmt.Println(s.Entry())
}
Output:
golint.new.go|3 col 5| exported var V should have comment or be unexported
golint.new.go|5 col 5| exported var NewError1 should have comment or be unexported
golint.new.go|7 col 1| comment on exported function F should be of the form "F ..."
golint.new.go|11 col 1| comment on exported function F2 should be of the form "F2 ..."

CLI tool

Installation
go get -u github.com/reviewdog/errorformat/cmd/errorformat
Usage
Usage: errorformat [flags] [errorformat ...]

errorformat reads compiler/linter/static analyzer result from STDIN, formats
them by given 'errorformat' (90% compatible with Vim's errorformat. :h
errorformat), and outputs formated result to STDOUT.

Example:
        $ echo '/path/to/file:14:28: error message\nfile2:3:4: msg' | errorformat "%f:%l:%c: %m"
        /path/to/file|14 col 28| error message
        file2|3 col 4| msg

        $ golint ./... | errorformat -name=golint

The -f flag specifies an alternate format for the entry, using the
syntax of package template.  The default output is equivalent to -f
'{{.String}}'. The struct being passed to the template is:

        type Entry struct {
                // name of a file
                Filename string
                // line number
                Lnum int
                // column number (first column is 1)
                Col int
                // true: "col" is visual column
                // false: "col" is byte index
                Vcol bool
                // error number
                Nr int
                // search pattern used to locate the error
                Pattern string
                // description of the error
                Text string
                // type of the error, 'E', '1', etc.
                Type rune
                // true: recognized error message
                Valid bool

                // Original error lines (often one line. more than one line for multi-line
                // errorformat. :h errorformat-multi-line)
                Lines []string
        }

Flags:
  -f string
        format template for -w=template (default "{{.String}}")
  -list
        list defined errorformats
  -name string
        defined errorformat name
  -w string
        writer format (template|checkstyle) (default "template")
$ cat testdata/sbt.in
[warn] /path/to/F1.scala:203: local val in method f is never used: (warning smaple 3)
[warn]         val x = 1
[warn]             ^
[warn] /path/to/F1.scala:204: local val in method f is never used: (warning smaple 2)
[warn]   val x = 2
[warn]       ^
[error] /path/to/F2.scala:1093: error: value ++ is not a member of Int
[error]     val x = 1 ++ 2
[error]               ^
[warn] /path/to/dir/F3.scala:83: local val in method f is never used
[warn]         val x = 4
[warn]             ^
[error] /path/to/dir/F3.scala:84: error: value ++ is not a member of Int
[error]         val x = 5 ++ 2
[error]                   ^
[warn] /path/to/dir/F3.scala:86: local val in method f is never used
[warn]         val x = 6
[warn]             ^
$ errorformat "%E[%t%.%+] %f:%l: error: %m" "%A[%t%.%+] %f:%l: %m" "%Z[%.%+] %p^" "%C[%.%+] %.%#" "%-G%.%#" < testdata/sbt.in
/path/to/F1.scala|203 col 13 warning| local val in method f is never used: (warning smaple 3)
/path/to/F1.scala|204 col 7 warning| local val in method f is never used: (warning smaple 2)
/path/to/F2.scala|1093 col 15 error| value &#43;&#43; is not a member of Int
/path/to/dir/F3.scala|83 col 13 warning| local val in method f is never used
/path/to/dir/F3.scala|84 col 19 error| value &#43;&#43; is not a member of Int
/path/to/dir/F3.scala|86 col 13 warning| local val in method f is never used
$ cat fmts/testdata/sbt.in | errorformat -name=sbt -w=checkstyle
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="1.0">
  <file name="/home/haya14busa/src/github.com/reviewdog/errorformat/fmts/testdata/resources/scala/scalac.scala">
    <error column="3" line="6" message="missing argument list for method error in object Predef" severity="error"></error>
    <error column="15" line="4" message="private val in object F is never used" severity="warning"></error>
    <error column="15" line="5" message="private method in object F is never used" severity="warning"></error>
  </file>
</checkstyle>

Use cases of 'errorformat' outside Vim

🐦 Author

haya14busa (https://github.com/haya14busa)

Documentation ΒΆ

Overview ΒΆ

Package errorformat provides 'errorformat' functionality of Vim. :h errorformat

Index ΒΆ

Examples ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type Efm ΒΆ

type Efm struct {
	// contains filtered or unexported fields
}

Efm represents a errorformat.

func NewEfm ΒΆ

func NewEfm(errorformat string) (*Efm, error)

NewEfm converts a 'errorformat' string to regular expression pattern with flags and returns Efm.

quickfix.c: efm_to_regpat

func (*Efm) Match ΒΆ

func (efm *Efm) Match(s string) *Match

Match returns match against given string.

type Entry ΒΆ

type Entry struct {
	// name of a file
	Filename string
	// line number
	Lnum int
	// column number (first column is 1)
	Col int
	// true: "col" is visual column
	// false: "col" is byte index
	Vcol bool
	// error number
	Nr int
	// search pattern used to locate the error
	Pattern string
	// description of the error
	Text string
	// type of the error, 'E', '1', etc.
	Type rune
	// true: recognized error message
	Valid bool

	// Original error lines (often one line. more than one line for multi-line
	// errorformat. :h errorformat-multi-line)
	Lines []string
}

Entry represents matched entry of errorformat, equivalent to Vim's quickfix list item.

func (*Entry) String ΒΆ

func (e *Entry) String() string

|| message /path/to/file|| message /path/to/file|1| message /path/to/file|1 col 14| message /path/to/file|1 col 14 error 8| message {filename}|{lnum}[ col {col}][ {type} [{nr}]]| {text}

func (*Entry) Types ΒΆ

func (e *Entry) Types() string

Types makes a nice message out of the error character and the error number:

qf_types in src/quickfix.c

type Errorformat ΒΆ

type Errorformat struct {
	Efms []*Efm
}

Errorformat provides errorformat feature.

Example ΒΆ
package main

import (
	"fmt"
	"strings"

	"github.com/reviewdog/errorformat"
)

func main() {
	in := `
golint.new.go:3:5: exported var V should have comment or be unexported
golint.new.go:5:5: exported var NewError1 should have comment or be unexported
golint.new.go:7:1: comment on exported function F should be of the form "F ..."
golint.new.go:11:1: comment on exported function F2 should be of the form "F2 ..."
`
	efm, _ := errorformat.NewErrorformat([]string{`%f:%l:%c: %m`, `%-G%.%#`})
	s := efm.NewScanner(strings.NewReader(in))
	for s.Scan() {
		fmt.Println(s.Entry())
	}
}
Output:

golint.new.go|3 col 5| exported var V should have comment or be unexported
golint.new.go|5 col 5| exported var NewError1 should have comment or be unexported
golint.new.go|7 col 1| comment on exported function F should be of the form "F ..."
golint.new.go|11 col 1| comment on exported function F2 should be of the form "F2 ..."

func NewErrorformat ΒΆ

func NewErrorformat(efms []string) (*Errorformat, error)

NewErrorformat compiles given errorformats string (efms) and returns a new Errorformat. It returns error if the errorformat is invalid.

func (*Errorformat) NewScanner ΒΆ

func (errorformat *Errorformat) NewScanner(r io.Reader) *Scanner

NewScanner returns a new Scanner to read from r.

type Match ΒΆ

type Match struct {
	F string // (%f) file name
	N int    // (%n) error number
	L int    // (%l) line number
	C int    // (%c) column number
	T byte   // (%t) error type
	M string // (%m) error message
	R string // (%r) the "rest" of a single-line file message
	P string // (%p) pointer line
	V int    // (%v) virtual column number
	S string // (%s) search text
}

Match represents match of Efm. ref: Basic items in :h errorformat

type Scanner ΒΆ

type Scanner struct {
	*Errorformat
	// contains filtered or unexported fields
}

Scanner provides a interface for scanning compiler/linter/static analyzer result using Errorformat.

func (*Scanner) Entry ΒΆ

func (s *Scanner) Entry() *Entry

Entry returns the most recent entry generated by a call to Scan.

func (*Scanner) Scan ΒΆ

func (s *Scanner) Scan() bool

Scan advances the Scanner to the next entry matched with errorformat, which will then be available through the Entry method. It returns false when the scan stops by reaching the end of the input.

Directories ΒΆ

Path Synopsis
cmd
Package fmts holds defined errorformats.
Package fmts holds defined errorformats.
Package writer provides error result writers.
Package writer provides error result writers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL