| // Copyright (c) 2017, 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 '../analyzer.dart'; |
| import '../util/dart_type_utilities.dart'; |
| |
| const _desc = r'Avoid setters without getters.'; |
| |
| const _details = r''' |
| |
| **DON'T** define a setter without a corresponding getter. |
| |
| Defining a setter without defining a corresponding getter can lead to logical |
| inconsistencies. Doing this could allow you to set a property to some value, |
| but then upon observing the property's value, it could easily be different. |
| |
| **BAD:** |
| ```dart |
| class Bad { |
| int l, r; |
| |
| set length(int newLength) { |
| r = l + newLength; |
| } |
| } |
| ``` |
| |
| **GOOD:** |
| ```dart |
| class Good { |
| int l, r; |
| |
| int get length => r - l; |
| |
| set length(int newLength) { |
| r = l + newLength; |
| } |
| } |
| ``` |
| |
| '''; |
| |
| bool _hasGetter(MethodDeclaration node) => |
| DartTypeUtilities.lookUpGetter(node) != null; |
| |
| bool _hasInheritedSetter(MethodDeclaration node) => |
| DartTypeUtilities.lookUpInheritedConcreteSetter(node) != null; |
| |
| class AvoidSettersWithoutGetters extends LintRule implements NodeLintRule { |
| AvoidSettersWithoutGetters() |
| : super( |
| name: 'avoid_setters_without_getters', |
| description: _desc, |
| details: _details, |
| group: Group.style); |
| |
| @override |
| void registerNodeProcessors( |
| NodeLintRegistry registry, LinterContext context) { |
| var visitor = _Visitor(this); |
| registry.addClassDeclaration(this, visitor); |
| } |
| } |
| |
| class _Visitor extends SimpleAstVisitor<void> { |
| final LintRule rule; |
| |
| _Visitor(this.rule); |
| |
| @override |
| void visitClassDeclaration(ClassDeclaration node) { |
| for (var member in node.members.whereType<MethodDeclaration>()) { |
| if (member.isSetter && |
| !_hasInheritedSetter(member) && |
| !_hasGetter(member)) { |
| rule.reportLint(member.name); |
| } |
| } |
| } |
| } |