| 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 Expression, | 20 Expression, |
| 21 SimpleIdentifier; | 21 SimpleIdentifier; |
| 22 import 'package:analyzer/src/generated/constant.dart' show DartObjectImpl; |
| 22 import 'package:analyzer/src/generated/element.dart'; | 23 import 'package:analyzer/src/generated/element.dart'; |
| 23 import 'package:analyzer/src/generated/engine.dart' | 24 import 'package:analyzer/src/generated/engine.dart' |
| 24 show ParseDartTask, AnalysisContext; | 25 show ParseDartTask, AnalysisContext; |
| 25 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; | 26 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; |
| 26 import 'package:analyzer/src/generated/source.dart' show Source; | 27 import 'package:analyzer/src/generated/source.dart' show Source; |
| 27 import 'package:analyzer/analyzer.dart' show parseDirectives; | 28 import 'package:analyzer/analyzer.dart' show parseDirectives; |
| 28 import 'package:crypto/crypto.dart' show CryptoUtils, MD5; | 29 import 'package:crypto/crypto.dart' show CryptoUtils, MD5; |
| 29 import 'package:source_span/source_span.dart'; | 30 import 'package:source_span/source_span.dart'; |
| 30 import 'package:yaml/yaml.dart'; | 31 import 'package:yaml/yaml.dart'; |
| 31 | 32 |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 // It seems like we should know our package's root directory without needing | 340 // It seems like we should know our package's root directory without needing |
| 340 // to search like this. | 341 // to search like this. |
| 341 var pubspec = | 342 var pubspec = |
| 342 loadYaml(new File(path.join(dir, 'pubspec.yaml')).readAsStringSync()); | 343 loadYaml(new File(path.join(dir, 'pubspec.yaml')).readAsStringSync()); |
| 343 | 344 |
| 344 // Ensure this is loaded from the dev_compiler package. | 345 // Ensure this is loaded from the dev_compiler package. |
| 345 if (pubspec['name'] != 'dev_compiler') return null; | 346 if (pubspec['name'] != 'dev_compiler') return null; |
| 346 return path.join('dev_compiler', 'runtime', filename); | 347 return path.join('dev_compiler', 'runtime', filename); |
| 347 } | 348 } |
| 348 | 349 |
| 350 /// Given an annotated [node] and a [test] function, returns the first matching |
| 351 /// constant valued annotation. |
| 352 /// |
| 353 /// For example if we had the ClassDeclaration node for `FontElement`: |
| 354 /// |
| 355 /// @JsName('HTMLFontElement') |
| 356 /// @deprecated |
| 357 /// class FontElement { ... } |
| 358 /// |
| 359 /// We could match `@deprecated` with a test function like: |
| 360 /// |
| 361 /// (v) => v.type.name == 'Deprecated' && v.type.element.library.isDartCore |
| 362 /// |
| 363 DartObjectImpl getAnnotationValue( |
| 364 AnnotatedNode node, bool test(DartObjectImpl value)) { |
| 365 for (var metadata in node.metadata) { |
| 366 ElementAnnotationImpl element = metadata.elementAnnotation; |
| 367 if (element == null) continue; |
| 368 |
| 369 var evalResult = element.evaluationResult; |
| 370 if (evalResult == null) continue; |
| 371 |
| 372 var value = evalResult.value; |
| 373 if (value != null && test(value)) return value; |
| 374 } |
| 375 return null; |
| 376 } |
| 377 |
| 378 /// Given a constant [value], a [fieldName], and an [expectedType], returns the |
| 379 /// value of that field. |
| 380 /// |
| 381 /// If the field is missing or is not [expectedType], returns null. |
| 382 Object getConstantField( |
| 383 DartObjectImpl value, String fieldName, DartType expectedType) { |
| 384 if (value == null) return null; |
| 385 var f = value.fields[fieldName]; |
| 386 return (f == null || f.type != expectedType) ? null : f.value; |
| 387 } |
| 388 |
| 349 InterfaceType fillDynamicTypeArgs(InterfaceType t, TypeProvider types) => | 389 InterfaceType fillDynamicTypeArgs(InterfaceType t, TypeProvider types) => |
| 350 t.substitute4(new List.filled(t.typeArguments.length, types.dynamicType)); | 390 t.substitute4(new List.filled(t.typeArguments.length, types.dynamicType)); |
| OLD | NEW |