Skip to content

Commit e7c35b3

Browse files
committed
Fix for alphanumerics following braces.
BUG=3 [email protected], [email protected] Review URL: https://chromiumcodereview.appspot.com//900093002
1 parent 42881a9 commit e7c35b3

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

lib/src/rules/unnecessary_brace_in_string_interp.dart

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,38 @@ library unnecessary_brace_in_string_interp;
66

77
import 'package:analyzer/src/generated/ast.dart';
88
import 'package:analyzer/src/generated/error.dart';
9+
import 'package:analyzer/src/generated/scanner.dart';
910
import 'package:analyzer/src/services/lint.dart';
1011

11-
const msg = '''
12-
Interpolated simple identifiers (not followed by an alphanumeric string) do
13-
not need braces.
14-
''';
12+
final RegExp alphaNumeric = new RegExp(r'^[a-zA-Z0-9]');
13+
14+
const msg =
15+
'''Interpolated simple identifiers (not followed by an alphanumeric string) do
16+
not need braces.''';
1517

1618
const name = 'UnnecessaryBraceInStringInterp';
1719

20+
bool isAlphaNumeric(Token token) =>
21+
token is StringToken && token.lexeme.startsWith(alphaNumeric);
22+
1823
class UnnecessaryBraceInStringInterp extends Linter {
1924
@override
2025
AstVisitor getVisitor() => new Visitor(reporter);
2126
}
2227

23-
class Visitor extends SimpleAstVisitor<Object> {
28+
class Visitor extends SimpleAstVisitor {
2429
ErrorReporter reporter;
2530
Visitor(this.reporter);
2631

2732
@override
2833
visitStringInterpolation(StringInterpolation node) {
2934
var expressions = node.elements.where((e) => e is InterpolationExpression);
3035
for (InterpolationExpression expression in expressions) {
31-
if (expression.expression is SimpleIdentifier &&
32-
expression.rightBracket != null) {
33-
reporter.reportErrorForNode(new LintCode(name, msg), expression, []);
36+
if (expression.expression is SimpleIdentifier) {
37+
Token bracket = expression.rightBracket;
38+
if (bracket != null && !isAlphaNumeric(bracket.next)) {
39+
reporter.reportErrorForNode(new LintCode(name, msg), expression, []);
40+
}
3441
}
3542
}
3643
}

test/rules/unnecessary_brace_in_string_interp.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
main(args) {
66
print('hello');
77
print('hello $args');
8+
print('hello $args!');
9+
print('hello ${args}1');
810
print('hello ${args}'); //LINT
11+
print('hello ${args}!'); //LINT
912
print('hello ${args.length}');
1013
}

0 commit comments

Comments
 (0)