/
index.ts
147 lines (126 loc) · 3.76 KB
/
index.ts
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import {
isObjectType,
isUnionType,
isUnionOrIntersectionType,
unionTypeParts,
} from 'tsutils';
import * as ts from 'typescript';
import { nullThrows, NullThrowsReasons } from '..';
import { isReadonlySymbol } from './isReadonlySymbol';
/**
* Returns:
* - null if the type is not an array or tuple,
* - true if the type is a readonly array or readonly tuple,
* - false if the type is a mutable array or mutable tuple.
*/
function isTypeReadonlyArrayOrTuple(
checker: ts.TypeChecker,
type: ts.Type,
): boolean | null {
function checkTypeArguments(arrayType: ts.TypeReference): boolean {
const typeArguments = checker.getTypeArguments(arrayType);
if (typeArguments.length === 0) {
// this shouldn't happen in reality as:
// - tuples require at least 1 type argument
// - ReadonlyArray requires at least 1 type argument
return true;
}
// validate the element types are also readonly
if (typeArguments.some(typeArg => !isTypeReadonly(checker, typeArg))) {
return false;
}
return true;
}
if (checker.isArrayType(type)) {
const symbol = nullThrows(
type.getSymbol(),
NullThrowsReasons.MissingToken('symbol', 'array type'),
);
const escapedName = symbol.getEscapedName();
if (escapedName === 'Array' && escapedName !== 'ReadonlyArray') {
return false;
}
return checkTypeArguments(type);
}
if (checker.isTupleType(type)) {
if (!type.target.readonly) {
return false;
}
return checkTypeArguments(type);
}
return null;
}
/**
* Returns:
* - null if the type is not an object,
* - true if the type is an object with only readonly props,
* - false if the type is an object with at least one mutable prop.
*/
function isTypeReadonlyObject(
checker: ts.TypeChecker,
type: ts.Type,
): boolean | null {
function checkIndexSignature(kind: ts.IndexKind): boolean | null {
const indexInfo = checker.getIndexInfoOfType(type, kind);
if (indexInfo) {
return indexInfo.isReadonly ? true : false;
}
return null;
}
const properties = type.getProperties();
if (properties.length) {
for (const property of properties) {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
const x = checker.isReadonlySymbol(property);
const y = isReadonlySymbol(property);
if (x !== y) {
throw new Error('FUCK');
}
if (!isReadonlySymbol(property)) {
return false;
}
}
// all properties were readonly
}
const isStringIndexSigReadonly = checkIndexSignature(ts.IndexKind.String);
if (isStringIndexSigReadonly === false) {
return isStringIndexSigReadonly;
}
const isNumberIndexSigReadonly = checkIndexSignature(ts.IndexKind.Number);
if (isNumberIndexSigReadonly === false) {
return isNumberIndexSigReadonly;
}
return true;
}
/**
* Checks if the given type is readonly
*/
function isTypeReadonly(checker: ts.TypeChecker, type: ts.Type): boolean {
if (isUnionType(type)) {
// all types in the union must be readonly
return unionTypeParts(type).every(t => isTypeReadonly(checker, t));
}
// all non-object, non-intersection types are readonly.
// this should only be primitive types
if (!isObjectType(type) && !isUnionOrIntersectionType(type)) {
return true;
}
// pure function types are readonly
if (
type.getCallSignatures().length > 0 &&
type.getProperties().length === 0
) {
return true;
}
const isReadonlyArray = isTypeReadonlyArrayOrTuple(checker, type);
if (isReadonlyArray !== null) {
return isReadonlyArray;
}
const isReadonlyObject = isTypeReadonlyObject(checker, type);
if (isReadonlyObject !== null) {
return isReadonlyObject;
}
throw new Error('Unhandled type');
}
export { isTypeReadonly };