@@ -3,7 +3,11 @@ package formatter
33import (
44"encoding/xml"
55"fmt"
6+ "slices"
7+ "strings"
68
9+ hcl "github.com/hashicorp/hcl/v2"
10+ sdk "github.com/terraform-linters/tflint-plugin-sdk/tflint"
711"github.com/terraform-linters/tflint/tflint"
812)
913
@@ -53,9 +57,38 @@ func (f *Formatter) checkstylePrint(issues tflint.Issues, appErr error, sources
5357}
5458}
5559
60+ for _ , cherr := range f .checkstyleErrors (appErr ) {
61+ filename := cherr .Source
62+ if filename == "" {
63+ filename = applicationErrorSource
64+ }
65+ if file , exists := files [filename ]; exists {
66+ file .Errors = append (file .Errors , cherr )
67+ } else {
68+ files [filename ] = & checkstyleFile {
69+ Name : filename ,
70+ Errors : []* checkstyleError {cherr },
71+ }
72+ }
73+ }
74+
75+ filenames := make ([]string , 0 , len (files ))
76+ for filename := range files {
77+ filenames = append (filenames , filename )
78+ }
79+ slices .SortFunc (filenames , func (a , b string ) int {
80+ if a == applicationErrorSource {
81+ return - 1
82+ }
83+ if b == applicationErrorSource {
84+ return 1
85+ }
86+ return strings .Compare (a , b )
87+ })
88+
5689ret := & checkstyle {}
57- for _ , file := range files {
58- ret .Files = append (ret .Files , file )
90+ for _ , filename := range filenames {
91+ ret .Files = append (ret .Files , files [ filename ] )
5992}
6093
6194out , err := xml .MarshalIndent (ret , "" , " " )
@@ -64,8 +97,26 @@ func (f *Formatter) checkstylePrint(issues tflint.Issues, appErr error, sources
6497}
6598fmt .Fprint (f .Stdout , xml .Header )
6699fmt .Fprint (f .Stdout , string (out ))
100+ }
67101
68- if appErr != nil {
69- f .prettyPrintErrors (appErr , sources , false )
70- }
102+ func (f * Formatter ) checkstyleErrors (err error ) []* checkstyleError {
103+ return mapErrors (err , errorMapper [* checkstyleError ]{
104+ diagnostic : func (diag * hcl.Diagnostic ) * checkstyleError {
105+ return & checkstyleError {
106+ Source : diag .Summary ,
107+ Line : diag .Subject .Start .Line ,
108+ Column : diag .Subject .Start .Column ,
109+ Severity : fromHclSeverity (diag .Severity ),
110+ Message : diag .Detail ,
111+ }
112+ },
113+ error : func (err error ) * checkstyleError {
114+ return & checkstyleError {
115+ Source : applicationErrorSource ,
116+ Severity : toSeverity (sdk .ERROR ),
117+ Message : err .Error (),
118+ Rule : applicationErrorSource ,
119+ }
120+ },
121+ })
71122}
0 commit comments