@rangular-framework/compiler 0.0.0-stage → 1.0.1
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/compiler/src/catalog.d.ts +63 -0
- package/dist/compiler/src/catalog.js +198 -0
- package/dist/compiler/src/class/formInit.d.ts +119 -0
- package/dist/compiler/src/class/formInit.js +370 -0
- package/dist/compiler/src/class/parseComponent.d.ts +232 -0
- package/dist/compiler/src/class/parseComponent.js +430 -0
- package/dist/compiler/src/class/parseDirective.d.ts +42 -0
- package/dist/compiler/src/class/parseDirective.js +227 -0
- package/dist/compiler/src/class/parseRoutes.d.ts +59 -0
- package/dist/compiler/src/class/parseRoutes.js +163 -0
- package/dist/compiler/src/codegen/generate.d.ts +166 -0
- package/dist/compiler/src/codegen/generate.js +3466 -0
- package/dist/compiler/src/codegen/methods.d.ts +148 -0
- package/dist/compiler/src/codegen/methods.js +975 -0
- package/dist/compiler/src/codegen/sourceMap.d.ts +42 -0
- package/dist/compiler/src/codegen/sourceMap.js +77 -0
- package/dist/compiler/src/compile.d.ts +101 -0
- package/dist/compiler/src/compile.js +775 -0
- package/dist/compiler/src/diagnostics.d.ts +37 -0
- package/dist/compiler/src/diagnostics.js +113 -0
- package/dist/compiler/src/doctor.d.ts +35 -0
- package/dist/compiler/src/doctor.js +144 -0
- package/dist/compiler/src/fix.d.ts +21 -0
- package/dist/compiler/src/fix.js +44 -0
- package/dist/compiler/src/index.d.ts +22 -0
- package/dist/compiler/src/index.js +38 -0
- package/dist/compiler/src/preview/renderPreview.d.ts +101 -0
- package/dist/compiler/src/preview/renderPreview.js +7158 -0
- package/dist/compiler/src/preview/styleSnapshot.d.ts +36 -0
- package/dist/compiler/src/preview/styleSnapshot.js +268 -0
- package/dist/compiler/src/propTable.d.ts +22 -0
- package/dist/compiler/src/propTable.js +107 -0
- package/dist/compiler/src/style/css.d.ts +23 -0
- package/dist/compiler/src/style/css.js +161 -0
- package/dist/compiler/src/template/ast.d.ts +66 -0
- package/dist/compiler/src/template/ast.js +19 -0
- package/dist/compiler/src/template/evaluate.d.ts +45 -0
- package/dist/compiler/src/template/evaluate.js +151 -0
- package/dist/compiler/src/template/expr.d.ts +94 -0
- package/dist/compiler/src/template/expr.js +360 -0
- package/dist/compiler/src/template/forms.d.ts +42 -0
- package/dist/compiler/src/template/forms.js +80 -0
- package/dist/compiler/src/template/literal.d.ts +38 -0
- package/dist/compiler/src/template/literal.js +248 -0
- package/dist/compiler/src/template/parser.d.ts +14 -0
- package/dist/compiler/src/template/parser.js +230 -0
- package/dist/compiler/src/template/pipes.d.ts +48 -0
- package/dist/compiler/src/template/pipes.js +225 -0
- package/dist/compiler/src/template/printAst.d.ts +3 -0
- package/dist/compiler/src/template/printAst.js +38 -0
- package/dist/compiler/src/template/structural.d.ts +27 -0
- package/dist/compiler/src/template/structural.js +51 -0
- package/dist/runtime/src/index.d.ts +486 -0
- package/dist/runtime/src/index.js +1197 -0
- package/dist/ui/src/chart.d.ts +81 -0
- package/dist/ui/src/chart.js +196 -0
- package/dist/ui/src/datetime.d.ts +92 -0
- package/dist/ui/src/datetime.js +240 -0
- package/dist/ui/src/icons.d.ts +63 -0
- package/dist/ui/src/icons.js +310 -0
- package/dist/ui/src/mask.d.ts +38 -0
- package/dist/ui/src/mask.js +103 -0
- package/dist/ui/src/palette.d.ts +66 -0
- package/dist/ui/src/palette.js +141 -0
- package/dist/ui/src/tokens.d.ts +129 -0
- package/dist/ui/src/tokens.js +173 -0
- package/package.json +24 -4
- package/README.md +0 -3
|
@@ -0,0 +1,370 @@
|
|
|
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.isFormInitializer = isFormInitializer;
|
|
37
|
+
exports.signalKindOf = signalKindOf;
|
|
38
|
+
exports.isSignalInitializer = isSignalInitializer;
|
|
39
|
+
exports.isStreamInitializer = isStreamInitializer;
|
|
40
|
+
exports.builderDepsOf = builderDepsOf;
|
|
41
|
+
exports.formFieldKind = formFieldKind;
|
|
42
|
+
exports.rewriteBuilderInitializer = rewriteBuilderInitializer;
|
|
43
|
+
exports.analyzeFormInitializer = analyzeFormInitializer;
|
|
44
|
+
exports.evaluateFormInitializer = evaluateFormInitializer;
|
|
45
|
+
const ts = __importStar(require("typescript"));
|
|
46
|
+
const index_1 = require("../../../runtime/src/index");
|
|
47
|
+
const FORM_NEW_RE = /^\s*new\s+(FormGroup|FormControl|FormArray)\s*\(/;
|
|
48
|
+
const FORM_BUILDER_RE = /^\s*this\.([A-Za-z_$][\w$]*)\.(group|control|array)\s*\(/;
|
|
49
|
+
/**
|
|
50
|
+
* True for field initializers that build a form tree: a `new
|
|
51
|
+
* FormGroup/FormControl/FormArray(...)`, or a call on a FormBuilder
|
|
52
|
+
* constructor dep (`this.fb.group(...)`) when the dep name is in
|
|
53
|
+
* builderDeps.
|
|
54
|
+
*/
|
|
55
|
+
function isFormInitializer(initializer, builderDeps) {
|
|
56
|
+
if (!initializer)
|
|
57
|
+
return false;
|
|
58
|
+
if (FORM_NEW_RE.test(initializer))
|
|
59
|
+
return true;
|
|
60
|
+
const m = FORM_BUILDER_RE.exec(initializer);
|
|
61
|
+
return !!m && !!builderDeps && builderDeps.has(m[1]);
|
|
62
|
+
}
|
|
63
|
+
const SIGNAL_CALL_RE = /^\s*(signal|computed)\s*(<[^>]*>)?\s*\(/;
|
|
64
|
+
/**
|
|
65
|
+
* The kind of a signal field initializer (signals adoption): a field
|
|
66
|
+
* initialised with `signal(...)` or `computed(...)` from
|
|
67
|
+
* '@angular/core'. Like a subject field, a signal field is one stable
|
|
68
|
+
* instance for the component's whole life: codegen holds it in a ref,
|
|
69
|
+
* because a signal recreated per render would lose its value and
|
|
70
|
+
* orphan every reader. In a service the same initializer is a plain
|
|
71
|
+
* class field, emitted verbatim.
|
|
72
|
+
*/
|
|
73
|
+
function signalKindOf(initializer) {
|
|
74
|
+
if (!initializer)
|
|
75
|
+
return undefined;
|
|
76
|
+
const m = SIGNAL_CALL_RE.exec(initializer);
|
|
77
|
+
return m ? m[1] : undefined;
|
|
78
|
+
}
|
|
79
|
+
function isSignalInitializer(initializer) {
|
|
80
|
+
return signalKindOf(initializer) !== undefined;
|
|
81
|
+
}
|
|
82
|
+
const STREAM_NEW_RE = /^\s*new\s+(Subject|BehaviorSubject|ReplaySubject|AsyncSubject)\s*(<[^>]*>)?\s*\(/;
|
|
83
|
+
/**
|
|
84
|
+
* True when a field initializer constructs an RxJS subject (RxJS
|
|
85
|
+
* adoption). Like a form field, a subject field is one stable
|
|
86
|
+
* instance for the component's whole life: codegen holds it in a
|
|
87
|
+
* ref, because a subject recreated per render would orphan every
|
|
88
|
+
* subscriber of the previous instance.
|
|
89
|
+
*/
|
|
90
|
+
function isStreamInitializer(initializer) {
|
|
91
|
+
if (!initializer)
|
|
92
|
+
return false;
|
|
93
|
+
return STREAM_NEW_RE.test(initializer);
|
|
94
|
+
}
|
|
95
|
+
/** The FormBuilder constructor dep names of a host component or service. */
|
|
96
|
+
function builderDepsOf(host) {
|
|
97
|
+
return new Set((host.constructorDeps ?? [])
|
|
98
|
+
.filter((d) => d.typeName === 'FormBuilder')
|
|
99
|
+
.map((d) => d.propertyName));
|
|
100
|
+
}
|
|
101
|
+
/** Parse `const __x = <initializer>;` and return its top-level expression. */
|
|
102
|
+
function topExpression(initializer) {
|
|
103
|
+
const sf = ts.createSourceFile('form-init.ts', `const __x = ${initializer};`, ts.ScriptTarget.Latest,
|
|
104
|
+
/* setParentNodes */ true);
|
|
105
|
+
let found;
|
|
106
|
+
const visit = (node) => {
|
|
107
|
+
if (found)
|
|
108
|
+
return;
|
|
109
|
+
if (ts.isVariableDeclaration(node) && node.initializer) {
|
|
110
|
+
found = node.initializer;
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
ts.forEachChild(node, visit);
|
|
114
|
+
};
|
|
115
|
+
visit(sf);
|
|
116
|
+
return found;
|
|
117
|
+
}
|
|
118
|
+
/** The builder call parts of `this.<dep>.<method>(...)`, when it is one. */
|
|
119
|
+
function builderCallOf(node) {
|
|
120
|
+
if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression))
|
|
121
|
+
return undefined;
|
|
122
|
+
const target = node.expression.expression;
|
|
123
|
+
if (!ts.isPropertyAccessExpression(target) || target.expression.kind !== ts.SyntaxKind.ThisKeyword)
|
|
124
|
+
return undefined;
|
|
125
|
+
const method = node.expression.name.text;
|
|
126
|
+
if (method !== 'group' && method !== 'control' && method !== 'array')
|
|
127
|
+
return undefined;
|
|
128
|
+
return { dep: target.name.text, method, args: node.arguments };
|
|
129
|
+
}
|
|
130
|
+
/** The kind one config entry builds, from its shape alone. */
|
|
131
|
+
function kindOfEntry(node) {
|
|
132
|
+
if (ts.isNewExpression(node) && ts.isIdentifier(node.expression)) {
|
|
133
|
+
if (node.expression.text === 'FormGroup')
|
|
134
|
+
return 'group';
|
|
135
|
+
if (node.expression.text === 'FormArray')
|
|
136
|
+
return 'array';
|
|
137
|
+
return 'control';
|
|
138
|
+
}
|
|
139
|
+
const call = builderCallOf(node);
|
|
140
|
+
if (call)
|
|
141
|
+
return call.method === 'group' ? 'group' : call.method === 'array' ? 'array' : 'control';
|
|
142
|
+
return 'control';
|
|
143
|
+
}
|
|
144
|
+
function kindsOfConfig(config) {
|
|
145
|
+
const controlNames = [];
|
|
146
|
+
const controlKinds = {};
|
|
147
|
+
if (config && ts.isObjectLiteralExpression(config)) {
|
|
148
|
+
for (const prop of config.properties) {
|
|
149
|
+
if (ts.isPropertyAssignment(prop)) {
|
|
150
|
+
const name = prop.name.getText().replace(/^['"]|['"]$/g, '');
|
|
151
|
+
controlNames.push(name);
|
|
152
|
+
controlKinds[name] = kindOfEntry(prop.initializer);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return { controlNames, controlKinds };
|
|
157
|
+
}
|
|
158
|
+
/** Kind and group control names from the initializer's shape alone. */
|
|
159
|
+
function formFieldKind(initializer, builderDeps) {
|
|
160
|
+
const expr = topExpression(initializer);
|
|
161
|
+
if (!expr)
|
|
162
|
+
return { kind: 'control', controlNames: [], controlKinds: {} };
|
|
163
|
+
if (ts.isNewExpression(expr) && ts.isIdentifier(expr.expression)) {
|
|
164
|
+
if (expr.expression.text === 'FormGroup') {
|
|
165
|
+
return { kind: 'group', ...kindsOfConfig(expr.arguments?.[0]) };
|
|
166
|
+
}
|
|
167
|
+
if (expr.expression.text === 'FormArray')
|
|
168
|
+
return { kind: 'array', controlNames: [], controlKinds: {} };
|
|
169
|
+
return { kind: 'control', controlNames: [], controlKinds: {} };
|
|
170
|
+
}
|
|
171
|
+
const call = builderCallOf(expr);
|
|
172
|
+
if (call && builderDeps && builderDeps.has(call.dep)) {
|
|
173
|
+
if (call.method === 'group') {
|
|
174
|
+
return { kind: 'group', ...kindsOfConfig(call.args[0]), builderDep: call.dep };
|
|
175
|
+
}
|
|
176
|
+
return { kind: call.method === 'array' ? 'array' : 'control', controlNames: [], controlKinds: {}, builderDep: call.dep };
|
|
177
|
+
}
|
|
178
|
+
return { kind: 'control', controlNames: [], controlKinds: {} };
|
|
179
|
+
}
|
|
180
|
+
class Unsupported extends Error {
|
|
181
|
+
}
|
|
182
|
+
function evalLiteral(node) {
|
|
183
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
184
|
+
return node.text;
|
|
185
|
+
if (ts.isNumericLiteral(node))
|
|
186
|
+
return Number(node.text);
|
|
187
|
+
if (node.kind === ts.SyntaxKind.TrueKeyword)
|
|
188
|
+
return true;
|
|
189
|
+
if (node.kind === ts.SyntaxKind.FalseKeyword)
|
|
190
|
+
return false;
|
|
191
|
+
if (node.kind === ts.SyntaxKind.NullKeyword)
|
|
192
|
+
return null;
|
|
193
|
+
if (ts.isPrefixUnaryExpression(node) && node.operator === ts.SyntaxKind.MinusToken) {
|
|
194
|
+
const inner = evalLiteral(node.operand);
|
|
195
|
+
if (typeof inner === 'number')
|
|
196
|
+
return -inner;
|
|
197
|
+
}
|
|
198
|
+
throw new Unsupported(`a ${ts.SyntaxKind[node.kind]} is not a literal value`);
|
|
199
|
+
}
|
|
200
|
+
/** `Validators.required` and friends, as member or call expressions. */
|
|
201
|
+
function evalValidator(node) {
|
|
202
|
+
if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.expression)
|
|
203
|
+
&& node.expression.text === 'Validators') {
|
|
204
|
+
if (node.name.text === 'required')
|
|
205
|
+
return index_1.Validators.required;
|
|
206
|
+
if (node.name.text === 'email')
|
|
207
|
+
return index_1.Validators.email;
|
|
208
|
+
throw new Unsupported(`Validators.${node.name.text} is used as a value but takes an argument`);
|
|
209
|
+
}
|
|
210
|
+
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)
|
|
211
|
+
&& ts.isIdentifier(node.expression.expression)
|
|
212
|
+
&& node.expression.expression.text === 'Validators') {
|
|
213
|
+
const name = node.expression.name.text;
|
|
214
|
+
const arg = node.arguments[0] ? evalLiteral(node.arguments[0]) : undefined;
|
|
215
|
+
if (name === 'minLength' && typeof arg === 'number')
|
|
216
|
+
return index_1.Validators.minLength(arg);
|
|
217
|
+
if (name === 'maxLength' && typeof arg === 'number')
|
|
218
|
+
return index_1.Validators.maxLength(arg);
|
|
219
|
+
if (name === 'pattern' && typeof arg === 'string')
|
|
220
|
+
return index_1.Validators.pattern(arg);
|
|
221
|
+
throw new Unsupported(`Validators.${name}(...) is outside the supported subset`);
|
|
222
|
+
}
|
|
223
|
+
throw new Unsupported('validators must be Validators references or calls');
|
|
224
|
+
}
|
|
225
|
+
function evalValidators(node) {
|
|
226
|
+
if (!node)
|
|
227
|
+
return [];
|
|
228
|
+
if (ts.isArrayLiteralExpression(node))
|
|
229
|
+
return node.elements.map(evalValidator);
|
|
230
|
+
return [evalValidator(node)];
|
|
231
|
+
}
|
|
232
|
+
/** One builder config entry: a node expression, a tuple, or a literal. */
|
|
233
|
+
function evalBuilderEntry(node) {
|
|
234
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
235
|
+
const initial = node.elements[0] ? evalLiteral(node.elements[0]) : null;
|
|
236
|
+
return new index_1.FormControl(initial, evalValidators(node.elements[1]));
|
|
237
|
+
}
|
|
238
|
+
if (ts.isNewExpression(node) || ts.isCallExpression(node))
|
|
239
|
+
return evalFormNode(node);
|
|
240
|
+
return new index_1.FormControl(evalLiteral(node));
|
|
241
|
+
}
|
|
242
|
+
function evalFormNode(node) {
|
|
243
|
+
if (ts.isNewExpression(node) && ts.isIdentifier(node.expression)) {
|
|
244
|
+
if (node.expression.text === 'FormControl') {
|
|
245
|
+
const args = node.arguments ?? [];
|
|
246
|
+
const initial = args[0] ? evalLiteral(args[0]) : null;
|
|
247
|
+
return new index_1.FormControl(initial, evalValidators(args[1]));
|
|
248
|
+
}
|
|
249
|
+
if (node.expression.text === 'FormGroup') {
|
|
250
|
+
const config = node.arguments?.[0];
|
|
251
|
+
if (!config || !ts.isObjectLiteralExpression(config)) {
|
|
252
|
+
throw new Unsupported('a FormGroup config must be an object literal of form nodes');
|
|
253
|
+
}
|
|
254
|
+
const controls = {};
|
|
255
|
+
for (const prop of config.properties) {
|
|
256
|
+
if (!ts.isPropertyAssignment(prop)) {
|
|
257
|
+
throw new Unsupported('FormGroup entries must be name: <form node> pairs');
|
|
258
|
+
}
|
|
259
|
+
const name = prop.name.getText().replace(/^['"]|['"]$/g, '');
|
|
260
|
+
controls[name] = evalFormNode(prop.initializer);
|
|
261
|
+
}
|
|
262
|
+
return new index_1.FormGroup(controls);
|
|
263
|
+
}
|
|
264
|
+
if (node.expression.text === 'FormArray') {
|
|
265
|
+
const list = node.arguments?.[0];
|
|
266
|
+
if (!list || !ts.isArrayLiteralExpression(list)) {
|
|
267
|
+
throw new Unsupported('a FormArray argument must be an array literal of form nodes');
|
|
268
|
+
}
|
|
269
|
+
return new index_1.FormArray(list.elements.map(evalFormNode));
|
|
270
|
+
}
|
|
271
|
+
throw new Unsupported(`new ${node.expression.text}(...) is not a form class`);
|
|
272
|
+
}
|
|
273
|
+
const call = builderCallOf(node);
|
|
274
|
+
if (call) {
|
|
275
|
+
if (call.method === 'control') {
|
|
276
|
+
const initial = call.args[0] ? evalLiteral(call.args[0]) : null;
|
|
277
|
+
return new index_1.FormControl(initial, evalValidators(call.args[1]));
|
|
278
|
+
}
|
|
279
|
+
if (call.method === 'group') {
|
|
280
|
+
const config = call.args[0];
|
|
281
|
+
if (!config || !ts.isObjectLiteralExpression(config)) {
|
|
282
|
+
throw new Unsupported('a builder group config must be an object literal');
|
|
283
|
+
}
|
|
284
|
+
const controls = {};
|
|
285
|
+
for (const prop of config.properties) {
|
|
286
|
+
if (!ts.isPropertyAssignment(prop)) {
|
|
287
|
+
throw new Unsupported('builder group entries must be name: <entry> pairs');
|
|
288
|
+
}
|
|
289
|
+
const name = prop.name.getText().replace(/^['"]|['"]$/g, '');
|
|
290
|
+
controls[name] = evalBuilderEntry(prop.initializer);
|
|
291
|
+
}
|
|
292
|
+
return new index_1.FormGroup(controls);
|
|
293
|
+
}
|
|
294
|
+
const list = call.args[0];
|
|
295
|
+
if (!list || !ts.isArrayLiteralExpression(list)) {
|
|
296
|
+
throw new Unsupported('a builder array argument must be an array literal');
|
|
297
|
+
}
|
|
298
|
+
return new index_1.FormArray(list.elements.map(evalBuilderEntry));
|
|
299
|
+
}
|
|
300
|
+
throw new Unsupported('expected new FormControl(...), new FormGroup(...), new FormArray(...) or a FormBuilder call');
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Rewrite the `this.<dep>` roots of a builder initializer to the bare
|
|
304
|
+
* dep name, for generated code: the generated component holds the
|
|
305
|
+
* injected FormBuilder in a local of the dep's own name, so
|
|
306
|
+
* `this.fb.group(...)` becomes `fb.group(...)`, nested builder calls
|
|
307
|
+
* included. Spans come from the TypeScript AST, so the text of a
|
|
308
|
+
* string literal in the config is never touched.
|
|
309
|
+
*/
|
|
310
|
+
function rewriteBuilderInitializer(initializer, dep) {
|
|
311
|
+
const prefix = 'const __x = ';
|
|
312
|
+
const sf = ts.createSourceFile('form-init.ts', `${prefix}${initializer};`, ts.ScriptTarget.Latest,
|
|
313
|
+
/* setParentNodes */ true);
|
|
314
|
+
const spans = [];
|
|
315
|
+
const visit = (node) => {
|
|
316
|
+
if (ts.isPropertyAccessExpression(node)
|
|
317
|
+
&& node.expression.kind === ts.SyntaxKind.ThisKeyword
|
|
318
|
+
&& node.name.text === dep) {
|
|
319
|
+
spans.push({ start: node.getStart() - prefix.length, end: node.name.getEnd() - prefix.length });
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
ts.forEachChild(node, visit);
|
|
323
|
+
};
|
|
324
|
+
visit(sf);
|
|
325
|
+
let out = initializer;
|
|
326
|
+
for (const span of spans.sort((a, b) => b.start - a.start)) {
|
|
327
|
+
out = out.slice(0, span.start) + dep + out.slice(span.end);
|
|
328
|
+
}
|
|
329
|
+
return out;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Analyse one form field initializer: its kind, its group control
|
|
333
|
+
* names and kinds, and whether the preview can evaluate it.
|
|
334
|
+
* builderDeps names the component's FormBuilder constructor deps, so
|
|
335
|
+
* `this.fb.group(...)` initializers are recognised as builder calls.
|
|
336
|
+
*/
|
|
337
|
+
function analyzeFormInitializer(initializer, builderDeps) {
|
|
338
|
+
const { kind, controlNames, controlKinds, builderDep } = formFieldKind(initializer, builderDeps);
|
|
339
|
+
const expr = topExpression(initializer);
|
|
340
|
+
if (!expr)
|
|
341
|
+
return { kind, controlNames, controlKinds, supported: false, reason: 'the initializer could not be parsed', builderDep };
|
|
342
|
+
try {
|
|
343
|
+
evalFormNode(expr);
|
|
344
|
+
return { kind, controlNames, controlKinds, supported: true, builderDep };
|
|
345
|
+
}
|
|
346
|
+
catch (e) {
|
|
347
|
+
return {
|
|
348
|
+
kind, controlNames, controlKinds, supported: false,
|
|
349
|
+
reason: e instanceof Unsupported ? e.message : 'the initializer could not be evaluated',
|
|
350
|
+
builderDep,
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Build the real runtime instance for a form field initializer, for
|
|
356
|
+
* the preview's server-side paint. Fails with { ok: false } on the
|
|
357
|
+
* same shapes analyzeFormInitializer reports as unsupported.
|
|
358
|
+
*/
|
|
359
|
+
function evaluateFormInitializer(initializer, builderDeps) {
|
|
360
|
+
const expr = topExpression(initializer);
|
|
361
|
+
if (!expr)
|
|
362
|
+
return { ok: false, reason: 'the initializer could not be parsed' };
|
|
363
|
+
try {
|
|
364
|
+
void builderDeps;
|
|
365
|
+
return { ok: true, value: evalFormNode(expr) };
|
|
366
|
+
}
|
|
367
|
+
catch (e) {
|
|
368
|
+
return { ok: false, reason: e instanceof Unsupported ? e.message : 'the initializer could not be evaluated' };
|
|
369
|
+
}
|
|
370
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { Diagnostic } from '../diagnostics';
|
|
2
|
+
export interface ComponentField {
|
|
3
|
+
name: string;
|
|
4
|
+
/** Source text of the initializer, if any (e.g. `'Welcome'`, `0`, `false`). */
|
|
5
|
+
initializer?: string;
|
|
6
|
+
/** Source text of the declared type, if any. */
|
|
7
|
+
type?: string;
|
|
8
|
+
isInput: boolean;
|
|
9
|
+
isOutput: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface MethodParam {
|
|
12
|
+
/** Bare parameter name, e.g. `step` or `$event`. */
|
|
13
|
+
name: string;
|
|
14
|
+
/** Full source text, e.g. `step: number = 1`, preserved in generated .tsx. */
|
|
15
|
+
text: string;
|
|
16
|
+
}
|
|
17
|
+
export interface ComponentMethod {
|
|
18
|
+
name: string;
|
|
19
|
+
params: MethodParam[];
|
|
20
|
+
/** Source text of the method body block (including braces), if it has one. */
|
|
21
|
+
body?: string;
|
|
22
|
+
}
|
|
23
|
+
/** One constructor parameter property: `private mail: MailService`. */
|
|
24
|
+
export interface ConstructorDep {
|
|
25
|
+
/** The property the dependency is stored on, e.g. `mail`. */
|
|
26
|
+
propertyName: string;
|
|
27
|
+
/** The declared type name used as the DI token, e.g. `MailService`. */
|
|
28
|
+
typeName: string;
|
|
29
|
+
}
|
|
30
|
+
export interface ComponentInfo {
|
|
31
|
+
className: string;
|
|
32
|
+
/** 1-based line of the class declaration in its source file (Stage 5, source maps). */
|
|
33
|
+
sourceLine?: number;
|
|
34
|
+
selector?: string;
|
|
35
|
+
template?: string;
|
|
36
|
+
templateUrl?: string;
|
|
37
|
+
styleUrls: string[];
|
|
38
|
+
/** Inline styles, joined when several strings are given. */
|
|
39
|
+
styles?: string;
|
|
40
|
+
fields: ComponentField[];
|
|
41
|
+
methods: string[];
|
|
42
|
+
/** Method signatures + body source, for method translation (Stage 2). */
|
|
43
|
+
methodDetails: ComponentMethod[];
|
|
44
|
+
/** Constructor parameter properties (Stage 3 DI). Empty when no constructor. */
|
|
45
|
+
constructorDeps?: ConstructorDep[];
|
|
46
|
+
/** Source text of the constructor body block, when one exists. */
|
|
47
|
+
constructorBody?: string;
|
|
48
|
+
}
|
|
49
|
+
/** An @Injectable class (Stage 3, M6), same member shape as a component. */
|
|
50
|
+
export interface ServiceInfo {
|
|
51
|
+
className: string;
|
|
52
|
+
/** True only for @Injectable({ providedIn: 'root' }), the injectable kind. */
|
|
53
|
+
providedInRoot: boolean;
|
|
54
|
+
fields: ComponentField[];
|
|
55
|
+
methods: string[];
|
|
56
|
+
methodDetails: ComponentMethod[];
|
|
57
|
+
constructorDeps: ConstructorDep[];
|
|
58
|
+
constructorBody?: string;
|
|
59
|
+
/** Key of the file this class was read from (the compile.ts registry key). */
|
|
60
|
+
fileName: string;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* A custom @Pipe class (Stage 12). The compiler registers it by its
|
|
64
|
+
* pipe `name`, emits the class into the generated file and rewrites
|
|
65
|
+
* `value | name:args` into a call on one shared instance's transform.
|
|
66
|
+
*/
|
|
67
|
+
export interface PipeInfo {
|
|
68
|
+
/** The pipe name from @Pipe({ name: '...' }), used after the | in templates. */
|
|
69
|
+
pipeName: string;
|
|
70
|
+
className: string;
|
|
71
|
+
fields: ComponentField[];
|
|
72
|
+
methods: string[];
|
|
73
|
+
methodDetails: ComponentMethod[];
|
|
74
|
+
constructorDeps: ConstructorDep[];
|
|
75
|
+
constructorBody?: string;
|
|
76
|
+
/** Bare parameter names of transform(value, ...args), in order. */
|
|
77
|
+
transformParams: string[];
|
|
78
|
+
/**
|
|
79
|
+
* When transform's body is exactly one return statement, the source
|
|
80
|
+
* text of the returned expression. The preview's static paint can
|
|
81
|
+
* evaluate that shape with its safe expression evaluator (no eval);
|
|
82
|
+
* richer bodies run only in the interactive preview and the app.
|
|
83
|
+
*/
|
|
84
|
+
transformReturn?: string;
|
|
85
|
+
/** Key of the file this class was read from (the compile.ts registry key). */
|
|
86
|
+
fileName: string;
|
|
87
|
+
}
|
|
88
|
+
/** One @Input field of a directive class. */
|
|
89
|
+
export interface DirectiveInput {
|
|
90
|
+
name: string;
|
|
91
|
+
/** Source text of the initializer, when one is written (the default). */
|
|
92
|
+
initializer?: string;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* An attribute @Directive class (Stage 12), the supported subset: a
|
|
96
|
+
* bracket selector, @Input fields, an ElementRef constructor
|
|
97
|
+
* dependency, and styling logic in ngOnInit or in a setter for one
|
|
98
|
+
* input. parseDirective.ts extracts the style assignments.
|
|
99
|
+
*/
|
|
100
|
+
export interface DirectiveInfo {
|
|
101
|
+
className: string;
|
|
102
|
+
/** The selector as written, e.g. '[appHighlight]'. */
|
|
103
|
+
selector?: string;
|
|
104
|
+
/** The attribute name inside the brackets, when the selector has that shape. */
|
|
105
|
+
attrName?: string;
|
|
106
|
+
inputs: DirectiveInput[];
|
|
107
|
+
/** The constructor property typed ElementRef, when one is injected. */
|
|
108
|
+
elProp?: string;
|
|
109
|
+
/** Source text of the ngOnInit body block, when present. */
|
|
110
|
+
initBody?: string;
|
|
111
|
+
/** A set accessor: its property, its parameter name, its body block. */
|
|
112
|
+
setter?: {
|
|
113
|
+
prop: string;
|
|
114
|
+
paramName: string;
|
|
115
|
+
body: string;
|
|
116
|
+
};
|
|
117
|
+
/** Key of the file this class was read from (the compile.ts registry key). */
|
|
118
|
+
fileName: string;
|
|
119
|
+
}
|
|
120
|
+
/** One binding an import declaration introduces in the importing file. */
|
|
121
|
+
export interface ImportBinding {
|
|
122
|
+
/** Local name bound in the importing file, e.g. 'fmt'. */
|
|
123
|
+
local: string;
|
|
124
|
+
/**
|
|
125
|
+
* The exported name in the source module: 'default' for a default
|
|
126
|
+
* import, '*' for a namespace import, or the named export as written
|
|
127
|
+
* before any `as`.
|
|
128
|
+
*/
|
|
129
|
+
imported: string;
|
|
130
|
+
/** Type-only binding (an `import type` clause or an inline `type` modifier). */
|
|
131
|
+
typeOnly: boolean;
|
|
132
|
+
}
|
|
133
|
+
export interface ImportInfo {
|
|
134
|
+
/** Named imports by local name, e.g. ['Component', 'Input']. */
|
|
135
|
+
names: string[];
|
|
136
|
+
/** Module specifier as written, e.g. './mail.service' or '@angular/core'. */
|
|
137
|
+
from: string;
|
|
138
|
+
/**
|
|
139
|
+
* Every binding the declaration introduces (default, named,
|
|
140
|
+
* namespace). Empty for a side-effect import (`import 'x';`).
|
|
141
|
+
*/
|
|
142
|
+
bindings: ImportBinding[];
|
|
143
|
+
/** 1-based line of the declaration in its file. */
|
|
144
|
+
line: number;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* An `import A = require('x')` or `import A = B.C` declaration
|
|
148
|
+
* (Stage: third-party imports). Recorded so the pipeline can diagnose
|
|
149
|
+
* it: the form cannot be carried into the generated module.
|
|
150
|
+
*/
|
|
151
|
+
export interface ImportEqualsInfo {
|
|
152
|
+
/** The local name the declaration binds, e.g. 'AS'. */
|
|
153
|
+
local: string;
|
|
154
|
+
/** 1-based line of the declaration in its file. */
|
|
155
|
+
line: number;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* A third-party import binding compile.ts hands to codegen for
|
|
159
|
+
* carrying into the generated module (merged across contributing
|
|
160
|
+
* files: the same binding declared twice is carried once).
|
|
161
|
+
*/
|
|
162
|
+
export interface CarriedImportBinding extends ImportBinding {
|
|
163
|
+
/** Module specifier as written in the source, carried verbatim. */
|
|
164
|
+
from: string;
|
|
165
|
+
/** The contributing source file that declared the binding. */
|
|
166
|
+
file: string;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* A top-level `const` declaration in a source file (0.6.5). A file
|
|
170
|
+
* that declares no classes can still contribute values: an exported
|
|
171
|
+
* const whose initializer is a literal (a string, number, boolean,
|
|
172
|
+
* or an array or object built only of literals) is carried into
|
|
173
|
+
* the generated module, which is what makes the Angular
|
|
174
|
+
* environment.ts shape work. Anything else stays uncarried and a
|
|
175
|
+
* value import of it is reported (GEN_IMPORT_CLASSLESS).
|
|
176
|
+
*/
|
|
177
|
+
export interface ModuleConstInfo {
|
|
178
|
+
/** The declared name, e.g. 'environment'. */
|
|
179
|
+
name: string;
|
|
180
|
+
/** The full declaration as written, normalised to one statement: `const name = <init>;`. */
|
|
181
|
+
text: string;
|
|
182
|
+
/** The initializer's source text, for re-emission under an import alias. */
|
|
183
|
+
initText: string;
|
|
184
|
+
/** True when the declaration carries the export modifier. */
|
|
185
|
+
exported: boolean;
|
|
186
|
+
/** True when the initializer is a literal or built only of literals. */
|
|
187
|
+
literal: boolean;
|
|
188
|
+
/** 1-based line of the declaration in its file. */
|
|
189
|
+
line: number;
|
|
190
|
+
}
|
|
191
|
+
/** Everything one TypeScript file contributes to a compilation. */
|
|
192
|
+
export interface ClassFileInfo {
|
|
193
|
+
components: ComponentInfo[];
|
|
194
|
+
services: ServiceInfo[];
|
|
195
|
+
/** Custom @Pipe classes (Stage 12). */
|
|
196
|
+
pipes: PipeInfo[];
|
|
197
|
+
/** Attribute @Directive classes (Stage 12). */
|
|
198
|
+
directives: DirectiveInfo[];
|
|
199
|
+
/** Top-level const declarations (0.6.5), carried when literal and imported. */
|
|
200
|
+
consts: ModuleConstInfo[];
|
|
201
|
+
imports: ImportInfo[];
|
|
202
|
+
/** `import A = require(...)` / `import A = B.C` declarations. */
|
|
203
|
+
importEquals: ImportEqualsInfo[];
|
|
204
|
+
/**
|
|
205
|
+
* TypeScript syntax errors in this file, from the parser itself.
|
|
206
|
+
* TypeScript recovers from these and still returns a tree, so
|
|
207
|
+
* without this list a malformed file reads as a smaller, valid
|
|
208
|
+
* file: the silent drop the compiler must never do. compile.ts
|
|
209
|
+
* turns each entry into a CLS_PARSE error.
|
|
210
|
+
*/
|
|
211
|
+
parseErrors: {
|
|
212
|
+
message: string;
|
|
213
|
+
line: number;
|
|
214
|
+
column: number;
|
|
215
|
+
}[];
|
|
216
|
+
}
|
|
217
|
+
export interface ParseComponentResult {
|
|
218
|
+
component?: ComponentInfo;
|
|
219
|
+
diagnostics: Diagnostic[];
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Read a whole TypeScript file: every @Component class, every @Injectable
|
|
223
|
+
* class and every import statement. Stage 3's registry (compile.ts) follows
|
|
224
|
+
* relative imports across files using this per-file view. Emits no
|
|
225
|
+
* diagnostics of its own, a file of only services is a valid file.
|
|
226
|
+
*/
|
|
227
|
+
export declare function parseClassFile(tsSource: string, fileName?: string): ClassFileInfo;
|
|
228
|
+
/**
|
|
229
|
+
* Extract @Component metadata from a TypeScript source file using the
|
|
230
|
+
* official TypeScript compiler API, Rangular never parses TS by hand.
|
|
231
|
+
*/
|
|
232
|
+
export declare function parseComponent(tsSource: string, fileName?: string): ParseComponentResult;
|