cddl2ts 0.6.0 → 0.7.1
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/build/index.d.ts.map +1 -1
- package/build/index.js +21 -3
- package/package.json +2 -2
- package/src/index.ts +21 -3
- package/tests/transform.test.ts +74 -11
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAWH,KAAK,UAAU,EAMlB,MAAM,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAWH,KAAK,UAAU,EAMlB,MAAM,MAAM,CAAA;AAyCb,MAAM,WAAW,gBAAgB;IAC7B,UAAU,CAAC,EAAE,OAAO,CAAA;CACvB;AAED,wBAAgB,SAAS,CAAE,WAAW,EAAE,UAAU,EAAE,EAAE,OAAO,CAAC,EAAE,gBAAgB,UAwB/E"}
|
package/build/index.js
CHANGED
|
@@ -7,17 +7,35 @@ const b = types.builders;
|
|
|
7
7
|
const NATIVE_TYPES = {
|
|
8
8
|
any: b.tsAnyKeyword(),
|
|
9
9
|
number: b.tsNumberKeyword(),
|
|
10
|
+
integer: b.tsNumberKeyword(),
|
|
10
11
|
int: b.tsNumberKeyword(),
|
|
11
|
-
float: b.tsNumberKeyword(),
|
|
12
12
|
uint: b.tsNumberKeyword(),
|
|
13
|
+
nint: b.tsNumberKeyword(),
|
|
14
|
+
unsigned: b.tsNumberKeyword(),
|
|
15
|
+
float: b.tsNumberKeyword(),
|
|
16
|
+
float16: b.tsNumberKeyword(),
|
|
17
|
+
float32: b.tsNumberKeyword(),
|
|
18
|
+
float64: b.tsNumberKeyword(),
|
|
19
|
+
'float16-32': b.tsNumberKeyword(),
|
|
20
|
+
'float32-64': b.tsNumberKeyword(),
|
|
13
21
|
bool: b.tsBooleanKeyword(),
|
|
22
|
+
false: b.tsBooleanKeyword(),
|
|
23
|
+
true: b.tsBooleanKeyword(),
|
|
24
|
+
bstr: b.tsTypeReference(b.identifier('Uint8Array')),
|
|
25
|
+
bytes: b.tsTypeReference(b.identifier('Uint8Array')),
|
|
14
26
|
str: b.tsStringKeyword(),
|
|
15
27
|
text: b.tsStringKeyword(),
|
|
16
28
|
tstr: b.tsStringKeyword(),
|
|
17
29
|
range: b.tsNumberKeyword(),
|
|
30
|
+
undefined: b.tsUndefinedKeyword(),
|
|
18
31
|
nil: b.tsNullKeyword(),
|
|
19
32
|
null: b.tsNullKeyword()
|
|
20
33
|
};
|
|
34
|
+
const RECORD_KEY_TYPES = new Set([
|
|
35
|
+
'int', 'uint', 'nint', 'integer', 'unsigned', 'number',
|
|
36
|
+
'float', 'float16', 'float32', 'float64', 'float16-32', 'float32-64',
|
|
37
|
+
'str', 'text', 'tstr'
|
|
38
|
+
]);
|
|
21
39
|
export function transform(assignments, options) {
|
|
22
40
|
if (options?.useUnknown) {
|
|
23
41
|
NATIVE_TYPES.any = b.tsUnknownKeyword();
|
|
@@ -146,7 +164,7 @@ function parseAssignment(assignment) {
|
|
|
146
164
|
if (props.length === 1) {
|
|
147
165
|
const prop = props[0];
|
|
148
166
|
const propType = Array.isArray(prop.Type) ? prop.Type : [prop.Type];
|
|
149
|
-
if (propType.length === 1 &&
|
|
167
|
+
if (propType.length === 1 && RECORD_KEY_TYPES.has(prop.Name)) {
|
|
150
168
|
const value = parseUnionType(assignment);
|
|
151
169
|
const expr = b.tsTypeAliasDeclaration(id, value);
|
|
152
170
|
expr.comments = assignment.Comments.map((c) => b.commentLine(` ${c.Content}`, true));
|
|
@@ -511,7 +529,7 @@ function parseUnionType(t) {
|
|
|
511
529
|
/**
|
|
512
530
|
* {*text => text} which will be transformed to `Record<string, string>`
|
|
513
531
|
*/
|
|
514
|
-
if (prop.length === 1 &&
|
|
532
|
+
if (prop.length === 1 && RECORD_KEY_TYPES.has(prop[0].Name)) {
|
|
515
533
|
return b.tsTypeReference(b.identifier('Record'), b.tsTypeParameterInstantiation([
|
|
516
534
|
NATIVE_TYPES[prop[0].Name],
|
|
517
535
|
parseUnionType(prop[0].Type[0])
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cddl2ts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "A Node.js package that can generate a TypeScript definition based on a CDDL file",
|
|
5
5
|
"author": "Christian Bromann <mail@bromann.dev>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"camelcase": "^9.0.0",
|
|
33
33
|
"recast": "^0.23.11",
|
|
34
34
|
"yargs": "^18.0.0",
|
|
35
|
-
"cddl": "0.
|
|
35
|
+
"cddl": "0.19.1"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"release": "release-it --config .release-it.ts --VV",
|
package/src/index.ts
CHANGED
|
@@ -27,17 +27,35 @@ const b = types.builders
|
|
|
27
27
|
const NATIVE_TYPES: Record<string, any> = {
|
|
28
28
|
any: b.tsAnyKeyword(),
|
|
29
29
|
number: b.tsNumberKeyword(),
|
|
30
|
+
integer: b.tsNumberKeyword(),
|
|
30
31
|
int: b.tsNumberKeyword(),
|
|
31
|
-
float: b.tsNumberKeyword(),
|
|
32
32
|
uint: b.tsNumberKeyword(),
|
|
33
|
+
nint: b.tsNumberKeyword(),
|
|
34
|
+
unsigned: b.tsNumberKeyword(),
|
|
35
|
+
float: b.tsNumberKeyword(),
|
|
36
|
+
float16: b.tsNumberKeyword(),
|
|
37
|
+
float32: b.tsNumberKeyword(),
|
|
38
|
+
float64: b.tsNumberKeyword(),
|
|
39
|
+
'float16-32': b.tsNumberKeyword(),
|
|
40
|
+
'float32-64': b.tsNumberKeyword(),
|
|
33
41
|
bool: b.tsBooleanKeyword(),
|
|
42
|
+
false: b.tsBooleanKeyword(),
|
|
43
|
+
true: b.tsBooleanKeyword(),
|
|
44
|
+
bstr: b.tsTypeReference(b.identifier('Uint8Array')),
|
|
45
|
+
bytes: b.tsTypeReference(b.identifier('Uint8Array')),
|
|
34
46
|
str: b.tsStringKeyword(),
|
|
35
47
|
text: b.tsStringKeyword(),
|
|
36
48
|
tstr: b.tsStringKeyword(),
|
|
37
49
|
range: b.tsNumberKeyword(),
|
|
50
|
+
undefined: b.tsUndefinedKeyword(),
|
|
38
51
|
nil: b.tsNullKeyword(),
|
|
39
52
|
null: b.tsNullKeyword()
|
|
40
53
|
}
|
|
54
|
+
const RECORD_KEY_TYPES = new Set([
|
|
55
|
+
'int', 'uint', 'nint', 'integer', 'unsigned', 'number',
|
|
56
|
+
'float', 'float16', 'float32', 'float64', 'float16-32', 'float32-64',
|
|
57
|
+
'str', 'text', 'tstr'
|
|
58
|
+
])
|
|
41
59
|
type ObjectEntry = types.namedTypes.TSCallSignatureDeclaration | types.namedTypes.TSConstructSignatureDeclaration | types.namedTypes.TSIndexSignature | types.namedTypes.TSMethodSignature | types.namedTypes.TSPropertySignature
|
|
42
60
|
type ObjectBody = ObjectEntry[]
|
|
43
61
|
type TSTypeKind = types.namedTypes.TSAsExpression['typeAnnotation']
|
|
@@ -195,7 +213,7 @@ function parseAssignment (assignment: Assignment) {
|
|
|
195
213
|
if (props.length === 1) {
|
|
196
214
|
const prop = props[0]
|
|
197
215
|
const propType = Array.isArray(prop.Type) ? prop.Type : [prop.Type]
|
|
198
|
-
if (propType.length === 1 &&
|
|
216
|
+
if (propType.length === 1 && RECORD_KEY_TYPES.has(prop.Name)) {
|
|
199
217
|
const value = parseUnionType(assignment)
|
|
200
218
|
const expr = b.tsTypeAliasDeclaration(id, value)
|
|
201
219
|
expr.comments = assignment.Comments.map((c) => b.commentLine(` ${c.Content}`, true))
|
|
@@ -578,7 +596,7 @@ function parseUnionType (t: PropertyType | Assignment): TSTypeKind {
|
|
|
578
596
|
/**
|
|
579
597
|
* {*text => text} which will be transformed to `Record<string, string>`
|
|
580
598
|
*/
|
|
581
|
-
if (prop.length === 1 &&
|
|
599
|
+
if (prop.length === 1 && RECORD_KEY_TYPES.has((prop[0] as Property).Name)) {
|
|
582
600
|
return b.tsTypeReference(
|
|
583
601
|
b.identifier('Record'),
|
|
584
602
|
b.tsTypeParameterInstantiation([
|
package/tests/transform.test.ts
CHANGED
|
@@ -1,21 +1,84 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest'
|
|
2
2
|
import { transform } from '../src/index.js'
|
|
3
|
-
import type { Variable } from 'cddl'
|
|
3
|
+
import type { Group, Property, Variable } from 'cddl'
|
|
4
|
+
|
|
5
|
+
function variable(name: string, propertyType: Variable['PropertyType']): Variable {
|
|
6
|
+
return {
|
|
7
|
+
Type: 'variable',
|
|
8
|
+
Name: name,
|
|
9
|
+
PropertyType: propertyType,
|
|
10
|
+
Comments: [],
|
|
11
|
+
IsChoiceAddition: false
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function property(name: string, type: Property['Type']): Property {
|
|
16
|
+
return {
|
|
17
|
+
HasCut: false,
|
|
18
|
+
Occurrence: { n: 1, m: 1 },
|
|
19
|
+
Name: name,
|
|
20
|
+
Type: type,
|
|
21
|
+
Comments: []
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function group(name: string, properties: Group['Properties']): Group {
|
|
26
|
+
return {
|
|
27
|
+
Type: 'group',
|
|
28
|
+
Name: name,
|
|
29
|
+
IsChoiceAddition: false,
|
|
30
|
+
Properties: properties,
|
|
31
|
+
Comments: []
|
|
32
|
+
}
|
|
33
|
+
}
|
|
4
34
|
|
|
5
35
|
describe('literal transformation direct', () => {
|
|
6
36
|
it('should transform bigint literals correctly', () => {
|
|
7
|
-
const assignment
|
|
8
|
-
Type: '
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
Type: 'literal',
|
|
12
|
-
Value: 9007199254740995n
|
|
13
|
-
} as any,
|
|
14
|
-
Comments: [],
|
|
15
|
-
IsChoiceAddition: false
|
|
16
|
-
}
|
|
37
|
+
const assignment = variable('MyBigInt', {
|
|
38
|
+
Type: 'literal',
|
|
39
|
+
Value: 9007199254740995n
|
|
40
|
+
} as any)
|
|
17
41
|
|
|
18
42
|
const output = transform([assignment])
|
|
19
43
|
expect(output).toContain('export type MyBigInt = 9007199254740995n;')
|
|
20
44
|
})
|
|
45
|
+
|
|
46
|
+
it('should transform float32 aliases correctly', () => {
|
|
47
|
+
const assignment = variable('score', 'float32')
|
|
48
|
+
|
|
49
|
+
const output = transform([assignment])
|
|
50
|
+
expect(output).toContain('export type Score = number;')
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it.each([
|
|
54
|
+
['integer-value', 'integer', 'number'],
|
|
55
|
+
['negative-value', 'nint', 'number'],
|
|
56
|
+
['unsigned-value', 'unsigned', 'number'],
|
|
57
|
+
['half-float', 'float16', 'number'],
|
|
58
|
+
['double-float', 'float64', 'number'],
|
|
59
|
+
['float-window', 'float16-32', 'number'],
|
|
60
|
+
['float-range', 'float32-64', 'number'],
|
|
61
|
+
['binary-payload', 'bytes', 'Uint8Array'],
|
|
62
|
+
['binary-blob', 'bstr', 'Uint8Array'],
|
|
63
|
+
['missing-value', 'undefined', 'undefined']
|
|
64
|
+
] as const)('should map %s (%s) to %s', (name, propertyType, expectedType) => {
|
|
65
|
+
const output = transform([variable(name, propertyType)])
|
|
66
|
+
expect(output).toContain(`export type ${name.split('-').map((part) => `${part[0]!.toUpperCase()}${part.slice(1)}`).join('')} = ${expectedType};`)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('should keep bytes fields as object properties instead of record aliases', () => {
|
|
70
|
+
const output = transform([
|
|
71
|
+
group('network-get-data-result', [
|
|
72
|
+
property('bytes', {
|
|
73
|
+
Type: 'group',
|
|
74
|
+
Value: 'network.BytesValue',
|
|
75
|
+
Unwrapped: false
|
|
76
|
+
} as any)
|
|
77
|
+
])
|
|
78
|
+
])
|
|
79
|
+
|
|
80
|
+
expect(output).toContain('export interface NetworkGetDataResult {')
|
|
81
|
+
expect(output).toContain('bytes: NetworkBytesValue;')
|
|
82
|
+
expect(output).not.toContain('export type NetworkGetDataResult = Record')
|
|
83
|
+
})
|
|
21
84
|
})
|