@esportsplus/reactivity 0.29.4 → 0.29.6
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/compiler/array.d.ts +1 -1
- package/build/compiler/array.js +11 -11
- package/build/compiler/constants.d.ts +11 -0
- package/build/compiler/constants.js +13 -0
- package/build/compiler/index.d.ts +2 -10
- package/build/compiler/index.js +22 -22
- package/build/compiler/object.d.ts +1 -1
- package/build/compiler/object.js +22 -22
- package/build/compiler/plugins/vite.js +1 -1
- package/build/compiler/primitives.d.ts +1 -1
- package/build/compiler/primitives.js +13 -13
- package/build/compiler/types.d.ts +3 -0
- package/build/compiler/types.js +1 -0
- package/build/constants.d.ts +1 -10
- package/build/constants.js +1 -12
- package/build/types.d.ts +2 -3
- package/package.json +1 -1
- package/src/compiler/array.ts +12 -12
- package/src/compiler/constants.ts +20 -0
- package/src/compiler/index.ts +24 -26
- package/src/compiler/object.ts +24 -24
- package/src/compiler/plugins/vite.ts +1 -1
- package/src/compiler/primitives.ts +15 -15
- package/src/compiler/types.ts +7 -0
- package/src/constants.ts +2 -18
- package/src/types.ts +1 -4
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ReplacementIntent } from '@esportsplus/typescript/compiler';
|
|
2
2
|
import { ts } from '@esportsplus/typescript';
|
|
3
|
-
import type { Bindings } from '
|
|
3
|
+
import type { Bindings } from './types.js';
|
|
4
4
|
declare const _default: (sourceFile: ts.SourceFile, bindings: Bindings) => ReplacementIntent[];
|
|
5
5
|
export default _default;
|
package/build/compiler/array.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
2
|
import { ast } from '@esportsplus/typescript/compiler';
|
|
3
|
-
import {
|
|
3
|
+
import { NAMESPACE, TYPES } from './constants.js';
|
|
4
4
|
function getElementTypeText(typeNode, sourceFile) {
|
|
5
5
|
if (ts.isArrayTypeNode(typeNode)) {
|
|
6
6
|
return typeNode.elementType.getText(sourceFile);
|
|
@@ -29,25 +29,25 @@ function visit(ctx, node) {
|
|
|
29
29
|
elementType = getElementTypeText(node.parent.type, ctx.sourceFile);
|
|
30
30
|
}
|
|
31
31
|
if (node.parent && ts.isVariableDeclaration(node.parent) && ts.isIdentifier(node.parent.name)) {
|
|
32
|
-
ctx.bindings.set(node.parent.name.text,
|
|
32
|
+
ctx.bindings.set(node.parent.name.text, TYPES.Array);
|
|
33
33
|
}
|
|
34
34
|
let typeParam = elementType ? `<${elementType}>` : '';
|
|
35
35
|
ctx.replacements.push({
|
|
36
36
|
node,
|
|
37
37
|
generate: (sf) => expression.elements.length > 0
|
|
38
|
-
? ` new ${
|
|
39
|
-
: ` new ${
|
|
38
|
+
? ` new ${NAMESPACE}.ReactiveArray${typeParam}(...${expression.getText(sf)})`
|
|
39
|
+
: ` new ${NAMESPACE}.ReactiveArray${typeParam}()`
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer) {
|
|
44
|
-
if (ts.isIdentifier(node.initializer) && ctx.bindings.get(node.initializer.text) ===
|
|
45
|
-
ctx.bindings.set(node.name.text,
|
|
44
|
+
if (ts.isIdentifier(node.initializer) && ctx.bindings.get(node.initializer.text) === TYPES.Array) {
|
|
45
|
+
ctx.bindings.set(node.name.text, TYPES.Array);
|
|
46
46
|
}
|
|
47
47
|
if (ts.isPropertyAccessExpression(node.initializer)) {
|
|
48
48
|
let path = ast.property.path(node.initializer);
|
|
49
|
-
if (path && ctx.bindings.get(path) ===
|
|
50
|
-
ctx.bindings.set(node.name.text,
|
|
49
|
+
if (path && ctx.bindings.get(path) === TYPES.Array) {
|
|
50
|
+
ctx.bindings.set(node.name.text, TYPES.Array);
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
}
|
|
@@ -58,7 +58,7 @@ function visit(ctx, node) {
|
|
|
58
58
|
ts.isTypeReferenceNode(param.type) &&
|
|
59
59
|
ts.isIdentifier(param.type.typeName) &&
|
|
60
60
|
param.type.typeName.text === 'ReactiveArray') {
|
|
61
|
-
ctx.bindings.set(param.name.text,
|
|
61
|
+
ctx.bindings.set(param.name.text, TYPES.Array);
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
}
|
|
@@ -69,7 +69,7 @@ function visit(ctx, node) {
|
|
|
69
69
|
!ts.isPostfixUnaryExpression(node.parent) &&
|
|
70
70
|
!ts.isPrefixUnaryExpression(node.parent)))) {
|
|
71
71
|
let name = ast.expression.name(node.expression);
|
|
72
|
-
if (name && ctx.bindings.get(name) ===
|
|
72
|
+
if (name && ctx.bindings.get(name) === TYPES.Array) {
|
|
73
73
|
ctx.replacements.push({
|
|
74
74
|
node,
|
|
75
75
|
generate: (sf) => `${node.expression.getText(sf)}.$length()`
|
|
@@ -80,7 +80,7 @@ function visit(ctx, node) {
|
|
|
80
80
|
node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
|
81
81
|
ts.isElementAccessExpression(node.left)) {
|
|
82
82
|
let element = node.left, name = ast.expression.name(element.expression);
|
|
83
|
-
if (name && ctx.bindings.get(name) ===
|
|
83
|
+
if (name && ctx.bindings.get(name) === TYPES.Array) {
|
|
84
84
|
ctx.replacements.push({
|
|
85
85
|
node,
|
|
86
86
|
generate: (sf) => `${element.expression.getText(sf)}.$set(
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare const ENTRYPOINT = "reactive";
|
|
2
|
+
declare const ENTRYPOINT_REGEX: RegExp;
|
|
3
|
+
declare const NAMESPACE: string;
|
|
4
|
+
declare const enum TYPES {
|
|
5
|
+
Array = 0,
|
|
6
|
+
Computed = 1,
|
|
7
|
+
Object = 2,
|
|
8
|
+
Signal = 3
|
|
9
|
+
}
|
|
10
|
+
export { ENTRYPOINT, ENTRYPOINT_REGEX, NAMESPACE, TYPES };
|
|
11
|
+
export { PACKAGE } from '../constants.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { uid } from '@esportsplus/typescript/compiler';
|
|
2
|
+
const ENTRYPOINT = 'reactive';
|
|
3
|
+
const ENTRYPOINT_REGEX = /\breactive\b/;
|
|
4
|
+
const NAMESPACE = uid('reactivity');
|
|
5
|
+
var TYPES;
|
|
6
|
+
(function (TYPES) {
|
|
7
|
+
TYPES[TYPES["Array"] = 0] = "Array";
|
|
8
|
+
TYPES[TYPES["Computed"] = 1] = "Computed";
|
|
9
|
+
TYPES[TYPES["Object"] = 2] = "Object";
|
|
10
|
+
TYPES[TYPES["Signal"] = 3] = "Signal";
|
|
11
|
+
})(TYPES || (TYPES = {}));
|
|
12
|
+
export { ENTRYPOINT, ENTRYPOINT_REGEX, NAMESPACE, TYPES };
|
|
13
|
+
export { PACKAGE } from '../constants.js';
|
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { TransformContext } from '@esportsplus/typescript/compiler';
|
|
2
2
|
declare const _default: {
|
|
3
3
|
patterns: string[];
|
|
4
|
-
transform: (ctx: TransformContext) => {
|
|
5
|
-
imports?: undefined;
|
|
6
|
-
prepend?: undefined;
|
|
7
|
-
replacements?: undefined;
|
|
8
|
-
} | {
|
|
9
|
-
imports: ImportIntent[];
|
|
10
|
-
prepend: string[];
|
|
11
|
-
replacements: ReplacementIntent[];
|
|
12
|
-
};
|
|
4
|
+
transform: (ctx: TransformContext) => {};
|
|
13
5
|
};
|
|
14
6
|
export default _default;
|
package/build/compiler/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
2
|
import { imports } from '@esportsplus/typescript/compiler';
|
|
3
|
-
import {
|
|
3
|
+
import { ENTRYPOINT, NAMESPACE, PACKAGE } from './constants.js';
|
|
4
4
|
import array from './array.js';
|
|
5
5
|
import object from './object.js';
|
|
6
6
|
import primitives from './primitives.js';
|
|
@@ -15,14 +15,14 @@ function isReactiveCallExpression(checker, node) {
|
|
|
15
15
|
}
|
|
16
16
|
let expr = node.expression;
|
|
17
17
|
if (ts.isIdentifier(expr)) {
|
|
18
|
-
if (expr.text ===
|
|
18
|
+
if (expr.text === ENTRYPOINT) {
|
|
19
19
|
return true;
|
|
20
20
|
}
|
|
21
21
|
if (checker) {
|
|
22
|
-
return imports.includes(checker, expr, PACKAGE,
|
|
22
|
+
return imports.includes(checker, expr, PACKAGE, ENTRYPOINT);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
if (ts.isPropertyAccessExpression(expr) && expr.name.text ===
|
|
25
|
+
if (ts.isPropertyAccessExpression(expr) && expr.name.text === ENTRYPOINT && checker) {
|
|
26
26
|
return imports.includes(checker, expr, PACKAGE);
|
|
27
27
|
}
|
|
28
28
|
return false;
|
|
@@ -30,7 +30,7 @@ function isReactiveCallExpression(checker, node) {
|
|
|
30
30
|
function visit(ctx, node) {
|
|
31
31
|
if (isReactiveCallExpression(ctx.checker, node) && !ctx.transformedNodes.has(node) && !ctx.transformedNodes.has(node.expression)) {
|
|
32
32
|
ctx.replacements.push({
|
|
33
|
-
generate: () => `${
|
|
33
|
+
generate: () => `${NAMESPACE}.reactive(${node.arguments.map(a => a.getText(ctx.sourceFile)).join(', ')})`,
|
|
34
34
|
node
|
|
35
35
|
});
|
|
36
36
|
}
|
|
@@ -39,27 +39,27 @@ function visit(ctx, node) {
|
|
|
39
39
|
export default {
|
|
40
40
|
patterns: ['reactive(', 'reactive<'],
|
|
41
41
|
transform: (ctx) => {
|
|
42
|
-
if (!imports.all(ctx.sourceFile, PACKAGE).some(i => i.specifiers.has(
|
|
42
|
+
if (!imports.all(ctx.sourceFile, PACKAGE).some(i => i.specifiers.has(ENTRYPOINT))) {
|
|
43
43
|
return {};
|
|
44
44
|
}
|
|
45
|
-
let bindings = new Map(),
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
replacements.push(...
|
|
51
|
-
replacements
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
45
|
+
let bindings = new Map(), intents = {
|
|
46
|
+
imports: [],
|
|
47
|
+
prepend: [],
|
|
48
|
+
replacements: []
|
|
49
|
+
};
|
|
50
|
+
intents.replacements.push(...primitives(ctx.sourceFile, bindings, (node) => isReactiveCallExpression(ctx.checker, node)));
|
|
51
|
+
let { prepend, replacements } = object(ctx.sourceFile, bindings);
|
|
52
|
+
intents.prepend.push(...prepend);
|
|
53
|
+
intents.replacements.push(...replacements);
|
|
54
|
+
intents.replacements.push(...array(ctx.sourceFile, bindings));
|
|
55
|
+
intents.replacements.push(...findRemainingCalls(ctx.checker, ctx.sourceFile, new Set(intents.replacements.map(r => r.node))));
|
|
56
|
+
if (intents.replacements.length > 0 || intents.prepend.length > 0) {
|
|
57
|
+
intents.imports.push({
|
|
58
|
+
namespace: NAMESPACE,
|
|
55
59
|
package: PACKAGE,
|
|
56
|
-
remove: [
|
|
60
|
+
remove: [ENTRYPOINT]
|
|
57
61
|
});
|
|
58
62
|
}
|
|
59
|
-
return
|
|
60
|
-
imports: importsIntent,
|
|
61
|
-
prepend,
|
|
62
|
-
replacements
|
|
63
|
-
};
|
|
63
|
+
return intents;
|
|
64
64
|
}
|
|
65
65
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ReplacementIntent } from '@esportsplus/typescript/compiler';
|
|
2
2
|
import { ts } from '@esportsplus/typescript';
|
|
3
|
-
import type { Bindings } from '
|
|
3
|
+
import type { Bindings } from './types.js';
|
|
4
4
|
type ObjectTransformResult = {
|
|
5
5
|
prepend: string[];
|
|
6
6
|
replacements: ReplacementIntent[];
|
package/build/compiler/object.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
2
|
import { uid } from '@esportsplus/typescript/compiler';
|
|
3
|
-
import {
|
|
3
|
+
import { NAMESPACE, TYPES } from './constants.js';
|
|
4
4
|
function analyzeProperty(prop, sourceFile) {
|
|
5
5
|
if (!ts.isPropertyAssignment(prop)) {
|
|
6
6
|
return null;
|
|
@@ -17,7 +17,7 @@ function analyzeProperty(prop, sourceFile) {
|
|
|
17
17
|
unwrapped = unwrapped.expression;
|
|
18
18
|
}
|
|
19
19
|
if (ts.isArrowFunction(unwrapped) || ts.isFunctionExpression(unwrapped)) {
|
|
20
|
-
return { isStatic: false, key, type:
|
|
20
|
+
return { isStatic: false, key, type: TYPES.Computed, valueText };
|
|
21
21
|
}
|
|
22
22
|
if (ts.isArrayLiteralExpression(unwrapped)) {
|
|
23
23
|
let elements = unwrapped.elements, isStatic = value === unwrapped;
|
|
@@ -26,67 +26,67 @@ function analyzeProperty(prop, sourceFile) {
|
|
|
26
26
|
isStatic = false;
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
-
return { isStatic, key, type:
|
|
29
|
+
return { isStatic, key, type: TYPES.Array, valueText };
|
|
30
30
|
}
|
|
31
|
-
return { isStatic: isStaticValue(value), key, type:
|
|
31
|
+
return { isStatic: isStaticValue(value), key, type: TYPES.Signal, valueText };
|
|
32
32
|
}
|
|
33
33
|
function buildClassCode(className, properties) {
|
|
34
34
|
let accessors = [], body = [], fields = [], generics = [], parameters = [], setters = 0;
|
|
35
35
|
for (let i = 0, n = properties.length; i < n; i++) {
|
|
36
36
|
let { isStatic, key, type, valueText } = properties[i], generic = `T${parameters.length}`, parameter = `_p${parameters.length}`;
|
|
37
|
-
if (type ===
|
|
37
|
+
if (type === TYPES.Signal) {
|
|
38
38
|
let value = `_v${setters++}`;
|
|
39
39
|
if (isStatic) {
|
|
40
40
|
accessors.push(`
|
|
41
41
|
get ${key}() {
|
|
42
|
-
return ${
|
|
42
|
+
return ${NAMESPACE}.read(this.#${key});
|
|
43
43
|
}
|
|
44
44
|
set ${key}(${value}) {
|
|
45
|
-
${
|
|
45
|
+
${NAMESPACE}.write(this.#${key}, ${value});
|
|
46
46
|
}
|
|
47
47
|
`);
|
|
48
|
-
fields.push(`#${key} = this[${
|
|
48
|
+
fields.push(`#${key} = this[${NAMESPACE}.SIGNAL](${valueText});`);
|
|
49
49
|
}
|
|
50
50
|
else {
|
|
51
51
|
accessors.push(`
|
|
52
52
|
get ${key}() {
|
|
53
|
-
return ${
|
|
53
|
+
return ${NAMESPACE}.read(this.#${key}) as ${generic};
|
|
54
54
|
}
|
|
55
55
|
set ${key}(${value}) {
|
|
56
|
-
${
|
|
56
|
+
${NAMESPACE}.write(this.#${key}, ${value});
|
|
57
57
|
}
|
|
58
58
|
`);
|
|
59
|
-
body.push(`this.#${key} = this[${
|
|
59
|
+
body.push(`this.#${key} = this[${NAMESPACE}.SIGNAL](${parameter});`);
|
|
60
60
|
fields.push(`#${key};`);
|
|
61
61
|
generics.push(generic);
|
|
62
62
|
parameters.push(`${parameter}: ${generic}`);
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
|
-
else if (type ===
|
|
65
|
+
else if (type === TYPES.Array) {
|
|
66
66
|
accessors.push(`
|
|
67
67
|
get ${key}() {
|
|
68
68
|
return this.#${key};
|
|
69
69
|
}
|
|
70
70
|
`);
|
|
71
|
-
body.push(`this.#${key} = this[${
|
|
71
|
+
body.push(`this.#${key} = this[${NAMESPACE}.REACTIVE_ARRAY](${parameter});`);
|
|
72
72
|
fields.push(`#${key};`);
|
|
73
73
|
generics.push(`${generic} extends unknown[]`);
|
|
74
74
|
parameters.push(`${parameter}: ${generic}`);
|
|
75
75
|
}
|
|
76
|
-
else if (type ===
|
|
76
|
+
else if (type === TYPES.Computed) {
|
|
77
77
|
accessors.push(`
|
|
78
78
|
get ${key}() {
|
|
79
|
-
return ${
|
|
79
|
+
return ${NAMESPACE}.read(this.#${key});
|
|
80
80
|
}
|
|
81
81
|
`);
|
|
82
|
-
body.push(`this.#${key} = this[${
|
|
82
|
+
body.push(`this.#${key} = this[${NAMESPACE}.COMPUTED](${parameter});`);
|
|
83
83
|
fields.push(`#${key};`);
|
|
84
|
-
generics.push(`${generic} extends ${
|
|
84
|
+
generics.push(`${generic} extends ${NAMESPACE}.Computed<ReturnType<${generic}>>['fn']`);
|
|
85
85
|
parameters.push(`${parameter}: ${generic}`);
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
return `
|
|
89
|
-
class ${className}${generics.length > 0 ? `<${generics.join(', ')}>` : ''} extends ${
|
|
89
|
+
class ${className}${generics.length > 0 ? `<${generics.join(', ')}>` : ''} extends ${NAMESPACE}.ReactiveObject<any> {
|
|
90
90
|
${fields.join('\n')}
|
|
91
91
|
constructor(${parameters.join(', ')}) {
|
|
92
92
|
super(null);
|
|
@@ -117,7 +117,7 @@ function visit(ctx, node) {
|
|
|
117
117
|
let properties = [], props = arg.properties, varname = null;
|
|
118
118
|
if (node.parent && ts.isVariableDeclaration(node.parent) && ts.isIdentifier(node.parent.name)) {
|
|
119
119
|
varname = node.parent.name.text;
|
|
120
|
-
ctx.bindings.set(varname,
|
|
120
|
+
ctx.bindings.set(varname, TYPES.Object);
|
|
121
121
|
}
|
|
122
122
|
for (let i = 0, n = props.length; i < n; i++) {
|
|
123
123
|
let prop = props[i];
|
|
@@ -131,8 +131,8 @@ function visit(ctx, node) {
|
|
|
131
131
|
return;
|
|
132
132
|
}
|
|
133
133
|
properties.push(analyzed);
|
|
134
|
-
if (analyzed.type ===
|
|
135
|
-
ctx.bindings.set(`${varname}.${analyzed.key}`,
|
|
134
|
+
if (analyzed.type === TYPES.Array && varname) {
|
|
135
|
+
ctx.bindings.set(`${varname}.${analyzed.key}`, TYPES.Array);
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
138
|
ctx.calls.push({
|
|
@@ -161,7 +161,7 @@ export default (sourceFile, bindings) => {
|
|
|
161
161
|
prepend.push(buildClassCode(call.className, call.properties));
|
|
162
162
|
replacements.push({
|
|
163
163
|
generate: () => ` new ${call.className}(${call.properties
|
|
164
|
-
.filter(({ isStatic, type }) => !isStatic || type ===
|
|
164
|
+
.filter(({ isStatic, type }) => !isStatic || type === TYPES.Computed)
|
|
165
165
|
.map(p => p.valueText)
|
|
166
166
|
.join(', ')})`,
|
|
167
167
|
node: call.node,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ReplacementIntent } from '@esportsplus/typescript/compiler';
|
|
2
2
|
import { ts } from '@esportsplus/typescript';
|
|
3
|
-
import type { Bindings } from '
|
|
3
|
+
import type { Bindings } from './types.js';
|
|
4
4
|
declare const _default: (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: (node: ts.Node) => boolean) => ReplacementIntent[];
|
|
5
5
|
export default _default;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
|
-
import {
|
|
2
|
+
import { NAMESPACE, TYPES } from './constants.js';
|
|
3
3
|
const COMPOUND_OPERATORS = new Map([
|
|
4
4
|
[ts.SyntaxKind.AmpersandAmpersandEqualsToken, '&&'],
|
|
5
5
|
[ts.SyntaxKind.AmpersandEqualsToken, '&'],
|
|
@@ -31,9 +31,9 @@ function visit(ctx, node) {
|
|
|
31
31
|
if (ctx.isReactiveCall(node)) {
|
|
32
32
|
let call = node;
|
|
33
33
|
if (call.arguments.length > 0) {
|
|
34
|
-
let arg = call.arguments[0], classification =
|
|
34
|
+
let arg = call.arguments[0], classification = TYPES.Signal;
|
|
35
35
|
if (ts.isArrowFunction(arg) || ts.isFunctionExpression(arg)) {
|
|
36
|
-
classification =
|
|
36
|
+
classification = TYPES.Computed;
|
|
37
37
|
}
|
|
38
38
|
else if (ts.isArrayLiteralExpression(arg) || ts.isObjectLiteralExpression(arg)) {
|
|
39
39
|
classification = null;
|
|
@@ -71,9 +71,9 @@ function visit(ctx, node) {
|
|
|
71
71
|
ctx.scopedBindings.push({ name: varname, scope, type: classification });
|
|
72
72
|
}
|
|
73
73
|
ctx.replacements.push({
|
|
74
|
-
generate: () => classification ===
|
|
75
|
-
? `${
|
|
76
|
-
: `${
|
|
74
|
+
generate: () => classification === TYPES.Computed
|
|
75
|
+
? `${NAMESPACE}.computed`
|
|
76
|
+
: `${NAMESPACE}.signal`,
|
|
77
77
|
node: call.expression
|
|
78
78
|
});
|
|
79
79
|
}
|
|
@@ -116,18 +116,18 @@ function visit(ctx, node) {
|
|
|
116
116
|
}
|
|
117
117
|
}
|
|
118
118
|
if (writeCtx) {
|
|
119
|
-
if (binding.type !==
|
|
119
|
+
if (binding.type !== TYPES.Computed) {
|
|
120
120
|
if (writeCtx === 'simple' && ts.isBinaryExpression(parent)) {
|
|
121
121
|
let right = parent.right;
|
|
122
122
|
ctx.replacements.push({
|
|
123
|
-
generate: (sf) => `${
|
|
123
|
+
generate: (sf) => `${NAMESPACE}.write(${name}, ${right.getText(sf)})`,
|
|
124
124
|
node: parent
|
|
125
125
|
});
|
|
126
126
|
}
|
|
127
127
|
else if (writeCtx === 'compound' && ts.isBinaryExpression(parent)) {
|
|
128
128
|
let op = COMPOUND_OPERATORS.get(parent.operatorToken.kind) ?? '+', right = parent.right;
|
|
129
129
|
ctx.replacements.push({
|
|
130
|
-
generate: (sf) => `${
|
|
130
|
+
generate: (sf) => `${NAMESPACE}.write(${name}, ${name}.value ${op} ${right.getText(sf)})`,
|
|
131
131
|
node: parent
|
|
132
132
|
});
|
|
133
133
|
}
|
|
@@ -135,20 +135,20 @@ function visit(ctx, node) {
|
|
|
135
135
|
let delta = parent.operator === ts.SyntaxKind.PlusPlusToken ? '+ 1' : '- 1', isPrefix = ts.isPrefixUnaryExpression(parent);
|
|
136
136
|
if (ts.isExpressionStatement(parent.parent)) {
|
|
137
137
|
ctx.replacements.push({
|
|
138
|
-
generate: () => `${
|
|
138
|
+
generate: () => `${NAMESPACE}.write(${name}, ${name}.value ${delta})`,
|
|
139
139
|
node: parent
|
|
140
140
|
});
|
|
141
141
|
}
|
|
142
142
|
else if (isPrefix) {
|
|
143
143
|
ctx.replacements.push({
|
|
144
|
-
generate: () => `(${
|
|
144
|
+
generate: () => `(${NAMESPACE}.write(${name}, ${name}.value ${delta}), ${name}.value)`,
|
|
145
145
|
node: parent
|
|
146
146
|
});
|
|
147
147
|
}
|
|
148
148
|
else {
|
|
149
149
|
let tmp = `_t${ctx.tmpCounter++}`;
|
|
150
150
|
ctx.replacements.push({
|
|
151
|
-
generate: () => `((${tmp}) => (${
|
|
151
|
+
generate: () => `((${tmp}) => (${NAMESPACE}.write(${name}, ${tmp} ${delta}), ${tmp}))(${name}.value)`,
|
|
152
152
|
node: parent
|
|
153
153
|
});
|
|
154
154
|
}
|
|
@@ -157,7 +157,7 @@ function visit(ctx, node) {
|
|
|
157
157
|
}
|
|
158
158
|
else {
|
|
159
159
|
ctx.replacements.push({
|
|
160
|
-
generate: () => `${
|
|
160
|
+
generate: () => `${NAMESPACE}.read(${name})`,
|
|
161
161
|
node
|
|
162
162
|
});
|
|
163
163
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/build/constants.d.ts
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
declare const COMPILER_ENTRYPOINT = "reactive";
|
|
2
|
-
declare const COMPILER_ENTRYPOINT_REGEX: RegExp;
|
|
3
|
-
declare const COMPILER_NAMESPACE: string;
|
|
4
|
-
declare const enum COMPILER_TYPES {
|
|
5
|
-
Array = 0,
|
|
6
|
-
Computed = 1,
|
|
7
|
-
Object = 2,
|
|
8
|
-
Signal = 3
|
|
9
|
-
}
|
|
10
1
|
declare const COMPUTED: unique symbol;
|
|
11
2
|
declare const PACKAGE = "@esportsplus/reactivity";
|
|
12
3
|
declare const REACTIVE_ARRAY: unique symbol;
|
|
@@ -22,4 +13,4 @@ declare const STATE_DIRTY: number;
|
|
|
22
13
|
declare const STATE_RECOMPUTING: number;
|
|
23
14
|
declare const STATE_IN_HEAP: number;
|
|
24
15
|
declare const STATE_NOTIFY_MASK: number;
|
|
25
|
-
export {
|
|
16
|
+
export { COMPUTED, REACTIVE_ARRAY, REACTIVE_OBJECT, PACKAGE, 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
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
import { uid } from '@esportsplus/typescript/compiler';
|
|
2
|
-
const COMPILER_ENTRYPOINT = 'reactive';
|
|
3
|
-
const COMPILER_ENTRYPOINT_REGEX = /\breactive\b/;
|
|
4
|
-
const COMPILER_NAMESPACE = uid('reactivity');
|
|
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 = {}));
|
|
12
1
|
const COMPUTED = Symbol('reactivity.computed');
|
|
13
2
|
const PACKAGE = '@esportsplus/reactivity';
|
|
14
3
|
const REACTIVE_ARRAY = Symbol('reactivity.reactive.array');
|
|
@@ -24,4 +13,4 @@ const STATE_DIRTY = 1 << 1;
|
|
|
24
13
|
const STATE_RECOMPUTING = 1 << 2;
|
|
25
14
|
const STATE_IN_HEAP = 1 << 3;
|
|
26
15
|
const STATE_NOTIFY_MASK = (STATE_CHECK | STATE_DIRTY);
|
|
27
|
-
export {
|
|
16
|
+
export { COMPUTED, REACTIVE_ARRAY, REACTIVE_OBJECT, PACKAGE, 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/types.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
|
-
import {
|
|
2
|
+
import { 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 Bindings = Map<string, COMPILER_TYPES>;
|
|
5
4
|
interface Computed<T> {
|
|
6
5
|
cleanup: VoidFunction | VoidFunction[] | null;
|
|
7
6
|
deps: Link | null;
|
|
@@ -45,4 +44,4 @@ interface TransformResult {
|
|
|
45
44
|
code: string;
|
|
46
45
|
sourceFile: ts.SourceFile;
|
|
47
46
|
}
|
|
48
|
-
export type {
|
|
47
|
+
export type { Computed, Link, Reactive, Signal, TransformResult };
|
package/package.json
CHANGED
package/src/compiler/array.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { ReplacementIntent } from '@esportsplus/typescript/compiler';
|
|
2
2
|
import { ts } from '@esportsplus/typescript';
|
|
3
3
|
import { ast } from '@esportsplus/typescript/compiler';
|
|
4
|
-
import {
|
|
5
|
-
import type { Bindings } from '
|
|
4
|
+
import { NAMESPACE, TYPES } from './constants';
|
|
5
|
+
import type { Bindings } from './types';
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
function getElementTypeText(typeNode: ts.TypeNode, sourceFile: ts.SourceFile): string | null {
|
|
@@ -44,7 +44,7 @@ function visit(ctx: { bindings: Bindings, replacements: ReplacementIntent[], sou
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
if (node.parent && ts.isVariableDeclaration(node.parent) && ts.isIdentifier(node.parent.name)) {
|
|
47
|
-
ctx.bindings.set(node.parent.name.text,
|
|
47
|
+
ctx.bindings.set(node.parent.name.text, TYPES.Array);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
let typeParam = elementType ? `<${elementType}>` : '';
|
|
@@ -52,22 +52,22 @@ function visit(ctx: { bindings: Bindings, replacements: ReplacementIntent[], sou
|
|
|
52
52
|
ctx.replacements.push({
|
|
53
53
|
node,
|
|
54
54
|
generate: (sf) => expression.elements.length > 0
|
|
55
|
-
? ` new ${
|
|
56
|
-
: ` new ${
|
|
55
|
+
? ` new ${NAMESPACE}.ReactiveArray${typeParam}(...${expression.getText(sf)})`
|
|
56
|
+
: ` new ${NAMESPACE}.ReactiveArray${typeParam}()`
|
|
57
57
|
});
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer) {
|
|
62
|
-
if (ts.isIdentifier(node.initializer) && ctx.bindings.get(node.initializer.text) ===
|
|
63
|
-
ctx.bindings.set(node.name.text,
|
|
62
|
+
if (ts.isIdentifier(node.initializer) && ctx.bindings.get(node.initializer.text) === TYPES.Array) {
|
|
63
|
+
ctx.bindings.set(node.name.text, TYPES.Array);
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
if (ts.isPropertyAccessExpression(node.initializer)) {
|
|
67
67
|
let path = ast.property.path(node.initializer);
|
|
68
68
|
|
|
69
|
-
if (path && ctx.bindings.get(path) ===
|
|
70
|
-
ctx.bindings.set(node.name.text,
|
|
69
|
+
if (path && ctx.bindings.get(path) === TYPES.Array) {
|
|
70
|
+
ctx.bindings.set(node.name.text, TYPES.Array);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
}
|
|
@@ -82,7 +82,7 @@ function visit(ctx: { bindings: Bindings, replacements: ReplacementIntent[], sou
|
|
|
82
82
|
ts.isIdentifier(param.type.typeName) &&
|
|
83
83
|
param.type.typeName.text === 'ReactiveArray'
|
|
84
84
|
) {
|
|
85
|
-
ctx.bindings.set(param.name.text,
|
|
85
|
+
ctx.bindings.set(param.name.text, TYPES.Array);
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
}
|
|
@@ -101,7 +101,7 @@ function visit(ctx: { bindings: Bindings, replacements: ReplacementIntent[], sou
|
|
|
101
101
|
) {
|
|
102
102
|
let name = ast.expression.name(node.expression);
|
|
103
103
|
|
|
104
|
-
if (name && ctx.bindings.get(name) ===
|
|
104
|
+
if (name && ctx.bindings.get(name) === TYPES.Array) {
|
|
105
105
|
ctx.replacements.push({
|
|
106
106
|
node,
|
|
107
107
|
generate: (sf) => `${node.expression.getText(sf)}.$length()`
|
|
@@ -117,7 +117,7 @@ function visit(ctx: { bindings: Bindings, replacements: ReplacementIntent[], sou
|
|
|
117
117
|
let element = node.left,
|
|
118
118
|
name = ast.expression.name(element.expression);
|
|
119
119
|
|
|
120
|
-
if (name && ctx.bindings.get(name) ===
|
|
120
|
+
if (name && ctx.bindings.get(name) === TYPES.Array) {
|
|
121
121
|
ctx.replacements.push({
|
|
122
122
|
node,
|
|
123
123
|
generate: (sf) => `${element.expression.getText(sf)}.$set(
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { uid } from '@esportsplus/typescript/compiler';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
const ENTRYPOINT = 'reactive';
|
|
5
|
+
|
|
6
|
+
const ENTRYPOINT_REGEX = /\breactive\b/;
|
|
7
|
+
|
|
8
|
+
const NAMESPACE = uid('reactivity');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
const enum TYPES {
|
|
12
|
+
Array,
|
|
13
|
+
Computed,
|
|
14
|
+
Object,
|
|
15
|
+
Signal
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
export { ENTRYPOINT, ENTRYPOINT_REGEX, NAMESPACE, TYPES };
|
|
20
|
+
export { PACKAGE } from '../constants';
|
package/src/compiler/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { ImportIntent, ReplacementIntent, TransformContext } from '@esportsplus/typescript/compiler';
|
|
2
2
|
import { ts } from '@esportsplus/typescript';
|
|
3
3
|
import { imports } from '@esportsplus/typescript/compiler';
|
|
4
|
-
import {
|
|
5
|
-
import type { Bindings } from '
|
|
4
|
+
import { ENTRYPOINT, NAMESPACE, PACKAGE } from './constants';
|
|
5
|
+
import type { Bindings } from './types';
|
|
6
6
|
import array from './array';
|
|
7
7
|
import object from './object';
|
|
8
8
|
import primitives from './primitives';
|
|
@@ -37,18 +37,18 @@ function isReactiveCallExpression(checker: ts.TypeChecker | undefined, node: ts.
|
|
|
37
37
|
// Direct call: reactive(...) or aliasedName(...)
|
|
38
38
|
if (ts.isIdentifier(expr)) {
|
|
39
39
|
// Fast path: literal "reactive"
|
|
40
|
-
if (expr.text ===
|
|
40
|
+
if (expr.text === ENTRYPOINT) {
|
|
41
41
|
return true;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
// Use checker to resolve aliases
|
|
45
45
|
if (checker) {
|
|
46
|
-
return imports.includes(checker, expr, PACKAGE,
|
|
46
|
+
return imports.includes(checker, expr, PACKAGE, ENTRYPOINT);
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
// Property access: ns.reactive(...)
|
|
51
|
-
if (ts.isPropertyAccessExpression(expr) && expr.name.text ===
|
|
51
|
+
if (ts.isPropertyAccessExpression(expr) && expr.name.text === ENTRYPOINT && checker) {
|
|
52
52
|
return imports.includes(checker, expr, PACKAGE);
|
|
53
53
|
}
|
|
54
54
|
|
|
@@ -59,7 +59,7 @@ function visit(ctx: FindRemainingContext, node: ts.Node): void {
|
|
|
59
59
|
// Check if call or its expression has already been transformed
|
|
60
60
|
if (isReactiveCallExpression(ctx.checker, node) && !ctx.transformedNodes.has(node) && !ctx.transformedNodes.has(node.expression)) {
|
|
61
61
|
ctx.replacements.push({
|
|
62
|
-
generate: () => `${
|
|
62
|
+
generate: () => `${NAMESPACE}.reactive(${node.arguments.map(a => a.getText(ctx.sourceFile)).join(', ')})`,
|
|
63
63
|
node
|
|
64
64
|
});
|
|
65
65
|
}
|
|
@@ -71,47 +71,45 @@ function visit(ctx: FindRemainingContext, node: ts.Node): void {
|
|
|
71
71
|
export default {
|
|
72
72
|
patterns: ['reactive(', 'reactive<'],
|
|
73
73
|
transform: (ctx: TransformContext) => {
|
|
74
|
-
if (!imports.all(ctx.sourceFile, PACKAGE).some(i => i.specifiers.has(
|
|
74
|
+
if (!imports.all(ctx.sourceFile, PACKAGE).some(i => i.specifiers.has(ENTRYPOINT))) {
|
|
75
75
|
return {};
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
let bindings: Bindings = new Map(),
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
intents = {
|
|
80
|
+
imports: [] as ImportIntent[],
|
|
81
|
+
prepend: [] as string[],
|
|
82
|
+
replacements: [] as ReplacementIntent[]
|
|
83
|
+
};
|
|
82
84
|
|
|
83
85
|
// Run primitives transform first (tracks bindings for signal/computed)
|
|
84
|
-
replacements.push(
|
|
86
|
+
intents.replacements.push(
|
|
85
87
|
...primitives(ctx.sourceFile, bindings, (node: ts.Node) => isReactiveCallExpression(ctx.checker, node))
|
|
86
88
|
);
|
|
87
89
|
|
|
88
90
|
// Run object transform
|
|
89
|
-
let
|
|
91
|
+
let { prepend, replacements } = object(ctx.sourceFile, bindings);
|
|
90
92
|
|
|
91
|
-
prepend.push(...
|
|
92
|
-
replacements.push(...
|
|
93
|
+
intents.prepend.push(...prepend);
|
|
94
|
+
intents.replacements.push(...replacements);
|
|
93
95
|
|
|
94
96
|
// Run array transform separately ( avoid race conditions )
|
|
95
|
-
replacements.push(...array(ctx.sourceFile, bindings));
|
|
97
|
+
intents.replacements.push(...array(ctx.sourceFile, bindings));
|
|
96
98
|
|
|
97
99
|
// Find remaining reactive() calls that weren't transformed and replace with namespace version
|
|
98
|
-
replacements.push(
|
|
99
|
-
...findRemainingCalls(ctx.checker, ctx.sourceFile, new Set(replacements.map(r => r.node)))
|
|
100
|
+
intents.replacements.push(
|
|
101
|
+
...findRemainingCalls(ctx.checker, ctx.sourceFile, new Set(intents.replacements.map(r => r.node)))
|
|
100
102
|
);
|
|
101
103
|
|
|
102
104
|
// Build import intent
|
|
103
|
-
if (replacements.length > 0 || prepend.length > 0) {
|
|
104
|
-
|
|
105
|
-
namespace:
|
|
105
|
+
if (intents.replacements.length > 0 || intents.prepend.length > 0) {
|
|
106
|
+
intents.imports.push({
|
|
107
|
+
namespace: NAMESPACE,
|
|
106
108
|
package: PACKAGE,
|
|
107
|
-
remove: [
|
|
109
|
+
remove: [ENTRYPOINT]
|
|
108
110
|
});
|
|
109
111
|
}
|
|
110
112
|
|
|
111
|
-
return
|
|
112
|
-
imports: importsIntent,
|
|
113
|
-
prepend,
|
|
114
|
-
replacements
|
|
115
|
-
};
|
|
113
|
+
return intents;
|
|
116
114
|
}
|
|
117
115
|
};
|
package/src/compiler/object.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import type { ReplacementIntent } from '@esportsplus/typescript/compiler';
|
|
2
2
|
import { ts } from '@esportsplus/typescript';
|
|
3
3
|
import { uid } from '@esportsplus/typescript/compiler';
|
|
4
|
-
import {
|
|
5
|
-
import type { Bindings } from '
|
|
4
|
+
import { NAMESPACE, TYPES } from './constants';
|
|
5
|
+
import type { Bindings } from './types';
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
interface AnalyzedProperty {
|
|
9
9
|
isStatic: boolean;
|
|
10
10
|
key: string;
|
|
11
|
-
type:
|
|
11
|
+
type: TYPES;
|
|
12
12
|
valueText: string;
|
|
13
13
|
}
|
|
14
14
|
|
|
@@ -49,7 +49,7 @@ function analyzeProperty(prop: ts.ObjectLiteralElementLike, sourceFile: ts.Sourc
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
if (ts.isArrowFunction(unwrapped) || ts.isFunctionExpression(unwrapped)) {
|
|
52
|
-
return { isStatic: false, key, type:
|
|
52
|
+
return { isStatic: false, key, type: TYPES.Computed, valueText };
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
if (ts.isArrayLiteralExpression(unwrapped)) {
|
|
@@ -62,10 +62,10 @@ function analyzeProperty(prop: ts.ObjectLiteralElementLike, sourceFile: ts.Sourc
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
return { isStatic, key, type:
|
|
65
|
+
return { isStatic, key, type: TYPES.Array, valueText };
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
return { isStatic: isStaticValue(value), key, type:
|
|
68
|
+
return { isStatic: isStaticValue(value), key, type: TYPES.Signal, valueText };
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
function buildClassCode(className: string, properties: AnalyzedProperty[]): string {
|
|
@@ -81,61 +81,61 @@ function buildClassCode(className: string, properties: AnalyzedProperty[]): stri
|
|
|
81
81
|
generic = `T${parameters.length}`,
|
|
82
82
|
parameter = `_p${parameters.length}`;
|
|
83
83
|
|
|
84
|
-
if (type ===
|
|
84
|
+
if (type === TYPES.Signal) {
|
|
85
85
|
let value = `_v${setters++}`;
|
|
86
86
|
|
|
87
87
|
if (isStatic) {
|
|
88
88
|
accessors.push(`
|
|
89
89
|
get ${key}() {
|
|
90
|
-
return ${
|
|
90
|
+
return ${NAMESPACE}.read(this.#${key});
|
|
91
91
|
}
|
|
92
92
|
set ${key}(${value}) {
|
|
93
|
-
${
|
|
93
|
+
${NAMESPACE}.write(this.#${key}, ${value});
|
|
94
94
|
}
|
|
95
95
|
`);
|
|
96
|
-
fields.push(`#${key} = this[${
|
|
96
|
+
fields.push(`#${key} = this[${NAMESPACE}.SIGNAL](${valueText});`);
|
|
97
97
|
}
|
|
98
98
|
else {
|
|
99
99
|
accessors.push(`
|
|
100
100
|
get ${key}() {
|
|
101
|
-
return ${
|
|
101
|
+
return ${NAMESPACE}.read(this.#${key}) as ${generic};
|
|
102
102
|
}
|
|
103
103
|
set ${key}(${value}) {
|
|
104
|
-
${
|
|
104
|
+
${NAMESPACE}.write(this.#${key}, ${value});
|
|
105
105
|
}
|
|
106
106
|
`);
|
|
107
|
-
body.push(`this.#${key} = this[${
|
|
107
|
+
body.push(`this.#${key} = this[${NAMESPACE}.SIGNAL](${parameter});`);
|
|
108
108
|
fields.push(`#${key};`);
|
|
109
109
|
generics.push(generic);
|
|
110
110
|
parameters.push(`${parameter}: ${generic}`);
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
|
-
else if (type ===
|
|
113
|
+
else if (type === TYPES.Array) {
|
|
114
114
|
accessors.push(`
|
|
115
115
|
get ${key}() {
|
|
116
116
|
return this.#${key};
|
|
117
117
|
}
|
|
118
118
|
`);
|
|
119
|
-
body.push(`this.#${key} = this[${
|
|
119
|
+
body.push(`this.#${key} = this[${NAMESPACE}.REACTIVE_ARRAY](${parameter});`);
|
|
120
120
|
fields.push(`#${key};`);
|
|
121
121
|
generics.push(`${generic} extends unknown[]`);
|
|
122
122
|
parameters.push(`${parameter}: ${generic}`);
|
|
123
123
|
}
|
|
124
|
-
else if (type ===
|
|
124
|
+
else if (type === TYPES.Computed) {
|
|
125
125
|
accessors.push(`
|
|
126
126
|
get ${key}() {
|
|
127
|
-
return ${
|
|
127
|
+
return ${NAMESPACE}.read(this.#${key});
|
|
128
128
|
}
|
|
129
129
|
`);
|
|
130
|
-
body.push(`this.#${key} = this[${
|
|
130
|
+
body.push(`this.#${key} = this[${NAMESPACE}.COMPUTED](${parameter});`);
|
|
131
131
|
fields.push(`#${key};`);
|
|
132
|
-
generics.push(`${generic} extends ${
|
|
132
|
+
generics.push(`${generic} extends ${NAMESPACE}.Computed<ReturnType<${generic}>>['fn']`);
|
|
133
133
|
parameters.push(`${parameter}: ${generic}`);
|
|
134
134
|
}
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
return `
|
|
138
|
-
class ${className}${generics.length > 0 ? `<${generics.join(', ')}>` : ''} extends ${
|
|
138
|
+
class ${className}${generics.length > 0 ? `<${generics.join(', ')}>` : ''} extends ${NAMESPACE}.ReactiveObject<any> {
|
|
139
139
|
${fields.join('\n')}
|
|
140
140
|
constructor(${parameters.join(', ')}) {
|
|
141
141
|
super(null);
|
|
@@ -176,7 +176,7 @@ function visit(ctx: VisitContext, node: ts.Node): void {
|
|
|
176
176
|
|
|
177
177
|
if (node.parent && ts.isVariableDeclaration(node.parent) && ts.isIdentifier(node.parent.name)) {
|
|
178
178
|
varname = node.parent.name.text;
|
|
179
|
-
ctx.bindings.set(varname,
|
|
179
|
+
ctx.bindings.set(varname, TYPES.Object);
|
|
180
180
|
}
|
|
181
181
|
|
|
182
182
|
for (let i = 0, n = props.length; i < n; i++) {
|
|
@@ -196,8 +196,8 @@ function visit(ctx: VisitContext, node: ts.Node): void {
|
|
|
196
196
|
|
|
197
197
|
properties.push(analyzed);
|
|
198
198
|
|
|
199
|
-
if (analyzed.type ===
|
|
200
|
-
ctx.bindings.set(`${varname}.${analyzed.key}`,
|
|
199
|
+
if (analyzed.type === TYPES.Array && varname) {
|
|
200
|
+
ctx.bindings.set(`${varname}.${analyzed.key}`, TYPES.Array);
|
|
201
201
|
}
|
|
202
202
|
}
|
|
203
203
|
|
|
@@ -243,7 +243,7 @@ export default (sourceFile: ts.SourceFile, bindings: Bindings): ObjectTransformR
|
|
|
243
243
|
replacements.push({
|
|
244
244
|
generate: () => ` new ${call.className}(${
|
|
245
245
|
call.properties
|
|
246
|
-
.filter(({ isStatic, type }) => !isStatic || type ===
|
|
246
|
+
.filter(({ isStatic, type }) => !isStatic || type === TYPES.Computed)
|
|
247
247
|
.map(p => p.valueText)
|
|
248
248
|
.join(', ')
|
|
249
249
|
})`,
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import type { ReplacementIntent } from '@esportsplus/typescript/compiler';
|
|
2
2
|
import { ts } from '@esportsplus/typescript';
|
|
3
|
-
import {
|
|
4
|
-
import type { Bindings } from '
|
|
3
|
+
import { NAMESPACE, TYPES } from './constants';
|
|
4
|
+
import type { Bindings } from './types';
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
interface ScopeBinding {
|
|
8
8
|
name: string;
|
|
9
9
|
scope: ts.Node;
|
|
10
|
-
type:
|
|
10
|
+
type: TYPES;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
interface TransformContext {
|
|
@@ -59,10 +59,10 @@ function visit(ctx: TransformContext, node: ts.Node): void {
|
|
|
59
59
|
|
|
60
60
|
if (call.arguments.length > 0) {
|
|
61
61
|
let arg = call.arguments[0],
|
|
62
|
-
classification:
|
|
62
|
+
classification: TYPES | null = TYPES.Signal;
|
|
63
63
|
|
|
64
64
|
if (ts.isArrowFunction(arg) || ts.isFunctionExpression(arg)) {
|
|
65
|
-
classification =
|
|
65
|
+
classification = TYPES.Computed;
|
|
66
66
|
}
|
|
67
67
|
else if (ts.isArrayLiteralExpression(arg) || ts.isObjectLiteralExpression(arg)) {
|
|
68
68
|
classification = null;
|
|
@@ -114,9 +114,9 @@ function visit(ctx: TransformContext, node: ts.Node): void {
|
|
|
114
114
|
|
|
115
115
|
// Replace just the 'reactive' identifier with the appropriate namespace function
|
|
116
116
|
ctx.replacements.push({
|
|
117
|
-
generate: () => classification ===
|
|
118
|
-
? `${
|
|
119
|
-
: `${
|
|
117
|
+
generate: () => classification === TYPES.Computed
|
|
118
|
+
? `${NAMESPACE}.computed`
|
|
119
|
+
: `${NAMESPACE}.signal`,
|
|
120
120
|
node: call.expression
|
|
121
121
|
});
|
|
122
122
|
|
|
@@ -179,12 +179,12 @@ function visit(ctx: TransformContext, node: ts.Node): void {
|
|
|
179
179
|
}
|
|
180
180
|
|
|
181
181
|
if (writeCtx) {
|
|
182
|
-
if (binding.type !==
|
|
182
|
+
if (binding.type !== TYPES.Computed) {
|
|
183
183
|
if (writeCtx === 'simple' && ts.isBinaryExpression(parent)) {
|
|
184
184
|
let right = parent.right;
|
|
185
185
|
|
|
186
186
|
ctx.replacements.push({
|
|
187
|
-
generate: (sf) => `${
|
|
187
|
+
generate: (sf) => `${NAMESPACE}.write(${name}, ${right.getText(sf)})`,
|
|
188
188
|
node: parent
|
|
189
189
|
});
|
|
190
190
|
}
|
|
@@ -193,7 +193,7 @@ function visit(ctx: TransformContext, node: ts.Node): void {
|
|
|
193
193
|
right = parent.right;
|
|
194
194
|
|
|
195
195
|
ctx.replacements.push({
|
|
196
|
-
generate: (sf) => `${
|
|
196
|
+
generate: (sf) => `${NAMESPACE}.write(${name}, ${name}.value ${op} ${right.getText(sf)})`,
|
|
197
197
|
node: parent
|
|
198
198
|
});
|
|
199
199
|
}
|
|
@@ -203,13 +203,13 @@ function visit(ctx: TransformContext, node: ts.Node): void {
|
|
|
203
203
|
|
|
204
204
|
if (ts.isExpressionStatement(parent.parent)) {
|
|
205
205
|
ctx.replacements.push({
|
|
206
|
-
generate: () => `${
|
|
206
|
+
generate: () => `${NAMESPACE}.write(${name}, ${name}.value ${delta})`,
|
|
207
207
|
node: parent
|
|
208
208
|
});
|
|
209
209
|
}
|
|
210
210
|
else if (isPrefix) {
|
|
211
211
|
ctx.replacements.push({
|
|
212
|
-
generate: () => `(${
|
|
212
|
+
generate: () => `(${NAMESPACE}.write(${name}, ${name}.value ${delta}), ${name}.value)`,
|
|
213
213
|
node: parent
|
|
214
214
|
});
|
|
215
215
|
}
|
|
@@ -217,7 +217,7 @@ function visit(ctx: TransformContext, node: ts.Node): void {
|
|
|
217
217
|
let tmp = `_t${ctx.tmpCounter++}`;
|
|
218
218
|
|
|
219
219
|
ctx.replacements.push({
|
|
220
|
-
generate: () => `((${tmp}) => (${
|
|
220
|
+
generate: () => `((${tmp}) => (${NAMESPACE}.write(${name}, ${tmp} ${delta}), ${tmp}))(${name}.value)`,
|
|
221
221
|
node: parent
|
|
222
222
|
});
|
|
223
223
|
}
|
|
@@ -226,7 +226,7 @@ function visit(ctx: TransformContext, node: ts.Node): void {
|
|
|
226
226
|
}
|
|
227
227
|
else {
|
|
228
228
|
ctx.replacements.push({
|
|
229
|
-
generate: () => `${
|
|
229
|
+
generate: () => `${NAMESPACE}.read(${name})`,
|
|
230
230
|
node
|
|
231
231
|
});
|
|
232
232
|
}
|
package/src/constants.ts
CHANGED
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
import { uid } from '@esportsplus/typescript/compiler';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const COMPILER_ENTRYPOINT = 'reactive';
|
|
5
|
-
|
|
6
|
-
const COMPILER_ENTRYPOINT_REGEX = /\breactive\b/;
|
|
7
|
-
|
|
8
|
-
const COMPILER_NAMESPACE = uid('reactivity');
|
|
9
|
-
|
|
10
|
-
const enum COMPILER_TYPES {
|
|
11
|
-
Array,
|
|
12
|
-
Computed,
|
|
13
|
-
Object,
|
|
14
|
-
Signal
|
|
15
|
-
}
|
|
16
|
-
|
|
17
1
|
const COMPUTED = Symbol('reactivity.computed');
|
|
18
2
|
|
|
19
3
|
const PACKAGE = '@esportsplus/reactivity';
|
|
@@ -46,9 +30,9 @@ const STATE_NOTIFY_MASK = (STATE_CHECK | STATE_DIRTY);
|
|
|
46
30
|
|
|
47
31
|
|
|
48
32
|
export {
|
|
49
|
-
|
|
50
|
-
PACKAGE,
|
|
33
|
+
COMPUTED,
|
|
51
34
|
REACTIVE_ARRAY, REACTIVE_OBJECT,
|
|
35
|
+
PACKAGE,
|
|
52
36
|
SIGNAL,
|
|
53
37
|
STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED,
|
|
54
38
|
STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE, STATE_NOTIFY_MASK, STATE_RECOMPUTING
|
package/src/types.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
|
-
import {
|
|
2
|
+
import { 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 Bindings = Map<string, COMPILER_TYPES>;
|
|
7
|
-
|
|
8
6
|
interface Computed<T> {
|
|
9
7
|
cleanup: VoidFunction | VoidFunction[] | null;
|
|
10
8
|
deps: Link | null;
|
|
@@ -63,7 +61,6 @@ interface TransformResult {
|
|
|
63
61
|
|
|
64
62
|
|
|
65
63
|
export type {
|
|
66
|
-
Bindings,
|
|
67
64
|
Computed,
|
|
68
65
|
Link,
|
|
69
66
|
Reactive,
|