| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2017 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  | //@ts-check
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 15:03:57 +08:00
										 |  |  | const playwright = require('../../'); | 
					
						
							| 
									
										
										
										
											2020-12-03 05:50:10 +08:00
										 |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  | const path = require('path'); | 
					
						
							|  |  |  | const Source = require('./Source'); | 
					
						
							| 
									
										
										
										
											2020-12-29 09:38:00 +08:00
										 |  |  | const md = require('../markdown'); | 
					
						
							| 
									
										
										
										
											2020-12-04 01:11:48 +08:00
										 |  |  | const { spawnSync } = require('child_process'); | 
					
						
							| 
									
										
										
										
											2020-12-05 01:03:33 +08:00
										 |  |  | const preprocessor = require('./preprocessor'); | 
					
						
							| 
									
										
										
										
											2020-12-29 09:38:00 +08:00
										 |  |  | const { MDOutline } = require('./MDBuilder'); | 
					
						
							|  |  |  | const missingDocs = require('./missingDocs'); | 
					
						
							| 
									
										
										
										
											2020-12-30 09:35:01 +08:00
										 |  |  | const Documentation = require('./Documentation'); | 
					
						
							| 
									
										
										
										
											2020-02-14 10:26:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 08:19:28 +08:00
										 |  |  | /** @typedef {import('./Documentation').Type} Type */ | 
					
						
							| 
									
										
										
										
											2020-12-29 09:38:00 +08:00
										 |  |  | /** @typedef {import('../markdown').MarkdownNode} MarkdownNode */ | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  | const PROJECT_DIR = path.join(__dirname, '..', '..'); | 
					
						
							|  |  |  | const VERSION = require(path.join(PROJECT_DIR, 'package.json')).version; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const RED_COLOR = '\x1b[31m'; | 
					
						
							|  |  |  | const YELLOW_COLOR = '\x1b[33m'; | 
					
						
							|  |  |  | const RESET_COLOR = '\x1b[0m'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  | const links = new Map(); | 
					
						
							|  |  |  | const rLinks = new Map(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 01:21:56 +08:00
										 |  |  | run().catch(e => { | 
					
						
							|  |  |  |   console.error(e); | 
					
						
							|  |  |  |   process.exit(1); | 
					
						
							|  |  |  | });; | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | async function run() { | 
					
						
							|  |  |  |   const startTime = Date.now(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 08:19:28 +08:00
										 |  |  |   /** @type {!Array<string>} */ | 
					
						
							|  |  |  |   const errors = []; | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |   let changedFiles = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |   const header = fs.readFileSync(path.join(PROJECT_DIR, 'docs-src', 'api-header.md')).toString(); | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  |   let footer = fs.readFileSync(path.join(PROJECT_DIR, 'docs-src', 'api-footer.md')).toString(); | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |   const links = fs.readFileSync(path.join(PROJECT_DIR, 'docs-src', 'api-links.md')).toString(); | 
					
						
							| 
									
										
										
										
											2020-12-29 09:38:00 +08:00
										 |  |  |   const outline = new MDOutline(path.join(PROJECT_DIR, 'docs-src', 'api-body.md'), path.join(PROJECT_DIR, 'docs-src', 'api-params.md')); | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  |   const generatedComment = '<!-- THIS FILE IS NOW GENERATED -->'; | 
					
						
							|  |  |  |   let generatedLinksSuffix; | 
					
						
							|  |  |  |   const api = await Source.readFile(path.join(PROJECT_DIR, 'docs', 'api.md')); | 
					
						
							|  |  |  |   const docs = await (await Source.readdir(path.join(PROJECT_DIR, 'docs'), '.md')).filter(s => s.name() !== 'api.md'); | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 05:50:10 +08:00
										 |  |  |   // Produce api.md
 | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  |     outline.setLinkRenderer(item => { | 
					
						
							| 
									
										
										
										
											2020-12-29 15:42:51 +08:00
										 |  |  |       const { clazz, member, param, option } = item; | 
					
						
							|  |  |  |       if (param) | 
					
						
							|  |  |  |         return `\`${param}\``; | 
					
						
							|  |  |  |       if (option) | 
					
						
							|  |  |  |         return `\`${option}\``; | 
					
						
							|  |  |  |       if (clazz) | 
					
						
							|  |  |  |         return `[${clazz.name}]`; | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  |       return createMemberLink(member); | 
					
						
							| 
									
										
										
										
											2020-12-29 15:42:51 +08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |       const localLinks = []; | 
					
						
							|  |  |  |       for (const clazz of outline.classesArray) | 
					
						
							|  |  |  |         localLinks.push(`[${clazz.name}]: api.md#class-${clazz.name.toLowerCase()} "${clazz.name}"`); | 
					
						
							|  |  |  |       generatedLinksSuffix = localLinks.join('\n') + '\n' + links; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-05 01:03:33 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-12-29 09:38:00 +08:00
										 |  |  |       /** @type {MarkdownNode[]} */ | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |       const result = []; | 
					
						
							|  |  |  |       for (const clazz of outline.classesArray) { | 
					
						
							|  |  |  |         // Iterate over classes, create header node.
 | 
					
						
							| 
									
										
										
										
											2020-12-29 09:38:00 +08:00
										 |  |  |         /** @type {MarkdownNode} */ | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |         const classNode = { type: 'h3', text: `class: ${clazz.name}` }; | 
					
						
							|  |  |  |         result.push(classNode); | 
					
						
							|  |  |  |         // Append class comments
 | 
					
						
							| 
									
										
										
										
											2020-12-29 09:38:00 +08:00
										 |  |  |         classNode.children = (clazz.spec || []).map(c => md.clone(c)); | 
					
						
							| 
									
										
										
										
											2020-12-30 09:35:01 +08:00
										 |  |  |         classNode.children.push({ | 
					
						
							|  |  |  |           type: 'text', | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  |           text: '' | 
					
						
							| 
									
										
										
										
											2020-12-30 09:35:01 +08:00
										 |  |  |         }); | 
					
						
							|  |  |  |         classNode.children.push(...generateToc(clazz)); | 
					
						
							|  |  |  |         if (clazz.extends && clazz.extends !== 'EventEmitter' && clazz.extends !== 'Error') { | 
					
						
							|  |  |  |           const superClass = outline.documentation.classes.get(clazz.extends); | 
					
						
							|  |  |  |           classNode.children.push(...generateToc(superClass)); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for (const member of clazz.membersArray) { | 
					
						
							|  |  |  |           // Iterate members
 | 
					
						
							| 
									
										
										
										
											2020-12-29 09:38:00 +08:00
										 |  |  |           /** @type {MarkdownNode} */ | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |           const memberNode = { type: 'h4', children: [] }; | 
					
						
							|  |  |  |           if (member.kind === 'event') { | 
					
						
							| 
									
										
										
										
											2020-12-29 15:42:51 +08:00
										 |  |  |             memberNode.text = `${clazz.varName}.on('${member.name}')`; | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |           } else if (member.kind === 'property') { | 
					
						
							| 
									
										
										
										
											2020-12-29 15:42:51 +08:00
										 |  |  |             memberNode.text = `${clazz.varName}.${member.name}`; | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |           } else if (member.kind === 'method') { | 
					
						
							|  |  |  |             // Patch method signatures
 | 
					
						
							| 
									
										
										
										
											2020-12-29 15:42:51 +08:00
										 |  |  |             memberNode.text = `${clazz.varName}.${member.name}(${member.signature})`; | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |             for (const arg of member.argsArray) { | 
					
						
							|  |  |  |               if (arg.type) | 
					
						
							|  |  |  |                memberNode.children.push(renderProperty(`\`${arg.name}\``, arg.type, arg.spec)); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-12-05 03:09:20 +08:00
										 |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |           // Append type
 | 
					
						
							|  |  |  |           if (member.type && member.type.name !== 'void') { | 
					
						
							|  |  |  |             let name; | 
					
						
							|  |  |  |             switch (member.kind) { | 
					
						
							|  |  |  |               case 'event': name = 'type:'; break; | 
					
						
							|  |  |  |               case 'property': name = 'type:'; break; | 
					
						
							|  |  |  |               case 'method': name = 'returns:'; break; | 
					
						
							| 
									
										
										
										
											2020-12-05 10:05:35 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |             memberNode.children.push(renderProperty(name, member.type)); | 
					
						
							| 
									
										
										
										
											2020-12-05 03:09:20 +08:00
										 |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |           // Append member doc
 | 
					
						
							| 
									
										
										
										
											2020-12-29 09:38:00 +08:00
										 |  |  |           memberNode.children.push(...(member.spec || []).map(c => md.clone(c))); | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |           classNode.children.push(memberNode); | 
					
						
							| 
									
										
										
										
											2020-12-05 01:03:33 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-04 14:28:11 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  |       footer = outline.renderLinksInText(footer); | 
					
						
							|  |  |  |       api.setText([generatedComment, header, md.render(result), footer, generatedLinksSuffix].join('\n')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Produce other docs
 | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     for (const doc of docs) { | 
					
						
							|  |  |  |       if (!fs.existsSync(path.join(PROJECT_DIR, 'docs-src', doc.name()))) | 
					
						
							|  |  |  |         continue; | 
					
						
							|  |  |  |       const content = fs.readFileSync(path.join(PROJECT_DIR, 'docs-src', doc.name())).toString(); | 
					
						
							|  |  |  |       doc.setText([generatedComment, outline.renderLinksInText(content), generatedLinksSuffix].join('\n')); | 
					
						
							| 
									
										
										
										
											2020-12-04 14:28:11 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-03 05:50:10 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |   // Documentation checks.
 | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  |     const readme = await Source.readFile(path.join(PROJECT_DIR, 'README.md')); | 
					
						
							|  |  |  |     const binReadme = await Source.readFile(path.join(PROJECT_DIR, 'bin', 'README.md')); | 
					
						
							|  |  |  |     const contributing = await Source.readFile(path.join(PROJECT_DIR, 'CONTRIBUTING.md')); | 
					
						
							|  |  |  |     const mdSources = [readme, binReadme, api, contributing, ...docs]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-13 05:03:01 +08:00
										 |  |  |     const browserVersions = await getBrowserVersions(); | 
					
						
							| 
									
										
										
										
											2020-12-29 08:19:28 +08:00
										 |  |  |     errors.push(...(await preprocessor.runCommands(mdSources, { | 
					
						
							| 
									
										
										
										
											2020-02-14 10:26:38 +08:00
										 |  |  |       libversion: VERSION, | 
					
						
							|  |  |  |       chromiumVersion: browserVersions.chromium, | 
					
						
							|  |  |  |       firefoxVersion: browserVersions.firefox, | 
					
						
							| 
									
										
										
										
											2020-12-22 10:09:55 +08:00
										 |  |  |       webkitVersion: browserVersions.webkit, | 
					
						
							| 
									
										
										
										
											2020-02-14 10:26:38 +08:00
										 |  |  |     }))); | 
					
						
							| 
									
										
										
										
											2020-07-23 02:03:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  |     errors.push(...preprocessor.findInvalidLinks(PROJECT_DIR, mdSources, getRepositoryFiles())); | 
					
						
							| 
									
										
										
										
											2020-12-05 04:19:48 +08:00
										 |  |  |     const jsSources = await Source.readdir(path.join(PROJECT_DIR, 'src', 'client'), '', []); | 
					
						
							| 
									
										
										
										
											2020-12-29 09:38:00 +08:00
										 |  |  |     errors.push(...missingDocs(outline, jsSources, path.join(PROJECT_DIR, 'src', 'client', 'api.ts'))); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const source of mdSources) { | 
					
						
							|  |  |  |       if (!source.hasUpdatedText()) | 
					
						
							|  |  |  |         continue; | 
					
						
							|  |  |  |       await source.save(); | 
					
						
							|  |  |  |       changedFiles = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Report results.
 | 
					
						
							|  |  |  |   if (errors.length) { | 
					
						
							|  |  |  |     for (let i = 0; i < errors.length; ++i) { | 
					
						
							| 
									
										
										
										
											2020-12-29 08:19:28 +08:00
										 |  |  |       const error = errors[i].split('\n').join('\n      '); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       console.log(`  ${i + 1}) ${RED_COLOR}${error}${RESET_COLOR}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-12-29 08:19:28 +08:00
										 |  |  |   let clearExit = errors.length === 0; | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |   if (changedFiles) { | 
					
						
							|  |  |  |     if (clearExit) | 
					
						
							|  |  |  |       console.log(`${YELLOW_COLOR}Some files were updated.${RESET_COLOR}`); | 
					
						
							|  |  |  |     clearExit = false; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-12-29 08:19:28 +08:00
										 |  |  |   console.log(`${errors.length} failures.`); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |   const runningTime = Date.now() - startTime; | 
					
						
							|  |  |  |   console.log(`DocLint Finished in ${runningTime / 1000} seconds`); | 
					
						
							| 
									
										
										
										
											2020-12-05 04:19:48 +08:00
										 |  |  |   process.exit(clearExit ? 0 : 1); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-14 10:26:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-13 05:03:01 +08:00
										 |  |  | async function getBrowserVersions() { | 
					
						
							| 
									
										
										
										
											2020-12-22 10:09:55 +08:00
										 |  |  |   const names = ['chromium', 'firefox', 'webkit']; | 
					
						
							|  |  |  |   const browsers = await Promise.all(names.map(name => playwright[name].launch())); | 
					
						
							|  |  |  |   const result = {}; | 
					
						
							|  |  |  |   for (let i = 0; i < names.length; i++) { | 
					
						
							|  |  |  |     result[names[i]] = browsers[i].version(); | 
					
						
							| 
									
										
										
										
											2020-03-13 05:03:01 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-12-22 10:09:55 +08:00
										 |  |  |   await Promise.all(browsers.map(browser => browser.close())); | 
					
						
							|  |  |  |   return result; | 
					
						
							| 
									
										
										
										
											2020-03-13 05:03:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-24 10:52:06 +08:00
										 |  |  | function getRepositoryFiles() { | 
					
						
							|  |  |  |   const out = spawnSync('git', ['ls-files'], {cwd: PROJECT_DIR}); | 
					
						
							| 
									
										
										
										
											2020-12-05 03:09:20 +08:00
										 |  |  |   const files = out.stdout.toString().trim().split('\n').filter(f => !f.startsWith('docs-src')); | 
					
						
							|  |  |  |   return files.map(file => path.join(PROJECT_DIR, file)); | 
					
						
							| 
									
										
										
										
											2020-04-24 10:52:06 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-30 09:35:01 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  |  * @param {string} file | 
					
						
							| 
									
										
										
										
											2020-12-30 09:35:01 +08:00
										 |  |  |  * @param {string} text | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  | function createLink(file, text) { | 
					
						
							|  |  |  |   const key = file + '#' + text; | 
					
						
							|  |  |  |   if (links.has(key)) | 
					
						
							|  |  |  |     return links.get(key); | 
					
						
							|  |  |  |   const baseLink = file + '#' + text.toLowerCase().split(',').map(c => c.replace(/[^a-z]/g, '')).join('-'); | 
					
						
							|  |  |  |   let link = baseLink; | 
					
						
							| 
									
										
										
										
											2020-12-30 09:35:01 +08:00
										 |  |  |   let index = 0; | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  |   while (rLinks.has(link)) | 
					
						
							|  |  |  |     link = baseLink + '-' + (++index); | 
					
						
							|  |  |  |   const result = `[${text}](${link})`; | 
					
						
							|  |  |  |   links.set(key, result); | 
					
						
							|  |  |  |   rLinks.set(link, text); | 
					
						
							| 
									
										
										
										
											2020-12-30 09:35:01 +08:00
										 |  |  |   return result; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {Documentation.Member} member | 
					
						
							|  |  |  |  * @return {string} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function createMemberLink(member) { | 
					
						
							|  |  |  |   const file = `./api.md`; | 
					
						
							|  |  |  |   if (member.kind === 'property') | 
					
						
							|  |  |  |     return createLink(file, `${member.clazz.varName}.${member.name}`); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (member.kind === 'event') | 
					
						
							|  |  |  |     return createLink(file, `${member.clazz.varName}.on('${member.name}')`); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (member.kind === 'method') | 
					
						
							|  |  |  |     return createLink(file, `${member.clazz.varName}.${member.name}(${member.signature})`); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-30 09:35:01 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {Documentation.Class} clazz | 
					
						
							|  |  |  |  * @return {MarkdownNode[]} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function generateToc(clazz) { | 
					
						
							|  |  |  |   /** @type {MarkdownNode[]} */ | 
					
						
							|  |  |  |   const result = []; | 
					
						
							|  |  |  |   for (const member of clazz.membersArray) { | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  |     result.push({ | 
					
						
							|  |  |  |       type: 'li', | 
					
						
							|  |  |  |       liType: 'default', | 
					
						
							|  |  |  |       text: createMemberLink(member) | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-12-30 09:35:01 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {string} name | 
					
						
							|  |  |  |  * @param {Type} type | 
					
						
							|  |  |  |  * @param {MarkdownNode[]} [spec] | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function renderProperty(name, type, spec) { | 
					
						
							|  |  |  |   let comment = ''; | 
					
						
							|  |  |  |   if (spec && spec.length) | 
					
						
							|  |  |  |     comment = spec[0].text; | 
					
						
							|  |  |  |   let children; | 
					
						
							|  |  |  |   if (type.properties && type.properties.length) | 
					
						
							|  |  |  |     children = type.properties.map(p => renderProperty(`\`${p.name}\``, p.type, p.spec)) | 
					
						
							|  |  |  |   else if (spec && spec.length > 1) | 
					
						
							| 
									
										
										
										
											2020-12-29 09:38:00 +08:00
										 |  |  |     children = spec.slice(1).map(s => md.clone(s)); | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 09:38:00 +08:00
										 |  |  |   /** @type {MarkdownNode} */ | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |   const result = { | 
					
						
							|  |  |  |     type: 'li', | 
					
						
							|  |  |  |     liType: 'default', | 
					
						
							|  |  |  |     text: `${name} <${renderType(type.name)}>${comment ? ' ' + comment : ''}`, | 
					
						
							|  |  |  |     children | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param {string} type | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function renderType(type) { | 
					
						
							|  |  |  |   if (type.includes('"')) | 
					
						
							|  |  |  |     return type.replace(/,/g, '|').replace(/Array/, "[Array]").replace(/null/, "[null]").replace(/number/, "[number]"); | 
					
						
							|  |  |  |   const result = type.replace(/([\w]+)/g, '[$1]'); | 
					
						
							|  |  |  |   if (result === '[Promise]<[void]>') | 
					
						
							|  |  |  |     return '[Promise]'; | 
					
						
							|  |  |  |   return result.replace(/[(]/g, '\\(').replace(/[)]/g, '\\)'); | 
					
						
							|  |  |  | } |