This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement enable_null_safety rule * year * Update tests * Use `reflective_test_loader` * `all.dart`
- Loading branch information
Showing
6 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/token.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
|
||
import '../analyzer.dart'; | ||
|
||
const _desc = r'Do use sound null safety.'; | ||
|
||
const _details = r''' | ||
**DO** use sound null safety, by not specifying a dart version lower than `2.12`. | ||
**BAD:** | ||
```dart | ||
// @dart=2.8 | ||
a() { | ||
} | ||
``` | ||
**GOOD:** | ||
```dart | ||
b() { | ||
} | ||
'''; | ||
|
||
class EnableNullSafety extends LintRule implements NodeLintRule { | ||
EnableNullSafety() | ||
: super( | ||
name: 'enable_null_safety', | ||
description: _desc, | ||
details: _details, | ||
group: Group.style); | ||
|
||
@override | ||
void registerNodeProcessors( | ||
NodeLintRegistry registry, LinterContext context) { | ||
var visitor = _Visitor(this, context); | ||
registry.addCompilationUnit(this, visitor); | ||
} | ||
} | ||
|
||
class _Visitor extends SimpleAstVisitor<void> { | ||
// to be kept in sync with LanguageVersionOverrideVerifier (added groups for the version) | ||
static final regExp = RegExp(r'^\s*//\s*@dart\s*=\s*(\d+)\.(\d+)'); | ||
final LintRule rule; | ||
final LinterContext context; | ||
|
||
_Visitor(this.rule, this.context); | ||
|
||
@override | ||
void visitCompilationUnit(CompilationUnit node) { | ||
var beginToken = node.beginToken; | ||
if (beginToken.type == TokenType.SCRIPT_TAG) { | ||
beginToken = beginToken.next!; | ||
} | ||
CommentToken? comment = beginToken.precedingComments; | ||
while (comment != null) { | ||
var match = regExp.firstMatch(comment.lexeme); | ||
if (match != null && match.groupCount == 2) { | ||
var major = int.parse(match.group(1)!); | ||
var minor = int.parse(match.group(2)!); | ||
if (major == 1 || (major == 2 && minor < 12)) { | ||
rule.reportLintForToken(comment); | ||
} | ||
} | ||
|
||
var next = comment.next; | ||
comment = next is CommentToken ? next : null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:test_reflective_loader/test_reflective_loader.dart'; | ||
|
||
import '../rule_test_support.dart'; | ||
|
||
main() { | ||
defineReflectiveSuite(() { | ||
defineReflectiveTests(EnableNullSafetyTest); | ||
}); | ||
} | ||
|
||
@reflectiveTest | ||
class EnableNullSafetyTest extends LintRuleTest { | ||
@override | ||
String get lintRule => 'enable_null_safety'; | ||
|
||
test_2_11() async { | ||
await assertDiagnostics(r''' | ||
// @dart=2.11 | ||
f() { | ||
} | ||
''', [ | ||
lint(0, 13), | ||
]); | ||
} | ||
|
||
test_2_12() async { | ||
await assertNoDiagnostics(r''' | ||
// @dart=2.12 | ||
f() { | ||
} | ||
'''); | ||
} | ||
|
||
test_2_8() async { | ||
await assertDiagnostics(r''' | ||
// @dart=2.8 | ||
f() { | ||
} | ||
''', [ | ||
lint(0, 12), | ||
]); | ||
} | ||
|
||
test_2_8_shebang() async { | ||
await assertDiagnostics(r''' | ||
#!/usr/bin/dart | ||
// @dart=2.8 | ||
f() { | ||
} | ||
''', [ | ||
lint(16, 12), | ||
]); | ||
} | ||
} |