Skip to content

Commit 8329968

Browse files
pqcommit-bot@chromium.org
authored andcommitted
fix for prefer_single_quotes
Change-Id: I844c6847fb3318cca34fb18b5fb89e1f4196f357 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/116540 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 9357b48 commit 8329968

File tree

9 files changed

+129
-55
lines changed

9 files changed

+129
-55
lines changed

pkg/analysis_server/lib/src/services/correction/assist.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ class DartAssistKind {
112112
static const CONVERT_TO_SINGLE_QUOTED_STRING = const AssistKind(
113113
'dart.assist.convert.toSingleQuotedString',
114114
30,
115-
"Convert to single quoted string",
116-
// todo (pq): migrate to (conditional) fix
117-
associatedErrorCodes: <String>['prefer_single_quotes']);
115+
"Convert to single quoted string");
118116
static const CONVERT_TO_SPREAD =
119117
const AssistKind('dart.assist.convertToSpread', 30, "Convert to a spread",
120118
// todo (pq): migrate to (conditional) fix

pkg/analysis_server/lib/src/services/correction/assist_internal.dart

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ class AssistProcessor extends BaseProcessor {
106106
)) {
107107
await _addProposal_convertToPackageImport();
108108
}
109-
await _addProposal_convertToSingleQuotedString();
109+
if (!_containsErrorCode(
110+
{LintNames.prefer_single_quotes},
111+
)) {
112+
await _addProposal_convertToSingleQuotedString();
113+
}
110114
await _addProposal_encapsulateField();
111115
await _addProposal_exchangeOperands();
112116
await _addProposal_flutterConvertToChildren();
@@ -3400,57 +3404,8 @@ class AssistProcessor extends BaseProcessor {
34003404
}
34013405

34023406
Future<void> _convertQuotes(bool fromDouble, AssistKind kind) async {
3403-
if (node is SimpleStringLiteral) {
3404-
SimpleStringLiteral literal = node;
3405-
if (fromDouble ? !literal.isSingleQuoted : literal.isSingleQuoted) {
3406-
String newQuote = literal.isMultiline
3407-
? (fromDouble ? "'''" : '"""')
3408-
: (fromDouble ? "'" : '"');
3409-
int quoteLength = literal.isMultiline ? 3 : 1;
3410-
String lexeme = literal.literal.lexeme;
3411-
if (lexeme.indexOf(newQuote) < 0) {
3412-
var changeBuilder = _newDartChangeBuilder();
3413-
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
3414-
builder.addSimpleReplacement(
3415-
new SourceRange(
3416-
literal.offset + (literal.isRaw ? 1 : 0), quoteLength),
3417-
newQuote);
3418-
builder.addSimpleReplacement(
3419-
new SourceRange(literal.end - quoteLength, quoteLength),
3420-
newQuote);
3421-
});
3422-
_addAssistFromBuilder(changeBuilder, kind);
3423-
}
3424-
}
3425-
} else if (node is InterpolationString) {
3426-
StringInterpolation parent = node.parent;
3427-
if (fromDouble ? !parent.isSingleQuoted : parent.isSingleQuoted) {
3428-
String newQuote = parent.isMultiline
3429-
? (fromDouble ? "'''" : '"""')
3430-
: (fromDouble ? "'" : '"');
3431-
int quoteLength = parent.isMultiline ? 3 : 1;
3432-
NodeList<InterpolationElement> elements = parent.elements;
3433-
for (int i = 0; i < elements.length; i++) {
3434-
InterpolationElement element = elements[i];
3435-
if (element is InterpolationString) {
3436-
String lexeme = element.contents.lexeme;
3437-
if (lexeme.indexOf(newQuote) >= 0) {
3438-
return;
3439-
}
3440-
}
3441-
}
3442-
var changeBuilder = _newDartChangeBuilder();
3443-
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
3444-
builder.addSimpleReplacement(
3445-
new SourceRange(
3446-
parent.offset + (parent.isRaw ? 1 : 0), quoteLength),
3447-
newQuote);
3448-
builder.addSimpleReplacement(
3449-
new SourceRange(parent.end - quoteLength, quoteLength), newQuote);
3450-
});
3451-
_addAssistFromBuilder(changeBuilder, kind);
3452-
}
3453-
}
3407+
final changeBuilder = await createBuilder_convertQuotes(fromDouble);
3408+
_addAssistFromBuilder(changeBuilder, kind);
34543409
}
34553410

34563411
/**

pkg/analysis_server/lib/src/services/correction/base_processor.dart

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,61 @@ abstract class BaseProcessor {
473473
return changeBuilder;
474474
}
475475

476+
Future<ChangeBuilder> createBuilder_convertQuotes(bool fromDouble) async {
477+
if (node is SimpleStringLiteral) {
478+
SimpleStringLiteral literal = node;
479+
if (fromDouble ? !literal.isSingleQuoted : literal.isSingleQuoted) {
480+
String newQuote = literal.isMultiline
481+
? (fromDouble ? "'''" : '"""')
482+
: (fromDouble ? "'" : '"');
483+
int quoteLength = literal.isMultiline ? 3 : 1;
484+
String lexeme = literal.literal.lexeme;
485+
if (lexeme.indexOf(newQuote) < 0) {
486+
var changeBuilder = _newDartChangeBuilder();
487+
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
488+
builder.addSimpleReplacement(
489+
new SourceRange(
490+
literal.offset + (literal.isRaw ? 1 : 0), quoteLength),
491+
newQuote);
492+
builder.addSimpleReplacement(
493+
new SourceRange(literal.end - quoteLength, quoteLength),
494+
newQuote);
495+
});
496+
return changeBuilder;
497+
}
498+
}
499+
} else if (node is InterpolationString) {
500+
StringInterpolation parent = node.parent;
501+
if (fromDouble ? !parent.isSingleQuoted : parent.isSingleQuoted) {
502+
String newQuote = parent.isMultiline
503+
? (fromDouble ? "'''" : '"""')
504+
: (fromDouble ? "'" : '"');
505+
int quoteLength = parent.isMultiline ? 3 : 1;
506+
NodeList<InterpolationElement> elements = parent.elements;
507+
for (int i = 0; i < elements.length; i++) {
508+
InterpolationElement element = elements[i];
509+
if (element is InterpolationString) {
510+
String lexeme = element.contents.lexeme;
511+
if (lexeme.indexOf(newQuote) >= 0) {
512+
return null;
513+
}
514+
}
515+
}
516+
var changeBuilder = _newDartChangeBuilder();
517+
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
518+
builder.addSimpleReplacement(
519+
new SourceRange(
520+
parent.offset + (parent.isRaw ? 1 : 0), quoteLength),
521+
newQuote);
522+
builder.addSimpleReplacement(
523+
new SourceRange(parent.end - quoteLength, quoteLength), newQuote);
524+
});
525+
return changeBuilder;
526+
}
527+
}
528+
return null;
529+
}
530+
476531
Future<ChangeBuilder> createBuilder_convertToExpressionFunctionBody() async {
477532
// prepare current body
478533
FunctionBody body = getEnclosingFunctionBody();

pkg/analysis_server/lib/src/services/correction/fix.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ class DartFixKind {
201201
const FixKind('CONVERT_TO_NULL_AWARE', 50, "Convert to use '?.'");
202202
static const CONVERT_TO_PACKAGE_IMPORT = const FixKind(
203203
'CONVERT_TO_PACKAGE_IMPORT', 50, "Convert to 'package:' import");
204+
static const CONVERT_TO_SINGLE_QUOTED_STRING = const FixKind(
205+
'CONVERT_TO_SINGLE_QUOTED_STRING', 50, "Convert to single quoted string");
204206
static const CREATE_CLASS =
205207
const FixKind('CREATE_CLASS', 50, "Create class '{0}'");
206208
static const CREATE_CONSTRUCTOR =

pkg/analysis_server/lib/src/services/correction/fix_internal.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,9 @@ class FixProcessor extends BaseProcessor {
659659
if (name == LintNames.prefer_null_aware_operators) {
660660
await _addFix_convertToNullAware();
661661
}
662+
if (name == LintNames.prefer_single_quotes) {
663+
await _addFix_convertSingleQuotes();
664+
}
662665
if (errorCode.name == LintNames.slash_for_doc_comments) {
663666
await _addFix_convertDocumentationIntoLine();
664667
}
@@ -1366,6 +1369,12 @@ class FixProcessor extends BaseProcessor {
13661369
_addFixFromBuilder(changeBuilder, DartFixKind.CONVERT_TO_FOR_ELEMENT);
13671370
}
13681371

1372+
Future<void> _addFix_convertSingleQuotes() async {
1373+
final changeBuilder = await createBuilder_convertQuotes(true);
1374+
_addFixFromBuilder(
1375+
changeBuilder, DartFixKind.CONVERT_TO_SINGLE_QUOTED_STRING);
1376+
}
1377+
13691378
Future<void> _addFix_convertToExpressionBody() async {
13701379
final changeBuilder = await createBuilder_convertToExpressionFunctionBody();
13711380
_addFixFromBuilder(changeBuilder, DartFixKind.CONVERT_INTO_EXPRESSION_BODY);

pkg/analysis_server/lib/src/services/linter/lint_names.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class LintNames {
4747
static const String prefer_is_not_empty = 'prefer_is_not_empty';
4848
static const String prefer_null_aware_operators =
4949
'prefer_null_aware_operators';
50+
static const String prefer_single_quotes = 'prefer_single_quotes';
5051
static const String slash_for_doc_comments = 'slash_for_doc_comments';
5152
static const String type_annotate_public_apis = 'type_annotate_public_apis';
5253
static const String type_init_formals = 'type_init_formals';

pkg/analysis_server/test/src/services/correction/assist/convert_to_single_quoted_string_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analysis_server/src/services/correction/assist.dart';
6+
import 'package:analysis_server/src/services/linter/lint_names.dart';
67
import 'package:analyzer_plugin/utilities/assist/assist.dart';
78
import 'package:test_reflective_loader/test_reflective_loader.dart';
89

@@ -80,6 +81,17 @@ main() {
8081
''');
8182
}
8283

84+
test_one_simple_noAssistWithLint() async {
85+
createAnalysisOptionsFile(lints: [LintNames.prefer_single_quotes]);
86+
verifyNoTestUnitErrors = false;
87+
await resolveTestUnit('''
88+
main() {
89+
print("abc");
90+
}
91+
''');
92+
await assertNoAssist();
93+
}
94+
8395
test_three_embeddedTarget() async {
8496
await resolveTestUnit('''
8597
main() {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2019, 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:analysis_server/src/services/correction/fix.dart';
6+
import 'package:analysis_server/src/services/linter/lint_names.dart';
7+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
8+
import 'package:test_reflective_loader/test_reflective_loader.dart';
9+
10+
import 'fix_processor.dart';
11+
12+
main() {
13+
defineReflectiveSuite(() {
14+
defineReflectiveTests(ConvertToSingleQuotedStringTest);
15+
});
16+
}
17+
18+
@reflectiveTest
19+
class ConvertToSingleQuotedStringTest extends FixProcessorLintTest {
20+
@override
21+
FixKind get kind => DartFixKind.CONVERT_TO_SINGLE_QUOTED_STRING;
22+
23+
@override
24+
String get lintCode => LintNames.prefer_single_quotes;
25+
26+
/// More coverage in the `convert_to_single_quoted_string_test.dart` assist test.
27+
test_one_simple() async {
28+
await resolveTestUnit('''
29+
main() {
30+
print(/*LINT*/"abc");
31+
}
32+
''');
33+
await assertHasFix('''
34+
main() {
35+
print(/*LINT*/'abc');
36+
}
37+
''');
38+
}
39+
}

pkg/analysis_server/test/src/services/correction/fix/test_all.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import 'convert_to_int_literal_test.dart' as convert_to_int_literal;
4444
import 'convert_to_named_arguments_test.dart' as convert_to_named_arguments;
4545
import 'convert_to_null_aware_test.dart' as convert_to_null_aware;
4646
import 'convert_to_package_import_test.dart' as convert_to_package_import;
47+
import 'convert_to_single_quoted_string_test.dart'
48+
as convert_to_single_quoted_string;
4749
import 'create_class_test.dart' as create_class;
4850
import 'create_constructor_for_final_fields_test.dart'
4951
as create_constructor_for_final_field;
@@ -158,6 +160,7 @@ main() {
158160
convert_to_named_arguments.main();
159161
convert_to_null_aware.main();
160162
convert_to_package_import.main();
163+
convert_to_single_quoted_string.main();
161164
create_class.main();
162165
create_constructor_for_final_field.main();
163166
create_constructor_super.main();

0 commit comments

Comments
 (0)