Add simple binary that prints the inferred type information of a single function
BUG=
R=sra@google.com
Review-Url: https://codereview.chromium.org//2758843003 .
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1090b68..c160295 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 0.5.4
+
+- Added script to show inferred types of functions and fields on the command
+ line.
+
## 0.5.3+1
- Improved the stability of `ConstantInfo.id`.
diff --git a/bin/show_inferred_types.dart b/bin/show_inferred_types.dart
new file mode 100644
index 0000000..f8d509a
--- /dev/null
+++ b/bin/show_inferred_types.dart
@@ -0,0 +1,58 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+/// Simple script that shows the inferred types of a function.
+library compiler.tool.show_inferred_types;
+
+import 'dart:convert';
+import 'dart:io';
+
+import 'package:dart2js_info/info.dart';
+import 'package:dart2js_info/src/util.dart';
+
+main(args) {
+ if (args.length < 2) {
+ var scriptName = Platform.script.pathSegments.last;
+ print('usage: dart $scriptName <info.json> <function-name-regex> [-l]');
+ print(' -l: print the long qualified name for a function.');
+ exit(1);
+ }
+
+ var showLongName = args.length > 2 && args[2] == '-l';
+
+ var json = JSON.decode(new File(args[0]).readAsStringSync());
+ var info = new AllInfoJsonCodec().decode(json);
+ var nameRegExp = new RegExp(args[1]);
+ matches(e) => nameRegExp.hasMatch(longName(e));
+
+ bool noResults = true;
+ void showMethods() {
+ var sources = info.functions.where(matches).toList();
+ if (sources.isEmpty) return;
+ noResults = false;
+ for (var s in sources) {
+ var params = s.parameters.map((p) => '${p.name}: ${p.type}').join(', ');
+ var name = showLongName ? longName(s) : s.name;
+ print('$name($params): ${s.returnType}');
+ }
+ }
+
+ void showFields() {
+ var sources = info.fields.where(matches).toList();
+ if (sources.isEmpty) return;
+ noResults = false;
+ for (var s in sources) {
+ var name = showLongName ? longName(s) : s.name;
+ print('$name: ${s.inferredType}');
+ }
+ }
+
+ showMethods();
+ showFields();
+ if (noResults) {
+ print('error: no function or field that matches ${args[1]} was found.');
+ exit(1);
+ }
+}
+
diff --git a/pubspec.yaml b/pubspec.yaml
index efe0d4d..16c5aef 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: dart2js_info
-version: 0.5.3+1
+version: 0.5.4
description: >
Libraries and tools to process data produced when running dart2js with
--dump-info.
@@ -28,3 +28,4 @@
dart2js_info_function_size_analysis: function_size_analysis
dart2js_info_library_size_split: library_size_split
dart2js_info_live_code_size_analysis: live_code_size_analysis
+ dart2js_info_show_inferred_types: show_inferred_types