1
- // @ts -check
1
+ // eslint-disable-next-line no-restricted-imports
2
+ import type { SchemaObject } from "ajv" ;
2
3
import * as fs from "node:fs/promises" ;
3
4
import * as path from "node:path" ;
4
5
import { URL , fileURLToPath } from "node:url" ;
5
6
import { generateSchema } from "../schema.mjs" ;
7
+ import { assertDefinition } from "./generate-schema.mts" ;
8
+
9
+ type Language = {
10
+ options : {
11
+ indent : string ;
12
+ level : number ;
13
+ footer ?: string ;
14
+ header ?: string ;
15
+ } ;
16
+ arrayProperty : ( name : string , type : string , required : boolean ) => string ;
17
+ objectProperty : ( name : string , required : boolean ) => string ;
18
+ stringProperty : ( name : string , required : boolean ) => string ;
19
+ structBegin : ( name : string ) => string ;
20
+ structEnd : string ;
21
+ } ;
6
22
7
- /**
8
- * @import { SchemaObject } from "ajv";
9
- * @import { Language } from "../types.js";
10
- */
11
23
const thisScript = fileURLToPath ( import . meta. url ) ;
12
24
13
25
/**
14
26
* Returns the struct name of the definition key or reference.
15
27
* @param {string } ref
16
28
* @returns {string }
17
29
*/
18
- function typename ( ref ) {
30
+ function typename ( ref : string ) : string {
19
31
const i = ref . lastIndexOf ( "/" ) + 1 ;
20
32
return ref [ i ] . toUpperCase ( ) + ref . substring ( i + 1 ) ;
21
33
}
@@ -25,11 +37,10 @@ function typename(ref) {
25
37
* @param {string } output
26
38
* @returns {Language }
27
39
*/
28
- function getLanguage ( output ) {
40
+ function getLanguage ( output : string ) : Language {
29
41
switch ( path . extname ( output ) ) {
30
42
case ".h" : {
31
- /** @type {(type: string, required: boolean) => string } */
32
- const nullable = ( type , required ) =>
43
+ const nullable = ( type : string , required : boolean ) =>
33
44
required ? type : `std::optional<${ type } >` ;
34
45
return {
35
46
options : {
@@ -80,8 +91,7 @@ function getLanguage(output) {
80
91
}
81
92
82
93
case ".kt" : {
83
- /** @type {(required: boolean) => "" | "?" } */
84
- const nullable = ( required ) => ( required ? "" : "?" ) ;
94
+ const nullable = ( required : boolean ) => ( required ? "" : "?" ) ;
85
95
return {
86
96
options : {
87
97
indent : " " ,
@@ -110,8 +120,7 @@ function getLanguage(output) {
110
120
}
111
121
112
122
case ".swift" : {
113
- /** @type {(required: boolean) => "" | "?" } */
114
- const nullable = ( required ) => ( required ? "" : "?" ) ;
123
+ const nullable = ( required : boolean ) => ( required ? "" : "?" ) ;
115
124
return {
116
125
options : {
117
126
indent : " " ,
@@ -152,12 +161,12 @@ function getLanguage(output) {
152
161
153
162
/**
154
163
* Generates a data model from the specified schema definition.
155
- * @param {string } name
156
- * @param {SchemaObject } definition
157
- * @param {Language } lang
158
- * @returns {string[] }
159
164
*/
160
- function generateType ( name , definition , lang ) {
165
+ function generateType (
166
+ name : string ,
167
+ definition : SchemaObject ,
168
+ lang : Language
169
+ ) : string [ ] {
161
170
const { indent, level } = lang . options ;
162
171
const outer = indent . repeat ( level ) ;
163
172
const inner = indent . repeat ( level + 1 ) ;
@@ -166,6 +175,8 @@ function generateType(name, definition, lang) {
166
175
167
176
const { properties, required = [ ] } = definition ;
168
177
Object . entries ( properties ) . forEach ( ( [ name , prop ] ) => {
178
+ assertDefinition ( prop ) ;
179
+
169
180
const isRequired = required . includes ( name ) ;
170
181
switch ( prop . type ) {
171
182
case "array" :
@@ -188,10 +199,8 @@ function generateType(name, definition, lang) {
188
199
189
200
/**
190
201
* Generates manifest data models and writes them to specified path.
191
- * @param {SchemaObject } schema
192
- * @param {string } output
193
202
*/
194
- async function generate ( schema , output ) {
203
+ async function generate ( schema : SchemaObject , output : string ) {
195
204
const lang = getLanguage ( output ) ;
196
205
const lines = [
197
206
`// This file was generated by ${ path . basename ( thisScript ) } .` ,
@@ -204,15 +213,9 @@ async function generate(schema, output) {
204
213
}
205
214
206
215
Object . entries ( schema . $defs ) . forEach ( ( [ key , definition ] ) => {
216
+ assertDefinition ( definition ) ;
207
217
if ( ! ( "exclude-from-codegen" in definition ) ) {
208
- lines . push (
209
- ...generateType (
210
- typename ( key ) ,
211
- /** @type {SchemaObject } */ ( definition ) ,
212
- lang
213
- ) ,
214
- ""
215
- ) ;
218
+ lines . push ( ...generateType ( typename ( key ) , definition , lang ) , "" ) ;
216
219
}
217
220
return lines ;
218
221
} ) ;
0 commit comments