pubspecNameOf function

String? pubspecNameOf(
  1. RuleContext context
)

The name: declared in the pubspec.yaml of the package that contains the library analysed in context, or null when there is none.

Every rule that needs it asks for every library, so the parsed name is cached per pubspec and re-read only when the file's modification stamp changes.

Implementation

String? pubspecNameOf(RuleContext context) {
  final root = context.package?.root;
  if (root == null) return null;
  final pubspec = root.getFile('pubspec.yaml');
  if (!pubspec.exists) return null;
  final stamp = pubspec.modificationStamp;
  final cached = _pubspecNames[pubspec.path];
  if (cached != null && cached.stamp == stamp) return cached.name;
  final match = _pubspecNamePattern.firstMatch(pubspec.readAsStringSync());
  final name = match?.group(1);
  _pubspecNames[pubspec.path] = (stamp: stamp, name: name);
  return name;
}