-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathAST.ts
176 lines (144 loc) · 3.41 KB
/
AST.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import {JSONSchema4Type} from 'json-schema'
export type AST_TYPE = AST['type']
export type AST =
| TAny
| TArray
| TBoolean
| TEnum
| TInterface
| TNamedInterface
| TIntersection
| TLiteral
| TNever
| TNumber
| TNull
| TObject
| TReference
| TString
| TTuple
| TUnion
| TUnknown
| TCustomType
export interface AbstractAST {
comment?: string
isExternalSchema: boolean
keyName?: string
standaloneName?: string
type: AST_TYPE
deprecated?: boolean
}
export type ASTWithComment = AST & {comment: string}
export type ASTWithName = AST & {keyName: string}
export type ASTWithStandaloneName = AST & {standaloneName: string}
export function hasComment(ast: AST): ast is ASTWithComment {
return (
('comment' in ast && ast.comment != null && ast.comment !== '') ||
// Compare to true because ast.deprecated might be undefined
('deprecated' in ast && ast.deprecated === true)
)
}
export function hasStandaloneName(ast: AST): ast is ASTWithStandaloneName {
return 'standaloneName' in ast && ast.standaloneName != null && ast.standaloneName !== ''
}
//////////////////////////////////////////// types
export interface TAny extends AbstractAST {
type: 'ANY'
}
export interface TArray extends AbstractAST {
type: 'ARRAY'
params: AST
}
export interface TBoolean extends AbstractAST {
type: 'BOOLEAN'
}
export interface TEnum extends AbstractAST {
standaloneName: string
type: 'ENUM'
params: TEnumParam[]
}
export interface TEnumParam {
ast: AST
keyName: string
}
export interface TInterface extends AbstractAST {
type: 'INTERFACE'
params: TInterfaceParam[]
superTypes: TNamedInterface[]
}
export interface TNamedInterface extends AbstractAST {
standaloneName: string
type: 'INTERFACE'
params: TInterfaceParam[]
superTypes: TNamedInterface[]
}
export interface TNever extends AbstractAST {
type: 'NEVER'
}
export interface TInterfaceParam {
ast: AST
keyName: string
isRequired: boolean
isPatternProperty: boolean
isUnreachableDefinition: boolean
}
export interface TIntersection extends AbstractAST {
type: 'INTERSECTION'
params: AST[]
}
export interface TLiteral extends AbstractAST {
params: JSONSchema4Type
type: 'LITERAL'
}
export interface TNumber extends AbstractAST {
type: 'NUMBER'
}
export interface TNull extends AbstractAST {
type: 'NULL'
}
export interface TObject extends AbstractAST {
type: 'OBJECT'
}
export interface TReference extends AbstractAST {
type: 'REFERENCE'
params: string
}
export interface TString extends AbstractAST {
type: 'STRING'
}
export interface TTuple extends AbstractAST {
type: 'TUPLE'
params: AST[]
spreadParam?: AST
minItems: number
maxItems?: number
}
export interface TUnion extends AbstractAST {
type: 'UNION'
params: AST[]
}
export interface TUnknown extends AbstractAST {
type: 'UNKNOWN'
}
export interface TCustomType extends AbstractAST {
type: 'CUSTOM_TYPE'
params: string
}
//////////////////////////////////////////// literals
export const T_ANY: TAny = {
type: 'ANY',
isExternalSchema: false,
}
export const T_ANY_ADDITIONAL_PROPERTIES: TAny & ASTWithName = {
keyName: '[k: string]',
type: 'ANY',
isExternalSchema: false,
}
export const T_UNKNOWN: TUnknown = {
type: 'UNKNOWN',
isExternalSchema: false,
}
export const T_UNKNOWN_ADDITIONAL_PROPERTIES: TUnknown & ASTWithName = {
keyName: '[k: string]',
type: 'UNKNOWN',
isExternalSchema: false,
}