generateAstMap function

Future<Map> generateAstMap(
  1. String path, {
  2. String? sourcePath,
})

Implementation

Future<Map> generateAstMap(String path, {String? sourcePath}) async {
  if (path.isEmpty) {
    stdout.writeln('File not found');
  } else {
    await _pathCheck(path);
    if (exitCode == 2) {
      try {
        ///FeatureSet.latestLanguageVersion()方法待测试
        // var parseResult = parseFile(path: path, featureSet: FeatureSet.latestLanguageVersion());
        var collection = AnalysisContextCollection(includedPaths: [path]);
        // 为了获取 Function 的描述,改用该方法
        var parseResult = await collection
            .contextFor(path)
            .currentSession
            .getResolvedUnit(path) as ResolvedUnitResult;

        var compilationUnit = parseResult.unit;

        //遍历AST
        //buildStep.inputId.uri
        var astData = compilationUnit.accept(
            CustomAstVisitor(sourcePath, await File(path).readAsString()));

        // var encode = json.encode(astData);
        // print(encode);
        // stdout.writeln('################gen_ast_begin######################');
        // stdout.writeln(jsonEncode(astData));
        // stdout.writeln('################gen_ast_end######################');
        return Future.value(astData);
      } catch (e) {
        stdout.writeln('Visit dart ast error: ${e.toString()}');
      }
    }
  }
  return Future.value();
}