Skip to content

analysis_server_plugin to warn when matchers are being used agains a non-expected type #2558

@FMorschel

Description

@FMorschel

Based on #2461, I'd like to request we have a way of adding an annotation to Matchers that, when used, will trigger a lint if the type being matched against is not expected.

Something like:

import 'package:test/test.dart';

void main() {
  test('MyMatcher description', () {
    expect(2, myMatcher);
    expect('2', myMatcher); // This would trigger a lint because the type `String` is not on the list of types for the annotation
  });
}

final myMatcher = MyMatcher();

@Annotation([int])
class MyMatcher extends Matcher {
  @override
  Description describe(Description description) =>
      StringDescription('an integer');

  @override
  bool matches(item, Map<dynamic, dynamic> matchState) =>
      item is int && item > 0;
}

/// Consider built-in
class Annotation {
  const Annotation(List<Type> types);
}

@srawlins' #2461 (comment):

For first-party matchers in the matcher package, I don't think it would be worth the extra complexity. So the feature would be for custom matchers implemented outside of the matcher and test packages.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions