crystal-ameba / ameba

A static code analysis tool for Crystal
https://crystal-ameba.github.io
MIT License
524 stars 39 forks source link

Include correction in JSON output #504

Open nobodywasishere opened 9 hours ago

nobodywasishere commented 9 hours ago

It would be really useful for the VS Code extension to be able to correct issues within editor directly instead of needing to go to the terminal to invoke ameba. Depending on how much additional time it takes to compute, it may be worth doing. Something like:

❯ ameba --stdin-filename filename.cr --format json /dev/null | jq
a.try { |i| i.odd? }
{
  "sources": [
    {
      "path": "filename.cr",
      "issues": [
        {
          "rule_name": "Style/VerboseBlock",
          "severity": "Convention",
          "message": "Use short block notation instead: `try(&.odd?)`",
          "location": {
            "line": 1,
            "column": 3
          },
          "end_location": {
            "line": 1,
            "column": 20
          },
          "correction": {
            "location": {
              "line": 1,
              "column": 3
            },
            "end_location": {
              "line": 1,
              "column": 20
            },
            "text": "try(&.odd?)"
          }
        }
      ]
    }
  ],
  "metadata": {
    "ameba_version": "1.6.3",
    "crystal_version": "1.14.0"
  },
  "summary": {
    "target_sources_count": 1,
    "issues_count": 1
  }
}

Which would work perfectly to create a WorkspaceEdit with the replace method.

nobodywasishere commented 8 hours ago

Alternatively if generating the correction takes too long, may be more worth it just to include whether it's correctable via a bool and have a different way of getting / generating the correction after the user has said they want it.

Sija commented 5 hours ago

That's an interesting use-case, I reckon we could think of sth along these lines. Do you know how other tools (in Ruby or JS for instance) are handling this?

nobodywasishere commented 5 hours ago

Having language servers that implement this functionality, as far as I know (see lsp spec).

The full correction could be excluded by default but put behind a flag --include-corrections. Then when the user clicks "Correct issue" or similar code action on hovering over ameba lint, ameba would be run again (just for this file) with that flag set, and the code action would be executed w/ the results from that.