@esportsplus/reactivity 0.27.0 → 0.27.2

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,364 +0,0 @@
1
- import { ts } from '@esportsplus/typescript';
2
- import { ast, code as c, imports, type Range, type Replacement } from '@esportsplus/typescript/compiler';
3
- import { COMPILER_ENTRYPOINT, COMPILER_TYPES, PACKAGE } from '~/constants';
4
- import type { Bindings } from '~/types';
5
-
6
-
7
- interface ArgContext {
8
- argStart: number;
9
- innerReplacements: Replacement[];
10
- ns: string;
11
- scopedBindings: ScopeBinding[];
12
- sourceFile: ts.SourceFile;
13
- }
14
-
15
- interface ScopeBinding {
16
- name: string;
17
- scope: ts.Node;
18
- type: COMPILER_TYPES;
19
- }
20
-
21
- interface TransformContext {
22
- bindings: Bindings;
23
- checker?: ts.TypeChecker;
24
- computedArgRanges: Range[];
25
- ns: string;
26
- replacements: Replacement[];
27
- scopedBindings: ScopeBinding[];
28
- sourceFile: ts.SourceFile;
29
- tmpCounter: number;
30
- }
31
-
32
-
33
- const COMPOUND_OPERATORS = new Map<ts.SyntaxKind, string>([
34
- [ts.SyntaxKind.AmpersandAmpersandEqualsToken, '&&'],
35
- [ts.SyntaxKind.AmpersandEqualsToken, '&'],
36
- [ts.SyntaxKind.AsteriskAsteriskEqualsToken, '**'],
37
- [ts.SyntaxKind.AsteriskEqualsToken, '*'],
38
- [ts.SyntaxKind.BarBarEqualsToken, '||'],
39
- [ts.SyntaxKind.BarEqualsToken, '|'],
40
- [ts.SyntaxKind.CaretEqualsToken, '^'],
41
- [ts.SyntaxKind.GreaterThanGreaterThanEqualsToken, '>>'],
42
- [ts.SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken, '>>>'],
43
- [ts.SyntaxKind.LessThanLessThanEqualsToken, '<<'],
44
- [ts.SyntaxKind.MinusEqualsToken, '-'],
45
- [ts.SyntaxKind.PercentEqualsToken, '%'],
46
- [ts.SyntaxKind.PlusEqualsToken, '+'],
47
- [ts.SyntaxKind.QuestionQuestionEqualsToken, '??'],
48
- [ts.SyntaxKind.SlashEqualsToken, '/']
49
- ]);
50
-
51
-
52
- function findBinding(bindings: ScopeBinding[], name: string, node: ts.Node): ScopeBinding | undefined {
53
- for (let i = 0, n = bindings.length; i < n; i++) {
54
- let b = bindings[i];
55
-
56
- if (b.name === name && isInScope(node, b)) {
57
- return b;
58
- }
59
- }
60
-
61
- return undefined;
62
- }
63
-
64
- function findEnclosingScope(node: ts.Node): ts.Node {
65
- let current = node.parent;
66
-
67
- while (current) {
68
- if (
69
- ts.isBlock(current) ||
70
- ts.isSourceFile(current) ||
71
- ts.isFunctionDeclaration(current) ||
72
- ts.isFunctionExpression(current) ||
73
- ts.isArrowFunction(current) ||
74
- ts.isForStatement(current) ||
75
- ts.isForInStatement(current) ||
76
- ts.isForOfStatement(current)
77
- ) {
78
- return current;
79
- }
80
-
81
- current = current.parent;
82
- }
83
-
84
- return node.getSourceFile();
85
- }
86
-
87
- function isInDeclarationInit(node: ts.Node): boolean {
88
- let parent = node.parent;
89
-
90
- return ts.isVariableDeclaration(parent) && parent.initializer === node;
91
- }
92
-
93
- function isInScope(reference: ts.Node, binding: ScopeBinding): boolean {
94
- let current: ts.Node | undefined = reference;
95
-
96
- while (current) {
97
- if (current === binding.scope) {
98
- return true;
99
- }
100
-
101
- current = current.parent;
102
- }
103
-
104
- return false;
105
- }
106
-
107
- function isReactiveCall(node: ts.CallExpression, checker?: ts.TypeChecker): boolean {
108
- if (!ts.isIdentifier(node.expression)) {
109
- return false;
110
- }
111
-
112
- if (node.expression.text !== COMPILER_ENTRYPOINT) {
113
- return false;
114
- }
115
-
116
- return imports.isFromPackage(node.expression, PACKAGE, checker);
117
- }
118
-
119
- function isReactiveReassignment(node: ts.Node, checker?: ts.TypeChecker): boolean {
120
- let parent = node.parent;
121
-
122
- if (
123
- ts.isBinaryExpression(parent) &&
124
- parent.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
125
- parent.right === node &&
126
- ts.isCallExpression(node)
127
- ) {
128
- return isReactiveCall(node as ts.CallExpression, checker);
129
- }
130
-
131
- return false;
132
- }
133
-
134
- function isWriteContext(node: ts.Identifier): 'simple' | 'compound' | 'increment' | false {
135
- let parent = node.parent;
136
-
137
- if (ts.isBinaryExpression(parent) && parent.left === node) {
138
- let op = parent.operatorToken.kind;
139
-
140
- if (op === ts.SyntaxKind.EqualsToken) {
141
- return 'simple';
142
- }
143
-
144
- if (COMPOUND_OPERATORS.has(op)) {
145
- return 'compound';
146
- }
147
- }
148
-
149
- if (ts.isPostfixUnaryExpression(parent) || ts.isPrefixUnaryExpression(parent)) {
150
- let op = parent.operator;
151
-
152
- if (op === ts.SyntaxKind.PlusPlusToken || op === ts.SyntaxKind.MinusMinusToken) {
153
- return 'increment';
154
- }
155
- }
156
-
157
- return false;
158
- }
159
-
160
- function visit(ctx: TransformContext, node: ts.Node): void {
161
- if (
162
- ts.isCallExpression(node) &&
163
- node.arguments.length > 0 &&
164
- isReactiveCall(node, ctx.checker)
165
- ) {
166
- let arg = node.arguments[0],
167
- classification: COMPILER_TYPES | null = COMPILER_TYPES.Signal;
168
-
169
- if (ts.isArrowFunction(arg) || ts.isFunctionExpression(arg)) {
170
- classification = COMPILER_TYPES.Computed;
171
- }
172
- else if (ts.isObjectLiteralExpression(arg) || ts.isArrayLiteralExpression(arg)) {
173
- classification = null;
174
- }
175
-
176
- if (classification) {
177
- let varName: string | null = null;
178
-
179
- if (node.parent && ts.isVariableDeclaration(node.parent) && ts.isIdentifier(node.parent.name)) {
180
- varName = node.parent.name.text;
181
- }
182
- else if (
183
- node.parent &&
184
- ts.isBinaryExpression(node.parent) &&
185
- node.parent.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
186
- ts.isIdentifier(node.parent.left)
187
- ) {
188
- varName = node.parent.left.text;
189
- }
190
-
191
- if (varName) {
192
- let scope = findEnclosingScope(node);
193
-
194
- ctx.scopedBindings.push({ name: varName, scope, type: classification });
195
- ctx.bindings.set(varName, classification);
196
- }
197
-
198
- if (classification === COMPILER_TYPES.Computed) {
199
- let argStart = arg.getStart(ctx.sourceFile);
200
-
201
- ctx.computedArgRanges.push({ end: arg.end, start: argStart });
202
-
203
- let argCtx: ArgContext = {
204
- argStart,
205
- innerReplacements: [],
206
- ns: ctx.ns,
207
- scopedBindings: ctx.scopedBindings,
208
- sourceFile: ctx.sourceFile
209
- };
210
-
211
- visitArg(argCtx, arg);
212
-
213
- let argText = c.replace(arg.getText(ctx.sourceFile), argCtx.innerReplacements);
214
-
215
- ctx.replacements.push({
216
- end: node.end,
217
- newText: `${ctx.ns}.computed(${argText})`,
218
- start: node.pos
219
- });
220
- }
221
- else {
222
- ctx.replacements.push({
223
- end: node.end,
224
- newText: `${ctx.ns}.signal(${arg.getText(ctx.sourceFile)})`,
225
- start: node.pos
226
- });
227
- }
228
- }
229
- }
230
-
231
-
232
- if (ts.isIdentifier(node) && node.parent && !isInDeclarationInit(node.parent)) {
233
- if (ts.isPropertyAccessExpression(node.parent) && node.parent.name === node) {
234
- ts.forEachChild(node, n => visit(ctx, n));
235
- return;
236
- }
237
-
238
- let nodeStart = node.getStart(ctx.sourceFile);
239
-
240
- if (ast.inRange(ctx.computedArgRanges, nodeStart, node.end)) {
241
- ts.forEachChild(node, n => visit(ctx, n));
242
- return;
243
- }
244
-
245
- let binding = findBinding(ctx.scopedBindings, node.text, node),
246
- name = node.text;
247
-
248
- if (binding && node.parent) {
249
- if (
250
- !isReactiveReassignment(node.parent, ctx.checker) &&
251
- !(ts.isTypeOfExpression(node.parent) && node.parent.expression === node)
252
- ) {
253
- let writeCtx = isWriteContext(node);
254
-
255
- if (writeCtx) {
256
- if (binding.type !== COMPILER_TYPES.Computed) {
257
- let parent = node.parent;
258
-
259
- if (writeCtx === 'simple' && ts.isBinaryExpression(parent)) {
260
- ctx.replacements.push({
261
- end: parent.end,
262
- newText: `${ctx.ns}.write(${name}, ${parent.right.getText(ctx.sourceFile)})`,
263
- start: parent.pos
264
- });
265
- }
266
- else if (writeCtx === 'compound' && ts.isBinaryExpression(parent)) {
267
- let op = COMPOUND_OPERATORS.get(parent.operatorToken.kind) ?? '+'
268
-
269
- ctx.replacements.push({
270
- end: parent.end,
271
- newText: `${ctx.ns}.write(${name}, ${name}.value ${op} ${parent.right.getText(ctx.sourceFile)})`,
272
- start: parent.pos
273
- });
274
- }
275
- else if (writeCtx === 'increment') {
276
- let delta = (parent as ts.PrefixUnaryExpression | ts.PostfixUnaryExpression).operator === ts.SyntaxKind.PlusPlusToken ? '+ 1' : '- 1',
277
- isPrefix = ts.isPrefixUnaryExpression(parent);
278
-
279
- if (ts.isExpressionStatement(parent.parent)) {
280
- ctx.replacements.push({
281
- end: parent.end,
282
- newText: `${ctx.ns}.write(${name}, ${name}.value ${delta})`,
283
- start: parent.pos
284
- });
285
- }
286
- else if (isPrefix) {
287
- ctx.replacements.push({
288
- end: parent.end,
289
- newText: `(${ctx.ns}.write(${name}, ${name}.value ${delta}), ${name}.value)`,
290
- start: parent.pos
291
- });
292
- }
293
- else {
294
- let tmp = `_t${ctx.tmpCounter++}`;
295
-
296
- ctx.replacements.push({
297
- end: parent.end,
298
- newText: `((${tmp}) => (${ctx.ns}.write(${name}, ${tmp} ${delta}), ${tmp}))(${name}.value)`,
299
- start: parent.pos
300
- });
301
- }
302
- }
303
- }
304
- }
305
- else {
306
- ctx.replacements.push({
307
- end: node.end,
308
- newText: `${ctx.ns}.read(${name})`,
309
- start: node.pos
310
- });
311
- }
312
- }
313
- }
314
- }
315
-
316
- ts.forEachChild(node, n => visit(ctx, n));
317
- }
318
-
319
-
320
- function visitArg(ctx: ArgContext, node: ts.Node): void {
321
- if (ts.isIdentifier(node) && node.parent) {
322
- if (
323
- (ts.isPropertyAccessExpression(node.parent) && node.parent.name === node) ||
324
- (ts.isCallExpression(node.parent) && node.parent.expression === node)
325
- ) {
326
- ts.forEachChild(node, n => visitArg(ctx, n));
327
- return;
328
- }
329
-
330
- if (findBinding(ctx.scopedBindings, node.text, node)) {
331
- ctx.innerReplacements.push({
332
- end: node.end - ctx.argStart,
333
- newText: `${ctx.ns}.read(${node.text})`,
334
- start: node.getStart(ctx.sourceFile) - ctx.argStart
335
- });
336
- }
337
- }
338
-
339
- ts.forEachChild(node, n => visitArg(ctx, n));
340
- }
341
-
342
-
343
- export default (sourceFile: ts.SourceFile, bindings: Bindings, ns: string, checker?: ts.TypeChecker): string => {
344
- let code = sourceFile.getFullText(),
345
- ctx: TransformContext = {
346
- bindings,
347
- checker,
348
- computedArgRanges: [],
349
- ns,
350
- replacements: [],
351
- scopedBindings: [],
352
- sourceFile,
353
- tmpCounter: 0
354
- };
355
-
356
- visit(ctx, sourceFile);
357
-
358
- if (ctx.replacements.length === 0) {
359
- return code;
360
- }
361
-
362
- return c.replace(code, ctx.replacements);
363
- };
364
-