| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Code transform for @observable. The core transformation is relatively | 6 * Code transform for @observable. The core transformation is relatively |
| 7 * straightforward, and essentially like an editor refactoring. | 7 * straightforward, and essentially like an editor refactoring. |
| 8 */ | 8 */ |
| 9 library observe.transform; | 9 library observe.transform; |
| 10 | 10 |
| 11 import 'dart:async'; | 11 import 'dart:async'; |
| 12 import 'package:path/path.dart' as path; | 12 |
| 13 import 'package:analyzer_experimental/src/generated/ast.dart'; | 13 import 'package:analyzer_experimental/src/generated/ast.dart'; |
| 14 import 'package:analyzer_experimental/src/generated/error.dart'; | 14 import 'package:analyzer_experimental/src/generated/error.dart'; |
| 15 import 'package:analyzer_experimental/src/generated/parser.dart'; | 15 import 'package:analyzer_experimental/src/generated/parser.dart'; |
| 16 import 'package:analyzer_experimental/src/generated/scanner.dart'; | 16 import 'package:analyzer_experimental/src/generated/scanner.dart'; |
| 17 import 'package:barback/barback.dart'; | 17 import 'package:barback/barback.dart'; |
| 18 import 'package:path/path.dart' as path; |
| 18 import 'package:source_maps/refactor.dart'; | 19 import 'package:source_maps/refactor.dart'; |
| 19 import 'package:source_maps/span.dart' show SourceFile; | 20 import 'package:source_maps/span.dart' show SourceFile; |
| 20 | 21 |
| 21 /** | 22 /** |
| 22 * A [Transformer] that replaces observables based on dirty-checking with an | 23 * A [Transformer] that replaces observables based on dirty-checking with an |
| 23 * implementation based on change notifications. | 24 * implementation based on change notifications. |
| 24 * | 25 * |
| 25 * The transformation adds hooks for field setters and notifies the observation | 26 * The transformation adds hooks for field setters and notifies the observation |
| 26 * system of the change. | 27 * system of the change. |
| 27 */ | 28 */ |
| (...skipping 28 matching lines...) Expand all Loading... |
| 56 // dartbug.com/12340) | 57 // dartbug.com/12340) |
| 57 printer.build(url); | 58 printer.build(url); |
| 58 transform.addOutput(new Asset.fromString(id, printer.text)); | 59 transform.addOutput(new Asset.fromString(id, printer.text)); |
| 59 })); | 60 })); |
| 60 } | 61 } |
| 61 } | 62 } |
| 62 | 63 |
| 63 TextEditTransaction _transformCompilationUnit( | 64 TextEditTransaction _transformCompilationUnit( |
| 64 String inputCode, SourceFile sourceFile, TransformLogger logger) { | 65 String inputCode, SourceFile sourceFile, TransformLogger logger) { |
| 65 var unit = _parseCompilationUnit(inputCode); | 66 var unit = _parseCompilationUnit(inputCode); |
| 66 return transformObservables(unit, sourceFile, inputCode, logger); | 67 var code = new TextEditTransaction(inputCode, sourceFile); |
| 67 } | |
| 68 | |
| 69 // TODO(sigmund): make this private. This is currently public so it can be used | |
| 70 // by the polymer.dart package which is not yet entirely migrated to use | |
| 71 // pub-serve and pub-deploy. | |
| 72 TextEditTransaction transformObservables(CompilationUnit unit, | |
| 73 SourceFile sourceFile, String content, TransformLogger logger) { | |
| 74 var code = new TextEditTransaction(content, sourceFile); | |
| 75 for (var directive in unit.directives) { | 68 for (var directive in unit.directives) { |
| 76 if (directive is LibraryDirective && _hasObservable(directive)) { | 69 if (directive is LibraryDirective && _hasObservable(directive)) { |
| 77 logger.warning('@observable on a library no longer has any effect. ' | 70 logger.warning('@observable on a library no longer has any effect. ' |
| 78 'It should be placed on individual fields.', | 71 'It should be placed on individual fields.', |
| 79 _getSpan(sourceFile, directive)); | 72 _getSpan(sourceFile, directive)); |
| 80 break; | 73 break; |
| 81 } | 74 } |
| 82 } | 75 } |
| 83 | 76 |
| 84 for (var declaration in unit.declarations) { | 77 for (var declaration in unit.declarations) { |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 $type __\$$name$initializer; | 322 $type __\$$name$initializer; |
| 330 $type get $name => __\$$name; | 323 $type get $name => __\$$name; |
| 331 set $name($type value) { | 324 set $name($type value) { |
| 332 __\$$name = notifyPropertyChange(const Symbol('$name'), __\$$name, value); | 325 __\$$name = notifyPropertyChange(const Symbol('$name'), __\$$name, value); |
| 333 } | 326 } |
| 334 '''.replaceAll('\n', '\n$indent')); | 327 '''.replaceAll('\n', '\n$indent')); |
| 335 } | 328 } |
| 336 | 329 |
| 337 code.edit(begin, end, '$replace'); | 330 code.edit(begin, end, '$replace'); |
| 338 } | 331 } |
| OLD | NEW |