Skip to main content

match-class-name-pattern

added in: 1.8.0
⚙️
Pro+

Warns when a class name does not match the configured pattern.

Helps you ensure that classes within a matching directory or file are named in a specific way.

note

This rule requires configuration in order to highlight any issues.

⚙️ Config

Set entries (default is empty) to configure the list of entries for class names to match.

Each entry is an object with 4 fields: path, pattern, ignore-private and severity:

  • Set path (can be a regular expression) to configure the path to which the entry will apply.
  • Set pattern (can be a regular expression) to configure a pattern that all class names in the given file should match.
  • Set ignore-private to exclude private classes from the rule's check.
  • Set severity (optional) to override default severity for the given entry.
analysis_options.yaml
dart_code_metrics:
rules:
- match-class-name-pattern:
entries:
- path: '.*state.dart'
pattern: 'State$'
ignore-private: true
severity: error
warning

For Windows devices, ensure that the path config works with Windows path separators.

Example

❌ Bad:

// LINT: Class name is expected to match the configured pattern 'State$'.
class SomeClass {
...
}

class MyState {}

✅ Good:

class SomeState {
...
}

class MyState {}