Chromium Code Reviews| 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; |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 /// For example if we had the ClassDeclaration node for `FontElement`: | 342 /// For example if we had the ClassDeclaration node for `FontElement`: |
| 343 /// | 343 /// |
| 344 /// @JsName('HTMLFontElement') | 344 /// @JsName('HTMLFontElement') |
| 345 /// @deprecated | 345 /// @deprecated |
| 346 /// class FontElement { ... } | 346 /// class FontElement { ... } |
| 347 /// | 347 /// |
| 348 /// We could match `@deprecated` with a test function like: | 348 /// We could match `@deprecated` with a test function like: |
| 349 /// | 349 /// |
| 350 /// (v) => v.type.name == 'Deprecated' && v.type.element.library.isDartCore | 350 /// (v) => v.type.name == 'Deprecated' && v.type.element.library.isDartCore |
| 351 /// | 351 /// |
| 352 DartObjectImpl getAnnotationValue( | 352 DartObjectImpl findNodeAnnotation( |
| 353 AnnotatedNode node, bool test(DartObjectImpl value)) { | 353 AnnotatedNode node, bool test(DartObjectImpl value)) { |
|
vsm
2015/05/20 19:15:29
Could this just call findElementAnnotation(node.el
Jennifer Messerly
2015/05/20 19:25:32
you'd think so, but AnnotatedNode doesn't have .el
vsm
2015/05/20 19:39:50
sgtm
| |
| 354 for (var metadata in node.metadata) { | 354 for (var metadata in node.metadata) { |
| 355 ElementAnnotationImpl element = metadata.elementAnnotation; | 355 ElementAnnotationImpl element = metadata.elementAnnotation; |
| 356 if (element == null) continue; | 356 if (element == null) continue; |
| 357 | 357 |
| 358 var evalResult = element.evaluationResult; | 358 var evalResult = element.evaluationResult; |
| 359 if (evalResult == null) continue; | 359 if (evalResult == null) continue; |
| 360 | 360 |
| 361 var value = evalResult.value; | 361 var value = evalResult.value; |
| 362 if (value != null && test(value)) return value; | 362 if (value != null && test(value)) return value; |
| 363 } | 363 } |
| 364 return null; | 364 return null; |
| 365 } | 365 } |
| 366 | 366 |
| 367 /// Similar to [findNodeAnnotation] but starts from any element. | |
| 368 DartObjectImpl findElementAnnotation( | |
| 369 Element element, bool test(DartObjectImpl value)) { | |
| 370 for (var metadata in element.metadata) { | |
| 371 var evalResult = metadata.evaluationResult; | |
| 372 if (evalResult == null) continue; | |
| 373 | |
| 374 var value = evalResult.value; | |
| 375 if (value != null && test(value)) return value; | |
| 376 } | |
| 377 return null; | |
| 378 } | |
| 379 | |
| 367 /// Given a constant [value], a [fieldName], and an [expectedType], returns the | 380 /// Given a constant [value], a [fieldName], and an [expectedType], returns the |
| 368 /// value of that field. | 381 /// value of that field. |
| 369 /// | 382 /// |
| 370 /// If the field is missing or is not [expectedType], returns null. | 383 /// If the field is missing or is not [expectedType], returns null. |
| 371 Object getConstantField( | 384 Object getConstantField( |
| 372 DartObjectImpl value, String fieldName, DartType expectedType) { | 385 DartObjectImpl value, String fieldName, DartType expectedType) { |
| 373 if (value == null) return null; | 386 if (value == null) return null; |
| 374 var f = value.fields[fieldName]; | 387 var f = value.fields[fieldName]; |
| 375 return (f == null || f.type != expectedType) ? null : f.value; | 388 return (f == null || f.type != expectedType) ? null : f.value; |
| 376 } | 389 } |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 453 int lineEnd = endLoc.offset; | 466 int lineEnd = endLoc.offset; |
| 454 int unitEnd = unit.endToken.end; | 467 int unitEnd = unit.endToken.end; |
| 455 int lineNum = lineInfo.getLocation(lineEnd).lineNumber; | 468 int lineNum = lineInfo.getLocation(lineEnd).lineNumber; |
| 456 while (lineEnd < unitEnd && | 469 while (lineEnd < unitEnd && |
| 457 lineInfo.getLocation(++lineEnd).lineNumber == lineNum); | 470 lineInfo.getLocation(++lineEnd).lineNumber == lineNum); |
| 458 | 471 |
| 459 var text = content.substring(start, end); | 472 var text = content.substring(start, end); |
| 460 var lineText = content.substring(lineStart, lineEnd); | 473 var lineText = content.substring(lineStart, lineEnd); |
| 461 return new SourceSpanWithContext(startLoc, endLoc, text, lineText); | 474 return new SourceSpanWithContext(startLoc, endLoc, text, lineText); |
| 462 } | 475 } |
| OLD | NEW |