Skip to content

TypeScript 2.5 #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
"name": "Dominik Moritz",
"email": "[email protected]",
"url": "https://www.domoritz.de/"
},
{
"name": "Vladimir Krivosheev",
"email": "[email protected]"
},
{
"name": "Fabian Pirklbauer",
"email": "[email protected]"
}
],
"repository": {
Expand All @@ -40,22 +48,22 @@
"dependencies": {
"glob": "~7.1.2",
"json-stable-stringify": "^1.0.1",
"typescript": "~2.1.5",
"yargs": "^8.0.1"
"typescript": "~2.5.3",
"yargs": "^8.0.2"
},
"devDependencies": {
"@types/assertion-error": "^1.0.30",
"@types/chai": "^3.5.2",
"@types/chai": "^4.0.1",
"@types/glob": "^5.0.30",
"@types/json-stable-stringify": "^1.0.31",
"@types/mocha": "^2.2.41",
"@types/node": "^7.0.22",
"ajv": "^5.1.5",
"chai": "^4.0.0",
"@types/node": "^8.0.7",
"ajv": "^5.2.0",
"chai": "^4.0.2",
"mocha": "^3.4.2",
"source-map-support": "^0.4.15",
"tslint": "^5.3.2",
"ts-node": "^3.0.4"
"ts-node": "^3.1.0",
"tslint": "^5.4.3"
},
"scripts": {
"test": "npm run build && mocha -t 5000 --require source-map-support/register test",
Expand Down
42 changes: 25 additions & 17 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ export type Definition = {
type?: string | string[],
definitions?: {[key: string]: any},
format?: string,
items?: Definition,
items?: Definition | Definition[],
minItems?: number,
additionalItems?: {
anyOf: Definition
anyOf: Definition[]
},
enum?: PrimitiveType[] | Definition[],
default?: PrimitiveType | Object,
additionalProperties?: Definition,
additionalProperties?: Definition | boolean,
required?: string[],
propertyOrder?: string[],
properties?: {},
Expand Down Expand Up @@ -235,13 +235,14 @@ export class JsonSchemaGenerator {

private extractLiteralValue(typ: ts.Type): PrimitiveType | undefined {
if (typ.flags & ts.TypeFlags.EnumLiteral) {
let str = (<ts.LiteralType>typ).text;
// or .text for old TS
let str = (<ts.LiteralType>typ).value || (typ as any).text;
let num = parseFloat(str);
return isNaN(num) ? str : num;
} else if (typ.flags & ts.TypeFlags.StringLiteral) {
return (<ts.LiteralType>typ).text;
return (<ts.LiteralType>typ).value || (typ as any).text;
} else if (typ.flags & ts.TypeFlags.NumberLiteral) {
return parseFloat((<ts.LiteralType>typ).text);
return parseFloat((<ts.LiteralType>typ).value || (typ as any).text);
} else if (typ.flags & ts.TypeFlags.BooleanLiteral) {
return (typ as any).intrinsicName === "true";
}
Expand Down Expand Up @@ -308,7 +309,7 @@ export class JsonSchemaGenerator {
definition.type = typeof value;
definition.enum = [ value ];
} else if (symbol && (symbol.getName() === "Array" || symbol.getName() === "ReadonlyArray")) {
const arrayType = (<ts.TypeReference>propertyType).typeArguments[0];
const arrayType = (<ts.TypeReference>propertyType).typeArguments![0];
definition.type = "array";
definition.items = this.getTypeDefinition(arrayType, tc);
} else {
Expand Down Expand Up @@ -344,6 +345,7 @@ export class JsonSchemaGenerator {
const reffedType = this.getReferencedTypeSymbol(prop, tc);

let definition = this.getTypeDefinition(propertyType, tc, undefined, undefined, prop, reffedType);

if (this.args.titles) {
definition.title = propertyName;
}
Expand Down Expand Up @@ -381,11 +383,11 @@ export class JsonSchemaGenerator {
}

private getEnumDefinition(clazzType: ts.Type, tc: ts.TypeChecker, definition: Definition): Definition {
const node = clazzType.getSymbol().getDeclarations()[0];
const node = clazzType.getSymbol()!.getDeclarations()![0];
const fullName = tc.typeToString(clazzType, undefined, ts.TypeFormatFlags.UseFullyQualifiedType);
const members: ts.EnumMember[] = node.kind === ts.SyntaxKind.EnumDeclaration ?
const members: ts.NodeArray<ts.EnumMember> = node.kind === ts.SyntaxKind.EnumDeclaration ?
(node as ts.EnumDeclaration).members :
[node as ts.EnumMember];
ts.createNodeArray([node as ts.EnumMember]);
var enumValues: (number|boolean|string|null)[] = [];
let enumTypes: string[] = [];

Expand Down Expand Up @@ -526,7 +528,7 @@ export class JsonSchemaGenerator {
}

private getClassDefinition(clazzType: ts.Type, tc: ts.TypeChecker, definition: Definition): Definition {
const node = clazzType.getSymbol().getDeclarations()[0];
const node = clazzType.getSymbol()!.getDeclarations()![0];
if (this.args.typeOfKeyword && node.kind === ts.SyntaxKind.FunctionType) {
definition.typeof = "function";
return definition;
Expand All @@ -546,7 +548,7 @@ export class JsonSchemaGenerator {
definition.oneOf = oneOf;
} else {
if (clazz.members) {
const indexSignatures = clazz.members.filter(x => x.kind === ts.SyntaxKind.IndexSignature);
const indexSignatures = clazz.members == null ? [] : clazz.members.filter(x => x.kind === ts.SyntaxKind.IndexSignature);
if (indexSignatures.length === 1) {
// for case "array-types"
const indexSignature = indexSignatures[0] as ts.IndexSignatureDeclaration;
Expand Down Expand Up @@ -704,6 +706,12 @@ export class JsonSchemaGenerator {

private getTypeDefinition(typ: ts.Type, tc: ts.TypeChecker, asRef = this.args.ref, unionModifier: string = "anyOf", prop?: ts.Symbol, reffedType?: ts.Symbol): Definition {
const definition: Definition = {}; // real definition

if (this.args.typeOfKeyword && (typ.flags & ts.TypeFlags.Object) && ((<ts.ObjectType>typ).objectFlags & ts.ObjectFlags.Anonymous)) {
definition.typeof = "function";
return definition;
}

let returnedDefinition = definition; // returned definition, may be a $ref

const symbol = typ.getSymbol();
Expand Down Expand Up @@ -750,7 +758,7 @@ export class JsonSchemaGenerator {
if (prop) {
this.parseCommentsIntoDefinition(prop, returnedDefinition, otherAnnotations);
}
this.parseCommentsIntoDefinition(symbol, definition, otherAnnotations);
this.parseCommentsIntoDefinition(symbol!, definition, otherAnnotations);

// Create the actual definition only if is an inline definition, or
// if it will be a $ref and it is not yet created
Expand All @@ -761,7 +769,7 @@ export class JsonSchemaGenerator {
definition.title = fullTypeName;
}
}
const node = symbol && symbol.getDeclarations() !== undefined ? symbol.getDeclarations()[0] : null;
const node = symbol && symbol.getDeclarations() !== undefined ? symbol.getDeclarations()![0] : null;

if (definition.type === undefined) { // if users override the type, do not try to infer it
if (typ.flags & ts.TypeFlags.Union) {
Expand All @@ -788,7 +796,7 @@ export class JsonSchemaGenerator {
this.getDefinitionForRootType(typ, tc, reffedType!, definition);
} else if (node && (node.kind === ts.SyntaxKind.EnumDeclaration || node.kind === ts.SyntaxKind.EnumMember)) {
this.getEnumDefinition(typ, tc, definition);
} else if (symbol && symbol.flags & ts.SymbolFlags.TypeLiteral && Object.keys(symbol.members).length === 0) {
} else if (symbol && symbol.flags & ts.SymbolFlags.TypeLiteral && symbol.members!.size === 0) {
// {} is TypeLiteral with no members. Need special case because it doesn't have declarations.
definition.type = "object";
definition.properties = {};
Expand Down Expand Up @@ -937,7 +945,7 @@ export function buildGenerator(program: ts.Program, args: PartialArgs = {}): Jso
diagnostics.forEach((diagnostic) => {
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
if(diagnostic.file) {
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
console.error(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
} else {
console.error(message);
Expand Down Expand Up @@ -965,7 +973,7 @@ export function generateSchema(program: ts.Program, fullTypeName: string, args:

export function programFromConfig(configFileName: string): ts.Program {
// basically a copy of https://github.com/Microsoft/TypeScript/blob/3663d400270ccae8b69cbeeded8ffdc8fa12d7ad/src/compiler/tsc.ts -> parseConfigFile
const result = ts.parseConfigFileTextToJson(configFileName, ts.sys.readFile(configFileName));
const result = ts.parseConfigFileTextToJson(configFileName, ts.sys.readFile(configFileName)!);
const configObject = result.config;

const configParseResult = ts.parseJsonConfigFileContent(configObject, ts.sys, path.dirname(configFileName), {}, configFileName);
Expand Down
Loading