-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomElements.ts
167 lines (159 loc) · 4.64 KB
/
CustomElements.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
import * as ts from "typescript";
import PostCSS from "../CSSCompiler";
type SClass = ts.ClassDeclaration & { Tag?: string };
function ElementDefine(Tag: string, Classname: string) {
return ts.createStatement(
ts.createCall(
ts.createPropertyAccess(
ts.createIdentifier("customElements"),
ts.createIdentifier("define")
),
undefined,
[ts.createStringLiteral(Tag), ts.createIdentifier(Classname)]
)
);
}
function ManagerDefine(Classname: string) {
return ts.createStatement(
ts.createCall(
ts.createPropertyAccess(
ts.createIdentifier(Classname),
ts.createIdentifier("$Constr")
),
undefined,
[]
)
);
}
const DashCase = (name: string) =>
name.replace(/([A-Z])/g, g => `-${g[0].toLowerCase()}`);
const SpliceDecorator = (
node: ts.Node,
Condition: (x: ts.Decorator) => boolean
) => {
if (!node.decorators) return;
const Dec = node.decorators.findIndex(Condition);
if (Dec > -1) {
const R = node.decorators[Dec];
const Decs = Array.from(node.decorators);
Decs.splice(Dec, 1);
if (Decs && Decs.length) {
node.decorators = ts.createNodeArray(Decs);
} else {
delete node.decorators;
}
return R;
}
};
const CustomElementDecorator = (node: SClass) => {
const CustomElement = SpliceDecorator(
node,
x =>
ts.isPropertyAccessExpression(x.expression) &&
((x.expression as ts.PropertyAccessExpression)
.expression as ts.Identifier).escapedText == "CustomElement"
);
const NamedManager = SpliceDecorator(
node,
x =>
ts.isPropertyAccessExpression(x.expression) &&
((x.expression as ts.PropertyAccessExpression)
.expression as ts.Identifier).escapedText == "Execute"
);
const Manager = SpliceDecorator(
node,
x => (x.expression as ts.Identifier).escapedText == "Execute"
);
if (CustomElement || Manager || NamedManager) {
const name = node.name.getText();
const ns = CustomElement
? (CustomElement.expression as ts.PropertyAccessExpression).name
.getText()
.toLowerCase()
: "manager";
const Key = NamedManager
? (NamedManager.expression as ts.PropertyAccessExpression).name.getText()
: ns + DashCase(name);
node.Tag = Key;
const AssignTag = ts.createProperty(
undefined,
[ts.createModifier(ts.SyntaxKind.StaticKeyword)],
"Tag",
undefined,
undefined,
ts.createStringLiteral(Key)
);
const DOMDisable = ts.createProperty(
undefined,
[ts.createModifier(ts.SyntaxKind.StaticKeyword)],
"DOM",
undefined,
undefined,
ts.createFalse()
);
let Nodes: any[] = [AssignTag];
if(NamedManager) Nodes.push(DOMDisable);
Nodes = Nodes.concat(node.members);
node = ts.updateClassDeclaration(
node,
node.decorators,
node.modifiers,
node.name,
node.typeParameters,
node.heritageClauses,
ts.createNodeArray(Nodes)
);
const R = [node];
R.push(ElementDefine((NamedManager ? "managed-" : "") + Key, node.name.text) as any);
if (Manager || NamedManager) R.push(ManagerDefine(node.name.text) as any);
return R;
}
return [node];
};
const ClassMembersVisitor = (Cls: SClass, context: ts.TransformationContext) => (
node: ts.MemberExpression
) => {
if (PostCSS.IsPostCSS(node)) {
const IsStatic =
node.parent &&
node.parent.modifiers &&
node.parent.modifiers.find(x => x.kind == ts.SyntaxKind.StaticKeyword);
const Tag = IsStatic ? Cls.Tag : undefined;
return PostCSS.AddBlock(node as ts.TaggedTemplateExpression, Tag);
}
return ts.visitEachChild(node, ClassMembersVisitor(Cls, context), context);
};
function CustomElementTransformer<T extends ts.Node>(): ts.TransformerFactory<
T
> {
return context => {
const visit: ts.Visitor = node => {
if (ts.isSourceFile(node))
return ts.visitEachChild(node, child => visit(child), context);
if (ts.isClassDeclaration(node)) {
if (node.decorators) {
const R = CustomElementDecorator(node);
R[0] = ts.visitEachChild(
R[0],
ClassMembersVisitor(node, context),
context
);
return R;
}
return ts.visitEachChild(
node,
ClassMembersVisitor(node, context),
context
);
}
else if(ts.isExpressionStatement(node) && PostCSS.IsPostCSS(node.expression)){
return PostCSS.AddBlock(node.expression as ts.TaggedTemplateExpression);
}
return node;
};
return node => ts.visitNode(node, visit);
};
}
export default {
before: [CustomElementTransformer()]
};