@esportsplus/reactivity 0.24.2 → 0.24.3
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/transformer/factory.d.ts +7 -7
- package/build/transformer/factory.js +14 -14
- package/build/transformer/index.js +26 -24
- package/build/transformer/transforms/object.d.ts +2 -2
- package/build/transformer/transforms/object.js +12 -11
- package/build/transformer/transforms/primitives.d.ts +2 -2
- package/build/transformer/transforms/primitives.js +12 -11
- package/build/types.d.ts +6 -1
- package/package.json +1 -1
- package/src/transformer/factory.ts +21 -19
- package/src/transformer/index.ts +53 -52
- package/src/transformer/transforms/object.ts +25 -20
- package/src/transformer/transforms/primitives.ts +22 -15
- package/src/types.ts +7 -0
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
|
-
declare function createReadCall(factory: ts.NodeFactory, expr: ts.Expression): ts.CallExpression;
|
|
3
|
-
declare function createSetCall(factory: ts.NodeFactory, target: ts.Expression, value: ts.Expression): ts.CallExpression;
|
|
4
|
-
declare function createSignalCall(factory: ts.NodeFactory, initialValue: ts.Expression): ts.CallExpression;
|
|
5
|
-
declare function createComputedCall(factory: ts.NodeFactory, fn: ts.Expression): ts.CallExpression;
|
|
2
|
+
declare function createReadCall(factory: ts.NodeFactory, ns: string, expr: ts.Expression): ts.CallExpression;
|
|
3
|
+
declare function createSetCall(factory: ts.NodeFactory, ns: string, target: ts.Expression, value: ts.Expression): ts.CallExpression;
|
|
4
|
+
declare function createSignalCall(factory: ts.NodeFactory, ns: string, initialValue: ts.Expression): ts.CallExpression;
|
|
5
|
+
declare function createComputedCall(factory: ts.NodeFactory, ns: string, fn: ts.Expression): ts.CallExpression;
|
|
6
6
|
declare function createArrayLengthCall(factory: ts.NodeFactory, arrayExpr: ts.Expression): ts.CallExpression;
|
|
7
7
|
declare function createArraySetCall(factory: ts.NodeFactory, arrayExpr: ts.Expression, index: ts.Expression, value: ts.Expression): ts.CallExpression;
|
|
8
|
-
declare function createReactiveArrayNew(factory: ts.NodeFactory, elements: ts.Expression[]): ts.NewExpression;
|
|
9
|
-
declare function createDisposeCall(factory: ts.NodeFactory, expr: ts.Expression): ts.CallExpression;
|
|
8
|
+
declare function createReactiveArrayNew(factory: ts.NodeFactory, ns: string, elements: ts.Expression[]): ts.NewExpression;
|
|
9
|
+
declare function createDisposeCall(factory: ts.NodeFactory, ns: string, expr: ts.Expression): ts.CallExpression;
|
|
10
10
|
declare function createBinaryExpr(factory: ts.NodeFactory, left: ts.Expression, op: ts.BinaryOperator, right: ts.Expression): ts.BinaryExpression;
|
|
11
11
|
declare function createCommaExpr(factory: ts.NodeFactory, first: ts.Expression, second: ts.Expression): ts.ParenthesizedExpression;
|
|
12
|
-
declare function createPostfixIncrementExpr(factory: ts.NodeFactory, tmpName: string, signalName: string, op: ts.SyntaxKind.PlusToken | ts.SyntaxKind.MinusToken): ts.CallExpression;
|
|
12
|
+
declare function createPostfixIncrementExpr(factory: ts.NodeFactory, ns: string, tmpName: string, signalName: string, op: ts.SyntaxKind.PlusToken | ts.SyntaxKind.MinusToken): ts.CallExpression;
|
|
13
13
|
export { createArrayLengthCall, createArraySetCall, createBinaryExpr, createCommaExpr, createComputedCall, createDisposeCall, createPostfixIncrementExpr, createReactiveArrayNew, createReadCall, createSetCall, createSignalCall };
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
|
-
function createReadCall(factory, expr) {
|
|
3
|
-
return factory.createCallExpression(factory.createIdentifier('read'), undefined, [expr]);
|
|
2
|
+
function createReadCall(factory, ns, expr) {
|
|
3
|
+
return factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(ns), 'read'), undefined, [expr]);
|
|
4
4
|
}
|
|
5
|
-
function createSetCall(factory, target, value) {
|
|
6
|
-
return factory.createCallExpression(factory.createIdentifier('set'), undefined, [target, value]);
|
|
5
|
+
function createSetCall(factory, ns, target, value) {
|
|
6
|
+
return factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(ns), 'set'), undefined, [target, value]);
|
|
7
7
|
}
|
|
8
|
-
function createSignalCall(factory, initialValue) {
|
|
9
|
-
return factory.createCallExpression(factory.createIdentifier('signal'), undefined, [initialValue]);
|
|
8
|
+
function createSignalCall(factory, ns, initialValue) {
|
|
9
|
+
return factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(ns), 'signal'), undefined, [initialValue]);
|
|
10
10
|
}
|
|
11
|
-
function createComputedCall(factory, fn) {
|
|
12
|
-
return factory.createCallExpression(factory.createIdentifier('computed'), undefined, [fn]);
|
|
11
|
+
function createComputedCall(factory, ns, fn) {
|
|
12
|
+
return factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(ns), 'computed'), undefined, [fn]);
|
|
13
13
|
}
|
|
14
14
|
function createArrayLengthCall(factory, arrayExpr) {
|
|
15
15
|
return factory.createCallExpression(factory.createPropertyAccessExpression(arrayExpr, '$length'), undefined, []);
|
|
@@ -17,11 +17,11 @@ function createArrayLengthCall(factory, arrayExpr) {
|
|
|
17
17
|
function createArraySetCall(factory, arrayExpr, index, value) {
|
|
18
18
|
return factory.createCallExpression(factory.createPropertyAccessExpression(arrayExpr, '$set'), undefined, [index, value]);
|
|
19
19
|
}
|
|
20
|
-
function createReactiveArrayNew(factory, elements) {
|
|
21
|
-
return factory.createNewExpression(factory.createIdentifier('ReactiveArray'), undefined, elements);
|
|
20
|
+
function createReactiveArrayNew(factory, ns, elements) {
|
|
21
|
+
return factory.createNewExpression(factory.createPropertyAccessExpression(factory.createIdentifier(ns), 'ReactiveArray'), undefined, elements);
|
|
22
22
|
}
|
|
23
|
-
function createDisposeCall(factory, expr) {
|
|
24
|
-
return factory.createCallExpression(factory.createIdentifier('dispose'), undefined, [expr]);
|
|
23
|
+
function createDisposeCall(factory, ns, expr) {
|
|
24
|
+
return factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(ns), 'dispose'), undefined, [expr]);
|
|
25
25
|
}
|
|
26
26
|
function createBinaryExpr(factory, left, op, right) {
|
|
27
27
|
return factory.createBinaryExpression(left, op, right);
|
|
@@ -29,8 +29,8 @@ function createBinaryExpr(factory, left, op, right) {
|
|
|
29
29
|
function createCommaExpr(factory, first, second) {
|
|
30
30
|
return factory.createParenthesizedExpression(factory.createBinaryExpression(first, ts.SyntaxKind.CommaToken, second));
|
|
31
31
|
}
|
|
32
|
-
function createPostfixIncrementExpr(factory, tmpName, signalName, op) {
|
|
32
|
+
function createPostfixIncrementExpr(factory, ns, tmpName, signalName, op) {
|
|
33
33
|
let tmpIdent = factory.createIdentifier(tmpName), signalIdent = factory.createIdentifier(signalName);
|
|
34
|
-
return factory.createCallExpression(factory.createParenthesizedExpression(factory.createArrowFunction(undefined, undefined, [factory.createParameterDeclaration(undefined, undefined, tmpIdent)], undefined, factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), createCommaExpr(factory, createSetCall(factory, signalIdent, factory.createBinaryExpression(tmpIdent, op, factory.createNumericLiteral(1))), tmpIdent))), undefined, [factory.createPropertyAccessExpression(signalIdent, 'value')]);
|
|
34
|
+
return factory.createCallExpression(factory.createParenthesizedExpression(factory.createArrowFunction(undefined, undefined, [factory.createParameterDeclaration(undefined, undefined, tmpIdent)], undefined, factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), createCommaExpr(factory, createSetCall(factory, ns, signalIdent, factory.createBinaryExpression(tmpIdent, op, factory.createNumericLiteral(1))), tmpIdent))), undefined, [factory.createPropertyAccessExpression(signalIdent, 'value')]);
|
|
35
35
|
}
|
|
36
36
|
export { createArrayLengthCall, createArraySetCall, createBinaryExpr, createCommaExpr, createComputedCall, createDisposeCall, createPostfixIncrementExpr, createReactiveArrayNew, createReadCall, createSetCall, createSignalCall };
|
|
@@ -1,37 +1,35 @@
|
|
|
1
|
+
import { uid } from '@esportsplus/typescript/transformer';
|
|
1
2
|
import { createArrayTransformer } from './transforms/array.js';
|
|
2
3
|
import { createObjectTransformer } from './transforms/object.js';
|
|
3
4
|
import { createPrimitivesTransformer } from './transforms/primitives.js';
|
|
4
5
|
import { mightNeedTransform } from './detector.js';
|
|
5
6
|
import { ts } from '@esportsplus/typescript';
|
|
6
|
-
|
|
7
|
-
{ module: '@esportsplus/reactivity/constants', specifier: 'REACTIVE_OBJECT' },
|
|
8
|
-
{ module: '@esportsplus/reactivity/reactive/array', specifier: 'ReactiveArray' }
|
|
9
|
-
];
|
|
10
|
-
function addImportsTransformer(neededImports, extraImports) {
|
|
7
|
+
function addImportsTransformer(neededImports, ns) {
|
|
11
8
|
return (context) => {
|
|
12
9
|
return (sourceFile) => {
|
|
13
10
|
if (neededImports.size === 0) {
|
|
14
11
|
return sourceFile;
|
|
15
12
|
}
|
|
16
|
-
let
|
|
17
|
-
for (let i = 0, n = extraImports.length; i < n; i++) {
|
|
18
|
-
extraSpecifiers.add(extraImports[i].specifier);
|
|
19
|
-
}
|
|
13
|
+
let factory = context.factory, needsArray = false, needsConstants = false, needsReactivity = false, newStatements = [];
|
|
20
14
|
for (let imp of neededImports) {
|
|
21
|
-
if (
|
|
22
|
-
|
|
15
|
+
if (imp === 'ReactiveArray') {
|
|
16
|
+
needsArray = true;
|
|
17
|
+
}
|
|
18
|
+
else if (imp === 'REACTIVE_OBJECT') {
|
|
19
|
+
needsConstants = true;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
needsReactivity = true;
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
if (
|
|
26
|
-
newStatements.push(factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.
|
|
25
|
+
if (needsReactivity) {
|
|
26
|
+
newStatements.push(factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamespaceImport(factory.createIdentifier(ns.reactivity))), factory.createStringLiteral('@esportsplus/reactivity')));
|
|
27
27
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
])), factory.createStringLiteral(extra.module)));
|
|
34
|
-
}
|
|
28
|
+
if (needsArray) {
|
|
29
|
+
newStatements.push(factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamespaceImport(factory.createIdentifier(ns.array))), factory.createStringLiteral('@esportsplus/reactivity/reactive/array')));
|
|
30
|
+
}
|
|
31
|
+
if (needsConstants) {
|
|
32
|
+
newStatements.push(factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamespaceImport(factory.createIdentifier(ns.constants))), factory.createStringLiteral('@esportsplus/reactivity/constants')));
|
|
35
33
|
}
|
|
36
34
|
let insertIndex = 0, statements = sourceFile.statements;
|
|
37
35
|
for (let i = 0, n = statements.length; i < n; i++) {
|
|
@@ -83,16 +81,20 @@ const createTransformer = () => {
|
|
|
83
81
|
if (!mightNeedTransform(code)) {
|
|
84
82
|
return sourceFile;
|
|
85
83
|
}
|
|
86
|
-
let bindings = new Map(), generatedClasses = [], neededImports = new Set()
|
|
87
|
-
|
|
84
|
+
let bindings = new Map(), generatedClasses = [], neededImports = new Set(), ns = {
|
|
85
|
+
array: uid('ra'),
|
|
86
|
+
constants: uid('rc'),
|
|
87
|
+
reactivity: uid('r')
|
|
88
|
+
};
|
|
89
|
+
let objectTransformer = createObjectTransformer(bindings, neededImports, generatedClasses, ns)(context);
|
|
88
90
|
sourceFile = objectTransformer(sourceFile);
|
|
89
91
|
let arrayTransformer = createArrayTransformer(bindings)(context);
|
|
90
92
|
sourceFile = arrayTransformer(sourceFile);
|
|
91
|
-
let primitivesTransformer = createPrimitivesTransformer(bindings, neededImports)(context);
|
|
93
|
+
let primitivesTransformer = createPrimitivesTransformer(bindings, neededImports, ns)(context);
|
|
92
94
|
sourceFile = primitivesTransformer(sourceFile);
|
|
93
95
|
let classInserter = insertClassesTransformer(generatedClasses)(context);
|
|
94
96
|
sourceFile = classInserter(sourceFile);
|
|
95
|
-
let importAdder = addImportsTransformer(neededImports,
|
|
97
|
+
let importAdder = addImportsTransformer(neededImports, ns)(context);
|
|
96
98
|
sourceFile = importAdder(sourceFile);
|
|
97
99
|
return sourceFile;
|
|
98
100
|
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { Bindings } from '../../types.js';
|
|
1
|
+
import type { Bindings, Namespaces } from '../../types.js';
|
|
2
2
|
import { ts } from '@esportsplus/typescript';
|
|
3
3
|
interface GeneratedClass {
|
|
4
4
|
classDecl: ts.ClassDeclaration;
|
|
5
5
|
className: string;
|
|
6
6
|
needsImports: Set<string>;
|
|
7
7
|
}
|
|
8
|
-
declare const createObjectTransformer: (bindings: Bindings, neededImports: Set<string>, generatedClasses: GeneratedClass[]) => (context: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile;
|
|
8
|
+
declare const createObjectTransformer: (bindings: Bindings, neededImports: Set<string>, generatedClasses: GeneratedClass[], ns: Namespaces) => (context: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile;
|
|
9
9
|
export { createObjectTransformer };
|
|
10
10
|
export type { GeneratedClass };
|
|
@@ -23,7 +23,7 @@ function analyzeProperty(prop) {
|
|
|
23
23
|
function buildReactiveClass(ctx, className, properties, varName) {
|
|
24
24
|
let factory = ctx.factory, members = [], needsImports = new Set();
|
|
25
25
|
needsImports.add('REACTIVE_OBJECT');
|
|
26
|
-
members.push(factory.createPropertyDeclaration(undefined, factory.createComputedPropertyName(factory.createIdentifier('REACTIVE_OBJECT')), undefined, undefined, factory.createTrue()));
|
|
26
|
+
members.push(factory.createPropertyDeclaration(undefined, factory.createComputedPropertyName(factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.constants), 'REACTIVE_OBJECT')), undefined, undefined, factory.createTrue()));
|
|
27
27
|
let disposeStatements = [];
|
|
28
28
|
for (let i = 0, n = properties.length; i < n; i++) {
|
|
29
29
|
let prop = properties[i];
|
|
@@ -32,12 +32,12 @@ function buildReactiveClass(ctx, className, properties, varName) {
|
|
|
32
32
|
needsImports.add('set');
|
|
33
33
|
needsImports.add('signal');
|
|
34
34
|
let privateName = factory.createPrivateIdentifier(`#${prop.key}`), paramName = uid('v');
|
|
35
|
-
members.push(factory.createPropertyDeclaration(undefined, privateName, undefined, undefined, factory.createCallExpression(factory.createIdentifier('signal'), undefined, [prop.value])));
|
|
35
|
+
members.push(factory.createPropertyDeclaration(undefined, privateName, undefined, undefined, factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.reactivity), 'signal'), undefined, [prop.value])));
|
|
36
36
|
members.push(factory.createGetAccessorDeclaration(undefined, factory.createIdentifier(prop.key), [], undefined, factory.createBlock([
|
|
37
|
-
factory.createReturnStatement(factory.createCallExpression(factory.createIdentifier('read'), undefined, [factory.createPropertyAccessExpression(factory.createThis(), privateName)]))
|
|
37
|
+
factory.createReturnStatement(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.reactivity), 'read'), undefined, [factory.createPropertyAccessExpression(factory.createThis(), privateName)]))
|
|
38
38
|
], true)));
|
|
39
39
|
members.push(factory.createSetAccessorDeclaration(undefined, factory.createIdentifier(prop.key), [factory.createParameterDeclaration(undefined, undefined, paramName)], factory.createBlock([
|
|
40
|
-
factory.createExpressionStatement(factory.createCallExpression(factory.createIdentifier('set'), undefined, [
|
|
40
|
+
factory.createExpressionStatement(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.reactivity), 'set'), undefined, [
|
|
41
41
|
factory.createPropertyAccessExpression(factory.createThis(), privateName),
|
|
42
42
|
factory.createIdentifier(paramName)
|
|
43
43
|
]))
|
|
@@ -45,7 +45,7 @@ function buildReactiveClass(ctx, className, properties, varName) {
|
|
|
45
45
|
}
|
|
46
46
|
else if (prop.type === 'array') {
|
|
47
47
|
needsImports.add('ReactiveArray');
|
|
48
|
-
members.push(factory.createPropertyDeclaration(undefined, factory.createIdentifier(prop.key), undefined, undefined, factory.createNewExpression(factory.createIdentifier('ReactiveArray'), undefined, prop.elements || [])));
|
|
48
|
+
members.push(factory.createPropertyDeclaration(undefined, factory.createIdentifier(prop.key), undefined, undefined, factory.createNewExpression(factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.array), 'ReactiveArray'), undefined, prop.elements || [])));
|
|
49
49
|
if (varName) {
|
|
50
50
|
ctx.bindings.set(`${varName}.${prop.key}`, 'array');
|
|
51
51
|
}
|
|
@@ -63,11 +63,11 @@ function buildReactiveClass(ctx, className, properties, varName) {
|
|
|
63
63
|
factory.createLiteralTypeNode(factory.createNull())
|
|
64
64
|
]), factory.createNull()));
|
|
65
65
|
members.push(factory.createGetAccessorDeclaration(undefined, factory.createIdentifier(prop.key), [], undefined, factory.createBlock([
|
|
66
|
-
factory.createReturnStatement(factory.createCallExpression(factory.createIdentifier('read'), undefined, [
|
|
67
|
-
factory.createBinaryExpression(factory.createPropertyAccessExpression(factory.createThis(), privateName), ts.SyntaxKind.QuestionQuestionEqualsToken, factory.createCallExpression(factory.createIdentifier('computed'), undefined, [prop.value]))
|
|
66
|
+
factory.createReturnStatement(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.reactivity), 'read'), undefined, [
|
|
67
|
+
factory.createBinaryExpression(factory.createPropertyAccessExpression(factory.createThis(), privateName), ts.SyntaxKind.QuestionQuestionEqualsToken, factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.reactivity), 'computed'), undefined, [prop.value]))
|
|
68
68
|
]))
|
|
69
69
|
], true)));
|
|
70
|
-
disposeStatements.push(factory.createIfStatement(factory.createPropertyAccessExpression(factory.createThis(), privateName), factory.createExpressionStatement(factory.createCallExpression(factory.createIdentifier('dispose'), undefined, [factory.createPropertyAccessExpression(factory.createThis(), privateName)]))));
|
|
70
|
+
disposeStatements.push(factory.createIfStatement(factory.createPropertyAccessExpression(factory.createThis(), privateName), factory.createExpressionStatement(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.reactivity), 'dispose'), undefined, [factory.createPropertyAccessExpression(factory.createThis(), privateName)]))));
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
members.push(factory.createMethodDeclaration(undefined, undefined, 'dispose', undefined, undefined, [], undefined, factory.createBlock(disposeStatements, true)));
|
|
@@ -102,7 +102,7 @@ function visit(ctx, node) {
|
|
|
102
102
|
ctx.bindings.set(varName, 'array');
|
|
103
103
|
}
|
|
104
104
|
ctx.neededImports.add('ReactiveArray');
|
|
105
|
-
return ctx.factory.createNewExpression(ctx.factory.createIdentifier('ReactiveArray'), undefined, [...arg.elements]);
|
|
105
|
+
return ctx.factory.createNewExpression(ctx.factory.createPropertyAccessExpression(ctx.factory.createIdentifier(ctx.ns.array), 'ReactiveArray'), undefined, [...arg.elements]);
|
|
106
106
|
}
|
|
107
107
|
if (arg && ts.isObjectLiteralExpression(arg)) {
|
|
108
108
|
let varName = null;
|
|
@@ -133,7 +133,7 @@ function visit(ctx, node) {
|
|
|
133
133
|
}
|
|
134
134
|
return ts.visitEachChild(node, n => visit(ctx, n), ctx.context);
|
|
135
135
|
}
|
|
136
|
-
const createObjectTransformer = (bindings, neededImports, generatedClasses) => {
|
|
136
|
+
const createObjectTransformer = (bindings, neededImports, generatedClasses, ns) => {
|
|
137
137
|
return (context) => {
|
|
138
138
|
return (sourceFile) => {
|
|
139
139
|
let ctx = {
|
|
@@ -142,7 +142,8 @@ const createObjectTransformer = (bindings, neededImports, generatedClasses) => {
|
|
|
142
142
|
factory: context.factory,
|
|
143
143
|
generatedClasses,
|
|
144
144
|
hasReactiveImport: false,
|
|
145
|
-
neededImports
|
|
145
|
+
neededImports,
|
|
146
|
+
ns
|
|
146
147
|
};
|
|
147
148
|
return ts.visitNode(sourceFile, n => visit(ctx, n));
|
|
148
149
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Bindings } from '../../types.js';
|
|
1
|
+
import type { Bindings, Namespaces } from '../../types.js';
|
|
2
2
|
import { ts } from '@esportsplus/typescript';
|
|
3
|
-
declare const createPrimitivesTransformer: (bindings: Bindings, neededImports: Set<string
|
|
3
|
+
declare const createPrimitivesTransformer: (bindings: Bindings, neededImports: Set<string>, ns: Namespaces) => (context: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile;
|
|
4
4
|
export { createPrimitivesTransformer };
|
|
@@ -168,11 +168,11 @@ function visit(ctx, node) {
|
|
|
168
168
|
if (classification === 'computed') {
|
|
169
169
|
ctx.neededImports.add('computed');
|
|
170
170
|
let transformedArg = ts.visitEachChild(arg, n => visitComputedArg(ctx, n), ctx.context);
|
|
171
|
-
return createComputedCall(ctx.factory, transformedArg);
|
|
171
|
+
return createComputedCall(ctx.factory, ctx.ns.reactivity, transformedArg);
|
|
172
172
|
}
|
|
173
173
|
else {
|
|
174
174
|
ctx.neededImports.add('signal');
|
|
175
|
-
return createSignalCall(ctx.factory, arg);
|
|
175
|
+
return createSignalCall(ctx.factory, ctx.ns.reactivity, arg);
|
|
176
176
|
}
|
|
177
177
|
}
|
|
178
178
|
}
|
|
@@ -184,11 +184,11 @@ function visit(ctx, node) {
|
|
|
184
184
|
ctx.neededImports.add('set');
|
|
185
185
|
let factory = ctx.factory, name = node.left.text, signalIdent = factory.createIdentifier(name), transformedRight = ts.visitEachChild(node.right, n => visit(ctx, n), ctx.context);
|
|
186
186
|
if (assignType === 'simple') {
|
|
187
|
-
return createSetCall(factory, signalIdent, transformedRight);
|
|
187
|
+
return createSetCall(factory, ctx.ns.reactivity, signalIdent, transformedRight);
|
|
188
188
|
}
|
|
189
189
|
else {
|
|
190
190
|
let op = getCompoundOperator(node.operatorToken.kind), valueAccess = factory.createPropertyAccessExpression(signalIdent, 'value');
|
|
191
|
-
return createSetCall(factory, signalIdent, factory.createBinaryExpression(valueAccess, op, transformedRight));
|
|
191
|
+
return createSetCall(factory, ctx.ns.reactivity, signalIdent, factory.createBinaryExpression(valueAccess, op, transformedRight));
|
|
192
192
|
}
|
|
193
193
|
}
|
|
194
194
|
}
|
|
@@ -201,10 +201,10 @@ function visit(ctx, node) {
|
|
|
201
201
|
ctx.neededImports.add('set');
|
|
202
202
|
let delta = op === ts.SyntaxKind.PlusPlusToken ? ts.SyntaxKind.PlusToken : ts.SyntaxKind.MinusToken, factory = ctx.factory, name = node.operand.text, signalIdent = factory.createIdentifier(name), valueAccess = factory.createPropertyAccessExpression(signalIdent, 'value');
|
|
203
203
|
if (node.parent && ts.isExpressionStatement(node.parent)) {
|
|
204
|
-
return createSetCall(factory, signalIdent, factory.createBinaryExpression(valueAccess, delta, factory.createNumericLiteral(1)));
|
|
204
|
+
return createSetCall(factory, ctx.ns.reactivity, signalIdent, factory.createBinaryExpression(valueAccess, delta, factory.createNumericLiteral(1)));
|
|
205
205
|
}
|
|
206
206
|
else {
|
|
207
|
-
return createCommaExpr(factory, createSetCall(factory, signalIdent, factory.createBinaryExpression(valueAccess, delta, factory.createNumericLiteral(1))), factory.createPropertyAccessExpression(factory.createIdentifier(name), 'value'));
|
|
207
|
+
return createCommaExpr(factory, createSetCall(factory, ctx.ns.reactivity, signalIdent, factory.createBinaryExpression(valueAccess, delta, factory.createNumericLiteral(1))), factory.createPropertyAccessExpression(factory.createIdentifier(name), 'value'));
|
|
208
208
|
}
|
|
209
209
|
}
|
|
210
210
|
}
|
|
@@ -217,10 +217,10 @@ function visit(ctx, node) {
|
|
|
217
217
|
ctx.neededImports.add('set');
|
|
218
218
|
let delta = op === ts.SyntaxKind.PlusPlusToken ? ts.SyntaxKind.PlusToken : ts.SyntaxKind.MinusToken, factory = ctx.factory, name = node.operand.text, signalIdent = factory.createIdentifier(name), valueAccess = factory.createPropertyAccessExpression(signalIdent, 'value');
|
|
219
219
|
if (node.parent && ts.isExpressionStatement(node.parent)) {
|
|
220
|
-
return createSetCall(factory, signalIdent, factory.createBinaryExpression(valueAccess, delta, factory.createNumericLiteral(1)));
|
|
220
|
+
return createSetCall(factory, ctx.ns.reactivity, signalIdent, factory.createBinaryExpression(valueAccess, delta, factory.createNumericLiteral(1)));
|
|
221
221
|
}
|
|
222
222
|
else {
|
|
223
|
-
return createPostfixIncrementExpr(factory, uid('tmp'), name, delta);
|
|
223
|
+
return createPostfixIncrementExpr(factory, ctx.ns.reactivity, uid('tmp'), name, delta);
|
|
224
224
|
}
|
|
225
225
|
}
|
|
226
226
|
}
|
|
@@ -242,7 +242,7 @@ function visit(ctx, node) {
|
|
|
242
242
|
let binding = findBinding(ctx.scopedBindings, node.text, node);
|
|
243
243
|
if (binding) {
|
|
244
244
|
ctx.neededImports.add('read');
|
|
245
|
-
return createReadCall(ctx.factory, ctx.factory.createIdentifier(node.text));
|
|
245
|
+
return createReadCall(ctx.factory, ctx.ns.reactivity, ctx.factory.createIdentifier(node.text));
|
|
246
246
|
}
|
|
247
247
|
}
|
|
248
248
|
return ts.visitEachChild(node, n => visit(ctx, n), ctx.context);
|
|
@@ -258,12 +258,12 @@ function visitComputedArg(ctx, node) {
|
|
|
258
258
|
let binding = findBinding(ctx.scopedBindings, node.text, node);
|
|
259
259
|
if (binding) {
|
|
260
260
|
ctx.neededImports.add('read');
|
|
261
|
-
return createReadCall(ctx.factory, ctx.factory.createIdentifier(node.text));
|
|
261
|
+
return createReadCall(ctx.factory, ctx.ns.reactivity, ctx.factory.createIdentifier(node.text));
|
|
262
262
|
}
|
|
263
263
|
}
|
|
264
264
|
return ts.visitEachChild(node, n => visitComputedArg(ctx, n), ctx.context);
|
|
265
265
|
}
|
|
266
|
-
const createPrimitivesTransformer = (bindings, neededImports) => {
|
|
266
|
+
const createPrimitivesTransformer = (bindings, neededImports, ns) => {
|
|
267
267
|
return (context) => {
|
|
268
268
|
return (sourceFile) => {
|
|
269
269
|
let ctx = {
|
|
@@ -272,6 +272,7 @@ const createPrimitivesTransformer = (bindings, neededImports) => {
|
|
|
272
272
|
factory: context.factory,
|
|
273
273
|
hasReactiveImport: false,
|
|
274
274
|
neededImports,
|
|
275
|
+
ns,
|
|
275
276
|
scopedBindings: []
|
|
276
277
|
};
|
|
277
278
|
return ts.visitNode(sourceFile, n => visit(ctx, n));
|
package/build/types.d.ts
CHANGED
|
@@ -2,6 +2,11 @@ import { COMPUTED, SIGNAL, STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE,
|
|
|
2
2
|
import { ReactiveArray, ReactiveObject } from './reactive/index.js';
|
|
3
3
|
type BindingType = 'array' | 'computed' | 'object' | 'signal';
|
|
4
4
|
type Bindings = Map<string, BindingType>;
|
|
5
|
+
interface Namespaces {
|
|
6
|
+
array: string;
|
|
7
|
+
constants: string;
|
|
8
|
+
reactivity: string;
|
|
9
|
+
}
|
|
5
10
|
interface Computed<T> {
|
|
6
11
|
cleanup: VoidFunction | VoidFunction[] | null;
|
|
7
12
|
deps: Link | null;
|
|
@@ -30,4 +35,4 @@ type Signal<T> = {
|
|
|
30
35
|
type: typeof SIGNAL;
|
|
31
36
|
value: T;
|
|
32
37
|
};
|
|
33
|
-
export type { BindingType, Bindings, Computed, Link, ReactiveArray, ReactiveObject, Signal };
|
|
38
|
+
export type { BindingType, Bindings, Computed, Link, Namespaces, ReactiveArray, ReactiveObject, Signal };
|
package/package.json
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
// Create: read(expr)
|
|
5
|
-
function createReadCall(factory: ts.NodeFactory, expr: ts.Expression): ts.CallExpression {
|
|
4
|
+
// Create: ns.read(expr)
|
|
5
|
+
function createReadCall(factory: ts.NodeFactory, ns: string, expr: ts.Expression): ts.CallExpression {
|
|
6
6
|
return factory.createCallExpression(
|
|
7
|
-
factory.createIdentifier('read'),
|
|
7
|
+
factory.createPropertyAccessExpression(factory.createIdentifier(ns), 'read'),
|
|
8
8
|
undefined,
|
|
9
9
|
[expr]
|
|
10
10
|
);
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
// Create: set(target, value)
|
|
14
|
-
function createSetCall(factory: ts.NodeFactory, target: ts.Expression, value: ts.Expression): ts.CallExpression {
|
|
13
|
+
// Create: ns.set(target, value)
|
|
14
|
+
function createSetCall(factory: ts.NodeFactory, ns: string, target: ts.Expression, value: ts.Expression): ts.CallExpression {
|
|
15
15
|
return factory.createCallExpression(
|
|
16
|
-
factory.createIdentifier('set'),
|
|
16
|
+
factory.createPropertyAccessExpression(factory.createIdentifier(ns), 'set'),
|
|
17
17
|
undefined,
|
|
18
18
|
[target, value]
|
|
19
19
|
);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
// Create: signal(initialValue)
|
|
23
|
-
function createSignalCall(factory: ts.NodeFactory, initialValue: ts.Expression): ts.CallExpression {
|
|
22
|
+
// Create: ns.signal(initialValue)
|
|
23
|
+
function createSignalCall(factory: ts.NodeFactory, ns: string, initialValue: ts.Expression): ts.CallExpression {
|
|
24
24
|
return factory.createCallExpression(
|
|
25
|
-
factory.createIdentifier('signal'),
|
|
25
|
+
factory.createPropertyAccessExpression(factory.createIdentifier(ns), 'signal'),
|
|
26
26
|
undefined,
|
|
27
27
|
[initialValue]
|
|
28
28
|
);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
// Create: computed(fn)
|
|
32
|
-
function createComputedCall(factory: ts.NodeFactory, fn: ts.Expression): ts.CallExpression {
|
|
31
|
+
// Create: ns.computed(fn)
|
|
32
|
+
function createComputedCall(factory: ts.NodeFactory, ns: string, fn: ts.Expression): ts.CallExpression {
|
|
33
33
|
return factory.createCallExpression(
|
|
34
|
-
factory.createIdentifier('computed'),
|
|
34
|
+
factory.createPropertyAccessExpression(factory.createIdentifier(ns), 'computed'),
|
|
35
35
|
undefined,
|
|
36
36
|
[fn]
|
|
37
37
|
);
|
|
@@ -55,19 +55,19 @@ function createArraySetCall(factory: ts.NodeFactory, arrayExpr: ts.Expression, i
|
|
|
55
55
|
);
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
// Create: new ReactiveArray(elements...)
|
|
59
|
-
function createReactiveArrayNew(factory: ts.NodeFactory, elements: ts.Expression[]): ts.NewExpression {
|
|
58
|
+
// Create: new ns.ReactiveArray(elements...)
|
|
59
|
+
function createReactiveArrayNew(factory: ts.NodeFactory, ns: string, elements: ts.Expression[]): ts.NewExpression {
|
|
60
60
|
return factory.createNewExpression(
|
|
61
|
-
factory.createIdentifier('ReactiveArray'),
|
|
61
|
+
factory.createPropertyAccessExpression(factory.createIdentifier(ns), 'ReactiveArray'),
|
|
62
62
|
undefined,
|
|
63
63
|
elements
|
|
64
64
|
);
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
// Create: dispose(expr)
|
|
68
|
-
function createDisposeCall(factory: ts.NodeFactory, expr: ts.Expression): ts.CallExpression {
|
|
67
|
+
// Create: ns.dispose(expr)
|
|
68
|
+
function createDisposeCall(factory: ts.NodeFactory, ns: string, expr: ts.Expression): ts.CallExpression {
|
|
69
69
|
return factory.createCallExpression(
|
|
70
|
-
factory.createIdentifier('dispose'),
|
|
70
|
+
factory.createPropertyAccessExpression(factory.createIdentifier(ns), 'dispose'),
|
|
71
71
|
undefined,
|
|
72
72
|
[expr]
|
|
73
73
|
);
|
|
@@ -85,9 +85,10 @@ function createCommaExpr(factory: ts.NodeFactory, first: ts.Expression, second:
|
|
|
85
85
|
);
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
// Create: ((tmp) => (set(name, tmp op delta), tmp))(name.value)
|
|
88
|
+
// Create: ((tmp) => (ns.set(name, tmp op delta), tmp))(name.value)
|
|
89
89
|
function createPostfixIncrementExpr(
|
|
90
90
|
factory: ts.NodeFactory,
|
|
91
|
+
ns: string,
|
|
91
92
|
tmpName: string,
|
|
92
93
|
signalName: string,
|
|
93
94
|
op: ts.SyntaxKind.PlusToken | ts.SyntaxKind.MinusToken
|
|
@@ -107,6 +108,7 @@ function createPostfixIncrementExpr(
|
|
|
107
108
|
factory,
|
|
108
109
|
createSetCall(
|
|
109
110
|
factory,
|
|
111
|
+
ns,
|
|
110
112
|
signalIdent,
|
|
111
113
|
factory.createBinaryExpression(
|
|
112
114
|
tmpIdent,
|
package/src/transformer/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { uid } from '@esportsplus/typescript/transformer';
|
|
2
|
+
import type { Bindings, Namespaces } from '~/types';
|
|
2
3
|
import { createArrayTransformer } from './transforms/array';
|
|
3
4
|
import { createObjectTransformer, type GeneratedClass } from './transforms/object';
|
|
4
5
|
import { createPrimitivesTransformer } from './transforms/primitives';
|
|
@@ -6,20 +7,9 @@ import { mightNeedTransform } from './detector';
|
|
|
6
7
|
import { ts } from '@esportsplus/typescript';
|
|
7
8
|
|
|
8
9
|
|
|
9
|
-
interface ExtraImport {
|
|
10
|
-
module: string;
|
|
11
|
-
specifier: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const EXTRA_IMPORTS: ExtraImport[] = [
|
|
15
|
-
{ module: '@esportsplus/reactivity/constants', specifier: 'REACTIVE_OBJECT' },
|
|
16
|
-
{ module: '@esportsplus/reactivity/reactive/array', specifier: 'ReactiveArray' }
|
|
17
|
-
];
|
|
18
|
-
|
|
19
|
-
|
|
20
10
|
function addImportsTransformer(
|
|
21
11
|
neededImports: Set<string>,
|
|
22
|
-
|
|
12
|
+
ns: Namespaces
|
|
23
13
|
): (context: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile {
|
|
24
14
|
return (context: ts.TransformationContext) => {
|
|
25
15
|
return (sourceFile: ts.SourceFile): ts.SourceFile => {
|
|
@@ -27,59 +17,65 @@ function addImportsTransformer(
|
|
|
27
17
|
return sourceFile;
|
|
28
18
|
}
|
|
29
19
|
|
|
30
|
-
let
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
for (let i = 0, n = extraImports.length; i < n; i++) {
|
|
36
|
-
extraSpecifiers.add(extraImports[i].specifier);
|
|
37
|
-
}
|
|
20
|
+
let factory = context.factory,
|
|
21
|
+
needsArray = false,
|
|
22
|
+
needsConstants = false,
|
|
23
|
+
needsReactivity = false,
|
|
24
|
+
newStatements: ts.Statement[] = [];
|
|
38
25
|
|
|
39
26
|
for (let imp of neededImports) {
|
|
40
|
-
if (
|
|
41
|
-
|
|
27
|
+
if (imp === 'ReactiveArray') {
|
|
28
|
+
needsArray = true;
|
|
29
|
+
}
|
|
30
|
+
else if (imp === 'REACTIVE_OBJECT') {
|
|
31
|
+
needsConstants = true;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
needsReactivity = true;
|
|
42
35
|
}
|
|
43
36
|
}
|
|
44
37
|
|
|
45
|
-
// Add
|
|
46
|
-
if (
|
|
38
|
+
// Add namespace imports
|
|
39
|
+
if (needsReactivity) {
|
|
47
40
|
newStatements.push(
|
|
48
41
|
factory.createImportDeclaration(
|
|
49
42
|
undefined,
|
|
50
43
|
factory.createImportClause(
|
|
51
44
|
false,
|
|
52
45
|
undefined,
|
|
53
|
-
factory.
|
|
54
|
-
reactivitySpecifiers.map(s =>
|
|
55
|
-
factory.createImportSpecifier(false, undefined, factory.createIdentifier(s))
|
|
56
|
-
)
|
|
57
|
-
)
|
|
46
|
+
factory.createNamespaceImport(factory.createIdentifier(ns.reactivity))
|
|
58
47
|
),
|
|
59
48
|
factory.createStringLiteral('@esportsplus/reactivity')
|
|
60
49
|
)
|
|
61
50
|
);
|
|
62
51
|
}
|
|
63
52
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
53
|
+
if (needsArray) {
|
|
54
|
+
newStatements.push(
|
|
55
|
+
factory.createImportDeclaration(
|
|
56
|
+
undefined,
|
|
57
|
+
factory.createImportClause(
|
|
58
|
+
false,
|
|
59
|
+
undefined,
|
|
60
|
+
factory.createNamespaceImport(factory.createIdentifier(ns.array))
|
|
61
|
+
),
|
|
62
|
+
factory.createStringLiteral('@esportsplus/reactivity/reactive/array')
|
|
63
|
+
)
|
|
64
|
+
);
|
|
65
|
+
}
|
|
67
66
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
if (needsConstants) {
|
|
68
|
+
newStatements.push(
|
|
69
|
+
factory.createImportDeclaration(
|
|
70
|
+
undefined,
|
|
71
|
+
factory.createImportClause(
|
|
72
|
+
false,
|
|
71
73
|
undefined,
|
|
72
|
-
factory.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
])
|
|
78
|
-
),
|
|
79
|
-
factory.createStringLiteral(extra.module)
|
|
80
|
-
)
|
|
81
|
-
);
|
|
82
|
-
}
|
|
74
|
+
factory.createNamespaceImport(factory.createIdentifier(ns.constants))
|
|
75
|
+
),
|
|
76
|
+
factory.createStringLiteral('@esportsplus/reactivity/constants')
|
|
77
|
+
)
|
|
78
|
+
);
|
|
83
79
|
}
|
|
84
80
|
|
|
85
81
|
// Insert new imports after existing imports
|
|
@@ -154,10 +150,15 @@ const createTransformer = (): ts.TransformerFactory<ts.SourceFile> => {
|
|
|
154
150
|
|
|
155
151
|
let bindings: Bindings = new Map(),
|
|
156
152
|
generatedClasses: GeneratedClass[] = [],
|
|
157
|
-
neededImports = new Set<string>()
|
|
153
|
+
neededImports = new Set<string>(),
|
|
154
|
+
ns: Namespaces = {
|
|
155
|
+
array: uid('ra'),
|
|
156
|
+
constants: uid('rc'),
|
|
157
|
+
reactivity: uid('r')
|
|
158
|
+
};
|
|
158
159
|
|
|
159
160
|
// Run object transformer first (generates classes, tracks array bindings)
|
|
160
|
-
let objectTransformer = createObjectTransformer(bindings, neededImports, generatedClasses)(context);
|
|
161
|
+
let objectTransformer = createObjectTransformer(bindings, neededImports, generatedClasses, ns)(context);
|
|
161
162
|
|
|
162
163
|
sourceFile = objectTransformer(sourceFile);
|
|
163
164
|
|
|
@@ -167,7 +168,7 @@ const createTransformer = (): ts.TransformerFactory<ts.SourceFile> => {
|
|
|
167
168
|
sourceFile = arrayTransformer(sourceFile);
|
|
168
169
|
|
|
169
170
|
// Run primitives transformer (handles signal/computed, reads/writes)
|
|
170
|
-
let primitivesTransformer = createPrimitivesTransformer(bindings, neededImports)(context);
|
|
171
|
+
let primitivesTransformer = createPrimitivesTransformer(bindings, neededImports, ns)(context);
|
|
171
172
|
|
|
172
173
|
sourceFile = primitivesTransformer(sourceFile);
|
|
173
174
|
|
|
@@ -176,8 +177,8 @@ const createTransformer = (): ts.TransformerFactory<ts.SourceFile> => {
|
|
|
176
177
|
|
|
177
178
|
sourceFile = classInserter(sourceFile);
|
|
178
179
|
|
|
179
|
-
// Add
|
|
180
|
-
let importAdder = addImportsTransformer(neededImports,
|
|
180
|
+
// Add namespace imports
|
|
181
|
+
let importAdder = addImportsTransformer(neededImports, ns)(context);
|
|
181
182
|
|
|
182
183
|
sourceFile = importAdder(sourceFile);
|
|
183
184
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { uid } from '@esportsplus/typescript/transformer';
|
|
2
|
-
import type { Bindings } from '~/types';
|
|
2
|
+
import type { Bindings, Namespaces } from '~/types';
|
|
3
3
|
import { ts } from '@esportsplus/typescript';
|
|
4
4
|
|
|
5
5
|
|
|
@@ -23,6 +23,7 @@ interface TransformContext {
|
|
|
23
23
|
generatedClasses: GeneratedClass[];
|
|
24
24
|
hasReactiveImport: boolean;
|
|
25
25
|
neededImports: Set<string>;
|
|
26
|
+
ns: Namespaces;
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
|
|
@@ -65,11 +66,13 @@ function buildReactiveClass(
|
|
|
65
66
|
|
|
66
67
|
needsImports.add('REACTIVE_OBJECT');
|
|
67
68
|
|
|
68
|
-
// [REACTIVE_OBJECT] = true
|
|
69
|
+
// [ns.constants.REACTIVE_OBJECT] = true
|
|
69
70
|
members.push(
|
|
70
71
|
factory.createPropertyDeclaration(
|
|
71
72
|
undefined,
|
|
72
|
-
factory.createComputedPropertyName(
|
|
73
|
+
factory.createComputedPropertyName(
|
|
74
|
+
factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.constants), 'REACTIVE_OBJECT')
|
|
75
|
+
),
|
|
73
76
|
undefined,
|
|
74
77
|
undefined,
|
|
75
78
|
factory.createTrue()
|
|
@@ -89,7 +92,7 @@ function buildReactiveClass(
|
|
|
89
92
|
let privateName = factory.createPrivateIdentifier(`#${prop.key}`),
|
|
90
93
|
paramName = uid('v');
|
|
91
94
|
|
|
92
|
-
// Private field: #key = signal(value)
|
|
95
|
+
// Private field: #key = ns.signal(value)
|
|
93
96
|
members.push(
|
|
94
97
|
factory.createPropertyDeclaration(
|
|
95
98
|
undefined,
|
|
@@ -97,14 +100,14 @@ function buildReactiveClass(
|
|
|
97
100
|
undefined,
|
|
98
101
|
undefined,
|
|
99
102
|
factory.createCallExpression(
|
|
100
|
-
factory.createIdentifier('signal'),
|
|
103
|
+
factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.reactivity), 'signal'),
|
|
101
104
|
undefined,
|
|
102
105
|
[prop.value]
|
|
103
106
|
)
|
|
104
107
|
)
|
|
105
108
|
);
|
|
106
109
|
|
|
107
|
-
// Getter: get key() { return read(this.#key); }
|
|
110
|
+
// Getter: get key() { return ns.read(this.#key); }
|
|
108
111
|
members.push(
|
|
109
112
|
factory.createGetAccessorDeclaration(
|
|
110
113
|
undefined,
|
|
@@ -114,7 +117,7 @@ function buildReactiveClass(
|
|
|
114
117
|
factory.createBlock([
|
|
115
118
|
factory.createReturnStatement(
|
|
116
119
|
factory.createCallExpression(
|
|
117
|
-
factory.createIdentifier('read'),
|
|
120
|
+
factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.reactivity), 'read'),
|
|
118
121
|
undefined,
|
|
119
122
|
[factory.createPropertyAccessExpression(factory.createThis(), privateName)]
|
|
120
123
|
)
|
|
@@ -123,7 +126,7 @@ function buildReactiveClass(
|
|
|
123
126
|
)
|
|
124
127
|
);
|
|
125
128
|
|
|
126
|
-
// Setter: set key(v) { set(this.#key, v); }
|
|
129
|
+
// Setter: set key(v) { ns.set(this.#key, v); }
|
|
127
130
|
members.push(
|
|
128
131
|
factory.createSetAccessorDeclaration(
|
|
129
132
|
undefined,
|
|
@@ -132,7 +135,7 @@ function buildReactiveClass(
|
|
|
132
135
|
factory.createBlock([
|
|
133
136
|
factory.createExpressionStatement(
|
|
134
137
|
factory.createCallExpression(
|
|
135
|
-
factory.createIdentifier('set'),
|
|
138
|
+
factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.reactivity), 'set'),
|
|
136
139
|
undefined,
|
|
137
140
|
[
|
|
138
141
|
factory.createPropertyAccessExpression(factory.createThis(), privateName),
|
|
@@ -147,7 +150,7 @@ function buildReactiveClass(
|
|
|
147
150
|
else if (prop.type === 'array') {
|
|
148
151
|
needsImports.add('ReactiveArray');
|
|
149
152
|
|
|
150
|
-
// Public field: key = new ReactiveArray(elements...)
|
|
153
|
+
// Public field: key = new ns.array.ReactiveArray(elements...)
|
|
151
154
|
members.push(
|
|
152
155
|
factory.createPropertyDeclaration(
|
|
153
156
|
undefined,
|
|
@@ -155,7 +158,7 @@ function buildReactiveClass(
|
|
|
155
158
|
undefined,
|
|
156
159
|
undefined,
|
|
157
160
|
factory.createNewExpression(
|
|
158
|
-
factory.createIdentifier('ReactiveArray'),
|
|
161
|
+
factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.array), 'ReactiveArray'),
|
|
159
162
|
undefined,
|
|
160
163
|
prop.elements || []
|
|
161
164
|
)
|
|
@@ -204,7 +207,7 @@ function buildReactiveClass(
|
|
|
204
207
|
)
|
|
205
208
|
);
|
|
206
209
|
|
|
207
|
-
// Getter: get key() { return read(this.#key ??= computed(fn)); }
|
|
210
|
+
// Getter: get key() { return ns.read(this.#key ??= ns.computed(fn)); }
|
|
208
211
|
members.push(
|
|
209
212
|
factory.createGetAccessorDeclaration(
|
|
210
213
|
undefined,
|
|
@@ -214,14 +217,14 @@ function buildReactiveClass(
|
|
|
214
217
|
factory.createBlock([
|
|
215
218
|
factory.createReturnStatement(
|
|
216
219
|
factory.createCallExpression(
|
|
217
|
-
factory.createIdentifier('read'),
|
|
220
|
+
factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.reactivity), 'read'),
|
|
218
221
|
undefined,
|
|
219
222
|
[
|
|
220
223
|
factory.createBinaryExpression(
|
|
221
224
|
factory.createPropertyAccessExpression(factory.createThis(), privateName),
|
|
222
225
|
ts.SyntaxKind.QuestionQuestionEqualsToken,
|
|
223
226
|
factory.createCallExpression(
|
|
224
|
-
factory.createIdentifier('computed'),
|
|
227
|
+
factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.reactivity), 'computed'),
|
|
225
228
|
undefined,
|
|
226
229
|
[prop.value]
|
|
227
230
|
)
|
|
@@ -233,13 +236,13 @@ function buildReactiveClass(
|
|
|
233
236
|
)
|
|
234
237
|
);
|
|
235
238
|
|
|
236
|
-
// dispose: if (this.#key) dispose(this.#key)
|
|
239
|
+
// dispose: if (this.#key) ns.dispose(this.#key)
|
|
237
240
|
disposeStatements.push(
|
|
238
241
|
factory.createIfStatement(
|
|
239
242
|
factory.createPropertyAccessExpression(factory.createThis(), privateName),
|
|
240
243
|
factory.createExpressionStatement(
|
|
241
244
|
factory.createCallExpression(
|
|
242
|
-
factory.createIdentifier('dispose'),
|
|
245
|
+
factory.createPropertyAccessExpression(factory.createIdentifier(ctx.ns.reactivity), 'dispose'),
|
|
243
246
|
undefined,
|
|
244
247
|
[factory.createPropertyAccessExpression(factory.createThis(), privateName)]
|
|
245
248
|
)
|
|
@@ -306,7 +309,7 @@ function visit(ctx: TransformContext, node: ts.Node): ts.Node | ts.Node[] {
|
|
|
306
309
|
) {
|
|
307
310
|
let arg = node.arguments[0];
|
|
308
311
|
|
|
309
|
-
// Handle reactive([...]) → new ReactiveArray(...)
|
|
312
|
+
// Handle reactive([...]) → new ns.array.ReactiveArray(...)
|
|
310
313
|
if (arg && ts.isArrayLiteralExpression(arg)) {
|
|
311
314
|
let varName: string | null = null;
|
|
312
315
|
|
|
@@ -318,7 +321,7 @@ function visit(ctx: TransformContext, node: ts.Node): ts.Node | ts.Node[] {
|
|
|
318
321
|
ctx.neededImports.add('ReactiveArray');
|
|
319
322
|
|
|
320
323
|
return ctx.factory.createNewExpression(
|
|
321
|
-
ctx.factory.createIdentifier('ReactiveArray'),
|
|
324
|
+
ctx.factory.createPropertyAccessExpression(ctx.factory.createIdentifier(ctx.ns.array), 'ReactiveArray'),
|
|
322
325
|
undefined,
|
|
323
326
|
[...arg.elements]
|
|
324
327
|
);
|
|
@@ -378,7 +381,8 @@ function visit(ctx: TransformContext, node: ts.Node): ts.Node | ts.Node[] {
|
|
|
378
381
|
const createObjectTransformer = (
|
|
379
382
|
bindings: Bindings,
|
|
380
383
|
neededImports: Set<string>,
|
|
381
|
-
generatedClasses: GeneratedClass[]
|
|
384
|
+
generatedClasses: GeneratedClass[],
|
|
385
|
+
ns: Namespaces
|
|
382
386
|
): (context: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile => {
|
|
383
387
|
return (context: ts.TransformationContext) => {
|
|
384
388
|
return (sourceFile: ts.SourceFile): ts.SourceFile => {
|
|
@@ -388,7 +392,8 @@ const createObjectTransformer = (
|
|
|
388
392
|
factory: context.factory,
|
|
389
393
|
generatedClasses,
|
|
390
394
|
hasReactiveImport: false,
|
|
391
|
-
neededImports
|
|
395
|
+
neededImports,
|
|
396
|
+
ns
|
|
392
397
|
};
|
|
393
398
|
|
|
394
399
|
return ts.visitNode(sourceFile, n => visit(ctx, n)) as ts.SourceFile;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { uid } from '@esportsplus/typescript/transformer';
|
|
2
|
-
import type { BindingType, Bindings } from '~/types';
|
|
2
|
+
import type { BindingType, Bindings, Namespaces } from '~/types';
|
|
3
3
|
import {
|
|
4
4
|
createCommaExpr,
|
|
5
5
|
createComputedCall,
|
|
@@ -23,6 +23,7 @@ interface TransformContext {
|
|
|
23
23
|
factory: ts.NodeFactory;
|
|
24
24
|
hasReactiveImport: boolean;
|
|
25
25
|
neededImports: Set<string>;
|
|
26
|
+
ns: Namespaces;
|
|
26
27
|
scopedBindings: ScopeBinding[];
|
|
27
28
|
}
|
|
28
29
|
|
|
@@ -245,12 +246,12 @@ function visit(ctx: TransformContext, node: ts.Node): ts.Node {
|
|
|
245
246
|
// Transform the function body to wrap reactive reads
|
|
246
247
|
let transformedArg = ts.visitEachChild(arg, n => visitComputedArg(ctx, n), ctx.context);
|
|
247
248
|
|
|
248
|
-
return createComputedCall(ctx.factory, transformedArg as ts.Expression);
|
|
249
|
+
return createComputedCall(ctx.factory, ctx.ns.reactivity, transformedArg as ts.Expression);
|
|
249
250
|
}
|
|
250
251
|
else {
|
|
251
252
|
ctx.neededImports.add('signal');
|
|
252
253
|
|
|
253
|
-
return createSignalCall(ctx.factory, arg);
|
|
254
|
+
return createSignalCall(ctx.factory, ctx.ns.reactivity, arg);
|
|
254
255
|
}
|
|
255
256
|
}
|
|
256
257
|
}
|
|
@@ -271,16 +272,17 @@ function visit(ctx: TransformContext, node: ts.Node): ts.Node {
|
|
|
271
272
|
transformedRight = ts.visitEachChild(node.right, n => visit(ctx, n), ctx.context) as ts.Expression;
|
|
272
273
|
|
|
273
274
|
if (assignType === 'simple') {
|
|
274
|
-
// x = value → set(x, value)
|
|
275
|
-
return createSetCall(factory, signalIdent, transformedRight);
|
|
275
|
+
// x = value → ns.set(x, value)
|
|
276
|
+
return createSetCall(factory, ctx.ns.reactivity, signalIdent, transformedRight);
|
|
276
277
|
}
|
|
277
278
|
else {
|
|
278
|
-
// x += value → set(x, x.value + value)
|
|
279
|
+
// x += value → ns.set(x, x.value + value)
|
|
279
280
|
let op = getCompoundOperator(node.operatorToken.kind),
|
|
280
281
|
valueAccess = factory.createPropertyAccessExpression(signalIdent, 'value');
|
|
281
282
|
|
|
282
283
|
return createSetCall(
|
|
283
284
|
factory,
|
|
285
|
+
ctx.ns.reactivity,
|
|
284
286
|
signalIdent,
|
|
285
287
|
factory.createBinaryExpression(valueAccess, op, transformedRight)
|
|
286
288
|
);
|
|
@@ -306,19 +308,21 @@ function visit(ctx: TransformContext, node: ts.Node): ts.Node {
|
|
|
306
308
|
valueAccess = factory.createPropertyAccessExpression(signalIdent, 'value');
|
|
307
309
|
|
|
308
310
|
if (node.parent && ts.isExpressionStatement(node.parent)) {
|
|
309
|
-
// ++x as statement → set(x, x.value + 1)
|
|
311
|
+
// ++x as statement → ns.set(x, x.value + 1)
|
|
310
312
|
return createSetCall(
|
|
311
313
|
factory,
|
|
314
|
+
ctx.ns.reactivity,
|
|
312
315
|
signalIdent,
|
|
313
316
|
factory.createBinaryExpression(valueAccess, delta, factory.createNumericLiteral(1))
|
|
314
317
|
);
|
|
315
318
|
}
|
|
316
319
|
else {
|
|
317
|
-
// ++x in expression → (set(x, x.value + 1), x.value)
|
|
320
|
+
// ++x in expression → (ns.set(x, x.value + 1), x.value)
|
|
318
321
|
return createCommaExpr(
|
|
319
322
|
factory,
|
|
320
323
|
createSetCall(
|
|
321
324
|
factory,
|
|
325
|
+
ctx.ns.reactivity,
|
|
322
326
|
signalIdent,
|
|
323
327
|
factory.createBinaryExpression(valueAccess, delta, factory.createNumericLiteral(1))
|
|
324
328
|
),
|
|
@@ -346,16 +350,17 @@ function visit(ctx: TransformContext, node: ts.Node): ts.Node {
|
|
|
346
350
|
valueAccess = factory.createPropertyAccessExpression(signalIdent, 'value');
|
|
347
351
|
|
|
348
352
|
if (node.parent && ts.isExpressionStatement(node.parent)) {
|
|
349
|
-
// x++ as statement → set(x, x.value + 1)
|
|
353
|
+
// x++ as statement → ns.set(x, x.value + 1)
|
|
350
354
|
return createSetCall(
|
|
351
355
|
factory,
|
|
356
|
+
ctx.ns.reactivity,
|
|
352
357
|
signalIdent,
|
|
353
358
|
factory.createBinaryExpression(valueAccess, delta, factory.createNumericLiteral(1))
|
|
354
359
|
);
|
|
355
360
|
}
|
|
356
361
|
else {
|
|
357
|
-
// x++ in expression → ((tmp) => (set(x, tmp + 1), tmp))(x.value)
|
|
358
|
-
return createPostfixIncrementExpr(factory, uid('tmp'), name, delta);
|
|
362
|
+
// x++ in expression → ((tmp) => (ns.set(x, tmp + 1), tmp))(x.value)
|
|
363
|
+
return createPostfixIncrementExpr(factory, ctx.ns.reactivity, uid('tmp'), name, delta);
|
|
359
364
|
}
|
|
360
365
|
}
|
|
361
366
|
}
|
|
@@ -389,10 +394,10 @@ function visit(ctx: TransformContext, node: ts.Node): ts.Node {
|
|
|
389
394
|
let binding = findBinding(ctx.scopedBindings, node.text, node);
|
|
390
395
|
|
|
391
396
|
if (binding) {
|
|
392
|
-
// Read access → read(x)
|
|
397
|
+
// Read access → ns.read(x)
|
|
393
398
|
ctx.neededImports.add('read');
|
|
394
399
|
|
|
395
|
-
return createReadCall(ctx.factory, ctx.factory.createIdentifier(node.text));
|
|
400
|
+
return createReadCall(ctx.factory, ctx.ns.reactivity, ctx.factory.createIdentifier(node.text));
|
|
396
401
|
}
|
|
397
402
|
}
|
|
398
403
|
|
|
@@ -416,7 +421,7 @@ function visitComputedArg(ctx: TransformContext, node: ts.Node): ts.Node {
|
|
|
416
421
|
if (binding) {
|
|
417
422
|
ctx.neededImports.add('read');
|
|
418
423
|
|
|
419
|
-
return createReadCall(ctx.factory, ctx.factory.createIdentifier(node.text));
|
|
424
|
+
return createReadCall(ctx.factory, ctx.ns.reactivity, ctx.factory.createIdentifier(node.text));
|
|
420
425
|
}
|
|
421
426
|
}
|
|
422
427
|
|
|
@@ -426,7 +431,8 @@ function visitComputedArg(ctx: TransformContext, node: ts.Node): ts.Node {
|
|
|
426
431
|
|
|
427
432
|
const createPrimitivesTransformer = (
|
|
428
433
|
bindings: Bindings,
|
|
429
|
-
neededImports: Set<string
|
|
434
|
+
neededImports: Set<string>,
|
|
435
|
+
ns: Namespaces
|
|
430
436
|
): (context: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile => {
|
|
431
437
|
return (context: ts.TransformationContext) => {
|
|
432
438
|
return (sourceFile: ts.SourceFile): ts.SourceFile => {
|
|
@@ -436,6 +442,7 @@ const createPrimitivesTransformer = (
|
|
|
436
442
|
factory: context.factory,
|
|
437
443
|
hasReactiveImport: false,
|
|
438
444
|
neededImports,
|
|
445
|
+
ns,
|
|
439
446
|
scopedBindings: []
|
|
440
447
|
};
|
|
441
448
|
|
package/src/types.ts
CHANGED
|
@@ -6,6 +6,12 @@ type BindingType = 'array' | 'computed' | 'object' | 'signal';
|
|
|
6
6
|
|
|
7
7
|
type Bindings = Map<string, BindingType>;
|
|
8
8
|
|
|
9
|
+
interface Namespaces {
|
|
10
|
+
array: string;
|
|
11
|
+
constants: string;
|
|
12
|
+
reactivity: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
9
15
|
interface Computed<T> {
|
|
10
16
|
cleanup: VoidFunction | VoidFunction[] | null;
|
|
11
17
|
deps: Link | null;
|
|
@@ -48,6 +54,7 @@ export type {
|
|
|
48
54
|
Bindings,
|
|
49
55
|
Computed,
|
|
50
56
|
Link,
|
|
57
|
+
Namespaces,
|
|
51
58
|
ReactiveArray,
|
|
52
59
|
ReactiveObject,
|
|
53
60
|
Signal
|