@esportsplus/template 0.35.0 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,193 +0,0 @@
1
- import { COMPILER_ENTRYPOINT, COMPILER_ENTRYPOINT_REACTIVITY, COMPILER_TYPES, DIRECT_ATTACH_EVENTS, LIFECYCLE_EVENTS } from '../constants.js';
2
- import { ts } from '@esportsplus/typescript';
3
- function analyzeSpread(expr, checker) {
4
- while (ts.isParenthesizedExpression(expr)) {
5
- expr = expr.expression;
6
- }
7
- if (ts.isObjectLiteralExpression(expr)) {
8
- let keys = [];
9
- for (let i = 0, n = expr.properties.length; i < n; i++) {
10
- let prop = expr.properties[i];
11
- if (ts.isPropertyAssignment(prop)) {
12
- if (ts.isIdentifier(prop.name) || ts.isStringLiteral(prop.name)) {
13
- keys.push(prop.name.text);
14
- }
15
- }
16
- else if (ts.isShorthandPropertyAssignment(prop)) {
17
- keys.push(prop.name.text);
18
- }
19
- else if (ts.isSpreadAssignment(prop)) {
20
- return { canUnpack: false, keys: [] };
21
- }
22
- }
23
- return { canUnpack: true, keys };
24
- }
25
- if (checker && (ts.isIdentifier(expr) || ts.isPropertyAccessExpression(expr))) {
26
- try {
27
- let keys = [], props = checker.getTypeAtLocation(expr).getProperties();
28
- for (let i = 0, n = props.length; i < n; i++) {
29
- let name = props[i].getName();
30
- if (name.startsWith('__') || name.startsWith('[')) {
31
- continue;
32
- }
33
- keys.push(name);
34
- }
35
- if (keys.length > 0) {
36
- return { canUnpack: true, keys };
37
- }
38
- }
39
- catch { }
40
- }
41
- return { canUnpack: false, keys: [] };
42
- }
43
- function inferCOMPILER_TYPES(expr, checker) {
44
- while (ts.isParenthesizedExpression(expr)) {
45
- expr = expr.expression;
46
- }
47
- if (ts.isArrowFunction(expr) || ts.isFunctionExpression(expr)) {
48
- return COMPILER_TYPES.Effect;
49
- }
50
- if (ts.isCallExpression(expr) &&
51
- ts.isPropertyAccessExpression(expr.expression) &&
52
- ts.isIdentifier(expr.expression.expression) &&
53
- expr.expression.expression.text === COMPILER_ENTRYPOINT &&
54
- expr.expression.name.text === COMPILER_ENTRYPOINT_REACTIVITY) {
55
- return COMPILER_TYPES.ArraySlot;
56
- }
57
- if (ts.isTaggedTemplateExpression(expr) && ts.isIdentifier(expr.tag) && expr.tag.text === COMPILER_ENTRYPOINT) {
58
- return COMPILER_TYPES.DocumentFragment;
59
- }
60
- if (ts.isArrayLiteralExpression(expr)) {
61
- return COMPILER_TYPES.ArraySlot;
62
- }
63
- if (ts.isNumericLiteral(expr) ||
64
- ts.isStringLiteral(expr) ||
65
- ts.isNoSubstitutionTemplateLiteral(expr) ||
66
- expr.kind === ts.SyntaxKind.TrueKeyword ||
67
- expr.kind === ts.SyntaxKind.FalseKeyword ||
68
- expr.kind === ts.SyntaxKind.NullKeyword ||
69
- expr.kind === ts.SyntaxKind.UndefinedKeyword) {
70
- return COMPILER_TYPES.Static;
71
- }
72
- if (ts.isTemplateExpression(expr)) {
73
- return COMPILER_TYPES.Primitive;
74
- }
75
- if (ts.isConditionalExpression(expr)) {
76
- let whenFalse = inferCOMPILER_TYPES(expr.whenFalse, checker), whenTrue = inferCOMPILER_TYPES(expr.whenTrue, checker);
77
- if (whenTrue === whenFalse) {
78
- return whenTrue;
79
- }
80
- if (whenTrue === COMPILER_TYPES.Effect || whenFalse === COMPILER_TYPES.Effect) {
81
- return COMPILER_TYPES.Effect;
82
- }
83
- return COMPILER_TYPES.Unknown;
84
- }
85
- if (checker && (ts.isIdentifier(expr) || ts.isPropertyAccessExpression(expr) || ts.isCallExpression(expr))) {
86
- try {
87
- let type = checker.getTypeAtLocation(expr);
88
- if (isTypeFunction(type, checker)) {
89
- return COMPILER_TYPES.Effect;
90
- }
91
- if (isTypeArray(type, checker)) {
92
- return COMPILER_TYPES.ArraySlot;
93
- }
94
- }
95
- catch {
96
- }
97
- }
98
- return COMPILER_TYPES.Unknown;
99
- }
100
- function isTypeArray(type, checker) {
101
- if (checker.isArrayType(type)) {
102
- return true;
103
- }
104
- return type.getSymbol()?.getName() === 'ReactiveArray';
105
- }
106
- function isTypeFunction(type, checker) {
107
- if (type.getCallSignatures().length > 0) {
108
- return true;
109
- }
110
- if (type.isUnion()) {
111
- for (let i = 0, n = type.types.length; i < n; i++) {
112
- if (isTypeFunction(type.types[i], checker)) {
113
- return true;
114
- }
115
- }
116
- }
117
- return false;
118
- }
119
- const analyzeExpression = (expr, checker) => {
120
- return inferCOMPILER_TYPES(expr, checker);
121
- };
122
- const generateAttributeBinding = (elementVar, name, expr, staticValue, neededImports) => {
123
- if (name.startsWith('on') && name.length > 2) {
124
- let event = name.slice(2).toLowerCase(), key = name.toLowerCase();
125
- if (LIFECYCLE_EVENTS.has(key)) {
126
- neededImports.add(key);
127
- return `${key}(${elementVar}, ${expr});`;
128
- }
129
- if (DIRECT_ATTACH_EVENTS.has(key)) {
130
- neededImports.add('on');
131
- return `on(${elementVar}, '${event}', ${expr});`;
132
- }
133
- neededImports.add('delegate');
134
- return `delegate(${elementVar}, '${event}', ${expr});`;
135
- }
136
- if (name === 'class') {
137
- neededImports.add('setClass');
138
- return `setClass(${elementVar}, '${staticValue}', ${expr});`;
139
- }
140
- if (name === COMPILER_TYPES.Attributes) {
141
- neededImports.add('setProperties');
142
- return `setProperties(${elementVar}, ${expr});`;
143
- }
144
- if (name === 'style') {
145
- neededImports.add('setStyle');
146
- return `setStyle(${elementVar}, '${staticValue}', ${expr});`;
147
- }
148
- neededImports.add('setProperty');
149
- return `setProperty(${elementVar}, '${name}', ${expr});`;
150
- };
151
- const generateSpreadBindings = (expr, exprCode, elementVar, checker, neededImports) => {
152
- while (ts.isParenthesizedExpression(expr)) {
153
- expr = expr.expression;
154
- }
155
- let analysis = analyzeSpread(expr, checker);
156
- if (!analysis.canUnpack) {
157
- neededImports.add('setProperties');
158
- return [`setProperties(${elementVar}, ${exprCode});`];
159
- }
160
- let lines = [];
161
- if (ts.isObjectLiteralExpression(expr)) {
162
- for (let i = 0, n = analysis.keys.length; i < n; i++) {
163
- let key = analysis.keys[i], value = null;
164
- for (let j = 0, m = expr.properties.length; j < m; j++) {
165
- let prop = expr.properties[j];
166
- if (ts.isPropertyAssignment(prop)) {
167
- let text = ts.isIdentifier(prop.name)
168
- ? prop.name.text
169
- : ts.isStringLiteral(prop.name) ? prop.name.text : null;
170
- if (text === key) {
171
- value = prop.initializer.getText();
172
- break;
173
- }
174
- }
175
- else if (ts.isShorthandPropertyAssignment(prop) && prop.name.text === key) {
176
- value = prop.name.text;
177
- break;
178
- }
179
- }
180
- if (value !== null) {
181
- lines.push(generateAttributeBinding(elementVar, key, value, '', neededImports));
182
- }
183
- }
184
- }
185
- else {
186
- for (let i = 0, n = analysis.keys.length; i < n; i++) {
187
- let key = analysis.keys[i];
188
- lines.push(generateAttributeBinding(elementVar, key, `${exprCode}.${key}`, '', neededImports));
189
- }
190
- }
191
- return lines;
192
- };
193
- export { analyzeExpression, generateAttributeBinding, generateSpreadBindings };
@@ -1,274 +0,0 @@
1
- import {
2
- COMPILER_ENTRYPOINT,
3
- COMPILER_ENTRYPOINT_REACTIVITY,
4
- COMPILER_TYPES,
5
- DIRECT_ATTACH_EVENTS,
6
- LIFECYCLE_EVENTS
7
- } from '~/constants';
8
- import { ts } from '@esportsplus/typescript';
9
-
10
-
11
- type SpreadAnalysis = {
12
- canUnpack: boolean;
13
- keys: string[];
14
- };
15
-
16
-
17
- function analyzeSpread(expr: ts.Expression, checker?: ts.TypeChecker): SpreadAnalysis {
18
- while (ts.isParenthesizedExpression(expr)) {
19
- expr = expr.expression;
20
- }
21
-
22
- if (ts.isObjectLiteralExpression(expr)) {
23
- let keys: string[] = [];
24
-
25
- for (let i = 0, n = expr.properties.length; i < n; i++) {
26
- let prop = expr.properties[i];
27
-
28
- if (ts.isPropertyAssignment(prop)) {
29
- if (ts.isIdentifier(prop.name) || ts.isStringLiteral(prop.name)) {
30
- keys.push(prop.name.text);
31
- }
32
- }
33
- else if (ts.isShorthandPropertyAssignment(prop)) {
34
- keys.push(prop.name.text);
35
- }
36
- else if (ts.isSpreadAssignment(prop)) {
37
- return { canUnpack: false, keys: [] };
38
- }
39
- }
40
-
41
- return { canUnpack: true, keys };
42
- }
43
-
44
- if (checker && (ts.isIdentifier(expr) || ts.isPropertyAccessExpression(expr))) {
45
- try {
46
- let keys: string[] = [],
47
- props = checker.getTypeAtLocation(expr).getProperties();
48
-
49
- for (let i = 0, n = props.length; i < n; i++) {
50
- let name = props[i].getName();
51
-
52
- if (name.startsWith('__') || name.startsWith('[')) {
53
- continue;
54
- }
55
-
56
- keys.push(name);
57
- }
58
-
59
- if (keys.length > 0) {
60
- return { canUnpack: true, keys };
61
- }
62
- }
63
- catch { }
64
- }
65
-
66
- return { canUnpack: false, keys: [] };
67
- }
68
-
69
- function inferCOMPILER_TYPES(expr: ts.Expression, checker?: ts.TypeChecker): COMPILER_TYPES {
70
- while (ts.isParenthesizedExpression(expr)) {
71
- expr = expr.expression;
72
- }
73
-
74
- if (ts.isArrowFunction(expr) || ts.isFunctionExpression(expr)) {
75
- return COMPILER_TYPES.Effect;
76
- }
77
-
78
- if (
79
- ts.isCallExpression(expr) &&
80
- ts.isPropertyAccessExpression(expr.expression) &&
81
- ts.isIdentifier(expr.expression.expression) &&
82
- expr.expression.expression.text === COMPILER_ENTRYPOINT &&
83
- expr.expression.name.text === COMPILER_ENTRYPOINT_REACTIVITY
84
- ) {
85
- return COMPILER_TYPES.ArraySlot;
86
- }
87
-
88
- if (ts.isTaggedTemplateExpression(expr) && ts.isIdentifier(expr.tag) && expr.tag.text === COMPILER_ENTRYPOINT) {
89
- return COMPILER_TYPES.DocumentFragment;
90
- }
91
-
92
- if (ts.isArrayLiteralExpression(expr)) {
93
- return COMPILER_TYPES.ArraySlot;
94
- }
95
-
96
- if (
97
- ts.isNumericLiteral(expr) ||
98
- ts.isStringLiteral(expr) ||
99
- ts.isNoSubstitutionTemplateLiteral(expr) ||
100
- expr.kind === ts.SyntaxKind.TrueKeyword ||
101
- expr.kind === ts.SyntaxKind.FalseKeyword ||
102
- expr.kind === ts.SyntaxKind.NullKeyword ||
103
- expr.kind === ts.SyntaxKind.UndefinedKeyword
104
- ) {
105
- return COMPILER_TYPES.Static;
106
- }
107
-
108
- if (ts.isTemplateExpression(expr)) {
109
- return COMPILER_TYPES.Primitive;
110
- }
111
-
112
- if (ts.isConditionalExpression(expr)) {
113
- let whenFalse = inferCOMPILER_TYPES(expr.whenFalse, checker),
114
- whenTrue = inferCOMPILER_TYPES(expr.whenTrue, checker);
115
-
116
- if (whenTrue === whenFalse) {
117
- return whenTrue;
118
- }
119
-
120
- if (whenTrue === COMPILER_TYPES.Effect || whenFalse === COMPILER_TYPES.Effect) {
121
- return COMPILER_TYPES.Effect;
122
- }
123
-
124
- return COMPILER_TYPES.Unknown;
125
- }
126
-
127
- if (checker && (ts.isIdentifier(expr) || ts.isPropertyAccessExpression(expr) || ts.isCallExpression(expr))) {
128
- try {
129
- let type = checker.getTypeAtLocation(expr);
130
-
131
- if (isTypeFunction(type, checker)) {
132
- return COMPILER_TYPES.Effect;
133
- }
134
-
135
- if (isTypeArray(type, checker)) {
136
- return COMPILER_TYPES.ArraySlot;
137
- }
138
- }
139
- catch {
140
- }
141
- }
142
-
143
- return COMPILER_TYPES.Unknown;
144
- }
145
-
146
- function isTypeArray(type: ts.Type, checker: ts.TypeChecker): boolean {
147
- if (checker.isArrayType(type)) {
148
- return true;
149
- }
150
-
151
- return type.getSymbol()?.getName() === 'ReactiveArray';
152
- }
153
-
154
- function isTypeFunction(type: ts.Type, checker: ts.TypeChecker): boolean {
155
- if (type.getCallSignatures().length > 0) {
156
- return true;
157
- }
158
-
159
- if (type.isUnion()) {
160
- for (let i = 0, n = type.types.length; i < n; i++) {
161
- if (isTypeFunction(type.types[i], checker)) {
162
- return true;
163
- }
164
- }
165
- }
166
-
167
- return false;
168
- }
169
-
170
-
171
- const analyzeExpression = (expr: ts.Expression, checker?: ts.TypeChecker): COMPILER_TYPES => {
172
- return inferCOMPILER_TYPES(expr, checker);
173
- };
174
-
175
- const generateAttributeBinding = (elementVar: string, name: string, expr: string, staticValue: string, neededImports: Set<string>): string => {
176
- if (name.startsWith('on') && name.length > 2) {
177
- let event = name.slice(2).toLowerCase(),
178
- key = name.toLowerCase();
179
-
180
- if (LIFECYCLE_EVENTS.has(key)) {
181
- neededImports.add(key);
182
- return `${key}(${elementVar}, ${expr});`;
183
- }
184
-
185
- if (DIRECT_ATTACH_EVENTS.has(key)) {
186
- neededImports.add('on');
187
- return `on(${elementVar}, '${event}', ${expr});`;
188
- }
189
-
190
- neededImports.add('delegate');
191
- return `delegate(${elementVar}, '${event}', ${expr});`;
192
- }
193
-
194
- if (name === 'class') {
195
- neededImports.add('setClass');
196
- return `setClass(${elementVar}, '${staticValue}', ${expr});`;
197
- }
198
-
199
- if (name === COMPILER_TYPES.Attributes) {
200
- neededImports.add('setProperties');
201
- return `setProperties(${elementVar}, ${expr});`;
202
- }
203
-
204
- if (name === 'style') {
205
- neededImports.add('setStyle');
206
- return `setStyle(${elementVar}, '${staticValue}', ${expr});`;
207
- }
208
-
209
- neededImports.add('setProperty');
210
- return `setProperty(${elementVar}, '${name}', ${expr});`;
211
- };
212
-
213
- const generateSpreadBindings = (
214
- expr: ts.Expression,
215
- exprCode: string,
216
- elementVar: string,
217
- checker: ts.TypeChecker | undefined,
218
- neededImports: Set<string>
219
- ): string[] => {
220
- while (ts.isParenthesizedExpression(expr)) {
221
- expr = expr.expression;
222
- }
223
-
224
- let analysis = analyzeSpread(expr, checker);
225
-
226
- if (!analysis.canUnpack) {
227
- neededImports.add('setProperties');
228
- return [`setProperties(${elementVar}, ${exprCode});`];
229
- }
230
-
231
- let lines: string[] = [];
232
-
233
- if (ts.isObjectLiteralExpression(expr)) {
234
- for (let i = 0, n = analysis.keys.length; i < n; i++) {
235
- let key = analysis.keys[i],
236
- value: string | null = null;
237
-
238
- for (let j = 0, m = expr.properties.length; j < m; j++) {
239
- let prop = expr.properties[j];
240
-
241
- if (ts.isPropertyAssignment(prop)) {
242
- let text = ts.isIdentifier(prop.name)
243
- ? prop.name.text
244
- : ts.isStringLiteral(prop.name) ? prop.name.text : null;
245
-
246
- if (text === key) {
247
- value = prop.initializer.getText();
248
- break;
249
- }
250
- }
251
- else if (ts.isShorthandPropertyAssignment(prop) && prop.name.text === key) {
252
- value = prop.name.text;
253
- break;
254
- }
255
- }
256
-
257
- if (value !== null) {
258
- lines.push(generateAttributeBinding(elementVar, key, value, '', neededImports));
259
- }
260
- }
261
- }
262
- else {
263
- for (let i = 0, n = analysis.keys.length; i < n; i++) {
264
- let key = analysis.keys[i];
265
-
266
- lines.push(generateAttributeBinding(elementVar, key, `${exprCode}.${key}`, '', neededImports));
267
- }
268
- }
269
-
270
- return lines;
271
- };
272
-
273
-
274
- export { analyzeExpression, generateAttributeBinding, generateSpreadBindings };