blob: 86111fee75446621a91a36996ea518cdd1a4aa3a [file] [log] [blame]
// Copyright (c) 2017, 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/analysis_rule/rule_context.dart';
import 'package:analyzer/analysis_rule/rule_visitor_registry.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/error/error.dart';
import '../analyzer.dart';
const _desc = r'Use adjacent strings to concatenate string literals.';
class PreferAdjacentStringConcatenation extends LintRule {
PreferAdjacentStringConcatenation()
: super(
name: LintNames.prefer_adjacent_string_concatenation,
description: _desc,
);
@override
DiagnosticCode get diagnosticCode =>
LinterLintCode.preferAdjacentStringConcatenation;
@override
void registerNodeProcessors(
RuleVisitorRegistry registry,
RuleContext context,
) {
var visitor = _Visitor(this);
registry.addBinaryExpression(this, visitor);
}
}
class _Visitor extends SimpleAstVisitor<void> {
final LintRule rule;
_Visitor(this.rule);
@override
void visitBinaryExpression(BinaryExpression node) {
if (node.operator.type.lexeme == '+' &&
node.leftOperand is StringLiteral &&
node.rightOperand is StringLiteral) {
rule.reportAtToken(node.operator);
}
}
}