blob: 88b472fc3cb6709241888a377fe853dac7fa20de [file] [log] [blame]
// Copyright (c) 2020, 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 = r'Unnecessary string interpolation.';
const _details = r'''
Don't use string interpolation if there's only a string expression in it.
**BAD:**
```dart
String message;
String o = '$message';
```
**GOOD:**
```dart
String message;
String o = message;
```
''';
class UnnecessaryStringInterpolations extends LintRule implements NodeLintRule {
UnnecessaryStringInterpolations()
: super(
name: 'unnecessary_string_interpolations',
description: _desc,
details: _details,
group: Group.style);
@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
var visitor = _Visitor(this);
registry.addStringInterpolation(this, visitor);
}
}
class _Visitor extends SimpleAstVisitor<void> {
final LintRule rule;
_Visitor(this.rule);
@override
void visitStringInterpolation(StringInterpolation node) {
if (node.parent is AdjacentStrings) return;
if (node.elements.length == 3) {
var start = node.elements[0] as InterpolationString;
var interpolation = node.elements[1] as InterpolationExpression;
var end = node.elements[2] as InterpolationString;
if (start.value.isEmpty && end.value.isEmpty) {
var staticType = interpolation.expression.staticType;
if (staticType != null && staticType.isDartCoreString) {
rule.reportLint(node);
}
}
}
}
}