-
Notifications
You must be signed in to change notification settings - Fork 409
/
Copy pathshared_checkers.dart
39 lines (31 loc) · 1.51 KB
/
shared_checkers.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/element/type.dart';
import 'package:source_gen/source_gen.dart' show TypeChecker;
import 'package:source_helper/source_helper.dart';
/// A [TypeChecker] for [Iterable].
const coreIterableTypeChecker = TypeChecker.fromUrl('dart:core#Iterable');
const coreStringTypeChecker = TypeChecker.fromUrl('dart:core#String');
const coreMapTypeChecker = TypeChecker.fromUrl('dart:core#Map');
/// Returns the generic type of the [Iterable] represented by [type].
///
/// If [type] does not extend [Iterable], an error is thrown.
DartType coreIterableGenericType(DartType type) =>
type.typeArgumentsOf(coreIterableTypeChecker)!.single;
/// Returns the generic key type of the [Map] represented by [type].
///
/// If [type] does not extend [Map], an error is thrown.
DartType coreMapGenericKeyType(DartType type) =>
type.typeArgumentsOf(coreMapTypeChecker)![0];
/// Returns the generic value type of the [Map] represented by [type].
///
/// If [type] does not extend [Map], an error is thrown.
DartType coreMapGenericValueType(DartType type) =>
type.typeArgumentsOf(coreMapTypeChecker)![1];
/// A [TypeChecker] for [String], [bool] and [num].
const simpleJsonTypeChecker = TypeChecker.any([
coreStringTypeChecker,
TypeChecker.fromUrl('dart:core#bool'),
TypeChecker.fromUrl('dart:core#num')
]);