Skip to content

Commit

Permalink
Issue 52340. When inline method, check for string interpolation of pa…
Browse files Browse the repository at this point in the history
…rameters.

Bug: #52340
Change-Id: Ic8d74778e62b3bee2152bf921fbb48d2febd3701
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/302641
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed May 10, 2023
1 parent a3956e4 commit 25b2a4c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ String _getMethodSourceForInvocation(
var range = occurrence.range;
// prepare argument source to apply at this occurrence
String occurrenceArgumentSource;
if (argumentPrecedence < occurrence.parentPrecedence) {
if (occurrence.inStringInterpolation && argument is! SimpleIdentifier) {
occurrenceArgumentSource = '{$argumentSource}';
} else if (argumentPrecedence < occurrence.parentPrecedence) {
occurrenceArgumentSource = '($argumentSource)';
} else {
occurrenceArgumentSource = argumentSource;
Expand Down Expand Up @@ -447,10 +449,15 @@ class InlineMethodRefactoringImpl extends RefactoringImpl
}

class _ParameterOccurrence {
final Precedence parentPrecedence;
final SourceRange range;
final Precedence parentPrecedence;
final bool inStringInterpolation;

_ParameterOccurrence(this.parentPrecedence, this.range);
_ParameterOccurrence({
required this.range,
required this.parentPrecedence,
required this.inStringInterpolation,
});
}

/// Processor for single [SearchMatch] reference to [methodElement].
Expand Down Expand Up @@ -746,15 +753,25 @@ class _SourcePart {
_implicitThisOffsets.add(offset - _base);
}

void addParameterOccurrence(ParameterElement parameter,
SourceRange identifierRange, Precedence precedence) {
void addParameterOccurrence({
required ParameterElement parameter,
required SourceRange identifierRange,
required Precedence parentPrecedence,
required bool inStringInterpolation,
}) {
var occurrences = _parameters[parameter];
if (occurrences == null) {
occurrences = [];
_parameters[parameter] = occurrences;
}
identifierRange = range.offsetBy(identifierRange, -_base);
occurrences.add(_ParameterOccurrence(precedence, identifierRange));
occurrences.add(
_ParameterOccurrence(
parentPrecedence: parentPrecedence,
range: identifierRange,
inStringInterpolation: inStringInterpolation,
),
);
}

void addVariable(VariableElement element, SourceRange identifierRange) {
Expand Down Expand Up @@ -865,7 +882,11 @@ class _VariablesVisitor extends GeneralizingAstVisitor<void> {
var nodeRange = range.node(node);
var parentPrecedence = getExpressionParentPrecedence(node);
result.addParameterOccurrence(
parameterElement, nodeRange, parentPrecedence);
parameter: parameterElement,
identifierRange: nodeRange,
parentPrecedence: parentPrecedence,
inStringInterpolation: node.parent is InterpolationExpression,
);
}

void _addVariable(SimpleIdentifier node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,46 @@ void f() {
expect(refactoring.inlineAll, false);
}

Future<void> test_intoStringInterpolation_identifier() async {
await indexTestUnit(r'''
void f() {
final v = 0;
test(v);
}
void test(int a) {
'a: $a';
}
''');
_createRefactoring('test(int');
// validate change
return _assertSuccessfulRefactoring(r'''
void f() {
final v = 0;
'a: $v';
}
''');
}

Future<void> test_intoStringInterpolation_integerLiteral() async {
await indexTestUnit(r'''
void f() {
test(0);
}
void test(int a) {
'a: $a';
}
''');
_createRefactoring('test(int');
// validate change
return _assertSuccessfulRefactoring(r'''
void f() {
'a: ${0}';
}
''');
}

Future<void> test_method_async() async {
await indexTestUnit(r'''
import 'dart:async';
Expand Down

0 comments on commit 25b2a4c

Please sign in to comment.