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

How does a Dart fix get associated with a Linter rule? #47585

Open
domesticmouse opened this issue Nov 1, 2021 · 6 comments
Open

How does a Dart fix get associated with a Linter rule? #47585

domesticmouse opened this issue Nov 1, 2021 · 6 comments
Labels
analyzer-linter Issues with the analyzer's support for the linter package analyzer-quick-fix area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-documentation A request to add or improve documentation

Comments

@domesticmouse
Copy link
Member

Reading through the quick_fix documentation I'm left confused as to how a Dart fix is associated with a Linter rule. I'm guessing the Lint rule name has to be added to lib/src/services/linter/lint_names.dart, at least for testing purposes. It's probably worth explaining in the quick_fix documentation what the linkage is, and how it works.

I'm also confused as to why the linter package entry in pubspec.yaml is any. Shouldn't it be incremented to the version of the Linter package that contains all the lint rules listed in lib/src/services/linter/lint_names.dart?

@domesticmouse
Copy link
Member Author

/cc @bwilkerson

@bwilkerson
Copy link
Member

Reading through the quick_fix documentation I'm left confused as to how a Dart fix is associated with a Linter rule.

I'll update the doc later, but to unblock you...

The association is made in the lintProducerMap in FixProcessor.

I'm guessing the Lint rule name has to be added to lib/src/services/linter/lint_names.dart, at least for testing purposes.

It isn't necessary to define the lint name there, but it is the convention we follow.

I'm also confused as to why the linter package entry in pubspec.yaml is any.

I assume you mean the pubspec in the analyzer package. For the analyzer built in the sdk, the version constraint is in the DEPS file. For uses of the analyzer as a package, I suspect that it's always been acceptable to use the latest version. We don't let them get out of sync.

@devoncarew devoncarew added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Nov 1, 2021
@pq pq added analyzer-linter Issues with the analyzer's support for the linter package analyzer-quick-fix labels Nov 3, 2021
@srawlins srawlins added type-documentation A request to add or improve documentation P3 A lower priority bug or feature request labels Nov 15, 2021
@FMorschel
Copy link
Contributor

FMorschel commented Aug 30, 2024

Reading through the quick_fix documentation

Does anyone know where that doc got moved to?

@bwilkerson
Copy link
Member

@FMorschel
Copy link
Contributor

Then I believe this is already completed look here:

Registering the fix

Before we can run the test, we need to register the correction producer so that
it can be run.

The list of fixes is computed by a FixProcessor. For each diagnostic passed
to it, it will look up the diagnostic in a table to get a list of the correction
producers it can use to produce fixes. There are three tables:

  • The [lintProducerMap][lintmap] is used for fixes related to lint rules. The
    table is keyed by the name of the lint to which the fix applies.

  • The [nonLintProducerMap][nonlintmap] is used for all other fixes. The table
    is keyed by the ErrorCode associated with the diagnostic.

  • The [nonLintMultiProducerMap][multimap] is used for multi-producers, which
    are described below in "Multi-fix producers".

Actually, the tables contain lists of functions used to create the producers. We
do that so that producers can't accidentally carry state over from one use to
the next. These functions are usually a tear-off of one of the correction
producer's constructors.

The last step is to add your correction producer to the appropriate map. If
you're adding a fix for a lint, then you'd add an entry like

LintNames.could_be_final: [
  AddFinal.new,
],

At this point you should be able to run the test and see it failing.

@bwilkerson
Copy link
Member

That's slightly out of date. The lintProducerMap is now keyed by a LintCode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-linter Issues with the analyzer's support for the linter package analyzer-quick-fix area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-documentation A request to add or improve documentation
Projects
None yet
Development

No branches or pull requests

6 participants