X Tutup
Skip to content

Commit e310bee

Browse files
Tim Blasikegluneq
authored andcommitted
feat(dart/transform): Avoid print in transformer code.
Replace direct uses of `print` in the transformer with explicit uses of stderr. Add a few @OverRide annotations for clarification of other `print` implementations. Clarify error message on incorrect custom_annotations value. Closes angular#7855
1 parent 4902244 commit e310bee

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

modules_dart/transform/lib/src/transform/common/async_string_writer.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
library angular2.transform.common.async_string_writer;
22

33
import 'dart:async';
4+
45
import 'package:analyzer/src/generated/java_core.dart';
56

67
/// [PrintWriter] implementation that allows asynchronous printing via
@@ -18,6 +19,7 @@ class AsyncStringWriter extends PrintWriter {
1819

1920
AsyncStringWriter([Object content = ""]) : this._(new StringBuffer(content));
2021

22+
@override
2123
void print(x) {
2224
_curr.write(x);
2325
}
@@ -54,6 +56,7 @@ class AsyncStringWriter extends PrintWriter {
5456
}).whenComplete(_semaphoreDecrementAndCleanup);
5557
}
5658

59+
@override
5760
String toString() => _bufs.map((buf) => '$buf').join('(async gap)');
5861

5962
void _semaphoreIncrement() {

modules_dart/transform/lib/src/transform/common/logging.dart

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
library angular2.src.transform.common.logging;
22

33
import 'dart:async';
4+
import 'dart:io' show stderr;
45

56
import 'package:barback/barback.dart';
67
import 'package:source_span/source_span.dart';
@@ -49,12 +50,16 @@ void _logElapsed(Stopwatch timer, String operationName, AssetId assetId) {
4950
log.fine(buf.toString(), asset: assetId);
5051
}
5152

52-
/// Prints logged messages to the console.
53+
/// Writes logged messages to the provided [StringSink].
5354
///
54-
/// A simple implementation of [TransformLogger] that prints messages to the
55-
/// console and discards `asset` and `span` information.
56-
class PrintLogger implements TransformLogger {
57-
void _printWithPrefix(prefix, msg) => print('$prefix: $msg');
55+
/// A simple implementation of [TransformLogger] that writes messages to a
56+
/// [StringSink] and discards `asset` and `span` information.
57+
class SinkLogger implements TransformLogger {
58+
final StringSink _sink;
59+
60+
SinkLogger(this._sink);
61+
62+
void _printWithPrefix(prefix, msg) => _sink.writeln('$prefix: $msg');
5863

5964
@override
6065
void info(msg, {AssetId asset, SourceSpan span}) =>
@@ -74,6 +79,14 @@ class PrintLogger implements TransformLogger {
7479
}
7580
}
7681

82+
/// Prints logged messages to stderr.
83+
///
84+
/// A simple implementation of [TransformLogger] that prints messages to
85+
/// [stderr] and discards `asset` and `span` information.
86+
class PrintLogger extends SinkLogger {
87+
PrintLogger() : super(stderr);
88+
}
89+
7790
class PrintLoggerError extends Error {
7891
final String message;
7992
final AssetId asset;

modules_dart/transform/lib/src/transform/common/options_reader.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
library angular2.transform.common.options_reader;
22

3+
import 'dart:io';
4+
35
import 'package:barback/barback.dart';
6+
47
import 'annotation_matcher.dart';
58
import 'mirror_mode.dart';
69
import 'options.dart';
710
import './url_resolver.dart';
811

9-
TransformerOptions parseBarbackSettings(BarbackSettings settings) {
12+
TransformerOptions parseBarbackSettings(BarbackSettings settings) {
1013
var config = settings.configuration;
1114
var entryPoints = _readStringList(config, ENTRY_POINT_PARAM);
1215
var initReflector =
@@ -79,7 +82,8 @@ List<String> _readStringList(Map config, String paramName) {
7982
error = true;
8083
}
8184
if (error) {
82-
print('Invalid value for "$paramName" in the Angular 2 transformer.');
85+
stderr.writeln(
86+
'Invalid value for "$paramName" in the Angular 2 transformer.');
8387
}
8488
return result;
8589
}
@@ -111,7 +115,7 @@ List<ClassDescriptor> _readCustomAnnotations(Map config) {
111115
}
112116
}
113117
if (error) {
114-
print(CUSTOM_ANNOTATIONS_ERROR);
118+
stderr.writeln(CUSTOM_ANNOTATIONS_ERROR);
115119
}
116120
return descriptors;
117121
}
@@ -121,7 +125,7 @@ const CUSTOM_ANNOTATIONS_ERROR = '''
121125
Expected something that looks like the following:
122126
123127
transformers:
124-
- angular2:
128+
- angular2[/transform/codegen]:
125129
custom_annotations:
126130
- name: MyAnnotation
127131
import: 'package:my_package/my_annotation.dart'

0 commit comments

Comments
 (0)
X Tutup