describeNodeSource function

String describeNodeSource(
  1. SAstNode node, {
  2. required Map<String, SCompilationUnit> modules,
  3. Map<String, String>? sources,
})

A short description of where node is and what it says.

  • With the module's source bundled: '<excerpt>' (<uri>:<line>:<column>).
  • Without it: the module and offset, and how to get the excerpt. Naming the module is still worth having, because a release bundle may legitimately leave its source out.
  • When no loaded module contains the node: says so.

Implementation

String describeNodeSource(
  SAstNode node, {
  required Map<String, SCompilationUnit> modules,
  Map<String, String>? sources,
}) {
  final uri = moduleContaining(node, modules);
  if (uri == null) {
    return '(not available: no loaded module contains this node; '
        'offset ${node.offset})';
  }
  final source = sources?[uri];
  if (source == null ||
      node.offset < 0 ||
      node.offset + node.length > source.length) {
    return '(not bundled: $uri, offset ${node.offset}; build the bundle '
        'with its sources to quote them)';
  }
  final text = source
      .substring(node.offset, node.offset + node.length)
      .replaceAll(RegExp(r'\s+'), ' ');
  final excerpt = text.length <= 60 ? text : '${text.substring(0, 57)}...';
  final (line, column) = _lineAndColumn(source, node.offset);
  return "'$excerpt' ($uri:$line:$column)";
}