forked from typescript-eslint/typescript-eslint
/
types.js
40 lines (33 loc) · 1017 Bytes
/
types.js
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
40
'use strict';
const tsutils = require('tsutils');
const ts = require('typescript');
/**
* @param {string} type Type being checked by name.
* @param {Set<string>} allowedNames Symbol names checking on the type.
* @returns {boolean} Whether the type is, extends, or contains any of the allowed names.
*/
function containsTypeByName(type, allowedNames) {
if (tsutils.isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
return true;
}
if (tsutils.isTypeReference(type)) {
type = type.target;
}
if (
typeof type.symbol !== 'undefined' &&
allowedNames.has(type.symbol.name)
) {
return true;
}
if (tsutils.isUnionOrIntersectionType(type)) {
return type.types.some(innerType =>
containsTypeByName(innerType, allowedNames)
);
}
const baseTypes = type.getBaseTypes();
return (
typeof baseTypes !== 'undefined' &&
baseTypes.some(baseType => containsTypeByName(baseType, allowedNames))
);
}
exports.containsTypeByName = containsTypeByName;