-
Notifications
You must be signed in to change notification settings - Fork 805
/
Copy pathcomponent.ts
245 lines (227 loc) · 8.42 KB
/
component.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { normalizePath, unique } from '@utils';
import { dirname, isAbsolute, join, relative } from 'path';
import ts from 'typescript';
import type * as d from '../../../declarations';
import { addComponentMetaStatic } from '../add-component-meta-static';
import { setComponentBuildConditionals } from '../component-build-conditionals';
import { getComponentTagName, getStaticValue, isInternal, isStaticGetter, serializeSymbol } from '../transform-utils';
import { parseCallExpression } from './call-expression';
import { parseClassMethods } from './class-methods';
import { parseStaticElementRef } from './element-ref';
import { parseStaticEncapsulation, parseStaticShadowDelegatesFocus } from './encapsulation';
import { parseStaticEvents } from './events';
import { parseStaticListeners } from './listeners';
import { parseStaticMethods } from './methods';
import { parseStaticProps } from './props';
import { parseStaticStates } from './states';
import { parseStringLiteral } from './string-literal';
import { parseStaticStyles } from './styles';
import { parseStaticWatchers } from './watchers';
/**
* Given an instance of TypeScript's Intermediate Representation (IR) for a
* class declaration ({@see ts.ClassDeclaration}) which represents a Stencil
* component class declaration, parse and format various pieces of data about
* static class members which we use in the compilation process
*
* @param compilerCtx the current compiler context
* @param typeChecker a TypeScript type checker instance
* @param cmpNode the TypeScript class declaration for the component
* @param moduleFile Stencil's IR for a module, used here as an out param
* @param transformOpts options which control various aspects of the
* transformation
* @returns the TypeScript class declaration IR instance with which the
* function was called
*/
export const parseStaticComponentMeta = (
compilerCtx: d.CompilerCtx,
typeChecker: ts.TypeChecker,
cmpNode: ts.ClassDeclaration,
moduleFile: d.Module,
transformOpts?: d.TransformOptions
): ts.ClassDeclaration => {
if (cmpNode.members == null) {
return cmpNode;
}
const staticMembers = cmpNode.members.filter(isStaticGetter);
const tagName = getComponentTagName(staticMembers);
if (tagName == null) {
return cmpNode;
}
const symbol = typeChecker ? typeChecker.getSymbolAtLocation(cmpNode.name) : undefined;
const docs = serializeSymbol(typeChecker, symbol);
const isCollectionDependency = moduleFile.isCollectionDependency;
const encapsulation = parseStaticEncapsulation(staticMembers);
const cmp: d.ComponentCompilerMeta = {
tagName: tagName,
excludeFromCollection: moduleFile.excludeFromCollection,
isCollectionDependency,
componentClassName: cmpNode.name ? cmpNode.name.text : '',
componentClassTypeParameters: [],
elementRef: parseStaticElementRef(staticMembers),
encapsulation,
shadowDelegatesFocus: parseStaticShadowDelegatesFocus(encapsulation, staticMembers),
properties: parseStaticProps(staticMembers),
virtualProperties: parseVirtualProps(docs),
states: parseStaticStates(staticMembers),
methods: parseStaticMethods(staticMembers),
listeners: parseStaticListeners(staticMembers),
events: parseStaticEvents(staticMembers),
watchers: parseStaticWatchers(staticMembers),
styles: parseStaticStyles(compilerCtx, tagName, moduleFile.sourceFilePath, isCollectionDependency, staticMembers),
legacyConnect: getStaticValue(staticMembers, 'connectProps') || [],
legacyContext: getStaticValue(staticMembers, 'contextProps') || [],
internal: isInternal(docs),
assetsDirs: parseAssetsDirs(staticMembers, moduleFile.jsFilePath),
styleDocs: [],
docs,
jsFilePath: moduleFile.jsFilePath,
sourceFilePath: moduleFile.sourceFilePath,
sourceMapPath: moduleFile.sourceMapPath,
hasAttributeChangedCallbackFn: false,
hasComponentWillLoadFn: false,
hasComponentDidLoadFn: false,
hasComponentShouldUpdateFn: false,
hasComponentWillUpdateFn: false,
hasComponentDidUpdateFn: false,
hasComponentWillRenderFn: false,
hasComponentDidRenderFn: false,
hasComponentDidUnloadFn: false,
hasConnectedCallbackFn: false,
hasDisconnectedCallbackFn: false,
hasElement: false,
hasEvent: false,
hasLifecycle: false,
hasListener: false,
hasListenerTarget: false,
hasListenerTargetWindow: false,
hasListenerTargetDocument: false,
hasListenerTargetBody: false,
hasListenerTargetParent: false,
hasMember: false,
hasMethod: false,
hasMode: false,
hasAttribute: false,
hasProp: false,
hasPropNumber: false,
hasPropBoolean: false,
hasPropString: false,
hasPropMutable: false,
hasReflect: false,
hasRenderFn: false,
hasState: false,
hasStyle: false,
hasVdomAttribute: false,
hasVdomXlink: false,
hasVdomClass: false,
hasVdomFunctional: false,
hasVdomKey: false,
hasVdomListener: false,
hasVdomPropOrAttr: false,
hasVdomRef: false,
hasVdomRender: false,
hasVdomStyle: false,
hasVdomText: false,
hasWatchCallback: false,
isPlain: false,
htmlAttrNames: [],
htmlTagNames: [],
htmlParts: [],
isUpdateable: false,
potentialCmpRefs: [],
};
/**
* This code add a component class name with the type
* parameters. Later in the compiler execution there
* will this name used to named the interfaces in the
* component.d.ts.
*
* Addressed issue(s):
* - https://github.com/ionic-team/stencil/issues/2895
*
* TypeScript implementation:
* - https://github.com/microsoft/TypeScript/blob/7584e6aad6b21f7334562bfd9d8c3c80aafed064/src/services/services.ts#L326
*/
if (
typeof symbol === 'object' &&
symbol !== null &&
typeof symbol.getDeclarations === 'function'
) {
const declarations = symbol.getDeclarations();
if (Array.isArray(declarations) && declarations.length > 0) {
const declaration = declarations[0] as ts.ClassDeclaration;
if (Array.isArray(declaration.typeParameters)) {
declaration.typeParameters.forEach((typeParameter) =>
cmp.componentClassTypeParameters.push(typeParameter.name.text)
);
}
}
}
const visitComponentChildNode = (node: ts.Node) => {
if (ts.isCallExpression(node)) {
parseCallExpression(cmp, node);
} else if (ts.isStringLiteral(node)) {
parseStringLiteral(cmp, node);
}
node.forEachChild(visitComponentChildNode);
};
visitComponentChildNode(cmpNode);
parseClassMethods(cmpNode, cmp);
cmp.legacyConnect.forEach(({ connect }) => {
cmp.htmlTagNames.push(connect);
if (connect.includes('-')) {
cmp.potentialCmpRefs.push(connect);
}
});
cmp.htmlAttrNames = unique(cmp.htmlAttrNames);
cmp.htmlTagNames = unique(cmp.htmlTagNames);
cmp.potentialCmpRefs = unique(cmp.potentialCmpRefs);
setComponentBuildConditionals(cmp);
if (transformOpts && transformOpts.componentMetadata === 'compilerstatic') {
cmpNode = addComponentMetaStatic(cmpNode, cmp);
}
// add to module map
moduleFile.cmps.push(cmp);
// add to node map
compilerCtx.nodeMap.set(cmpNode, cmp);
return cmpNode;
};
const parseVirtualProps = (docs: d.CompilerJsDoc) => {
return docs.tags
.filter(({ name }) => name === 'virtualProp')
.map(parseVirtualProp)
.filter((prop) => !!prop);
};
const parseVirtualProp = (tag: d.CompilerJsDocTagInfo): d.ComponentCompilerVirtualProperty => {
const results = /^\s*(?:\{([^}]+)\}\s+)?(\w+)\s+-\s+(.*)$/.exec(tag.text);
if (!results) {
return undefined;
}
const [, type, name, docs] = results;
return {
type: type == null ? 'any' : type.trim(),
name: name.trim(),
docs: docs.trim(),
};
};
const parseAssetsDirs = (staticMembers: ts.ClassElement[], componentFilePath: string): d.AssetsMeta[] => {
const dirs: string[] = getStaticValue(staticMembers, 'assetsDirs') || [];
const componentDir = normalizePath(dirname(componentFilePath));
return dirs.map((dir) => {
// get the relative path from the component file to the assets directory
dir = normalizePath(dir.trim());
let absolutePath = dir;
let cmpRelativePath = dir;
if (isAbsolute(dir)) {
// if this is an absolute path already, let's convert it to be relative
cmpRelativePath = relative(componentDir, dir);
} else {
// create the absolute path to the asset dir
absolutePath = join(componentDir, dir);
}
return {
absolutePath,
cmpRelativePath,
originalComponentPath: dir,
};
});
};