checkKeyShape function

void checkKeyShape(
  1. List<Chki18nIssue> issues,
  2. String key,
  3. String group,
  4. Chki18nResolvedOptions options,
)

Checks the shape of a key: the case its segments are written in, and how deeply it is nested.

Both are off until the project says what it wants, because neither has a right answer on its own. Reported once per key rather than once per locale: a key is named the same everywhere, so one badly named key is one finding, not one per language.

Implementation

void checkKeyShape(
  List<Chki18nIssue> issues,
  String key,
  String group,
  Chki18nResolvedOptions options,
) {
  // Asked of every key of every group, so the case where the project has said
  // nothing costs nothing: not even splitting the key into its segments.
  if (key.isEmpty || (options.maxKeyDepth == null && options.keyCase == null)) {
    return;
  }

  final segments = key.split(keySeparator);
  final maxKeyDepth = options.maxKeyDepth;

  if (maxKeyDepth != null &&
      segments.length > maxKeyDepth &&
      options.enabledChecks.contains(Chki18nCheckCode.keyDepth)) {
    issues.add(
      createIssue(
        Chki18nCheckCode.keyDepth,
        key: key,
        group: group,
        message:
            'The key is ${segments.length} levels deep, and `maxKeyDepth` allows $maxKeyDepth.',
      ),
    );
  }

  final keyCase = options.keyCase;

  if (keyCase == null || !options.enabledChecks.contains(Chki18nCheckCode.keyNaming)) {
    return;
  }

  final pattern = _segmentPattern[keyCase]!;

  for (final segment in segments) {
    if (pattern.hasMatch(_withoutLibrarySuffix(segment, keyCase))) {
      continue;
    }

    issues.add(
      createIssue(
        Chki18nCheckCode.keyNaming,
        key: key,
        group: group,
        message:
            segments.length > 1
                ? 'The part `$segment` is not written in ${keyCase.name} case.'
                : 'The key is not written in ${keyCase.name} case.',
      ),
    );

    // One finding per key. Naming a second bad segment of the same key adds
    // nothing to what has to be done about it.
    return;
  }
}