csutils / csdiff

Utilities for processing results of static analyzers, dynamic analyzers, and formal verification tools
GNU General Public License v3.0
18 stars 16 forks source link

Please add support for ShellCheck JSON format #75

Open jamacku opened 2 years ago

jamacku commented 2 years ago

ShellCheck can suggest a possible solution of the reported defect. Unfortunately, this data isn't available in gcc format. It would be nice if csdiff could parse ShellCheck JSON format, which contains possible fix under fix key.

Example:

{
  "comments": [
    {
      "file": "myotherscript",
      "line": 2,
      "endLine": 2,
      "column": 1,
      "endColumn": 2,
      "level": "error",
      "code": 1035,
      "message": "You need a space after the [ and before the ].",
      "fix": null
    },
    {
      "file": "myscript",
      "line": 2,
      "endLine": 2,
      "column": 6,
      "endColumn": 8,
      "level": "warning",
      "code": 2039,
      "message": "In POSIX sh, echo flags are undefined.",
      "fix": null
    },
    {
      "file": "myscript",
      "line": 2,
      "endLine": 2,
      "column": 9,
      "endColumn": 11,
      "level": "info",
      "code": 2086,
      "message": "Double quote to prevent globbing and word splitting.",
      "fix": {
        "replacements": [
          {
            "line": 2,
            "endLine": 2,
            "precedence": 7,
            "insertionPoint": "afterEnd",
            "column": 9,
            "replacement": "\"",
            "endColumn": 9
          },
          {
            "line": 2,
            "endLine": 2,
            "precedence": 7,
            "insertionPoint": "beforeStart",
            "column": 11,
            "replacement": "\"",
            "endColumn": 11
          }
        ]
      }
    }
  ]
}

Having this kind of information would be very useful for:

Thanks for working on this project! :+1:

kdudka commented 2 years ago

I tried to run shellcheck --format=json **/*.sh and the output looked like this (the top-level "comments": was not there):

[
  {
    "file": "tests/test-lib.sh",
    "line": 1,
    "endLine": 1,
    "column": 1,
    "endColumn": 1,
    "level": "error",
    "code": 2148,
    "message": "Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.",
    "fix": null
  },
  {
    "file": "tests/cssort/sync-diff.sh",
    "line": 2,
    "endLine": 2,
    "column": 8,
    "endColumn": 14,
    "level": "warning",
    "code": 2155,
    "message": "Declare and assign separately to avoid masking return values.",
    "fix": null
  },
  {
    "file": "tests/cssort/sync-diff.sh",
    "line": 5,
    "endLine": 5,
    "column": 8,
    "endColumn": 12,
    "level": "info",
    "code": 2086,
    "message": "Double quote to prevent globbing and word splitting.",
    "fix": {
      "replacements": [
        {
          "line": 5,
          "endLine": 5,
          "precedence": 14,
          "insertionPoint": "afterEnd",
          "column": 8,
          "replacement": "\"",
          "endColumn": 8
        },
        {
          "line": 5,
          "endLine": 5,
          "precedence": 14,
          "insertionPoint": "beforeStart",
          "column": 12,
          "replacement": "\"",
          "endColumn": 12
        }
      ]
    }
  }
]
jamacku commented 2 years ago

Hmm, have you tried json1 format, json seems to be marked as legacy.

kdudka commented 2 years ago

json1 works as expected. My bad.

kdudka commented 2 years ago

I have submitted draft pull request #83 that implements the basic support of the JSON format produced by ShellCheck. We yet need to figure out how to represent the information in the fix key such that it works with all the output formats supported by csdiff.