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

Unified Diff: pkg/analyzer/lib/src/dart/sdk/patch.dart

Issue 2413293002: Patch token stream when replace/add nodes. (Closed)
Patch Set: Created 4 years, 2 months 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/analyzer/test/src/dart/sdk/patch_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/dart/sdk/patch.dart
diff --git a/pkg/analyzer/lib/src/dart/sdk/patch.dart b/pkg/analyzer/lib/src/dart/sdk/patch.dart
index 424e521cd162712d9d316eff9a0aa3c415259a32..576cb96149b00c6e687877b6bb316b0cf38d9cd7 100644
--- a/pkg/analyzer/lib/src/dart/sdk/patch.dart
+++ b/pkg/analyzer/lib/src/dart/sdk/patch.dart
@@ -132,15 +132,19 @@ class SdkPatcher {
baseDeclaration.name.name == name) {
if (_hasPatchAnnotation(patchDeclaration.metadata)) {
// Remove the "external" keyword.
- if (baseDeclaration.externalKeyword != null) {
+ Token externalKeyword = baseDeclaration.externalKeyword;
+ if (externalKeyword != null) {
baseDeclaration.externalKeyword = null;
+ _removeToken(externalKeyword);
} else {
_failExternalKeyword(
baseSource, name, baseDeclaration.offset);
}
// Replace the body.
- baseDeclaration.functionExpression.body =
- patchDeclaration.functionExpression.body;
+ FunctionExpression oldExpr = baseDeclaration.functionExpression;
+ FunctionBody newBody = patchDeclaration.functionExpression.body;
+ _replaceNodeTokens(oldExpr.body, newBody);
+ oldExpr.body = newBody;
}
}
}
@@ -161,7 +165,13 @@ class SdkPatcher {
}
}
// Append new top-level declarations.
- baseUnit.declarations.addAll(declarationsToAppend);
+ Token lastToken = baseUnit.endToken.previous;
+ for (CompilationUnitMember newDeclaration in declarationsToAppend) {
+ newDeclaration.endToken.setNext(lastToken.next);
+ lastToken.setNext(newDeclaration.beginToken);
+ baseUnit.declarations.add(newDeclaration);
+ lastToken = newDeclaration.endToken;
+ }
}
/**
@@ -196,4 +206,19 @@ class SdkPatcher {
name.name == 'patch';
});
}
+
+ /**
+ * Remove the [token] from the stream.
+ */
+ static void _removeToken(Token token) {
+ token.previous.setNext(token.next);
+ }
+
+ /**
+ * Replace tokens of the [oldNode] with tokens of the [newNode].
+ */
+ static void _replaceNodeTokens(AstNode oldNode, AstNode newNode) {
+ oldNode.beginToken.previous.setNext(newNode.beginToken);
+ newNode.endToken.setNext(oldNode.endToken.next);
+ }
}
« no previous file with comments | « no previous file | pkg/analyzer/test/src/dart/sdk/patch_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698