import 'dart:io';
import 'dart:convert';
import 'package:source_maps/source_maps.dart';

main(List<String> args) {
  if (args.length < 2) {
    print('usage: read.dart <source-map-file> <name>');
    exit(1);
  }
  var sourcemapFile = new File.fromUri(Uri.base.resolve(args[0]));
  if (!sourcemapFile.existsSync()) {
    print('no source-map-file in ${args[0]}');
    exit(1);
  }
  var name = args[1];
  var json = jsonDecode(sourcemapFile.readAsStringSync());
  SingleMapping mapping = parseJson(json);
  var extensions = json['x_org_dartlang_dart2js'];
  if (extensions == null) {
    print('source-map file has no dart2js extensions');
    exit(1);
  }
  var minifiedNames = extensions['minified_names'];
  if (minifiedNames == null) {
    print('source-map file has no minified names in the dart2js extensions');
    exit(1);
  }
  var gid = minifiedNames['global'][name];
  if (gid != null) print('$name => ${mapping.names[gid]} (a global name)');
  var iid = minifiedNames['instance'][name];
  if (iid != null) print('$name => ${mapping.names[iid]} (an instance name)');
  if (gid == null && iid == null) print('Name \'$name\' not found.');
}

import 'dart:io';
import 'dart:convert';
import 'package:source_maps/source_maps.dart';
import 'package:dart2js_tools/src/dart2js_mapping.dart';

main(List<String> args) {
  if (args.length < 2) {
    print('usage: read.dart <source-map-file> <name>');
    exit(1);
  }
  var name = args[1];

  var sourcemapFile = new File.fromUri(Uri.base.resolve(args[0]));
  if (!sourcemapFile.existsSync()) {
    print('Error: no such file: $sourcemapFile');
    exit(1);
  }
  var json = jsonDecode(sourcemapFile.readAsStringSync());
  Dart2jsMapping mapping = Dart2jsMapping(parseJson(json), json);
  var global = mapping.globalNames[name];
  if (global != null) print('$name => $global (a global name)');
  var instance = mapping.instanceNames[name];
  if (instance != null) print('$name => $instance (an instance name)');
  if (global == null && instance == null) print('Name \'$name\' not found.');
}

Update lookup-name tool to use the dart2js_mapping implementation.
Update lookup-name tool to use the dart2js_mapping implementation.

Note: the current implementation broke when we changed the encoding.
Change-Id: I48d9e5fedad3f3be85fc1e73dd5dd091bb7278b0
Reviewed-on: https://dart-review.googlesource.com/c/84881
Commit-Queue: Sigmund Cherem <beaea8fd7d2a56555046ba4f362c8a93e3938576@google.com>
Commit-Queue: Stephen Adams <60cb5b73b7e0cb7dcda53302da4fab6ecfc7fa8b@google.com>
Auto-Submit: Sigmund Cherem <beaea8fd7d2a56555046ba4f362c8a93e3938576@google.com>
Reviewed-by: Stephen Adams <60cb5b73b7e0cb7dcda53302da4fab6ecfc7fa8b@google.com>
