Every `@Component` class a file declares, with its selectors and template.
(file: string, content: string, nodes: readonly Node[], ctx: ResolutionContext)
| 78 | |
| 79 | /** Every `@Component` class a file declares, with its selectors and template. */ |
| 80 | function 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). */ |
| 131 | const componentIndexes = new WeakMap<ResolutionContext, { source: readonly Node[]; components: AngularComponent[] }>(); |
no test coverage detected