|
3 | 3 | // SPDX-License-Identifier: AGPL-3.0-only |
4 | 4 |
|
5 | 5 | import { |
| 6 | + type BlockTypeProperty, |
| 7 | + type CellRangeLiteral, |
| 8 | + type ConstraintDefinition, |
| 9 | + type TransformDefinition, |
6 | 10 | type ValuetypeAssignment, |
| 11 | + isBlockTypeProperty, |
| 12 | + isCellRangeLiteral, |
| 13 | + isConstraintDefinition, |
| 14 | + isTransformDefinition, |
7 | 15 | isValuetypeAssignment, |
8 | 16 | } from '../generated/ast'; |
9 | 17 |
|
10 | 18 | import { |
| 19 | + type AtomicInternalValueRepresentation, |
11 | 20 | type InternalValueRepresentation, |
12 | 21 | type InternalValueRepresentationTypeguard, |
13 | 22 | } from './internal-value-representation'; |
14 | 23 |
|
15 | 24 | export const INTERNAL_VALUE_REPRESENTATION_TYPEGUARD: InternalValueRepresentationTypeguard< |
16 | 25 | InternalValueRepresentation |
17 | | -> = ( |
18 | | - value: InternalValueRepresentation, |
19 | | -): value is InternalValueRepresentation => { |
20 | | - return true; |
| 26 | +> = (value): value is InternalValueRepresentation => { |
| 27 | + return ( |
| 28 | + ATOMIC_INTERNAL_VALUE_REPRESENTATION_TYPEGUARD(value) || |
| 29 | + COLLECTION_TYPEGUARD(value) |
| 30 | + ); |
21 | 31 | }; |
| 32 | + |
| 33 | +export const ATOMIC_INTERNAL_VALUE_REPRESENTATION_TYPEGUARD: InternalValueRepresentationTypeguard< |
| 34 | + AtomicInternalValueRepresentation |
| 35 | +> = (value): value is AtomicInternalValueRepresentation => |
| 36 | + BOOLEAN_TYPEGUARD(value) || |
| 37 | + NUMBER_TYPEGUARD(value) || |
| 38 | + STRING_TYPEGUARD(value) || |
| 39 | + CELLRANGELITERAL_TYPEGUARD(value) || |
| 40 | + CONSTRAINTDEFINITION_TYPEGUARD(value) || |
| 41 | + VALUETYPEASSIGNMENT_TYPEGUARD(value) || |
| 42 | + BLOCKTYPEPROPERTY_TYPEGUARD(value) || |
| 43 | + TRANSFORMDEFINITION_TYPEGUARD(value); |
| 44 | + |
22 | 45 | export const NUMBER_TYPEGUARD: InternalValueRepresentationTypeguard<number> = ( |
23 | | - value: InternalValueRepresentation, |
24 | | -): value is number => { |
25 | | - return typeof value === 'number'; |
26 | | -}; |
| 46 | + value, |
| 47 | +) => typeof value === 'number'; |
27 | 48 | export const BOOLEAN_TYPEGUARD: InternalValueRepresentationTypeguard< |
28 | 49 | boolean |
29 | | -> = (value: InternalValueRepresentation): value is boolean => { |
30 | | - return typeof value === 'boolean'; |
31 | | -}; |
| 50 | +> = (value) => typeof value === 'boolean'; |
32 | 51 | export const STRING_TYPEGUARD: InternalValueRepresentationTypeguard<string> = ( |
33 | | - value: InternalValueRepresentation, |
34 | | -): value is string => { |
35 | | - return typeof value === 'string'; |
36 | | -}; |
| 52 | + value, |
| 53 | +) => typeof value === 'string'; |
37 | 54 |
|
38 | 55 | export const REGEXP_TYPEGUARD: InternalValueRepresentationTypeguard<RegExp> = ( |
39 | | - value: InternalValueRepresentation, |
40 | | -): value is RegExp => { |
41 | | - return value instanceof RegExp; |
42 | | -}; |
| 56 | + value, |
| 57 | +) => value instanceof RegExp; |
| 58 | + |
| 59 | +export const CELLRANGELITERAL_TYPEGUARD: InternalValueRepresentationTypeguard< |
| 60 | + CellRangeLiteral |
| 61 | +> = (value) => isCellRangeLiteral(value); |
| 62 | + |
| 63 | +export const CONSTRAINTDEFINITION_TYPEGUARD: InternalValueRepresentationTypeguard< |
| 64 | + ConstraintDefinition |
| 65 | +> = (value) => isConstraintDefinition(value); |
43 | 66 |
|
44 | 67 | export const VALUETYPEASSIGNMENT_TYPEGUARD: InternalValueRepresentationTypeguard< |
45 | 68 | ValuetypeAssignment |
46 | | -> = (value: InternalValueRepresentation): value is ValuetypeAssignment => { |
47 | | - return isValuetypeAssignment(value); |
48 | | -}; |
| 69 | +> = (value) => isValuetypeAssignment(value); |
| 70 | + |
| 71 | +export const BLOCKTYPEPROPERTY_TYPEGUARD: InternalValueRepresentationTypeguard< |
| 72 | + BlockTypeProperty |
| 73 | +> = (value) => isBlockTypeProperty(value); |
| 74 | + |
| 75 | +export const TRANSFORMDEFINITION_TYPEGUARD: InternalValueRepresentationTypeguard< |
| 76 | + TransformDefinition |
| 77 | +> = (value) => isTransformDefinition(value); |
49 | 78 |
|
50 | 79 | export const COLLECTION_TYPEGUARD: InternalValueRepresentationTypeguard< |
51 | 80 | InternalValueRepresentation[] |
52 | | -> = ( |
53 | | - value: InternalValueRepresentation, |
54 | | -): value is InternalValueRepresentation[] => { |
55 | | - return Array.isArray(value); |
56 | | -}; |
| 81 | +> = (value) => |
| 82 | + Array.isArray(value) && |
| 83 | + value.every((v) => INTERNAL_VALUE_REPRESENTATION_TYPEGUARD(v)); |
57 | 84 |
|
58 | 85 | export function isEveryValueDefined<T>(array: (T | undefined)[]): array is T[] { |
59 | 86 | return array.every((value) => value !== undefined); |
|
0 commit comments