AtomLinter / linter-codeclimate

An Atom Linter plugin for the Code Climate CLI
http://github.com/codeclimate/codeclimate
MIT License
10 stars 5 forks source link

CodeClimate JSON Formatter Update Causing Getting Error [Linter] Error running Code Climate TypeError: Cannot convert undefined or null to object #76

Closed a-ali closed 6 years ago

a-ali commented 6 years ago

I'm using CodeClimate version 0.70.3

When running the linter on a ruby file, I got this error on the console [Linter] Error running Code Climate TypeError: Cannot convert undefined or null to object

After some investigation, the root cause was this line in index.js file. It's trying to access issue.location, but the issue object has no location key.

That's the output of running codeclimate -f json /path/to/file.rb

[
  {
    "name":"ruby.parse.succeeded",
    "type":"measurement",
    "value":1,
    "engine_name":"structure"
  },
  {
    "type":"Issue",
    "check_name":"sql_injection",
    "description":"Possible SQL injection",
    "fingerprint":"...",
    "categories":[
      "Security"
    ],
    "severity":"minor",
    "remediation_points":300000,
    "location":{
      "path":"/path/to/file.rb",
      "lines":{
        "begin":23,
        "end":23
      }
    },
    "content":{
      "body":"..."
    },
    "engine_name":"brakeman"
  },
  {
    "type":"Issue",
    "check_name":"Rubocop/Metrics/CyclomaticComplexity",
    "description":"Cyclomatic complexity for load_resources is too high. [8/6]",
    "categories":[
      "Complexity"
    ],
    "remediation_points":1140000,
    "location":{
      "path":"/path/to/file.rb",
      "positions":{
        "begin":{
          "column":3,
          "line":107
        },
        "end":{
          "column":6,
          "line":114
        }
      }
    },
    "content":{
      "body":"..."
    },
    "fingerprint":"...",
    "engine_name":"rubocop",
    "severity":"minor"
  }
]

You can notice that the first element in the output is of type measurments not Issue, this has been recently introduced in codeclimate json formatter.

So as a quick solution, I just added this condition inside the forEach block

if (issue.type !== 'Issue') {
  return;
}

We may also filter the messages before calling forEach.

Do you have a specific guide to create a pull request with this fix?

maxjacobson commented 6 years ago

Great catch! I think your proposal looks correct and I’d be happy to merge a PR that makes that change. Ideally, you would include a test that exercises this logic, but if that proves difficult please open the PR anyway

a-ali commented 6 years ago

Actually, the test was failing with this message

The codeclimate provider for Linter
  it works with a valid .codeclimate.yml file
    Expected promise to be resolved, but it was rejected with: Cannot convert undefined or null to object {  }

But it passes now. Since the test is checking for the messages returned from the linter, not from codeclimate, I couldn't write a scenario for it.

cgalvarez commented 6 years ago

The error reported by the test has to do with the promises timeout. You don't need to write an scenario for it. I describe how to fix this at #78