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 MethodInvocation; | 22 MethodInvocation; |
23 import 'package:analyzer/src/generated/constant.dart' show DartObjectImpl; | 23 import 'package:analyzer/src/generated/constant.dart' show DartObject; |
24 import 'package:analyzer/src/generated/element.dart'; | 24 import 'package:analyzer/src/generated/element.dart'; |
25 import 'package:analyzer/src/generated/engine.dart' | 25 import 'package:analyzer/src/generated/engine.dart' |
26 show ParseDartTask, AnalysisContext; | 26 show ParseDartTask, AnalysisContext; |
27 import 'package:analyzer/src/generated/error.dart' show ErrorCode; | 27 import 'package:analyzer/src/generated/error.dart' show ErrorCode; |
28 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; | 28 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; |
29 import 'package:analyzer/src/generated/source.dart' show LineInfo, Source; | 29 import 'package:analyzer/src/generated/source.dart' show LineInfo, Source; |
30 import 'package:analyzer/analyzer.dart' show parseDirectives; | 30 import 'package:analyzer/analyzer.dart' show parseDirectives; |
31 import 'package:crypto/crypto.dart' show CryptoUtils, MD5; | 31 import 'package:crypto/crypto.dart' show CryptoUtils, MD5; |
32 import 'package:source_span/source_span.dart'; | 32 import 'package:source_span/source_span.dart'; |
33 | 33 |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 /// For example if we had the ClassDeclaration node for `FontElement`: | 331 /// For example if we had the ClassDeclaration node for `FontElement`: |
332 /// | 332 /// |
333 /// @JsName('HTMLFontElement') | 333 /// @JsName('HTMLFontElement') |
334 /// @deprecated | 334 /// @deprecated |
335 /// class FontElement { ... } | 335 /// class FontElement { ... } |
336 /// | 336 /// |
337 /// We could match `@deprecated` with a test function like: | 337 /// We could match `@deprecated` with a test function like: |
338 /// | 338 /// |
339 /// (v) => v.type.name == 'Deprecated' && v.type.element.library.isDartCore | 339 /// (v) => v.type.name == 'Deprecated' && v.type.element.library.isDartCore |
340 /// | 340 /// |
341 DartObjectImpl findAnnotation( | 341 DartObject findAnnotation(Element element, bool test(DartObject value)) { |
342 Element element, bool test(DartObjectImpl value)) { | |
343 for (var metadata in element.metadata) { | 342 for (var metadata in element.metadata) { |
344 var evalResult = metadata.evaluationResult; | 343 var value = metadata.constantValue; |
345 if (evalResult == null) continue; | |
346 | |
347 var value = evalResult.value; | |
348 if (value != null && test(value)) return value; | 344 if (value != null && test(value)) return value; |
349 } | 345 } |
350 return null; | 346 return null; |
351 } | 347 } |
352 | 348 |
353 /// Given a constant [value], a [fieldName], and an [expectedType], returns the | 349 /// Given a constant [value], a [fieldName], and an [expectedType], returns the |
354 /// value of that field. | 350 /// value of that field. |
355 /// | 351 /// |
356 /// If the field is missing or is not [expectedType], returns null. | 352 /// If the field is missing or is not [expectedType], returns null. |
357 Object getConstantField( | 353 DartObject getConstantField( |
358 DartObjectImpl value, String fieldName, DartType expectedType) { | 354 DartObject value, String fieldName, DartType expectedType) { |
359 if (value == null) return null; | 355 var f = value?.getField(fieldName); |
360 var f = value.fields[fieldName]; | 356 return (f == null || f.type != expectedType) ? null : f; |
361 return (f == null || f.type != expectedType) ? null : f.value; | |
362 } | 357 } |
363 | 358 |
364 DartType fillDynamicTypeArgs(DartType t, TypeProvider types) { | 359 DartType fillDynamicTypeArgs(DartType t, TypeProvider types) { |
365 if (t is ParameterizedType) { | 360 if (t is ParameterizedType) { |
366 var dyn = new List.filled(t.typeArguments.length, types.dynamicType); | 361 var dyn = new List.filled(t.typeArguments.length, types.dynamicType); |
367 return t.substitute2(dyn, t.typeArguments); | 362 return t.substitute2(dyn, t.typeArguments); |
368 } | 363 } |
369 return t; | 364 return t; |
370 } | 365 } |
371 | 366 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 } | 420 } |
426 } | 421 } |
427 | 422 |
428 bool isInlineJS(Element e) => e is FunctionElement && | 423 bool isInlineJS(Element e) => e is FunctionElement && |
429 e.library.source.uri.toString() == 'dart:_foreign_helper' && | 424 e.library.source.uri.toString() == 'dart:_foreign_helper' && |
430 e.name == 'JS'; | 425 e.name == 'JS'; |
431 | 426 |
432 bool isDartMathMinMax(Element e) => e is FunctionElement && | 427 bool isDartMathMinMax(Element e) => e is FunctionElement && |
433 e.library.source.uri.toString() == 'dart:math' && | 428 e.library.source.uri.toString() == 'dart:math' && |
434 (e.name == 'min' || e.name == 'max'); | 429 (e.name == 'min' || e.name == 'max'); |
OLD | NEW |