parseDartFile function

  1. @Deprecated('Please use parseFile2 instead')
CompilationUnit parseDartFile (String path, { bool suppressErrors: false, bool parseFunctionBodies: true, FeatureSet featureSet })

Parses a Dart file into an AST.

Throws an AnalyzerErrorGroup if any errors occurred, unless suppressErrors is true, in which case any errors are discarded.

If parseFunctionBodies is false then only function signatures will be parsed. (Currently broken; function bodies are always parsed).

Deprecated - please use the parseFile2 function (from package:analyzer/dart/analysis/utilities.dart) instead.

Note that parseFile2 does not support the parseFunctionBodies option; callers that don't require function bodies should simply ignore them.

Implementation

@Deprecated('Please use parseFile2 instead')
CompilationUnit parseDartFile(String path,
    {bool suppressErrors: false,
    bool parseFunctionBodies: true,
    FeatureSet featureSet}) {
  // TODO(paulberry): Make featureSet a required parameter
  featureSet ??= FeatureSet.fromEnableFlags([]);
  String contents = new File(path).readAsStringSync();
  var sourceFactory = new SourceFactory(
      [new ResourceUriResolver(PhysicalResourceProvider.INSTANCE)]);

  var absolutePath = pathos.absolute(path);
  var source = sourceFactory.forUri(pathos.toUri(absolutePath).toString());
  if (source == null) {
    throw new ArgumentError("Can't get source for path $path");
  }
  if (!source.exists()) {
    throw new ArgumentError("Source $source doesn't exist");
  }

  return _parseSource(contents, source, featureSet,
      suppressErrors: suppressErrors, parseFunctionBodies: parseFunctionBodies);
}