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

Implement null_safety rule #2773

Merged
merged 7 commits into from
Nov 9, 2022
Merged

Implement null_safety rule #2773

merged 7 commits into from
Nov 9, 2022

Conversation

asashour
Copy link
Contributor

@asashour asashour commented Jul 16, 2021

Implements the first requirement of #2702

I am not sure how best to process pubspec.yaml

Should the name be sound_null_safety, unsound_null_safety, ...?

@google-cla google-cla bot added the cla: yes label Jul 16, 2021
@coveralls
Copy link

coveralls commented Jul 16, 2021

Coverage Status

Coverage increased (+0.01%) to 95.83% when pulling 4568fe5 on asashour:2702_null_safety into 54b1663 on dart-lang:main.

Copy link
Member

@bwilkerson bwilkerson left a comment

Choose a reason for hiding this comment

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

Thanks!

You should be able to handle the pubspec case by overriding PubspecVisitor getPubspecVisitor() in your subclass of LintRule. I haven't verified that it will give you access to everything you need, but we can extend it if necessary.

class NullSafety extends LintRule implements NodeLintRule {
NullSafety()
: super(
name: 'null_safety',
Copy link
Member

Choose a reason for hiding this comment

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

Although it isn't strictly a requirement, consider naming this with a verb phrase, something like enable_null_safety. We generally try to name lints so that they describe either what should be done or what shouldn't be done.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, it is now enable_null_safety. Another suggestion is ensure_sound_null_safety.

lib/src/rules/null_safety.dart Outdated Show resolved Hide resolved
test_data/integration/null_safety/src/null_safety.dart Outdated Show resolved Hide resolved
@asashour asashour force-pushed the 2702_null_safety branch 3 times, most recently from abd2b6d to eb560a5 Compare July 16, 2021 20:50
@bwilkerson
Copy link
Member

@pq Do we want to merge this now (well, after the conflict is resolved) and risk it being a breaking change to have additional checks, or would it be better to merge this after the next roll into the SDK so that it can't be used yet? A third option would be to mark it as experimental until the pubspec checking has been added. (FYI, Flutter is interested in this lint.)

lib/src/rules/enable_null_safety.dart Outdated Show resolved Hide resolved
lib/src/rules/enable_null_safety.dart Outdated Show resolved Hide resolved
@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
var visitor = _Visitor(this, context);
Copy link
Member

Choose a reason for hiding this comment

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

I don't know how expensive the comment parsing below is but I wonder if it's worth bailing if non-nullability is enabled?

    if (context.isEnabled(Feature.non_nullable)) {
      return;
    }

Copy link
Member

Choose a reason for hiding this comment

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

The comments have already been parsed and are in the AST so it's fairly inexpensive to iterate over them, but it wouldn't hurt to check.

Copy link
Member

Choose a reason for hiding this comment

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

👍

More specifically I mean the cost of the regexps.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for hinting.

if (context.isEnabled(Feature.non_nullable)) {
  return;
}

doesn't work, giving all cases are ok (2 should fail).

On the other hand, there was also a suggestion to do something like:

if (line.replaceAll(' ', '').startsWith('//@dart=')) {
    // parse the version
}

if there is a concern about regExp.

Copy link
Member

Choose a reason for hiding this comment

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

It might not be too bad, given that we only need to look at the comments before the first non-comment token in the file (modulo the corner case below). The performance is then dependent on the length of the file header and library documentation comments.

@srawlins
Copy link
Member

I think it would be super good to land this. Would you mind fixing the merge conflict, @asashour ?

@srawlins
Copy link
Member

srawlins commented Nov 2, 2022

@bwilkerson @pq this is the rule that bans // @dart=2.9, etc. Likely not needed for Dart >= 3.0, but maybe extra useful before then.

lib/src/rules/enable_null_safety.dart Outdated Show resolved Hide resolved
@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
var visitor = _Visitor(this, context);
Copy link
Member

Choose a reason for hiding this comment

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

It might not be too bad, given that we only need to look at the comments before the first non-comment token in the file (modulo the corner case below). The performance is then dependent on the length of the file header and library documentation comments.

test/integration/enable_null_safety.dart Outdated Show resolved Hide resolved
Copy link
Member

@pq pq left a comment

Choose a reason for hiding this comment

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

👍

@pq
Copy link
Member

pq commented Nov 3, 2022

I'd like to hold off on landing this until after the currently in progress roll is finished. (See: https://dart-review.googlesource.com/c/sdk/+/264961)

Thanks for your patience!

@pq pq mentioned this pull request Nov 9, 2022
5 tasks
@pq pq merged commit 0c8ba72 into dart-lang:main Nov 9, 2022
@asashour asashour deleted the 2702_null_safety branch November 9, 2022 19:47
mockturtl added a commit to mockturtl/tidy that referenced this pull request Nov 21, 2022
copybara-service bot pushed a commit to dart-lang/sdk that referenced this pull request Aug 23, 2023
* Implement enable_null_safety rule

* year

* Update tests

* Use `reflective_test_loader`

* `all.dart`
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 this pull request may close these issues.

5 participants