Skip to content

Commit

Permalink
chore: document printers
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jan 30, 2025
1 parent 87da074 commit 2602996
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 114 deletions.
40 changes: 21 additions & 19 deletions pkg/printers/checkstyle.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,8 @@ import (

const defaultCheckstyleSeverity = "error"

type checkstyleOutput struct {
XMLName xml.Name `xml:"checkstyle"`
Version string `xml:"version,attr"`
Files []*checkstyleFile `xml:"file"`
}

type checkstyleFile struct {
Name string `xml:"name,attr"`
Errors []*checkstyleError `xml:"error"`
}

type checkstyleError struct {
Column int `xml:"column,attr"`
Line int `xml:"line,attr"`
Message string `xml:"message,attr"`
Severity string `xml:"severity,attr"`
Source string `xml:"source,attr"`
}

// Checkstyle prints issues in the Checkstyle format.
// https://checkstyle.org/config.html
type Checkstyle struct {
w io.Writer
}
Expand Down Expand Up @@ -93,3 +76,22 @@ func (p Checkstyle) Print(issues []result.Issue) error {

return nil
}

type checkstyleOutput struct {
XMLName xml.Name `xml:"checkstyle"`
Version string `xml:"version,attr"`
Files []*checkstyleFile `xml:"file"`
}

type checkstyleFile struct {
Name string `xml:"name,attr"`
Errors []*checkstyleError `xml:"error"`
}

type checkstyleError struct {
Column int `xml:"column,attr"`
Line int `xml:"line,attr"`
Message string `xml:"message,attr"`
Severity string `xml:"severity,attr"`
Source string `xml:"source,attr"`
}
38 changes: 20 additions & 18 deletions pkg/printers/codeclimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,8 @@ import (

const defaultCodeClimateSeverity = "critical"

// CodeClimateIssue is a subset of the Code Climate spec.
// https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types
// It is just enough to support GitLab CI Code Quality.
// https://docs.gitlab.com/ee/ci/testing/code_quality.html#code-quality-report-format
type CodeClimateIssue struct {
Description string `json:"description"`
CheckName string `json:"check_name"`
Severity string `json:"severity,omitempty"`
Fingerprint string `json:"fingerprint"`
Location struct {
Path string `json:"path"`
Lines struct {
Begin int `json:"begin"`
} `json:"lines"`
} `json:"location"`
}

// CodeClimate prints issues in the Code Climate format.
// https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md
type CodeClimate struct {
w io.Writer

Expand All @@ -44,7 +29,7 @@ func (p CodeClimate) Print(issues []result.Issue) error {
codeClimateIssues := make([]CodeClimateIssue, 0, len(issues))

for i := range issues {
issue := &issues[i]
issue := issues[i]

codeClimateIssue := CodeClimateIssue{}
codeClimateIssue.Description = issue.Description()
Expand All @@ -63,3 +48,20 @@ func (p CodeClimate) Print(issues []result.Issue) error {

return json.NewEncoder(p.w).Encode(codeClimateIssues)
}

// CodeClimateIssue is a subset of the Code Climate spec.
// https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types
// It is just enough to support GitLab CI Code Quality.
// https://docs.gitlab.com/ee/ci/testing/code_quality.html#code-quality-report-format
type CodeClimateIssue struct {
Description string `json:"description"`
CheckName string `json:"check_name"`
Severity string `json:"severity,omitempty"`
Fingerprint string `json:"fingerprint"`
Location struct {
Path string `json:"path"`
Lines struct {
Begin int `json:"begin"`
} `json:"lines"`
} `json:"location"`
}
2 changes: 2 additions & 0 deletions pkg/printers/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ type htmlIssue struct {
Code string
}

// HTML prints issues in an HTML page.
// It uses the Cloudflare CDN (cdnjs) and React.
type HTML struct {
w io.Writer
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/printers/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

// JSON prints issues in a JSON representation.
type JSON struct {
rd *report.Data // TODO(ldez) should be drop in v2. Only use by JSON reporter.
rd *report.Data
w io.Writer
}

Expand Down
60 changes: 32 additions & 28 deletions pkg/printers/junitxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,10 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

type testSuitesXML struct {
XMLName xml.Name `xml:"testsuites"`
TestSuites []testSuiteXML
}

type testSuiteXML struct {
XMLName xml.Name `xml:"testsuite"`
Suite string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Errors int `xml:"errors,attr"`
Failures int `xml:"failures,attr"`
TestCases []testCaseXML `xml:"testcase"`
}

type testCaseXML struct {
Name string `xml:"name,attr"`
ClassName string `xml:"classname,attr"`
Failure failureXML `xml:"failure"`
File string `xml:"file,attr,omitempty"`
Line int `xml:"line,attr,omitempty"`
}

type failureXML struct {
Message string `xml:"message,attr"`
Type string `xml:"type,attr"`
Content string `xml:",cdata"`
}

// JunitXML prints issues in the Junit XML format.
// There is no official specification for the JUnit XML file format,
// and various tools generate and support different flavors of this format.
// https://github.com/testmoapp/junitxml
type JunitXML struct {
extended bool
w io.Writer
Expand Down Expand Up @@ -97,3 +73,31 @@ func (p JunitXML) Print(issues []result.Issue) error {
}
return nil
}

type testSuitesXML struct {
XMLName xml.Name `xml:"testsuites"`
TestSuites []testSuiteXML
}

type testSuiteXML struct {
XMLName xml.Name `xml:"testsuite"`
Suite string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Errors int `xml:"errors,attr"`
Failures int `xml:"failures,attr"`
TestCases []testCaseXML `xml:"testcase"`
}

type testCaseXML struct {
Name string `xml:"name,attr"`
ClassName string `xml:"classname,attr"`
Failure failureXML `xml:"failure"`
File string `xml:"file,attr,omitempty"`
Line int `xml:"line,attr,omitempty"`
}

type failureXML struct {
Message string `xml:"message,attr"`
Type string `xml:"type,attr"`
Content string `xml:",cdata"`
}
97 changes: 50 additions & 47 deletions pkg/printers/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,9 @@ const (
sarifSchemaURI = "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.6.json"
)

type SarifOutput struct {
Version string `json:"version"`
Schema string `json:"$schema"`
Runs []sarifRun `json:"runs"`
}

type sarifRun struct {
Tool sarifTool `json:"tool"`
Results []sarifResult `json:"results"`
}

type sarifTool struct {
Driver struct {
Name string `json:"name"`
} `json:"driver"`
}

type sarifResult struct {
RuleID string `json:"ruleId"`
Level string `json:"level"`
Message sarifMessage `json:"message"`
Locations []sarifLocation `json:"locations"`
}

type sarifMessage struct {
Text string `json:"text"`
}

type sarifLocation struct {
PhysicalLocation sarifPhysicalLocation `json:"physicalLocation"`
}

type sarifPhysicalLocation struct {
ArtifactLocation sarifArtifactLocation `json:"artifactLocation"`
Region sarifRegion `json:"region"`
}

type sarifArtifactLocation struct {
URI string `json:"uri"`
Index int `json:"index"`
}

type sarifRegion struct {
StartLine int `json:"startLine"`
StartColumn int `json:"startColumn"`
}

// Sarif prints issues in the SARIF format.
// https://sarifweb.azurewebsites.net/
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/
type Sarif struct {
w io.Writer
}
Expand Down Expand Up @@ -115,3 +71,50 @@ func (p Sarif) Print(issues []result.Issue) error {

return json.NewEncoder(p.w).Encode(output)
}

type SarifOutput struct {
Version string `json:"version"`
Schema string `json:"$schema"`
Runs []sarifRun `json:"runs"`
}

type sarifRun struct {
Tool sarifTool `json:"tool"`
Results []sarifResult `json:"results"`
}

type sarifTool struct {
Driver struct {
Name string `json:"name"`
} `json:"driver"`
}

type sarifResult struct {
RuleID string `json:"ruleId"`
Level string `json:"level"`
Message sarifMessage `json:"message"`
Locations []sarifLocation `json:"locations"`
}

type sarifMessage struct {
Text string `json:"text"`
}

type sarifLocation struct {
PhysicalLocation sarifPhysicalLocation `json:"physicalLocation"`
}

type sarifPhysicalLocation struct {
ArtifactLocation sarifArtifactLocation `json:"artifactLocation"`
Region sarifRegion `json:"region"`
}

type sarifArtifactLocation struct {
URI string `json:"uri"`
Index int `json:"index"`
}

type sarifRegion struct {
StartLine int `json:"startLine"`
StartColumn int `json:"startColumn"`
}
1 change: 1 addition & 0 deletions pkg/printers/tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

// Tab prints issues using tabulation as field separator.
type Tab struct {
printLinterName bool
useColors bool
Expand Down
3 changes: 2 additions & 1 deletion pkg/printers/teamcity.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const (
largeLimit = 4000
)

// TeamCity printer for TeamCity format.
// TeamCity prints issues in the TeamCity format.
// https://www.jetbrains.com/help/teamcity/service-messages.html
type TeamCity struct {
w io.Writer
escaper *strings.Replacer
Expand Down
1 change: 1 addition & 0 deletions pkg/printers/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

// Text prints issues with a human friendly representation.
type Text struct {
printIssuedLine bool
printLinterName bool
Expand Down

0 comments on commit 2602996

Please sign in to comment.