-
Notifications
You must be signed in to change notification settings - Fork 798
/
Copy pathlazy-element-getter.ts
48 lines (43 loc) · 1.56 KB
/
lazy-element-getter.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
import ts from 'typescript';
import type * as d from '../../../declarations';
import { addCoreRuntimeApi, GET_ELEMENT, RUNTIME_APIS } from '../core-runtime-apis';
export const addLazyElementGetter = (
classMembers: ts.ClassElement[],
moduleFile: d.Module,
cmp: d.ComponentCompilerMeta
) => {
// @Element() element;
// is transformed into:
// get element() { return __stencil_getElement(this); }
if (cmp.elementRef) {
addCoreRuntimeApi(moduleFile, RUNTIME_APIS.getElement);
// Create the getter that will be used in the transformed class declaration
const getter = ts.factory.createGetAccessorDeclaration(
undefined,
cmp.elementRef,
[],
undefined,
ts.factory.createBlock([
ts.factory.createReturnStatement(
ts.factory.createCallExpression(ts.factory.createIdentifier(GET_ELEMENT), undefined, [
ts.factory.createThis(),
])
),
])
);
// Find the index in the class members array that correlates with the element
// ref identifier we have
const index = classMembers.findIndex(
(member) =>
member.kind === ts.SyntaxKind.PropertyDeclaration && (member.name as any)?.escapedText === cmp.elementRef
);
// Index should never not be a valid integer, but we'll be safe just in case.
// If the index is valid, we'll overwrite the existing class member with the getter
// so we don't create multiple members with the same identifier
if (index >= 0) {
classMembers[index] = getter;
} else {
classMembers.push(getter);
}
}
};