MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / componentsIn

Function componentsIn

src/resolution/angular-template-synthesizer.ts:80–128  ·  view source on GitHub ↗

Every `@Component` class a file declares, with its selectors and template.

(file: string, content: string, nodes: readonly Node[], ctx: ResolutionContext)

Source from the content-addressed store, hash-verified

78
79/** Every `@Component` class a file declares, with its selectors and template. */
80function componentsIn(file: string, content: string, nodes: readonly Node[], ctx: ResolutionContext): AngularComponent[] {
81 const out: AngularComponent[] = [];
82 const safe = stripCommentsForRegex(content, 'typescript');
83 const decorator = /@Component\s*\(/g;
84 let m: RegExpExecArray | null;
85 while ((m = decorator.exec(safe)) !== null) {
86 const paren = m.index + m[0].length - 1;
87 const close = matchBracket(safe, paren);
88 if (close < 0) continue;
89 decorator.lastIndex = close;
90 const open = safe.indexOf('{', paren);
91 if (open < 0 || open > close) continue;
92 const objEnd = matchBracket(safe, open);
93 if (objEnd < 0) continue;
94 const cls = /^\s*(?:export\s+)?(?:default\s+)?(?:abstract\s+)?class\s+([A-Za-z_$][\w$]*)/.exec(safe.slice(close + 1, close + 400));
95 if (!cls) continue;
96 const node = nodes.find((n) => n.kind === 'class' && n.name === cls[1]);
97 if (!node) continue;
98 const fields = readFields(safe, open, objEnd);
99 const selectorText = fields.get('selector')?.text;
100 const selector = selectorText ? staticString(selectorText) : null;
101 const selectors = (selector ?? '')
102 .split(',')
103 .map((s) => s.trim())
104 .filter((s) => ELEMENT_SELECTOR.test(s));
105
106 let template: AngularComponent['template'] = null;
107 const url = fields.get('templateUrl')?.text;
108 const templateUrl = url ? staticString(url) : null;
109 if (templateUrl) {
110 const templateFile = templatePathFor(file, templateUrl);
111 const text = ctx.readFile(templateFile);
112 if (text) template = { file: templateFile, text, firstLine: 1, inline: false };
113 } else {
114 const inline = fields.get('template');
115 if (inline) {
116 // The template string's body, from the original source (stripping
117 // comments must not touch a template's text), at its own offset.
118 const quoteAt = content.indexOf(inline.text.trim()[0] ?? '`', inline.at + 'template'.length);
119 const end = quoteAt < 0 ? -1 : skipString(content, quoteAt);
120 if (end > quoteAt) {
121 template = { file, text: content.slice(quoteAt + 1, end), firstLine: lineOfOffset(content, quoteAt + 1), inline: true };
122 }
123 }
124 }
125 out.push({ node, file, selectors, template });
126 }
127 return out;
128}
129
130/** Per context, renewed with the route list (whose identity changes when the resolver's caches do). */
131const componentIndexes = new WeakMap<ResolutionContext, { source: readonly Node[]; components: AngularComponent[] }>();

Callers 1

angularComponentsFunction · 0.85

Calls 12

stripCommentsForRegexFunction · 0.90
matchBracketFunction · 0.90
readFieldsFunction · 0.90
staticStringFunction · 0.90
templatePathForFunction · 0.90
skipStringFunction · 0.90
lineOfOffsetFunction · 0.85
mapMethod · 0.80
pushMethod · 0.80
execMethod · 0.65
getMethod · 0.65
readFileMethod · 0.65

Tested by

no test coverage detected