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

Unified Diff: pkg/analysis_server/lib/src/services/refactoring/inline_local.dart

Issue 695383002: Issue 21496. Fix for inlining negate/decrement into negate. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/inline_local_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analysis_server/lib/src/services/refactoring/inline_local.dart
diff --git a/pkg/analysis_server/lib/src/services/refactoring/inline_local.dart b/pkg/analysis_server/lib/src/services/refactoring/inline_local.dart
index 4f79dc7ae13b81e063a9add6cb73d6e5645ffe0f..fdee7212086a1f5d03bf5c75a44d7442b1a68f34 100644
--- a/pkg/analysis_server/lib/src/services/refactoring/inline_local.dart
+++ b/pkg/analysis_server/lib/src/services/refactoring/inline_local.dart
@@ -137,7 +137,6 @@ class InlineLocalRefactoringImpl extends RefactoringImpl implements
// prepare initializer
Expression initializer = _variableNode.initializer;
String initializerCode = utils.getNodeText(initializer);
- int initializerPrecedence = getExpressionPrecedence(initializer);
// replace references
for (SearchMatch reference in _references) {
SourceRange range = reference.sourceRange;
@@ -171,7 +170,7 @@ class InlineLocalRefactoringImpl extends RefactoringImpl implements
} else {
codeForReference = initializerCode;
}
- } else if (initializerPrecedence < getExpressionParentPrecedence(node)) {
+ } else if (_shouldUseParenthesis(initializer, node)) {
codeForReference = '($initializerCode)';
} else {
codeForReference = initializerCode;
@@ -195,4 +194,25 @@ class InlineLocalRefactoringImpl extends RefactoringImpl implements
return targetType == TokenType.STRING_INTERPOLATION_IDENTIFIER &&
expression is! SimpleIdentifier;
}
+
+ static bool _shouldUseParenthesis(Expression init, AstNode node) {
+ // check precedence
+ int initPrecedence = getExpressionPrecedence(init);
+ if (initPrecedence < getExpressionParentPrecedence(node)) {
+ return true;
+ }
+ // special case for '-'
+ AstNode parent = node.parent;
+ if (init is PrefixExpression && parent is PrefixExpression) {
+ if (parent.operator.type == TokenType.MINUS) {
+ TokenType initializerOperator = init.operator.type;
+ if (initializerOperator == TokenType.MINUS ||
+ initializerOperator == TokenType.MINUS_MINUS) {
+ return true;
+ }
+ }
+ }
+ // no () is needed
+ return false;
+ }
}
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/inline_local_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698