MCPcopy
hub / github.com/angular/components / parseExampleFile

Function parseExampleFile

tools/example-module/parse-example-file.ts:18–95  ·  view source on GitHub ↗
(fileName: string, content: string)

Source from the content-addressed store, hash-verified

16
17/** Parse the AST of the given source file and collect Angular component metadata. */
18export function parseExampleFile(fileName: string, content: string): ParsedMetadataResults {
19 const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, false);
20 const metas: ParsedMetadata[] = [];
21
22 const visitNode = (node: any): void => {
23 if (node.kind === ts.SyntaxKind.ClassDeclaration) {
24 const decorators = ts.getDecorators(node);
25 const meta = {
26 componentName: node.name.text,
27 } as ParsedMetadata;
28
29 if (node.jsDoc && node.jsDoc.length) {
30 for (const doc of node.jsDoc) {
31 if (doc.tags && doc.tags.length) {
32 for (const tag of doc.tags) {
33 const tagValue = tag.comment;
34 const tagName = tag.tagName.text;
35 if (tagName === 'title') {
36 meta.title = tagValue;
37 meta.isPrimary = true;
38 }
39 }
40 }
41 }
42 }
43
44 if (decorators && decorators.length) {
45 for (const decorator of decorators) {
46 const call = decorator.expression;
47
48 if (
49 !ts.isCallExpression(call) ||
50 !ts.isIdentifier(call.expression) ||
51 call.expression.text !== 'Component' ||
52 call.arguments.length === 0 ||
53 !ts.isObjectLiteralExpression(call.arguments[0])
54 ) {
55 continue;
56 }
57
58 for (const prop of call.arguments[0].properties) {
59 if (!ts.isPropertyAssignment(prop) || !prop.name || !ts.isIdentifier(prop.name)) {
60 continue;
61 }
62
63 const propName = prop.name.text;
64
65 // Since additional files can be also stylesheets, we need to properly parse
66 // the styleUrls metadata property.
67 if (propName === 'styleUrls' && ts.isArrayLiteralExpression(prop.initializer)) {
68 meta[propName] = prop.initializer.elements.map(
69 literal => (literal as ts.StringLiteralLike).text,
70 );
71 } else if (propName === 'styleUrl' && ts.isStringLiteralLike(prop.initializer)) {
72 meta.styleUrls = [prop.initializer.text];
73 } else if (
74 ts.isStringLiteralLike(prop.initializer) ||
75 ts.isIdentifier(prop.initializer)

Callers 1

analyzeExamplesFunction · 0.90

Calls 2

visitNodeFunction · 0.70
filterMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…