Skip to content
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

Preserve attribute tags in JSDoc comments #3508

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions packages/gen-typescript-declarations/src/gen-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,9 @@ class TypeGenerator {
readOnly: property.readOnly,
});
p.description = property.description || '';
if (property.jsdoc) {
p.tags = property.jsdoc.tags || [];
}
tsProperties.push(p);
}
return tsProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ declare namespace Polymer {
* The z-depth of this element, from 0-5. Setting to 0 will remove the
* shadow, and each increasing number greater than 0 will be "deeper"
* than the last.
* @attribute elevation
*/
readonly elevation: number|null|undefined;
hostAttributes: object|null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface PaperButtonBehavior extends IronButtonState, IronControlState, PaperRi
* The z-depth of this element, from 0-5. Setting to 0 will remove the
* shadow, and each increasing number greater than 0 will be "deeper"
* than the last.
* @attribute elevation
*/
readonly elevation: number|null|undefined;
hostAttributes: object|null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ declare namespace Polymer {
* The z-depth of this element, from 0-5. Setting to 0 will remove the
* shadow, and each increasing number greater than 0 will be "deeper"
* than the last.
* @attribute elevation
*/
readonly elevation: number|null|undefined;
hostAttributes: object|null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ declare module 'goog:polymer.paperBehaviors.paperButtonBehavior' {
* The z-depth of this element, from 0-5. Setting to 0 will remove the
* shadow, and each increasing number greater than 0 will be "deeper"
* than the last.
* @attribute elevation
*/
readonly elevation: number|null|undefined;
hostAttributes: object|null;
Expand Down
31 changes: 31 additions & 0 deletions packages/gen-typescript-declarations/src/test/serialize_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,37 @@ suite('serializeTsDeclarations', () => {
assert.equal(p.serialize(), '"my-unsafe-property": string;\n');
});

test('property with custom tag', () => {
const p = new ts.Property({
name: 'myProperty',
tags: [{ title: 'attr', description: '{string} my-property' }],
type: new ts.NameType('string'),
});
assert.equal(p.serialize(), `
/**
* @attr {string} my-property
*/
myProperty: string;
`);
});

test('property with description and custom tag', () => {
const p = new ts.Property({
name: 'myProperty',
tags: [{ title: 'attr', description: '{string} my-property' }],
type: new ts.NameType('string'),
});
p.description = 'This is my property.\nIt has a multi-line description.';
assert.equal(p.serialize(), `
/**
* This is my property.
* It has a multi-line description.
* @attr {string} my-property
*/
myProperty: string;
`);
});

test('function with description', () => {
const m = new ts.Function({
name: 'MyMethod',
Expand Down
11 changes: 10 additions & 1 deletion packages/gen-typescript-declarations/src/ts-ast/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* rights grant found at http://polymer.github.io/PATENTS.txt
*/

import {Tag} from 'doctrine';
import {formatComment, indent, quotePropertyName} from './formatting';
import {Node} from './index';
import {anyType, ParamType, Type} from './types';
Expand Down Expand Up @@ -358,17 +359,20 @@ export class Property {
description: string;
type: Type;
readOnly: boolean;
tags: Tag[] = [];

constructor(data: {
name: string,
description?: string,
type?: Type,
readOnly?: boolean,
tags?: Tag[],
}) {
this.name = data.name;
this.description = data.description || '';
this.type = data.type || anyType;
this.readOnly = data.readOnly || false;
this.tags = data.tags || [];
}

* traverse(): Iterable<Node> {
Expand All @@ -379,8 +383,13 @@ export class Property {
serialize(depth: number = 0): string {
let out = '';
const i = indent(depth);
const tags = this.tags
.filter((tag) => tag.title === 'attr' || tag.title === 'attribute')
.map((tag) => '@' + tag.title + ' ' + tag.description).join('\n');
if (this.description) {
out += '\n' + formatComment(this.description, depth);
out += '\n' + formatComment(tags ? this.description + '\n' + tags : this.description, depth);
} else if (tags) {
out += '\n' + formatComment(tags, depth);
}
out += i;
if (this.readOnly) {
Expand Down