-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextends.ts
543 lines (497 loc) · 22.7 KB
/
extends.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*--------------------------------------------------------------------------
@sidewinder/type
The MIT License (MIT)
Copyright (c) 2022 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/
import * as Types from './type'
export enum ExtendsResult {
Union,
True,
False,
}
/** Tests if one type structurally extends another based on 4.6.3 extends rules */
export namespace Extends {
const referenceMap = new Map<string, Types.TAnySchema>()
// ----------------------------------------------------------------------
// Rules
// ----------------------------------------------------------------------
function AnyOrUnknownRule<Right extends Types.TAnySchema>(right: Right) {
// https://github.com/microsoft/TypeScript/issues/40049
if (right[Types.Kind] === 'Union' && right.anyOf.some((schema: Types.TSchema) => schema[Types.Kind] === 'Any' || schema[Types.Kind] === 'Unknown')) return true
if (right[Types.Kind] === 'Unknown') return true
if (right[Types.Kind] === 'Any') return true
return false
}
function ObjectRightRule<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right) {
// type A = boolean extends {} ? 1 : 2 // additionalProperties: false
// type B = boolean extends object ? 1 : 2 // additionalProperties: true
const additionalProperties = right.additionalProperties
const propertyLength = globalThis.Object.keys(right.properties).length
return additionalProperties === false && propertyLength === 0
}
function UnionRightRule<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
const result = right.anyOf.some((right: Types.TSchema) => Extends(left, right) !== ExtendsResult.False)
return result ? ExtendsResult.True : ExtendsResult.False
}
// ----------------------------------------------------------------------
// Records
// ----------------------------------------------------------------------
function RecordPattern<T extends Types.TRecord>(schema: T) {
return globalThis.Object.keys(schema.patternProperties)[0] as string
}
function RecordNumberOrStringKey<T extends Types.TRecord>(schema: T) {
const pattern = RecordPattern(schema)
return pattern === '^.*$' || pattern === '^(0|[1-9][0-9]*)$'
}
export function RecordValue<T extends Types.TRecord>(schema: T) {
const pattern = RecordPattern(schema)
return schema.patternProperties[pattern]
}
export function RecordKey<T extends Types.TRecord>(schema: T) {
const pattern = RecordPattern(schema)
if (pattern === '^.*$') {
return Types.Type.String()
} else if (pattern === '^(0|[1-9][0-9]*)$') {
return Types.Type.Number()
} else {
const keys = pattern.slice(1, pattern.length - 1).split('|')
const schemas = keys.map((key) => (isNaN(+key) ? Types.Type.Literal(key) : Types.Type.Literal(parseFloat(key))))
return Types.Type.Union(schemas)
}
}
function PropertyMap<T extends Types.TAnySchema>(schema: T) {
const comparable = new Map<string, Types.TSchema>()
if (schema[Types.Kind] === 'Record') {
const propertyPattern = RecordPattern(schema as Types.TRecord)
if (propertyPattern === '^.*$' || propertyPattern === '^(0|[1-9][0-9]*)$') throw Error('Cannot extract record properties without property constraints')
const propertySchema = schema.patternProperties[propertyPattern] as Types.TSchema
const propertyKeys = propertyPattern.slice(1, propertyPattern.length - 1).split('|')
propertyKeys.forEach((propertyKey) => {
comparable.set(propertyKey, propertySchema)
})
} else if (schema[Types.Kind] === 'Object') {
globalThis.Object.entries(schema.properties).forEach(([propertyKey, propertySchema]) => {
comparable.set(propertyKey, propertySchema as Types.TSchema)
})
} else {
throw Error(`Cannot create property map from '${schema[Types.Kind]}' types`)
}
return comparable
}
// ----------------------------------------------------------------------
// Indexable
// ----------------------------------------------------------------------
function Indexable<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (right[Types.Kind] === 'Union') {
return ExtendsResult.False
} else {
return Extends(left, right)
}
}
// ----------------------------------------------------------------------
// Checks
// ----------------------------------------------------------------------
function Any<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
return AnyOrUnknownRule(right) ? ExtendsResult.True : ExtendsResult.Union
}
function Array<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Object') {
if (right.properties['length'] !== undefined && right.properties['length'][Types.Kind] === 'Number') return ExtendsResult.True
if (globalThis.Object.keys(right.properties).length === 0) return ExtendsResult.True
return ExtendsResult.False
} else if (right[Types.Kind] !== 'Array') {
return ExtendsResult.False
} else if (left.items === undefined && right.items !== undefined) {
return ExtendsResult.False
} else if (left.items !== undefined && right.items === undefined) {
return ExtendsResult.False
} else if (left.items === undefined && right.items === undefined) {
return ExtendsResult.False
} else {
const result = Extends(left.items, right.items) !== ExtendsResult.False
return result ? ExtendsResult.True : ExtendsResult.False
}
}
function Boolean<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Object' && ObjectRightRule(left, right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Boolean') {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Union') {
return UnionRightRule(left, right)
} else {
return ExtendsResult.False
}
}
function Constructor<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] !== 'Constructor') {
return ExtendsResult.False
} else if (right.parameters.length < left.parameters.length) {
return ExtendsResult.False
} else {
if (Extends(left.returns, right.returns) === ExtendsResult.False) {
return ExtendsResult.False
}
for (let i = 0; i < left.parameters.length; i++) {
const result = Extends(right.parameters[i], left.parameters[i])
if (result === ExtendsResult.False) return ExtendsResult.False
}
return ExtendsResult.True
}
}
function Enum<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Enum') {
return ExtendsResult.False // Cannot check enum as only values are encoded
} else {
return ExtendsResult.False
}
}
function Function<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Object') {
if (right.properties['length'] !== undefined && right.properties['length'][Types.Kind] === 'Number') return ExtendsResult.True
if (globalThis.Object.keys(right.properties).length === 0) return ExtendsResult.True
return ExtendsResult.False
} else if (right[Types.Kind] !== 'Function') {
return ExtendsResult.False
} else if (right.parameters.length < left.parameters.length) {
return ExtendsResult.False
} else if (Extends(left.returns, right.returns) === ExtendsResult.False) {
return ExtendsResult.False
} else {
for (let i = 0; i < left.parameters.length; i++) {
const result = Extends(right.parameters[i], left.parameters[i])
if (result === ExtendsResult.False) return ExtendsResult.False
}
return ExtendsResult.True
}
}
function Integer<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Object' && ObjectRightRule(left, right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Integer') {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Number') {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Union') {
return UnionRightRule(left, right)
} else {
return ExtendsResult.False
}
}
function Literal<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Object' && ObjectRightRule(left, right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Record') {
if (typeof left.const === 'string') {
return Indexable(left, RecordValue(right as Types.TRecord))
} else {
return ExtendsResult.False
}
} else if (right[Types.Kind] === 'Literal' && left.const === right.const) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'String' && typeof left.const === 'string') {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Number' && typeof left.const === 'number') {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Integer' && typeof left.const === 'number') {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Boolean' && typeof left.const === 'boolean') {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Union') {
return UnionRightRule(left, right)
} else {
return ExtendsResult.False
}
}
function Number<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Object' && ObjectRightRule(left, right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Number') {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Integer') {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Union') {
return UnionRightRule(left, right)
} else {
return ExtendsResult.False
}
}
function Null<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Null') {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Union') {
return UnionRightRule(left, right)
} else {
return ExtendsResult.False
}
}
function Properties(left: Map<string, Types.TSchema>, right: Map<string, Types.TSchema>) {
if (right.size > left.size) return ExtendsResult.False
if (![...right.keys()].every((rightKey) => left.has(rightKey))) return ExtendsResult.False
for (const rightKey of right.keys()) {
const leftProp = left.get(rightKey)!
const rightProp = right.get(rightKey)!
if (Extends(leftProp, rightProp) === ExtendsResult.False) {
return ExtendsResult.False
}
}
return ExtendsResult.True
}
function Object<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Object') {
return Properties(PropertyMap(left), PropertyMap(right))
} else if (right[Types.Kind] === 'Record') {
if (!RecordNumberOrStringKey(right as Types.TRecord)) {
return Properties(PropertyMap(left), PropertyMap(right))
} else {
return ExtendsResult.True
}
} else {
return ExtendsResult.False
}
}
function Promise<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Object') {
if (ObjectRightRule(left, right) || globalThis.Object.keys(right.properties).length === 0) {
return ExtendsResult.True
} else {
return ExtendsResult.False
}
} else if (right[Types.Kind] !== 'Promise') {
return ExtendsResult.False
} else {
const result = Extends(left.item, right.item) !== ExtendsResult.False
return result ? ExtendsResult.True : ExtendsResult.False
}
}
function Unknown<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (right[Types.Kind] === 'Union') {
const result = right.anyOf.some((right: Types.TSchema) => right[Types.Kind] === 'Any' || right[Types.Kind] === 'Unknown')
return result ? ExtendsResult.True : ExtendsResult.False
} else if (right[Types.Kind] === 'Any') {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Unknown') {
return ExtendsResult.True
} else {
return ExtendsResult.False
}
}
function Record<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Object') {
if (!RecordNumberOrStringKey(left as Types.TRecord)) {
return Properties(PropertyMap(left), PropertyMap(right))
} else {
return globalThis.Object.keys(right.properties).length === 0 ? ExtendsResult.True : ExtendsResult.False
}
} else if (right[Types.Kind] === 'Record') {
if (!RecordNumberOrStringKey(left as Types.TRecord) && !RecordNumberOrStringKey(right as Types.TRecord)) {
return Properties(PropertyMap(left), PropertyMap(right))
} else if (RecordNumberOrStringKey(left as Types.TRecord) && !RecordNumberOrStringKey(right as Types.TRecord)) {
const leftKey = RecordKey(left as Types.TRecord)
const rightKey = RecordKey(right as Types.TRecord)
if (Extends(rightKey, leftKey) === ExtendsResult.False) {
return ExtendsResult.False
} else {
return ExtendsResult.True
}
} else {
return ExtendsResult.True
}
} else {
return ExtendsResult.False
}
}
function Ref<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (!referenceMap.has(left.$ref)) throw Error(`Cannot locate referenced $id '${left.$ref}'`)
const resolved = referenceMap.get(left.$ref)!
return Extends(resolved, right)
}
function Self<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (!referenceMap.has(left.$ref)) throw Error(`Cannot locate referenced self $id '${left.$ref}'`)
const resolved = referenceMap.get(left.$ref)!
return Extends(resolved, right)
}
function String<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Object' && ObjectRightRule(left, right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Record') {
return Indexable(left, RecordValue(right as Types.TRecord))
} else if (right[Types.Kind] === 'String') {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Union') {
return UnionRightRule(left, right)
} else {
return ExtendsResult.False
}
}
function Tuple<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Object') {
const result = ObjectRightRule(left, right) || globalThis.Object.keys(right.properties).length === 0
return result ? ExtendsResult.True : ExtendsResult.False
} else if (right[Types.Kind] === 'Record') {
return Indexable(left, RecordValue(right as Types.TRecord))
} else if (right[Types.Kind] === 'Array') {
if (right.items === undefined) {
return ExtendsResult.False
} else if (right.items[Types.Kind] === 'Union') {
const result = left.items.every((left: Types.TSchema) => UnionRightRule(left, right.items) !== ExtendsResult.False)
return result ? ExtendsResult.True : ExtendsResult.False
} else if (right.items[Types.Kind] === 'Any') {
return ExtendsResult.True
} else {
return ExtendsResult.False
}
} else {
if (right[Types.Kind] !== 'Tuple') return ExtendsResult.False
if (left.items === undefined && right.items === undefined) return ExtendsResult.True
if (left.items === undefined && right.items !== undefined) return ExtendsResult.False
if (left.items !== undefined && right.items === undefined) return ExtendsResult.False
if (left.items === undefined && right.items === undefined) return ExtendsResult.True
if (left.minItems !== right.minItems || left.maxItems !== right.maxItems) return ExtendsResult.False
for (let i = 0; i < left.items!.length; i++) {
if (Extends(left.items![i], right.items![i]) === ExtendsResult.False) return ExtendsResult.False
}
return ExtendsResult.True
}
}
function Uint8Array<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Object' && ObjectRightRule(left, right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Record') {
return Indexable(left, RecordValue(right as Types.TRecord))
} else if (right[Types.Kind] === 'Uint8Array') {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Union') {
return UnionRightRule(left, right)
} else {
return ExtendsResult.False
}
}
function Undefined<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (AnyOrUnknownRule(right)) {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Undefined') {
return ExtendsResult.True
} else if (right[Types.Kind] === 'Union') {
return UnionRightRule(left, right)
} else {
return ExtendsResult.False
}
}
function Union<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
if (left.anyOf.some((left: Types.TSchema) => left[Types.Kind] === 'Any')) {
return ExtendsResult.Union
} else if (right[Types.Kind] === 'Union') {
const result = left.anyOf.every((left: Types.TSchema) => right.anyOf.some((right: Types.TSchema) => Extends(left, right) !== ExtendsResult.False))
return result ? ExtendsResult.True : ExtendsResult.False
} else {
const result = left.anyOf.every((left: Types.TSchema) => Extends(left, right) !== ExtendsResult.False)
return result ? ExtendsResult.True : ExtendsResult.False
}
}
let recursionDepth = 0
function Extends<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
recursionDepth += 1
if (recursionDepth >= 1000) return ExtendsResult.True
if (left.$id !== undefined) referenceMap.set(left.$id!, left)
if (right.$id !== undefined) referenceMap.set(right.$id!, right)
const resolvedRight = right[Types.Kind] === 'Self' ? referenceMap.get(right.$ref)! : right
switch (left[Types.Kind]) {
case 'Any':
return Any(left, resolvedRight)
case 'Array':
return Array(left, resolvedRight)
case 'Boolean':
return Boolean(left, resolvedRight)
case 'Constructor':
return Constructor(left, resolvedRight)
case 'Enum':
return Enum(left, resolvedRight)
case 'Function':
return Function(left, resolvedRight)
case 'Integer':
return Integer(left, resolvedRight)
case 'Literal':
return Literal(left, resolvedRight)
case 'Null':
return Null(left, resolvedRight)
case 'Number':
return Number(left, resolvedRight)
case 'Object':
return Object(left, resolvedRight)
case 'Promise':
return Promise(left, resolvedRight)
case 'Record':
return Record(left, resolvedRight)
case 'Ref':
return Ref(left, resolvedRight)
case 'String':
return String(left, resolvedRight)
case 'Tuple':
return Tuple(left, resolvedRight)
case 'Undefined':
return Undefined(left, resolvedRight)
case 'Uint8Array':
return Uint8Array(left, resolvedRight)
case 'Union':
return Union(left, resolvedRight)
case 'Unknown':
return Unknown(left, resolvedRight)
case 'Self':
return Self(left, resolvedRight)
default:
return ExtendsResult.False
}
}
/** Returns ExtendsResult.True if the left schema structurally extends the right schema. */
export function Check<Left extends Types.TAnySchema, Right extends Types.TAnySchema>(left: Left, right: Right): ExtendsResult {
referenceMap.clear()
recursionDepth = 0
return Extends(left, right)
}
}