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.
* add deprecated_consistency * handle null field * address review comments * address review comments
- Loading branch information
Showing
4 changed files
with
126 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
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); | ||
} | ||
} | ||
} |
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,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 | ||
} |