|
1 | 1 | import { Project, SyntaxKind, Node, SourceFile, CallExpression, ObjectLiteralExpression, JsxSelfClosingElement, JsxElement, Identifier, Symbol } from 'ts-morph'; |
| 2 | +import { isComponentFile, isComponentType } from '../common/utils'; |
2 | 3 |
|
3 | 4 | interface TestUnit { |
4 | 5 | props?: string[]; |
@@ -89,12 +90,33 @@ class TestUnitAnalyzer { |
89 | 90 | if (!declarationNode) return null; |
90 | 91 | const declarationSourceFile = declarationNode.getSourceFile(); |
91 | 92 | const originalPath = declarationSourceFile.getFilePath(); |
| 93 | + if (!isComponentFile(originalPath)) { |
| 94 | + return this.resolveTsPath(declarationNode); |
| 95 | + } |
92 | 96 | return originalPath; |
93 | 97 | } catch (error) { |
94 | 98 | return null; |
95 | 99 | } |
96 | 100 | } |
97 | 101 |
|
| 102 | + // 解析ts路径 |
| 103 | + resolveTsPath(declarationNode: Node) { |
| 104 | + if (!Node.isExportAssignment(declarationNode)) return null; |
| 105 | + const exportedExpression = declarationNode.getExpression(); |
| 106 | + if (Node.isCallExpression(exportedExpression)) { |
| 107 | + const args = exportedExpression.getArguments(); |
| 108 | + for (const arg of args) { |
| 109 | + const argType = arg.getType(); |
| 110 | + if (isComponentType(argType)) { |
| 111 | + // 获取文件路径 |
| 112 | + const res = this.resolveComponentPath(arg as Identifier) as string; |
| 113 | + return res; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
98 | 120 |
|
99 | 121 | // 分析传统挂载mount/shallowMount方法调用 |
100 | 122 | private analyzeTraditionalMountCalls(testCall: CallExpression) { |
@@ -545,6 +567,23 @@ class TestUnitAnalyzer { |
545 | 567 | if (!component.emits.includes(propName)) { |
546 | 568 | component.emits.push(propName); |
547 | 569 | } |
| 570 | + } else if (propName === 'v-slots') { |
| 571 | + // Handle v-slots directive |
| 572 | + const initializer = attr.getInitializer(); |
| 573 | + if (initializer && Node.isJsxExpression(initializer)) { |
| 574 | + const expression = initializer.getExpression(); |
| 575 | + if (expression && Node.isObjectLiteralExpression(expression)) { |
| 576 | + component.slots = component.slots || []; |
| 577 | + expression.getProperties().forEach(prop => { |
| 578 | + if (Node.isPropertyAssignment(prop) || Node.isShorthandPropertyAssignment(prop)) { |
| 579 | + const slotName = prop.getName(); |
| 580 | + if (slotName && !component.slots!.includes(slotName)) { |
| 581 | + component.slots!.push(slotName); |
| 582 | + } |
| 583 | + } |
| 584 | + }); |
| 585 | + } |
| 586 | + } |
548 | 587 | } else { |
549 | 588 | let isVModel = false; |
550 | 589 | // Handle v-model transformation for props |
|
0 commit comments