Skip to content

Commit 40a3cd2

Browse files
author
Tim Blasi
committed
style(dart/transform): Do not format generated code by default
Formatting code requires time and memory during the build -- do not do it unless explicitly requested via a parameter to the transformer. Add an entry to the [wiki](https://github.com/angular/angular/wiki/Angular-2-Dart-Transformer) describing the added parameter.
1 parent 3db0ae1 commit 40a3cd2

5 files changed

Lines changed: 45 additions & 17 deletions

File tree

modules/angular2/src/transform/common/formatter.dart

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,31 @@ library angular2.transform.common.formatter;
22

33
import 'package:dart_style/dart_style.dart';
44

5-
import 'logging.dart';
6-
7-
DartFormatter _formatter = null;
5+
AngularDartFormatter _formatter = null;
86

97
void init(DartFormatter formatter) {
10-
_formatter = formatter;
8+
_formatter = new _RealFormatter(formatter);
119
}
1210

13-
DartFormatter get formatter {
11+
AngularDartFormatter get formatter {
1412
if (_formatter == null) {
15-
logger.info('Formatter never initialized, using default formatter.');
16-
_formatter = new DartFormatter();
13+
_formatter = new _PassThroughFormatter();
1714
}
1815
return _formatter;
1916
}
17+
18+
abstract class AngularDartFormatter {
19+
String format(String source, {uri});
20+
}
21+
22+
class _PassThroughFormatter implements AngularDartFormatter {
23+
String format(String source, {uri}) => source;
24+
}
25+
26+
class _RealFormatter implements AngularDartFormatter {
27+
final DartFormatter _formatter;
28+
29+
_RealFormatter(this._formatter);
30+
31+
String format(source, {uri}) => _formatter.format(source, uri: uri);
32+
}

modules/angular2/src/transform/common/options.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const DEFAULT_OPTIMIZATION_PHASES = 5;
88

99
const CUSTOM_ANNOTATIONS_PARAM = 'custom_annotations';
1010
const ENTRY_POINT_PARAM = 'entry_points';
11+
const FORMAT_CODE_PARAM = 'format_code';
1112
const GENERATE_CHANGE_DETECTORS_PARAM = 'generate_change_detectors';
1213
const INIT_REFLECTOR_PARAM = 'init_reflector';
1314
const INLINE_VIEWS_PARAM = 'inline_views';
@@ -51,6 +52,11 @@ class TransformerOptions {
5152
/// The "correct" number of phases varies with the structure of the app.
5253
final int optimizationPhases;
5354

55+
/// Whether to format generated code.
56+
/// Code that is only modified will never be formatted because doing so may
57+
/// invalidate the source maps generated by `dart2js` and/or other tools.
58+
final bool formatCode;
59+
5460
TransformerOptions._internal(
5561
this.entryPoints,
5662
this.reflectionEntryPoints,
@@ -59,8 +65,9 @@ class TransformerOptions {
5965
this.initReflector,
6066
this.annotationMatcher,
6167
this.optimizationPhases,
62-
this.generateChangeDetectors,
63-
this.inlineViews);
68+
{this.generateChangeDetectors,
69+
this.inlineViews,
70+
this.formatCode});
6471

6572
factory TransformerOptions(List<String> entryPoints,
6673
{List<String> reflectionEntryPoints,
@@ -70,7 +77,8 @@ class TransformerOptions {
7077
List<ClassDescriptor> customAnnotationDescriptors: const [],
7178
int optimizationPhases: DEFAULT_OPTIMIZATION_PHASES,
7279
bool inlineViews: true,
73-
bool generateChangeDetectors: true}) {
80+
bool generateChangeDetectors: true,
81+
bool formatCode: false}) {
7482
if (reflectionEntryPoints == null || reflectionEntryPoints.isEmpty) {
7583
reflectionEntryPoints = entryPoints;
7684
}
@@ -85,7 +93,8 @@ class TransformerOptions {
8593
initReflector,
8694
annotationMatcher,
8795
optimizationPhases,
88-
generateChangeDetectors,
89-
inlineViews);
96+
generateChangeDetectors: generateChangeDetectors,
97+
inlineViews: inlineViews,
98+
formatCode: formatCode);
9099
}
91100
}

modules/angular2/src/transform/common/options_reader.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ TransformerOptions parseBarbackSettings(BarbackSettings settings) {
1515
var inlineViews = _readBool(config, INLINE_VIEWS_PARAM, defaultValue: true);
1616
var generateChangeDetectors =
1717
_readBool(config, GENERATE_CHANGE_DETECTORS_PARAM, defaultValue: true);
18+
var formatCode = _readBool(config, FORMAT_CODE_PARAM, defaultValue: false);
1819
String mirrorModeVal =
1920
config.containsKey(MIRROR_MODE_PARAM) ? config[MIRROR_MODE_PARAM] : '';
2021
var mirrorMode = MirrorMode.none;
@@ -39,7 +40,8 @@ TransformerOptions parseBarbackSettings(BarbackSettings settings) {
3940
inlineViews: inlineViews,
4041
customAnnotationDescriptors: _readCustomAnnotations(config),
4142
optimizationPhases: optimizationPhases,
42-
generateChangeDetectors: generateChangeDetectors);
43+
generateChangeDetectors: generateChangeDetectors,
44+
formatCode: formatCode);
4345
}
4446

4547
bool _readBool(Map config, String paramName, {bool defaultValue}) {

modules/angular2/src/transform/transformer.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ export 'common/options.dart';
1818

1919
/// Replaces Angular 2 mirror use with generated code.
2020
class AngularTransformerGroup extends TransformerGroup {
21-
AngularTransformerGroup._(phases) : super(phases) {
22-
formatter.init(new DartFormatter());
21+
AngularTransformerGroup._(phases, {bool formatCode: false}) : super(phases) {
22+
if (formatCode) {
23+
formatter.init(new DartFormatter());
24+
}
2325
}
2426

2527
factory AngularTransformerGroup(TransformerOptions options) {
@@ -38,7 +40,8 @@ class AngularTransformerGroup extends TransformerGroup {
3840
[new BindGenerator(options)],
3941
[new TemplateCompiler(options)]
4042
]);
41-
return new AngularTransformerGroup._(phases);
43+
return new AngularTransformerGroup._(phases,
44+
formatCode: options.formatCode);
4245
}
4346

4447
factory AngularTransformerGroup.asPlugin(BarbackSettings settings) {

modules/angular2/test/transform/integration/all_tests.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ main() {
1414
var formatter = new DartFormatter();
1515
var transform = new AngularTransformerGroup(new TransformerOptions(
1616
['web/index.dart'],
17-
reflectionEntryPoints: ['web/index.dart']));
17+
reflectionEntryPoints: ['web/index.dart'],
18+
formatCode: true));
1819

1920
class IntegrationTestConfig {
2021
final String name;

0 commit comments

Comments
 (0)