| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library linter.src.rules.avoid_as; |
| 6 |
| 7 import 'package:analyzer/src/generated/ast.dart' |
| 8 show AsExpression, AstVisitor, SimpleAstVisitor; |
| 9 import 'package:linter/src/linter.dart'; |
| 10 |
| 11 const desc = r'Avoid using `as`.'; |
| 12 |
| 13 const details = r''' |
| 14 From the [flutter style guide] |
| 15 (https://github.com/flutter/engine/blob/master/sky/specs/style-guide.md): |
| 16 |
| 17 **AVOID** using `as`. |
| 18 |
| 19 If you know the type is correct, use an assertion or assign to a more |
| 20 narrowly-typed variable (this avoids the type check in release mode; `as` |
| 21 is not compiled out in release mode). If you don't know whether the type is |
| 22 correct, check using `is` (this avoids the exception that `as` raises). |
| 23 |
| 24 **BAD:** |
| 25 ``` |
| 26 try { |
| 27 (pm as Person).firstName = 'Seth'; |
| 28 } on CastError { } |
| 29 ``` |
| 30 |
| 31 **GOOD:** |
| 32 ``` |
| 33 Person person = pm; |
| 34 person.firstName = 'Seth'; |
| 35 ``` |
| 36 '''; |
| 37 |
| 38 class AvoidAs extends LintRule { |
| 39 AvoidAs() |
| 40 : super( |
| 41 name: 'avoid_as', |
| 42 description: desc, |
| 43 details: details, |
| 44 group: Group.style); |
| 45 |
| 46 @override |
| 47 AstVisitor getVisitor() => new Visitor(this); |
| 48 } |
| 49 |
| 50 class Visitor extends SimpleAstVisitor { |
| 51 final LintRule rule; |
| 52 Visitor(this.rule); |
| 53 |
| 54 @override |
| 55 visitAsExpression(AsExpression node) { |
| 56 rule.reportLint(node); |
| 57 } |
| 58 } |
| OLD | NEW |