Skip to content

Commit ecf38d7

Browse files
bergundydomoritz
authored andcommitted
Really include only requested include files
1 parent f13354e commit ecf38d7

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

Diff for: test/programs/user-symbols/main.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Context {
2+
ip: string;
3+
}

Diff for: test/programs/user-symbols/schema.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-06/schema#",
3+
"definitions": {
4+
"Context": {
5+
"properties": {
6+
"ip": {
7+
"type": "string"
8+
}
9+
},
10+
"required": [
11+
"ip"
12+
],
13+
"type": "object"
14+
}
15+
}
16+
}
17+

Diff for: test/schema.test.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export function assertSchema(group: string, type: string, settings: TJS.PartialA
1919
settings.required = true;
2020
}
2121

22-
const actual = TJS.generateSchema(TJS.getProgramFromFiles([resolve(BASE + group + "/main.ts")], compilerOptions), type, settings);
22+
const files = [resolve(BASE + group + "/main.ts")];
23+
const actual = TJS.generateSchema(TJS.getProgramFromFiles(files, compilerOptions), type, settings, files);
2324

2425
// writeFileSync(BASE + group + "/schema.json", stringify(actual, {space: 4}) + "\n\n");
2526

@@ -275,6 +276,8 @@ describe("schema", () => {
275276
});
276277

277278
assertSchema("builtin-names", "Ext.Foo");
279+
280+
assertSchema("user-symbols", "*");
278281
});
279282
});
280283

Diff for: typescript-json-schema.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ export function getProgramFromFiles(files: string[], jsonCompilerOptions: any =
10091009
// use built-in default options
10101010
const compilerOptions = ts.convertCompilerOptionsFromJson(jsonCompilerOptions, basePath).options;
10111011
const options: ts.CompilerOptions = {
1012-
noEmit: true, emitDecoratorMetadata: true, experimentalDecorators: true, target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS
1012+
noEmit: true, emitDecoratorMetadata: true, experimentalDecorators: true, target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS, allowUnusedLabels: true,
10131013
};
10141014
for (const k in compilerOptions) {
10151015
if (compilerOptions.hasOwnProperty(k)) {
@@ -1019,7 +1019,13 @@ export function getProgramFromFiles(files: string[], jsonCompilerOptions: any =
10191019
return ts.createProgram(files, options);
10201020
}
10211021

1022-
export function buildGenerator(program: ts.Program, args: PartialArgs = {}): JsonSchemaGenerator|null {
1022+
export function buildGenerator(program: ts.Program, args: PartialArgs = {}, onlyIncludeFiles?: string[]): JsonSchemaGenerator|null {
1023+
function isUserFile(file: ts.SourceFile): boolean {
1024+
if (onlyIncludeFiles === undefined) {
1025+
return !file.hasNoDefaultLib;
1026+
}
1027+
return onlyIncludeFiles.indexOf(file.fileName) >= 0;
1028+
}
10231029
// Use defaults unles otherwise specified
10241030
const settings = getDefaultArgs();
10251031

@@ -1058,10 +1064,11 @@ export function buildGenerator(program: ts.Program, args: PartialArgs = {}): Jso
10581064
const name = !args.uniqueNames ? typeName : `${typeName}.${(<any>symbol).id}`;
10591065

10601066
symbols.push({ name, typeName, fullyQualifiedName, symbol });
1061-
allSymbols[name] = nodeType;
1067+
if (!userSymbols[name]) {
1068+
allSymbols[name] = nodeType;
1069+
}
10621070

1063-
// if (sourceFileIdx === 1) {
1064-
if (!sourceFile.hasNoDefaultLib) {
1071+
if (isUserFile(sourceFile)) {
10651072
userSymbols[name] = symbol;
10661073
}
10671074

@@ -1097,7 +1104,7 @@ export function buildGenerator(program: ts.Program, args: PartialArgs = {}): Jso
10971104
}
10981105

10991106
export function generateSchema(program: ts.Program, fullTypeName: string, args: PartialArgs = {}, onlyIncludeFiles?: string[]): Definition|null {
1100-
const generator = buildGenerator(program, args);
1107+
const generator = buildGenerator(program, args, onlyIncludeFiles);
11011108

11021109
if (generator === null) {
11031110
return null;

0 commit comments

Comments
 (0)