<commit_before>import 'package:analyzer_experimental/src/generated/java_io.dart';
import 'package:analyzer_experimental/src/generated/source_io.dart';
import 'package:analyzer_experimental/src/generated/ast.dart';
import 'package:analyzer_experimental/src/generated/sdk.dart' show DartSdk;
import 'package:analyzer_experimental/src/generated/sdk_io.dart' show DirectoryBasedDartSdk;
import 'package:analyzer_experimental/src/generated/element.dart';
import 'package:analyzer_experimental/src/generated/engine.dart';

import 'dart:io';

const String PACKAGE_PREFIX = 'package:';
const String DART_PACKAGE_PREFIX = 'dart:';

main() {
  var args = new Options().arguments;
  if (args.length < 5) {
    print('Usage: generator path_to_sdk file_to_resolve annotations output package_roots+');
    exit(0);
  }

  var pathToSdk = args[0];
  var entryPoint = args[1];
  var classAnnotations = args[2].split(',');
  var output = args[3];
  var packageRoots = args.sublist(4);

  print('pathToSdk: $pathToSdk');
  print('entryPoint: $entryPoint');
  print('classAnnotations: ${classAnnotations.join(', ')}');
  print('output: $output');
  print('packageRoots: $packageRoots');

  var c = new SourceCrawler(pathToSdk, packageRoots);
  List<String> imports = <String>[];
  List<ClassElement> typeFactoryTypes = <ClassElement>[];
  Map<String, String> typeToImport = new Map<String, String>();
  c.crawl(entryPoint, (CompilationUnitElement compilationUnit, SourceFile source) {
      new CompilationUnitVisitor(c.context, source, classAnnotations, imports,
          typeToImport, typeFactoryTypes).visit(compilationUnit);
  });
  var code = printLibraryCode(typeToImport, imports, typeFactoryTypes);
  new File(output).writeAsStringSync(code);
}

String printLibraryCode(Map<String, String> typeToImport, List<String> imports,
                      List<ClassElement> typeFactoryTypes) {
  List<String> requiredImports = <String>[];
  StringBuffer factories = new StringBuffer();

  String resolveClassIdentifier(InterfaceType type) {
    if ((type.element as ClassElement).library.isDartCore) {
      return type.name;
    }
    String import = typeToImport[getCanonicalName(type)];
    if (!requiredImports.contains(import)) {
      requiredImports.add(import);
    }
    return 'import_${imports.indexOf(import)}.${type.name}';
  }

  typeFactoryTypes.forEach((ClassElement clazz) {
    factories.write(
        'typeFactories[${resolveClassIdentifier(clazz.type)}] = (f) => ');
    factories.write('new ${resolveClassIdentifier(clazz.type)}(');
    ConstructorElement constr =
        clazz.constructors.firstWhere((c) => c.name.isEmpty);
    factories.write(constr.parameters.map((param) =>
        'f(${resolveClassIdentifier(param.type)})').join(', '));
    factories.write(');\n');
  });
  StringBuffer code = new StringBuffer();
  code.write('library di.generated.type_factories;\n');
  requiredImports.forEach((import) {
    code.write ('import "$import" as import_${imports.indexOf(import)};\n');
  });
  code.write('var typeFactories = new Map<Type, Function>();\n');
  code.write('main() {\n');
  code.write(factories);
  code.write('}\n');

  return code.toString();
}

class CompilationUnitVisitor {
  List<String> imports;
  Map<String, String> typeToImport;
  List<ClassElement> typeFactoryTypes;
  List<String> classAnnotations;
  SourceFile source;
  AnalysisContext context;
<commit_msg>fix: removed generated Map type annotation, fails in checked mode<commit_after>import 'package:analyzer_experimental/src/generated/java_io.dart';
import 'package:analyzer_experimental/src/generated/source_io.dart';
import 'package:analyzer_experimental/src/generated/ast.dart';
import 'package:analyzer_experimental/src/generated/sdk.dart' show DartSdk;
import 'package:analyzer_experimental/src/generated/sdk_io.dart' show DirectoryBasedDartSdk;
import 'package:analyzer_experimental/src/generated/element.dart';
import 'package:analyzer_experimental/src/generated/engine.dart';

import 'dart:io';

const String PACKAGE_PREFIX = 'package:';
const String DART_PACKAGE_PREFIX = 'dart:';

main() {
  var args = new Options().arguments;
  if (args.length < 5) {
    print('Usage: generator path_to_sdk file_to_resolve annotations output package_roots+');
    exit(0);
  }

  var pathToSdk = args[0];
  var entryPoint = args[1];
  var classAnnotations = args[2].split(',');
  var output = args[3];
  var packageRoots = args.sublist(4);

  print('pathToSdk: $pathToSdk');
  print('entryPoint: $entryPoint');
  print('classAnnotations: ${classAnnotations.join(', ')}');
  print('output: $output');
  print('packageRoots: $packageRoots');

  var c = new SourceCrawler(pathToSdk, packageRoots);
  List<String> imports = <String>[];
  List<ClassElement> typeFactoryTypes = <ClassElement>[];
  Map<String, String> typeToImport = new Map<String, String>();
  c.crawl(entryPoint, (CompilationUnitElement compilationUnit, SourceFile source) {
      new CompilationUnitVisitor(c.context, source, classAnnotations, imports,
          typeToImport, typeFactoryTypes).visit(compilationUnit);
  });
  var code = printLibraryCode(typeToImport, imports, typeFactoryTypes);
  new File(output).writeAsStringSync(code);
}

String printLibraryCode(Map<String, String> typeToImport, List<String> imports,
                      List<ClassElement> typeFactoryTypes) {
  List<String> requiredImports = <String>[];
  StringBuffer factories = new StringBuffer();

  String resolveClassIdentifier(InterfaceType type) {
    if ((type.element as ClassElement).library.isDartCore) {
      return type.name;
    }
    String import = typeToImport[getCanonicalName(type)];
    if (!requiredImports.contains(import)) {
      requiredImports.add(import);
    }
    return 'import_${imports.indexOf(import)}.${type.name}';
  }

  typeFactoryTypes.forEach((ClassElement clazz) {
    factories.write(
        'typeFactories[${resolveClassIdentifier(clazz.type)}] = (f) => ');
    factories.write('new ${resolveClassIdentifier(clazz.type)}(');
    ConstructorElement constr =
        clazz.constructors.firstWhere((c) => c.name.isEmpty);
    factories.write(constr.parameters.map((param) =>
        'f(${resolveClassIdentifier(param.type)})').join(', '));
    factories.write(');\n');
  });
  StringBuffer code = new StringBuffer();
  code.write('library di.generated.type_factories;\n');
  requiredImports.forEach((import) {
    code.write ('import "$import" as import_${imports.indexOf(import)};\n');
  });
  code.write('var typeFactories = new Map();\n');
  code.write('main() {\n');
  code.write(factories);
  code.write('}\n');

  return code.toString();
}

class CompilationUnitVisitor {
  List<String> imports;
  Map<String, String> typeToImport;
  List<ClassElement> typeFactoryTypes;
  List<String> classAnnotations;
  SourceFile source;
  AnalysisContext context;

{'fraction_non_alphanumeric': 0.14308728763913298, 'fraction_numerical': 0.002050380785002929, 'mean_word_length': 5.294009216589862, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3902987', 'n_tokens_mistral': 2147, 'n_tokens_neox': 2095, 'n_words': 386}