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

fix unnecessary_brace_in_string_interps for this expressions #3694

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/src/rules/unnecessary_brace_in_string_interps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,19 @@ class _Visitor extends SimpleAstVisitor<void> {
var exp = expression.expression;
if (exp is SimpleIdentifier) {
var identifier = exp;
var bracket = expression.rightBracket;
if (bracket != null &&
!isIdentifierPart(bracket.next) &&
!identifier.name.contains('\$')) {
rule.reportLint(expression);
if (!identifier.name.contains('\$')) {
_check(expression);
}
} else if (exp is ThisExpression) {
_check(expression);
}
}
}

void _check(InterpolationExpression expression) {
var bracket = expression.rightBracket;
if (bracket != null && !isIdentifierPart(bracket.next)) {
rule.reportLint(expression);
}
}
}
3 changes: 3 additions & 0 deletions test/rules/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ import 'tighten_type_of_initializing_formals_test.dart'
as tighten_type_of_initializing_formals;
import 'type_init_formals_test.dart' as type_init_formals;
import 'unawaited_futures_test.dart' as unawaited_futures;
import 'unnecessary_brace_in_string_interps_test.dart'
as unnecessary_brace_in_string_interps;
import 'unnecessary_const_test.dart' as unnecessary_const;
import 'unnecessary_null_checks_test.dart' as unnecessary_null_checks;
import 'unnecessary_overrides_test.dart' as unnecessary_overrides;
Expand Down Expand Up @@ -129,6 +131,7 @@ void main() {
tighten_type_of_initializing_formals.main();
type_init_formals.main();
unawaited_futures.main();
unnecessary_brace_in_string_interps.main();
unnecessary_const.main();
unnecessary_null_checks.main();
unnecessary_overrides.main();
Expand Down
70 changes: 70 additions & 0 deletions test/rules/unnecessary_brace_in_string_interps_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2022, 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:test_reflective_loader/test_reflective_loader.dart';

import '../rule_test_support.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryBraceInStringInterpsTest);
});
}

@reflectiveTest
class UnnecessaryBraceInStringInterpsTest extends LintRuleTest {
@override
String get lintRule => 'unnecessary_brace_in_string_interps';

test_simpleIdentifier() async {
await assertDiagnostics(r'''
void hi(String name) {
print('hi: ${name}');
}
''', [
lint(36, 7),
]);
}

test_simpleIdentifier_suffixed() async {
await assertNoDiagnostics(r'''
void hi(String name) {
print('hi: ${name}s');
}
''');
}

test_this_methodInvocation() async {
await assertNoDiagnostics(r'''
class A {
void hi() {
print('hi: ${this.toString()}');
}
}
''');
}

/// https://github.com/dart-lang/linter/issues/3691
test_thisExpression() async {
await assertDiagnostics(r'''
class A {
void hi() {
print('hi: ${this}');
}
}
''', [
lint(39, 7),
]);
}

test_thisExpression_suffixed() async {
await assertNoDiagnostics(r'''
class A {
void hi() {
print('${this}s');
}
}
''');
}
}