Skip to content

Commit

Permalink
Generate TSDoc @deprecated tag
Browse files Browse the repository at this point in the history
Fixes #58
  • Loading branch information
luisfpg committed Jan 8, 2020
1 parent 7252716 commit 3953a86
Show file tree
Hide file tree
Showing 7 changed files with 21,853 additions and 7,132 deletions.
7 changes: 5 additions & 2 deletions lib/gen-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,19 @@ export function toBasicChars(text: string, firstNonDigit = false): string {
/**
* Returns the TypeScript comments for the given schema description, in a given indentation level
*/
export function tsComments(description: string | undefined, level: number) {
export function tsComments(description: string | undefined, level: number, deprecated?: boolean) {
const indent = ' '.repeat(level);
if (description == undefined || description.length === 0) {
return indent;
return indent + (deprecated ? '/** @deprecated */' : '');
}
const lines = description.trim().split('\n');
let result = '\n' + indent + '/**\n';
lines.forEach(line => {
result += indent + ' *' + (line === '' ? '' : ' ' + line.replace(/\*\//g, '* / ')) + '\n';
});
if (deprecated) {
result += indent + ' *\n' + indent + ' * @deprecated\n';
}
result += indent + ' */\n' + indent;
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class Model extends GenType {
super(name, unqualifiedName, options);

const description = schema.description || '';
this.tsComments = tsComments(description, 0);
this.tsComments = tsComments(description, 0, schema.deprecated);

const type = schema.type || 'any';

Expand Down
4 changes: 2 additions & 2 deletions lib/operation-variant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export class OperationVariant {
if (description !== '') {
description += '\n\n';
}
this.responseMethodTsComments = tsComments(this.responseMethodDescription(), 1);
this.bodyMethodTsComments = tsComments(this.bodyMethodDescription(), 1);
this.responseMethodTsComments = tsComments(this.responseMethodDescription(), 1, operation.deprecated);
this.bodyMethodTsComments = tsComments(this.bodyMethodDescription(), 1, operation.deprecated);
}

private inferResponseType(mediaType: string): string {
Expand Down
5 changes: 4 additions & 1 deletion lib/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class Operation {
hasParameters: boolean;
parametersRequired = false;
security: Security[][] = [];
deprecated: boolean;

tsComments: string;
requestBody?: RequestBody;
Expand All @@ -39,7 +40,7 @@ export class Operation {
this.path = this.path.replace(/\'/g, '\\\'');
this.tags = spec.tags || [];

this.tsComments = tsComments(spec.description || '', 1);
this.tsComments = tsComments(spec.description || '', 1, spec.deprecated);
this.pathVar = `${upperFirst(id)}Path`;
this.methodName = spec['x-operation-name'] || this.id;

Expand Down Expand Up @@ -71,6 +72,8 @@ export class Operation {
this.allResponses = responses.all;
this.pathExpression = this.toPathExpression();

this.deprecated = !!spec.deprecated;

// Now calculate the variants: request body content x success response content
this.calculateVariants();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Parameter {
constructor(public spec: ParameterObject, options: Options) {
this.name = spec.name;
this.var = methodName(this.name);
this.tsComments = tsComments(spec.description || '', 2);
this.tsComments = tsComments(spec.description || '', 2, spec.deprecated);
this.in = spec.in || 'query';
this.required = this.in === 'path' || spec.required || false;
this.type = tsType(spec.schema, options);
Expand Down
2 changes: 1 addition & 1 deletion lib/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export class Property {

this.type = tsType(this.schema, options);
const description = (schema as SchemaObject).description || '';
this.tsComments = tsComments(description, 1);
this.tsComments = tsComments(description, 1, (schema as SchemaObject).deprecated);
}
}
28,963 changes: 21,839 additions & 7,124 deletions test/cyclos.json

Large diffs are not rendered by default.

0 comments on commit 3953a86

Please sign in to comment.