Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit cd1fe53

Browse files
authored
fix unnecessary_brace_in_string_interps for this expressions (#3694)
* fix unnecessary_brace_in_string_interps for this expressions * shared checking
1 parent 1a3af7b commit cd1fe53

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

lib/src/rules/unnecessary_brace_in_string_interps.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,19 @@ class _Visitor extends SimpleAstVisitor<void> {
6262
var exp = expression.expression;
6363
if (exp is SimpleIdentifier) {
6464
var identifier = exp;
65-
var bracket = expression.rightBracket;
66-
if (bracket != null &&
67-
!isIdentifierPart(bracket.next) &&
68-
!identifier.name.contains('\$')) {
69-
rule.reportLint(expression);
65+
if (!identifier.name.contains('\$')) {
66+
_check(expression);
7067
}
68+
} else if (exp is ThisExpression) {
69+
_check(expression);
7170
}
7271
}
7372
}
73+
74+
void _check(InterpolationExpression expression) {
75+
var bracket = expression.rightBracket;
76+
if (bracket != null && !isIdentifierPart(bracket.next)) {
77+
rule.reportLint(expression);
78+
}
79+
}
7480
}

test/rules/all.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ import 'tighten_type_of_initializing_formals_test.dart'
7070
as tighten_type_of_initializing_formals;
7171
import 'type_init_formals_test.dart' as type_init_formals;
7272
import 'unawaited_futures_test.dart' as unawaited_futures;
73+
import 'unnecessary_brace_in_string_interps_test.dart'
74+
as unnecessary_brace_in_string_interps;
7375
import 'unnecessary_const_test.dart' as unnecessary_const;
7476
import 'unnecessary_null_checks_test.dart' as unnecessary_null_checks;
7577
import 'unnecessary_overrides_test.dart' as unnecessary_overrides;
@@ -129,6 +131,7 @@ void main() {
129131
tighten_type_of_initializing_formals.main();
130132
type_init_formals.main();
131133
unawaited_futures.main();
134+
unnecessary_brace_in_string_interps.main();
132135
unnecessary_const.main();
133136
unnecessary_null_checks.main();
134137
unnecessary_overrides.main();
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
import 'package:test_reflective_loader/test_reflective_loader.dart';
6+
7+
import '../rule_test_support.dart';
8+
9+
main() {
10+
defineReflectiveSuite(() {
11+
defineReflectiveTests(UnnecessaryBraceInStringInterpsTest);
12+
});
13+
}
14+
15+
@reflectiveTest
16+
class UnnecessaryBraceInStringInterpsTest extends LintRuleTest {
17+
@override
18+
String get lintRule => 'unnecessary_brace_in_string_interps';
19+
20+
test_simpleIdentifier() async {
21+
await assertDiagnostics(r'''
22+
void hi(String name) {
23+
print('hi: ${name}');
24+
}
25+
''', [
26+
lint(36, 7),
27+
]);
28+
}
29+
30+
test_simpleIdentifier_suffixed() async {
31+
await assertNoDiagnostics(r'''
32+
void hi(String name) {
33+
print('hi: ${name}s');
34+
}
35+
''');
36+
}
37+
38+
test_this_methodInvocation() async {
39+
await assertNoDiagnostics(r'''
40+
class A {
41+
void hi() {
42+
print('hi: ${this.toString()}');
43+
}
44+
}
45+
''');
46+
}
47+
48+
/// https://github.com/dart-lang/linter/issues/3691
49+
test_thisExpression() async {
50+
await assertDiagnostics(r'''
51+
class A {
52+
void hi() {
53+
print('hi: ${this}');
54+
}
55+
}
56+
''', [
57+
lint(39, 7),
58+
]);
59+
}
60+
61+
test_thisExpression_suffixed() async {
62+
await assertNoDiagnostics(r'''
63+
class A {
64+
void hi() {
65+
print('${this}s');
66+
}
67+
}
68+
''');
69+
}
70+
}

0 commit comments

Comments
 (0)