github.com/bgpat/reviewdog@v0.0.0-20230909064023-077e44ca1f66/parser/checkstyle_test.go (about)

     1  package parser
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"google.golang.org/protobuf/encoding/protojson"
    10  )
    11  
    12  func ExampleCheckStyleParser() {
    13  	const sample = `<?xml version="1.0" encoding="utf-8"?><checkstyle version="4.3"><file name="/path/to/file"><error line="1" column="10" severity="error" message="&apos;addOne&apos; is defined but never used. (no-unused-vars)" source="eslint.rules.no-unused-vars" /><error line="2" column="9" severity="error" message="Use the isNaN function to compare with NaN. (use-isnan)" source="eslint.rules.use-isnan" /><error line="3" column="16" severity="error" message="Unexpected space before unary operator &apos;++&apos;. (space-unary-ops)" source="eslint.rules.space-unary-ops" /><error line="3" column="20" severity="warning" message="Missing semicolon. (semi)" source="eslint.rules.semi" /><error line="4" column="12" severity="warning" message="Unnecessary &apos;else&apos; after &apos;return&apos;. (no-else-return)" source="eslint.rules.no-else-return" /><error line="5" column="7" severity="warning" message="Expected indentation of 8 spaces but found 6. (indent)" source="eslint.rules.indent" /><error line="5" column="7" severity="error" message="Expected a return value. (consistent-return)" source="eslint.rules.consistent-return" /><error line="5" column="13" severity="warning" message="Missing semicolon. (semi)" source="eslint.rules.semi" /><error line="7" column="2" severity="error" message="Unnecessary semicolon. (no-extra-semi)" source="eslint.rules.no-extra-semi" /></file></checkstyle>`
    14  
    15  	p := NewCheckStyleParser()
    16  	diagnostics, err := p.Parse(strings.NewReader(sample))
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  	for _, d := range diagnostics {
    21  		rdjson, _ := protojson.MarshalOptions{Indent: "  "}.Marshal(d)
    22  		var out bytes.Buffer
    23  		json.Indent(&out, rdjson, "", "  ")
    24  		fmt.Println(out.String())
    25  	}
    26  	// Output:
    27  	// {
    28  	//   "message": "'addOne' is defined but never used. (no-unused-vars)",
    29  	//   "location": {
    30  	//     "path": "/path/to/file",
    31  	//     "range": {
    32  	//       "start": {
    33  	//         "line": 1,
    34  	//         "column": 10
    35  	//       }
    36  	//     }
    37  	//   },
    38  	//   "severity": "ERROR",
    39  	//   "code": {
    40  	//     "value": "eslint.rules.no-unused-vars"
    41  	//   },
    42  	//   "originalOutput": "/path/to/file:1:10: error: 'addOne' is defined but never used. (no-unused-vars) (eslint.rules.no-unused-vars)"
    43  	// }
    44  	// {
    45  	//   "message": "Use the isNaN function to compare with NaN. (use-isnan)",
    46  	//   "location": {
    47  	//     "path": "/path/to/file",
    48  	//     "range": {
    49  	//       "start": {
    50  	//         "line": 2,
    51  	//         "column": 9
    52  	//       }
    53  	//     }
    54  	//   },
    55  	//   "severity": "ERROR",
    56  	//   "code": {
    57  	//     "value": "eslint.rules.use-isnan"
    58  	//   },
    59  	//   "originalOutput": "/path/to/file:2:9: error: Use the isNaN function to compare with NaN. (use-isnan) (eslint.rules.use-isnan)"
    60  	// }
    61  	// {
    62  	//   "message": "Unexpected space before unary operator '++'. (space-unary-ops)",
    63  	//   "location": {
    64  	//     "path": "/path/to/file",
    65  	//     "range": {
    66  	//       "start": {
    67  	//         "line": 3,
    68  	//         "column": 16
    69  	//       }
    70  	//     }
    71  	//   },
    72  	//   "severity": "ERROR",
    73  	//   "code": {
    74  	//     "value": "eslint.rules.space-unary-ops"
    75  	//   },
    76  	//   "originalOutput": "/path/to/file:3:16: error: Unexpected space before unary operator '++'. (space-unary-ops) (eslint.rules.space-unary-ops)"
    77  	// }
    78  	// {
    79  	//   "message": "Missing semicolon. (semi)",
    80  	//   "location": {
    81  	//     "path": "/path/to/file",
    82  	//     "range": {
    83  	//       "start": {
    84  	//         "line": 3,
    85  	//         "column": 20
    86  	//       }
    87  	//     }
    88  	//   },
    89  	//   "severity": "WARNING",
    90  	//   "code": {
    91  	//     "value": "eslint.rules.semi"
    92  	//   },
    93  	//   "originalOutput": "/path/to/file:3:20: warning: Missing semicolon. (semi) (eslint.rules.semi)"
    94  	// }
    95  	// {
    96  	//   "message": "Unnecessary 'else' after 'return'. (no-else-return)",
    97  	//   "location": {
    98  	//     "path": "/path/to/file",
    99  	//     "range": {
   100  	//       "start": {
   101  	//         "line": 4,
   102  	//         "column": 12
   103  	//       }
   104  	//     }
   105  	//   },
   106  	//   "severity": "WARNING",
   107  	//   "code": {
   108  	//     "value": "eslint.rules.no-else-return"
   109  	//   },
   110  	//   "originalOutput": "/path/to/file:4:12: warning: Unnecessary 'else' after 'return'. (no-else-return) (eslint.rules.no-else-return)"
   111  	// }
   112  	// {
   113  	//   "message": "Expected indentation of 8 spaces but found 6. (indent)",
   114  	//   "location": {
   115  	//     "path": "/path/to/file",
   116  	//     "range": {
   117  	//       "start": {
   118  	//         "line": 5,
   119  	//         "column": 7
   120  	//       }
   121  	//     }
   122  	//   },
   123  	//   "severity": "WARNING",
   124  	//   "code": {
   125  	//     "value": "eslint.rules.indent"
   126  	//   },
   127  	//   "originalOutput": "/path/to/file:5:7: warning: Expected indentation of 8 spaces but found 6. (indent) (eslint.rules.indent)"
   128  	// }
   129  	// {
   130  	//   "message": "Expected a return value. (consistent-return)",
   131  	//   "location": {
   132  	//     "path": "/path/to/file",
   133  	//     "range": {
   134  	//       "start": {
   135  	//         "line": 5,
   136  	//         "column": 7
   137  	//       }
   138  	//     }
   139  	//   },
   140  	//   "severity": "ERROR",
   141  	//   "code": {
   142  	//     "value": "eslint.rules.consistent-return"
   143  	//   },
   144  	//   "originalOutput": "/path/to/file:5:7: error: Expected a return value. (consistent-return) (eslint.rules.consistent-return)"
   145  	// }
   146  	// {
   147  	//   "message": "Missing semicolon. (semi)",
   148  	//   "location": {
   149  	//     "path": "/path/to/file",
   150  	//     "range": {
   151  	//       "start": {
   152  	//         "line": 5,
   153  	//         "column": 13
   154  	//       }
   155  	//     }
   156  	//   },
   157  	//   "severity": "WARNING",
   158  	//   "code": {
   159  	//     "value": "eslint.rules.semi"
   160  	//   },
   161  	//   "originalOutput": "/path/to/file:5:13: warning: Missing semicolon. (semi) (eslint.rules.semi)"
   162  	// }
   163  	// {
   164  	//   "message": "Unnecessary semicolon. (no-extra-semi)",
   165  	//   "location": {
   166  	//     "path": "/path/to/file",
   167  	//     "range": {
   168  	//       "start": {
   169  	//         "line": 7,
   170  	//         "column": 2
   171  	//       }
   172  	//     }
   173  	//   },
   174  	//   "severity": "ERROR",
   175  	//   "code": {
   176  	//     "value": "eslint.rules.no-extra-semi"
   177  	//   },
   178  	//   "originalOutput": "/path/to/file:7:2: error: Unnecessary semicolon. (no-extra-semi) (eslint.rules.no-extra-semi)"
   179  	// }
   180  }