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

Unified Diff: lib/build/initializer_plugin.dart

Issue 951463004: support more types of expressions (Closed) Base URL: git@github.com:dart-lang/static-init.git@master
Patch Set: update pubspec/changelog Created 5 years, 10 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 | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/build/initializer_plugin.dart
diff --git a/lib/build/initializer_plugin.dart b/lib/build/initializer_plugin.dart
index 035161ea427d3ceb138ddb4d127bb4103bd1cbc8..79666601ac5f33611c26405025ce2efc2d958d3d 100644
--- a/lib/build/initializer_plugin.dart
+++ b/lib/build/initializer_plugin.dart
@@ -202,14 +202,8 @@ class DefaultInitializerPlugin implements InitializerPlugin {
var logger = pluginData.logger;
var libraryPrefixes = pluginData.libraryPrefixes;
var buffer = new StringBuffer();
- if (expression is StringLiteral) {
- var value = expression.stringValue;
- if (value == null) {
- logger.error('Only const strings are allowed in initializer '
- 'expressions, found $expression');
- }
- value = value.replaceAll(r'\', r'\\').replaceAll(r"'", r"\'");
- buffer.write("'$value'");
+ if (expression is StringLiteral && expression.stringValue != null) {
+ buffer.write(_stringValue(expression.stringValue));
} else if (expression is BooleanLiteral ||
expression is DoubleLiteral ||
expression is IntegerLiteral ||
@@ -239,7 +233,7 @@ class DefaultInitializerPlugin implements InitializerPlugin {
var element = expression.bestElement;
if (element == null || !element.isPublic) {
logger.error('Private constants are not supported in intializer '
- 'constructors, found $element.');
+ 'constructors, found $element.');
}
libraryPrefixes.putIfAbsent(
element.library, () => 'i${libraryPrefixes.length}');
@@ -256,10 +250,35 @@ class DefaultInitializerPlugin implements InitializerPlugin {
} else {
logger.error('Unsupported argument to initializer constructor.');
}
+ } else if (expression is InstanceCreationExpression) {
+ logger.error('Unsupported expression in initializer, found $expression. '
+ 'Instance creation expressions are not supported (yet). Instead, '
+ 'please assign it to a const variable and use that instead.');
} else {
- logger.error('Only literals and identifiers are allowed for initializer '
- 'expressions, found $expression.');
+ // Try to evaluate the constant and use that.
+ var result = pluginData.resolver.evaluateConstant(
+ pluginData.initializer.targetElement.library, expression);
+ if (!result.isValid) {
+ logger.error('Invalid expression in initializer, found $expression. '
+ 'And got the following errors: ${result.errors}.');
+ }
+ var value = result.value.value;
+ if (value == null) {
+ logger.error('Unsupported expression in initializer, found '
+ '$expression. Please file a bug at '
+ 'https://github.com/dart-lang/initialize/issues');
+ }
+
+ if (value is String) value = _stringValue(value);
Siggi Cherem (dart-lang) 2015/02/25 19:32:08 we might need to check for instance creations here
jakemac 2015/02/25 20:23:05 Above this there is a special case for Identifiers
Siggi Cherem (dart-lang) 2015/02/25 21:06:33 but is there any case where those could evaluate t
jakemac 2015/02/25 22:06:08 Done.
+ buffer.write(value);
}
return buffer.toString();
}
+
+ // Returns an expression for a string value. Wraps it in single quotes and
+ // escapes existing single quotes and escapes.
+ _stringValue(String value) {
+ value = value.replaceAll(r'\', r'\\').replaceAll(r"'", r"\'");
+ return "'$value'";
+ }
}
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698