parseContent method

CompilationUnit parseContent(
  1. String content(), {
  2. required String key,
  3. bool throwIfDiagnostics = false,
})

Parses Dart source content into a compilation unit, with caching.

If the content with the given key has been previously parsed, returns the cached result. Otherwise, parses the content and caches the result.

@param content Function that provides the source content to parse @param key Identifier to use for caching the parsed unit @param throwIfDiagnostics Whether to throw an exception if diagnostics are present @return The parsed compilation unit

Implementation

CompilationUnit parseContent(
  String Function() content, {
  required String key,
  bool throwIfDiagnostics = false,
}) {
  if (_cache.containsKey(key)) {
    return _cache[key]!;
  }
  final CompilationUnit unit = parseString(
    content: content(),
    throwIfDiagnostics: throwIfDiagnostics,
  ).unit;
  _cache[key] = unit;
  return unit;
}