Skip to content

Conversation

@xJac0b
Copy link
Contributor

@xJac0b xJac0b commented Jun 5, 2025

Resolves #126

Rule avoid_using_api:

  • Lint only default constructor by using () identifier
  • Lint usage of specific named parameter

@xJac0b xJac0b requested a review from solid-vovabeloded June 5, 2025 14:27
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 @xJac0b, 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

Hello team,

Gemini here, providing a summary of this pull request by @xJac0b.

This PR enhances the avoid_using_api lint rule to provide more granular control over API usage. Specifically, it adds the capability to lint against the use of a class's default constructor and the use of a specific named parameter in method or constructor calls. This addresses issue #126.

The changes involve updating the core linter logic to handle these new cases, extending the configuration model to include a named_parameter field, and updating the rule definition to process this new configuration. Comprehensive tests have been added to cover both new scenarios.

Highlights

  • Enhance avoid_using_api rule: The avoid_using_api lint rule now supports two new types of restrictions: banning the default constructor of a class and banning the usage of a specific named parameter in method or constructor calls.
  • Default Constructor Linting: A new mechanism is introduced to identify and lint the default constructor of a class using the identifier () in the configuration.
  • Named Parameter Linting: The rule can now be configured to flag usages of methods or constructors when they include a specific named parameter in their argument list.
  • Configuration Model Update: The AvoidUsingApiEntryParameters model is updated to include a namedParameter field to support the new linting capability.
  • New Test Cases: New test configurations and source files are added to verify the correct behavior of the default constructor and named parameter linting features.

Changelog

Click here to see the changelog
  • lib/src/lints/avoid_using_api/avoid_using_api_linter.dart
    • Added defaultConstructorIdentifier constant.
    • Modified banUsage to delegate default constructor checks to a new method.
    • Added _banDefaultConstructor method to lint default constructor usage.
    • Added banUsageWithSpecificNamedParameter method to lint usage based on a specific named parameter.
    • Added _hasNamedParameter helper method to check for the presence of a named parameter in an argument list.
  • lib/src/lints/avoid_using_api/avoid_using_api_rule.dart
    • Added a new case to the switch statement to handle the identifier, namedParameter, className, source configuration combination.
    • Calls the new linter.banUsageWithSpecificNamedParameter method for the new configuration.
  • lib/src/lints/avoid_using_api/models/avoid_using_api_entry_parameters.dart
    • Added documentation for the named_parameter field.
    • Added namedParameter field to the AvoidUsingApiEntryParameters class.
    • Added namedParameter to the constructor.
    • Added parsing for named_parameter in the fromJson factory.
  • lint_test/avoid_using_api/external_source/lib/external_source.dart
    • Modified various constructors and methods in BannedCodeUsage and BannedExtension to include named parameters for testing purposes.
    • Added a new named constructor BannedCodeUsage.test5.
  • lint_test/avoid_using_api/identifier_class_source_ban/analysis_options.yaml
    • Added a configuration entry to ban the default constructor () of BannedCodeUsage from package:external_source.
  • lint_test/avoid_using_api/identifier_class_source_ban/lib/identifier_class_source_ban_test.dart
    • Added an expect_lint comment above the usage of the default constructor BannedCodeUsage() to test the new lint.
  • lint_test/avoid_using_api/named_parameter_ban/analysis_options.yaml
    • Added a new test configuration file.
    • Includes multiple entries banning the usage of the parameter1 named parameter for various constructors and methods.
  • lint_test/avoid_using_api/named_parameter_ban/lib/named_parameter_ban_test.dart
    • Added a new test file.
    • Includes various code snippets using the banned named parameter parameter1 with expect_lint comments.
  • lint_test/avoid_using_api/named_parameter_ban/pubspec.yaml
    • Added a new pubspec file for the named parameter test directory.
    • Configured dependencies for the test environment.
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.


A constructor's call,
Or parameter named,
Lint rules stand tall,
Code quality claimed.

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 valuable enhancements to the avoid_using_api rule by adding support for linting default constructors and the usage of specific named parameters. The implementation is largely well-structured, and the new test cases effectively cover the added functionality. I've identified a couple of areas for improvement, one of which is a potential runtime error.

Summary of Findings

  • Potential Runtime Error in _matchesSource: In AvoidUsingApiLinter.banUsageWithSpecificNamedParameter, if enclosingElement.library2 is null, sourcePath becomes ''. This can lead to an error in _matchesSource when trying to access pathSegments.first on a URI with an empty path. An early return if library2 is null is recommended.
  • Test Package Name Inconsistency: The pubspec.yaml for the named_parameter_ban test has its name field set to identifier_class_source_ban. This should likely be changed to named_parameter_ban for consistency.
  • Minor Typo in Comment: In lib/src/lints/avoid_using_api/models/avoid_using_api_entry_parameters.dart (line 39 RIGHT), there's a minor typo in the field comment: "constrcutor" should be "constructor". This was not commented on directly due to review settings (low severity).

Merge Readiness

The pull request introduces significant improvements. However, there is a high-severity issue related to a potential runtime error that should be addressed before merging. Additionally, a medium-severity issue regarding test package naming consistency has been noted. Once the high-severity issue is resolved, this PR will be in much better shape for merging. As a reviewer, I am not authorized to approve pull requests; please ensure further review and approval from authorized maintainers.

Copy link
Collaborator

@solid-vovabeloded solid-vovabeloded left a comment

Choose a reason for hiding this comment

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

Good job, @xJac0b! I've added a couple of suggestions here - please, take a look.

@xJac0b xJac0b force-pushed the issue-126-lint-default-constructor-only branch from 97038d8 to b26b7c2 Compare June 6, 2025 11:13
@xJac0b xJac0b merged commit ee3e27d into master Jun 6, 2025
2 checks passed
@xJac0b xJac0b deleted the issue-126-lint-default-constructor-only branch June 6, 2025 13:43
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.

avoid_using_api: Lint Default Constructor Only

3 participants