|
| 1 | +module TypeDoc.Factories |
| 2 | +{ |
| 3 | + /** |
| 4 | + * Structure used by [[ContainerCommentHandler]] to store discovered module comments. |
| 5 | + */ |
| 6 | + interface IModuleComment |
| 7 | + { |
| 8 | + /** |
| 9 | + * The module reflection this comment is targeting. |
| 10 | + */ |
| 11 | + reflection:Models.DeclarationReflection; |
| 12 | + |
| 13 | + /** |
| 14 | + * The full text of the best matched comment. |
| 15 | + */ |
| 16 | + fullText:string; |
| 17 | + |
| 18 | + /** |
| 19 | + * Has the full text been marked as being preferred? |
| 20 | + */ |
| 21 | + isPreferred:boolean; |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | + /** |
| 26 | + * A handler that extracts comments of containers like modules. |
| 27 | + * |
| 28 | + * The [[CommentHandler]] only extracts comments directly attached to the current |
| 29 | + * declaration, while this handler looks up the comments of the parent ast of the given |
| 30 | + * declaration if it is some container. As modules might be defined multiple times, |
| 31 | + * this handler stores the found comments and applies them in the resolving phase. |
| 32 | + * |
| 33 | + * If multiple comments for the same module are found, the longest comment will be preferred. |
| 34 | + * One may explicitly set the preferred module comment by appending the tag `@preferred`. |
| 35 | + */ |
| 36 | + export class ModuleCommentHandler extends BaseHandler |
| 37 | + { |
| 38 | + /** |
| 39 | + * The ast walker factory. |
| 40 | + */ |
| 41 | + private factory:TypeScript.AstWalkerFactory; |
| 42 | + |
| 43 | + /** |
| 44 | + * List of discovered module comments. |
| 45 | + */ |
| 46 | + private comments:{[id:number]:IModuleComment}; |
| 47 | + |
| 48 | + |
| 49 | + /** |
| 50 | + * Create a new ModuleCommentHandler instance. |
| 51 | + * |
| 52 | + * @param dispatcher The dispatcher this handler should be attached to. |
| 53 | + */ |
| 54 | + constructor(dispatcher:Dispatcher) { |
| 55 | + super(dispatcher); |
| 56 | + |
| 57 | + this.factory = TypeScript.getAstWalkerFactory(); |
| 58 | + |
| 59 | + dispatcher.on(Dispatcher.EVENT_BEGIN, this.onBegin, this); |
| 60 | + dispatcher.on(Dispatcher.EVENT_DECLARATION, this.onDeclaration, this); |
| 61 | + dispatcher.on(Dispatcher.EVENT_BEGIN_RESOLVE, this.onBeginResolve, this); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + /** |
| 66 | + * Triggered once per project before the dispatcher invokes the compiler. |
| 67 | + * |
| 68 | + * @param event An event object containing the related project and compiler instance. |
| 69 | + */ |
| 70 | + private onBegin(event:DispatcherEvent) { |
| 71 | + this.comments = {}; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + /** |
| 76 | + * Triggered when the dispatcher processes a declaration. |
| 77 | + * |
| 78 | + * @param state The state that describes the current declaration and reflection. |
| 79 | + */ |
| 80 | + private onDeclaration(state:DeclarationState) { |
| 81 | + if (!state.kindOf(TypeScript.PullElementKind.Container)) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + var ast = state.declaration.ast(); |
| 86 | + ast = ast.parent; |
| 87 | + if (ast && ast.kind() == TypeScript.SyntaxKind.QualifiedName) { |
| 88 | + var identifiers = []; |
| 89 | + this.factory.simpleWalk(ast, (ast:TypeScript.AST, astState:any) => { |
| 90 | + if (ast.kind() == TypeScript.SyntaxKind.IdentifierName) { |
| 91 | + identifiers.push(ast); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + if (identifiers.indexOf(state.declaration.ast()) < identifiers.length - 1) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + while (ast && ast.kind() == TypeScript.SyntaxKind.QualifiedName) { |
| 100 | + ast = ast.parent; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (!ast || ast.kind() != TypeScript.SyntaxKind.ModuleDeclaration) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + var comments = ast.preComments(); |
| 109 | + if (!comments || comments.length == 0) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + var comment = comments[comments.length -1]; |
| 114 | + if (!CommentHandler.isDocComment(comment)) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + var fullText = comment.fullText(); |
| 119 | + var isPreferred = (fullText.toLowerCase().indexOf('@preferred') != -1); |
| 120 | + |
| 121 | + if (this.comments[state.reflection.id]) { |
| 122 | + var info = this.comments[state.reflection.id]; |
| 123 | + if (!isPreferred && (info.isPreferred || info.fullText.length > fullText.length)) { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + info.fullText = fullText; |
| 128 | + info.isPreferred = isPreferred; |
| 129 | + } else { |
| 130 | + this.comments[state.reflection.id] = { |
| 131 | + reflection: state.reflection, |
| 132 | + fullText: fullText, |
| 133 | + isPreferred: isPreferred |
| 134 | + }; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + /** |
| 140 | + * Triggered when the dispatcher enters the resolving phase. |
| 141 | + * |
| 142 | + * @param event An event object containing the related project and compiler instance. |
| 143 | + */ |
| 144 | + private onBeginResolve(event:DispatcherEvent) { |
| 145 | + for (var id in this.comments) { |
| 146 | + if (!this.comments.hasOwnProperty(id)) { |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + var info = this.comments[id]; |
| 151 | + var comment = CommentHandler.parseDocComment(info.fullText); |
| 152 | + CommentHandler.removeTags(comment, 'preferred'); |
| 153 | + |
| 154 | + info.reflection.comment = comment; |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + /** |
| 161 | + * Register this handler. |
| 162 | + */ |
| 163 | + Dispatcher.HANDLERS.push(ModuleCommentHandler); |
| 164 | +} |
0 commit comments