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

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: support PropertyAccess expressions 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..f70e885b51c80c48a2c1b2360fa82e1f1b2bbd14 100644
--- a/lib/build/initializer_plugin.dart
+++ b/lib/build/initializer_plugin.dart
@@ -92,8 +92,6 @@ class DefaultInitializerPlugin implements InitializerPlugin {
var constructor = annotation.constructorName == null
? ''
: '.${annotation.constructorName}';
- // TODO(jakemac): Support more than raw values here
- // https://github.com/dart-lang/static_init/issues/5
var args = buildArgumentList(annotation.arguments, pluginData);
return 'const $metaPrefix.${clazz}$constructor$args';
}
@@ -202,14 +200,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 +231,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 +248,38 @@ class DefaultInitializerPlugin implements InitializerPlugin {
} else {
logger.error('Unsupported argument to initializer constructor.');
}
+ } else if (expression is PropertyAccess) {
+ buffer.write(buildExpression(expression.target, pluginData));
+ buffer.write('.${expression.propertyName}');
+ } 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);
+ 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