|
|
@@ -3,6 +3,29 @@ import { generateNodeId } from './tree-sitter-helpers';
|
|
|
import { TreeSitterExtractor } from './tree-sitter';
|
|
|
import { isLanguageSupported } from './grammars';
|
|
|
|
|
|
+/**
|
|
|
+ * Vue built-in components — skipped so a `<Transition>` / `<KeepAlive>` in the
|
|
|
+ * template doesn't become a phantom reference to a user component. Checked
|
|
|
+ * AFTER kebab→Pascal conversion, so `<keep-alive>` is caught here too.
|
|
|
+ */
|
|
|
+const VUE_BUILTIN_COMPONENTS = new Set([
|
|
|
+ 'Transition',
|
|
|
+ 'TransitionGroup',
|
|
|
+ 'KeepAlive',
|
|
|
+ 'Suspense',
|
|
|
+ 'Teleport',
|
|
|
+ 'Component',
|
|
|
+ 'Slot',
|
|
|
+]);
|
|
|
+
|
|
|
+/** `my-component` → `MyComponent` (Vue allows either form in templates). */
|
|
|
+function kebabToPascal(name: string): string {
|
|
|
+ return name
|
|
|
+ .split('-')
|
|
|
+ .map((part) => (part ? part[0]!.toUpperCase() + part.slice(1) : ''))
|
|
|
+ .join('');
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* VueExtractor - Extracts code relationships from Vue Single-File Component files
|
|
|
*
|
|
|
@@ -41,6 +64,12 @@ export class VueExtractor {
|
|
|
for (const block of scriptBlocks) {
|
|
|
this.processScriptBlock(block, componentNode.id);
|
|
|
}
|
|
|
+
|
|
|
+ // Extract component usages from the <template> (<ComponentName>).
|
|
|
+ // Without this, a Vue component used only in another component's
|
|
|
+ // markup (incl. through a barrel import) is invisible to callers /
|
|
|
+ // impact (#629 follow-up).
|
|
|
+ this.extractTemplateComponents(componentNode.id);
|
|
|
} catch (error) {
|
|
|
this.errors.push({
|
|
|
message: `Vue extraction error: ${error instanceof Error ? error.message : String(error)}`,
|
|
|
@@ -195,4 +224,67 @@ export class VueExtractor {
|
|
|
this.errors.push(error);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Extract component usages from the Vue `<template>`.
|
|
|
+ *
|
|
|
+ * PascalCase tags (`<Modal>`, `<Button />`) and kebab-case tags
|
|
|
+ * (`<my-button>`) both represent component instantiations — analogous to
|
|
|
+ * function calls in imperative code. Capturing them creates parent→child
|
|
|
+ * component edges and lets `callers` / `impact` see a component that is
|
|
|
+ * only ever used in markup. Vue's extractor previously parsed only the
|
|
|
+ * `<script>` block, so these usages produced no edge at all (#629).
|
|
|
+ *
|
|
|
+ * HTML elements (lowercase, no hyphen) and Vue built-ins are skipped.
|
|
|
+ * Unmatched names create no edge during resolution, so converting
|
|
|
+ * kebab-case is safe even for native custom elements.
|
|
|
+ */
|
|
|
+ private extractTemplateComponents(componentNodeId: string): void {
|
|
|
+ // Ranges covered by <script> / <style> blocks — skip them so script
|
|
|
+ // identifiers and CSS selectors aren't mistaken for template tags. This
|
|
|
+ // also correctly handles nested <template> tags (v-if / slots), which a
|
|
|
+ // single non-greedy <template>…</template> match would mis-bound.
|
|
|
+ const coveredRanges: Array<[number, number]> = [];
|
|
|
+ const blockRegex = /<(script|style)(\s[^>]*)?>[\s\S]*?<\/\1>/g;
|
|
|
+ let blockMatch;
|
|
|
+ while ((blockMatch = blockRegex.exec(this.source)) !== null) {
|
|
|
+ const startLine = (this.source.substring(0, blockMatch.index).match(/\n/g) || []).length;
|
|
|
+ const endLine = startLine + (blockMatch[0].match(/\n/g) || []).length;
|
|
|
+ coveredRanges.push([startLine, endLine]);
|
|
|
+ }
|
|
|
+
|
|
|
+ const lines = this.source.split('\n');
|
|
|
+ // Opening / self-closing tags (closing `</Foo>` starts with `</`, so the
|
|
|
+ // leading `<` followed by a name letter won't match it).
|
|
|
+ const tagRegex = /<([A-Za-z][A-Za-z0-9_-]*)\b/g;
|
|
|
+
|
|
|
+ for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) {
|
|
|
+ if (coveredRanges.some(([start, end]) => lineIdx >= start && lineIdx <= end)) continue;
|
|
|
+
|
|
|
+ const line = lines[lineIdx]!;
|
|
|
+ let match;
|
|
|
+ while ((match = tagRegex.exec(line)) !== null) {
|
|
|
+ const raw = match[1]!;
|
|
|
+ let componentName: string;
|
|
|
+ if (/^[A-Z]/.test(raw)) {
|
|
|
+ componentName = raw; // PascalCase component
|
|
|
+ } else if (raw.includes('-')) {
|
|
|
+ componentName = kebabToPascal(raw); // kebab-case component
|
|
|
+ } else {
|
|
|
+ continue; // lowercase, no hyphen → native HTML element
|
|
|
+ }
|
|
|
+ if (VUE_BUILTIN_COMPONENTS.has(componentName)) continue;
|
|
|
+
|
|
|
+ this.unresolvedReferences.push({
|
|
|
+ fromNodeId: componentNodeId,
|
|
|
+ referenceName: componentName,
|
|
|
+ referenceKind: 'references',
|
|
|
+ line: lineIdx + 1, // 1-indexed
|
|
|
+ column: match.index + 1,
|
|
|
+ filePath: this.filePath,
|
|
|
+ language: 'vue',
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|