standard: rule new-cap problem

Hey, i use the package hapijs/boom for server errors and all the methods are with camelCase for example new Boom.notImplemented() standard is throwing an error

A constructor name should not start with a lowercase letter  new-cap

but i can’t disable each line individually, is there a way to disable all new-cap warnings? or maybe fix the rule so it won’t throw an error?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 19 (9 by maintainers)

Most upvoted comments

Missed answering to the previous comment, so here goes: I still think that that isn’t used too much, wouldn’t one do something like this in those cases? 🤔

const Foobar = require('foobar').default

const blah = new Foobar()

You can do it by adding a comment like // eslint-disable new-cap to the end of the line

Maybe we could make an exception for .default 🤔

@LinusU Agreed. Let’s not add an exception for this. If others can raise valid reasons, happy to reconsider.

I personally still feel that you should always reassign it to a variable with a capitalised name, but I don’t feel too strongly about it…

const Foo = require('foo').default

new Foo()
import('./components/my-component').then(module => {
  const DyamicLoadedClass = module.default

  new DyamicLoadedClass()
})
import('./components/my-component').then(({ default: DyamicLoadedClass }) => {
  new DyamicLoadedClass()
})

It’s always better to always assign the name default to a name with a capitalized first letter before calling the constructor. It should always be possible to do this. For example, @jkga’s code sample can be modified like this:

return import('./components/my-component').then(dyamicLoadedClass => { 
      const MyClass = dyamicLoadedClass.default
      return new MyClass()
      ...
})

Is there some reason that I’m missing why this won’t work? @LinusU do you have thoughts?

We could change the rule to this:

"new-cap": ["error", { "newIsCap": true, "capIsNew": false, "properties": true, "newIsCapExceptions": ["default"] }]

which would allow the original code sample to work:

return import('./components/my-component').then(dyamicLoadedClass => { 
      return new dyamicLoadedClass.default()
      ...
})

But can someone make an argument for a situation where this is more desirable?

Try with // eslint-disable-line new-cap at the end of the line or // eslint-disable-next-line new-cap one line above the code you want eslint to disable

@LinusU but its also being used with dynamic import

return import('./components/my-component').then(dyamicLoadedClass => { 
      return new dyamicLoadedClass.default()
      ...
})