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.
This commit is contained in:
parent
cac74c73e1
commit
bba849909c
|
@ -18,7 +18,37 @@ void setLogger(BuildLogger logger) {
|
||||||
/// The logger the transformer should use for messaging.
|
/// The logger the transformer should use for messaging.
|
||||||
BuildLogger get logger {
|
BuildLogger get logger {
|
||||||
if (_logger == null) {
|
if (_logger == null) {
|
||||||
throw new StateError('Logger never initialized.');
|
_logger = new PrintLogger();
|
||||||
}
|
}
|
||||||
return _logger;
|
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)}.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -10,14 +10,12 @@ import 'package:path/path.dart' as path;
|
||||||
import 'package:guinness/guinness.dart';
|
import 'package:guinness/guinness.dart';
|
||||||
import 'package:unittest/vm_config.dart';
|
import 'package:unittest/vm_config.dart';
|
||||||
|
|
||||||
import '../common/logger.dart';
|
|
||||||
import '../common/read_file.dart';
|
import '../common/read_file.dart';
|
||||||
|
|
||||||
var formatter = new DartFormatter();
|
var formatter = new DartFormatter();
|
||||||
|
|
||||||
void allTests() {
|
void allTests() {
|
||||||
var reader = new TestAssetReader();
|
var reader = new TestAssetReader();
|
||||||
setLogger(new NullLogger());
|
|
||||||
|
|
||||||
it('should ensure that dependencies are property chained.', () async {
|
it('should ensure that dependencies are property chained.', () async {
|
||||||
for (var inputPath in [
|
for (var inputPath in [
|
||||||
|
|
|
@ -9,7 +9,6 @@ import 'package:angular2/src/transform/template_compiler/generator.dart';
|
||||||
import 'package:dart_style/dart_style.dart';
|
import 'package:dart_style/dart_style.dart';
|
||||||
import 'package:guinness/guinness.dart';
|
import 'package:guinness/guinness.dart';
|
||||||
|
|
||||||
import '../common/logger.dart';
|
|
||||||
import '../common/read_file.dart';
|
import '../common/read_file.dart';
|
||||||
|
|
||||||
var formatter = new DartFormatter();
|
var formatter = new DartFormatter();
|
||||||
|
@ -17,7 +16,6 @@ var formatter = new DartFormatter();
|
||||||
void allTests() {
|
void allTests() {
|
||||||
Html5LibDomAdapter.makeCurrent();
|
Html5LibDomAdapter.makeCurrent();
|
||||||
AssetReader reader = new TestAssetReader();
|
AssetReader reader = new TestAssetReader();
|
||||||
setLogger(new NullLogger());
|
|
||||||
|
|
||||||
it('should parse simple expressions in inline templates.', () async {
|
it('should parse simple expressions in inline templates.', () async {
|
||||||
var inputPath =
|
var inputPath =
|
||||||
|
|
Loading…
Reference in New Issue