Skip to content

Commit d80b1dd

Browse files
feat: support deprecated and experimental in TSDoc (#325)
1 parent 7de76fa commit d80b1dd

File tree

5 files changed

+1583
-109
lines changed

5 files changed

+1583
-109
lines changed

scripts/protocol-dts-generator.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ const emitGlobalTypeDefs = () => {
109109
const emitDomain = (domain: P.Domain) => {
110110
const domainName = toTitleCase(domain.domain)
111111
emitLine()
112-
emitDescription(domain.description)
112+
emitTsComment(domain)
113113
emitOpenBlock(`export namespace ${domainName}`)
114114
if (domain.types) domain.types.forEach(emitDomainType)
115115
if (domain.commands) domain.commands.forEach(emitCommand)
@@ -118,15 +118,30 @@ const emitDomain = (domain: P.Domain) => {
118118
}
119119

120120
const getCommentLines = (description: string) => {
121-
const lines = description
121+
return description
122122
.split(/\r?\n/g)
123123
.filter(line => !line.startsWith('LINT.'))
124-
.map(line => ` * ${line}`)
125-
return [`/**`, ...lines, ` */`]
126124
}
127125

128-
const emitDescription = (description?: string) => {
129-
if (description) getCommentLines(description).map(l => emitLine(l))
126+
const emitDescription = (lines: string[]) => {
127+
emitLine(`/**`)
128+
lines.map(l => emitLine(` * ${l}`))
129+
emitLine(` */`)
130+
}
131+
132+
const emitTsComment = (object: P.Event|P.PropertyType|P.DomainType|P.Command|P.Domain) => {
133+
const commentLines = object.description ? getCommentLines(object.description) : [];
134+
if('deprecated' in object){
135+
commentLines.push('@deprecated');
136+
}
137+
138+
if('experimental' in object){
139+
commentLines.push('@experimental');
140+
}
141+
142+
if (commentLines.length) {
143+
emitDescription(commentLines)
144+
}
130145
}
131146

132147
const isPropertyInlineEnum = (prop: P.ProtocolType): boolean => {
@@ -175,7 +190,7 @@ const emitProperty = (interfaceName: string, prop: P.PropertyType) => {
175190
description = `${description || ''} (${enumName} enum)`;
176191
}
177192

178-
emitDescription(description)
193+
emitTsComment(prop)
179194
emitLine(`${getPropertyDef(interfaceName, prop)};`)
180195
}
181196

@@ -218,7 +233,7 @@ const emitInterface = (interfaceName: string, props?: P.PropertyType[]) => {
218233
const emitDomainType = (type: P.DomainType) => {
219234
emitInlineEnumForDomainType(type);
220235
emitLine()
221-
emitDescription(type.description)
236+
emitTsComment(type)
222237

223238
if (type.type === 'object') {
224239
emitInterface(type.id, type.properties)
@@ -256,7 +271,7 @@ const emitEvent = (event: P.Event) => {
256271

257272
emitInlineEnumsForEvents(event);
258273
emitLine()
259-
emitDescription(event.description)
274+
emitTsComment(event)
260275
emitInterface(toEventPayloadName(event.name), event.parameters)
261276
}
262277

@@ -309,7 +324,7 @@ const emitMapping = (moduleName: string, protocolModuleName: string, domains: P.
309324
emitHeaderComments()
310325
emitLine(`import Protocol from './${protocolModuleName}'`)
311326
emitLine()
312-
emitDescription('Mappings from protocol event and command names to the types required for them.')
327+
emitDescription(['Mappings from protocol event and command names to the types required for them.'])
313328
emitOpenBlock(`export namespace ${moduleName}`)
314329

315330
const protocolModulePrefix = toTitleCase(protocolModuleName)
@@ -334,7 +349,7 @@ const emitMapping = (moduleName: string, protocolModuleName: string, domains: P.
334349

335350
const emitApiCommand = (command: P.Command, domainName: string, modulePrefix: string, eventStyle: EventStyleEnum) => {
336351
const prefix = `${modulePrefix}.${domainName}.`
337-
emitDescription(command.description)
352+
emitTsComment(command)
338353
const params = command.parameters ? `params: ${prefix}${toCmdRequestName(command.name)}` : ''
339354
const response = command.returns ? `${prefix}${toCmdResponseName(command.name)}` : 'void'
340355
switch (eventStyle) {
@@ -352,7 +367,7 @@ const emitApiCommand = (command: P.Command, domainName: string, modulePrefix: st
352367

353368
const emitApiEvent = (event: P.Event, domainName: string, modulePrefix: string, eventStyle: EventStyleEnum) => {
354369
const prefix = `${modulePrefix}.${domainName}.`
355-
emitDescription(event.description)
370+
emitTsComment(event)
356371
switch (eventStyle) {
357372
case EventStyle.REGULAR: {
358373
const params = event.parameters ? `params: ${prefix}${toEventPayloadName(event.name)}` : ''
@@ -385,7 +400,7 @@ const emitApi = (moduleName: string, protocolModuleName: string, domains: P.Doma
385400
emitHeaderComments()
386401
emitLine(`import Protocol from './${protocolModuleName}'`)
387402
emitLine()
388-
emitDescription('API generated from Protocol commands and events.')
403+
emitDescription(['API generated from Protocol commands and events.'])
389404
emitOpenBlock(`export namespace ${moduleName}`)
390405

391406
emitLine()

scripts/protocol-schema.d.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ export interface IProtocol {
44
domains: Protocol.Domain[]
55
}
66

7-
export module Protocol {
7+
export namespace Protocol {
88
export interface Version {
99
major: string
1010
minor: string
1111
}
1212

13-
export interface Domain {
13+
export interface ExtraInformation {
14+
deprecated?: boolean;
15+
experimental?: boolean;
16+
}
17+
18+
export interface Domain extends ExtraInformation {
1419
/** Name of domain */
1520
domain: string
1621
/** Description of the domain */
@@ -31,7 +36,7 @@ export module Protocol {
3136
redirect?: string
3237
}
3338

34-
export interface Event {
39+
export interface Event extends ExtraInformation {
3540
name: string
3641
parameters?: PropertyType[]
3742
/** Description of the event */
@@ -86,9 +91,9 @@ export module Protocol {
8691
id: string
8792
/** Description of the type */
8893
description?: string
89-
} & (StringType | ObjectType | ArrayType | PrimitiveType)
94+
} & (StringType | ObjectType | ArrayType | PrimitiveType) & ExtraInformation;
9095

91-
type ProtocolType = StringType | ObjectType | ArrayType | PrimitiveType | RefType | AnyType
96+
type ProtocolType = StringType | ObjectType | ArrayType | PrimitiveType | RefType | AnyType;
9297

93-
type PropertyType = PropertyBaseType & ProtocolType
98+
type PropertyType = PropertyBaseType & ProtocolType;
9499
}

0 commit comments

Comments
 (0)