Skip to content

Commit

Permalink
Linter 0.1.21
Browse files Browse the repository at this point in the history
* New `only_throw_errors` lint.
* New lint to check for `empty_statements` (#259).
* Fixed NSME when file contents cannot be read (#260).
* Fixed unsafe cast in `iterable_contains_unrelated_type` (#267).

[email protected], [email protected]

Review URL: https://codereview.chromium.org//2100013005 .
  • Loading branch information
pq committed Jun 27, 2016
1 parent ddbbf34 commit 7ca3aab
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 58 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 0.1.21

* New `only_throw_errors` lint.
* New lint to check for `empty_statements` (#259).
* Fixed NSME when file contents cannot be read (#260).
* Fixed unsafe cast in `iterable_contains_unrelated_type` (#267).

# 0.1.20

* New `cancel_subscriptions` lint.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import 'package:linter/src/rules/library_names.dart';
import 'package:linter/src/rules/library_prefixes.dart';
import 'package:linter/src/rules/non_constant_identifier_names.dart';
import 'package:linter/src/rules/one_member_abstracts.dart';
import 'package:linter/src/rules/only_throw_error.dart';
import 'package:linter/src/rules/only_throw_errors.dart';
import 'package:linter/src/rules/overridden_fields.dart';
import 'package:linter/src/rules/package_api_docs.dart';
import 'package:linter/src/rules/package_prefixed_library_names.dart';
Expand Down Expand Up @@ -69,7 +69,7 @@ final Registry ruleRegistry = new Registry()
..register(new EmptyConstructorBodies())
..register(new EmptyStatements())
..register(new TestTypesInEquals())
..register(new OnlyThrowError())
..register(new OnlyThrowErrors())
..register(new OverriddenFields())
..register(new HashAndEquals())
..register(new ImplementationImports())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
library linter.src.rules.only_throw_error;

import 'dart:collection';

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:linter/src/linter.dart';
import 'package:linter/src/util/dart_type_utilities.dart';

const _desc = r'Only throw instaces of classes extending either Exception or Error';
const _desc =
r'Only throw instances of classes extending either Exception or Error';

const _details = r'''
**DO** throw only instances of classes that extend dart.core.Error or
dart.core.Exception.
**DO** throw only instances of classes that extend `dart.core.Error` or
`dart.core.Exception`.
**BAD:**
```
Expand All @@ -35,15 +37,30 @@ void throwArgumentError() {
```
''';

class OnlyThrowError extends LintRule {
const _errorClassName = 'Error';

const _exceptionClassName = 'Exception';

const _library = 'dart.core';
final LinkedHashSet<InterfaceTypeDefinition> _interfaceDefinitions =
new LinkedHashSet<InterfaceTypeDefinition>.from([
new InterfaceTypeDefinition(_exceptionClassName, _library),
new InterfaceTypeDefinition(_errorClassName, _library)
]);
bool _isThrowable(DartType type) {
return type.isDynamic ||
DartTypeUtilities.implementsAnyInterface(type, _interfaceDefinitions);
}
class OnlyThrowErrors extends LintRule {
_Visitor _visitor;

OnlyThrowError() : super(
name: 'only_throw_error',
description: _desc,
details: _details,
group: Group.style,
maturity: Maturity.experimental) {
OnlyThrowErrors()
: super(
name: 'only_throw_errors',
description: _desc,
details: _details,
group: Group.style,
maturity: Maturity.experimental) {
_visitor = new _Visitor(this);
}

Expand All @@ -68,17 +85,3 @@ class _Visitor extends SimpleAstVisitor {
}
}
}

const _library = 'dart.core';
const _errorClassName = 'Error';
const _exceptionClassName = 'Exception';
final LinkedHashSet<InterfaceTypeDefinition> _interfaceDefinitions =
new LinkedHashSet<InterfaceTypeDefinition>.from([
new InterfaceTypeDefinition(_exceptionClassName, _library),
new InterfaceTypeDefinition(_errorClassName, _library)
]);

bool _isThrowable(DartType type) {
return type.isDynamic ||
DartTypeUtilities.implementsAnyInterface(type, _interfaceDefinitions);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: linter
version: 0.1.20
version: 0.1.21
author: Dart Team <[email protected]>
description: Style linter for Dart.
homepage: https://github.com/dart-lang/linter
Expand Down
54 changes: 23 additions & 31 deletions test/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,15 @@ defineTests() {
});

test('close sinks', () {
dartlint.main([
'test/_data/close_sinks',
'--rules=close_sinks'
]);
dartlint.main(['test/_data/close_sinks', '--rules=close_sinks']);
expect(exitCode, 1);
expect(
collectingOut.trim(),
stringContainsInOrder(
[
'IOSink _sinkA; // LINT',
'IOSink _sinkF; // LINT',
'1 file analyzed, 2 issues found, in'
]));
stringContainsInOrder([
'IOSink _sinkA; // LINT',
'IOSink _sinkF; // LINT',
'1 file analyzed, 2 issues found, in'
]));
});
});

Expand All @@ -229,16 +225,15 @@ defineTests() {
expect(exitCode, 1);
expect(
collectingOut.trim(),
stringContainsInOrder(
[
'StreamSubscription _subscriptionA; // LINT',
'StreamSubscription _subscriptionF; // LINT',
'1 file analyzed, 2 issues found, in'
]));
stringContainsInOrder([
'StreamSubscription _subscriptionA; // LINT',
'StreamSubscription _subscriptionF; // LINT',
'1 file analyzed, 2 issues found, in'
]));
});
});

group('only_throw_error', () {
group('only_throw_errors', () {
IOSink currentOut = outSink;
CollectingSink collectingOut = new CollectingSink();
setUp(() {
Expand All @@ -251,23 +246,20 @@ defineTests() {
exitCode = 0;
});

test('only throw error', () {
dartlint.main([
'test/_data/only_throw_error',
'--rules=only_throw_error'
]);
test('only throw errors', () {
dartlint.main(
['test/_data/only_throw_errors', '--rules=only_throw_errors']);
expect(exitCode, 1);
expect(
collectingOut.trim(),
stringContainsInOrder(
[
"throw 'hello world!'; // LINT",
'throw null; // LINT',
'throw 7; // LINT',
'throw new Object(); // LINT',
'throw returnString(); // LINT',
'1 file analyzed, 5 issues found, in'
]));
stringContainsInOrder([
"throw 'hello world!'; // LINT",
'throw null; // LINT',
'throw 7; // LINT',
'throw new Object(); // LINT',
'throw returnString(); // LINT',
'1 file analyzed, 5 issues found, in'
]));
});
});

Expand Down

0 comments on commit 7ca3aab

Please sign in to comment.