Skip to content

Commit 367f257

Browse files
authored
Add a Make target to expand generics in schema.json (#1763)
1 parent 9b441f2 commit 367f257

File tree

5 files changed

+470
-12
lines changed

5 files changed

+470
-12
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ clean-dep: ## Clean npm dependencies
4444
@rm -rf compiler/node_modules
4545
@rm -rf typescript-generator/node_modules
4646

47+
transform-expand-generics: ## Create a new schema with all generics expanded
48+
@npm run transform-expand-generics --prefix compiler
49+
4750
contrib: | generate license-check spec-format-fix ## Pre contribution target
4851

4952
help: ## Display help

compiler/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"format:check": "prettier --config .prettierrc.json --check ../specification/",
1010
"format:fix": "prettier --config .prettierrc.json --write ../specification/",
1111
"generate-schema": "ts-node src/index.ts",
12+
"transform-expand-generics": "ts-node src/transform/expand-generics.ts",
1213
"compile:specification": "tsc --project ../specification/tsconfig.json --noEmit",
1314
"build": "rm -rf lib && tsc",
1415
"validate-ts-view": "tsc --noEmit ../output/typescript/types.ts",

compiler/src/model/build-model.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import {
5454
verifyUniqueness,
5555
parseJsDocTags,
5656
deepEqual,
57-
sourceLocation
57+
sourceLocation, sortTypeDefinitions
5858
} from './utils'
5959

6060
const jsonSpec = buildJsonSpec()
@@ -150,17 +150,7 @@ export function compileSpecification (endpointMappings: Record<string, model.End
150150
}
151151

152152
// Sort the types in alphabetical order
153-
model.types.sort((a, b) => {
154-
if (a.name.namespace === b.name.namespace) {
155-
if (a.name.name > b.name.name) return 1
156-
if (a.name.name < b.name.name) return -1
157-
return 0
158-
}
159-
160-
if (a.name.namespace > b.name.namespace) return 1
161-
if (a.name.namespace < b.name.namespace) return -1
162-
return 0
163-
})
153+
sortTypeDefinitions(model.types)
164154

165155
return model
166156
}

compiler/src/model/utils.ts

+17
Original file line numberDiff line numberDiff line change
@@ -1287,3 +1287,20 @@ export function sourceLocation (node: Node): string {
12871287
const sourceFile = node.getSourceFile()
12881288
return `${sourceFile.getFilePath().replace(basePath, '')}#L${node.getStartLineNumber(true)}-L${node.getEndLineNumber()}`
12891289
}
1290+
1291+
/**
1292+
* Sort an array of type definitions by type name
1293+
*/
1294+
export function sortTypeDefinitions (types: model.TypeDefinition[]): void {
1295+
types.sort((a, b) => {
1296+
if (a.name.namespace === b.name.namespace) {
1297+
if (a.name.name > b.name.name) return 1
1298+
if (a.name.name < b.name.name) return -1
1299+
return 0
1300+
}
1301+
1302+
if (a.name.namespace > b.name.namespace) return 1
1303+
if (a.name.namespace < b.name.namespace) return -1
1304+
return 0
1305+
})
1306+
}

0 commit comments

Comments
 (0)