|
| 1 | +/** |
| 2 | + * JSDoc plugin that recovers and standardizes documentation of ECMAScript-next features: |
| 3 | + * - Native private methods (#method()) |
| 4 | + * - Native private properties (#property) |
| 5 | + * - Bound arrow functions (#method = () => {}) |
| 6 | + * It injects missing @function and @private tags where necessary and formats the resulting doclets consistently. |
| 7 | + * @author Frank Kudermann @ alphanull |
| 8 | + * @version 1.0.0 |
| 9 | + * @license MIT |
| 10 | + */ |
| 11 | +export const handlers = { |
| 12 | + |
| 13 | + /** |
| 14 | + * Triggered when a symbol (e.g. Method or field) is found during AST traversal. |
| 15 | + * Used to recover names for private class members and inline assignments to `this.#foo`. |
| 16 | + * @param {Object} e The event containing symbol data and AST node information. |
| 17 | + */ |
| 18 | + symbolFound(e) { |
| 19 | + |
| 20 | + // Phase 1: Detect native private methods defined via #method() syntax |
| 21 | + if (e.code?.type === 'MethodDefinition' |
| 22 | + && e.code?.node?.key?.type === 'PrivateName' |
| 23 | + && e.code?.node?.key?.id?.name) { |
| 24 | + |
| 25 | + e.code.name = e.code.node.key.id.name; |
| 26 | + } |
| 27 | + |
| 28 | + // Phase 2: Detect assignments to this.#property inside constructor or methods |
| 29 | + if (e.code?.name === 'this.' // <- smells like a jsdoc parse bug |
| 30 | + && e.astnode?.type === 'AssignmentExpression' |
| 31 | + && e.astnode?.left?.type === 'MemberExpression' |
| 32 | + && e.astnode?.left?.property?.type === 'PrivateName' |
| 33 | + && e.astnode?.left?.property?.id?.name) { |
| 34 | + |
| 35 | + e.code.name = e.astnode.left.property.id.name; |
| 36 | + } |
| 37 | + }, |
| 38 | + |
| 39 | + /** |
| 40 | + * Triggered after all doclets have been collected. Used to finalize name formatting, |
| 41 | + * unify access levels, and merge scattered information into clean private member entries. |
| 42 | + * @param {Object} context The parse context object. |
| 43 | + * @param {Array<Object>} context.doclets The list of all generated doclets. |
| 44 | + */ |
| 45 | + parseComplete({ doclets }) { |
| 46 | + |
| 47 | + const privatePropNames = new Set(); |
| 48 | + |
| 49 | + // Phase 1: Collect names of undocumented ClassPrivateProperty placeholders |
| 50 | + for (const doclet of doclets) { |
| 51 | + if (doclet.undocumented && doclet.meta?.code?.type === 'ClassPrivateProperty') { |
| 52 | + privatePropNames.add(doclet.name); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Phase 2: Apply corrections for all relevant doclets |
| 57 | + for (const doclet of doclets) { |
| 58 | + |
| 59 | + // Handle value-carrying assignment doclets related to private properties |
| 60 | + if (doclet.kind === 'member' |
| 61 | + && doclet.scope === 'inner' |
| 62 | + && typeof doclet.name === 'string' |
| 63 | + && privatePropNames.has(doclet.name)) { |
| 64 | + |
| 65 | + doclet.name = `#${doclet.name}`; |
| 66 | + doclet.longname = `${doclet.memberof}#${doclet.name}`; |
| 67 | + doclet.access = 'private'; |
| 68 | + doclet.scope = 'instance'; |
| 69 | + // Do NOT reset undocumented = false here, placeholder doclets must remain hidden |
| 70 | + } |
| 71 | + |
| 72 | + // correct arrow methods by fixing the kind |
| 73 | + |
| 74 | + const isArrowMethod = doclet.kind === 'member' |
| 75 | + && (doclet.meta?.code?.type === 'ArrowFunctionExpression' |
| 76 | + || doclet.meta?.code?.node?.value?.type === 'ArrowFunctionExpression'); |
| 77 | + |
| 78 | + if (isArrowMethod) { |
| 79 | + doclet.kind = 'function'; |
| 80 | + } |
| 81 | + |
| 82 | + const isPrivateMethod = doclet.meta?.code?.node?.type === 'MethodDefinition' |
| 83 | + && doclet.meta?.code?.node?.key?.type === 'PrivateName'; |
| 84 | + |
| 85 | + const isPrivateProperty = doclet.meta?.code?.type === 'ClassPrivateProperty' |
| 86 | + || doclet.meta?.code?.node?.type === 'PropertyDefinition' |
| 87 | + && doclet.meta?.code?.node?.key?.type === 'PrivateName'; |
| 88 | + |
| 89 | + if (isPrivateMethod || isPrivateProperty) { |
| 90 | + |
| 91 | + // Common logic for private methods and properties with recognizable AST nodes |
| 92 | + |
| 93 | + if (!doclet.name.startsWith('#')) { |
| 94 | + doclet.name = `#${doclet.name}`; |
| 95 | + } |
| 96 | + |
| 97 | + if (!doclet.longname || doclet.longname === doclet.memberof) { |
| 98 | + doclet.longname = `${doclet.memberof}#${doclet.name}`; |
| 99 | + } |
| 100 | + |
| 101 | + doclet.access = 'private'; |
| 102 | + } |
| 103 | + |
| 104 | + if (doclet.meta?.code?.node?.static === true) { |
| 105 | + doclet.scope = 'static'; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +}; |
0 commit comments