@@ -74,13 +74,15 @@ export class JsonSchemaGenerator {
74
74
} ;
75
75
76
76
private allSymbols : { [ name : string ] : ts . Type } ;
77
+ private userSymbols : { [ name : string ] : ts . Type } ;
77
78
private inheritingTypes : { [ baseName : string ] : string [ ] } ;
78
79
private tc : ts . TypeChecker ;
79
80
80
81
private reffedDefinitions : { [ key : string ] : Definition } = { } ;
81
82
82
- constructor ( allSymbols : { [ name : string ] : ts . Type } , inheritingTypes : { [ baseName : string ] : string [ ] } , tc : ts . TypeChecker , private args = getDefaultArgs ( ) ) {
83
+ constructor ( allSymbols : { [ name : string ] : ts . Type } , userSymbols : { [ name : string ] : ts . Type } , inheritingTypes : { [ baseName : string ] : string [ ] } , tc : ts . TypeChecker , private args = getDefaultArgs ( ) ) {
83
84
this . allSymbols = allSymbols ;
85
+ this . userSymbols = userSymbols ;
84
86
this . inheritingTypes = inheritingTypes ;
85
87
this . tc = tc ;
86
88
}
@@ -674,6 +676,10 @@ export class JsonSchemaGenerator {
674
676
}
675
677
return root ;
676
678
}
679
+
680
+ public getUserSymbols ( ) : { [ name : string ] : ts . Type } {
681
+ return this . userSymbols ;
682
+ }
677
683
}
678
684
679
685
export function getProgramFromFiles ( files : string [ ] , compilerOptions : ts . CompilerOptions = { } ) : ts . Program {
@@ -689,7 +695,7 @@ export function getProgramFromFiles(files: string[], compilerOptions: ts.Compile
689
695
return ts . createProgram ( files , options ) ;
690
696
}
691
697
692
- export function generateSchema ( program : ts . Program , fullTypeName : string , args = getDefaultArgs ( ) ) {
698
+ export function buildGenerator ( program : ts . Program , args = getDefaultArgs ( ) ) : JsonSchemaGenerator {
693
699
const typeChecker = program . getTypeChecker ( ) ;
694
700
695
701
var diagnostics = ts . getPreEmitDiagnostics ( program ) ;
@@ -704,10 +710,10 @@ export function generateSchema(program: ts.Program, fullTypeName: string, args =
704
710
function inspect ( node : ts . Node , tc : ts . TypeChecker ) {
705
711
706
712
if ( node . kind === ts . SyntaxKind . ClassDeclaration
707
- || node . kind === ts . SyntaxKind . InterfaceDeclaration
708
- || node . kind === ts . SyntaxKind . EnumDeclaration
709
- || node . kind === ts . SyntaxKind . TypeAliasDeclaration
710
- ) {
713
+ || node . kind === ts . SyntaxKind . InterfaceDeclaration
714
+ || node . kind === ts . SyntaxKind . EnumDeclaration
715
+ || node . kind === ts . SyntaxKind . TypeAliasDeclaration
716
+ ) {
711
717
const symbol : ts . Symbol = ( < any > node ) . symbol ;
712
718
let fullName = tc . getFullyQualifiedName ( symbol ) ;
713
719
@@ -742,27 +748,33 @@ export function generateSchema(program: ts.Program, fullTypeName: string, args =
742
748
inspect ( sourceFile , typeChecker ) ;
743
749
} ) ;
744
750
745
- const generator = new JsonSchemaGenerator ( allSymbols , inheritingTypes , typeChecker , args ) ;
746
- let definition : Definition ;
747
- if ( fullTypeName === "*" ) { // All types in file(s)
748
- definition = generator . getSchemaForSymbols ( userSymbols ) ;
749
- } else { // Use specific type as root object
750
- definition = generator . getSchemaForSymbol ( fullTypeName ) ;
751
- }
752
- return definition ;
751
+ return new JsonSchemaGenerator ( allSymbols , userSymbols , inheritingTypes , typeChecker , args ) ;
753
752
} else {
754
753
diagnostics . forEach ( ( diagnostic ) => {
755
754
let message = ts . flattenDiagnosticMessageText ( diagnostic . messageText , "\n" ) ;
756
755
if ( diagnostic . file ) {
757
- let { line, character } = diagnostic . file . getLineAndCharacterOfPosition ( diagnostic . start ) ;
758
- console . warn ( `${ diagnostic . file . fileName } (${ line + 1 } ,${ character + 1 } ): ${ message } ` ) ;
756
+ let { line, character } = diagnostic . file . getLineAndCharacterOfPosition ( diagnostic . start ) ;
757
+ console . warn ( `${ diagnostic . file . fileName } (${ line + 1 } ,${ character + 1 } ): ${ message } ` ) ;
759
758
} else {
760
759
console . warn ( message ) ;
761
760
}
762
761
} ) ;
763
762
}
764
763
}
765
764
765
+ export function generateSchema ( program : ts . Program , fullTypeName : string , args = getDefaultArgs ( ) ) {
766
+
767
+ const generator = buildGenerator ( program , args ) ;
768
+
769
+ let definition : Definition ;
770
+ if ( fullTypeName === "*" ) { // All types in file(s)
771
+ definition = generator . getSchemaForSymbols ( generator . getUserSymbols ( ) ) ;
772
+ } else { // Use specific type as root object
773
+ definition = generator . getSchemaForSymbol ( fullTypeName ) ;
774
+ }
775
+ return definition ;
776
+ }
777
+
766
778
export function programFromConfig ( configFileName : string ) {
767
779
// basically a copy of https://github.com/Microsoft/TypeScript/blob/3663d400270ccae8b69cbeeded8ffdc8fa12d7ad/src/compiler/tsc.ts -> parseConfigFile
768
780
const result = ts . parseConfigFileTextToJson ( configFileName , ts . sys . readFile ( configFileName ) ) ;
0 commit comments