Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit 8053b74

Browse files
authored
add combinators_ordering (#3468)
* add combinators_ordering * address review comments
1 parent b3ff468 commit 8053b74

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

example/all.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ linter:
5656
- cascade_invocations
5757
- cast_nullable_to_non_nullable
5858
- close_sinks
59+
- combinators_ordering
5960
- comment_references
6061
- conditional_uri_does_not_exist
6162
- constant_identifier_names

lib/src/rules.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import 'rules/cancel_subscriptions.dart';
5858
import 'rules/cascade_invocations.dart';
5959
import 'rules/cast_nullable_to_non_nullable.dart';
6060
import 'rules/close_sinks.dart';
61+
import 'rules/combinators_ordering.dart';
6162
import 'rules/comment_references.dart';
6263
import 'rules/conditional_uri_does_not_exist.dart';
6364
import 'rules/constant_identifier_names.dart';
@@ -271,6 +272,7 @@ void registerLintRules({bool inTestMode = false}) {
271272
..register(CascadeInvocations())
272273
..register(CastNullableToNonNullable())
273274
..register(CloseSinks())
275+
..register(CombinatorsOrdering())
274276
..register(CommentReferences())
275277
..register(ConditionalUriDoesNotExist())
276278
..register(ConstantIdentifierNames())
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) 2022, 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+
import 'package:analyzer/dart/ast/ast.dart';
6+
import 'package:analyzer/dart/ast/visitor.dart';
7+
import 'package:collection/collection.dart';
8+
9+
import '../analyzer.dart';
10+
11+
const _desc = r'Sort combinator names alphabetically.';
12+
13+
const _details = r'''
14+
15+
**DO** sort combinator names alphabetically.
16+
17+
**BAD:**
18+
```dart
19+
import 'a.dart' show B, A hide D, C;
20+
export 'a.dart' show B, A hide D, C;
21+
```
22+
23+
**GOOD:**
24+
```dart
25+
import 'a.dart' show A, B hide C, D;
26+
export 'a.dart' show A, B hide C, D;
27+
```
28+
29+
''';
30+
31+
class CombinatorsOrdering extends LintRule {
32+
CombinatorsOrdering()
33+
: super(
34+
name: 'combinators_ordering',
35+
description: _desc,
36+
details: _details,
37+
group: Group.style,
38+
);
39+
40+
@override
41+
void registerNodeProcessors(
42+
NodeLintRegistry registry,
43+
LinterContext context,
44+
) {
45+
var visitor = _Visitor(this);
46+
registry.addHideCombinator(this, visitor);
47+
registry.addShowCombinator(this, visitor);
48+
}
49+
}
50+
51+
class _Visitor extends SimpleAstVisitor<void> {
52+
_Visitor(this.rule);
53+
54+
final LintRule rule;
55+
56+
@override
57+
void visitHideCombinator(HideCombinator node) {
58+
if (!node.hiddenNames.map((e) => e.name).isSorted()) {
59+
rule.reportLint(node);
60+
}
61+
}
62+
63+
@override
64+
void visitShowCombinator(ShowCombinator node) {
65+
if (!node.shownNames.map((e) => e.name).isSorted()) {
66+
rule.reportLint(node);
67+
}
68+
}
69+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2022, 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+
// test w/ `dart test -N combinators_ordering`
6+
7+
import 'dart:math' as m1 show max, min; // OK
8+
import 'dart:math' as m2 show min, max; // LINT
9+
10+
export 'dart:math' show max, min; // OK
11+
export 'dart:math' show min, max; // LINT
12+
13+
import 'dart:math' as m3 hide max, min; // OK
14+
import 'dart:math' as m4 hide min, max; // LINT
15+
16+
export 'dart:math' hide max, min; // OK
17+
export 'dart:math' hide min, max; // LINT

0 commit comments

Comments
 (0)