Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
add deprecated_consistency (#2451)
Browse files Browse the repository at this point in the history
* add deprecated_consistency

* handle null field

* address review comments

* address review comments
  • Loading branch information
a14n authored Feb 12, 2021
1 parent 127e095 commit 4f06a0e
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions example/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ linter:
- constant_identifier_names
- control_flow_in_finally
- curly_braces_in_flow_control_structures
- deprecated_consistency
- diagnostic_describe_all_properties
- directives_ordering
- do_not_use_environment
Expand Down
2 changes: 2 additions & 0 deletions lib/src/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import 'rules/comment_references.dart';
import 'rules/constant_identifier_names.dart';
import 'rules/control_flow_in_finally.dart';
import 'rules/curly_braces_in_flow_control_structures.dart';
import 'rules/deprecated_consistency.dart';
import 'rules/diagnostic_describe_all_properties.dart';
import 'rules/directives_ordering.dart';
import 'rules/do_not_use_environment.dart';
Expand Down Expand Up @@ -246,6 +247,7 @@ void registerLintRules() {
..register(ConstantIdentifierNames())
..register(ControlFlowInFinally())
..register(CurlyBracesInFlowControlStructures())
..register(DeprecatedConsistency())
..register(DiagnosticsDescribeAllProperties())
..register(DirectivesOrdering())
..register(DoNotUseEnvironment())
Expand Down
97 changes: 97 additions & 0 deletions lib/src/rules/deprecated_consistency.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) 2021, 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/visitor.dart';
import 'package:analyzer/dart/element/element.dart';

import '../analyzer.dart';

const _desc = r'Missing deprecated annotation.';

const _details = r'''
Do apply `@Deprecated()` consistently:
- if a class is deprecated, its constructors should also be deprecated.
- if a field is deprecated, the constructor parameter pointing to it should also be deprecated.
- if a constructor parameter pointing to a field is deprecated, the field should also be deprecated.
**BAD:**
```
@deprecated
class A {
A();
}
class B {
B({this.field});
@deprecated
Object field;
}
```
**GOOD:**
```
@deprecated
class A {
@deprecated
A();
}
class B {
B({@deprecated this.field});
@deprecated
Object field;
}
```
''';

class DeprecatedConsistency extends LintRule implements NodeLintRule {
DeprecatedConsistency()
: super(
name: 'deprecated_consistency',
description: _desc,
details: _details,
group: Group.style,
);

@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
final visitor = _Visitor(this);
registry.addConstructorDeclaration(this, visitor);
registry.addFieldFormalParameter(this, visitor);
}
}

class _Visitor extends SimpleAstVisitor<void> {
final LintRule rule;

_Visitor(this.rule);

@override
void visitConstructorDeclaration(ConstructorDeclaration node) {
var constructorElement = node.declaredElement;
if (constructorElement.enclosingElement.hasDeprecated &&
!constructorElement.hasDeprecated) {
rule.reportLint(node);
}
}

@override
void visitFieldFormalParameter(FieldFormalParameter node) {
var declaredElement = node.declaredElement as FieldFormalParameterElement;
var field = declaredElement.field;
if (field == null) return;

if (field.hasDeprecated && !declaredElement.hasDeprecated) {
rule.reportLint(node);
}
if (!field.hasDeprecated && declaredElement.hasDeprecated) {
rule.reportLintForOffset(field.nameOffset, field.nameLength);
}
}
}
26 changes: 26 additions & 0 deletions test/rules/deprecated_consistency.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2021, 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.

// test w/ `pub run test -N deprecated_consistency`

@deprecated
class A {
A.ko(); // LINT
@deprecated
A.ok(); // OK
}

class B {
B.ko({this.field = 0}); // LINT
B.ok({@deprecated this.field = 0}); // OK

@deprecated
Object field;
}

class C {
C({@deprecated this.field = 0}); // OK

Object field; // LINT
}

0 comments on commit 4f06a0e

Please sign in to comment.