Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: lib/src/rules/unnecessary_brace_in_string_interp.dart

Issue 900093002: Fix for alphanumerics following braces. (Closed) Base URL: https://github.com/dart-lang/dart_lint.git@master
Patch Set: Added final modifier. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/rules/unnecessary_brace_in_string_interp.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library unnecessary_brace_in_string_interp; 5 library unnecessary_brace_in_string_interp;
6 6
7 import 'package:analyzer/src/generated/ast.dart'; 7 import 'package:analyzer/src/generated/ast.dart';
8 import 'package:analyzer/src/generated/error.dart'; 8 import 'package:analyzer/src/generated/error.dart';
9 import 'package:analyzer/src/generated/scanner.dart';
9 import 'package:analyzer/src/services/lint.dart'; 10 import 'package:analyzer/src/services/lint.dart';
10 11
12 final RegExp alphaNumeric = new RegExp(r'^[a-zA-Z0-9]');
13
11 const msg = ''' 14 const msg = '''
12 Interpolated simple identifiers (not followed by an alphanumeric string) do 15 Interpolated simple identifiers (not followed by an alphanumeric string) do
13 not need braces. 16 not need braces.
Brian Wilkerson 2015/02/05 01:38:42 Don't include end-of-line characters in the messag
pquitslund 2015/02/05 03:42:06 Done.
14 '''; 17 ''';
15 18
16 const name = 'UnnecessaryBraceInStringInterp'; 19 const name = 'UnnecessaryBraceInStringInterp';
17 20
21 bool isAlphaNumeric(Token token) =>
22 token is StringToken && token.lexeme.startsWith(alphaNumeric);
23
18 class UnnecessaryBraceInStringInterp extends Linter { 24 class UnnecessaryBraceInStringInterp extends Linter {
19 @override 25 @override
20 AstVisitor getVisitor() => new Visitor(reporter); 26 AstVisitor getVisitor() => new Visitor(reporter);
21 } 27 }
22 28
23 class Visitor extends SimpleAstVisitor<Object> { 29 class Visitor extends SimpleAstVisitor<Object> {
scheglov 2015/02/05 01:38:08 Remove <Object>.
pquitslund 2015/02/05 03:42:06 Done.
24 ErrorReporter reporter; 30 ErrorReporter reporter;
25 Visitor(this.reporter); 31 Visitor(this.reporter);
26 32
27 @override 33 @override
28 visitStringInterpolation(StringInterpolation node) { 34 visitStringInterpolation(StringInterpolation node) {
29 var expressions = node.elements.where((e) => e is InterpolationExpression); 35 var expressions = node.elements.where((e) => e is InterpolationExpression);
30 for (InterpolationExpression expression in expressions) { 36 for (InterpolationExpression expression in expressions) {
31 if (expression.expression is SimpleIdentifier && 37 if (expression.expression is SimpleIdentifier) {
32 expression.rightBracket != null) { 38 Token bracket = expression.rightBracket;
33 reporter.reportErrorForNode(new LintCode(name, msg), expression, []); 39 if (bracket != null && !isAlphaNumeric(bracket.next)) {
40 reporter.reportErrorForNode(new LintCode(name, msg), expression, []);
Brian Wilkerson 2015/02/05 01:38:42 I would recommend defining static constant fields
pquitslund 2015/02/05 03:42:06 Agreed! That said, if I understand you right, I'm
41 }
34 } 42 }
35 } 43 }
36 } 44 }
37 } 45 }
OLDNEW
« no previous file with comments | « no previous file | test/rules/unnecessary_brace_in_string_interp.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698