| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// Holds a couple utility functions used at various places in the system. | 5 /// Holds a couple utility functions used at various places in the system. |
| 6 library dev_compiler.src.utils; | 6 library dev_compiler.src.utils; |
| 7 | 7 |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
| 11 import 'package:analyzer/src/generated/ast.dart' | 11 import 'package:analyzer/src/generated/ast.dart' |
| 12 show | 12 show |
| 13 ImportDirective, | 13 ImportDirective, |
| 14 ExportDirective, | 14 ExportDirective, |
| 15 PartDirective, | 15 PartDirective, |
| 16 CompilationUnit, | 16 CompilationUnit, |
| 17 Identifier, | 17 Identifier, |
| 18 AnnotatedNode, | 18 AnnotatedNode, |
| 19 AstNode; | 19 AstNode; |
| 20 import 'package:analyzer/src/generated/engine.dart' | 20 import 'package:analyzer/src/generated/engine.dart' |
| 21 show ParseDartTask, AnalysisContext; | 21 show ParseDartTask, AnalysisContext; |
| 22 import 'package:analyzer/src/generated/source.dart' show Source; | 22 import 'package:analyzer/src/generated/source.dart' show Source; |
| 23 import 'package:analyzer/src/generated/element.dart'; | 23 import 'package:analyzer/src/generated/element.dart'; |
| 24 import 'package:analyzer/analyzer.dart' show parseDirectives; | 24 import 'package:analyzer/analyzer.dart' show parseDirectives; |
| 25 import 'package:crypto/crypto.dart' show CryptoUtils, MD5; | 25 import 'package:crypto/crypto.dart' show CryptoUtils, MD5; |
| 26 import 'package:source_span/source_span.dart'; | 26 import 'package:source_span/source_span.dart'; |
| 27 import 'package:yaml/yaml.dart'; | 27 import 'package:yaml/yaml.dart'; |
| 28 | 28 |
| 29 import 'js/keywords.dart'; | 29 import 'codegen/js_names.dart' show invalidJSVariableName; |
| 30 | 30 |
| 31 bool isDartPrivateLibrary(LibraryElement library) { | 31 bool isDartPrivateLibrary(LibraryElement library) { |
| 32 var uri = library.source.uri; | 32 var uri = library.source.uri; |
| 33 if (uri.scheme != "dart") return false; | 33 if (uri.scheme != "dart") return false; |
| 34 return Identifier.isPrivateName(uri.path); | 34 return Identifier.isPrivateName(uri.path); |
| 35 } | 35 } |
| 36 | 36 |
| 37 /// Choose a canonical name from the library element. This is safe to use as a | 37 /// Choose a canonical name from the library element. This is safe to use as a |
| 38 /// namespace in JS and Dart code generation. This never uses the library's | 38 /// namespace in JS and Dart code generation. This never uses the library's |
| 39 /// name (the identifier in the `library` declaration) as it doesn't have any | 39 /// name (the identifier in the `library` declaration) as it doesn't have any |
| (...skipping 15 matching lines...) Expand all Loading... |
| 55 var needsEscape = ch == r'$' || _invalidCharInIdentifier.hasMatch(ch); | 55 var needsEscape = ch == r'$' || _invalidCharInIdentifier.hasMatch(ch); |
| 56 if (needsEscape && buffer == null) { | 56 if (needsEscape && buffer == null) { |
| 57 buffer = new StringBuffer(name.substring(0, i)); | 57 buffer = new StringBuffer(name.substring(0, i)); |
| 58 } | 58 } |
| 59 if (buffer != null) { | 59 if (buffer != null) { |
| 60 buffer.write(needsEscape ? '\$${ch.codeUnits.join("")}' : ch); | 60 buffer.write(needsEscape ? '\$${ch.codeUnits.join("")}' : ch); |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 | 63 |
| 64 var result = buffer != null ? '$buffer' : name; | 64 var result = buffer != null ? '$buffer' : name; |
| 65 // Ensure the idenifier first character is not numeric and that the whole | 65 // Ensure the identifier first character is not numeric and that the whole |
| 66 // identifier is not a keyword. | 66 // identifier is not a keyword. |
| 67 if (result.startsWith(new RegExp('[0-9]')) || isJsKeyword(result)) { | 67 if (result.startsWith(new RegExp('[0-9]')) || invalidJSVariableName(result)) { |
| 68 return '\$$result'; | 68 return '\$$result'; |
| 69 } | 69 } |
| 70 return result; | 70 return result; |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Invalid characters for identifiers, which would need to be escaped. | 73 // Invalid characters for identifiers, which would need to be escaped. |
| 74 final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]'); | 74 final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]'); |
| 75 | 75 |
| 76 /// Returns all libraries transitively imported or exported from [start]. | 76 /// Returns all libraries transitively imported or exported from [start]. |
| 77 Iterable<LibraryElement> reachableLibraries(LibraryElement start) { | 77 Iterable<LibraryElement> reachableLibraries(LibraryElement start) { |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 // TODO(jmesserly): can we implement this without repeatedly reading pubspec? | 329 // TODO(jmesserly): can we implement this without repeatedly reading pubspec? |
| 330 // It seems like we should know our package's root directory without needing | 330 // It seems like we should know our package's root directory without needing |
| 331 // to search like this. | 331 // to search like this. |
| 332 var pubspec = | 332 var pubspec = |
| 333 loadYaml(new File(path.join(dir, 'pubspec.yaml')).readAsStringSync()); | 333 loadYaml(new File(path.join(dir, 'pubspec.yaml')).readAsStringSync()); |
| 334 | 334 |
| 335 // Ensure this is loaded from the dev_compiler package. | 335 // Ensure this is loaded from the dev_compiler package. |
| 336 if (pubspec['name'] != 'dev_compiler') return null; | 336 if (pubspec['name'] != 'dev_compiler') return null; |
| 337 return path.join('dev_compiler', 'runtime', filename); | 337 return path.join('dev_compiler', 'runtime', filename); |
| 338 } | 338 } |
| OLD | NEW |