|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const yaml = require('js-yaml'); |
| 4 | + |
| 5 | +// JSDoc comments on these are either nested under another item or |
| 6 | +// excluded from the generated docs, so they do not need toc entries. |
| 7 | +const skippedTags = /@(private|internal|ignore|memberof)\b/; |
| 8 | + |
| 9 | +module.exports = { |
| 10 | + meta: { |
| 11 | + type: 'suggestion', |
| 12 | + docs: { |
| 13 | + description: |
| 14 | + 'Ensure JSDoc documented top level items are listed in the toc ' + |
| 15 | + 'of documentation.yml. Otherwise they render at an arbitrary ' + |
| 16 | + 'spot at the end of the generated docs.' |
| 17 | + }, |
| 18 | + schema: [] |
| 19 | + }, |
| 20 | + |
| 21 | + create(context) { |
| 22 | + const config = findTocConfig(path.dirname(context.getFilename())); |
| 23 | + |
| 24 | + if (!config) { |
| 25 | + return {}; |
| 26 | + } |
| 27 | + |
| 28 | + const sourceCode = context.getSourceCode(); |
| 29 | + |
| 30 | + return { |
| 31 | + Program(program) { |
| 32 | + program.body.forEach(statement => { |
| 33 | + const comment = jsdocCommentBefore(statement); |
| 34 | + |
| 35 | + if (!comment || skippedTags.test(comment.value)) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const name = documentedName(comment, statement); |
| 40 | + |
| 41 | + if (name && !config.names.has(name)) { |
| 42 | + context.report({ |
| 43 | + loc: comment.loc, |
| 44 | + message: |
| 45 | + `'${name}' has a JSDoc comment, but is not listed in the ` + |
| 46 | + `toc in ${config.relativePath}. Add it to the section ` + |
| 47 | + 'where it should show up in the generated docs.' |
| 48 | + }); |
| 49 | + } |
| 50 | + }); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + function jsdocCommentBefore(statement) { |
| 55 | + const comments = sourceCode.getCommentsBefore(statement); |
| 56 | + const comment = comments[comments.length - 1]; |
| 57 | + |
| 58 | + return comment && |
| 59 | + comment.type === 'Block' && |
| 60 | + comment.value.startsWith('*') ? comment : null; |
| 61 | + } |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +function documentedName(comment, statement) { |
| 66 | + const tagMatch = comment.value.match(/@(?:name|alias)\s+([\w.#]+)/); |
| 67 | + |
| 68 | + if (tagMatch) { |
| 69 | + return tagMatch[1]; |
| 70 | + } |
| 71 | + |
| 72 | + return declaredName(statement); |
| 73 | +} |
| 74 | + |
| 75 | +function declaredName(statement) { |
| 76 | + switch (statement.type) { |
| 77 | + case 'ExportNamedDeclaration': |
| 78 | + case 'ExportDefaultDeclaration': |
| 79 | + return statement.declaration && declaredName(statement.declaration); |
| 80 | + case 'FunctionDeclaration': |
| 81 | + case 'ClassDeclaration': |
| 82 | + return statement.id && statement.id.name; |
| 83 | + case 'VariableDeclaration': |
| 84 | + return statement.declarations[0].id.type === 'Identifier' ? |
| 85 | + statement.declarations[0].id.name : null; |
| 86 | + default: |
| 87 | + return null; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +const configCache = new Map(); |
| 92 | + |
| 93 | +function findTocConfig(directory) { |
| 94 | + if (configCache.has(directory)) { |
| 95 | + return configCache.get(directory); |
| 96 | + } |
| 97 | + |
| 98 | + const configPath = path.join(directory, 'documentation.yml'); |
| 99 | + let config; |
| 100 | + |
| 101 | + if (fs.existsSync(configPath)) { |
| 102 | + config = loadTocConfig(configPath); |
| 103 | + } |
| 104 | + else { |
| 105 | + const parent = path.dirname(directory); |
| 106 | + config = parent === directory ? null : findTocConfig(parent); |
| 107 | + } |
| 108 | + |
| 109 | + configCache.set(directory, config); |
| 110 | + return config; |
| 111 | +} |
| 112 | + |
| 113 | +function loadTocConfig(configPath) { |
| 114 | + const toc = yaml.safeLoad(fs.readFileSync(configPath, 'utf8')).toc || []; |
| 115 | + const names = new Set(); |
| 116 | + |
| 117 | + toc.forEach(section => |
| 118 | + (section.children || []).forEach(child => names.add(child)) |
| 119 | + ); |
| 120 | + |
| 121 | + return { |
| 122 | + names, |
| 123 | + relativePath: path.relative(process.cwd(), configPath) |
| 124 | + }; |
| 125 | +} |
0 commit comments