Skip to content
This repository was archived by the owner on Jan 12, 2026. It is now read-only.

extension type support for unnecessary_constructor_name#4677

Merged
pq merged 4 commits into
mainfrom
unnecessary_constructor_name_extensionTypes
Aug 12, 2023
Merged

extension type support for unnecessary_constructor_name#4677
pq merged 4 commits into
mainfrom
unnecessary_constructor_name_extensionTypes

Conversation

@pq

@pq pq commented Aug 11, 2023

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot added the linter-set-recommended Affects a rule in the recommended Dart rule set label Aug 11, 2023
@coveralls

coveralls commented Aug 11, 2023

Copy link
Copy Markdown

Coverage Status

coverage: 96.549% (+0.006%) from 96.543% when pulling 7333f2b on unnecessary_constructor_name_extensionTypes into 3e07457 on main.

Comment thread lib/src/rules/unnecessary_constructor_name.dart Outdated
@pq pq changed the title extension type support for duplicate_constructor extension type support for unnecessary_constructor_name Aug 11, 2023
if (node.parent is ExtensionTypeDeclaration) return;
var parent = node.parent;
if (parent is ExtensionTypeDeclaration &&
parent.representation.constructorName == null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I still don't understand why this condition exists. If I have

class C {
  C();
  C.new(int x);
}

Then I believe we'll still flag the new in the second constructor. (In other words, I don't think this lint checks to ensure that this is the first and only unnamed constructor.) I don't know why we'd do that for extension types if we're not doing that for classes.

Also, I think we need to visitRepresentationDeclaration in order to catch the use of .new there.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You're effectively checking the primary constructor in visitExtensionTypeDeclaration, though using visitRepresentationDeclaration might be slightly more direct and more general.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're effectively checking the primary constructor in visitExtensionTypeDeclaration, though using visitRepresentationDeclaration might be slightly more direct and more general.

You're right! Better still might even be visitRepresentationConstructorName -- which I hadn't noticed until just now.

I don't know why we'd do that for extension types if we're not doing that for classes.

No good reason except that I'm paying attention now. I overlooked the class case and think it's a false positive that should be fixed (for the same reasons).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In the end, if you think we shouldn't bother suppressing the lint in case a compilation error is being reported, I'm happy to update. Assuming that is the desire behavior, I added a failing test for the class case so we can swing back and fix it later.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As long as it gets fixed everywhere I'm fine with not double reporting.

But doing so might require visiting every kind of declaration that can have constructors so that we can set a flag indicating whether we've already seen an unnamed constructor, so that might change the way we want to handle the primary constructor.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah. That was my thinking too. I didn't want to tackle such a rewrite here so I left a test as a breadcrumb. Happy to rollback the logic for extension types if you'd rather keep the support symmetrical.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No, as long as we're planning on moving in that direction I'm fine with it being in-progress.

@pq pq merged commit 04fb66a into main Aug 12, 2023
@pq pq deleted the unnecessary_constructor_name_extensionTypes branch August 12, 2023 18:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

linter-set-recommended Affects a rule in the recommended Dart rule set

Development

Successfully merging this pull request may close these issues.

unnecessary_constructor_name support for extension types

3 participants