@rangular-framework/cli 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/cli/src/cli.js +1229 -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,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.evaluateExpression = evaluateExpression;
|
|
4
|
+
exports.evaluateExpr = evaluateExpr;
|
|
5
|
+
exports.applyPipe = applyPipe;
|
|
6
|
+
const expr_1 = require("./expr");
|
|
7
|
+
const pipes_1 = require("./pipes");
|
|
8
|
+
/**
|
|
9
|
+
* Evaluate a parsed template expression against a scope object.
|
|
10
|
+
* This walks the expression AST produced by the Pratt parser, it never
|
|
11
|
+
* uses eval/Function on source text.
|
|
12
|
+
*
|
|
13
|
+
* Semantics follow Angular templates pragmatically:
|
|
14
|
+
* - unknown identifiers evaluate to undefined;
|
|
15
|
+
* - member/index access on null/undefined evaluates to undefined;
|
|
16
|
+
* - method calls through a member (obj.fn()) keep `obj` as `this`;
|
|
17
|
+
* - built-in pipes: uppercase, lowercase, titlecase, json.
|
|
18
|
+
* Unknown pipes throw a clear error.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Evaluate a parsed template expression against a scope object.
|
|
22
|
+
* This walks the expression AST produced by the Pratt parser, it never
|
|
23
|
+
* uses eval/Function on source text.
|
|
24
|
+
*
|
|
25
|
+
* Semantics follow Angular templates pragmatically:
|
|
26
|
+
* - unknown identifiers evaluate to undefined;
|
|
27
|
+
* - member/index access on null/undefined evaluates to undefined;
|
|
28
|
+
* - method calls through a member (obj.fn()) keep `obj` as `this`;
|
|
29
|
+
* - built-in pipes: the Stage 12 set in template/pipes.ts
|
|
30
|
+
* (uppercase, lowercase, titlecase, json, date, number, percent,
|
|
31
|
+
* currency, slice, keyvalue, async), plus any custom pipes the
|
|
32
|
+
* caller supplies. Unknown pipes throw a clear error.
|
|
33
|
+
*
|
|
34
|
+
* `refs` (Stage 8) carries template reference variables: when the base
|
|
35
|
+
* of a member chain is a reference name (an ngModel input ref like
|
|
36
|
+
* `#email="ngModel"`), the chain reads from the reference object
|
|
37
|
+
* (its live validation state), not from the scope field of the same
|
|
38
|
+
* name. A bare reference identifier still resolves in the scope, exactly
|
|
39
|
+
* like the generated code, where only chain roots are rewritten.
|
|
40
|
+
*/
|
|
41
|
+
function evaluateExpression(source, scope, refs, pipes) {
|
|
42
|
+
return evaluateExpr((0, expr_1.parseExpression)(source), scope, refs, pipes);
|
|
43
|
+
}
|
|
44
|
+
function evaluateExpr(expr, scope, refs, pipes) {
|
|
45
|
+
switch (expr.type) {
|
|
46
|
+
case 'literal':
|
|
47
|
+
return expr.value;
|
|
48
|
+
case 'identifier':
|
|
49
|
+
return scope[expr.name];
|
|
50
|
+
case 'member': {
|
|
51
|
+
if (expr.object.type === 'identifier' && refs
|
|
52
|
+
&& Object.prototype.hasOwnProperty.call(refs, expr.object.name)) {
|
|
53
|
+
const base = refs[expr.object.name];
|
|
54
|
+
if (base === null || base === undefined)
|
|
55
|
+
return undefined;
|
|
56
|
+
return base[expr.property];
|
|
57
|
+
}
|
|
58
|
+
const obj = evaluateExpr(expr.object, scope, refs, pipes);
|
|
59
|
+
if (obj === null || obj === undefined)
|
|
60
|
+
return undefined;
|
|
61
|
+
return obj[expr.property];
|
|
62
|
+
}
|
|
63
|
+
case 'index': {
|
|
64
|
+
const obj = evaluateExpr(expr.object, scope, refs, pipes);
|
|
65
|
+
if (obj === null || obj === undefined)
|
|
66
|
+
return undefined;
|
|
67
|
+
const key = evaluateExpr(expr.index, scope, refs, pipes);
|
|
68
|
+
return obj[key];
|
|
69
|
+
}
|
|
70
|
+
case 'call': {
|
|
71
|
+
const args = expr.args.map((a) => evaluateExpr(a, scope, refs, pipes));
|
|
72
|
+
if (expr.callee.type === 'member') {
|
|
73
|
+
const obj = evaluateExpr(expr.callee.object, scope, refs, pipes);
|
|
74
|
+
if (obj === null || obj === undefined)
|
|
75
|
+
return undefined;
|
|
76
|
+
const fn = obj[expr.callee.property];
|
|
77
|
+
if (typeof fn !== 'function') {
|
|
78
|
+
throw new Error(`Expression error: '${expr.callee.property}' is not a function`);
|
|
79
|
+
}
|
|
80
|
+
return fn.apply(obj, args);
|
|
81
|
+
}
|
|
82
|
+
const fn = evaluateExpr(expr.callee, scope, refs, pipes);
|
|
83
|
+
if (typeof fn !== 'function') {
|
|
84
|
+
throw new Error('Expression error: attempted to call a non-function value');
|
|
85
|
+
}
|
|
86
|
+
return fn(...args);
|
|
87
|
+
}
|
|
88
|
+
case 'unary': {
|
|
89
|
+
const v = evaluateExpr(expr.argument, scope, refs, pipes);
|
|
90
|
+
if (expr.op === '!')
|
|
91
|
+
return !v;
|
|
92
|
+
if (expr.op === '-')
|
|
93
|
+
return -v;
|
|
94
|
+
if (expr.op === '+')
|
|
95
|
+
return +v;
|
|
96
|
+
throw new Error(`Expression error: unsupported unary operator '${expr.op}'`);
|
|
97
|
+
}
|
|
98
|
+
case 'binary':
|
|
99
|
+
return evaluateBinary(expr.op, expr.left, expr.right, scope, refs, pipes);
|
|
100
|
+
case 'conditional':
|
|
101
|
+
return evaluateExpr(expr.test, scope, refs, pipes)
|
|
102
|
+
? evaluateExpr(expr.consequent, scope, refs, pipes)
|
|
103
|
+
: evaluateExpr(expr.alternate, scope, refs, pipes);
|
|
104
|
+
case 'array':
|
|
105
|
+
return expr.elements.map((el) => evaluateExpr(el, scope, refs, pipes));
|
|
106
|
+
case 'pipe': {
|
|
107
|
+
const value = evaluateExpr(expr.input, scope, refs, pipes);
|
|
108
|
+
const args = expr.args.map((a) => evaluateExpr(a, scope, refs, pipes));
|
|
109
|
+
return applyPipe(expr.name, value, args, pipes);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function evaluateBinary(op, left, right, scope, refs, pipes) {
|
|
114
|
+
// Short-circuiting operators return operand values, like JavaScript.
|
|
115
|
+
if (op === '&&') {
|
|
116
|
+
const l = evaluateExpr(left, scope, refs, pipes);
|
|
117
|
+
return l ? evaluateExpr(right, scope, refs, pipes) : l;
|
|
118
|
+
}
|
|
119
|
+
if (op === '||') {
|
|
120
|
+
const l = evaluateExpr(left, scope, refs, pipes);
|
|
121
|
+
return l ? l : evaluateExpr(right, scope, refs, pipes);
|
|
122
|
+
}
|
|
123
|
+
const l = evaluateExpr(left, scope, refs, pipes);
|
|
124
|
+
const r = evaluateExpr(right, scope, refs, pipes);
|
|
125
|
+
switch (op) {
|
|
126
|
+
case '+': return l + r;
|
|
127
|
+
case '-': return l - r;
|
|
128
|
+
case '*': return l * r;
|
|
129
|
+
case '/': return l / r;
|
|
130
|
+
case '%': return l % r;
|
|
131
|
+
// eslint-disable-next-line eqeqeq -- Angular templates use JS loose equality for ==
|
|
132
|
+
case '==': return l == r; // NOSONAR
|
|
133
|
+
case '===': return l === r;
|
|
134
|
+
// eslint-disable-next-line eqeqeq -- see above
|
|
135
|
+
case '!=': return l != r; // NOSONAR
|
|
136
|
+
case '!==': return l !== r;
|
|
137
|
+
case '<': return l < r;
|
|
138
|
+
case '>': return l > r;
|
|
139
|
+
case '<=': return l <= r;
|
|
140
|
+
case '>=': return l >= r;
|
|
141
|
+
default: throw new Error(`Expression error: unsupported binary operator '${op}'`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function applyPipe(name, value, args = [], pipes) {
|
|
145
|
+
if (pipes && Object.prototype.hasOwnProperty.call(pipes, name)) {
|
|
146
|
+
return pipes[name](value, ...args);
|
|
147
|
+
}
|
|
148
|
+
if ((0, pipes_1.isBuiltinPipe)(name))
|
|
149
|
+
return (0, pipes_1.applyBuiltinPipe)(name, value, args);
|
|
150
|
+
throw new Error(`Unknown pipe '${name}'`);
|
|
151
|
+
}
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Pratt parser for template expressions: `{{ a + b * c }}`, `user.name`,
|
|
4
|
+
* `save($event)`, `cond ? a : b`, `items | async`, `name | uppercase:'x'`.
|
|
5
|
+
*
|
|
6
|
+
* Precedence (lowest to highest): pipe | ternary ?: | || | && |
|
|
7
|
+
* equality (== === != !==) | relational (< > <= >=) | additive (+ -) |
|
|
8
|
+
* multiplicative (* / %) | unary (! -) | call/member | primary.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.parseExpression = parseExpression;
|
|
12
|
+
exports.exprToString = exprToString;
|
|
13
|
+
exports.exprToCode = exprToCode;
|
|
14
|
+
exports.exprAstToCode = exprAstToCode;
|
|
15
|
+
const MULTI_OPS = ['===', '!==', '==', '!=', '<=', '>=', '&&', '||'];
|
|
16
|
+
const SINGLE_PUNCT = '+-*/%<>!?:.,()[]|';
|
|
17
|
+
function tokenize(input) {
|
|
18
|
+
const tokens = [];
|
|
19
|
+
let i = 0;
|
|
20
|
+
while (i < input.length) {
|
|
21
|
+
const ch = input[i];
|
|
22
|
+
if (/\s/.test(ch)) {
|
|
23
|
+
i++;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (ch === '"' || ch === "'") {
|
|
27
|
+
let j = i + 1;
|
|
28
|
+
let value = '';
|
|
29
|
+
while (j < input.length && input[j] !== ch) {
|
|
30
|
+
if (input[j] === '\\' && j + 1 < input.length) {
|
|
31
|
+
value += input[j + 1];
|
|
32
|
+
j += 2;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
value += input[j];
|
|
36
|
+
j++;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
tokens.push({ kind: 'string', value });
|
|
40
|
+
i = j + 1;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (/[0-9]/.test(ch)) {
|
|
44
|
+
let j = i;
|
|
45
|
+
while (j < input.length && /[0-9.]/.test(input[j]))
|
|
46
|
+
j++;
|
|
47
|
+
tokens.push({ kind: 'number', value: input.slice(i, j) });
|
|
48
|
+
i = j;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (/[A-Za-z_$]/.test(ch)) {
|
|
52
|
+
let j = i;
|
|
53
|
+
while (j < input.length && /[A-Za-z0-9_$]/.test(input[j]))
|
|
54
|
+
j++;
|
|
55
|
+
tokens.push({ kind: 'ident', value: input.slice(i, j) });
|
|
56
|
+
i = j;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const three = input.slice(i, i + 3);
|
|
60
|
+
const two = input.slice(i, i + 2);
|
|
61
|
+
if (MULTI_OPS.includes(three)) {
|
|
62
|
+
tokens.push({ kind: 'punct', value: three });
|
|
63
|
+
i += 3;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (MULTI_OPS.includes(two)) {
|
|
67
|
+
tokens.push({ kind: 'punct', value: two });
|
|
68
|
+
i += 2;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (SINGLE_PUNCT.includes(ch)) {
|
|
72
|
+
tokens.push({ kind: 'punct', value: ch });
|
|
73
|
+
i++;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
throw new Error(`Unexpected character '${ch}' in expression "${input}"`);
|
|
77
|
+
}
|
|
78
|
+
tokens.push({ kind: 'eof', value: '' });
|
|
79
|
+
return tokens;
|
|
80
|
+
}
|
|
81
|
+
const BINDING_POWER = {
|
|
82
|
+
'||': 1, '&&': 2,
|
|
83
|
+
'==': 3, '===': 3, '!=': 3, '!==': 3,
|
|
84
|
+
'<': 4, '>': 4, '<=': 4, '>=': 4,
|
|
85
|
+
'+': 5, '-': 5,
|
|
86
|
+
'*': 6, '/': 6, '%': 6,
|
|
87
|
+
};
|
|
88
|
+
class Parser {
|
|
89
|
+
tokens;
|
|
90
|
+
pos = 0;
|
|
91
|
+
constructor(tokens) {
|
|
92
|
+
this.tokens = tokens;
|
|
93
|
+
}
|
|
94
|
+
peek() { return this.tokens[this.pos]; }
|
|
95
|
+
next() { return this.tokens[this.pos++]; }
|
|
96
|
+
atPunct(v) {
|
|
97
|
+
const t = this.peek();
|
|
98
|
+
return t.kind === 'punct' && t.value === v;
|
|
99
|
+
}
|
|
100
|
+
expectPunct(v) {
|
|
101
|
+
if (!this.atPunct(v))
|
|
102
|
+
throw new Error(`Expected '${v}' but found '${this.peek().value || 'end of expression'}'`);
|
|
103
|
+
this.next();
|
|
104
|
+
}
|
|
105
|
+
/** Entry point: pipe is the lowest-precedence construct. */
|
|
106
|
+
parsePipe() {
|
|
107
|
+
let input = this.parseConditional();
|
|
108
|
+
while (this.atPunct('|')) {
|
|
109
|
+
this.next();
|
|
110
|
+
const nameTok = this.next();
|
|
111
|
+
if (nameTok.kind !== 'ident')
|
|
112
|
+
throw new Error(`Expected a pipe name after '|'`);
|
|
113
|
+
const args = [];
|
|
114
|
+
while (this.atPunct(':')) {
|
|
115
|
+
this.next();
|
|
116
|
+
args.push(this.parseConditional());
|
|
117
|
+
}
|
|
118
|
+
input = { type: 'pipe', input, name: nameTok.value, args };
|
|
119
|
+
}
|
|
120
|
+
return input;
|
|
121
|
+
}
|
|
122
|
+
parseConditional() {
|
|
123
|
+
const test = this.parseBinary(1);
|
|
124
|
+
if (this.atPunct('?')) {
|
|
125
|
+
this.next();
|
|
126
|
+
const consequent = this.parsePipe();
|
|
127
|
+
this.expectPunct(':');
|
|
128
|
+
const alternate = this.parseConditional();
|
|
129
|
+
return { type: 'conditional', test, consequent, alternate };
|
|
130
|
+
}
|
|
131
|
+
return test;
|
|
132
|
+
}
|
|
133
|
+
parseBinary(minPower) {
|
|
134
|
+
let left = this.parseUnary();
|
|
135
|
+
for (;;) {
|
|
136
|
+
const t = this.peek();
|
|
137
|
+
if (t.kind !== 'punct')
|
|
138
|
+
break;
|
|
139
|
+
const power = BINDING_POWER[t.value];
|
|
140
|
+
if (power === undefined || power < minPower)
|
|
141
|
+
break;
|
|
142
|
+
const op = this.next().value;
|
|
143
|
+
const right = this.parseBinary(power + 1); // left-associative
|
|
144
|
+
left = { type: 'binary', op, left, right };
|
|
145
|
+
}
|
|
146
|
+
return left;
|
|
147
|
+
}
|
|
148
|
+
parseUnary() {
|
|
149
|
+
const t = this.peek();
|
|
150
|
+
if (t.kind === 'punct' && (t.value === '!' || t.value === '-')) {
|
|
151
|
+
const op = this.next().value;
|
|
152
|
+
return { type: 'unary', op, argument: this.parseUnary() };
|
|
153
|
+
}
|
|
154
|
+
return this.parsePostfix();
|
|
155
|
+
}
|
|
156
|
+
parsePostfix() {
|
|
157
|
+
let expr = this.parsePrimary();
|
|
158
|
+
for (;;) {
|
|
159
|
+
if (this.atPunct('.')) {
|
|
160
|
+
this.next();
|
|
161
|
+
const prop = this.next();
|
|
162
|
+
if (prop.kind !== 'ident')
|
|
163
|
+
throw new Error(`Expected a property name after '.'`);
|
|
164
|
+
expr = { type: 'member', object: expr, property: prop.value };
|
|
165
|
+
}
|
|
166
|
+
else if (this.atPunct('[')) {
|
|
167
|
+
this.next();
|
|
168
|
+
const index = this.parsePipe();
|
|
169
|
+
this.expectPunct(']');
|
|
170
|
+
expr = { type: 'index', object: expr, index };
|
|
171
|
+
}
|
|
172
|
+
else if (this.atPunct('(')) {
|
|
173
|
+
this.next();
|
|
174
|
+
const args = [];
|
|
175
|
+
if (!this.atPunct(')')) {
|
|
176
|
+
args.push(this.parsePipe());
|
|
177
|
+
while (this.atPunct(',')) {
|
|
178
|
+
this.next();
|
|
179
|
+
args.push(this.parsePipe());
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
this.expectPunct(')');
|
|
183
|
+
expr = { type: 'call', callee: expr, args };
|
|
184
|
+
}
|
|
185
|
+
else
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
return expr;
|
|
189
|
+
}
|
|
190
|
+
parsePrimary() {
|
|
191
|
+
const t = this.next();
|
|
192
|
+
if (t.kind === 'ident') {
|
|
193
|
+
if (t.value === 'true')
|
|
194
|
+
return { type: 'literal', value: true, raw: 'true' };
|
|
195
|
+
if (t.value === 'false')
|
|
196
|
+
return { type: 'literal', value: false, raw: 'false' };
|
|
197
|
+
if (t.value === 'null')
|
|
198
|
+
return { type: 'literal', value: null, raw: 'null' };
|
|
199
|
+
return { type: 'identifier', name: t.value };
|
|
200
|
+
}
|
|
201
|
+
if (t.kind === 'number')
|
|
202
|
+
return { type: 'literal', value: Number(t.value), raw: t.value };
|
|
203
|
+
if (t.kind === 'string')
|
|
204
|
+
return { type: 'literal', value: t.value, raw: `'${t.value}'` };
|
|
205
|
+
if (t.kind === 'punct' && t.value === '(') {
|
|
206
|
+
const inner = this.parsePipe();
|
|
207
|
+
this.expectPunct(')');
|
|
208
|
+
return inner;
|
|
209
|
+
}
|
|
210
|
+
if (t.kind === 'punct' && t.value === '[') {
|
|
211
|
+
// Array literal (Stage 9): the [routerLink]="['/mail', m.id]" form.
|
|
212
|
+
// A '[' in postfix position is an index access instead; parsePostfix
|
|
213
|
+
// handles that, this branch only runs where a primary is expected.
|
|
214
|
+
// A trailing comma is tolerated, as in TypeScript.
|
|
215
|
+
const elements = [];
|
|
216
|
+
if (!this.atPunct(']')) {
|
|
217
|
+
elements.push(this.parsePipe());
|
|
218
|
+
while (this.atPunct(',')) {
|
|
219
|
+
this.next();
|
|
220
|
+
if (this.atPunct(']'))
|
|
221
|
+
break;
|
|
222
|
+
elements.push(this.parsePipe());
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
this.expectPunct(']');
|
|
226
|
+
return { type: 'array', elements };
|
|
227
|
+
}
|
|
228
|
+
throw new Error(`Unexpected token '${t.value || 'end of expression'}'`);
|
|
229
|
+
}
|
|
230
|
+
expectEnd() {
|
|
231
|
+
if (this.peek().kind !== 'eof')
|
|
232
|
+
throw new Error(`Unexpected trailing token '${this.peek().value}'`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
function parseExpression(source) {
|
|
236
|
+
const parser = new Parser(tokenize(source.trim()));
|
|
237
|
+
const expr = parser.parsePipe();
|
|
238
|
+
parser.expectEnd();
|
|
239
|
+
return expr;
|
|
240
|
+
}
|
|
241
|
+
/** Canonical, fully-parenthesised string form, used by tests and codegen. */
|
|
242
|
+
function exprToString(expr) {
|
|
243
|
+
switch (expr.type) {
|
|
244
|
+
case 'identifier': return expr.name;
|
|
245
|
+
case 'literal': return expr.raw;
|
|
246
|
+
case 'member': return `${exprToString(expr.object)}.${expr.property}`;
|
|
247
|
+
case 'index': return `${exprToString(expr.object)}[${exprToString(expr.index)}]`;
|
|
248
|
+
case 'call': return `${exprToString(expr.callee)}(${expr.args.map(exprToString).join(', ')})`;
|
|
249
|
+
case 'unary': return `${expr.op}${exprToString(expr.argument)}`;
|
|
250
|
+
case 'binary': return `(${exprToString(expr.left)} ${expr.op} ${exprToString(expr.right)})`;
|
|
251
|
+
case 'conditional':
|
|
252
|
+
return `(${exprToString(expr.test)} ? ${exprToString(expr.consequent)} : ${exprToString(expr.alternate)})`;
|
|
253
|
+
case 'array':
|
|
254
|
+
return `[${expr.elements.map(exprToString).join(', ')}]`;
|
|
255
|
+
case 'pipe': {
|
|
256
|
+
const args = expr.args.length ? `:${expr.args.map(exprToString).join(':')}` : '';
|
|
257
|
+
return `(${exprToString(expr.input)} | ${expr.name}${args})`;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
/** The root identifier of a member/index/call chain, when it has one. */
|
|
262
|
+
function chainRoot(expr) {
|
|
263
|
+
switch (expr.type) {
|
|
264
|
+
case 'identifier': return expr.name;
|
|
265
|
+
case 'member':
|
|
266
|
+
case 'index': return chainRoot(expr.object);
|
|
267
|
+
case 'call': return chainRoot(expr.callee);
|
|
268
|
+
default: return undefined;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/** Rename the root identifier of a chain (see chainRoot) to `to`. */
|
|
272
|
+
function renameChainRoot(expr, to) {
|
|
273
|
+
switch (expr.type) {
|
|
274
|
+
case 'identifier': return { type: 'identifier', name: to };
|
|
275
|
+
case 'member': return { ...expr, object: renameChainRoot(expr.object, to) };
|
|
276
|
+
case 'index': return { ...expr, object: renameChainRoot(expr.object, to) };
|
|
277
|
+
case 'call': return { ...expr, callee: renameChainRoot(expr.callee, to) };
|
|
278
|
+
default: return expr;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Rewrite every member/index/call chain whose root is a template
|
|
283
|
+
* reference name so the chain reads from the generated model object.
|
|
284
|
+
* Sub-expressions (arguments, operands, pipe inputs) are visited too.
|
|
285
|
+
*/
|
|
286
|
+
function withRefRoots(expr, refBases) {
|
|
287
|
+
const visitChildren = (e) => {
|
|
288
|
+
switch (e.type) {
|
|
289
|
+
case 'member': return { ...e, object: visit(e.object) };
|
|
290
|
+
case 'index': return { ...e, object: visit(e.object), index: visit(e.index) };
|
|
291
|
+
case 'call': return { ...e, callee: visit(e.callee), args: e.args.map(visit) };
|
|
292
|
+
case 'unary': return { ...e, argument: visit(e.argument) };
|
|
293
|
+
case 'binary': return { ...e, left: visit(e.left), right: visit(e.right) };
|
|
294
|
+
case 'conditional':
|
|
295
|
+
return { ...e, test: visit(e.test), consequent: visit(e.consequent), alternate: visit(e.alternate) };
|
|
296
|
+
case 'array': return { ...e, elements: e.elements.map(visit) };
|
|
297
|
+
case 'pipe': return { ...e, input: visit(e.input), args: e.args.map(visit) };
|
|
298
|
+
default: return e;
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
const visit = (e) => {
|
|
302
|
+
if (e.type === 'member' || e.type === 'index' || e.type === 'call') {
|
|
303
|
+
const root = chainRoot(e);
|
|
304
|
+
const model = root !== undefined ? refBases.get(root) : undefined;
|
|
305
|
+
if (model !== undefined)
|
|
306
|
+
return visitChildren(renameChainRoot(e, model));
|
|
307
|
+
}
|
|
308
|
+
return visitChildren(e);
|
|
309
|
+
};
|
|
310
|
+
return visit(expr);
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Translate a template expression into generated-code source.
|
|
314
|
+
* Built-in pipes are emitted as a call to the runtime
|
|
315
|
+
* `pipe(name, value, ...args)` helper; custom @Pipe classes (Stage 12)
|
|
316
|
+
* become a transform call on the generated pipe instance, and an
|
|
317
|
+
* `| async` whose input the generator hoisted becomes the hook's
|
|
318
|
+
* state variable.
|
|
319
|
+
*/
|
|
320
|
+
function exprToCode(source, options = {}) {
|
|
321
|
+
try {
|
|
322
|
+
return exprAstToCode(parseExpression(source), options);
|
|
323
|
+
}
|
|
324
|
+
catch {
|
|
325
|
+
return source.trim(); // fall back to the raw text; diagnostics are raised elsewhere
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
/** Translate an already-parsed expression (Stage 12: the pipe scan shares it). */
|
|
329
|
+
function exprAstToCode(exprIn, options = {}) {
|
|
330
|
+
let expr = exprIn;
|
|
331
|
+
if (options.refBases && options.refBases.size)
|
|
332
|
+
expr = withRefRoots(expr, options.refBases);
|
|
333
|
+
return codeOf(expr, options);
|
|
334
|
+
}
|
|
335
|
+
function codeOf(expr, options) {
|
|
336
|
+
const renames = options.renames;
|
|
337
|
+
switch (expr.type) {
|
|
338
|
+
case 'pipe': {
|
|
339
|
+
const inputCode = codeOf(expr.input, options);
|
|
340
|
+
if (expr.name === 'async' && options.asyncVars?.has(inputCode)) {
|
|
341
|
+
return options.asyncVars.get(inputCode);
|
|
342
|
+
}
|
|
343
|
+
const args = expr.args.map((a) => codeOf(a, options)).join(', ');
|
|
344
|
+
const custom = options.customPipes?.get(expr.name);
|
|
345
|
+
if (custom)
|
|
346
|
+
return `${custom}.transform(${inputCode}${args ? `, ${args}` : ''})`;
|
|
347
|
+
return `pipe('${expr.name}', ${inputCode}${args ? `, ${args}` : ''})`;
|
|
348
|
+
}
|
|
349
|
+
case 'binary': return `(${codeOf(expr.left, options)} ${expr.op} ${codeOf(expr.right, options)})`;
|
|
350
|
+
case 'conditional':
|
|
351
|
+
return `(${codeOf(expr.test, options)} ? ${codeOf(expr.consequent, options)} : ${codeOf(expr.alternate, options)})`;
|
|
352
|
+
case 'array': return `[${expr.elements.map((el) => codeOf(el, options)).join(', ')}]`;
|
|
353
|
+
case 'identifier': return renames?.get(expr.name) ?? expr.name;
|
|
354
|
+
case 'member': return `${codeOf(expr.object, options)}.${expr.property}`;
|
|
355
|
+
case 'index': return `${codeOf(expr.object, options)}[${codeOf(expr.index, options)}]`;
|
|
356
|
+
case 'call': return `${codeOf(expr.callee, options)}(${expr.args.map((a) => codeOf(a, options)).join(', ')})`;
|
|
357
|
+
case 'unary': return `${expr.op}${codeOf(expr.argument, options)}`;
|
|
358
|
+
case 'literal': return expr.raw;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ngModelBindingOf = ngModelBindingOf;
|
|
4
|
+
exports.hasNgModelSurface = hasNgModelSurface;
|
|
5
|
+
exports.validatorsOf = validatorsOf;
|
|
6
|
+
exports.plainFieldOf = plainFieldOf;
|
|
7
|
+
/** The ngModel binding of an element, when it has one. */
|
|
8
|
+
function ngModelBindingOf(el) {
|
|
9
|
+
const twoWay = el.twoWayBindings.find((b) => b.name === 'ngModel');
|
|
10
|
+
if (twoWay)
|
|
11
|
+
return { mode: 'twoWay', expression: twoWay.expression };
|
|
12
|
+
const pair = el.propBindings.find((b) => b.name === 'ngModel');
|
|
13
|
+
if (pair)
|
|
14
|
+
return { mode: 'pair', expression: pair.expression };
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
/** True when the element carries any ngModel surface ([(ngModel)], [ngModel], (ngModelChange)). */
|
|
18
|
+
function hasNgModelSurface(el) {
|
|
19
|
+
return !!ngModelBindingOf(el) || el.eventBindings.some((b) => b.name === 'ngModelChange');
|
|
20
|
+
}
|
|
21
|
+
const VALIDATOR_ORDER = ['required', 'minLength', 'maxLength', 'email', 'pattern'];
|
|
22
|
+
/**
|
|
23
|
+
* The validators an <input> declares as attributes, in Angular's style:
|
|
24
|
+
* `required` and `email` are boolean attributes, `minlength`,
|
|
25
|
+
* `maxlength` and `pattern` carry a value (static attribute or binding).
|
|
26
|
+
* `type="email"` is NOT a validator in Angular and is not one here.
|
|
27
|
+
*/
|
|
28
|
+
function validatorsOf(el) {
|
|
29
|
+
const specs = [];
|
|
30
|
+
const argOf = (attr, binding) => {
|
|
31
|
+
const b = el.propBindings.find((x) => x.name === binding);
|
|
32
|
+
if (b)
|
|
33
|
+
return b.expression;
|
|
34
|
+
const raw = el.attrs[attr];
|
|
35
|
+
if (raw === undefined)
|
|
36
|
+
return undefined;
|
|
37
|
+
return binding === 'pattern' ? JSON.stringify(raw) : raw;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* A length validator from a static attribute or a binding. A binding
|
|
41
|
+
* is the author's own expression and passes through; a static
|
|
42
|
+
* attribute is text, so only a plain number may become generated
|
|
43
|
+
* code (Stage 12 re-audit: anything else is argInvalid, dropped).
|
|
44
|
+
*/
|
|
45
|
+
const lengthArgOf = (attr, binding) => {
|
|
46
|
+
const b = el.propBindings.find((x) => x.name === binding);
|
|
47
|
+
if (b)
|
|
48
|
+
return { fn: binding === 'minlength' ? 'minLength' : 'maxLength', argSource: b.expression };
|
|
49
|
+
const raw = el.attrs[attr];
|
|
50
|
+
if (raw === undefined)
|
|
51
|
+
return undefined;
|
|
52
|
+
const fn = binding === 'minlength' ? 'minLength' : 'maxLength';
|
|
53
|
+
return /^\d+$/.test(raw.trim())
|
|
54
|
+
? { fn, argSource: raw.trim() }
|
|
55
|
+
: { fn, argInvalid: true };
|
|
56
|
+
};
|
|
57
|
+
if (el.booleanAttrs.includes('required')
|
|
58
|
+
|| (el.attrs['required'] !== undefined && el.attrs['required'] !== 'false')) {
|
|
59
|
+
specs.push({ fn: 'required' });
|
|
60
|
+
}
|
|
61
|
+
const min = lengthArgOf('minlength', 'minlength');
|
|
62
|
+
if (min)
|
|
63
|
+
specs.push(min);
|
|
64
|
+
const max = lengthArgOf('maxlength', 'maxlength');
|
|
65
|
+
if (max)
|
|
66
|
+
specs.push(max);
|
|
67
|
+
if (el.booleanAttrs.includes('email')
|
|
68
|
+
|| (el.attrs['email'] !== undefined && el.attrs['email'] !== 'false')) {
|
|
69
|
+
specs.push({ fn: 'email' });
|
|
70
|
+
}
|
|
71
|
+
const pattern = argOf('pattern', 'pattern');
|
|
72
|
+
if (pattern !== undefined)
|
|
73
|
+
specs.push({ fn: 'pattern', argSource: pattern });
|
|
74
|
+
return specs.sort((a, b) => VALIDATOR_ORDER.indexOf(a.fn) - VALIDATOR_ORDER.indexOf(b.fn));
|
|
75
|
+
}
|
|
76
|
+
/** The component field an ngModel expression targets, when it is a plain identifier. */
|
|
77
|
+
function plainFieldOf(expression) {
|
|
78
|
+
const trimmed = expression.trim();
|
|
79
|
+
return /^[A-Za-z_$][\w$]*$/.test(trimmed) ? trimmed : undefined;
|
|
80
|
+
}
|