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

New Lint: avoid_using_api #89

Merged

Conversation

getBoolean
Copy link
Contributor

@getBoolean getBoolean commented Nov 28, 2023

Closes #81

This lint has a lot of edge cases. Tests are included to verify basic functionality.

Packages Added:

  • glob: Determines which files an entry should apply to (better than Regex for paths)
  • yaml: To convert YamlMap to Map for the list of entries

Usage

External code can be "deprecated" when there is a better option available:

custom_lint:
  rules:
    - avoid_using_api:
      severity: info
      entries:
        - class_name: Future
          identifier: wait
          source: dart:async
          reason: "Future.wait should be avoided because it loses type safety for the results. Use a Record's `wait` method instead."
          severity: warning

Result:

void main() async {
  await Future.wait([...]); // LINT
  await (...,).wait; // OK
}

Advanced Usage

Each entry also has includes and excludes parameters. These paths utilize Glob patterns to determine if a lint entry should be applied to a file.

For example, a lint to prevent usage of a domain-only package outside of the domain folder:

custom_lint:
  rules:
    - avoid_using_api:
      severity: info
      entries:
        - source: package:modddels
          excludes:
            - "**/domain/**.dart"
          reason: "MODDDELS is only intended to be used in the domain layer."

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! I've added a couple of suggestions here - please take a look.

example/analysis_options.yaml Outdated Show resolved Hide resolved
lib/utils/parameter_utils.dart Outdated Show resolved Hide resolved
lib/utils/parameter_utils.dart Outdated Show resolved Hide resolved
lint_test/banned_external_code/class_ban/pubspec.yaml Outdated Show resolved Hide resolved
@getBoolean getBoolean changed the title New Lint: banned_external_code New Lint: avoid_using_api Dec 20, 2023
@getBoolean
Copy link
Contributor Author

I think that's everything!

I do think documentation is needed, it will be very useful to know what all the lints are and what triggers them. Documentation for lints options can be included alongside that.

Copy link
Collaborator

@yurii-prykhodko-solid yurii-prykhodko-solid left a comment

Choose a reason for hiding this comment

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

Looks good overall, great piece of work, @getBoolean!

Let's make the CI happy and we can merge.

@getBoolean
Copy link
Contributor Author

I hope that is everything.

Copy link
Collaborator

@yurii-prykhodko-solid yurii-prykhodko-solid left a comment

Choose a reason for hiding this comment

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

Outstanding piece of work, @getBoolean!

@illia-romanenko
Copy link
Collaborator

Nice! Our CI still fails - it feels like we need to fix those issues before merging?

@getBoolean
Copy link
Contributor Author

Not sure why its failing, I'll look into it when I get home from vacation in a week.

@getBoolean
Copy link
Contributor Author

I've confirmed it working correctly on Windows and MacOS. It might be an issue somewhere in AvoidUsingApiLinter._matchesSource on Linux or CI causing the source check to fail.

@getBoolean
Copy link
Contributor Author

getBoolean commented Jan 6, 2024

Good thing I setup Windows/Linux dual boot already, made tracking down the issue a lot easier.

The issue was that node.sourceUrl would return the filepath on Linux for project files, while it was always the package path on Windows/MacOS. In the documentation, maybe we should specifically state that doing source: package:myapp will not work on Linux and should be avoided. I updated the lint_test code to move the banned code to another package.

I also found two edge cases while debugging and fixed them

  • lint for method usage on variable of a banned class type (source was previously ignored for it)
  • lint for variable declaration of a banned class type if the type was inferred.

@yurii-prykhodko-solid
Copy link
Collaborator

yurii-prykhodko-solid commented Jan 8, 2024

In the documentation, maybe we should specifically state that doing source: package:myapp will not work on Linux and should be avoided.

Which makes sense -- that's what 'Deprecated' is for.

Let's add this as a note in code documentation, I think the AvoidUsingApiEntryParameters docs are the right place.

@yurii-prykhodko-solid
Copy link
Collaborator

Let's add this as a note in code documentation, I think the AvoidUsingApiEntryParameters docs are the right place.

Done.

@getBoolean Let me know if you're happy with my changes, then we can finish this up.

@yurii-prykhodko-solid
Copy link
Collaborator

@getBoolean Looks like there's some more issues -- CI still in the red.

@getBoolean
Copy link
Contributor Author

Before we merge, I want to investigate making _matchesSource take a non nullable parent source url, it was hiding a lint issue before.

@yurii-prykhodko-solid it looks like pub get needs to be run on those test packages in CI, is that possible?

@yurii-prykhodko-solid
Copy link
Collaborator

yurii-prykhodko-solid commented Jan 9, 2024

@yurii-prykhodko-solid it looks like pub get needs to be run on those test packages in CI, is that possible?

Sure -- have a stab at our CI file. A find -> exec or a find -> for should do the trick.

UPD: Happened to need this on a different project, CI now fetches dependencies for all pubspecs it can find.

This reverses the behavior: now if parent source if not found, it will not lint the node
@getBoolean
Copy link
Contributor Author

Thanks, I think that is everything now

@yurii-prykhodko-solid yurii-prykhodko-solid merged commit 7833cff into solid-software:master Jan 17, 2024
1 check passed
@getBoolean getBoolean deleted the feature/banned_code_lint branch January 20, 2024 03:44
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 Rule
5 participants