| 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 /// Code transform for @observable. The core transformation is relatively | 5 /// Code transform for @observable. The core transformation is relatively |
| 6 /// straightforward, and essentially like an editor refactoring. | 6 /// straightforward, and essentially like an editor refactoring. |
| 7 library observe.transformer; | 7 library observe.transformer; |
| 8 | 8 |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 | 10 |
| 11 import 'package:analyzer/analyzer.dart'; | 11 import 'package:analyzer/analyzer.dart'; |
| 12 import 'package:analyzer/src/generated/ast.dart'; | 12 import 'package:analyzer/src/generated/ast.dart'; |
| 13 import 'package:analyzer/src/generated/error.dart'; | 13 import 'package:analyzer/src/generated/error.dart'; |
| 14 import 'package:analyzer/src/generated/parser.dart'; | 14 import 'package:analyzer/src/generated/parser.dart'; |
| 15 import 'package:analyzer/src/generated/scanner.dart'; | 15 import 'package:analyzer/src/generated/scanner.dart'; |
| 16 import 'package:barback/barback.dart'; | 16 import 'package:barback/barback.dart'; |
| 17 import 'package:source_maps/refactor.dart'; | 17 import 'package:source_maps/refactor.dart'; |
| 18 import 'package:source_maps/span.dart' show SourceFile; | 18 import 'package:source_span/source_span.dart'; |
| 19 | 19 |
| 20 /// A [Transformer] that replaces observables based on dirty-checking with an | 20 /// A [Transformer] that replaces observables based on dirty-checking with an |
| 21 /// implementation based on change notifications. | 21 /// implementation based on change notifications. |
| 22 /// | 22 /// |
| 23 /// The transformation adds hooks for field setters and notifies the observation | 23 /// The transformation adds hooks for field setters and notifies the observation |
| 24 /// system of the change. | 24 /// system of the change. |
| 25 class ObservableTransformer extends Transformer { | 25 class ObservableTransformer extends Transformer { |
| 26 | 26 |
| 27 final List<String> _files; | 27 final List<String> _files; |
| 28 ObservableTransformer([List<String> files]) : _files = files; | 28 ObservableTransformer([List<String> files]) : _files = files; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 58 return transform.primaryInput.readAsString().then((content) { | 58 return transform.primaryInput.readAsString().then((content) { |
| 59 // Do a quick string check to determine if this is this file even | 59 // Do a quick string check to determine if this is this file even |
| 60 // plausibly might need to be transformed. If not, we can avoid an | 60 // plausibly might need to be transformed. If not, we can avoid an |
| 61 // expensive parse. | 61 // expensive parse. |
| 62 if (!observableMatcher.hasMatch(content)) return; | 62 if (!observableMatcher.hasMatch(content)) return; |
| 63 | 63 |
| 64 var id = transform.primaryInput.id; | 64 var id = transform.primaryInput.id; |
| 65 // TODO(sigmund): improve how we compute this url | 65 // TODO(sigmund): improve how we compute this url |
| 66 var url = id.path.startsWith('lib/') | 66 var url = id.path.startsWith('lib/') |
| 67 ? 'package:${id.package}/${id.path.substring(4)}' : id.path; | 67 ? 'package:${id.package}/${id.path.substring(4)}' : id.path; |
| 68 var sourceFile = new SourceFile.text(url, content); | 68 var sourceFile = new SourceFile(content, url: url); |
| 69 var transaction = _transformCompilationUnit( | 69 var transaction = _transformCompilationUnit( |
| 70 content, sourceFile, transform.logger); | 70 content, sourceFile, transform.logger); |
| 71 if (!transaction.hasEdits) { | 71 if (!transaction.hasEdits) { |
| 72 transform.addOutput(transform.primaryInput); | 72 transform.addOutput(transform.primaryInput); |
| 73 return; | 73 return; |
| 74 } | 74 } |
| 75 var printer = transaction.commit(); | 75 var printer = transaction.commit(); |
| 76 // TODO(sigmund): emit source maps when barback supports it (see | 76 // TODO(sigmund): emit source maps when barback supports it (see |
| 77 // dartbug.com/12340) | 77 // dartbug.com/12340) |
| 78 printer.build(url); | 78 printer.build(url); |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 token = token.next; | 417 token = token.next; |
| 418 } | 418 } |
| 419 return token; | 419 return token; |
| 420 } | 420 } |
| 421 | 421 |
| 422 // TODO(sigmund): remove hard coded Polymer support (@published). The proper way | 422 // TODO(sigmund): remove hard coded Polymer support (@published). The proper way |
| 423 // to do this would be to switch to use the analyzer to resolve whether | 423 // to do this would be to switch to use the analyzer to resolve whether |
| 424 // annotations are subtypes of ObservableProperty. | 424 // annotations are subtypes of ObservableProperty. |
| 425 final observableMatcher = | 425 final observableMatcher = |
| 426 new RegExp("@(published|observable|PublishedProperty|ObservableProperty)"); | 426 new RegExp("@(published|observable|PublishedProperty|ObservableProperty)"); |
| OLD | NEW |