expandIgnorePattern function

  1. @visibleForTesting
List<String> expandIgnorePattern(
  1. String pattern
)

Expands a gitignore-style pattern into glob patterns for matching.

Implementation

@visibleForTesting
List<String> expandIgnorePattern(String pattern) {
  var rootAnchored = false;
  var body = pattern;
  if (body.startsWith('/')) {
    rootAnchored = true;
    body = body.substring(1);
  }

  var directoryOnly = false;
  if (body.endsWith('/')) {
    directoryOnly = true;
    body = body.substring(0, body.length - 1);
  }

  if (body.isEmpty) {
    return const [];
  }

  final hasSlash = body.contains('/');
  final anchored = rootAnchored || hasSlash;

  if (directoryOnly) {
    if (anchored) {
      return [body, '$body/**'];
    }
    return [
      body,
      '$body/**',
      '**/$body',
      '**/$body/**',
    ];
  }

  if (!anchored) {
    return [body, '**/$body'];
  }

  if (body.startsWith('**/')) {
    return [body, body.substring(3)];
  }

  return [body];
}