From bba849909c6a99dcdfac7bd89f4fd4a18a2fa00f Mon Sep 17 00:00:00 2001 From: Tim Blasi Date: Thu, 9 Apr 2015 17:42:14 -0700 Subject: [PATCH] fix(dart/transform): Gracefully handle log calls before init - Lazily create and use a logger that prints instead of `throw`ing. - Use this logger in unit tests. --- .../src/transform/common/logging.dart | 32 ++++++++++++++++++- .../test/transform/common/logger.dart | 23 ------------- .../transform/directive_linker/all_tests.dart | 2 -- .../template_compiler/all_tests.dart | 2 -- 4 files changed, 31 insertions(+), 28 deletions(-) delete mode 100644 modules/angular2/test/transform/common/logger.dart diff --git a/modules/angular2/src/transform/common/logging.dart b/modules/angular2/src/transform/common/logging.dart index 9637541f51..db8c53f70c 100644 --- a/modules/angular2/src/transform/common/logging.dart +++ b/modules/angular2/src/transform/common/logging.dart @@ -18,7 +18,37 @@ void setLogger(BuildLogger logger) { /// The logger the transformer should use for messaging. BuildLogger get logger { if (_logger == null) { - throw new StateError('Logger never initialized.'); + _logger = new PrintLogger(); } return _logger; } + +class PrintLogger implements BuildLogger { + void _printWithPrefix(prefix, msg) => print('$prefix: $msg'); + void info(msg, {AssetId asset, SourceSpan span}) => + _printWithPrefix('INFO', msg); + void fine(msg, {AssetId asset, SourceSpan span}) => + _printWithPrefix('FINE', msg); + void warning(msg, {AssetId asset, SourceSpan span}) => + _printWithPrefix('WARN', msg); + void error(msg, {AssetId asset, SourceSpan span}) { + throw new PrintLoggerError(msg, asset, span); + } + Future writeOutput() => null; + Future addLogFilesFromAsset(AssetId id, [int nextNumber = 1]) => null; +} + +class PrintLoggerError extends Error { + final String message; + final AssetId asset; + final SourceSpan span; + + PrintLoggerError(message, asset, span); + + @override + String toString() { + return 'Message: ${Error.safeToString(message)}, ' + 'Asset: ${Error.safeToString(asset)}, ' + 'Span: ${Error.safeToString(span)}.'; + } +} diff --git a/modules/angular2/test/transform/common/logger.dart b/modules/angular2/test/transform/common/logger.dart deleted file mode 100644 index 8e3fba7b6e..0000000000 --- a/modules/angular2/test/transform/common/logger.dart +++ /dev/null @@ -1,23 +0,0 @@ -library angular2.test.transform.common.logger; - -import 'package:code_transformers/messages/build_logger.dart'; - -class NullLogger implements BuildLogger { - const NullLogger(); - void info(String message, {AssetId asset, SourceSpan span}) {} - void fine(String message, {AssetId asset, SourceSpan span}) {} - void warning(String message, {AssetId asset, SourceSpan span}) {} - void error(String message, {AssetId asset, SourceSpan span}) { - throw new NullLoggerError(message, asset, span); - } - Future writeOutput() => null; - Future addLogFilesFromAsset(AssetId id, [int nextNumber = 1]) => null; -} - -class NullLoggerError extends Error { - final String message; - final AssetId asset; - final SourceSpan span; - - NullLoggerError(message, asset, span); -} diff --git a/modules/angular2/test/transform/directive_linker/all_tests.dart b/modules/angular2/test/transform/directive_linker/all_tests.dart index 2ebb2d5655..a7068f5464 100644 --- a/modules/angular2/test/transform/directive_linker/all_tests.dart +++ b/modules/angular2/test/transform/directive_linker/all_tests.dart @@ -10,14 +10,12 @@ import 'package:path/path.dart' as path; import 'package:guinness/guinness.dart'; import 'package:unittest/vm_config.dart'; -import '../common/logger.dart'; import '../common/read_file.dart'; var formatter = new DartFormatter(); void allTests() { var reader = new TestAssetReader(); - setLogger(new NullLogger()); it('should ensure that dependencies are property chained.', () async { for (var inputPath in [ diff --git a/modules/angular2/test/transform/template_compiler/all_tests.dart b/modules/angular2/test/transform/template_compiler/all_tests.dart index 847fb6a3dc..e18fe5d5eb 100644 --- a/modules/angular2/test/transform/template_compiler/all_tests.dart +++ b/modules/angular2/test/transform/template_compiler/all_tests.dart @@ -9,7 +9,6 @@ import 'package:angular2/src/transform/template_compiler/generator.dart'; import 'package:dart_style/dart_style.dart'; import 'package:guinness/guinness.dart'; -import '../common/logger.dart'; import '../common/read_file.dart'; var formatter = new DartFormatter(); @@ -17,7 +16,6 @@ var formatter = new DartFormatter(); void allTests() { Html5LibDomAdapter.makeCurrent(); AssetReader reader = new TestAssetReader(); - setLogger(new NullLogger()); it('should parse simple expressions in inline templates.', () async { var inputPath =