@esportsplus/reactivity 0.25.9 → 0.25.11
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.
- package/build/constants.d.ts +7 -4
- package/build/constants.js +8 -4
- package/build/transformer/index.d.ts +1 -2
- package/build/transformer/index.js +1 -1
- package/build/transformer/transforms/array.js +8 -8
- package/build/transformer/transforms/object.js +10 -10
- package/build/transformer/transforms/primitives.js +5 -5
- package/build/types.d.ts +3 -4
- package/package.json +1 -1
- package/src/constants.ts +7 -6
- package/src/transformer/index.ts +1 -1
- package/src/transformer/transforms/array.ts +8 -8
- package/src/transformer/transforms/object.ts +11 -11
- package/src/transformer/transforms/primitives.ts +8 -8
- package/src/types.ts +3 -5
package/build/constants.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
declare const COMPILER_ENTRYPOINT = "reactive";
|
|
2
2
|
declare const COMPILER_ENTRYPOINT_REGEX: RegExp;
|
|
3
3
|
declare const COMPILER_NAMESPACE: string;
|
|
4
|
-
declare const
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
declare const enum COMPILER_TYPES {
|
|
5
|
+
Array = 0,
|
|
6
|
+
Computed = 1,
|
|
7
|
+
Object = 2,
|
|
8
|
+
Signal = 3
|
|
9
|
+
}
|
|
7
10
|
declare const COMPUTED: unique symbol;
|
|
8
11
|
declare const PACKAGE = "@esportsplus/reactivity";
|
|
9
12
|
declare const REACTIVE_ARRAY: unique symbol;
|
|
@@ -19,4 +22,4 @@ declare const STATE_DIRTY: number;
|
|
|
19
22
|
declare const STATE_RECOMPUTING: number;
|
|
20
23
|
declare const STATE_IN_HEAP: number;
|
|
21
24
|
declare const STATE_NOTIFY_MASK: number;
|
|
22
|
-
export {
|
|
25
|
+
export { COMPILER_ENTRYPOINT, COMPILER_ENTRYPOINT_REGEX, COMPILER_NAMESPACE, COMPILER_TYPES, COMPUTED, PACKAGE, REACTIVE_ARRAY, REACTIVE_OBJECT, SIGNAL, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED, STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE, STATE_NOTIFY_MASK, STATE_RECOMPUTING };
|
package/build/constants.js
CHANGED
|
@@ -2,9 +2,13 @@ import { uid } from '@esportsplus/typescript/transformer';
|
|
|
2
2
|
const COMPILER_ENTRYPOINT = 'reactive';
|
|
3
3
|
const COMPILER_ENTRYPOINT_REGEX = /\breactive\b/;
|
|
4
4
|
const COMPILER_NAMESPACE = uid(COMPILER_ENTRYPOINT);
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
var COMPILER_TYPES;
|
|
6
|
+
(function (COMPILER_TYPES) {
|
|
7
|
+
COMPILER_TYPES[COMPILER_TYPES["Array"] = 0] = "Array";
|
|
8
|
+
COMPILER_TYPES[COMPILER_TYPES["Computed"] = 1] = "Computed";
|
|
9
|
+
COMPILER_TYPES[COMPILER_TYPES["Object"] = 2] = "Object";
|
|
10
|
+
COMPILER_TYPES[COMPILER_TYPES["Signal"] = 3] = "Signal";
|
|
11
|
+
})(COMPILER_TYPES || (COMPILER_TYPES = {}));
|
|
8
12
|
const COMPUTED = Symbol('reactivity.computed');
|
|
9
13
|
const PACKAGE = '@esportsplus/reactivity';
|
|
10
14
|
const REACTIVE_ARRAY = Symbol('reactivity.reactive.array');
|
|
@@ -20,4 +24,4 @@ const STATE_DIRTY = 1 << 1;
|
|
|
20
24
|
const STATE_RECOMPUTING = 1 << 2;
|
|
21
25
|
const STATE_IN_HEAP = 1 << 3;
|
|
22
26
|
const STATE_NOTIFY_MASK = (STATE_CHECK | STATE_DIRTY);
|
|
23
|
-
export {
|
|
27
|
+
export { COMPILER_ENTRYPOINT, COMPILER_ENTRYPOINT_REGEX, COMPILER_NAMESPACE, COMPILER_TYPES, COMPUTED, PACKAGE, REACTIVE_ARRAY, REACTIVE_OBJECT, SIGNAL, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED, STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE, STATE_NOTIFY_MASK, STATE_RECOMPUTING };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
2
|
import type { TransformResult } from '../types.js';
|
|
3
|
-
declare function contains(code: string): boolean;
|
|
4
3
|
declare const transform: (sourceFile: ts.SourceFile) => TransformResult;
|
|
5
|
-
export {
|
|
4
|
+
export { transform };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { code as c } from '@esportsplus/typescript/transformer';
|
|
2
2
|
import { ts } from '@esportsplus/typescript';
|
|
3
|
-
import {
|
|
3
|
+
import { COMPILER_TYPES } from '../../constants.js';
|
|
4
4
|
function getExpressionName(node) {
|
|
5
5
|
if (ts.isIdentifier(node)) {
|
|
6
6
|
return node.text;
|
|
@@ -30,13 +30,13 @@ function isAssignmentTarget(node) {
|
|
|
30
30
|
}
|
|
31
31
|
function visit(ctx, node) {
|
|
32
32
|
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer) {
|
|
33
|
-
if (ts.isIdentifier(node.initializer) && ctx.bindings.get(node.initializer.text) ===
|
|
34
|
-
ctx.bindings.set(node.name.text,
|
|
33
|
+
if (ts.isIdentifier(node.initializer) && ctx.bindings.get(node.initializer.text) === COMPILER_TYPES.Array) {
|
|
34
|
+
ctx.bindings.set(node.name.text, COMPILER_TYPES.Array);
|
|
35
35
|
}
|
|
36
36
|
if (ts.isPropertyAccessExpression(node.initializer)) {
|
|
37
37
|
let path = getPropertyPath(node.initializer);
|
|
38
|
-
if (path && ctx.bindings.get(path) ===
|
|
39
|
-
ctx.bindings.set(node.name.text,
|
|
38
|
+
if (path && ctx.bindings.get(path) === COMPILER_TYPES.Array) {
|
|
39
|
+
ctx.bindings.set(node.name.text, COMPILER_TYPES.Array);
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
}
|
|
@@ -47,7 +47,7 @@ function visit(ctx, node) {
|
|
|
47
47
|
ts.isTypeReferenceNode(param.type) &&
|
|
48
48
|
ts.isIdentifier(param.type.typeName) &&
|
|
49
49
|
param.type.typeName.text === 'ReactiveArray') {
|
|
50
|
-
ctx.bindings.set(param.name.text,
|
|
50
|
+
ctx.bindings.set(param.name.text, COMPILER_TYPES.Array);
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
}
|
|
@@ -55,7 +55,7 @@ function visit(ctx, node) {
|
|
|
55
55
|
node.name.text === 'length' &&
|
|
56
56
|
!isAssignmentTarget(node)) {
|
|
57
57
|
let name = getExpressionName(node.expression);
|
|
58
|
-
if (name && ctx.bindings.get(name) ===
|
|
58
|
+
if (name && ctx.bindings.get(name) === COMPILER_TYPES.Array) {
|
|
59
59
|
let objText = node.expression.getText(ctx.sourceFile);
|
|
60
60
|
ctx.replacements.push({
|
|
61
61
|
end: node.end,
|
|
@@ -68,7 +68,7 @@ function visit(ctx, node) {
|
|
|
68
68
|
node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
|
69
69
|
ts.isElementAccessExpression(node.left)) {
|
|
70
70
|
let elemAccess = node.left, objName = getExpressionName(elemAccess.expression);
|
|
71
|
-
if (objName && ctx.bindings.get(objName) ===
|
|
71
|
+
if (objName && ctx.bindings.get(objName) === COMPILER_TYPES.Array) {
|
|
72
72
|
let indexText = elemAccess.argumentExpression.getText(ctx.sourceFile), objText = elemAccess.expression.getText(ctx.sourceFile), valueText = node.right.getText(ctx.sourceFile);
|
|
73
73
|
ctx.replacements.push({
|
|
74
74
|
end: node.end,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
2
|
import { code as c } from '@esportsplus/typescript/transformer';
|
|
3
|
-
import {
|
|
3
|
+
import { COMPILER_TYPES, PACKAGE } from '../../constants.js';
|
|
4
4
|
function analyzeProperty(prop, sourceFile) {
|
|
5
5
|
if (!ts.isPropertyAssignment(prop)) {
|
|
6
6
|
return null;
|
|
@@ -14,7 +14,7 @@ function analyzeProperty(prop, sourceFile) {
|
|
|
14
14
|
}
|
|
15
15
|
let value = prop.initializer, valueText = value.getText(sourceFile);
|
|
16
16
|
if (ts.isArrowFunction(value) || ts.isFunctionExpression(value)) {
|
|
17
|
-
return { key, type:
|
|
17
|
+
return { key, type: COMPILER_TYPES.Computed, valueText };
|
|
18
18
|
}
|
|
19
19
|
if (ts.isArrayLiteralExpression(value)) {
|
|
20
20
|
let elements = value.elements, elementsText = '';
|
|
@@ -24,26 +24,26 @@ function analyzeProperty(prop, sourceFile) {
|
|
|
24
24
|
}
|
|
25
25
|
elementsText += elements[i].getText(sourceFile);
|
|
26
26
|
}
|
|
27
|
-
return { key, type:
|
|
27
|
+
return { key, type: COMPILER_TYPES.Array, valueText: elementsText };
|
|
28
28
|
}
|
|
29
|
-
return { key, type:
|
|
29
|
+
return { key, type: COMPILER_TYPES.Signal, valueText };
|
|
30
30
|
}
|
|
31
31
|
function buildClassCode(className, properties, ns) {
|
|
32
32
|
let accessors = [], disposeStatements = [], fields = [], paramCounter = 0;
|
|
33
33
|
fields.push(`[${ns}.REACTIVE_OBJECT] = true;`);
|
|
34
34
|
for (let i = 0, n = properties.length; i < n; i++) {
|
|
35
35
|
let { key, type, valueText } = properties[i];
|
|
36
|
-
if (type ===
|
|
36
|
+
if (type === COMPILER_TYPES.Signal) {
|
|
37
37
|
let param = `_v${paramCounter++}`;
|
|
38
38
|
fields.push(`#${key} = ${ns}.signal(${valueText});`);
|
|
39
39
|
accessors.push(`get ${key}() { return ${ns}.read(this.#${key}); }`);
|
|
40
40
|
accessors.push(`set ${key}(${param}) { ${ns}.write(this.#${key}, ${param}); }`);
|
|
41
41
|
}
|
|
42
|
-
else if (type ===
|
|
42
|
+
else if (type === COMPILER_TYPES.Array) {
|
|
43
43
|
fields.push(`${key} = new ${ns}.ReactiveArray(${valueText});`);
|
|
44
44
|
disposeStatements.push(`this.${key}.dispose();`);
|
|
45
45
|
}
|
|
46
|
-
else if (type ===
|
|
46
|
+
else if (type === COMPILER_TYPES.Computed) {
|
|
47
47
|
fields.push(`#${key} = null;`);
|
|
48
48
|
accessors.push(`get ${key}() { return ${ns}.read(this.#${key} ??= ${ns}.computed(${valueText})); }`);
|
|
49
49
|
disposeStatements.push(`if (this.#${key}) ${ns}.dispose(this.#${key});`);
|
|
@@ -86,7 +86,7 @@ function visit(ctx, node) {
|
|
|
86
86
|
let properties = [], props = arg.properties, varName = null;
|
|
87
87
|
if (node.parent && ts.isVariableDeclaration(node.parent) && ts.isIdentifier(node.parent.name)) {
|
|
88
88
|
varName = node.parent.name.text;
|
|
89
|
-
ctx.bindings.set(varName,
|
|
89
|
+
ctx.bindings.set(varName, COMPILER_TYPES.Object);
|
|
90
90
|
}
|
|
91
91
|
for (let i = 0, n = props.length; i < n; i++) {
|
|
92
92
|
let prop = props[i];
|
|
@@ -100,8 +100,8 @@ function visit(ctx, node) {
|
|
|
100
100
|
return;
|
|
101
101
|
}
|
|
102
102
|
properties.push(analyzed);
|
|
103
|
-
if (analyzed.type ===
|
|
104
|
-
ctx.bindings.set(`${varName}.${analyzed.key}`,
|
|
103
|
+
if (analyzed.type === COMPILER_TYPES.Array && varName) {
|
|
104
|
+
ctx.bindings.set(`${varName}.${analyzed.key}`, COMPILER_TYPES.Array);
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
ctx.calls.push({
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
2
|
import { code as c } from '@esportsplus/typescript/transformer';
|
|
3
|
-
import {
|
|
3
|
+
import { COMPILER_ENTRYPOINT, COMPILER_TYPES, PACKAGE } from '../../constants.js';
|
|
4
4
|
let COMPOUND_OPERATORS = new Map([
|
|
5
5
|
[ts.SyntaxKind.AmpersandAmpersandEqualsToken, '&&'],
|
|
6
6
|
[ts.SyntaxKind.AmpersandEqualsToken, '&'],
|
|
@@ -20,12 +20,12 @@ let COMPOUND_OPERATORS = new Map([
|
|
|
20
20
|
]);
|
|
21
21
|
function classifyReactiveArg(arg) {
|
|
22
22
|
if (ts.isArrowFunction(arg) || ts.isFunctionExpression(arg)) {
|
|
23
|
-
return
|
|
23
|
+
return COMPILER_TYPES.Computed;
|
|
24
24
|
}
|
|
25
25
|
if (ts.isObjectLiteralExpression(arg) || ts.isArrayLiteralExpression(arg)) {
|
|
26
26
|
return null;
|
|
27
27
|
}
|
|
28
|
-
return
|
|
28
|
+
return COMPILER_TYPES.Signal;
|
|
29
29
|
}
|
|
30
30
|
function findBinding(bindings, name, node) {
|
|
31
31
|
for (let i = 0, n = bindings.length; i < n; i++) {
|
|
@@ -143,7 +143,7 @@ function visit(ctx, node) {
|
|
|
143
143
|
ctx.scopedBindings.push({ name: varName, scope, type: classification });
|
|
144
144
|
ctx.bindings.set(varName, classification);
|
|
145
145
|
}
|
|
146
|
-
if (classification ===
|
|
146
|
+
if (classification === COMPILER_TYPES.Computed) {
|
|
147
147
|
let argStart = arg.getStart(ctx.sourceFile);
|
|
148
148
|
ctx.computedArgRanges.push({ end: arg.end, start: argStart });
|
|
149
149
|
let argCtx = {
|
|
@@ -186,7 +186,7 @@ function visit(ctx, node) {
|
|
|
186
186
|
!(ts.isTypeOfExpression(node.parent) && node.parent.expression === node)) {
|
|
187
187
|
let writeCtx = isWriteContext(node);
|
|
188
188
|
if (writeCtx) {
|
|
189
|
-
if (binding.type !==
|
|
189
|
+
if (binding.type !== COMPILER_TYPES.Computed) {
|
|
190
190
|
let parent = node.parent;
|
|
191
191
|
if (writeCtx === 'simple' && ts.isBinaryExpression(parent)) {
|
|
192
192
|
ctx.replacements.push({
|
package/build/types.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
|
-
import { COMPUTED, SIGNAL, STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE, STATE_RECOMPUTING } from './constants.js';
|
|
2
|
+
import { COMPILER_TYPES, COMPUTED, SIGNAL, STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE, STATE_RECOMPUTING } from './constants.js';
|
|
3
3
|
import { ReactiveArray } from './reactive/index.js';
|
|
4
|
-
type
|
|
5
|
-
type Bindings = Map<string, BindingType>;
|
|
4
|
+
type Bindings = Map<string, COMPILER_TYPES>;
|
|
6
5
|
interface Computed<T> {
|
|
7
6
|
cleanup: VoidFunction | VoidFunction[] | null;
|
|
8
7
|
deps: Link | null;
|
|
@@ -46,4 +45,4 @@ interface TransformResult {
|
|
|
46
45
|
sourceFile: ts.SourceFile;
|
|
47
46
|
transformed: boolean;
|
|
48
47
|
}
|
|
49
|
-
export type {
|
|
48
|
+
export type { Bindings, Computed, Link, Reactive, Signal, TransformResult };
|
package/package.json
CHANGED
package/src/constants.ts
CHANGED
|
@@ -7,11 +7,12 @@ const COMPILER_ENTRYPOINT_REGEX = /\breactive\b/;
|
|
|
7
7
|
|
|
8
8
|
const COMPILER_NAMESPACE = uid(COMPILER_ENTRYPOINT);
|
|
9
9
|
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
const enum COMPILER_TYPES {
|
|
11
|
+
Array,
|
|
12
|
+
Computed,
|
|
13
|
+
Object,
|
|
14
|
+
Signal
|
|
15
|
+
}
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
const COMPUTED = Symbol('reactivity.computed');
|
|
@@ -48,7 +49,7 @@ const STATE_NOTIFY_MASK = (STATE_CHECK | STATE_DIRTY);
|
|
|
48
49
|
|
|
49
50
|
|
|
50
51
|
export {
|
|
51
|
-
|
|
52
|
+
COMPILER_ENTRYPOINT, COMPILER_ENTRYPOINT_REGEX, COMPILER_NAMESPACE, COMPILER_TYPES, COMPUTED,
|
|
52
53
|
PACKAGE,
|
|
53
54
|
REACTIVE_ARRAY, REACTIVE_OBJECT,
|
|
54
55
|
SIGNAL,
|
package/src/transformer/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { code as c, type Replacement } from '@esportsplus/typescript/transformer';
|
|
2
2
|
import { ts } from '@esportsplus/typescript';
|
|
3
|
-
import {
|
|
3
|
+
import { COMPILER_TYPES } from '~/constants';
|
|
4
4
|
import type { Bindings } from '~/types';
|
|
5
5
|
|
|
6
6
|
|
|
@@ -45,15 +45,15 @@ function isAssignmentTarget(node: ts.Node): boolean {
|
|
|
45
45
|
|
|
46
46
|
function visit(ctx: { bindings: Bindings, replacements: Replacement[], sourceFile: ts.SourceFile }, node: ts.Node): void {
|
|
47
47
|
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer) {
|
|
48
|
-
if (ts.isIdentifier(node.initializer) && ctx.bindings.get(node.initializer.text) ===
|
|
49
|
-
ctx.bindings.set(node.name.text,
|
|
48
|
+
if (ts.isIdentifier(node.initializer) && ctx.bindings.get(node.initializer.text) === COMPILER_TYPES.Array) {
|
|
49
|
+
ctx.bindings.set(node.name.text, COMPILER_TYPES.Array);
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
if (ts.isPropertyAccessExpression(node.initializer)) {
|
|
53
53
|
let path = getPropertyPath(node.initializer);
|
|
54
54
|
|
|
55
|
-
if (path && ctx.bindings.get(path) ===
|
|
56
|
-
ctx.bindings.set(node.name.text,
|
|
55
|
+
if (path && ctx.bindings.get(path) === COMPILER_TYPES.Array) {
|
|
56
|
+
ctx.bindings.set(node.name.text, COMPILER_TYPES.Array);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
}
|
|
@@ -68,7 +68,7 @@ function visit(ctx: { bindings: Bindings, replacements: Replacement[], sourceFil
|
|
|
68
68
|
ts.isIdentifier(param.type.typeName) &&
|
|
69
69
|
param.type.typeName.text === 'ReactiveArray'
|
|
70
70
|
) {
|
|
71
|
-
ctx.bindings.set(param.name.text,
|
|
71
|
+
ctx.bindings.set(param.name.text, COMPILER_TYPES.Array);
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
}
|
|
@@ -80,7 +80,7 @@ function visit(ctx: { bindings: Bindings, replacements: Replacement[], sourceFil
|
|
|
80
80
|
) {
|
|
81
81
|
let name = getExpressionName(node.expression);
|
|
82
82
|
|
|
83
|
-
if (name && ctx.bindings.get(name) ===
|
|
83
|
+
if (name && ctx.bindings.get(name) === COMPILER_TYPES.Array) {
|
|
84
84
|
let objText = node.expression.getText(ctx.sourceFile);
|
|
85
85
|
|
|
86
86
|
ctx.replacements.push({
|
|
@@ -99,7 +99,7 @@ function visit(ctx: { bindings: Bindings, replacements: Replacement[], sourceFil
|
|
|
99
99
|
let elemAccess = node.left,
|
|
100
100
|
objName = getExpressionName(elemAccess.expression);
|
|
101
101
|
|
|
102
|
-
if (objName && ctx.bindings.get(objName) ===
|
|
102
|
+
if (objName && ctx.bindings.get(objName) === COMPILER_TYPES.Array) {
|
|
103
103
|
let indexText = elemAccess.argumentExpression.getText(ctx.sourceFile),
|
|
104
104
|
objText = elemAccess.expression.getText(ctx.sourceFile),
|
|
105
105
|
valueText = node.right.getText(ctx.sourceFile);
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
2
|
import { code as c, type Replacement } from '@esportsplus/typescript/transformer';
|
|
3
|
-
import {
|
|
3
|
+
import { COMPILER_TYPES, PACKAGE } from '~/constants';
|
|
4
4
|
import type { Bindings } from '~/types';
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
interface AnalyzedProperty {
|
|
8
8
|
key: string;
|
|
9
|
-
type:
|
|
9
|
+
type: COMPILER_TYPES;
|
|
10
10
|
valueText: string;
|
|
11
11
|
}
|
|
12
12
|
|
|
@@ -47,7 +47,7 @@ function analyzeProperty(prop: ts.ObjectLiteralElementLike, sourceFile: ts.Sourc
|
|
|
47
47
|
valueText = value.getText(sourceFile);
|
|
48
48
|
|
|
49
49
|
if (ts.isArrowFunction(value) || ts.isFunctionExpression(value)) {
|
|
50
|
-
return { key, type:
|
|
50
|
+
return { key, type: COMPILER_TYPES.Computed, valueText };
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
if (ts.isArrayLiteralExpression(value)) {
|
|
@@ -62,10 +62,10 @@ function analyzeProperty(prop: ts.ObjectLiteralElementLike, sourceFile: ts.Sourc
|
|
|
62
62
|
elementsText += elements[i].getText(sourceFile);
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
return { key, type:
|
|
65
|
+
return { key, type: COMPILER_TYPES.Array, valueText: elementsText };
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
return { key, type:
|
|
68
|
+
return { key, type: COMPILER_TYPES.Signal, valueText };
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
function buildClassCode(className: string, properties: AnalyzedProperty[], ns: string): string {
|
|
@@ -79,18 +79,18 @@ function buildClassCode(className: string, properties: AnalyzedProperty[], ns: s
|
|
|
79
79
|
for (let i = 0, n = properties.length; i < n; i++) {
|
|
80
80
|
let { key, type, valueText } = properties[i];
|
|
81
81
|
|
|
82
|
-
if (type ===
|
|
82
|
+
if (type === COMPILER_TYPES.Signal) {
|
|
83
83
|
let param = `_v${paramCounter++}`;
|
|
84
84
|
|
|
85
85
|
fields.push(`#${key} = ${ns}.signal(${valueText});`);
|
|
86
86
|
accessors.push(`get ${key}() { return ${ns}.read(this.#${key}); }`);
|
|
87
87
|
accessors.push(`set ${key}(${param}) { ${ns}.write(this.#${key}, ${param}); }`);
|
|
88
88
|
}
|
|
89
|
-
else if (type ===
|
|
89
|
+
else if (type === COMPILER_TYPES.Array) {
|
|
90
90
|
fields.push(`${key} = new ${ns}.ReactiveArray(${valueText});`);
|
|
91
91
|
disposeStatements.push(`this.${key}.dispose();`);
|
|
92
92
|
}
|
|
93
|
-
else if (type ===
|
|
93
|
+
else if (type === COMPILER_TYPES.Computed) {
|
|
94
94
|
fields.push(`#${key} = null;`);
|
|
95
95
|
accessors.push(`get ${key}() { return ${ns}.read(this.#${key} ??= ${ns}.computed(${valueText})); }`);
|
|
96
96
|
disposeStatements.push(`if (this.#${key}) ${ns}.dispose(this.#${key});`);
|
|
@@ -147,7 +147,7 @@ function visit(ctx: TransformContext, node: ts.Node): void {
|
|
|
147
147
|
|
|
148
148
|
if (node.parent && ts.isVariableDeclaration(node.parent) && ts.isIdentifier(node.parent.name)) {
|
|
149
149
|
varName = node.parent.name.text;
|
|
150
|
-
ctx.bindings.set(varName,
|
|
150
|
+
ctx.bindings.set(varName, COMPILER_TYPES.Object);
|
|
151
151
|
}
|
|
152
152
|
|
|
153
153
|
for (let i = 0, n = props.length; i < n; i++) {
|
|
@@ -167,8 +167,8 @@ function visit(ctx: TransformContext, node: ts.Node): void {
|
|
|
167
167
|
|
|
168
168
|
properties.push(analyzed);
|
|
169
169
|
|
|
170
|
-
if (analyzed.type ===
|
|
171
|
-
ctx.bindings.set(`${varName}.${analyzed.key}`,
|
|
170
|
+
if (analyzed.type === COMPILER_TYPES.Array && varName) {
|
|
171
|
+
ctx.bindings.set(`${varName}.${analyzed.key}`, COMPILER_TYPES.Array);
|
|
172
172
|
}
|
|
173
173
|
}
|
|
174
174
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
2
|
import { code as c, type Range, type Replacement } from '@esportsplus/typescript/transformer';
|
|
3
|
-
import type {
|
|
4
|
-
import {
|
|
3
|
+
import type { Bindings } from '~/types';
|
|
4
|
+
import { COMPILER_ENTRYPOINT, COMPILER_TYPES, PACKAGE } from '~/constants';
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
interface ArgContext {
|
|
@@ -15,7 +15,7 @@ interface ArgContext {
|
|
|
15
15
|
interface ScopeBinding {
|
|
16
16
|
name: string;
|
|
17
17
|
scope: ts.Node;
|
|
18
|
-
type:
|
|
18
|
+
type: COMPILER_TYPES;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
interface TransformContext {
|
|
@@ -49,16 +49,16 @@ let COMPOUND_OPERATORS = new Map<ts.SyntaxKind, string>([
|
|
|
49
49
|
]);
|
|
50
50
|
|
|
51
51
|
|
|
52
|
-
function classifyReactiveArg(arg: ts.Expression):
|
|
52
|
+
function classifyReactiveArg(arg: ts.Expression): COMPILER_TYPES | null {
|
|
53
53
|
if (ts.isArrowFunction(arg) || ts.isFunctionExpression(arg)) {
|
|
54
|
-
return
|
|
54
|
+
return COMPILER_TYPES.Computed;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
if (ts.isObjectLiteralExpression(arg) || ts.isArrayLiteralExpression(arg)) {
|
|
58
58
|
return null;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
return
|
|
61
|
+
return COMPILER_TYPES.Signal;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
function findBinding(bindings: ScopeBinding[], name: string, node: ts.Node): ScopeBinding | undefined {
|
|
@@ -221,7 +221,7 @@ function visit(ctx: TransformContext, node: ts.Node): void {
|
|
|
221
221
|
ctx.bindings.set(varName, classification);
|
|
222
222
|
}
|
|
223
223
|
|
|
224
|
-
if (classification ===
|
|
224
|
+
if (classification === COMPILER_TYPES.Computed) {
|
|
225
225
|
let argStart = arg.getStart(ctx.sourceFile);
|
|
226
226
|
|
|
227
227
|
ctx.computedArgRanges.push({ end: arg.end, start: argStart });
|
|
@@ -278,7 +278,7 @@ function visit(ctx: TransformContext, node: ts.Node): void {
|
|
|
278
278
|
let writeCtx = isWriteContext(node);
|
|
279
279
|
|
|
280
280
|
if (writeCtx) {
|
|
281
|
-
if (binding.type !==
|
|
281
|
+
if (binding.type !== COMPILER_TYPES.Computed) {
|
|
282
282
|
let parent = node.parent;
|
|
283
283
|
|
|
284
284
|
if (writeCtx === 'simple' && ts.isBinaryExpression(parent)) {
|
package/src/types.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
|
-
import { COMPUTED, SIGNAL, STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE, STATE_RECOMPUTING } from './constants';
|
|
2
|
+
import { COMPILER_TYPES, COMPUTED, SIGNAL, STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE, STATE_RECOMPUTING } from './constants';
|
|
3
3
|
import { ReactiveArray } from './reactive';
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
type
|
|
7
|
-
|
|
8
|
-
type Bindings = Map<string, BindingType>;
|
|
6
|
+
type Bindings = Map<string, COMPILER_TYPES>;
|
|
9
7
|
|
|
10
8
|
interface Computed<T> {
|
|
11
9
|
cleanup: VoidFunction | VoidFunction[] | null;
|
|
@@ -65,7 +63,7 @@ interface TransformResult {
|
|
|
65
63
|
|
|
66
64
|
|
|
67
65
|
export type {
|
|
68
|
-
|
|
66
|
+
Bindings,
|
|
69
67
|
Computed,
|
|
70
68
|
Link,
|
|
71
69
|
Reactive,
|