| 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; | 5 library linter.src.rules.unnecessary_getters; |
| 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 = | 12 const desc = |
| 13 'Prefer using a public final field instead of a private field with a public
getter.'; | 13 'Prefer using a public final field instead of a private field with a public
getter.'; |
| 14 | 14 |
| 15 const details = ''' | 15 const details = ''' |
| 16 From the [style guide] (https://www.dartlang.org/articles/style-guide/): | 16 From the [style guide](https://www.dartlang.org/articles/style-guide/): |
| 17 | 17 |
| 18 **PREFER** using a public final field instead of a private field with a public | 18 **PREFER** using a public final field instead of a private field with a public |
| 19 getter. | 19 getter. |
| 20 | 20 |
| 21 If you have a field that outside code should be able to see but not assign to | 21 If you have a field that outside code should be able to see but not assign to |
| 22 (and you don't need to set it outside of the constructor), a simple solution | 22 (and you don't need to set it outside of the constructor), a simple solution |
| 23 that works in many cases is to just mark it `final`. | 23 that works in many cases is to just mark it `final`. |
| 24 | 24 |
| 25 **GOOD:** | 25 **GOOD:** |
| 26 | 26 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 var candidates = getters.keys.where((id) => !setters.keys.contains(id)); | 77 var candidates = getters.keys.where((id) => !setters.keys.contains(id)); |
| 78 candidates.map((n) => getters[n]).forEach(_visitGetter); | 78 candidates.map((n) => getters[n]).forEach(_visitGetter); |
| 79 } | 79 } |
| 80 | 80 |
| 81 _visitGetter(MethodDeclaration getter) { | 81 _visitGetter(MethodDeclaration getter) { |
| 82 if (isSimpleGetter(getter)) { | 82 if (isSimpleGetter(getter)) { |
| 83 rule.reportLint(getter.name); | 83 rule.reportLint(getter.name); |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 } | 86 } |
| OLD | NEW |