-
-
Notifications
You must be signed in to change notification settings - Fork 597
/
Copy pathfindComponents.ts
163 lines (150 loc) · 4.95 KB
/
findComponents.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as ts from 'typescript';
import { Definition, Range } from 'vscode-languageserver-types';
import Uri from 'vscode-uri';
export interface PropInfo {
name: string;
doc?: string;
}
export interface ComponentInfo {
name: string;
definition?: Definition;
props?: PropInfo[];
}
export function findComponents(service: ts.LanguageService, fileFsPath: string): ComponentInfo[] {
const program = service.getProgram();
const sourceFile = program.getSourceFile(fileFsPath);
const exportStmt = sourceFile!.statements.filter(st => st.kind === ts.SyntaxKind.ExportAssignment);
if (exportStmt.length === 0) {
return [];
}
const exportExpr = (exportStmt[0] as ts.ExportAssignment).expression;
const comp = getComponentFromExport(exportExpr);
if (!comp) {
return [];
}
const checker = program.getTypeChecker();
const compType = checker.getTypeAtLocation(comp);
const childComps = getPropertyTypeOfType(compType, 'components', checker);
if (!childComps) {
return [];
}
return checker.getPropertiesOfType(childComps).map(s => getCompInfo(s, checker));
}
function getComponentFromExport(exportExpr: ts.Expression) {
switch (exportExpr.kind) {
case ts.SyntaxKind.CallExpression:
// Vue.extend or synthetic __vueEditorBridge
return (exportExpr as ts.CallExpression).arguments[0];
case ts.SyntaxKind.ObjectLiteralExpression:
return exportExpr;
}
return undefined;
}
// Vue.extend will return a type without `props`. We need to find the object literal
function findDefinitionLiteralSymbol(symbol: ts.Symbol, checker: ts.TypeChecker) {
const node = symbol.valueDeclaration;
if (!node) {
return undefined;
}
if (node.kind === ts.SyntaxKind.PropertyAssignment) {
// {comp: importedComponent}
symbol = checker.getSymbolAtLocation((node as ts.PropertyAssignment).initializer) || symbol;
} else if (node.kind === ts.SyntaxKind.ShorthandPropertyAssignment) {
// {comp}
symbol = checker.getShorthandAssignmentValueSymbol(node) || symbol;
}
if (symbol.flags & ts.SymbolFlags.Alias) {
// resolve import Comp from './comp.vue'
symbol = checker.getAliasedSymbol(symbol);
}
return symbol;
}
function getCompInfo(symbol: ts.Symbol, checker: ts.TypeChecker) {
const info: ComponentInfo = {
name: hyphenate(symbol.name)
};
const literalSymbol = findDefinitionLiteralSymbol(symbol, checker);
if (!literalSymbol) {
return info;
}
const declaration = literalSymbol.valueDeclaration;
if (!declaration) {
return info;
}
info.definition = [
{
uri: Uri.file(declaration.getSourceFile().fileName).toString(),
range: Range.create(0, 0, 0, 0)
}
];
let node: ts.Node = declaration;
if (declaration.kind === ts.SyntaxKind.ExportAssignment) {
const expr = (declaration as ts.ExportAssignment).expression;
node = getComponentFromExport(expr) || declaration;
}
const compType = checker.getTypeAtLocation(node);
const arrayProps = getArrayProps(compType, checker);
if (arrayProps) {
info.props = arrayProps;
return info;
}
const props = getPropertyTypeOfType(compType, 'props', checker);
if (!props) {
return info;
}
info.props = checker.getPropertiesOfType(props).map(s => {
return {
name: hyphenate(s.name),
doc: getPropTypeDeclaration(s, checker)
};
});
return info;
}
function getPropTypeDeclaration(prop: ts.Symbol, checker: ts.TypeChecker) {
if (!prop.valueDeclaration) {
return '';
}
const declaration = prop.valueDeclaration.getChildAt(2);
if (!declaration) {
return '';
}
if (declaration.kind === ts.SyntaxKind.ObjectLiteralExpression) {
const text: string[] = [];
declaration.forEachChild(n => {
text.push(n.getText());
});
return text.join('\n');
}
return declaration.getText();
}
function isStringLiteral(e: ts.Expression): e is ts.StringLiteral {
return e.kind === ts.SyntaxKind.StringLiteral;
}
function getArrayProps(compType: ts.Type, checker: ts.TypeChecker) {
const propSymbol = checker.getPropertyOfType(compType, 'props');
if (!propSymbol || !propSymbol.valueDeclaration) {
return undefined;
}
const propDef = propSymbol.valueDeclaration.getChildAt(2);
if (!propDef || propDef.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
return undefined;
}
const propArray = propDef as ts.ArrayLiteralExpression;
return propArray.elements
.filter(isStringLiteral)
.map(e => ({ name: hyphenate(e.text) }));
}
function getPropertyTypeOfType(tpe: ts.Type, property: string, checker: ts.TypeChecker) {
const propSymbol = checker.getPropertyOfType(tpe, property);
return getSymbolType(propSymbol, checker);
}
function getSymbolType(symbol: ts.Symbol | undefined, checker: ts.TypeChecker) {
if (!symbol || !symbol.valueDeclaration) {
return undefined;
}
return checker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration);
}
const hyphenateRE = /\B([A-Z])/g;
function hyphenate(word: string) {
return word.replace(hyphenateRE, '-$1').toLowerCase();
}