| 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 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 } else if (value is String) { | 38 } else if (value is String) { |
| 39 files = [value]; | 39 files = [value]; |
| 40 error = false; | 40 error = false; |
| 41 } else { | 41 } else { |
| 42 error = true; | 42 error = true; |
| 43 } | 43 } |
| 44 if (error) print('Invalid value for "files" in the observe transformer.'); | 44 if (error) print('Invalid value for "files" in the observe transformer.'); |
| 45 return files; | 45 return files; |
| 46 } | 46 } |
| 47 | 47 |
| 48 Future<bool> isPrimary(Asset input) { | 48 Future<bool> isPrimary(AssetId id) => new Future.value( |
| 49 if (input.id.extension != '.dart' || | 49 id.extension != '.dart' || (_files != null && !_files.contains(id.path))); |
| 50 (_files != null && !_files.contains(input.id.path))) { | |
| 51 return new Future.value(false); | |
| 52 } | |
| 53 // Note: technically we should parse the file to find accurately the | |
| 54 // observable annotation, but that seems expensive. It would require almost | |
| 55 // as much work as applying the transform. We rather have some false | |
| 56 // positives here, and then generate no outputs when we apply this | |
| 57 // transform. | |
| 58 return input.readAsString().then( | |
| 59 (c) => c.contains("@observable") || c.contains("@published")); | |
| 60 } | |
| 61 | 50 |
| 62 Future apply(Transform transform) { | 51 Future apply(Transform transform) { |
| 63 return transform.primaryInput.readAsString().then((content) { | 52 return transform.primaryInput.readAsString().then((content) { |
| 53 // Do a quick string check to determine if this is this file even |
| 54 // plausibly might need to be transformed. If not, we can avoid an |
| 55 // expensive parse. |
| 56 if (!content.contains("@observable") && !content.contains("@published")) { |
| 57 return; |
| 58 } |
| 59 |
| 64 var id = transform.primaryInput.id; | 60 var id = transform.primaryInput.id; |
| 65 // TODO(sigmund): improve how we compute this url | 61 // TODO(sigmund): improve how we compute this url |
| 66 var url = id.path.startsWith('lib/') | 62 var url = id.path.startsWith('lib/') |
| 67 ? 'package:${id.package}/${id.path.substring(4)}' : id.path; | 63 ? 'package:${id.package}/${id.path.substring(4)}' : id.path; |
| 68 var sourceFile = new SourceFile.text(url, content); | 64 var sourceFile = new SourceFile.text(url, content); |
| 69 var transaction = _transformCompilationUnit( | 65 var transaction = _transformCompilationUnit( |
| 70 content, sourceFile, transform.logger); | 66 content, sourceFile, transform.logger); |
| 71 if (!transaction.hasEdits) { | 67 if (!transaction.hasEdits) { |
| 72 transform.addOutput(transform.primaryInput); | 68 transform.addOutput(transform.primaryInput); |
| 73 return; | 69 return; |
| (...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 | 422 |
| 427 Token _findFieldSeperator(Token token) { | 423 Token _findFieldSeperator(Token token) { |
| 428 while (token != null) { | 424 while (token != null) { |
| 429 if (token.type == TokenType.COMMA || token.type == TokenType.SEMICOLON) { | 425 if (token.type == TokenType.COMMA || token.type == TokenType.SEMICOLON) { |
| 430 break; | 426 break; |
| 431 } | 427 } |
| 432 token = token.next; | 428 token = token.next; |
| 433 } | 429 } |
| 434 return token; | 430 return token; |
| 435 } | 431 } |
| OLD | NEW |