parseDartFile function

  1. @Deprecated('Please use parseFile instead')
CompilationUnit parseDartFile (
  1. String path,
  2. {bool suppressErrors: false,
  3. bool parseFunctionBodies: true,
  4. 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 parseFile function (from package:analyzer/dart/analysis/utilities.dart) instead.

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

Implementation

@Deprecated('Please use parseFile 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 = File(path).readAsStringSync();
  var sourceFactory =
      SourceFactory([ResourceUriResolver(PhysicalResourceProvider.INSTANCE)]);

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

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