diff --git a/modules/angular2/src/transform/common/formatter.dart b/modules/angular2/src/transform/common/formatter.dart index 18bbb921fa..b6809a298c 100644 --- a/modules/angular2/src/transform/common/formatter.dart +++ b/modules/angular2/src/transform/common/formatter.dart @@ -2,18 +2,31 @@ library angular2.transform.common.formatter; import 'package:dart_style/dart_style.dart'; -import 'logging.dart'; - -DartFormatter _formatter = null; +AngularDartFormatter _formatter = null; void init(DartFormatter formatter) { - _formatter = formatter; + _formatter = new _RealFormatter(formatter); } -DartFormatter get formatter { +AngularDartFormatter get formatter { if (_formatter == null) { - logger.info('Formatter never initialized, using default formatter.'); - _formatter = new DartFormatter(); + _formatter = new _PassThroughFormatter(); } return _formatter; } + +abstract class AngularDartFormatter { + String format(String source, {uri}); +} + +class _PassThroughFormatter implements AngularDartFormatter { + String format(String source, {uri}) => source; +} + +class _RealFormatter implements AngularDartFormatter { + final DartFormatter _formatter; + + _RealFormatter(this._formatter); + + String format(source, {uri}) => _formatter.format(source, uri: uri); +} diff --git a/modules/angular2/src/transform/common/options.dart b/modules/angular2/src/transform/common/options.dart index 13752ad207..1e8ff41bd4 100644 --- a/modules/angular2/src/transform/common/options.dart +++ b/modules/angular2/src/transform/common/options.dart @@ -8,6 +8,7 @@ const DEFAULT_OPTIMIZATION_PHASES = 5; const CUSTOM_ANNOTATIONS_PARAM = 'custom_annotations'; const ENTRY_POINT_PARAM = 'entry_points'; +const FORMAT_CODE_PARAM = 'format_code'; const GENERATE_CHANGE_DETECTORS_PARAM = 'generate_change_detectors'; const INIT_REFLECTOR_PARAM = 'init_reflector'; const INLINE_VIEWS_PARAM = 'inline_views'; @@ -51,6 +52,11 @@ class TransformerOptions { /// The "correct" number of phases varies with the structure of the app. final int optimizationPhases; + /// Whether to format generated code. + /// Code that is only modified will never be formatted because doing so may + /// invalidate the source maps generated by `dart2js` and/or other tools. + final bool formatCode; + TransformerOptions._internal( this.entryPoints, this.reflectionEntryPoints, @@ -59,8 +65,9 @@ class TransformerOptions { this.initReflector, this.annotationMatcher, this.optimizationPhases, - this.generateChangeDetectors, - this.inlineViews); + {this.generateChangeDetectors, + this.inlineViews, + this.formatCode}); factory TransformerOptions(List entryPoints, {List reflectionEntryPoints, @@ -70,7 +77,8 @@ class TransformerOptions { List customAnnotationDescriptors: const [], int optimizationPhases: DEFAULT_OPTIMIZATION_PHASES, bool inlineViews: true, - bool generateChangeDetectors: true}) { + bool generateChangeDetectors: true, + bool formatCode: false}) { if (reflectionEntryPoints == null || reflectionEntryPoints.isEmpty) { reflectionEntryPoints = entryPoints; } @@ -85,7 +93,8 @@ class TransformerOptions { initReflector, annotationMatcher, optimizationPhases, - generateChangeDetectors, - inlineViews); + generateChangeDetectors: generateChangeDetectors, + inlineViews: inlineViews, + formatCode: formatCode); } } diff --git a/modules/angular2/src/transform/common/options_reader.dart b/modules/angular2/src/transform/common/options_reader.dart index 4dfc0c9b1d..565bb2cd1d 100644 --- a/modules/angular2/src/transform/common/options_reader.dart +++ b/modules/angular2/src/transform/common/options_reader.dart @@ -15,6 +15,7 @@ TransformerOptions parseBarbackSettings(BarbackSettings settings) { var inlineViews = _readBool(config, INLINE_VIEWS_PARAM, defaultValue: true); var generateChangeDetectors = _readBool(config, GENERATE_CHANGE_DETECTORS_PARAM, defaultValue: true); + var formatCode = _readBool(config, FORMAT_CODE_PARAM, defaultValue: false); String mirrorModeVal = config.containsKey(MIRROR_MODE_PARAM) ? config[MIRROR_MODE_PARAM] : ''; var mirrorMode = MirrorMode.none; @@ -39,7 +40,8 @@ TransformerOptions parseBarbackSettings(BarbackSettings settings) { inlineViews: inlineViews, customAnnotationDescriptors: _readCustomAnnotations(config), optimizationPhases: optimizationPhases, - generateChangeDetectors: generateChangeDetectors); + generateChangeDetectors: generateChangeDetectors, + formatCode: formatCode); } bool _readBool(Map config, String paramName, {bool defaultValue}) { diff --git a/modules/angular2/src/transform/transformer.dart b/modules/angular2/src/transform/transformer.dart index 4a915bda0a..4808905b8d 100644 --- a/modules/angular2/src/transform/transformer.dart +++ b/modules/angular2/src/transform/transformer.dart @@ -18,8 +18,10 @@ export 'common/options.dart'; /// Replaces Angular 2 mirror use with generated code. class AngularTransformerGroup extends TransformerGroup { - AngularTransformerGroup._(phases) : super(phases) { - formatter.init(new DartFormatter()); + AngularTransformerGroup._(phases, {bool formatCode: false}) : super(phases) { + if (formatCode) { + formatter.init(new DartFormatter()); + } } factory AngularTransformerGroup(TransformerOptions options) { @@ -38,7 +40,8 @@ class AngularTransformerGroup extends TransformerGroup { [new BindGenerator(options)], [new TemplateCompiler(options)] ]); - return new AngularTransformerGroup._(phases); + return new AngularTransformerGroup._(phases, + formatCode: options.formatCode); } factory AngularTransformerGroup.asPlugin(BarbackSettings settings) { diff --git a/modules/angular2/test/transform/integration/all_tests.dart b/modules/angular2/test/transform/integration/all_tests.dart index f10786bec9..8e62cd519f 100644 --- a/modules/angular2/test/transform/integration/all_tests.dart +++ b/modules/angular2/test/transform/integration/all_tests.dart @@ -14,7 +14,8 @@ main() { var formatter = new DartFormatter(); var transform = new AngularTransformerGroup(new TransformerOptions( ['web/index.dart'], - reflectionEntryPoints: ['web/index.dart'])); + reflectionEntryPoints: ['web/index.dart'], + formatCode: true)); class IntegrationTestConfig { final String name;