question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add the possibility to ignore some constructors in no-new

See original GitHub issue

What rule do you want to change?

no-new

Does this change cause the rule to produce more or fewer warnings?

  • fewer

How will the change be implemented? (New option, new default behavior, etc.)?

  • By default all side-effect usages of new will be reported, like now
  • You have the possibility to add constructors to an option, which will then be allowed.

Like:

no-new:
  - error
  - ignore:
      - Vue

Please provide some example code that this change will affect:

We often initialize Vue components with this pattern:

import Component from './components/component';

document.addEventListener('DOMContentLoaded', () => {
  const element = document.getElementById('id');
  new Vue({
    el: element,
    components: {
      Component,
    },
    render(createElement) {
      return createElement('component', {
        props: { ...element.dataset },
      });
    },
   })
})

See our open issues here:

What does the rule currently do for this code?

Throw an error. There are several workarounds for this, that are not nice:

  1. add eslint-disable, this really adds up if you have a lot of components.
  2. return the result of new Vue in the function, but this makes no sense, as no function caller is actually using it

What will the rule do after it’s changed?

Throw no new error.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
platinumazurecommented, Jun 25, 2018

@eslint/eslint-team This needs a champion and one more 👍, thanks!

1reaction
platinumazurecommented, Jun 16, 2018

I could get behind this.

Should we support ignoring just constructor identifier names? I feel it could be a bit more flexible if we instead allowed users to specify AST selectors which we would match against the callee nodes of the NewExpression.

Examples with AST selectors:

  • [name='Vue'] (ignores new Vue())
  • [object.name='Backbone'] (ignores new Backbone.foo() for any foo)

But the trade-off is that the rule configuration has a little steeper learning curve.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configure autofac to ignore constructors marked as obsolete
I have added it to my AutofacAnswers project on GitHub. ... AutoFac passes the proxy class type to the constructor finder but these ......
Read more >
c# - Constructors, Ignore Arguments
Builder Pattern is exact solution to this problem. Especially if the combination of optional and required parameters get big, ...
Read more >
Constructor, operator "new" - The Modern JavaScript Tutorial
A new empty object is created and assigned to this . The function body executes. Usually it modifies this , adds new properties...
Read more >
Inheritance and the prototype chain - JavaScript | MDN
All constructor functions in JavaScript have a special property called prototype , which works with the new operator. The reference to the ...
Read more >
Python Class Constructors: Control Your Object Instantiation
Calling the Point() class constructor creates, initializes, and returns a new instance of the class. This instance is then assigned to the point ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found