(classDoc: CategorizedClassDoc)
| 17 | * ``` |
| 18 | */ |
| 19 | export function getMetadata(classDoc: CategorizedClassDoc): Map<string, any> | null { |
| 20 | const declaration = classDoc.symbol.valueDeclaration; |
| 21 | const decorators = |
| 22 | declaration && ts.isClassDeclaration(declaration) ? ts.getDecorators(declaration) : null; |
| 23 | |
| 24 | if (!decorators?.length) { |
| 25 | return null; |
| 26 | } |
| 27 | |
| 28 | const expression = decorators |
| 29 | .filter(decorator => decorator.expression && ts.isCallExpression(decorator.expression)) |
| 30 | .map(decorator => decorator.expression as ts.CallExpression) |
| 31 | .find( |
| 32 | callExpression => |
| 33 | callExpression.expression.getText() === 'Component' || |
| 34 | callExpression.expression.getText() === 'Directive', |
| 35 | ); |
| 36 | |
| 37 | if (!expression) { |
| 38 | return null; |
| 39 | } |
| 40 | |
| 41 | // The argument length of the CallExpression needs to be exactly one, because it's the single |
| 42 | // JSON object in the @Component/@Directive decorator. |
| 43 | if (expression.arguments.length !== 1) { |
| 44 | return null; |
| 45 | } |
| 46 | |
| 47 | const objectExpression = expression.arguments[0] as ts.ObjectLiteralExpression; |
| 48 | const resultMetadata = new Map<string, any>(); |
| 49 | |
| 50 | (objectExpression.properties as ts.NodeArray<ts.PropertyAssignment>).forEach(prop => { |
| 51 | // Support ArrayLiteralExpression assignments in the directive metadata. |
| 52 | if (ts.isArrayLiteralExpression(prop.initializer)) { |
| 53 | const arrayData = prop.initializer.elements.map(literal => { |
| 54 | if (ts.isStringLiteralLike(literal)) { |
| 55 | return literal.text; |
| 56 | } |
| 57 | |
| 58 | if (ts.isObjectLiteralExpression(literal)) { |
| 59 | return literal.properties.reduce( |
| 60 | (result, prop) => { |
| 61 | if (ts.isPropertyAssignment(prop)) { |
| 62 | result[prop.name.getText()] = ts.isStringLiteralLike(prop.initializer) |
| 63 | ? prop.initializer.text |
| 64 | : prop.initializer.getText(); |
| 65 | } |
| 66 | |
| 67 | return result; |
| 68 | }, |
| 69 | {} as Record<string, string>, |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | return literal.getText(); |
| 74 | }); |
| 75 | |
| 76 | resultMetadata.set(prop.name.getText(), arrayData); |
no test coverage detected
searching dependent graphs…