@esportsplus/reactivity 0.37.2 → 0.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/compiler/array.d.ts +8 -1
- package/build/compiler/array.js +109 -93
- package/build/compiler/bindings.d.ts +18 -12
- package/build/compiler/bindings.js +172 -52
- package/build/compiler/constants.d.ts +1 -0
- package/build/compiler/constants.js +1 -0
- package/build/compiler/index.d.ts +11 -3
- package/build/compiler/index.js +96 -34
- package/build/compiler/object.js +11 -14
- package/build/compiler/plugins/vite.d.ts +14 -0
- package/build/compiler/primitives.d.ts +6 -0
- package/build/compiler/primitives.js +99 -67
- package/build/compiler/types.d.ts +5 -4
- package/build/types.d.ts +1 -1
- package/package.json +2 -2
- package/pnpm-workspace.yaml +1 -1
- package/src/compiler/array.ts +162 -120
- package/src/compiler/bindings.ts +259 -56
- package/src/compiler/constants.ts +1 -0
- package/src/compiler/index.ts +142 -46
- package/src/compiler/object.ts +17 -17
- package/src/compiler/primitives.ts +139 -88
- package/src/compiler/types.ts +7 -4
- package/src/types.ts +2 -1
- package/test/compiler/compiler.test.ts +152 -48
- package/test/compiler/coverage.test.ts +164 -0
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
2
|
import type { ReplacementIntent } from '@esportsplus/typescript/compiler';
|
|
3
3
|
import type { Bindings, IsReactiveCall } from './types.js';
|
|
4
|
+
import type { Failure } from './primitives.js';
|
|
5
|
+
type ArrayTransformResult = {
|
|
6
|
+
failures: Failure[];
|
|
7
|
+
namespaced: boolean;
|
|
8
|
+
reactive: ts.Expression[];
|
|
9
|
+
replacements: ReplacementIntent[];
|
|
10
|
+
};
|
|
4
11
|
export default _default;
|
|
5
|
-
declare function _default(sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall):
|
|
12
|
+
declare function _default(sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): ArrayTransformResult;
|
package/build/compiler/array.js
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
|
-
import { COMPOUND_OPERATORS, NAMESPACE
|
|
2
|
+
import { COMPOUND_OPERATORS, NAMESPACE } from './constants.js';
|
|
3
3
|
import scope from './bindings.js';
|
|
4
|
+
function collect(node, candidates) {
|
|
5
|
+
if (ts.isTypeNode(node)) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
if (ts.isPropertyAccessExpression(node) && node.name.text === 'length') {
|
|
9
|
+
candidates.lengths.push(node);
|
|
10
|
+
}
|
|
11
|
+
else if (ts.isElementAccessExpression(node) && isWritten(node)) {
|
|
12
|
+
candidates.writes.push(node);
|
|
13
|
+
}
|
|
14
|
+
else if (ts.isBinaryExpression(node) &&
|
|
15
|
+
node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
|
16
|
+
ts.isIdentifier(node.left) &&
|
|
17
|
+
ts.isArrayLiteralExpression(scope.unwrap(node.right))) {
|
|
18
|
+
candidates.reassignments.push(node);
|
|
19
|
+
}
|
|
20
|
+
node.forEachChild(child => collect(child, candidates));
|
|
21
|
+
}
|
|
4
22
|
function declare(ctx, node) {
|
|
5
23
|
if (ctx.isReactiveCall(node) && node.arguments.length > 0) {
|
|
6
24
|
let arg = node.arguments[0], expression = ts.isAsExpression(arg) ? arg.expression : arg;
|
|
@@ -12,9 +30,6 @@ function declare(ctx, node) {
|
|
|
12
30
|
else if (node.parent && ts.isVariableDeclaration(node.parent) && node.parent.type) {
|
|
13
31
|
elementType = getElementTypeText(node.parent.type, ctx.sourceFile);
|
|
14
32
|
}
|
|
15
|
-
if (node.parent && ts.isVariableDeclaration(node.parent) && ts.isIdentifier(node.parent.name)) {
|
|
16
|
-
scope.declare(ctx.bindings, node.parent.name, TYPES.Array);
|
|
17
|
-
}
|
|
18
33
|
let typeParam = elementType ? `<${elementType}>` : '';
|
|
19
34
|
ctx.replacements.push({
|
|
20
35
|
node,
|
|
@@ -24,25 +39,6 @@ function declare(ctx, node) {
|
|
|
24
39
|
});
|
|
25
40
|
}
|
|
26
41
|
}
|
|
27
|
-
if (ts.isVariableDeclaration(node) &&
|
|
28
|
-
ts.isIdentifier(node.name) &&
|
|
29
|
-
node.initializer &&
|
|
30
|
-
(ts.isIdentifier(node.initializer) || ts.isPropertyAccessExpression(node.initializer)) &&
|
|
31
|
-
scope.isArray(ctx.bindings, node.initializer)) {
|
|
32
|
-
scope.declare(ctx.bindings, node.name, TYPES.Array);
|
|
33
|
-
}
|
|
34
|
-
if ((ts.isFunctionDeclaration(node) || ts.isArrowFunction(node)) && node.parameters) {
|
|
35
|
-
for (let i = 0, n = node.parameters.length; i < n; i++) {
|
|
36
|
-
let param = node.parameters[i];
|
|
37
|
-
if (ts.isIdentifier(param.name) &&
|
|
38
|
-
param.type &&
|
|
39
|
-
ts.isTypeReferenceNode(param.type) &&
|
|
40
|
-
ts.isIdentifier(param.type.typeName) &&
|
|
41
|
-
param.type.typeName.text === 'ReactiveArray') {
|
|
42
|
-
scope.declare(ctx.bindings, param.name, TYPES.Array);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
42
|
node.forEachChild(child => declare(ctx, child));
|
|
47
43
|
}
|
|
48
44
|
function getElementTypeText(typeNode, sourceFile) {
|
|
@@ -58,82 +54,102 @@ function getElementTypeText(typeNode, sourceFile) {
|
|
|
58
54
|
}
|
|
59
55
|
return null;
|
|
60
56
|
}
|
|
61
|
-
function
|
|
62
|
-
|
|
63
|
-
|
|
57
|
+
function isWritten(access) {
|
|
58
|
+
let parent = access.parent;
|
|
59
|
+
if (!parent) {
|
|
60
|
+
return false;
|
|
64
61
|
}
|
|
65
|
-
if (ts.
|
|
66
|
-
let
|
|
67
|
-
|
|
68
|
-
let op = COMPOUND_OPERATORS.get(parent.operatorToken.kind);
|
|
69
|
-
if (op === undefined) {
|
|
70
|
-
ctx.replacements.push({
|
|
71
|
-
node: parent,
|
|
72
|
-
generate: (sf) => `${expr.getText(sf)}.$length = ${parent.right.getText(sf)}`
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
ctx.replacements.push({
|
|
77
|
-
node: parent,
|
|
78
|
-
generate: (sf) => `${expr.getText(sf)}.$length = ${expr.getText(sf)}.length ${op} ${parent.right.getText(sf)}`
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
else if (parent && (ts.isPostfixUnaryExpression(parent) || ts.isPrefixUnaryExpression(parent))) {
|
|
83
|
-
let op = parent.operator === ts.SyntaxKind.PlusPlusToken ? '+' : '-';
|
|
84
|
-
ctx.replacements.push({
|
|
85
|
-
node: parent,
|
|
86
|
-
generate: (sf) => `${expr.getText(sf)}.$length = ${expr.getText(sf)}.length ${op} 1`
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
ctx.replacements.push({
|
|
91
|
-
node,
|
|
92
|
-
generate: (sf) => `${expr.getText(sf)}.$length`
|
|
93
|
-
});
|
|
94
|
-
}
|
|
62
|
+
if (ts.isBinaryExpression(parent)) {
|
|
63
|
+
let op = parent.operatorToken.kind;
|
|
64
|
+
return parent.left === access && (op === ts.SyntaxKind.EqualsToken || COMPOUND_OPERATORS.has(op));
|
|
95
65
|
}
|
|
96
|
-
if (ts.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
66
|
+
if (ts.isPostfixUnaryExpression(parent) || ts.isPrefixUnaryExpression(parent)) {
|
|
67
|
+
return parent.operator === ts.SyntaxKind.PlusPlusToken || parent.operator === ts.SyntaxKind.MinusMinusToken;
|
|
68
|
+
}
|
|
69
|
+
return ts.isDeleteExpression(parent);
|
|
70
|
+
}
|
|
71
|
+
function length(access, replacements) {
|
|
72
|
+
let expr = access.expression, parent = access.parent;
|
|
73
|
+
if (parent && ts.isBinaryExpression(parent) && parent.left === access && (parent.operatorToken.kind === ts.SyntaxKind.EqualsToken || COMPOUND_OPERATORS.has(parent.operatorToken.kind))) {
|
|
74
|
+
let op = COMPOUND_OPERATORS.get(parent.operatorToken.kind);
|
|
75
|
+
replacements.push({
|
|
76
|
+
node: parent,
|
|
77
|
+
generate: (sf) => op === undefined
|
|
78
|
+
? `${expr.getText(sf)}.$length = ${parent.right.getText(sf)}`
|
|
79
|
+
: `${expr.getText(sf)}.$length = ${expr.getText(sf)}.length ${op} ${parent.right.getText(sf)}`
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
else if (parent && (ts.isPostfixUnaryExpression(parent) || ts.isPrefixUnaryExpression(parent))) {
|
|
83
|
+
let op = parent.operator === ts.SyntaxKind.PlusPlusToken ? '+' : '-';
|
|
84
|
+
replacements.push({
|
|
85
|
+
node: parent,
|
|
86
|
+
generate: (sf) => `${expr.getText(sf)}.$length = ${expr.getText(sf)}.length ${op} 1`
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
replacements.push({
|
|
91
|
+
node: access,
|
|
92
|
+
generate: (sf) => `${expr.getText(sf)}.$length`
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function write(access, ctx) {
|
|
97
|
+
let parent = access.parent, array = (sf) => access.expression.getText(sf), index = (sf) => access.argumentExpression.getText(sf);
|
|
98
|
+
if (ts.isDeleteExpression(parent)) {
|
|
99
|
+
ctx.failures.push({ message: 'an element of a reactive array cannot be deleted; use splice()', node: parent });
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (ts.isBinaryExpression(parent)) {
|
|
103
|
+
let op = COMPOUND_OPERATORS.get(parent.operatorToken.kind), right = parent.right;
|
|
101
104
|
ctx.replacements.push({
|
|
102
|
-
node,
|
|
103
|
-
generate: (sf) =>
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
)`
|
|
105
|
+
node: parent,
|
|
106
|
+
generate: (sf) => op === undefined
|
|
107
|
+
? `${array(sf)}.$set(${index(sf)}, ${right.getText(sf)})`
|
|
108
|
+
: `((_a, _k) => _a.$set(_k, _a[_k] ${op} (${right.getText(sf)})))(${array(sf)}, ${index(sf)})`
|
|
107
109
|
});
|
|
110
|
+
return;
|
|
108
111
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
112
|
+
let unary = parent, op = unary.operator === ts.SyntaxKind.PlusPlusToken ? '+' : '-';
|
|
113
|
+
ctx.replacements.push({
|
|
114
|
+
node: unary,
|
|
115
|
+
generate: (sf) => ts.isPrefixUnaryExpression(unary)
|
|
116
|
+
? `((_a, _k) => (_a.$set(_k, _a[_k] ${op} 1), _a[_k]))(${array(sf)}, ${index(sf)})`
|
|
117
|
+
: `((_a, _k, _v) => (_a.$set(_k, _v ${op} 1), _v))(${array(sf)}, ${index(sf)}, ${array(sf)}[${index(sf)}])`
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
export default (sourceFile, bindings, isReactiveCall) => {
|
|
121
|
+
let candidates = { lengths: [], reassignments: [], writes: [] }, result = { failures: [], namespaced: false, reactive: [], replacements: [] };
|
|
122
|
+
declare({ isReactiveCall, replacements: result.replacements, sourceFile }, sourceFile);
|
|
123
|
+
result.namespaced = result.replacements.length > 0;
|
|
124
|
+
collect(sourceFile, candidates);
|
|
125
|
+
let reactive = scope.arrays(bindings, [
|
|
126
|
+
...candidates.lengths.map(access => access.expression),
|
|
127
|
+
...candidates.reassignments.map(assignment => assignment.left),
|
|
128
|
+
...candidates.writes.map(access => access.expression)
|
|
129
|
+
]);
|
|
130
|
+
result.reactive = [...reactive];
|
|
131
|
+
for (let i = 0, n = candidates.lengths.length; i < n; i++) {
|
|
132
|
+
if (reactive.has(candidates.lengths[i].expression)) {
|
|
133
|
+
length(candidates.lengths[i], result.replacements);
|
|
116
134
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
generate: (sf) => elements.elements.length > 0
|
|
122
|
-
? `${name}.splice(0, ${name}.length, ...${elements.getText(sf)})`
|
|
123
|
-
: `${name}.splice(0, ${name}.length)`
|
|
124
|
-
});
|
|
135
|
+
}
|
|
136
|
+
for (let i = 0, n = candidates.writes.length; i < n; i++) {
|
|
137
|
+
if (reactive.has(candidates.writes[i].expression)) {
|
|
138
|
+
write(candidates.writes[i], result);
|
|
125
139
|
}
|
|
126
140
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
replacements
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
141
|
+
for (let i = 0, n = candidates.reassignments.length; i < n; i++) {
|
|
142
|
+
let assignment = candidates.reassignments[i];
|
|
143
|
+
if (!reactive.has(assignment.left) || isReactiveCall(scope.unwrap(assignment.right))) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
let name = assignment.left.text, elements = scope.unwrap(assignment.right);
|
|
147
|
+
result.replacements.push({
|
|
148
|
+
node: assignment,
|
|
149
|
+
generate: (sf) => elements.elements.length > 0
|
|
150
|
+
? `${name}.splice(0, ${name}.length, ...${elements.getText(sf)})`
|
|
151
|
+
: `${name}.splice(0, ${name}.length)`
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
return result;
|
|
139
155
|
};
|
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
|
+
import type { Origin } from '@esportsplus/typescript/compiler';
|
|
2
3
|
import { TYPES } from './constants.js';
|
|
3
4
|
import type { Bindings } from './types.js';
|
|
4
|
-
declare const
|
|
5
|
-
declare const
|
|
6
|
-
declare const
|
|
7
|
-
declare const
|
|
8
|
-
declare const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
declare const arrays: (bindings: Bindings, nodes: ts.Node[]) => Set<ts.Node>;
|
|
6
|
+
declare const classify: (bindings: Bindings, call: ts.CallExpression) => TYPES | null;
|
|
7
|
+
declare const create: (checker: ts.Checker, program: ts.Program, sourceFile: ts.SourceFile) => Bindings;
|
|
8
|
+
declare const denotes: (bindings: Bindings, expression: ts.Expression) => boolean;
|
|
9
|
+
declare const holds: (bindings: Bindings, found: Origin | null) => boolean;
|
|
10
|
+
declare const isReactiveCall: (bindings: Bindings, node: ts.Node) => node is ts.CallExpression;
|
|
11
|
+
declare const kind: (bindings: Bindings, found: Origin | null) => TYPES | null;
|
|
12
|
+
declare const origin: (bindings: Bindings, node: ts.Node) => Origin | null;
|
|
13
|
+
declare const unwrap: (expression: ts.Expression) => ts.Expression;
|
|
12
14
|
declare const _default: {
|
|
15
|
+
arrays: typeof arrays;
|
|
16
|
+
classify: typeof classify;
|
|
13
17
|
create: typeof create;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
denotes: typeof denotes;
|
|
19
|
+
holds: typeof holds;
|
|
20
|
+
isReactiveCall: typeof isReactiveCall;
|
|
21
|
+
kind: typeof kind;
|
|
22
|
+
origin: typeof origin;
|
|
23
|
+
unwrap: typeof unwrap;
|
|
18
24
|
};
|
|
19
25
|
export default _default;
|
|
@@ -1,65 +1,185 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (
|
|
10
|
-
return
|
|
2
|
+
import { references } from '@esportsplus/typescript/compiler';
|
|
3
|
+
import { ENTRYPOINT, PACKAGE_NAME, TYPES } from './constants.js';
|
|
4
|
+
const ARRAY_BRAND = 'once';
|
|
5
|
+
const REACTIVE_ARRAY = 'ReactiveArray';
|
|
6
|
+
let caches = new WeakMap();
|
|
7
|
+
function assignments(bindings, file) {
|
|
8
|
+
let cache = cachesOf(bindings.program).assignments, found = cache.get(file.fileName);
|
|
9
|
+
if (found) {
|
|
10
|
+
return found;
|
|
11
11
|
}
|
|
12
|
-
|
|
12
|
+
let result = new Map(), visit = (node) => {
|
|
13
|
+
if (ts.isBinaryExpression(node) &&
|
|
14
|
+
node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
|
15
|
+
ts.isIdentifier(node.left)) {
|
|
16
|
+
let call = unwrap(node.right);
|
|
17
|
+
if (ts.isCallExpression(call)) {
|
|
18
|
+
let list = result.get(node.left.text);
|
|
19
|
+
if (!list) {
|
|
20
|
+
list = [];
|
|
21
|
+
result.set(node.left.text, list);
|
|
22
|
+
}
|
|
23
|
+
list.push({ call, target: node.left });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
node.forEachChild(visit);
|
|
27
|
+
};
|
|
28
|
+
visit(file);
|
|
29
|
+
cache.set(file.fileName, result);
|
|
30
|
+
return result;
|
|
13
31
|
}
|
|
14
|
-
function
|
|
15
|
-
let
|
|
16
|
-
if (
|
|
17
|
-
|
|
32
|
+
function cachesOf(program) {
|
|
33
|
+
let found = caches.get(program);
|
|
34
|
+
if (!found) {
|
|
35
|
+
found = { arrays: new Map(), assignments: new Map(), kinds: new Map() };
|
|
36
|
+
caches.set(program, found);
|
|
18
37
|
}
|
|
19
|
-
return
|
|
38
|
+
return found;
|
|
20
39
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (symbol) {
|
|
30
|
-
bindings.names.add(identifier.text);
|
|
31
|
-
bindings.symbols.set(symbol, type);
|
|
40
|
+
function kindOf(checker, type) {
|
|
41
|
+
let members = (type.flags & ts.TypeFlags.Union) !== 0 ? type.getTypes() : [type], result = null;
|
|
42
|
+
for (let i = 0, n = members.length; i < n; i++) {
|
|
43
|
+
let kind = kindOfType(checker, members[i]);
|
|
44
|
+
if (kind === null || (result !== null && result !== kind)) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
result = kind;
|
|
32
48
|
}
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
function kindOfType(checker, type) {
|
|
52
|
+
if ((type.flags & ts.TypeFlags.TypeParameter) !== 0) {
|
|
53
|
+
let constraint = checker.getBaseConstraintOfType(type);
|
|
54
|
+
return constraint && constraint !== type ? kindOf(checker, constraint) : null;
|
|
55
|
+
}
|
|
56
|
+
if ((type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Never | ts.TypeFlags.Unknown)) !== 0) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
if ((type.flags & (ts.TypeFlags.BigIntLike |
|
|
60
|
+
ts.TypeFlags.BooleanLike |
|
|
61
|
+
ts.TypeFlags.EnumLike |
|
|
62
|
+
ts.TypeFlags.ESSymbolLike |
|
|
63
|
+
ts.TypeFlags.Null |
|
|
64
|
+
ts.TypeFlags.NumberLike |
|
|
65
|
+
ts.TypeFlags.StringLike |
|
|
66
|
+
ts.TypeFlags.TemplateLiteral |
|
|
67
|
+
ts.TypeFlags.Undefined |
|
|
68
|
+
ts.TypeFlags.Void)) !== 0) {
|
|
69
|
+
return TYPES.Signal;
|
|
70
|
+
}
|
|
71
|
+
if (checker.isArrayType(type) || checker.isTupleType(type)) {
|
|
72
|
+
return TYPES.Array;
|
|
73
|
+
}
|
|
74
|
+
if (checker.getSignaturesOfType(type, ts.SignatureKind.Call).length > 0) {
|
|
75
|
+
return TYPES.Computed;
|
|
76
|
+
}
|
|
77
|
+
return (type.flags & (ts.TypeFlags.Intersection | ts.TypeFlags.NonPrimitive | ts.TypeFlags.Object)) !== 0 ? TYPES.Object : null;
|
|
78
|
+
}
|
|
79
|
+
const arrays = (bindings, nodes) => {
|
|
80
|
+
let result = new Set();
|
|
81
|
+
if (nodes.length === 0) {
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
let files = new Set(references.exported(bindings.checker, bindings.program, PACKAGE_NAME, REACTIVE_ARRAY).map(declaration => declaration.path));
|
|
85
|
+
if (files.size === 0) {
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
let cache = cachesOf(bindings.program).arrays, types = bindings.checker.getTypeAtLocation(nodes);
|
|
89
|
+
for (let i = 0, n = nodes.length; i < n; i++) {
|
|
90
|
+
let type = types[i];
|
|
91
|
+
if (!type) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
let branded = cache.get(type.id);
|
|
95
|
+
if (branded === undefined) {
|
|
96
|
+
let property = bindings.checker.getPropertyOfType(bindings.checker.getNonNullableType(type) ?? type, ARRAY_BRAND);
|
|
97
|
+
branded = property?.declarations?.some(declaration => files.has(declaration.path)) === true;
|
|
98
|
+
cache.set(type.id, branded);
|
|
99
|
+
}
|
|
100
|
+
if (branded) {
|
|
101
|
+
result.add(nodes[i]);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
33
105
|
};
|
|
34
|
-
const
|
|
35
|
-
let
|
|
36
|
-
if (!
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
let
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
bindings.paths.set(symbol, paths);
|
|
43
|
-
}
|
|
44
|
-
bindings.names.add(identifier.text);
|
|
45
|
-
paths.add(path);
|
|
46
|
-
};
|
|
47
|
-
const isArray = (bindings, expression) => {
|
|
48
|
-
if (ts.isIdentifier(expression)) {
|
|
49
|
-
return resolve(bindings, expression)?.type === TYPES.Array;
|
|
106
|
+
const classify = (bindings, call) => {
|
|
107
|
+
let arg = call.arguments[0];
|
|
108
|
+
if (!arg || call.arguments.length !== 1 || ts.isSpreadElement(arg)) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
let value = unwrap(arg);
|
|
112
|
+
if (ts.isArrowFunction(value) || ts.isFunctionExpression(value)) {
|
|
113
|
+
return TYPES.Computed;
|
|
50
114
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return false;
|
|
115
|
+
if (ts.isArrayLiteralExpression(value)) {
|
|
116
|
+
return TYPES.Array;
|
|
54
117
|
}
|
|
55
|
-
|
|
56
|
-
|
|
118
|
+
if (ts.isObjectLiteralExpression(value)) {
|
|
119
|
+
return TYPES.Object;
|
|
120
|
+
}
|
|
121
|
+
let type = bindings.checker.getTypeAtLocation(arg);
|
|
122
|
+
return type ? kindOf(bindings.checker, type) : null;
|
|
123
|
+
};
|
|
124
|
+
const create = (checker, program, sourceFile) => ({
|
|
125
|
+
checker,
|
|
126
|
+
entries: new Set(references.exported(checker, program, PACKAGE_NAME, ENTRYPOINT).map(references.key)),
|
|
127
|
+
origins: references.origins(checker, program, sourceFile),
|
|
128
|
+
program,
|
|
129
|
+
sourceFile
|
|
130
|
+
});
|
|
131
|
+
const denotes = (bindings, expression) => {
|
|
132
|
+
return references.denotes(bindings.checker, bindings.program, expression, bindings.entries);
|
|
133
|
+
};
|
|
134
|
+
const holds = (bindings, found) => {
|
|
135
|
+
return references.holds(bindings.checker, bindings.program, found, bindings.entries);
|
|
136
|
+
};
|
|
137
|
+
const isReactiveCall = (bindings, node) => {
|
|
138
|
+
return ts.isCallExpression(node) && denotes(bindings, node.expression);
|
|
57
139
|
};
|
|
58
|
-
const
|
|
59
|
-
if (!
|
|
140
|
+
const kind = (bindings, found) => {
|
|
141
|
+
if (!found) {
|
|
60
142
|
return null;
|
|
61
143
|
}
|
|
62
|
-
let
|
|
63
|
-
|
|
144
|
+
let declaration = found.declaration;
|
|
145
|
+
if ((declaration.kind !== ts.SyntaxKind.VariableDeclaration && declaration.kind !== ts.SyntaxKind.ExportAssignment) ||
|
|
146
|
+
declaration.path.endsWith('.d.ts')) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
let cache = cachesOf(bindings.program).kinds, key = references.key(declaration), known = cache.get(key);
|
|
150
|
+
if (known !== undefined) {
|
|
151
|
+
return known;
|
|
152
|
+
}
|
|
153
|
+
cache.set(key, null);
|
|
154
|
+
let node = declaration.resolve(), result = null;
|
|
155
|
+
if (node && ts.isVariableDeclaration(node) && ts.isIdentifier(node.name)) {
|
|
156
|
+
let initializer = node.initializer && unwrap(node.initializer);
|
|
157
|
+
if (initializer && isReactiveCall(bindings, initializer)) {
|
|
158
|
+
result = classify(bindings, initializer);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
let candidates = assignments(bindings, node.getSourceFile()).get(node.name.text) ?? [];
|
|
162
|
+
for (let i = 0, n = candidates.length; i < n && result === null; i++) {
|
|
163
|
+
let { call, target } = candidates[i], assigned = origin(bindings, target);
|
|
164
|
+
if (assigned && references.key(assigned.declaration) === key && isReactiveCall(bindings, call)) {
|
|
165
|
+
result = classify(bindings, call);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
else if (node && ts.isExportAssignment(node)) {
|
|
171
|
+
let expression = unwrap(node.expression);
|
|
172
|
+
if (isReactiveCall(bindings, expression)) {
|
|
173
|
+
result = classify(bindings, expression);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
cache.set(key, result);
|
|
177
|
+
return result;
|
|
178
|
+
};
|
|
179
|
+
const origin = (bindings, node) => {
|
|
180
|
+
return bindings.origins.get(node) ?? references.origin(bindings.checker, bindings.program, node);
|
|
181
|
+
};
|
|
182
|
+
const unwrap = (expression) => {
|
|
183
|
+
return references.unwrap(expression);
|
|
64
184
|
};
|
|
65
|
-
export default { create,
|
|
185
|
+
export default { arrays, classify, create, denotes, holds, isReactiveCall, kind, origin, unwrap };
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
import type { TransformContext } from '@esportsplus/typescript/compiler';
|
|
1
|
+
import type { ImportIntent, ReplacementIntent, TransformContext } from '@esportsplus/typescript/compiler';
|
|
2
2
|
declare const _default: {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
transform: (ctx: TransformContext) => {
|
|
4
|
+
dependencies: string[];
|
|
5
|
+
} | {
|
|
6
|
+
dependencies?: undefined;
|
|
7
|
+
} | {
|
|
8
|
+
imports: ImportIntent[];
|
|
9
|
+
prepend: string[];
|
|
10
|
+
replacements: ReplacementIntent[];
|
|
11
|
+
dependencies: string[];
|
|
12
|
+
};
|
|
5
13
|
};
|
|
6
14
|
export default _default;
|