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

add combinators_ordering #3468

Merged
merged 2 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ linter:
- cascade_invocations
- cast_nullable_to_non_nullable
- close_sinks
- combinators_ordering
- comment_references
- conditional_uri_does_not_exist
- constant_identifier_names
Expand Down
2 changes: 2 additions & 0 deletions lib/src/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import 'rules/cancel_subscriptions.dart';
import 'rules/cascade_invocations.dart';
import 'rules/cast_nullable_to_non_nullable.dart';
import 'rules/close_sinks.dart';
import 'rules/combinators_ordering.dart';
import 'rules/comment_references.dart';
import 'rules/conditional_uri_does_not_exist.dart';
import 'rules/constant_identifier_names.dart';
Expand Down Expand Up @@ -271,6 +272,7 @@ void registerLintRules({bool inTestMode = false}) {
..register(CascadeInvocations())
..register(CastNullableToNonNullable())
..register(CloseSinks())
..register(CombinatorsOrdering())
..register(CommentReferences())
..register(ConditionalUriDoesNotExist())
..register(ConstantIdentifierNames())
Expand Down
69 changes: 69 additions & 0 deletions lib/src/rules/combinators_ordering.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:collection/collection.dart';

import '../analyzer.dart';

const _desc = r'Sort combinator names alphabetically.';

const _details = r'''

**DO** sort combinator names alphabetically.

**BAD:**
```dart
import 'a.dart' show B, A hide D, C;
export 'a.dart' show B, A hide D, C;
```

**GOOD:**
```dart
import 'a.dart' show A, B hide C, D;
export 'a.dart' show A, B hide C, D;
```

''';

class CombinatorsOrdering extends LintRule {
CombinatorsOrdering()
: super(
name: 'combinators_ordering',
description: _desc,
details: _details,
group: Group.style,
);

@override
void registerNodeProcessors(
NodeLintRegistry registry,
LinterContext context,
) {
var visitor = _Visitor(this);
registry.addHideCombinator(this, visitor);
registry.addShowCombinator(this, visitor);
}
}

class _Visitor extends SimpleAstVisitor<void> {
_Visitor(this.rule);

final LintRule rule;

@override
void visitHideCombinator(HideCombinator node) {
if (!node.hiddenNames.map((e) => e.name).isSorted()) {
rule.reportLint(node);
}
}

@override
void visitShowCombinator(ShowCombinator node) {
if (!node.shownNames.map((e) => e.name).isSorted()) {
rule.reportLint(node);
}
}
}
17 changes: 17 additions & 0 deletions test_data/rules/combinators_ordering.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// test w/ `dart test -N combinators_ordering`

import 'dart:math' as m1 show max, min; // OK
import 'dart:math' as m2 show min, max; // LINT

export 'dart:math' show max, min; // OK
export 'dart:math' show min, max; // LINT

import 'dart:math' as m3 hide max, min; // OK
import 'dart:math' as m4 hide min, max; // LINT

export 'dart:math' hide max, min; // OK
export 'dart:math' hide min, max; // LINT