Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rule for final_defined_class #278

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ostk0069
Copy link
Collaborator

@ostk0069 ostk0069 commented Mar 9, 2025

Overview

add rule final_defined_class

Feature type

  • Lint Rule
  • Quick fix
  • Assist
  • Other (Update docs, Configure CI, etc...)

Checklist

  • I have read the CONTRIBUTING docs.
  • I have written an related issue or linked with existing issues.
  • I have written tests and passed all of them.
    • Use melos test to run tests.
  • I have updated docs (if needed).

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Hello @ostk0069, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request introduces a new lint rule, final_defined_class, to encourage developers to mark classes as final if they are not intended to be extended. This enhances code clarity and prevents unintended inheritance. The PR includes the implementation of the lint rule, a quick fix to automatically add the final keyword, and updates to the documentation.

Highlights

  • New Lint Rule: final_defined_class: The pull request adds a new lint rule that suggests marking classes as final when they are not designed for inheritance. This promotes better code design and prevents unintended extensions.
  • Quick Fix Implementation: A quick fix is included to automatically add the final keyword to class declarations, making it easy for developers to apply the suggestion.
  • Documentation Update: The README.md file is updated to include documentation for the new final_defined_class rule, providing usage examples and configuration details.

Changelog

Click here to see the changelog
  • packages/nilts/README.md
    • Added documentation for the final_defined_class lint rule, including target SDK, rule type, maturity level, and quick fix availability.
    • Included 'bad' and 'good' code examples to illustrate the rule's purpose.
  • packages/nilts/lib/nilts.dart
    • Imported the final_defined_class.dart file.
    • Registered the FinalDefinedClass lint rule, making it available for analysis.
  • packages/nilts/lib/src/lints/final_defined_class.dart
    • Implemented the FinalDefinedClass lint rule, which checks for classes that are not marked as final but could be.
    • Implemented the _AddFinalKeyword fix, which automatically adds the final keyword to a class declaration.
    • The rule skips classes that are already sealed, abstract, final, base, interface, or mixin.
  • packages/nilts_test/test/lints/final_defined_class.dart
    • Added a test case to verify that the final_defined_class lint rule correctly identifies classes that should be marked as final.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Trivia time!

What keyword, introduced in Dart 3.0, restricts class inheritance?

Click here for the answer
The `final` keyword, when applied to a class, prevents it from being extended or implemented.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new lint rule final_defined_class to encourage marking classes as final when they are not intended to be extended. The rule includes a quick fix to automatically add the final keyword. The changes seem well-structured and include necessary documentation and tests.

Merge Readiness

The pull request introduces a new lint rule and associated quick fix, along with documentation and tests. Given the review settings, I have focused on identifying medium, high, and critical severity issues. I did not find any issues at those levels. Therefore, I believe the pull request is in good shape to be merged, pending any further reviews and approvals. I am unable to directly approve the pull request, and other reviewers should examine the code before merging.

@ostk0069 ostk0069 changed the title feat: add rule for final_defined_class feat: add rule for final_defined_class Mar 9, 2025
@ostk0069 ostk0069 marked this pull request as ready for review March 9, 2025 16:49
@ostk0069 ostk0069 requested a review from ronnnnn as a code owner March 9, 2025 16:49
@ostk0069 ostk0069 force-pushed the feat/final_defined_class branch from a25f3af to a1233aa Compare March 9, 2025 16:50
@@ -725,6 +725,36 @@ See also:

</details>

### final_defined_class
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Add the rule in the rule list table.

<details>

<!-- prettier-ignore-start -->
- Target SDK : >= Flutter 3.10.0 (Dart 3.0.0)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- Target SDK : >= Flutter 3.10.0 (Dart 3.0.0)
- Target SDK : Any versions nilts supports

ErrorReporter reporter,
CustomLintContext context,
) {
context.registry.addClassDeclaration((node) {
Copy link
Member

Choose a reason for hiding this comment

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

q
Can you check whether the class is inherited or not?

// should not show lint error
class MyClass {}

// should show lint error
class MySubClass extends MyClass {}

Comment on lines +43 to +50
if (node.sealedKeyword != null ||
node.abstractKeyword != null ||
node.finalKeyword != null ||
node.baseKeyword != null ||
node.interfaceKeyword != null ||
node.mixinKeyword != null) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

imo
You can check simpler.

Suggested change
if (node.sealedKeyword != null ||
node.abstractKeyword != null ||
node.finalKeyword != null ||
node.baseKeyword != null ||
node.interfaceKeyword != null ||
node.mixinKeyword != null) {
return;
}
if (node.classKeyword.previous?.keyword != null) return;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants