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

Add a new prefer_mixin rule #1165

Merged
merged 1 commit into from
Sep 12, 2018
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 analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ linter:
- prefer_is_empty
- prefer_is_not_empty
# - prefer_iterable_whereType # under review (see #1068)
- prefer_mixin
- prefer_single_quotes
# - prefer_typing_uninitialized_variables # under review (see #1068)
# - prefer_void_to_null # under review (see #1068)
Expand Down
1 change: 1 addition & 0 deletions example/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ linter:
- prefer_is_empty
- prefer_is_not_empty
- prefer_iterable_whereType
- prefer_mixin
- prefer_single_quotes
- prefer_typing_uninitialized_variables
- prefer_void_to_null
Expand Down
4 changes: 3 additions & 1 deletion lib/src/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import 'package:linter/src/rules/prefer_interpolation_to_compose_strings.dart';
import 'package:linter/src/rules/prefer_is_empty.dart';
import 'package:linter/src/rules/prefer_is_not_empty.dart';
import 'package:linter/src/rules/prefer_iterable_whereType.dart';
import 'package:linter/src/rules/prefer_mixin.dart';
import 'package:linter/src/rules/prefer_single_quotes.dart';
import 'package:linter/src/rules/prefer_typing_uninitialized_variables.dart';
import 'package:linter/src/rules/prefer_void_to_null.dart';
Expand Down Expand Up @@ -225,10 +226,11 @@ void registerLintRules() {
..register(new PreferIterableWhereType())
..register(new PreferIsEmpty())
..register(new PreferIsNotEmpty())
..register(new PublicMemberApiDocs())
..register(new PreferMixin())
..register(new PreferSingleQuotes())
..register(new PreferTypingUninitializedVariables())
..register(new PreferVoidToNull())
..register(new PublicMemberApiDocs())
..register(new PubPackageNames())
..register(new RecursiveGetters())
..registerDefault(new SlashForDocComments())
Expand Down
62 changes: 62 additions & 0 deletions lib/src/rules/prefer_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2018, 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:analyzer/dart/element/element.dart';
import 'package:linter/src/analyzer.dart';

const _desc = r'Prefer using mixins.';

const _details = r'''

Dart 2.1 introduced a new syntax for mixins that provides a safe way for a mixin
to invoke inherited members using `super`. The new style of mixins should always
be used for types that are to be mixed in. As a result, this lint will flag any
uses of a class in a `with` clause.

**BAD:**
```
class A {}
class B extends Object with A {}
```

**OK:**
```
mixin M {}
class C with M {}
```

''';

class PreferMixin extends LintRule implements NodeLintRule {
PreferMixin()
: super(
name: 'prefer_mixin',
description: _desc,
details: _details,
group: Group.style);

@override
void registerNodeProcessors(NodeLintRegistry registry) {
final visitor = new _Visitor(this);
registry.addWithClause(this, visitor);
}
}

class _Visitor extends SimpleAstVisitor<void> {
final LintRule rule;

_Visitor(this.rule);

@override
void visitWithClause(WithClause node) {
for (TypeName type in node.mixinTypes) {
Element element = type.name.staticElement;
if (element is ClassElement && !element.isMixin) {
rule.reportLint(type);
}
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ environment:
sdk: '>=2.0.0-dev <3.0.0'

dependencies:
analyzer: ^0.32.5
analyzer: ^0.33.0-alpha.0
args: '>=1.4.0 <2.0.0'
glob: ^1.0.3
meta: ^1.0.2
Expand Down
11 changes: 11 additions & 0 deletions test/rules/prefer_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2018, 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/ `pub run test -N prefer_mixin`

class A {}
class B extends Object with A {} // LINT

mixin M {}
class C with M {} // OK