Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: lib/src/rules/only_throw_error.dart

Issue 2100013005: Linter `0.1.21` (Closed) Base URL: https://github.com/dart-lang/linter.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/rules.dart ('k') | lib/src/rules/only_throw_errors.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2
3 // for details. All rights reserved. Use of this source code is governed by a
4 // BSD-style license that can be found in the LICENSE file.
5
6 library linter.src.rules.only_throw_error;
7
8 import 'dart:collection';
9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/visitor.dart';
11 import 'package:analyzer/dart/element/type.dart';
12 import 'package:linter/src/linter.dart';
13 import 'package:linter/src/util/dart_type_utilities.dart';
14
15 const _desc = r'Only throw instaces of classes extending either Exception or Err or';
16
17 const _details = r'''
18
19 **DO** throw only instances of classes that extend dart.core.Error or
20 dart.core.Exception.
21
22 **BAD:**
23 ```
24 void throwString() {
25 throw 'hello world!'; // LINT
26 }
27 ```
28
29 **GOOD:**
30 ```
31 void throwArgumentError() {
32 Error error = new ArgumentError('oh!');
33 throw error; // OK
34 }
35 ```
36 ''';
37
38 class OnlyThrowError extends LintRule {
39 _Visitor _visitor;
40
41 OnlyThrowError() : super(
42 name: 'only_throw_error',
43 description: _desc,
44 details: _details,
45 group: Group.style,
46 maturity: Maturity.experimental) {
47 _visitor = new _Visitor(this);
48 }
49
50 @override
51 AstVisitor getVisitor() => _visitor;
52 }
53
54 class _Visitor extends SimpleAstVisitor {
55 final LintRule rule;
56
57 _Visitor(this.rule);
58
59 @override
60 void visitThrowExpression(ThrowExpression node) {
61 if (node.expression is Literal) {
62 rule.reportLint(node);
63 return;
64 }
65
66 if (!_isThrowable(node.expression.bestType)) {
67 rule.reportLint(node);
68 }
69 }
70 }
71
72 const _library = 'dart.core';
73 const _errorClassName = 'Error';
74 const _exceptionClassName = 'Exception';
75 final LinkedHashSet<InterfaceTypeDefinition> _interfaceDefinitions =
76 new LinkedHashSet<InterfaceTypeDefinition>.from([
77 new InterfaceTypeDefinition(_exceptionClassName, _library),
78 new InterfaceTypeDefinition(_errorClassName, _library)
79 ]);
80
81 bool _isThrowable(DartType type) {
82 return type.isDynamic ||
83 DartTypeUtilities.implementsAnyInterface(type, _interfaceDefinitions);
84 }
OLDNEW
« no previous file with comments | « lib/src/rules.dart ('k') | lib/src/rules/only_throw_errors.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698