Skip to content

fix(linter/no-extraneous-class): improve docs, reporting and code refactor#10797

Merged
graphite-app[bot] merged 1 commit intomainfrom
don/05-04-fix_linter/no-extraneous-class_allow_empty_decorated_classes_by_default
May 9, 2025
Merged

fix(linter/no-extraneous-class): improve docs, reporting and code refactor#10797
graphite-app[bot] merged 1 commit intomainfrom
don/05-04-fix_linter/no-extraneous-class_allow_empty_decorated_classes_by_default

Conversation

@DonIsaac
Copy link
Contributor

@DonIsaac DonIsaac commented May 4, 2025

What This PR Does

Deviates from @typescript-eslint/no-extraneous-class by allowing empty, decorated classes by default.

This provides a better experience for NestJS user using Oxlint without a config file. In NestJS, Modules are often empty but use decorators to declaratively state their contents. Other similar frameworks (TSeD, maybe Angular) will also benefit.

Other Changes

  • Fix span on empty class diagnostics to not cover decorators.
  • Provide help messages on all diagnostics
  • Add missing links and rich text stuff to docs

@github-actions github-actions bot added A-linter Area - Linter C-bug Category - Bug labels May 4, 2025
Copy link
Contributor Author

DonIsaac commented May 4, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@DonIsaac DonIsaac marked this pull request as ready for review May 4, 2025 20:09
@coderabbitai
Copy link

coderabbitai bot commented May 4, 2025

Walkthrough

The changes enhance the NoExtraneousClass lint rule in the TypeScript linter module. The rule's configuration struct was augmented with JsonSchema derivation and serde attributes to support camelCase renaming and default values. An explicit Default implementation was added, setting allowWithDecorator to true by default. Documentation comments were reformatted and expanded with detailed explanations emphasizing the rationale against extraneous classes.

Diagnostic helper functions were updated to accept additional context, enabling conditional help messages for empty classes with decorators and tailored suggestions for classes containing only static members or only a constructor. The diagnostic span for empty classes was refined to exclude decorator spans by adjusting the span start position, improving diagnostic accuracy. The rule logic was modified to skip classes with superclasses or decorated classes when the allowWithDecorator option is enabled.

Test cases were reorganized, cleaned up, and expanded to cover new scenarios such as abstract classes and decorated classes with large decorators. The tests were updated to align with the new default configuration and improved formatting. No public trait methods or exported functions were removed or fundamentally changed.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a72b7c7 and 5a68c5e.

⛔ Files ignored due to path filters (1)
  • crates/oxc_linter/src/snapshots/typescript_no_extraneous_class.snap is excluded by !**/*.snap
📒 Files selected for processing (1)
  • crates/oxc_linter/src/rules/typescript/no_extraneous_class.rs (13 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: Benchmark (formatter)
  • GitHub Check: Benchmark (codegen)
  • GitHub Check: Benchmark (minifier)
  • GitHub Check: Benchmark (semantic)
  • GitHub Check: Benchmark (isolated_declarations)
  • GitHub Check: Benchmark (transformer)
  • GitHub Check: Benchmark (parser)
  • GitHub Check: Benchmark (lexer)
  • GitHub Check: Build Linter Benchmark
  • GitHub Check: Clippy
  • GitHub Check: Test Linux
🔇 Additional comments (15)
crates/oxc_linter/src/rules/typescript/no_extraneous_class.rs (15)

8-8: Adding JsonSchema support - good improvement

Adding JsonSchema support enables better schema generation for the rule configuration.


12-13: Clean serialization configuration

The JsonSchema derivation and serde attributes improve the configuration interface, making it consistent with camelCase naming expected in JavaScript/TypeScript linting tools.


21-30: Allowing decorated empty classes by default is a good UX improvement

This change aligns with the PR objectives to improve developer experience for frameworks like NestJS where empty decorated classes are common patterns. Setting allow_with_decorator: true by default is a sensible choice.


35-61: Documentation improvements provide better context

The expanded documentation nicely explains the rationale against extraneous classes with clear bullet points covering cognitive complexity, IDE limitations, and static analysis benefits.


86-95: Enhanced diagnostic with conditional help message

Good improvement to provide context-aware help messages based on whether decorators are present.


97-101: Helpful suggestion for static-only classes

Adding a help message that suggests using standalone functions instead of static methods provides better guidance for users.


103-107: Improved guidance for constructor-only classes

The help message clearly explains the alternatives to constructor-only classes.


131-131: Default value consistency

This change correctly aligns the configuration parsing with the new default implementation.


161-161: Good use of diagnostic context

Passing the decorator information to generate appropriate diagnostics improves user experience.


184-184: Clean import improvement

Using serde_json::json directly is cleaner than the macro syntax.


208-210: Concise test cases

These concise test cases are well-structured and clearly demonstrate the rule's behavior.


232-233: Tests validate the default behavior change

Good addition of test cases to verify that allowWithDecorator is on by default and can be explicitly enabled.


247-248: Good test coverage for abstract classes

Adding test cases for abstract classes with properties and methods ensures the rule handles these cases correctly.


290-294: Test case for disabling allowWithDecorator

This test verifies that decorated empty classes can be disallowed by explicitly setting allowWithDecorator: false.


318-320: Comprehensive test cases for abstract classes

These test cases ensure that abstract classes are properly linted according to the rule's configuration.


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
crates/oxc_linter/src/rules/typescript/no_extraneous_class.rs (2)

12-19: Consider deriving Serialize/Deserialize and relying on Serde for config parsing

Now that the struct carries both a JsonSchema derive and Serde container attributes, hand-rolling the JSON lookup logic below is unnecessary and brittle (every new field requires touching two places).
You could instead do:

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", default)]
pub struct NoExtraneousClass {
    pub allow_constructor_only: bool,
    pub allow_empty: bool,
    pub allow_static_only: bool,
    pub allow_with_decorator: bool,
}

impl Default for NoExtraneousClass {
    fn default() -> Self { Self { /* … */ } }
}

and then in from_configuration simply:

serde_json::from_value(value.get(0).cloned().unwrap_or_default())
    .unwrap_or_default()

This removes repetitive look-ups and guarantees that the schema, default impl and runtime parsing stay in sync.

Also applies to: 21-30


131-132: Minor consistency nit:

The hard-coded default unwrap_or(true) mirrors the Default impl, but once Serde-based parsing (see first comment) is adopted, this duplication disappears.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e607427 and 6406c27.

⛔ Files ignored due to path filters (1)
  • crates/oxc_linter/src/snapshots/typescript_no_extraneous_class.snap is excluded by !**/*.snap
📒 Files selected for processing (1)
  • crates/oxc_linter/src/rules/typescript/no_extraneous_class.rs (13 hunks)
🧰 Additional context used
🪛 GitHub Actions: CI
crates/oxc_linter/src/rules/typescript/no_extraneous_class.rs

[error] 153-153: Clippy lint 'cast_possible_truncation': casting usize to u32 may truncate the value on 64-bit targets. Use #[allow(clippy::cast_possible_truncation)] to allow or use try_from and handle the error.

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Benchmark linter (1)
  • GitHub Check: Benchmark linter (0)
🔇 Additional comments (1)
crates/oxc_linter/src/rules/typescript/no_extraneous_class.rs (1)

98-107: Nice usability improvement!

Adding concrete “how to fix” help messages markedly improves DX.
No issues spotted here.

@codspeed-hq
Copy link

codspeed-hq bot commented May 4, 2025

CodSpeed Instrumentation Performance Report

Merging #10797 will not alter performance

Comparing don/05-04-fix_linter/no-extraneous-class_allow_empty_decorated_classes_by_default (82889ae) with main (b215b6c)

Summary

✅ 36 untouched benchmarks

@DonIsaac DonIsaac force-pushed the don/05-04-fix_linter/no-extraneous-class_allow_empty_decorated_classes_by_default branch 3 times, most recently from a72b7c7 to 5a68c5e Compare May 5, 2025 06:15
@Sysix
Copy link
Member

Sysix commented May 5, 2025

I do not like that we are changing the default values.
Users expect the migrated rules to work like the native rules.
oxlint-migrate needs to handle this case too, if we want to change this :/

@camc314 camc314 added the 0-merge Merge with Graphite Merge Queue label May 9, 2025
Copy link
Contributor

camc314 commented May 9, 2025

Merge activity

@camc314 camc314 changed the title fix(linter/no-extraneous-class): allow empty decorated classes by default fix(linter/no-extraneous-class): improve docs, reporting and code refactor May 9, 2025
…actor (#10797)

## What This PR Does
Deviates from `@typescript-eslint/no-extraneous-class` by allowing empty, decorated classes by default.

This provides a better experience for NestJS user using Oxlint without a config file. In NestJS, [`Module`s are often empty but use decorators to declaratively state their contents](https://docs.nestjs.com/modules#feature-modules). Other similar frameworks (TSeD, maybe Angular) will also benefit.

### Other Changes
- Fix span on empty class diagnostics to not cover decorators.
- Provide help messages on all diagnostics
- Add missing links and rich text stuff to docs
@graphite-app graphite-app bot force-pushed the don/05-04-fix_linter/no-extraneous-class_allow_empty_decorated_classes_by_default branch from e320c29 to 82889ae Compare May 9, 2025 13:37
@graphite-app graphite-app bot merged commit 82889ae into main May 9, 2025
23 checks passed
@graphite-app graphite-app bot deleted the don/05-04-fix_linter/no-extraneous-class_allow_empty_decorated_classes_by_default branch May 9, 2025 13:40
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label May 9, 2025
camc314 added a commit that referenced this pull request May 16, 2025
## [0.16.11] - 2025-05-16

### Features

- 078bf0b language_server: Better fallback handling when passing invalid
`Options` values (#10930) (Sysix)
- be7f7e1 language_server/editor: Support multi workspace folders
(#10875) (Sysix)
- eef93b4 linter: Add import/no-unassigned-import (#10970) (yefan)
- cc0112f linter: No-unused-vars add setting for
`reportVarsOnlyUsedAsTypes` (#11009) (camc314)
- 17e49c3 linter: Implement configuration and checking loops for
`eslint/no_constant_condition` (#10949) (Ulrich Stark)
- 21117ac linter: Implement react/forbid-elements (#10928) (Thomas
BOCQUEZ)
- 466c24a linter: Add gitlab reporter output format (#10927) (Connor
Pearson)
- a064082 linter: Add import/consistent-type-specifier-style rule
(#10858) (yefan)
- 4733b52 linter/no-extraneous-class: Add conditional fixer (#10798)
(DonIsaac)

### Bug Fixes

- 87bf2a8 editor: Send only `workspace/didChangeConfiguration` when some
workspace configuration is effected (#11017) (Sysix)
- ed5708d editor: Detect all workspaces config path changes (#11016)
(Sysix)
- 89cc21b language_server: Normalize oxlintrc config path (#10982)
(Sysix)
- c52a9ba linter: Fix plugins inside overrides not being applied
(#11057) (camc314)
- b12bd48 linter: Fix rule config not being correctly applied (#11055)
(camc314)
- 9a368be linter: False negative in no-restriced-imports with `patterns`
and side effects (#11027) (camc314)
- 8c2cfbc linter: False negative in no-restricted-imports (#11026)
(camc314)
- 8956870 linter: False positive in no-unused-vars (#11002) (camc314)
- 33a60d2 linter: Skip eslint/no-redeclare when running on modules
(#11004) (camc314)
- 39063ce linter: Reword diagnostic message for no-control-regex
(#10993) (camc314)
- 9eedb58 linter: False positive with negative matches in
no-restricted-imports (#10976) (camc314)
- 10e77d7 linter: Improve diagnostics for no-control-regex (#10959)
(camc314)
- 0961296 linter: Add `gitlab` to linter `--help` docs (#10932)
(camc314)
- 82889ae linter/no-extraneous-class: Improve docs, reporting and code
refactor (#10797) (DonIsaac)
- 11c34e7 linter/no-img-element: Improve diagnostic and docs (#10908)
(DonIsaac)
- 584d8b9 napi: Enable mimalloc `no_opt_arch` feature on linux aarch64
(#11053) (Boshen)
- 126ae75 semantic: Distinguish class private elements (#11044)
(magic-akari)
- 773d0de semantic: Correctly handle nested brackets in jsdoc parsing
(#10922) (camc314)
- b215b6c semantic: Dont parse `@` as jsdoc tags inside `[`/`]` (#10919)
(camc314)

### Documentation

- db6afb9 linter: Improve docs of no-debugger (#11033) (camc314)
- 16541de linter: Improve docs of default-param-last (#11032) (camc314)
- 2c2f3c4 linter: Improve docs of default-case-last (#11031) (camc314)
- 56bb9ce linter: Improve docs of array-callback-return (#11030)
(camc314)
- 13dbcc6 linter: Correct docs for default config for no-redeclare
(#10995) (camc314)
- a86cbb3 linter: Fix incorrect backticks of fenced code blocks (#10947)
(Ulrich Stark)

### Refactor

- 3cc1466 language_server: New configuration structure for `initialize`
and `workspace/didChangeConfiguration` (#10890) (Sysix)
- bd2ef7d language_server: Use `Arc` for `diagnostic_report_map`
(#10940) (Sysix)
- bb999a3 language_server: Avoid cloning linter by taking reference in
LintService (#10907) (Ulrich Stark)
- d1b0c83 linter: Remove overrides index vec (#11058) (camc314)
- 7ad6cf8 linter: Store severity separately, remove `RuleWithSeverity`
(#11051) (camchenry)
- e31c361 linter: Remove nested match statements in
no-restricted-imports (#10975) (camc314)
- 6ad9d4f linter: Tidy `eslint/func-names` (#10923) (camc314)
- faf0a95 syntax: Rename `NameSpaceModule` to `NamespaceModule` (#10917)
(Dunqing)

### Testing

- 76b6b33 editor: Add tests for multi workspace folder setup (#10904)
(Sysix)

Co-authored-by: camc314 <18101008+camc314@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-linter Area - Linter C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants