cddl2ts 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts ADDED
@@ -0,0 +1,61 @@
1
+ import {
2
+ Assignment,
3
+ PropertyReference,
4
+ Property,
5
+ Array,
6
+ NativeTypeWithOperator,
7
+ Group,
8
+ Variable
9
+ } from 'cddl'
10
+ import camelcase from 'camelcase'
11
+
12
+ export function pascalCase(name: string) {
13
+ return camelcase(name, { pascalCase: true })
14
+ }
15
+
16
+ export function isVariable(assignment: Assignment): assignment is Variable {
17
+ return assignment.Type === 'variable'
18
+ }
19
+
20
+ export function isGroup(t: any): t is Group {
21
+ return t && t.Type === 'group'
22
+ }
23
+
24
+ export function isCDDLArray(t: any): t is Array {
25
+ return t && t.Type === 'array'
26
+ }
27
+
28
+ export function isProperty(t: any): t is Property {
29
+ return t && typeof t.Name === 'string' && typeof t.HasCut === 'boolean'
30
+ }
31
+
32
+ export function isUnNamedProperty(t: any): t is Property & { Name: '' } {
33
+ return isProperty(t) && t.Name === ''
34
+ }
35
+
36
+ export function isNamedGroupReference(t: any): t is PropertyReference & { Value: string } {
37
+ return isGroup(t) && isPropertyReference(t) && typeof t.Value === 'string'
38
+ }
39
+
40
+ export function isPropertyReference(t: any): t is PropertyReference {
41
+ return t && 'Value' in t
42
+ }
43
+
44
+ export function isNativeTypeWithOperator(t: any): t is NativeTypeWithOperator {
45
+ return t && typeof t.Type === 'object' && 'Operator' in t
46
+ }
47
+
48
+ export function isRange(t: any): boolean {
49
+ return t && typeof t.Type === 'object' && (t.Type as any).Type === 'range'
50
+ }
51
+
52
+ export function isLiteralWithValue(t: any): t is {
53
+ Type: 'literal'
54
+ Value: unknown
55
+ } {
56
+ return t && t.Type === 'literal' && 'Value' in t
57
+ }
58
+
59
+ export function hasTypeProperty(t: any): t is { Type: string } {
60
+ return t && typeof t.Type === 'string'
61
+ }
@@ -0,0 +1,72 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`group choice conversion > should generate a union type for multiple group choices 1`] = `
4
+ "export type Extensible = Record<string, unknown>;
5
+ export type ProxyConfiguration = AutodetectProxyConfiguration | DirectProxyConfiguration | ManualProxyConfiguration;
6
+
7
+ export type AutodetectProxyConfiguration = Extensible & {
8
+ proxyType: "autodetect";
9
+ };
10
+
11
+ export type DirectProxyConfiguration = Extensible & {
12
+ proxyType: "direct";
13
+ };
14
+
15
+ export // 1. Simple Group Choice
16
+ type ManualProxyConfiguration = Extensible & {
17
+ proxyType: "manual";
18
+ };
19
+
20
+ export // 2. Nested Group Choice
21
+ type SimpleGroupChoice = Int | string;
22
+
23
+ export // 3. Group Choice with Multiple Items (Sequence) - interpreted as tuple
24
+ type NestedGroupChoice = (Int | Tstr);
25
+
26
+ export // 4. Map Group Choice - interpreted as map (interface) union
27
+ type SequenceGroupChoice = [number, string] | number;
28
+
29
+ export // 5. Type Choice inside Group - should be value union
30
+ type MapGroupChoice = {
31
+ a: 1;
32
+ } | {
33
+ b: 2;
34
+ };
35
+
36
+ export // 6. Array with Group Choice - should be array of union?
37
+ type TypeChoiceInsideGroup = number | string;
38
+
39
+ export // 7. Map with nested group (bare and parens) - should be object like
40
+ type ArrayGroupChoice = Int | string[];
41
+
42
+ export type MapWithBareGroup = number;
43
+
44
+ export // 8. Group wrapped in map with multiple properties
45
+ type MapWithParensGroup = number;
46
+
47
+ export // 9. Property type choice without operators
48
+ type MapGroupWrap = ;
49
+
50
+ export // 10. Nested choice with group reference
51
+ interface PropChoiceNoOp {
52
+ a: number | number;
53
+ b: string;
54
+ }
55
+
56
+ export // 11. Complex type (group) followed by property without comma
57
+ type NestedChoiceRef = number | string;
58
+
59
+ export // 12. Multi-item group choice (verifies isChoice persistence)
60
+ interface MapNoComma {
61
+ a: number;
62
+ b: string;
63
+ }
64
+
65
+ export type GroupChoiceMulti = {
66
+ a: number;
67
+ } | {
68
+ b: string;
69
+ } | {
70
+ c: number;
71
+ };"
72
+ `;
@@ -0,0 +1,8 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`literals > should support bigint and null literals 1`] = `
4
+ "export interface LiteralTest {
5
+ bigInt: 9007199254740996;
6
+ nullVal: null;
7
+ }"
8
+ `;
@@ -0,0 +1,19 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`mixin union types > should generate union type aliases for mixin choices 1`] = `
4
+ "export type Mixins = MixinA & MixinB;
5
+
6
+ export interface MixinA {
7
+ a: number;
8
+ }
9
+
10
+ export interface MixinB {
11
+ b: string;
12
+ }
13
+
14
+ export type UnionMixin = MixinA | MixinB;
15
+
16
+ export type ComplexMixins = (MixinA | MixinB) & {
17
+ c: boolean;
18
+ };"
19
+ `;
@@ -0,0 +1,102 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`cddl2ts > should generate correct types for test.cddl 1`] = `
4
+ [
5
+ [
6
+ "export type Message = CommandResponse | ErrorResponse | Event;
7
+
8
+ export type ErrorResponse = Extensible & {
9
+ id?: JsUint | null;
10
+ error: ErrorCode;
11
+ message: string;
12
+ stacktrace?: string;
13
+ };
14
+
15
+ export type ResultData = BrowsingContextResult | EmptyResult | NetworkResult | ScriptResult | SessionResult;
16
+ export type ErrorCode = "invalid argument" | "invalid session id" | "no such alert" | "no such frame" | "no such handle" | "no such node" | "no such script" | "session not created" | "unknown command" | "unknown error" | "unsupported operation";
17
+ export type JsInt = number;
18
+ export type JsUint = number;
19
+ export type Handle = string;
20
+ export type SharedId = string;
21
+
22
+ export interface UndefinedValue {
23
+ type: "undefined";
24
+ }
25
+
26
+ export interface NullValue {
27
+ type: null;
28
+ }
29
+
30
+ export interface StringValue {
31
+ type: "string";
32
+ value: string;
33
+ }
34
+
35
+ export type SpecialNumber = "NaN" | "-0" | "Infinity" | "-Infinity";
36
+
37
+ export interface NumberValue {
38
+ type: "number";
39
+ value: Number | SpecialNumber;
40
+ }
41
+
42
+ export interface SessionCapabilitiesRequest {
43
+ alwaysMatch?: SessionCapabilityRequest;
44
+ firstMatch?: SessionCapabilityRequest[];
45
+ nested: {
46
+ obj: 123;
47
+ moreNesting: {
48
+ id: JsUint | null;
49
+ like: boolean;
50
+ };
51
+ };
52
+ }
53
+
54
+ export interface SomeGroup {
55
+ /**
56
+ * this is the optional leading param
57
+ * this is not leading
58
+ *
59
+ * @default 'foobar'
60
+ */
61
+ optional?: string;
62
+ /**
63
+ * @default 'portrait'
64
+ */
65
+ orientation?: "portrait" | "landscape";
66
+ /**
67
+ * @default 1
68
+ */
69
+ scale?: number;
70
+ /**
71
+ * @default true
72
+ */
73
+ shrinkToFit?: boolean;
74
+ /**
75
+ * @default true
76
+ */
77
+ shrinkToFitP?: boolean;
78
+ /**
79
+ * this is the pageRanges param
80
+ *
81
+ * @default 1
82
+ */
83
+ bottom?: number;
84
+ pageRanges?: (JsUint | string)[];
85
+ /**
86
+ * @default null
87
+ */
88
+ foo?: string;
89
+ /**
90
+ * @default 'mouse'
91
+ */
92
+ pointerType?: InputPointerType;
93
+ }
94
+
95
+ export type ScriptListLocalValue = ScriptLocalValue[];
96
+ export type ScriptMappingLocalValue = (ScriptLocalValue | ScriptLocalValue)[];
97
+
98
+ export // some comments here
99
+ type Extensible = Record<string, any>;",
100
+ ],
101
+ ]
102
+ `;
@@ -0,0 +1,14 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`repro proxy > should generate correct output for ProxyConfiguration 1`] = `
4
+ "export type Extensible = Record<string, unknown>;
5
+ export type ProxyConfiguration = AutodetectProxyConfiguration | DirectProxyConfiguration;
6
+
7
+ export interface AutodetectProxyConfiguration extends Extensible {
8
+ proxyType: "autodetect";
9
+ }
10
+
11
+ export interface DirectProxyConfiguration extends Extensible {
12
+ proxyType: "direct";
13
+ }"
14
+ `;
@@ -0,0 +1,18 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`union extension > should generate an intersection type instead of interface extends when extending a union 1`] = `
4
+ "export type Extensible = Record<string, unknown>;
5
+ export type Event = EventData & Extensible;
6
+ export type EventData = MouseEvent | KeyboardEvent;
7
+
8
+ export interface MouseEvent {
9
+ type: "mouse";
10
+ x: Number;
11
+ y: Number;
12
+ }
13
+
14
+ export interface KeyboardEvent {
15
+ type: "key";
16
+ code: Number;
17
+ }"
18
+ `;