Skip to content

Rule Change: Avoid highlighting the entire file for max-classes-per-file violations #20968

Description

@Pixel998

What rule do you want to change?

max-classes-per-file

What change do you want to make?

Generate fewer warnings

How do you think the change should be implemented?

A new default behavior

Example code

/*eslint max-classes-per-file: "error"*/

class Foo {}

class Bar {}

What does the rule currently do for this code?

Currently, the rule reports on the Program node, so editors will highlight the entire file. This is noisy because the violation is caused by the presence of an additional class, not by every line in the file.

What will the rule do after it's changed?

Ideally, the rule would report on a smaller, more precise location, such as the class head for the class that exceeds the configured limit. For example, in the code above, the violation could be reported on class B.

Another alternative would be to keep reporting from Program:exit, but use the same effective range that Program had before the v10 range change:

loc: {
    start: node.body[0].loc.start,
    end: node.body.at(-1).loc.end,
}

This would not be as precise as reporting on the class that exceeds the limit, because it would still cover the program body. However, it would avoid including leading/trailing comments in the reported range and would restore behavior closer to v9. For example:

/* leading file comment */
/* eslint-disable max-classes-per-file */

class A {}
class B {}

In v9, this disable comment suppresses the violation. In v10, it does not.

Participation

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

AI acknowledgment

  • I did not use AI to generate this issue report.
  • (If the above is not checked) I have reviewed the AI-generated content before submitting.

Additional comments

No response

Metadata

Metadata

Assignees

Labels

blockedThis change can't be completed until another issue is resolvedbreakingThis change is backwards-incompatibleenhancementThis change enhances an existing feature of ESLintruleRelates to ESLint's core rules

Type

No type

Fields

No fields configured for issues without a type.

Projects

Status
Blocked

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions