pugjs / pug-lint

An unopinionated and configurable linter and style checker for Pug
ISC License
228 stars 51 forks source link

JSON reporter #84

Open mrmlnc opened 8 years ago

mrmlnc commented 8 years ago

Maybe add a JSON reporter as in ESLint for easy interaction with the command line utility in plugins?

For example:

module.exports = function(errors) {
  if (errors.length) {
    console.log(errors.map((err) => err.toJSON()));
  }
}

Output:

[ { code: 'PUG:LINT_DISALLOWIDLITERALS',
    msg: 'ID literals must not be used',
    line: 11,
    column: 5,
    filename: 'fixture.jade' },
  { code: 'PUG:LINT_DISALLOWSPACESINSIDEATTRIBUTEBRACKETS',
    msg: 'Illegal space after opening bracket',
    line: 25,
    column: 12,
    filename: 'fixture.jade' },
  { code: 'PUG:LINT_DISALLOWSPACESINSIDEATTRIBUTEBRACKETS',
    msg: 'Illegal space before closing bracket',
    line: 25,
    column: 70,
    filename: 'fixture.jade' },
  { code: 'PUG:LINT_REQUIRECLASSLITERALSBEFOREIDLITERALS',
    msg: 'All class literals must be written before any ID literals',
    line: 11,
    column: 15,
    filename: 'fixture.jade' } ]                      
TimothyGu commented 8 years ago

With #92, this can be achieved with ESLint's JSON formatter:

$ node bin/pug-lint -r node_modules/eslint/lib/formatters/json.js --config t.json a.pug b.pug
[
  {
    "messages": [
      {
        "ruleId": "disallowIdLiterals",
        "severity": 2,
        "message": "ID literals must not be used",
        "line": 1,
        "column": 1,
        "nodeType": null,
        "source": "#id",
        "fatal": false,
        "messageWithSource": "a.pug:1:1\n  > 1| #id\n-------^\n    2| \n\nID literals must not be used",
        "fullSource": "#id\n",
        "pugError": {
          "code": "PUG:LINT_DISALLOWIDLITERALS",
          "msg": "ID literals must not be used",
          "line": 1,
          "column": 1,
          "filename": "a.pug"
        }
      }
    ],
    "filePath": "a.pug",
    "errorCount": 1,
    "warningCount": 0
  },
  {
    "messages": [
      {
        "ruleId": "",
        "severity": 2,
        "message": "Syntax Error: Unexpected character '#'",
        "line": 1,
        "column": 9,
        "nodeType": null,
        "source": "= (afds # sadf)",
        "fatal": false,
        "messageWithSource": "b.pug:1:9\n  > 1| = (afds # sadf)\n---------------^\n    2| \n\nSyntax Error: Unexpected character '#'",
        "fullSource": "= (afds # sadf)\n",
        "pugError": {
          "code": "PUG:SYNTAX_ERROR",
          "msg": "Syntax Error: Unexpected character '#'",
          "line": 1,
          "column": 9,
          "filename": "b.pug"
        }
      }
    ],
    "filePath": "b.pug",
    "errorCount": 1,
    "warningCount": 0
  }
]

(JSON formatted and private properties removed)

The schema of the output is defined by ESLint. The pug-error object is in message.pugError.