eslint: Rule Change: add option to ignore (stand-alone) class declarations with static initialization block in no-unused-vars

What rule do you want to change?

no-unused-vars

What change do you want to make?

Generate fewer warnings

How do you think the change should be implemented?

A new option

Example code

/* eslint no-unused-vars: "error" */

class ClassWithSIB {
  static {
    // …
  }
}

What does the rule currently do for this code?

‘ClassWithSIB’ is defined but never used. (no-unused-vars)

What will the rule do after it’s changed?

Ignore classes with Static initialization blocks in no-unused-vars

Participation

  • I am willing to submit a pull request to implement this change.

Additional comments

Introduction

SO-SIB-Class (definition for convenience): A stand-alone class with a static initialization block that is not referenced elsewhere

  • SO-SIB-Class is a valid OOP method
  • Changing a SO-SIB-Class to anything else would be undesirable, and contrary to OOP, abstraction & encapsulation
  • Currently, JavaScript class requires a name

    Uncaught SyntaxError: class statement requires a name

no-unused-vars

  • It is my understanding that the purpose of no-unused-vars is to reduce superfluous variables
  • A SO-SIB-Class is not a superfluous variable
  • no-unused-vars correctly reports a SO-SIB-Class as an unused var
  • Although correct in reporting, it necessitates multiple inline overrules which are in themselves superfluous
  • The aim is to introduce an option to eliminate the need for the superfluous inline overrules

Previously

#17764, #17757

About this issue

  • Original URL
  • State: closed
  • Created 7 months ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

You can look at the examples on Static initialization blocks.

class A {
  static field = "static field";
  static {
    console.log(this.field);
  }
}

It looks to me like a simplified example to show what this in static blocks refers to, rather than a recommendation to create classes just to wrap code in static blocks.

In programming languages where all code must be in classes this may be a common practice, but in JavaScript you can just write top-level code and wrap it in a block to isolate variables:

Should a programmer have to abandon a valid method of programming to suit the linter?

I personally think this is an unnecessary and uncommon use of classes in JavaScript. However, I can agree with the reasoning that this code can be valid (doing something it was intended to do) although the class name variable is unused, but is only necessary for the syntax to be valid, so I wouldn’t be opposed to adding this option if other team members support this proposal.