blob: 6762bd7266f6c9bc06a7ebe71cda1b33e9cccbc0 [file] [log] [blame]
// Copyright (c) 2015, 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 '../analyzer.dart';
const _desc = "Don't type annotate initializing formals.";
const _details = r'''
From the [style guide](https://dart.dev/guides/language/effective-dart/style/):
**DON'T** type annotate initializing formals.
If a constructor parameter is using `this.x` to initialize a field, then the
type of the parameter is understood to be the same type as the field.
**GOOD:**
```
class Point {
int x, y;
Point(this.x, this.y);
}
```
**BAD:**
```
class Point {
int x, y;
Point(int this.x, int this.y);
}
```
''';
class TypeInitFormals extends LintRule implements NodeLintRule {
TypeInitFormals()
: super(
name: 'type_init_formals',
description: _desc,
details: _details,
group: Group.style);
@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
final visitor = _Visitor(this);
registry.addFieldFormalParameter(this, visitor);
}
}
class _Visitor extends SimpleAstVisitor<void> {
final LintRule rule;
_Visitor(this.rule);
@override
void visitFieldFormalParameter(FieldFormalParameter node) {
if (node.type != null) {
rule.reportLint(node.type);
}
}
}