Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(639)

Side by Side Diff: lib/src/utils.dart

Issue 1084783005: use Analyzer's computed constants instead of pattern matching the annotation AST (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/engine.dart' 22 import 'package:analyzer/src/generated/engine.dart'
23 show ParseDartTask, AnalysisContext; 23 show ParseDartTask, AnalysisContext;
24 import 'package:analyzer/src/generated/source.dart' show Source; 24 import 'package:analyzer/src/generated/source.dart' show Source;
25 import 'package:analyzer/src/generated/element.dart'; 25 import 'package:analyzer/src/generated/element.dart';
26 import 'package:analyzer/src/generated/constant.dart' show DartObjectImpl;
26 import 'package:analyzer/analyzer.dart' show parseDirectives; 27 import 'package:analyzer/analyzer.dart' show parseDirectives;
27 import 'package:crypto/crypto.dart' show CryptoUtils, MD5; 28 import 'package:crypto/crypto.dart' show CryptoUtils, MD5;
28 import 'package:source_span/source_span.dart'; 29 import 'package:source_span/source_span.dart';
29 import 'package:yaml/yaml.dart'; 30 import 'package:yaml/yaml.dart';
30 31
31 import 'codegen/js_names.dart' show invalidJSVariableName; 32 import 'codegen/js_names.dart' show invalidJSVariableName;
32 33
33 bool isDartPrivateLibrary(LibraryElement library) { 34 bool isDartPrivateLibrary(LibraryElement library) {
34 var uri = library.source.uri; 35 var uri = library.source.uri;
35 if (uri.scheme != "dart") return false; 36 if (uri.scheme != "dart") return false;
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 // TODO(jmesserly): can we implement this without repeatedly reading pubspec? 338 // TODO(jmesserly): can we implement this without repeatedly reading pubspec?
338 // It seems like we should know our package's root directory without needing 339 // It seems like we should know our package's root directory without needing
339 // to search like this. 340 // to search like this.
340 var pubspec = 341 var pubspec =
341 loadYaml(new File(path.join(dir, 'pubspec.yaml')).readAsStringSync()); 342 loadYaml(new File(path.join(dir, 'pubspec.yaml')).readAsStringSync());
342 343
343 // Ensure this is loaded from the dev_compiler package. 344 // Ensure this is loaded from the dev_compiler package.
344 if (pubspec['name'] != 'dev_compiler') return null; 345 if (pubspec['name'] != 'dev_compiler') return null;
345 return path.join('dev_compiler', 'runtime', filename); 346 return path.join('dev_compiler', 'runtime', filename);
346 } 347 }
348
349 /// Given an annotated [node] and a [test] function, returns the first matching
350 /// constant valued annotation.
351 ///
352 /// For example if we had the ClassDeclaration node for `FontElement`:
353 ///
354 /// @JsName('HTMLFontElement')
355 /// @deprecated
356 /// class FontElement { ... }
357 ///
358 /// We could match `@deprecated` with a test function like:
359 ///
360 /// (v) => v.type.name == 'Deprecated' && v.type.element.library.isDartCore
361 ///
362 DartObjectImpl getAnnotationValue(
363 AnnotatedNode node, bool test(DartObjectImpl value)) {
364 for (var metadata in node.metadata) {
365 ElementAnnotationImpl element = metadata.elementAnnotation;
366 if (element == null) continue;
367
368 var evalResult = element.evaluationResult;
369 if (evalResult == null) continue;
370
371 var value = evalResult.value;
372 if (value != null && test(value)) return value;
373 }
374 return null;
375 }
376
377 /// Given a constant [value] and a [fieldName], returns the string value of
378 /// that field.
379 ///
380 /// If the field is missing or is not a string, returns null.
381 String getStringConstantField(DartObjectImpl value, String fieldName) {
382 if (value == null) return null;
383 var fieldValue = value.fields[fieldName];
384 if (fieldValue == null) return null;
385 var result = fieldValue.value;
386 return result is String ? result : null;
Paul Berry 2015/04/14 17:51:27 Not sure if it matters, but the analyzer stores bo
Jennifer Messerly 2015/04/15 00:21:26 ah good catch! I'll update it to fix that.
387 }
OLDNEW
« lib/src/codegen/js_codegen.dart ('K') | « lib/src/codegen/js_codegen.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698