| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library linter.src.rules.unnecessary_getters_setters; | 5 library linter.src.rules.unnecessary_getters_setters; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/visitor.dart'; | 8 import 'package:analyzer/dart/ast/visitor.dart'; |
| 9 import 'package:linter/src/ast.dart'; | 9 import 'package:linter/src/ast.dart'; |
| 10 import 'package:linter/src/linter.dart'; | 10 import 'package:linter/src/linter.dart'; |
| 11 | 11 |
| 12 const desc = 'Avoid wrapping fields in getters and setters just to be "safe".'; | 12 const desc = 'Avoid wrapping fields in getters and setters just to be "safe".'; |
| 13 | 13 |
| 14 const details = ''' | 14 const details = ''' |
| 15 From the [style guide] (https://www.dartlang.org/articles/style-guide/): | 15 From the [style guide](https://www.dartlang.org/articles/style-guide/): |
| 16 | 16 |
| 17 *AVOID** wrapping fields in getters and setters just to be "safe". | 17 *AVOID** wrapping fields in getters and setters just to be "safe". |
| 18 | 18 |
| 19 In Java and C#, it's common to hide all fields behind getters and setters | 19 In Java and C#, it's common to hide all fields behind getters and setters |
| 20 (or properties in C#), even if the implementation just forwards to the field. | 20 (or properties in C#), even if the implementation just forwards to the field. |
| 21 That way, if you ever need to do more work in those members, you can without | 21 That way, if you ever need to do more work in those members, you can without |
| 22 needing to touch the callsites. This is because calling a getter method is | 22 needing to touch the callsites. This is because calling a getter method is |
| 23 different than accessing a field in Java, and accessing a property isn't | 23 different than accessing a field in Java, and accessing a property isn't |
| 24 binary-compatible with accessing a raw field in C#. | 24 binary-compatible with accessing a raw field in C#. |
| 25 | 25 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 _visitGetterSetter(MethodDeclaration getter, MethodDeclaration setter) { | 87 _visitGetterSetter(MethodDeclaration getter, MethodDeclaration setter) { |
| 88 if (isSimpleSetter(setter) && | 88 if (isSimpleSetter(setter) && |
| 89 isSimpleGetter(getter) && | 89 isSimpleGetter(getter) && |
| 90 !isProtected(getter) && | 90 !isProtected(getter) && |
| 91 !isProtected(setter)) { | 91 !isProtected(setter)) { |
| 92 rule.reportLint(getter.name); | 92 rule.reportLint(getter.name); |
| 93 rule.reportLint(setter.name); | 93 rule.reportLint(setter.name); |
| 94 } | 94 } |
| 95 } | 95 } |
| 96 } | 96 } |
| OLD | NEW |