Skip to content

proposal: unnecessary_library_directive #58870

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

Closed
5 tasks done
kevmoo opened this issue Sep 14, 2022 · 6 comments · Fixed by dart-archive/linter#3791
Closed
5 tasks done

proposal: unnecessary_library_directive #58870

kevmoo opened this issue Sep 14, 2022 · 6 comments · Fixed by dart-archive/linter#3791
Labels

Comments

@kevmoo
Copy link
Member

kevmoo commented Sep 14, 2022

unnecessary_library_directive

Description

The library directive at the top of...libraries...is generally superfluous – UNLESS it's used to "anchor" a doc comment or an annotation (see #58871).

The use of the "naked" library directives should be avoided.

Details

Not much more than the description.

Kind

Style

Good Examples

// Notice no library directive – not needed!

void main() {}
// Doc comments and/or annotations are a good reason to still use the library directive

/// This is a cool test library
@TestOn('vm')
library; // assuming https://github.com/dart-lang/language/issues/1073 lands with support for no-name library directives

void main() {...}

Bad Examples

// The library directive accomplishes NOTHING!

library pointless.library.here;

void main() {}

Discussion

See dart-lang/language#1073 for the no-name library directive proposal.

Would "conflict" with the following lints. No point in enforcing conventions for names when we want to avoid library directives!

This could likely be added to Effective Dart, I think.

Discussion checklist

  • List any existing rules this proposal modifies, complements, overlaps or conflicts with.
  • List any relevant issues (reported here, the SDK Tracker, or elsewhere).
  • If there's any prior art (e.g., in other linters), please add references here.
  • If this proposal corresponds to Effective Dart or Flutter Style Guide advice, please call it out. (If there isn’t any corresponding advice, should there be?)
  • If this proposal is motivated by real-world examples, please provide as many details as you can. Demonstrating potential impact is especially valuable.
@bwilkerson
Copy link
Member

The lint should not flag library directives if the library has parts that use the library name in the part of directive (but the use of a URI in the part of directive shouldn't prevent the lint from firing).

@kevmoo
Copy link
Member Author

kevmoo commented Sep 14, 2022

The lint should not flag library directives if the library has parts that use the library name in the part of directive (but the use of a URI in the part of directive shouldn't prevent the lint from firing).

Although folks should NOT be using parts with library names anymore, too – right?

RE https://dart-lang.github.io/linter/lints/use_string_in_part_of_directives.html

@lrhn
Copy link
Member

lrhn commented Oct 7, 2022

A library directive is needed if:

  • It has metadata
  • It has doc-comments
  • it has a name, and a part which uses part of with that name. (If they don't use the use_string_in_part_of_directives lint, they can do that. If they use that lint, they'll probably stop having such a part of and this item stops applying.)
  • it has a name and someone uses dart:mirrors.

The last point is undecidable, so basically a library declaration with a name cannot be considered guaranteed unnecessary.

I'd still be fine with linting the declaration if the only use is (potentially) using dart:mirrors. Then you can add the // ignore and a comment saying why. Using dart:mirrors to look up a library by name is obscure enough that it needs commentary.

Separately, the library name itself can be unnecessary.
A @foo library foo.bar.baz; can still remove the name, even if the library declaration is not unnecessary. Is that a separate lint already, one we'd want, or could it be part of this one ("all unnecessary things about library declarations")?

@kevmoo
Copy link
Member Author

kevmoo commented Oct 26, 2022

FYI: the associated language feature has LANDED for Dart 1.19!

@kevmoo
Copy link
Member Author

kevmoo commented Oct 26, 2022

I've made a stab at implementing this – feedback welcome! dart-archive/linter#3791

@kevmoo kevmoo changed the title proposal: avoid_library_directive proposal: unnecessary_library_directive Oct 26, 2022
@pq
Copy link
Member

pq commented Nov 15, 2022

Implemented w/ dart-archive/linter#3791 🎉

@pq pq closed this as completed Nov 15, 2022
@devoncarew devoncarew added devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. labels Nov 19, 2024
@devoncarew devoncarew transferred this issue from dart-archive/linter Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants