Skip to content

Commit

Permalink
Linter. Updates to stop linter to crash while running on JsonSerializ…
Browse files Browse the repository at this point in the history
…able macro results.

It is not the full implementation and test coverage of these lints,
only changes necessary to stop crashing. The rest is left for someone
else.

Change-Id: I4fde48db81b40c061349484d4fc1bbf1135e834f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/348201
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Jan 24, 2024
1 parent dd680e3 commit 359c2fc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ class _Visitor extends SimpleAstVisitor<void> {

@override
void visitClassDeclaration(ClassDeclaration node) {
var declaredElement = node.declaredElement;
if (declaredElement == null || declaredElement.isSealed) {
var element = node.declaredElement;
if (element == null || element.isAugmentation || element.isSealed) {
return;
}

var interface = context.inheritanceManager.getInterface(declaredElement);
var interface = context.inheritanceManager.getInterface(element);
var map = interface.map;
for (var member in map.values) {
var enclosingElement = member.enclosingElement;
Expand All @@ -105,8 +105,7 @@ class _Visitor extends SimpleAstVisitor<void> {

if (node.members.isNotEmpty &&
node.members.every(_isStaticMember) &&
(declaredElement.methods.isNotEmpty ||
declaredElement.fields.any(_isNonConst))) {
(element.methods.isNotEmpty || element.fields.any(_isNonConst))) {
rule.reportLint(node);
}
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/linter/lib/src/rules/unnecessary_overrides.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,14 @@ class _UnnecessaryMethodOverrideVisitor
ExecutableElement? getInheritedElement(node) {
var element = node.declaredElement;
if (element == null) return null;

var enclosingElement = element.enclosingElement;
if (enclosingElement is! InterfaceElement) return null;
return enclosingElement.thisType.lookUpMethod2(

var augmented = enclosingElement.augmented;
if (augmented == null) return null;

return augmented.declaration.thisType.lookUpMethod2(
node.name.lexeme,
element.library,
concrete: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ class C {
]);
}

test_class_empty_augmentation_empty() async {
var a = newFile('$testPackageLibPath/a.dart', r'''
import augment 'b.dart';
class A {}
''');

var b = newFile('$testPackageLibPath/b.dart', r'''
library augment 'a.dart';
augment class A {}
''');

result = await resolveFile(a.path);
await assertNoDiagnosticsIn(errors);

result = await resolveFile(b.path);
await assertNoDiagnosticsIn(errors);
}

test_finalClass() async {
await assertDiagnostics(r'''
final class C {
Expand Down
22 changes: 22 additions & 0 deletions pkg/linter/test/rules/unnecessary_overrides_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ class UnnecessaryOverridesTest extends LintRuleTest {
@override
String get lintRule => 'unnecessary_overrides';

test_class_augmentation_method_withoutOverride_noSuper() async {
var a = newFile('$testPackageLibPath/a.dart', r'''
import augment 'b.dart';
class A {}
''');

var b = newFile('$testPackageLibPath/b.dart', r'''
library augment 'a.dart';
augment class A {
void foo() {}
}
''');

result = await resolveFile(a.path);
await assertNoDiagnosticsIn(errors);

result = await resolveFile(b.path);
await assertNoDiagnosticsIn(errors);
}

test_enum_field() async {
await assertDiagnostics(r'''
enum A {
Expand Down

0 comments on commit 359c2fc

Please sign in to comment.