@rangular-framework/cli 0.0.0-stage → 1.0.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/dist/cli/src/cli.js +1217 -0
- package/dist/compiler/src/catalog.js +198 -0
- package/dist/compiler/src/class/formInit.js +370 -0
- package/dist/compiler/src/class/parseComponent.js +430 -0
- package/dist/compiler/src/class/parseDirective.js +227 -0
- package/dist/compiler/src/class/parseRoutes.js +163 -0
- package/dist/compiler/src/codegen/generate.js +3466 -0
- package/dist/compiler/src/codegen/methods.js +975 -0
- package/dist/compiler/src/codegen/sourceMap.js +77 -0
- package/dist/compiler/src/compile.js +775 -0
- package/dist/compiler/src/diagnostics.js +113 -0
- package/dist/compiler/src/doctor.js +144 -0
- package/dist/compiler/src/fix.js +44 -0
- package/dist/compiler/src/index.js +38 -0
- package/dist/compiler/src/preview/renderPreview.js +7158 -0
- package/dist/compiler/src/preview/styleSnapshot.js +268 -0
- package/dist/compiler/src/propTable.js +107 -0
- package/dist/compiler/src/style/css.js +161 -0
- package/dist/compiler/src/template/ast.js +19 -0
- package/dist/compiler/src/template/evaluate.js +151 -0
- package/dist/compiler/src/template/expr.js +360 -0
- package/dist/compiler/src/template/forms.js +80 -0
- package/dist/compiler/src/template/literal.js +248 -0
- package/dist/compiler/src/template/parser.js +230 -0
- package/dist/compiler/src/template/pipes.js +225 -0
- package/dist/compiler/src/template/printAst.js +38 -0
- package/dist/compiler/src/template/structural.js +51 -0
- package/dist/runtime/src/index.js +1197 -0
- package/dist/ui/src/chart.js +196 -0
- package/dist/ui/src/datetime.js +240 -0
- package/dist/ui/src/icons.js +310 -0
- package/dist/ui/src/mask.js +103 -0
- package/dist/ui/src/palette.js +141 -0
- package/dist/ui/src/tokens.js +173 -0
- package/package.json +28 -4
- package/README.md +0 -3
|
@@ -0,0 +1,975 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.MUTATING_ARRAY_METHODS = void 0;
|
|
37
|
+
exports.setterName = setterName;
|
|
38
|
+
exports.shadowName = shadowName;
|
|
39
|
+
exports.arrayAssignmentFix = arrayAssignmentFix;
|
|
40
|
+
exports.analyzeMutatedFields = analyzeMutatedFields;
|
|
41
|
+
exports.analyzeFieldRereads = analyzeFieldRereads;
|
|
42
|
+
exports.analyzeTimers = analyzeTimers;
|
|
43
|
+
exports.analyzeDeepMutations = analyzeDeepMutations;
|
|
44
|
+
exports.findShadowedFields = findShadowedFields;
|
|
45
|
+
exports.translateMethods = translateMethods;
|
|
46
|
+
const ts = __importStar(require("typescript"));
|
|
47
|
+
const diagnostics_1 = require("../diagnostics");
|
|
48
|
+
function setterName(field) {
|
|
49
|
+
return 'set' + field.charAt(0).toUpperCase() + field.slice(1);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The method-local shadow for a state field that a method assigns and
|
|
53
|
+
* then reads again (react mode). Generated code holds fields in consts
|
|
54
|
+
* plus setters, so a read after `this.x = v` would see the stale render
|
|
55
|
+
* value, where the class reads the value it just wrote. A shadow local
|
|
56
|
+
* restores the class semantics: it starts as the render value, every
|
|
57
|
+
* assignment in the method updates it alongside the setter call, and
|
|
58
|
+
* reads of the field after its first assignment in the method go to it.
|
|
59
|
+
*/
|
|
60
|
+
function shadowName(field) {
|
|
61
|
+
return '__shadow' + field.charAt(0).toUpperCase() + field.slice(1);
|
|
62
|
+
}
|
|
63
|
+
const ASSIGN_OPS = new Set([
|
|
64
|
+
ts.SyntaxKind.EqualsToken,
|
|
65
|
+
ts.SyntaxKind.PlusEqualsToken,
|
|
66
|
+
ts.SyntaxKind.MinusEqualsToken,
|
|
67
|
+
ts.SyntaxKind.AsteriskEqualsToken,
|
|
68
|
+
ts.SyntaxKind.SlashEqualsToken,
|
|
69
|
+
ts.SyntaxKind.PercentEqualsToken,
|
|
70
|
+
]);
|
|
71
|
+
/** Parse a method body block once and hand back its statements. */
|
|
72
|
+
function bodyStatements(method) {
|
|
73
|
+
if (!method.body)
|
|
74
|
+
return undefined;
|
|
75
|
+
const params = method.params.map((p) => p.name).join(', ');
|
|
76
|
+
const sf = ts.createSourceFile('method.ts', `function __m(${params}) ${method.body}`, ts.ScriptTarget.Latest,
|
|
77
|
+
/* setParentNodes */ true);
|
|
78
|
+
let statements;
|
|
79
|
+
const visit = (node) => {
|
|
80
|
+
if (statements)
|
|
81
|
+
return;
|
|
82
|
+
if (ts.isFunctionDeclaration(node) && node.body)
|
|
83
|
+
statements = [...node.body.statements];
|
|
84
|
+
else
|
|
85
|
+
ts.forEachChild(node, visit);
|
|
86
|
+
};
|
|
87
|
+
visit(sf);
|
|
88
|
+
return statements;
|
|
89
|
+
}
|
|
90
|
+
function isThisProperty(node) {
|
|
91
|
+
if (ts.isPropertyAccessExpression(node) && node.expression.kind === ts.SyntaxKind.ThisKeyword) {
|
|
92
|
+
return { name: node.name.text };
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The array methods that mutate their receiver in place. A call of one
|
|
98
|
+
* of these on `this.<field>` changes the field's value, so the field
|
|
99
|
+
* counts as mutated (proof fix): without this, `this.items.push(x)`
|
|
100
|
+
* compiled silently against a plain const and the generated app never
|
|
101
|
+
* re-rendered, while the preview (whose state is a mutable object)
|
|
102
|
+
* did, a direct parity break.
|
|
103
|
+
*/
|
|
104
|
+
exports.MUTATING_ARRAY_METHODS = new Set([
|
|
105
|
+
'push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse', 'fill',
|
|
106
|
+
]);
|
|
107
|
+
/** If `node` is a call `this.<field>.<method>(...)`, name its parts. */
|
|
108
|
+
function thisMethodCall(node) {
|
|
109
|
+
if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression))
|
|
110
|
+
return undefined;
|
|
111
|
+
const self = isThisProperty(node.expression.expression);
|
|
112
|
+
if (!self)
|
|
113
|
+
return undefined;
|
|
114
|
+
return { field: self.name, method: node.expression.name.text };
|
|
115
|
+
}
|
|
116
|
+
/** The fix line that teaches the assignment form for array changes. */
|
|
117
|
+
function arrayAssignmentFix(field) {
|
|
118
|
+
return `Assign a new array instead: this.${field} = [...this.${field}, item] to add, this.${field} = this.${field}.filter(...) to remove, this.${field} = this.${field}.slice(...) to copy a range; push, pop and shift written as plain statements are translated for you`;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* First pass: which fields are assigned anywhere inside any method?
|
|
122
|
+
* Those fields become React state (useState) in codegen.
|
|
123
|
+
*/
|
|
124
|
+
function analyzeMutatedFields(component) {
|
|
125
|
+
const fieldNames = new Set(component.fields.map((f) => f.name));
|
|
126
|
+
const mutated = new Set();
|
|
127
|
+
for (const method of component.methodDetails) {
|
|
128
|
+
const statements = bodyStatements(method);
|
|
129
|
+
if (!statements)
|
|
130
|
+
continue;
|
|
131
|
+
const walk = (node) => {
|
|
132
|
+
if (ts.isBinaryExpression(node) && ASSIGN_OPS.has(node.operatorToken.kind)) {
|
|
133
|
+
const target = isThisProperty(node.left);
|
|
134
|
+
if (target && fieldNames.has(target.name))
|
|
135
|
+
mutated.add(target.name);
|
|
136
|
+
}
|
|
137
|
+
if ((ts.isPostfixUnaryExpression(node) || ts.isPrefixUnaryExpression(node)) &&
|
|
138
|
+
(node.operator === ts.SyntaxKind.PlusPlusToken || node.operator === ts.SyntaxKind.MinusMinusToken)) {
|
|
139
|
+
const target = isThisProperty(node.operand);
|
|
140
|
+
if (target && fieldNames.has(target.name))
|
|
141
|
+
mutated.add(target.name);
|
|
142
|
+
}
|
|
143
|
+
const call = thisMethodCall(node);
|
|
144
|
+
if (call && fieldNames.has(call.field) && exports.MUTATING_ARRAY_METHODS.has(call.method)) {
|
|
145
|
+
mutated.add(call.field);
|
|
146
|
+
}
|
|
147
|
+
ts.forEachChild(node, walk);
|
|
148
|
+
};
|
|
149
|
+
for (const s of statements)
|
|
150
|
+
walk(s);
|
|
151
|
+
}
|
|
152
|
+
return mutated;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The stale-read trap (verification fortification, 2026-11-26): within
|
|
156
|
+
* one method, a field that is assigned, then read, then assigned again
|
|
157
|
+
* is read as the value from before the method ran, because generated
|
|
158
|
+
* state reads see the render snapshot, not the pending assignment.
|
|
159
|
+
* Flagged once per field per method as GEN_FIELD_REREAD. The scan
|
|
160
|
+
* walks statements in order (if branches in sequence) and never into
|
|
161
|
+
* arrow bodies, whose execution is deferred. A call to a method of
|
|
162
|
+
* the same component is expanded one level (runtime prevention
|
|
163
|
+
* round, 2026-11-29, point 9): the helper runs with the caller's
|
|
164
|
+
* render snapshot, so an assign in the caller, a read in the
|
|
165
|
+
* helper and an assign in the helper is the same trap split across
|
|
166
|
+
* the call. Deeper chains and recursion are not followed.
|
|
167
|
+
*/
|
|
168
|
+
function analyzeFieldRereads(component) {
|
|
169
|
+
const fieldNames = new Set(component.fields.map((f) => f.name));
|
|
170
|
+
const diagnostics = [];
|
|
171
|
+
const bodiesByName = new Map();
|
|
172
|
+
for (const m of component.methodDetails) {
|
|
173
|
+
const b = bodyStatements(m);
|
|
174
|
+
if (b)
|
|
175
|
+
bodiesByName.set(m.name, b);
|
|
176
|
+
}
|
|
177
|
+
for (const method of component.methodDetails) {
|
|
178
|
+
const statements = bodyStatements(method);
|
|
179
|
+
if (!statements)
|
|
180
|
+
continue;
|
|
181
|
+
let state = new Map();
|
|
182
|
+
const snapshot = (m) => new Map([...m].map(([k, v]) => [k, { ...v }]));
|
|
183
|
+
const onRead = (name) => {
|
|
184
|
+
const s = state.get(name);
|
|
185
|
+
if (s?.assigned)
|
|
186
|
+
s.readSince = true;
|
|
187
|
+
};
|
|
188
|
+
const onWrite = (name) => {
|
|
189
|
+
const s = state.get(name) ?? { assigned: false, readSince: false, flagged: false };
|
|
190
|
+
if (s.assigned && s.readSince && !s.flagged) {
|
|
191
|
+
s.flagged = true;
|
|
192
|
+
diagnostics.push((0, diagnostics_1.warning)('GEN_FIELD_REREAD', `Method '${method.name}' assigns this.${name}, reads it, then assigns it again; the read sees the value from before the method ran, not the assigned value`, undefined, undefined, `Carry the new value in a local const inside ${method.name} and assign this.${name} once, at the end`));
|
|
193
|
+
}
|
|
194
|
+
s.assigned = true;
|
|
195
|
+
s.readSince = false;
|
|
196
|
+
state.set(name, s);
|
|
197
|
+
};
|
|
198
|
+
const walk = (node, depth = 0) => {
|
|
199
|
+
if (ts.isArrowFunction(node) || ts.isFunctionExpression(node))
|
|
200
|
+
return;
|
|
201
|
+
if (depth === 0 && ts.isCallExpression(node)) {
|
|
202
|
+
// One level of helper expansion: a call to a method of the
|
|
203
|
+
// same component runs that method's statements here, in
|
|
204
|
+
// sequence, with this render's snapshot.
|
|
205
|
+
const callee = isThisProperty(node.expression);
|
|
206
|
+
const body = callee && callee.name !== method.name ? bodiesByName.get(callee.name) : undefined;
|
|
207
|
+
if (callee && body) {
|
|
208
|
+
for (const arg of node.arguments)
|
|
209
|
+
walk(arg, depth);
|
|
210
|
+
for (const s of body)
|
|
211
|
+
walk(s, depth + 1);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (ts.isIfStatement(node)) {
|
|
216
|
+
// Branches are alternatives, not a sequence: walk each from the
|
|
217
|
+
// same entry state, then merge. A field counts as assigned when
|
|
218
|
+
// either branch assigns it, and a stale read counts when either
|
|
219
|
+
// branch leaves one pending, so the if/else toggle shape (one
|
|
220
|
+
// assignment per branch) stays silent while a read between two
|
|
221
|
+
// writes on any single path still flags.
|
|
222
|
+
walk(node.expression, depth);
|
|
223
|
+
const entry = snapshot(state);
|
|
224
|
+
walk(node.thenStatement, depth);
|
|
225
|
+
const afterThen = snapshot(state);
|
|
226
|
+
state = entry;
|
|
227
|
+
if (node.elseStatement)
|
|
228
|
+
walk(node.elseStatement, depth);
|
|
229
|
+
const afterElse = snapshot(state);
|
|
230
|
+
const merged = new Map();
|
|
231
|
+
for (const name of new Set([...afterThen.keys(), ...afterElse.keys()])) {
|
|
232
|
+
const a = afterThen.get(name);
|
|
233
|
+
const b = afterElse.get(name);
|
|
234
|
+
merged.set(name, {
|
|
235
|
+
assigned: (a?.assigned ?? false) || (b?.assigned ?? false),
|
|
236
|
+
readSince: (a?.readSince ?? false) || (b?.readSince ?? false),
|
|
237
|
+
flagged: (a?.flagged ?? false) || (b?.flagged ?? false),
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
state = merged;
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
if (ts.isBinaryExpression(node) && ASSIGN_OPS.has(node.operatorToken.kind)) {
|
|
244
|
+
const target = isThisProperty(node.left);
|
|
245
|
+
if (target && fieldNames.has(target.name)) {
|
|
246
|
+
walk(node.right, depth);
|
|
247
|
+
onWrite(target.name);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if ((ts.isPostfixUnaryExpression(node) || ts.isPrefixUnaryExpression(node)) &&
|
|
252
|
+
(node.operator === ts.SyntaxKind.PlusPlusToken || node.operator === ts.SyntaxKind.MinusMinusToken)) {
|
|
253
|
+
const target = isThisProperty(node.operand);
|
|
254
|
+
if (target && fieldNames.has(target.name)) {
|
|
255
|
+
onRead(target.name);
|
|
256
|
+
onWrite(target.name);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
const prop = isThisProperty(node);
|
|
261
|
+
if (prop && fieldNames.has(prop.name))
|
|
262
|
+
onRead(prop.name);
|
|
263
|
+
ts.forEachChild(node, (child) => walk(child, depth));
|
|
264
|
+
};
|
|
265
|
+
for (const s of statements)
|
|
266
|
+
walk(s);
|
|
267
|
+
}
|
|
268
|
+
return diagnostics;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* The stale timer trap (runtime prevention round, 2026-11-29,
|
|
272
|
+
* point 1 of the audit): a setInterval or setTimeout created in
|
|
273
|
+
* ngOnInit or the constructor runs its callback with the field
|
|
274
|
+
* values of the first render, because those bodies run exactly
|
|
275
|
+
* once and a component's methods are recreated per render, so the
|
|
276
|
+
* timer keeps the first copy. A callback that reads a component
|
|
277
|
+
* field therefore reads the first value forever: an interval
|
|
278
|
+
* counter freezes at its first tick. A write that reads no field
|
|
279
|
+
* is safe (the setter is stable and the value is the same from
|
|
280
|
+
* any render), and a timer created in an event handler runs with
|
|
281
|
+
* that event's own render, so neither is flagged. This analysis
|
|
282
|
+
* runs for components only (react mode), where fields are render
|
|
283
|
+
* state. A service's fields are plain instance fields, so its
|
|
284
|
+
* timers read live values and stay silent: the alarm heartbeat
|
|
285
|
+
* is the safe pattern this check points to. Flagged once per
|
|
286
|
+
* timer call as GEN_TIMER_STALE. A callback that calls a
|
|
287
|
+
* component method counts when that method reads a field itself
|
|
288
|
+
* (one level: setInterval(() => this.tick()) is the alarm shape,
|
|
289
|
+
* safe in a service and stale in a component).
|
|
290
|
+
*/
|
|
291
|
+
function analyzeTimers(component) {
|
|
292
|
+
const fieldNames = new Set(component.fields.map((f) => f.name));
|
|
293
|
+
const diagnostics = [];
|
|
294
|
+
// A field READ is what goes stale: the value comes from the
|
|
295
|
+
// creating render's snapshot. A write whose value reads no field
|
|
296
|
+
// (this.dipped = false) calls the same stable setter with the
|
|
297
|
+
// same value from any render, so it is safe and stays silent.
|
|
298
|
+
// readsField returns the first field read in a statement list;
|
|
299
|
+
// assignment targets are writes, not reads, and ++/-- reads.
|
|
300
|
+
const readsField = (statements, intoArrows) => {
|
|
301
|
+
if (!statements)
|
|
302
|
+
return undefined;
|
|
303
|
+
let found;
|
|
304
|
+
const walk = (node) => {
|
|
305
|
+
if (found)
|
|
306
|
+
return;
|
|
307
|
+
if (!intoArrows && (ts.isArrowFunction(node) || ts.isFunctionExpression(node)))
|
|
308
|
+
return;
|
|
309
|
+
if (ts.isBinaryExpression(node) && ASSIGN_OPS.has(node.operatorToken.kind)) {
|
|
310
|
+
walk(node.right);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if ((ts.isPostfixUnaryExpression(node) || ts.isPrefixUnaryExpression(node)) &&
|
|
314
|
+
(node.operator === ts.SyntaxKind.PlusPlusToken || node.operator === ts.SyntaxKind.MinusMinusToken)) {
|
|
315
|
+
const target = isThisProperty(node.operand);
|
|
316
|
+
if (target && fieldNames.has(target.name)) {
|
|
317
|
+
found = target.name;
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
const prop = isThisProperty(node);
|
|
322
|
+
if (prop && fieldNames.has(prop.name)) {
|
|
323
|
+
found = prop.name;
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
ts.forEachChild(node, walk);
|
|
327
|
+
};
|
|
328
|
+
for (const s of statements)
|
|
329
|
+
walk(s);
|
|
330
|
+
return found;
|
|
331
|
+
};
|
|
332
|
+
// First pass: which methods read a field in their own statement
|
|
333
|
+
// flow (arrow bodies excluded, the analyzeFieldRereads
|
|
334
|
+
// discipline: deferred code is not the method's flow).
|
|
335
|
+
const methodField = new Map();
|
|
336
|
+
for (const method of component.methodDetails) {
|
|
337
|
+
const f = readsField(bodyStatements(method), false);
|
|
338
|
+
if (f)
|
|
339
|
+
methodField.set(method.name, f);
|
|
340
|
+
}
|
|
341
|
+
// Inside a timer callback, deferred code still closes over the
|
|
342
|
+
// creating render, so this scan descends into nested arrows.
|
|
343
|
+
const callbackHit = (root) => {
|
|
344
|
+
let hit;
|
|
345
|
+
const walk = (node) => {
|
|
346
|
+
if (hit)
|
|
347
|
+
return;
|
|
348
|
+
if (ts.isCallExpression(node)) {
|
|
349
|
+
const callee = isThisProperty(node.expression);
|
|
350
|
+
if (callee && methodField.has(callee.name)) {
|
|
351
|
+
hit = { field: methodField.get(callee.name), via: callee.name };
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
ts.forEachChild(node, walk);
|
|
356
|
+
};
|
|
357
|
+
walk(root);
|
|
358
|
+
if (hit)
|
|
359
|
+
return hit;
|
|
360
|
+
const direct = readsField([root], true);
|
|
361
|
+
return direct ? { field: direct } : undefined;
|
|
362
|
+
};
|
|
363
|
+
// Scope: timers created in ngOnInit or the constructor, the two
|
|
364
|
+
// bodies that run exactly once, at the first render. A timer
|
|
365
|
+
// created in an event handler runs with that event's render,
|
|
366
|
+
// which is the delayed-completion idiom (a refresh landing, a
|
|
367
|
+
// press dip resetting); its window is short and its values are
|
|
368
|
+
// the event's own, so v1 stays silent there.
|
|
369
|
+
for (const method of component.methodDetails) {
|
|
370
|
+
if (method.name !== 'ngOnInit' && method.name !== 'constructor')
|
|
371
|
+
continue;
|
|
372
|
+
const statements = bodyStatements(method);
|
|
373
|
+
if (!statements)
|
|
374
|
+
continue;
|
|
375
|
+
const walk = (node) => {
|
|
376
|
+
if (ts.isArrowFunction(node) || ts.isFunctionExpression(node))
|
|
377
|
+
return;
|
|
378
|
+
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) &&
|
|
379
|
+
(node.expression.text === 'setInterval' || node.expression.text === 'setTimeout')) {
|
|
380
|
+
const kind = node.expression.text;
|
|
381
|
+
const cb = node.arguments[0];
|
|
382
|
+
let hit;
|
|
383
|
+
if (cb && (ts.isArrowFunction(cb) || ts.isFunctionExpression(cb))) {
|
|
384
|
+
hit = callbackHit(cb.body);
|
|
385
|
+
}
|
|
386
|
+
else if (cb) {
|
|
387
|
+
const ref = isThisProperty(cb);
|
|
388
|
+
if (ref && methodField.has(ref.name))
|
|
389
|
+
hit = { field: methodField.get(ref.name), via: ref.name };
|
|
390
|
+
}
|
|
391
|
+
if (hit) {
|
|
392
|
+
diagnostics.push((0, diagnostics_1.warning)('GEN_TIMER_STALE', `${kind} created in ${method.name} runs once, at the first render, and keeps that render's field values; its callback ${hit.via ? `calls this.${hit.via}(), which reads this.${hit.field}` : `reads this.${hit.field}`}, so it reads the first value forever and the display freezes or acts on stale state`, undefined, undefined, `Move the timer and the value it changes into a service, where fields are live instance fields (the alarm heartbeat pattern), and let the component read the service`));
|
|
393
|
+
}
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
ts.forEachChild(node, walk);
|
|
397
|
+
};
|
|
398
|
+
for (const s of statements)
|
|
399
|
+
walk(s);
|
|
400
|
+
}
|
|
401
|
+
return diagnostics;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* The deep mutation trap (runtime prevention round, 2026-11-29,
|
|
405
|
+
* point 2 of the audit): an assignment into a field's insides,
|
|
406
|
+
* this.items[0] = 'z' or this.cfg.loud = true, compiles clean and
|
|
407
|
+
* generates a raw mutation with no setter call, so the data
|
|
408
|
+
* changes and the view never re-renders. push and pop are
|
|
409
|
+
* rewritten through the setter and splice and sort are named by
|
|
410
|
+
* METHOD_UNSUPPORTED; this is the remaining silent shape. Flagged
|
|
411
|
+
* once per field per method as GEN_DEEP_MUTATION. Components only
|
|
412
|
+
* (react mode): a service's fields are plain instance fields and
|
|
413
|
+
* its subscribers re-render on the version bump of any assignment
|
|
414
|
+
* the service makes to the field itself; the trap is the component
|
|
415
|
+
* state slot, where only the setter call re-renders.
|
|
416
|
+
*/
|
|
417
|
+
function analyzeDeepMutations(component) {
|
|
418
|
+
const fieldNames = new Set(component.fields.map((f) => f.name));
|
|
419
|
+
const diagnostics = [];
|
|
420
|
+
for (const method of component.methodDetails) {
|
|
421
|
+
const statements = bodyStatements(method);
|
|
422
|
+
if (!statements)
|
|
423
|
+
continue;
|
|
424
|
+
const flagged = new Set();
|
|
425
|
+
const rootField = (node) => {
|
|
426
|
+
let cur = node;
|
|
427
|
+
while (ts.isPropertyAccessExpression(cur) || ts.isElementAccessExpression(cur)) {
|
|
428
|
+
if (ts.isPropertyAccessExpression(cur) && cur.expression.kind === ts.SyntaxKind.ThisKeyword) {
|
|
429
|
+
return fieldNames.has(cur.name.text) ? cur.name.text : undefined;
|
|
430
|
+
}
|
|
431
|
+
cur = cur.expression;
|
|
432
|
+
}
|
|
433
|
+
return undefined;
|
|
434
|
+
};
|
|
435
|
+
const walk = (node) => {
|
|
436
|
+
if (ts.isBinaryExpression(node) && ASSIGN_OPS.has(node.operatorToken.kind)) {
|
|
437
|
+
const direct = isThisProperty(node.left);
|
|
438
|
+
if (!direct) {
|
|
439
|
+
const root = rootField(node.left);
|
|
440
|
+
if (root && !flagged.has(root)) {
|
|
441
|
+
flagged.add(root);
|
|
442
|
+
diagnostics.push((0, diagnostics_1.warning)('GEN_DEEP_MUTATION', `Method '${method.name}' assigns into this.${root} (an index or a property of it); the generated code mutates the value in place with no setter call, so the view never re-renders`, undefined, undefined, `Build a new value and assign the field itself: this.${root} = [...this.${root}] for an array copy with the change applied, or this.${root} = { ...this.${root} } for an object`));
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if ((ts.isPostfixUnaryExpression(node) || ts.isPrefixUnaryExpression(node)) &&
|
|
447
|
+
(node.operator === ts.SyntaxKind.PlusPlusToken || node.operator === ts.SyntaxKind.MinusMinusToken)) {
|
|
448
|
+
const direct = isThisProperty(node.operand);
|
|
449
|
+
if (!direct) {
|
|
450
|
+
const root = rootField(node.operand);
|
|
451
|
+
if (root && !flagged.has(root)) {
|
|
452
|
+
flagged.add(root);
|
|
453
|
+
diagnostics.push((0, diagnostics_1.warning)('GEN_DEEP_MUTATION', `Method '${method.name}' increments or decrements inside this.${root}; the generated code mutates the value in place with no setter call, so the view never re-renders`, undefined, undefined, `Build a new value and assign the field itself: this.${root} = [...this.${root}] for an array copy with the change applied, or this.${root} = { ...this.${root} } for an object`));
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
ts.forEachChild(node, walk);
|
|
458
|
+
};
|
|
459
|
+
for (const s of statements)
|
|
460
|
+
walk(s);
|
|
461
|
+
}
|
|
462
|
+
return diagnostics;
|
|
463
|
+
}
|
|
464
|
+
class Translator {
|
|
465
|
+
ctx;
|
|
466
|
+
constructor(ctx) {
|
|
467
|
+
this.ctx = ctx;
|
|
468
|
+
}
|
|
469
|
+
fail(what, fix) {
|
|
470
|
+
this.ctx.unsupported = what;
|
|
471
|
+
if (fix !== undefined)
|
|
472
|
+
this.ctx.unsupportedFix = fix;
|
|
473
|
+
return 'undefined';
|
|
474
|
+
}
|
|
475
|
+
/** Translate an expression node to code, or mark the statement unsupported. */
|
|
476
|
+
expr(node) {
|
|
477
|
+
const c = this.ctx;
|
|
478
|
+
if (ts.isIdentifier(node))
|
|
479
|
+
return node.text;
|
|
480
|
+
if (node.kind === ts.SyntaxKind.TrueKeyword)
|
|
481
|
+
return 'true';
|
|
482
|
+
if (node.kind === ts.SyntaxKind.FalseKeyword)
|
|
483
|
+
return 'false';
|
|
484
|
+
if (node.kind === ts.SyntaxKind.NullKeyword)
|
|
485
|
+
return 'null';
|
|
486
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
487
|
+
return node.getText();
|
|
488
|
+
if (ts.isNumericLiteral(node))
|
|
489
|
+
return node.text;
|
|
490
|
+
if (ts.isRegularExpressionLiteral(node))
|
|
491
|
+
return node.getText();
|
|
492
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
493
|
+
const self = isThisProperty(node);
|
|
494
|
+
if (self)
|
|
495
|
+
return this.thisMember(self.name);
|
|
496
|
+
return `${this.expr(node.expression)}.${node.name.text}`;
|
|
497
|
+
}
|
|
498
|
+
if (ts.isElementAccessExpression(node)) {
|
|
499
|
+
return `${this.expr(node.expression)}[${this.expr(node.argumentExpression)}]`;
|
|
500
|
+
}
|
|
501
|
+
if (ts.isCallExpression(node)) {
|
|
502
|
+
// An array mutation method on a field, reached in expression
|
|
503
|
+
// position (its result is used, or it sits inside a larger
|
|
504
|
+
// expression): generated code holds fields in consts and state
|
|
505
|
+
// setters, so there is no faithful translation; refuse it and
|
|
506
|
+
// teach the assignment form (proof fix). Statement position is
|
|
507
|
+
// handled separately in statement(), where push, pop and shift
|
|
508
|
+
// do translate. State, service and pipe modes keep the native
|
|
509
|
+
// call: their fields really are mutable properties.
|
|
510
|
+
if (c.mode === 'react') {
|
|
511
|
+
const call = thisMethodCall(node);
|
|
512
|
+
if (call && c.fieldNames.has(call.field) && exports.MUTATING_ARRAY_METHODS.has(call.method)) {
|
|
513
|
+
return this.fail(`array mutation method '${call.method}' on field 'this.${call.field}' used as a value`, arrayAssignmentFix(call.field));
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
// @Output: this.saved.emit(x) -> onSaved?.(x) / props.onSaved?.(x).
|
|
517
|
+
if (ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === 'emit') {
|
|
518
|
+
const self = isThisProperty(node.expression.expression);
|
|
519
|
+
const callback = self ? c.outputCallbacks?.get(self.name) : undefined;
|
|
520
|
+
if (self && callback) {
|
|
521
|
+
const args = node.arguments.map((a) => this.expr(a)).join(', ');
|
|
522
|
+
return `${callback}?.(${args})`;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
const callee = this.expr(node.expression);
|
|
526
|
+
const args = node.arguments.map((a) => this.expr(a)).join(', ');
|
|
527
|
+
return `${callee}(${args})`;
|
|
528
|
+
}
|
|
529
|
+
if (ts.isBinaryExpression(node)) {
|
|
530
|
+
// An assignment buried inside an expression (e.g. an expression-bodied
|
|
531
|
+
// arrow `data => this.field = data`) cannot become a React setter call;
|
|
532
|
+
// refuse it so the statement degrades to METHOD_UNSUPPORTED instead of
|
|
533
|
+
// emitting `field = data` against a const. State/service modes can
|
|
534
|
+
// express it faithfully (state.field = data / this.field = data), and
|
|
535
|
+
// the generic path below already produces exactly that.
|
|
536
|
+
if (c.mode === 'react' && ASSIGN_OPS.has(node.operatorToken.kind) && isThisProperty(node.left)) {
|
|
537
|
+
return this.fail('assignment to a field inside an expression, use a block body with assignment statements');
|
|
538
|
+
}
|
|
539
|
+
return `${this.expr(node.left)} ${node.operatorToken.getText()} ${this.expr(node.right)}`;
|
|
540
|
+
}
|
|
541
|
+
if (ts.isPrefixUnaryExpression(node)) {
|
|
542
|
+
if (node.operator === ts.SyntaxKind.PlusPlusToken || node.operator === ts.SyntaxKind.MinusMinusToken) {
|
|
543
|
+
const target = isThisProperty(node.operand);
|
|
544
|
+
if (target) {
|
|
545
|
+
const opText = node.operator === ts.SyntaxKind.PlusPlusToken ? '++' : '--';
|
|
546
|
+
if (c.mode === 'state')
|
|
547
|
+
return `${opText}state.${target.name}`;
|
|
548
|
+
if (c.mode === 'service' || c.mode === 'pipe')
|
|
549
|
+
return `${opText}this.${target.name}`;
|
|
550
|
+
return this.fail(`'${opText}this.${target.name}' inside an expression`);
|
|
551
|
+
}
|
|
552
|
+
return `${node.operator === ts.SyntaxKind.PlusPlusToken ? '++' : '--'}${this.expr(node.operand)}`;
|
|
553
|
+
}
|
|
554
|
+
const op = node.operator === ts.SyntaxKind.ExclamationToken ? '!'
|
|
555
|
+
: node.operator === ts.SyntaxKind.MinusToken ? '-'
|
|
556
|
+
: node.operator === ts.SyntaxKind.PlusToken ? '+'
|
|
557
|
+
: node.operator === ts.SyntaxKind.TildeToken ? '~' : undefined;
|
|
558
|
+
if (op === undefined)
|
|
559
|
+
return this.fail(`unary operator ${ts.SyntaxKind[node.operator]}`);
|
|
560
|
+
return `${op}${this.expr(node.operand)}`;
|
|
561
|
+
}
|
|
562
|
+
if (ts.isPostfixUnaryExpression(node)) {
|
|
563
|
+
const target = isThisProperty(node.operand);
|
|
564
|
+
const opText = node.operator === ts.SyntaxKind.PlusPlusToken ? '++' : '--';
|
|
565
|
+
if (target) {
|
|
566
|
+
if (c.mode === 'state')
|
|
567
|
+
return `state.${target.name}${opText}`;
|
|
568
|
+
if (c.mode === 'service' || c.mode === 'pipe')
|
|
569
|
+
return `this.${target.name}${opText}`;
|
|
570
|
+
return this.fail(`'this.${target.name}${opText}' inside an expression`);
|
|
571
|
+
}
|
|
572
|
+
return `${this.expr(node.operand)}${opText}`;
|
|
573
|
+
}
|
|
574
|
+
if (ts.isConditionalExpression(node)) {
|
|
575
|
+
return `(${this.expr(node.condition)} ? ${this.expr(node.whenTrue)} : ${this.expr(node.whenFalse)})`;
|
|
576
|
+
}
|
|
577
|
+
if (ts.isParenthesizedExpression(node))
|
|
578
|
+
return `(${this.expr(node.expression)})`;
|
|
579
|
+
if (ts.isTemplateExpression(node)) {
|
|
580
|
+
let out = '`' + node.head.text;
|
|
581
|
+
for (const span of node.templateSpans) {
|
|
582
|
+
out += '${' + this.expr(span.expression) + '}' + span.literal.text;
|
|
583
|
+
}
|
|
584
|
+
return out + '`';
|
|
585
|
+
}
|
|
586
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
587
|
+
return `[${node.elements.map((e) => this.expr(e)).join(', ')}]`;
|
|
588
|
+
}
|
|
589
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
590
|
+
const parts = [];
|
|
591
|
+
for (const prop of node.properties) {
|
|
592
|
+
if (ts.isPropertyAssignment(prop))
|
|
593
|
+
parts.push(`${prop.name.getText()}: ${this.expr(prop.initializer)}`);
|
|
594
|
+
else if (ts.isShorthandPropertyAssignment(prop))
|
|
595
|
+
parts.push(prop.name.text);
|
|
596
|
+
else if (ts.isSpreadAssignment(prop))
|
|
597
|
+
parts.push(`...${this.expr(prop.expression)}`);
|
|
598
|
+
else
|
|
599
|
+
return this.fail('object literal method/getter');
|
|
600
|
+
}
|
|
601
|
+
return `{ ${parts.join(', ')} }`;
|
|
602
|
+
}
|
|
603
|
+
if (ts.isSpreadElement(node))
|
|
604
|
+
return `...${this.expr(node.expression)}`;
|
|
605
|
+
if (ts.isAsExpression(node) || ts.isTypeAssertionExpression(node))
|
|
606
|
+
return this.expr(node.expression);
|
|
607
|
+
if (ts.isNonNullExpression(node))
|
|
608
|
+
return this.expr(node.expression);
|
|
609
|
+
if (ts.isArrowFunction(node)) {
|
|
610
|
+
// Preview (state mode) bodies run as plain JavaScript in the page, so
|
|
611
|
+
// type annotations must be stripped there; generated .tsx keeps them.
|
|
612
|
+
const params = node.parameters
|
|
613
|
+
.map((p) => (c.mode === 'state' ? p.name.getText() : p.getText()))
|
|
614
|
+
.join(', ');
|
|
615
|
+
if (ts.isBlock(node.body)) {
|
|
616
|
+
const saved = c.unsupported;
|
|
617
|
+
const lines = this.statements([...node.body.statements], ' ');
|
|
618
|
+
if (c.unsupported)
|
|
619
|
+
return this.fail(c.unsupported);
|
|
620
|
+
void saved;
|
|
621
|
+
return `(${params}) => {\n${lines.join('\n')}\n}`;
|
|
622
|
+
}
|
|
623
|
+
return `(${params}) => ${this.expr(node.body)}`;
|
|
624
|
+
}
|
|
625
|
+
if (ts.isNewExpression(node)) {
|
|
626
|
+
// Forms (completion pass): rows are born in methods, an add-row
|
|
627
|
+
// handler is `this.members.push(new FormControl(''))`. The four
|
|
628
|
+
// forms classes translate verbatim: generated code imports the
|
|
629
|
+
// runtime classes, and the preview page binds the same names to
|
|
630
|
+
// its mirror factories.
|
|
631
|
+
const name = ts.isIdentifier(node.expression) ? node.expression.text : undefined;
|
|
632
|
+
if (name === 'FormControl' || name === 'FormGroup' || name === 'FormArray' || name === 'FormBuilder') {
|
|
633
|
+
const args = (node.arguments ?? []).map((a) => this.expr(a)).join(', ');
|
|
634
|
+
return `new ${name}(${args})`;
|
|
635
|
+
}
|
|
636
|
+
return this.fail(`new ${name ?? '...'}(...) in a method`);
|
|
637
|
+
}
|
|
638
|
+
return this.fail(ts.SyntaxKind[node.kind]);
|
|
639
|
+
}
|
|
640
|
+
thisMember(name) {
|
|
641
|
+
const c = this.ctx;
|
|
642
|
+
const callback = c.outputCallbacks?.get(name);
|
|
643
|
+
if (callback)
|
|
644
|
+
return callback;
|
|
645
|
+
if (c.mode === 'service' || c.mode === 'pipe')
|
|
646
|
+
return `this.${name}`;
|
|
647
|
+
if (c.methodNames.has(name))
|
|
648
|
+
return c.mode === 'state' ? `methods.${name}` : name;
|
|
649
|
+
// Stage 8: a reactive form instance is ref-held in generated code.
|
|
650
|
+
if (c.mode === 'react' && c.formFields?.has(name))
|
|
651
|
+
return `${name}Ref.current`;
|
|
652
|
+
// A field this method assigned earlier reads from its shadow local,
|
|
653
|
+
// which holds the value the class would see (see shadowName).
|
|
654
|
+
if (c.mode === 'react' && c.shadowFields?.has(name))
|
|
655
|
+
return c.shadowFields.get(name);
|
|
656
|
+
return c.mode === 'state' ? `state.${name}` : name;
|
|
657
|
+
}
|
|
658
|
+
/** Translate a list of statements at the given indent (no leading spaces beyond it). */
|
|
659
|
+
statements(list, indent) {
|
|
660
|
+
const lines = [];
|
|
661
|
+
for (const stmt of list)
|
|
662
|
+
lines.push(...this.statement(stmt, indent));
|
|
663
|
+
return lines;
|
|
664
|
+
}
|
|
665
|
+
unsupportedLine(indent, what, fix) {
|
|
666
|
+
this.ctx.diagnostics.push((0, diagnostics_1.warning)('METHOD_UNSUPPORTED', `Method '${this.ctx.methodName}': ${what} is not in the Stage 2 translation subset, left as a TODO`, undefined, undefined, fix ?? this.ctx.unsupportedFix
|
|
667
|
+
?? 'Rewrite with the supported subset: this.field assignments (+=, ++, =), this.method() calls, if/else, return, local const/let, or move the unsupported logic out of the method'));
|
|
668
|
+
return [`${indent}/* TODO(Rangular): unsupported in method '${this.ctx.methodName}': ${what} */`];
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* A statement that is exactly an array mutation call on a field,
|
|
672
|
+
* in react mode (proof fix). push becomes a concat assignment
|
|
673
|
+
* through the state setter; pop and shift with no arguments (their
|
|
674
|
+
* return values unused by construction in statement position)
|
|
675
|
+
* become slice assignments. The remaining mutating methods
|
|
676
|
+
* (unshift, splice, sort, reverse, fill) have no honest one-line
|
|
677
|
+
* setter form, so they degrade to METHOD_UNSUPPORTED with the
|
|
678
|
+
* assignment form as the fix. Returns undefined when the statement
|
|
679
|
+
* is not such a call, or the mode keeps native mutation (state
|
|
680
|
+
* mode mutates the preview state object; services and pipes are
|
|
681
|
+
* real classes).
|
|
682
|
+
*/
|
|
683
|
+
arrayMutationStatement(e, indent) {
|
|
684
|
+
const c = this.ctx;
|
|
685
|
+
if (c.mode !== 'react' || !ts.isCallExpression(e))
|
|
686
|
+
return undefined;
|
|
687
|
+
const call = thisMethodCall(e);
|
|
688
|
+
if (!call || !c.fieldNames.has(call.field) || !exports.MUTATING_ARRAY_METHODS.has(call.method)) {
|
|
689
|
+
return undefined;
|
|
690
|
+
}
|
|
691
|
+
const { field, method } = call;
|
|
692
|
+
if (!c.stateFields.has(field)) {
|
|
693
|
+
return this.unsupportedLine(indent, `array mutation method '${method}' on field '${field}', which was not hoisted to React state`, arrayAssignmentFix(field));
|
|
694
|
+
}
|
|
695
|
+
const set = setterName(field);
|
|
696
|
+
const shadow = c.shadowFields?.get(field);
|
|
697
|
+
const receiver = shadow ?? field;
|
|
698
|
+
if (method === 'push') {
|
|
699
|
+
const args = e.arguments.map((a) => this.expr(a)).join(', ');
|
|
700
|
+
if (c.unsupported)
|
|
701
|
+
return this.unsupportedLine(indent, c.unsupported);
|
|
702
|
+
if (shadow)
|
|
703
|
+
return [`${indent}${shadow} = ${receiver}.concat(${args});`, `${indent}${set}(${shadow});`];
|
|
704
|
+
return [`${indent}${set}(${field}.concat(${args}));`];
|
|
705
|
+
}
|
|
706
|
+
if ((method === 'pop' || method === 'shift') && e.arguments.length === 0) {
|
|
707
|
+
const sliced = method === 'pop' ? `${receiver}.slice(0, -1)` : `${receiver}.slice(1)`;
|
|
708
|
+
if (shadow)
|
|
709
|
+
return [`${indent}${shadow} = ${sliced};`, `${indent}${set}(${shadow});`];
|
|
710
|
+
return [`${indent}${set}(${sliced});`];
|
|
711
|
+
}
|
|
712
|
+
return this.unsupportedLine(indent, `array mutation method '${method}' on field 'this.${field}' as a statement`, arrayAssignmentFix(field));
|
|
713
|
+
}
|
|
714
|
+
statement(node, indent) {
|
|
715
|
+
const c = this.ctx;
|
|
716
|
+
c.unsupported = undefined;
|
|
717
|
+
c.unsupportedFix = undefined;
|
|
718
|
+
if (ts.isBlock(node))
|
|
719
|
+
return this.statements([...node.statements], indent);
|
|
720
|
+
if (ts.isExpressionStatement(node)) {
|
|
721
|
+
const e = node.expression;
|
|
722
|
+
// Assignment / update directly on this.field.
|
|
723
|
+
if (ts.isBinaryExpression(e) && ASSIGN_OPS.has(e.operatorToken.kind)) {
|
|
724
|
+
const target = isThisProperty(e.left);
|
|
725
|
+
if (target) {
|
|
726
|
+
if (!c.fieldNames.has(target.name)) {
|
|
727
|
+
return this.unsupportedLine(indent, `assignment to undeclared field 'this.${target.name}'`);
|
|
728
|
+
}
|
|
729
|
+
if (c.mode === 'react' && !c.stateFields.has(target.name)) {
|
|
730
|
+
return this.unsupportedLine(indent, `assignment to field '${target.name}', which was not hoisted to React state`);
|
|
731
|
+
}
|
|
732
|
+
const rhs = this.expr(e.right);
|
|
733
|
+
if (c.unsupported)
|
|
734
|
+
return this.unsupportedLine(indent, c.unsupported);
|
|
735
|
+
const op = e.operatorToken.getText();
|
|
736
|
+
if (c.mode === 'state')
|
|
737
|
+
return [`${indent}state.${target.name} ${op} ${rhs};`];
|
|
738
|
+
if (c.mode === 'service') {
|
|
739
|
+
return [
|
|
740
|
+
`${indent}this.${target.name} ${op} ${rhs};`,
|
|
741
|
+
`${indent}this.__notify();`,
|
|
742
|
+
];
|
|
743
|
+
}
|
|
744
|
+
if (c.mode === 'pipe') {
|
|
745
|
+
return [`${indent}this.${target.name} ${op} ${rhs};`];
|
|
746
|
+
}
|
|
747
|
+
const set = setterName(target.name);
|
|
748
|
+
const shadow = c.shadowFields?.get(target.name);
|
|
749
|
+
if (shadow) {
|
|
750
|
+
const value = op === '=' ? rhs : `${shadow} ${op.slice(0, -1)} ${rhs}`;
|
|
751
|
+
return [`${indent}${shadow} = ${value};`, `${indent}${set}(${shadow});`];
|
|
752
|
+
}
|
|
753
|
+
if (op === '=')
|
|
754
|
+
return [`${indent}${set}(${rhs});`];
|
|
755
|
+
const binOp = op.slice(0, -1);
|
|
756
|
+
return [`${indent}${set}(${target.name} ${binOp} ${rhs});`];
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
if ((ts.isPostfixUnaryExpression(e) || ts.isPrefixUnaryExpression(e)) &&
|
|
760
|
+
(e.operator === ts.SyntaxKind.PlusPlusToken || e.operator === ts.SyntaxKind.MinusMinusToken)) {
|
|
761
|
+
const target = isThisProperty(e.operand);
|
|
762
|
+
if (target) {
|
|
763
|
+
if (!c.fieldNames.has(target.name)) {
|
|
764
|
+
return this.unsupportedLine(indent, `update of undeclared field 'this.${target.name}'`);
|
|
765
|
+
}
|
|
766
|
+
if (c.mode === 'react' && !c.stateFields.has(target.name)) {
|
|
767
|
+
return this.unsupportedLine(indent, `update of field '${target.name}', which was not hoisted to React state`);
|
|
768
|
+
}
|
|
769
|
+
const delta = e.operator === ts.SyntaxKind.PlusPlusToken ? '+ 1' : '- 1';
|
|
770
|
+
if (c.mode === 'state') {
|
|
771
|
+
return [ts.isPostfixUnaryExpression(e)
|
|
772
|
+
? `${indent}state.${target.name}${delta.startsWith('+') ? '++' : '--'};`
|
|
773
|
+
: `${indent}${delta.startsWith('+') ? '++' : '--'}state.${target.name};`];
|
|
774
|
+
}
|
|
775
|
+
if (c.mode === 'service') {
|
|
776
|
+
const sym = delta.startsWith('+') ? '++' : '--';
|
|
777
|
+
return [
|
|
778
|
+
ts.isPostfixUnaryExpression(e)
|
|
779
|
+
? `${indent}this.${target.name}${sym};`
|
|
780
|
+
: `${indent}${sym}this.${target.name};`,
|
|
781
|
+
`${indent}this.__notify();`,
|
|
782
|
+
];
|
|
783
|
+
}
|
|
784
|
+
if (c.mode === 'pipe') {
|
|
785
|
+
const sym = delta.startsWith('+') ? '++' : '--';
|
|
786
|
+
return [ts.isPostfixUnaryExpression(e)
|
|
787
|
+
? `${indent}this.${target.name}${sym};`
|
|
788
|
+
: `${indent}${sym}this.${target.name};`];
|
|
789
|
+
}
|
|
790
|
+
const shadowInc = c.shadowFields?.get(target.name);
|
|
791
|
+
if (shadowInc) {
|
|
792
|
+
return [
|
|
793
|
+
`${indent}${shadowInc} = ${shadowInc} ${delta};`,
|
|
794
|
+
`${indent}${setterName(target.name)}(${shadowInc});`,
|
|
795
|
+
];
|
|
796
|
+
}
|
|
797
|
+
return [`${indent}${setterName(target.name)}(${target.name} ${delta});`];
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
// A bare array mutation call on a field: push/pop/shift
|
|
801
|
+
// translate to setter calls (proof fix), the rest degrade.
|
|
802
|
+
const mutation = this.arrayMutationStatement(e, indent);
|
|
803
|
+
if (mutation)
|
|
804
|
+
return mutation;
|
|
805
|
+
const code = this.expr(e);
|
|
806
|
+
if (c.unsupported)
|
|
807
|
+
return this.unsupportedLine(indent, c.unsupported);
|
|
808
|
+
return [`${indent}${code};`];
|
|
809
|
+
}
|
|
810
|
+
if (ts.isReturnStatement(node)) {
|
|
811
|
+
if (!node.expression)
|
|
812
|
+
return [`${indent}return;`];
|
|
813
|
+
const code = this.expr(node.expression);
|
|
814
|
+
if (c.unsupported)
|
|
815
|
+
return this.unsupportedLine(indent, c.unsupported);
|
|
816
|
+
return [`${indent}return ${code};`];
|
|
817
|
+
}
|
|
818
|
+
if (ts.isIfStatement(node)) {
|
|
819
|
+
const cond = this.expr(node.expression);
|
|
820
|
+
if (c.unsupported)
|
|
821
|
+
return this.unsupportedLine(indent, c.unsupported);
|
|
822
|
+
const lines = [`${indent}if (${cond}) {`];
|
|
823
|
+
lines.push(...this.statement(node.thenStatement, indent + ' '));
|
|
824
|
+
lines.push(`${indent}}`);
|
|
825
|
+
if (node.elseStatement) {
|
|
826
|
+
if (ts.isIfStatement(node.elseStatement)) {
|
|
827
|
+
const elseLines = this.statement(node.elseStatement, indent);
|
|
828
|
+
// Merge "} else if (...) {" onto one line.
|
|
829
|
+
lines[lines.length - 1] = `${indent}} else ${elseLines[0].trimStart()}`;
|
|
830
|
+
lines.push(...elseLines.slice(1));
|
|
831
|
+
}
|
|
832
|
+
else {
|
|
833
|
+
lines[lines.length - 1] = `${indent}} else {`;
|
|
834
|
+
lines.push(...this.statement(node.elseStatement, indent + ' '));
|
|
835
|
+
lines.push(`${indent}}`);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
return lines;
|
|
839
|
+
}
|
|
840
|
+
if (ts.isVariableStatement(node)) {
|
|
841
|
+
const stmtText = node.getText();
|
|
842
|
+
const keyword = stmtText.startsWith('let') ? 'let' : stmtText.startsWith('var') ? 'var' : 'const';
|
|
843
|
+
const lines = [];
|
|
844
|
+
for (const decl of node.declarationList.declarations) {
|
|
845
|
+
if (!ts.isIdentifier(decl.name)) {
|
|
846
|
+
return this.unsupportedLine(indent, 'destructuring declaration');
|
|
847
|
+
}
|
|
848
|
+
if (!decl.initializer) {
|
|
849
|
+
lines.push(`${indent}${keyword} ${decl.name.text};`);
|
|
850
|
+
continue;
|
|
851
|
+
}
|
|
852
|
+
const code = this.expr(decl.initializer);
|
|
853
|
+
if (c.unsupported)
|
|
854
|
+
return this.unsupportedLine(indent, c.unsupported);
|
|
855
|
+
lines.push(`${indent}${keyword} ${decl.name.text} = ${code};`);
|
|
856
|
+
}
|
|
857
|
+
return lines;
|
|
858
|
+
}
|
|
859
|
+
return this.unsupportedLine(indent, ts.SyntaxKind[node.kind]);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
/**
|
|
863
|
+
* React mode: find the state fields a method assigns and then reads
|
|
864
|
+
* again later in the same method (in source order, blocks and if/else
|
|
865
|
+
* branches flattened in order). Those fields get a method-local shadow
|
|
866
|
+
* (see shadowName), because the generated setter call does not update
|
|
867
|
+
* the render-scope const the bare field name reads. Reads inside the
|
|
868
|
+
* assigning statement's own right-hand side are not "later": they see
|
|
869
|
+
* the value from before that assignment, which is the render value or
|
|
870
|
+
* the shadow left by an earlier assignment, both already correct.
|
|
871
|
+
*/
|
|
872
|
+
function findShadowedFields(statements, fieldNames, methodNames, stateFields) {
|
|
873
|
+
const shadowed = new Map();
|
|
874
|
+
const assigned = new Set();
|
|
875
|
+
const collectReads = (node, into) => {
|
|
876
|
+
if (ts.isBinaryExpression(node) && ASSIGN_OPS.has(node.operatorToken.kind) && isThisProperty(node.left)) {
|
|
877
|
+
// The assignment target is a write, not a read; the right side reads.
|
|
878
|
+
collectReads(node.right, into);
|
|
879
|
+
return;
|
|
880
|
+
}
|
|
881
|
+
const self = isThisProperty(node);
|
|
882
|
+
if (self) {
|
|
883
|
+
if (fieldNames.has(self.name) && !methodNames.has(self.name))
|
|
884
|
+
into.add(self.name);
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
ts.forEachChild(node, (child) => collectReads(child, into));
|
|
888
|
+
};
|
|
889
|
+
const collectAssigns = (node, into) => {
|
|
890
|
+
if (ts.isBinaryExpression(node) && ASSIGN_OPS.has(node.operatorToken.kind)) {
|
|
891
|
+
const target = isThisProperty(node.left);
|
|
892
|
+
if (target && fieldNames.has(target.name))
|
|
893
|
+
into.add(target.name);
|
|
894
|
+
}
|
|
895
|
+
if ((ts.isPostfixUnaryExpression(node) || ts.isPrefixUnaryExpression(node)) &&
|
|
896
|
+
(node.operator === ts.SyntaxKind.PlusPlusToken || node.operator === ts.SyntaxKind.MinusMinusToken)) {
|
|
897
|
+
const target = isThisProperty(node.operand);
|
|
898
|
+
if (target && fieldNames.has(target.name))
|
|
899
|
+
into.add(target.name);
|
|
900
|
+
}
|
|
901
|
+
const call = thisMethodCall(node);
|
|
902
|
+
if (call && fieldNames.has(call.field) && exports.MUTATING_ARRAY_METHODS.has(call.method))
|
|
903
|
+
into.add(call.field);
|
|
904
|
+
ts.forEachChild(node, (child) => collectAssigns(child, into));
|
|
905
|
+
};
|
|
906
|
+
const units = [];
|
|
907
|
+
const flatten = (list) => {
|
|
908
|
+
for (const stmt of list) {
|
|
909
|
+
if (ts.isBlock(stmt)) {
|
|
910
|
+
flatten([...stmt.statements]);
|
|
911
|
+
continue;
|
|
912
|
+
}
|
|
913
|
+
if (ts.isIfStatement(stmt)) {
|
|
914
|
+
units.push(stmt.expression);
|
|
915
|
+
flatten([stmt.thenStatement]);
|
|
916
|
+
if (stmt.elseStatement)
|
|
917
|
+
flatten([stmt.elseStatement]);
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
920
|
+
units.push(stmt);
|
|
921
|
+
}
|
|
922
|
+
};
|
|
923
|
+
flatten(statements);
|
|
924
|
+
for (const unit of units) {
|
|
925
|
+
const reads = new Set();
|
|
926
|
+
collectReads(unit, reads);
|
|
927
|
+
for (const name of reads) {
|
|
928
|
+
if (assigned.has(name) && stateFields.has(name))
|
|
929
|
+
shadowed.set(name, shadowName(name));
|
|
930
|
+
}
|
|
931
|
+
collectAssigns(unit, assigned);
|
|
932
|
+
}
|
|
933
|
+
return shadowed;
|
|
934
|
+
}
|
|
935
|
+
/**
|
|
936
|
+
* Translate all methods of a component (or service) in the given mode.
|
|
937
|
+
* Returns the translated methods plus accumulated diagnostics.
|
|
938
|
+
*/
|
|
939
|
+
function translateMethods(component, mode, stateFields, options = {}) {
|
|
940
|
+
// The stale-read check runs for components only: a service's fields
|
|
941
|
+
// are plain instance fields, so an assignment there is visible to
|
|
942
|
+
// the next read immediately and the trap does not exist.
|
|
943
|
+
const diagnostics = mode === 'react' ? [...analyzeFieldRereads(component), ...analyzeTimers(component), ...analyzeDeepMutations(component)] : [];
|
|
944
|
+
const methods = [];
|
|
945
|
+
const fieldNames = new Set(component.fields.map((f) => f.name));
|
|
946
|
+
const methodNames = new Set(component.methods);
|
|
947
|
+
for (const method of component.methodDetails) {
|
|
948
|
+
const statements = bodyStatements(method);
|
|
949
|
+
if (!statements)
|
|
950
|
+
continue;
|
|
951
|
+
const shadowFields = mode === 'react'
|
|
952
|
+
? findShadowedFields(statements, fieldNames, methodNames, stateFields)
|
|
953
|
+
: new Map();
|
|
954
|
+
const ctx = {
|
|
955
|
+
mode, fieldNames, methodNames, stateFields,
|
|
956
|
+
methodName: method.name, diagnostics,
|
|
957
|
+
outputCallbacks: options.outputCallbacks,
|
|
958
|
+
formFields: options.formFields,
|
|
959
|
+
shadowFields,
|
|
960
|
+
};
|
|
961
|
+
const translator = new Translator(ctx);
|
|
962
|
+
const lines = translator.statements(statements, '');
|
|
963
|
+
if (shadowFields.size) {
|
|
964
|
+
const prologue = [...shadowFields.entries()].map(([field, shadow]) => `let ${shadow} = ${field};`);
|
|
965
|
+
lines.unshift(...prologue);
|
|
966
|
+
}
|
|
967
|
+
methods.push({
|
|
968
|
+
name: method.name,
|
|
969
|
+
paramTexts: method.params.map((p) => p.text),
|
|
970
|
+
paramNames: method.params.map((p) => p.name),
|
|
971
|
+
lines,
|
|
972
|
+
});
|
|
973
|
+
}
|
|
974
|
+
return { methods, diagnostics };
|
|
975
|
+
}
|