|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import ts from 'typescript'; |
| 10 | + |
| 11 | +import {extractInterface} from './class_extractor'; |
| 12 | +import {DecoratorEntry, DecoratorType, EntryType, PropertyEntry} from './entities'; |
| 13 | +import {extractJsDocDescription, extractJsDocTags, extractRawJsDoc} from './jsdoc_extractor'; |
| 14 | + |
| 15 | +/** Extracts an API documentation entry for an Angular decorator. */ |
| 16 | +export function extractorDecorator( |
| 17 | + declaration: ts.VariableDeclaration, typeChecker: ts.TypeChecker): DecoratorEntry { |
| 18 | + const documentedNode = getDecoratorJsDocNode(declaration); |
| 19 | + |
| 20 | + const decoratorType = getDecoratorType(declaration); |
| 21 | + if (!decoratorType) { |
| 22 | + throw new Error(`"${declaration.name.getText()} is not a decorator."`); |
| 23 | + } |
| 24 | + |
| 25 | + return { |
| 26 | + name: declaration.name.getText(), |
| 27 | + decoratorType: decoratorType, |
| 28 | + entryType: EntryType.Decorator, |
| 29 | + rawComment: extractRawJsDoc(documentedNode), |
| 30 | + description: extractJsDocDescription(documentedNode), |
| 31 | + jsdocTags: extractJsDocTags(documentedNode), |
| 32 | + options: getDecoratorOptions(declaration, typeChecker), |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +/** Gets whether the given variable declaration is an Angular decorator declaration. */ |
| 37 | +export function isDecoratorDeclaration(declaration: ts.VariableDeclaration): boolean { |
| 38 | + return !!getDecoratorType(declaration); |
| 39 | +} |
| 40 | + |
| 41 | +/** Gets whether an interface is the options interface for a decorator in the same file. */ |
| 42 | +export function isDecoratorOptionsInterface(declaration: ts.InterfaceDeclaration): boolean { |
| 43 | + return declaration.getSourceFile().statements.some( |
| 44 | + s => ts.isVariableStatement(s) && |
| 45 | + s.declarationList.declarations.some( |
| 46 | + d => isDecoratorDeclaration(d) && d.name.getText() === declaration.name.getText())); |
| 47 | +} |
| 48 | + |
| 49 | +/** Gets the type of decorator, or undefined if the declaration is not a decorator. */ |
| 50 | +function getDecoratorType(declaration: ts.VariableDeclaration): DecoratorType|undefined { |
| 51 | + // All Angular decorators are initialized with one of `makeDecorator`, `makePropDecorator`, |
| 52 | + // or `makeParamDecorator`. |
| 53 | + const initializer = declaration.initializer?.getFullText() ?? ''; |
| 54 | + if (initializer.includes('makeDecorator')) return DecoratorType.Class; |
| 55 | + if (initializer.includes('makePropDecorator')) return DecoratorType.Member; |
| 56 | + if (initializer.includes('makeParamDecorator')) return DecoratorType.Parameter; |
| 57 | + |
| 58 | + return undefined; |
| 59 | +} |
| 60 | + |
| 61 | +/** Gets the doc entry for the options object for an Angular decorator */ |
| 62 | +function getDecoratorOptions( |
| 63 | + declaration: ts.VariableDeclaration, typeChecker: ts.TypeChecker): PropertyEntry[] { |
| 64 | + const name = declaration.name.getText(); |
| 65 | + |
| 66 | + // Every decorator has an interface with its options in the same SourceFile. |
| 67 | + // Queries, however, are defined as a type alias pointing to an interface. |
| 68 | + const optionsDeclaration = declaration.getSourceFile().statements.find(node => { |
| 69 | + return (ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node)) && |
| 70 | + node.name.getText() === name; |
| 71 | + }); |
| 72 | + |
| 73 | + if (!optionsDeclaration) { |
| 74 | + throw new Error(`Decorator "${name}" has no corresponding options interface.`); |
| 75 | + } |
| 76 | + |
| 77 | + let optionsInterface: ts.InterfaceDeclaration; |
| 78 | + if (ts.isTypeAliasDeclaration(optionsDeclaration)) { |
| 79 | + // We hard-code the assumption that if the decorator's option type is a type alias, |
| 80 | + // it resolves to a single interface (this is true for all query decorators at time of |
| 81 | + // this writing). |
| 82 | + const aliasedType = typeChecker.getTypeAtLocation((optionsDeclaration.type)); |
| 83 | + optionsInterface = (aliasedType.getSymbol()?.getDeclarations() ?? |
| 84 | + []).find(d => ts.isInterfaceDeclaration(d)) as ts.InterfaceDeclaration; |
| 85 | + } else { |
| 86 | + optionsInterface = optionsDeclaration as ts.InterfaceDeclaration; |
| 87 | + } |
| 88 | + |
| 89 | + if (!optionsInterface || !ts.isInterfaceDeclaration(optionsInterface)) { |
| 90 | + throw new Error(`Options for decorator "${name}" is not an interface.`); |
| 91 | + } |
| 92 | + |
| 93 | + // Take advantage of the interface extractor to pull the appropriate member info. |
| 94 | + // Hard code the knowledge that decorator options only have properties, never methods. |
| 95 | + return extractInterface(optionsInterface, typeChecker).members as PropertyEntry[]; |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Gets the call signature node that has the decorator's public JsDoc block. |
| 100 | + * |
| 101 | + * Every decorator has three parts: |
| 102 | + * - A const that has the actual decorator. |
| 103 | + * - An interface with the same name as the const that documents the decorator's options. |
| 104 | + * - An interface suffixed with "Decorator" that has the decorator's call signature and JsDoc block. |
| 105 | + * |
| 106 | + * For the description and JsDoc tags, we need the interface suffixed with "Decorator". |
| 107 | + */ |
| 108 | +function getDecoratorJsDocNode(declaration: ts.VariableDeclaration): ts.HasJSDoc { |
| 109 | + const name = declaration.name.getText(); |
| 110 | + |
| 111 | + // Assume the existence of an interface in the same file with the same name |
| 112 | + // suffixed with "Decorator". |
| 113 | + const decoratorInterface = declaration.getSourceFile().statements.find(s => { |
| 114 | + return ts.isInterfaceDeclaration(s) && s.name.getText() === `${name}Decorator`; |
| 115 | + }); |
| 116 | + |
| 117 | + if (!decoratorInterface || !ts.isInterfaceDeclaration(decoratorInterface)) { |
| 118 | + throw new Error(`No interface "${name}Decorator" found.`); |
| 119 | + } |
| 120 | + |
| 121 | + // The public-facing JsDoc for each decorator is on one of its interface's call signatures. |
| 122 | + const callSignature = decoratorInterface.members.find(node => { |
| 123 | + // The description block lives on one of the call signatures for this interface. |
| 124 | + return ts.isCallSignatureDeclaration(node) && extractRawJsDoc(node); |
| 125 | + }); |
| 126 | + |
| 127 | + if (!callSignature || !ts.isCallSignatureDeclaration(callSignature)) { |
| 128 | + throw new Error(`No call signature with JsDoc on "${name}Decorator"`); |
| 129 | + } |
| 130 | + |
| 131 | + return callSignature; |
| 132 | +} |
0 commit comments