Skip to content

Commit

Permalink
[linter] Report deprecated_consistency only on constructor name
Browse files Browse the repository at this point in the history
Before this fix the lint was being reported on the entire constructor definition.

Also moves all tests to be reflective.

Change-Id: I8e32600cd87a7f34f8e8aa701acf180c741b915a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/342300
Reviewed-by: Brian Wilkerson <[email protected]>
Reviewed-by: Samuel Rawlins <[email protected]>
Commit-Queue: Brian Wilkerson <[email protected]>
  • Loading branch information
parlough authored and Commit Queue committed Dec 18, 2023
1 parent 89381ea commit 3e2ac67
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 28 deletions.
4 changes: 3 additions & 1 deletion pkg/linter/lib/src/rules/deprecated_consistency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ class _Visitor extends SimpleAstVisitor<void> {
if (constructorElement != null &&
constructorElement.enclosingElement.hasDeprecated &&
!constructorElement.hasDeprecated) {
rule.reportLint(node, errorCode: DeprecatedConsistency.constructorCode);
var nodeToAnnotate = node.name ?? node.returnType;
rule.reportLintForOffset(nodeToAnnotate.offset, nodeToAnnotate.length,
errorCode: DeprecatedConsistency.constructorCode);
}
}

Expand Down
95 changes: 92 additions & 3 deletions pkg/linter/test/rules/deprecated_consistency_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,107 @@ class DeprecatedConsistencyTest extends LintRuleTest {
@override
String get lintRule => 'deprecated_consistency';

test_superInit() async {
test_classDeprecated_factoryConstructor() async {
await assertDiagnostics(r'''
@deprecated
class A {
@deprecated
A._();
factory A() => A._();
}
''', [
lint(56, 1),
]);
}

test_classDeprecated_factoryConstructor_deprecated() async {
await assertNoDiagnostics(r'''
@deprecated
class A {
@deprecated
A._();
@deprecated
factory A() => A._();
}
''');
}

test_classDeprecated_generativeConstructor() async {
await assertDiagnostics(r'''
@deprecated
class A {
A();
}
''', [
lint(24, 1),
]);
}

test_classDeprecated_generativeConstructor_deprecated() async {
await assertNoDiagnostics(r'''
@deprecated
class A {
@deprecated
A();
}
''');
}

test_constructorFieldFormalDeprecated_field() async {
await assertDiagnostics(r'''
class A {
String? a;
A({@deprecated this.a});
}
''', [
lint(20, 1),
]);
}

test_constructorFieldFormalDeprecated_fieldDeprecated() async {
await assertNoDiagnostics(r'''
class A {
@deprecated
String? a;
A({@deprecated this.a});
}
''');
}

test_constructorSuperFieldFormalDeprecated_field() async {
await assertNoDiagnostics(r'''
class A {
String? a;
A({this.a});
}
class B extends A {
B({super.a});
B({@deprecated super.a});
}
''');
}

test_fieldDeprecated_fieldFormalParameter() async {
await assertDiagnostics(r'''
class A {
@deprecated
String? a;
A({this.a});
}
''', [
lint(20, 1),
lint(42, 6),
]);
}

test_fieldDeprecated_fieldFormalParameter_deprecated() async {
await assertNoDiagnostics(r'''
class A {
@deprecated
String? a;
A({@deprecated this.a});
}
''');
}
}
24 changes: 0 additions & 24 deletions pkg/linter/test_data/rules/deprecated_consistency.dart

This file was deleted.

0 comments on commit 3e2ac67

Please sign in to comment.