cddl2ts 0.1.0 → 0.2.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/README.md +1 -1
- package/bin/cddl2ts.js +0 -4
- package/build/index.d.ts.map +1 -1
- package/build/index.js +27 -5
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
CDDL to TypeScript ](https://github.com/christian-bromann/cddl2ts/actions/workflows/test.yml)
|
|
2
2
|
==================
|
|
3
3
|
|
|
4
4
|
> A Node.js package that can generate a TypeScript definition based on a CDDL file
|
package/bin/cddl2ts.js
CHANGED
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,KAAK,EAAE,UAAU,EAAmG,MAAM,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAmG,MAAM,MAAM,CAAA;AAsBvI,wBAAgB,SAAS,CAAE,WAAW,EAAE,UAAU,EAAE,UAkBnD"}
|
package/build/index.js
CHANGED
|
@@ -4,6 +4,7 @@ import typescriptParser from 'recast/parsers/typescript.js';
|
|
|
4
4
|
import { pkg } from './constants.js';
|
|
5
5
|
const b = types.builders;
|
|
6
6
|
const NATIVE_TYPES = {
|
|
7
|
+
any: b.tsAnyKeyword(),
|
|
7
8
|
number: b.tsNumberKeyword(),
|
|
8
9
|
float: b.tsNumberKeyword(),
|
|
9
10
|
uint: b.tsNumberKeyword(),
|
|
@@ -46,10 +47,19 @@ function parseAssignment(ast, assignment) {
|
|
|
46
47
|
}
|
|
47
48
|
const expr = b.tsTypeAliasDeclaration(id, typeParameters);
|
|
48
49
|
expr.comments = assignment.Comments.map((c) => b.commentLine(` ${c.Content}`, true));
|
|
49
|
-
return expr;
|
|
50
|
+
return b.exportDeclaration(false, expr);
|
|
50
51
|
}
|
|
51
52
|
if (assignment.Type === 'group') {
|
|
52
53
|
const id = b.identifier(camelcase(assignment.Name, { pascalCase: true }));
|
|
54
|
+
/**
|
|
55
|
+
* transform CDDL groups like `Extensible = (*text => any)`
|
|
56
|
+
*/
|
|
57
|
+
if (assignment.Properties.length === 1 && assignment.Properties[0].Type.length === 1 && Object.keys(NATIVE_TYPES).includes((assignment.Properties[0].Name))) {
|
|
58
|
+
const value = parseUnionType(assignment);
|
|
59
|
+
const expr = b.tsTypeAliasDeclaration(id, value);
|
|
60
|
+
expr.comments = assignment.Comments.map((c) => b.commentLine(` ${c.Content}`, true));
|
|
61
|
+
return b.exportDeclaration(false, expr);
|
|
62
|
+
}
|
|
53
63
|
const objectType = parseObjectType(assignment.Properties);
|
|
54
64
|
const extendInterfaces = assignment.Properties
|
|
55
65
|
.filter((prop) => prop.Name === '')
|
|
@@ -57,7 +67,7 @@ function parseAssignment(ast, assignment) {
|
|
|
57
67
|
const expr = b.tsInterfaceDeclaration(id, b.tsInterfaceBody(objectType));
|
|
58
68
|
expr.extends = extendInterfaces;
|
|
59
69
|
expr.comments = assignment.Comments.map((c) => b.commentLine(` ${c.Content}`, true));
|
|
60
|
-
return expr;
|
|
70
|
+
return b.exportDeclaration(false, expr);
|
|
61
71
|
}
|
|
62
72
|
if (assignment.Type === 'array') {
|
|
63
73
|
const id = b.identifier(camelcase(assignment.Name, { pascalCase: true }));
|
|
@@ -71,7 +81,7 @@ function parseAssignment(ast, assignment) {
|
|
|
71
81
|
const value = b.tsArrayType(b.tsParenthesizedType(b.tsUnionType(obj)));
|
|
72
82
|
const expr = b.tsTypeAliasDeclaration(id, value);
|
|
73
83
|
expr.comments = assignment.Comments.map((c) => b.commentLine(` ${c.Content}`, true));
|
|
74
|
-
return expr;
|
|
84
|
+
return b.exportDeclaration(false, expr);
|
|
75
85
|
}
|
|
76
86
|
throw new Error(`Unknown assignment type "${assignment.Type}"`);
|
|
77
87
|
}
|
|
@@ -146,10 +156,22 @@ function parseUnionType(t) {
|
|
|
146
156
|
else if (t.Type === 'group') {
|
|
147
157
|
const value = t.Value;
|
|
148
158
|
/**
|
|
149
|
-
* check if we have
|
|
150
|
-
* ?attributes: {*text => text},
|
|
159
|
+
* check if we have special groups
|
|
151
160
|
*/
|
|
152
161
|
if (!value && t.Properties) {
|
|
162
|
+
const prop = t.Properties;
|
|
163
|
+
/**
|
|
164
|
+
* {*text => text} which will be transformed to `Record<string, string>`
|
|
165
|
+
*/
|
|
166
|
+
if (prop.length === 1 && Object.keys(NATIVE_TYPES).includes(prop[0].Name)) {
|
|
167
|
+
return b.tsTypeReference(b.identifier('Record'), b.tsTypeParameterInstantiation([
|
|
168
|
+
NATIVE_TYPES[prop[0].Name],
|
|
169
|
+
parseUnionType(prop[0].Type[0])
|
|
170
|
+
]));
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* e.g. ?attributes: {*foo => text},
|
|
174
|
+
*/
|
|
153
175
|
return b.tsTypeLiteral(parseObjectType(t.Properties));
|
|
154
176
|
}
|
|
155
177
|
return b.tsTypeReference(b.identifier(camelcase(value.toString(), { pascalCase: true })));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cddl2ts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@babel/parser": "^7.21.9",
|
|
46
46
|
"camelcase": "^7.0.1",
|
|
47
|
-
"cddl": "^0.8.
|
|
47
|
+
"cddl": "^0.8.4",
|
|
48
48
|
"recast": "^0.23.2",
|
|
49
49
|
"yargs": "^17.7.2"
|
|
50
50
|
}
|