codeclimate/codeclimate-duplication

How can I add exceptions for code patterns ?

Igor-Lopes opened this issue · 3 comments

Hi,

I have a Node/Express app setup and Code Climate is giving me some duplication issues on files that aren't real duplication issues, since the code is already refactored and I'm only accessing these files.

On the following example I have to access the user model and a response helper lib in order to process the request in my controller. I've loaded these files into my app variable, passed as argument. I don't see how this is a code duplication, since I'm only loading these files and assigning them to variables. Is this a correct behavior ? Is there a way to add code pattern exceptions to ignore such issues ? I'm confused about these issues, I don't see how I can avoid having similar code or another way to access these files.

Here's the code section given as similar to others:

module.exports = (app) => {
  const User = app.models.user
  const responses = app.libs.responses.users
  const controller = {}

And here's the complete code for this file:

const { validationResult } = require('express-validator/check')

module.exports = (app) => {
  const User = app.models.user
  const responses = app.libs.responses.users
  const controller = {}

  controller.getProfile = async (req, res, next) => {
    try {
      validationResult(req).throw()

      let user = await User.findById(req.params.id).lean()

      res.send(responses.getProfile(user))
    } catch (ex) {
      next(ex)
    }
  }

  return controller
}

Hi there, if you are having issues with duplication reporting too many or too little, you can adjust the threshold. For more detailed reference, please see here in the readme.

For your particular issue, I would consider either raising the threshold for how many lines of duplicated block is necessary to count as duplication, or configuring a custom node filter . The first approach is much easier as you can just change the threshold in your codeclimate config file:

checks:
  identical-code:
    config:
      threshold: 25
  similar-code:
    config:
      threshold: 50

but the second approach is probably more accurate as it fits more in the actual issue you're running into.

In fact, in your node filters, I would probably filter anything that's a module.exports as that's just exporting the class and a React way of passing the components around.

I'm going to close this as an issue but feel free to keep commenting if you have any additional questions on setting up the filters!

Thank You :)