@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,227 @@
|
|
|
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.directiveStyles = directiveStyles;
|
|
37
|
+
const ts = __importStar(require("typescript"));
|
|
38
|
+
const BINARY_OPS = new Set([
|
|
39
|
+
ts.SyntaxKind.PlusToken,
|
|
40
|
+
ts.SyntaxKind.MinusToken,
|
|
41
|
+
ts.SyntaxKind.AsteriskToken,
|
|
42
|
+
ts.SyntaxKind.SlashToken,
|
|
43
|
+
ts.SyntaxKind.PercentToken,
|
|
44
|
+
ts.SyntaxKind.EqualsEqualsToken,
|
|
45
|
+
ts.SyntaxKind.EqualsEqualsEqualsToken,
|
|
46
|
+
ts.SyntaxKind.ExclamationEqualsToken,
|
|
47
|
+
ts.SyntaxKind.ExclamationEqualsEqualsToken,
|
|
48
|
+
ts.SyntaxKind.LessThanToken,
|
|
49
|
+
ts.SyntaxKind.GreaterThanToken,
|
|
50
|
+
ts.SyntaxKind.LessThanEqualsToken,
|
|
51
|
+
ts.SyntaxKind.GreaterThanEqualsToken,
|
|
52
|
+
ts.SyntaxKind.AmpersandAmpersandToken,
|
|
53
|
+
ts.SyntaxKind.BarBarToken,
|
|
54
|
+
]);
|
|
55
|
+
class Extractor {
|
|
56
|
+
info;
|
|
57
|
+
setterParam;
|
|
58
|
+
locals = new Map();
|
|
59
|
+
inputNames;
|
|
60
|
+
constructor(info, setterParam) {
|
|
61
|
+
this.info = info;
|
|
62
|
+
this.setterParam = setterParam;
|
|
63
|
+
this.inputNames = new Set(info.inputs.map((i) => i.name));
|
|
64
|
+
}
|
|
65
|
+
/** Convert one TypeScript expression to a template-expression source. */
|
|
66
|
+
expr(node) {
|
|
67
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
68
|
+
return JSON.stringify(node.text);
|
|
69
|
+
}
|
|
70
|
+
if (ts.isNumericLiteral(node))
|
|
71
|
+
return node.text;
|
|
72
|
+
if (node.kind === ts.SyntaxKind.TrueKeyword)
|
|
73
|
+
return 'true';
|
|
74
|
+
if (node.kind === ts.SyntaxKind.FalseKeyword)
|
|
75
|
+
return 'false';
|
|
76
|
+
if (node.kind === ts.SyntaxKind.NullKeyword)
|
|
77
|
+
return 'null';
|
|
78
|
+
if (ts.isIdentifier(node)) {
|
|
79
|
+
if (this.setterParam && node.text === this.setterParam.name)
|
|
80
|
+
return this.setterParam.input;
|
|
81
|
+
const local = this.locals.get(node.text);
|
|
82
|
+
if (local !== undefined)
|
|
83
|
+
return `(${local})`;
|
|
84
|
+
if (this.inputNames.has(node.text))
|
|
85
|
+
return node.text;
|
|
86
|
+
throw new Error(`'${node.text}' is not an @Input, a local or the setter parameter`);
|
|
87
|
+
}
|
|
88
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
89
|
+
if (node.expression.kind === ts.SyntaxKind.ThisKeyword) {
|
|
90
|
+
if (this.inputNames.has(node.name.text))
|
|
91
|
+
return node.name.text;
|
|
92
|
+
throw new Error(`this.${node.name.text} is not an @Input of the directive`);
|
|
93
|
+
}
|
|
94
|
+
return `${this.expr(node.expression)}.${node.name.text}`;
|
|
95
|
+
}
|
|
96
|
+
if (ts.isElementAccessExpression(node)) {
|
|
97
|
+
return `${this.expr(node.expression)}[${this.expr(node.argumentExpression)}]`;
|
|
98
|
+
}
|
|
99
|
+
if (ts.isCallExpression(node)) {
|
|
100
|
+
const args = node.arguments.map((a) => this.expr(a)).join(', ');
|
|
101
|
+
return `${this.expr(node.expression)}(${args})`;
|
|
102
|
+
}
|
|
103
|
+
if (ts.isBinaryExpression(node)) {
|
|
104
|
+
if (!BINARY_OPS.has(node.operatorToken.kind)) {
|
|
105
|
+
throw new Error(`operator '${node.operatorToken.getText()}' is not supported in a directive`);
|
|
106
|
+
}
|
|
107
|
+
return `(${this.expr(node.left)} ${node.operatorToken.getText()} ${this.expr(node.right)})`;
|
|
108
|
+
}
|
|
109
|
+
if (ts.isConditionalExpression(node)) {
|
|
110
|
+
return `(${this.expr(node.condition)} ? ${this.expr(node.whenTrue)} : ${this.expr(node.whenFalse)})`;
|
|
111
|
+
}
|
|
112
|
+
if (ts.isParenthesizedExpression(node))
|
|
113
|
+
return `(${this.expr(node.expression)})`;
|
|
114
|
+
if (ts.isPrefixUnaryExpression(node)) {
|
|
115
|
+
const op = node.operator === ts.SyntaxKind.ExclamationToken ? '!'
|
|
116
|
+
: node.operator === ts.SyntaxKind.MinusToken ? '-'
|
|
117
|
+
: node.operator === ts.SyntaxKind.PlusToken ? '+' : undefined;
|
|
118
|
+
if (op === undefined)
|
|
119
|
+
throw new Error('an unsupported unary operator in a directive');
|
|
120
|
+
return `${op}${this.expr(node.operand)}`;
|
|
121
|
+
}
|
|
122
|
+
if (ts.isTemplateExpression(node)) {
|
|
123
|
+
const parts = [JSON.stringify(node.head.text)];
|
|
124
|
+
for (const span of node.templateSpans) {
|
|
125
|
+
parts.push(this.expr(span.expression));
|
|
126
|
+
parts.push(JSON.stringify(span.literal.text));
|
|
127
|
+
}
|
|
128
|
+
return `(${parts.join(' + ')})`;
|
|
129
|
+
}
|
|
130
|
+
if (ts.isAsExpression(node) || ts.isNonNullExpression(node))
|
|
131
|
+
return this.expr(node.expression);
|
|
132
|
+
throw new Error(`${ts.SyntaxKind[node.kind]} is not supported in a directive style expression`);
|
|
133
|
+
}
|
|
134
|
+
/** The style property an assignment targets, or undefined when the shape is wrong. */
|
|
135
|
+
stylePropOf(left) {
|
|
136
|
+
// this.<el>.nativeElement.style.<prop>
|
|
137
|
+
if (!ts.isPropertyAccessExpression(left))
|
|
138
|
+
return undefined;
|
|
139
|
+
const prop = left.name.text;
|
|
140
|
+
const styleAccess = left.expression;
|
|
141
|
+
if (!ts.isPropertyAccessExpression(styleAccess) || styleAccess.name.text !== 'style')
|
|
142
|
+
return undefined;
|
|
143
|
+
const nativeAccess = styleAccess.expression;
|
|
144
|
+
if (!ts.isPropertyAccessExpression(nativeAccess) || nativeAccess.name.text !== 'nativeElement')
|
|
145
|
+
return undefined;
|
|
146
|
+
const elAccess = nativeAccess.expression;
|
|
147
|
+
if (!ts.isPropertyAccessExpression(elAccess))
|
|
148
|
+
return undefined;
|
|
149
|
+
if (elAccess.expression.kind !== ts.SyntaxKind.ThisKeyword)
|
|
150
|
+
return undefined;
|
|
151
|
+
if (this.info.elProp !== undefined && elAccess.name.text !== this.info.elProp)
|
|
152
|
+
return undefined;
|
|
153
|
+
return prop;
|
|
154
|
+
}
|
|
155
|
+
run(bodySource) {
|
|
156
|
+
const sf = ts.createSourceFile('directive.ts', `function __d() ${bodySource}`, ts.ScriptTarget.Latest,
|
|
157
|
+
/* setParentNodes */ true);
|
|
158
|
+
let statements;
|
|
159
|
+
const visit = (node) => {
|
|
160
|
+
if (statements)
|
|
161
|
+
return;
|
|
162
|
+
if (ts.isFunctionDeclaration(node) && node.body)
|
|
163
|
+
statements = [...node.body.statements];
|
|
164
|
+
else
|
|
165
|
+
ts.forEachChild(node, visit);
|
|
166
|
+
};
|
|
167
|
+
visit(sf);
|
|
168
|
+
if (!statements)
|
|
169
|
+
return { assignments: [], unsupported: 'the styling body could not be read' };
|
|
170
|
+
const assignments = [];
|
|
171
|
+
try {
|
|
172
|
+
for (const stmt of statements) {
|
|
173
|
+
if (ts.isVariableStatement(stmt)) {
|
|
174
|
+
for (const decl of stmt.declarationList.declarations) {
|
|
175
|
+
if (!ts.isIdentifier(decl.name) || !decl.initializer) {
|
|
176
|
+
return { assignments: [], unsupported: 'a local declaration without a plain name and value' };
|
|
177
|
+
}
|
|
178
|
+
this.locals.set(decl.name.text, this.expr(decl.initializer));
|
|
179
|
+
}
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (ts.isExpressionStatement(stmt) && ts.isBinaryExpression(stmt.expression)
|
|
183
|
+
&& stmt.expression.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
184
|
+
const prop = this.stylePropOf(stmt.expression.left);
|
|
185
|
+
if (prop === undefined) {
|
|
186
|
+
return { assignments: [], unsupported: 'a statement that is not a this.el.nativeElement.style.<prop> assignment' };
|
|
187
|
+
}
|
|
188
|
+
assignments.push({ prop, rhs: this.expr(stmt.expression.right) });
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const snippet = stmt.getText(sf).replace(/\s+/g, ' ').trim();
|
|
192
|
+
return {
|
|
193
|
+
assignments: [],
|
|
194
|
+
unsupported: `the statement \`${snippet.length > 60 ? `${snippet.slice(0, 57)}...` : snippet}\`; only style assignments and local declarations are supported`,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
catch (e) {
|
|
199
|
+
return { assignments: [], unsupported: e instanceof Error ? e.message : 'an unsupported expression' };
|
|
200
|
+
}
|
|
201
|
+
return { assignments };
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Extract the style assignments of a directive. ngOnInit wins when
|
|
206
|
+
* both exist (it is the canonical Angular shape); otherwise the
|
|
207
|
+
* setter for an input carries the logic, with its parameter standing
|
|
208
|
+
* for that input's value.
|
|
209
|
+
*/
|
|
210
|
+
function directiveStyles(info) {
|
|
211
|
+
if (info.attrName === undefined) {
|
|
212
|
+
return {
|
|
213
|
+
assignments: [],
|
|
214
|
+
unsupported: `the selector '${info.selector ?? ''}' is not an attribute selector like [appHighlight]`,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
if (info.initBody)
|
|
218
|
+
return new Extractor(info).run(info.initBody);
|
|
219
|
+
if (info.setter) {
|
|
220
|
+
return new Extractor(info, { name: info.setter.paramName, input: info.setter.prop })
|
|
221
|
+
.run(info.setter.body);
|
|
222
|
+
}
|
|
223
|
+
return {
|
|
224
|
+
assignments: [],
|
|
225
|
+
unsupported: 'no styling logic: the subset reads ngOnInit or a setter for an input',
|
|
226
|
+
};
|
|
227
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Diagnostic } from '../diagnostics';
|
|
2
|
+
/**
|
|
3
|
+
* Routes files (Stage 9): the Angular routing configuration, read with
|
|
4
|
+
* the TypeScript compiler API like everything else in Rangular.
|
|
5
|
+
*
|
|
6
|
+
* Rangular mirrors the modern standalone Angular form: a routes file
|
|
7
|
+
* exporting a `Routes` array,
|
|
8
|
+
*
|
|
9
|
+
* import { Routes } from '@angular/router';
|
|
10
|
+
* export const routes: Routes = [
|
|
11
|
+
* { path: '', redirectTo: 'inbox', pathMatch: 'full' },
|
|
12
|
+
* { path: 'inbox', component: InboxComponent },
|
|
13
|
+
* { path: 'mail/:id', component: MailDetailComponent },
|
|
14
|
+
* { path: '**', component: NotFoundComponent },
|
|
15
|
+
* ];
|
|
16
|
+
*
|
|
17
|
+
* (Historically the same array was handed to RouterModule.forRoot in an
|
|
18
|
+
* NgModule; Rangular has no modules, so the exported array is the whole
|
|
19
|
+
* configuration, the way provideRouter(routes) consumes it in current
|
|
20
|
+
* Angular.)
|
|
21
|
+
*
|
|
22
|
+
* One array is read per file: the first top-level variable whose name
|
|
23
|
+
* ends in "routes" (any case) or whose type annotation mentions
|
|
24
|
+
* `Routes`, with an array literal initializer. Each entry must be an
|
|
25
|
+
* object literal; the supported keys are path, component, redirectTo
|
|
26
|
+
* and pathMatch. Angular features beyond v1 (child routes, guards,
|
|
27
|
+
* resolvers, lazy loading) are named by GEN_ROUTE_UNSUPPORTED errors,
|
|
28
|
+
* never silently dropped. `data` and `title` are accepted and ignored:
|
|
29
|
+
* they are metadata with no runtime effect in this version.
|
|
30
|
+
*/
|
|
31
|
+
export interface RouteDef {
|
|
32
|
+
/** The path pattern as written, e.g. 'mail/:id', '' or '**'. */
|
|
33
|
+
path: string;
|
|
34
|
+
/** The component class name from `component: XComponent`, when present. */
|
|
35
|
+
componentName?: string;
|
|
36
|
+
/** The redirect target from `redirectTo: 'inbox'`, when present. */
|
|
37
|
+
redirectTo?: string;
|
|
38
|
+
/** The pathMatch value as written ('full' is the only behaviour v1 implements). */
|
|
39
|
+
pathMatch?: string;
|
|
40
|
+
/** Any key outside the supported set that was ignored with a warning. */
|
|
41
|
+
ignoredKeys: string[];
|
|
42
|
+
}
|
|
43
|
+
/** A route with its component resolved against the compilation's registry. */
|
|
44
|
+
export interface ResolvedRoute extends RouteDef {
|
|
45
|
+
/** The resolved component's selector, when the class resolved. */
|
|
46
|
+
selector?: string;
|
|
47
|
+
}
|
|
48
|
+
export interface ParseRoutesResult {
|
|
49
|
+
routes: RouteDef[];
|
|
50
|
+
diagnostics: Diagnostic[];
|
|
51
|
+
/** True when a Routes array was found in the file at all. */
|
|
52
|
+
found: boolean;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Read the Routes array of a routes file. Emits diagnostics for
|
|
56
|
+
* unsupported shapes; a file with no Routes array reports found: false
|
|
57
|
+
* and no diagnostics (compile.ts decides what that means).
|
|
58
|
+
*/
|
|
59
|
+
export declare function parseRoutesFile(tsSource: string, fileName?: string): ParseRoutesResult;
|
|
@@ -0,0 +1,163 @@
|
|
|
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.parseRoutesFile = parseRoutesFile;
|
|
37
|
+
const ts = __importStar(require("typescript"));
|
|
38
|
+
const diagnostics_1 = require("../diagnostics");
|
|
39
|
+
/** Route entry keys whose Angular feature is not in Rangular v1. */
|
|
40
|
+
const UNSUPPORTED_KEYS = {
|
|
41
|
+
children: 'child routes (nested outlets)',
|
|
42
|
+
canActivate: 'route guards (canActivate)',
|
|
43
|
+
canActivateChild: 'route guards (canActivateChild)',
|
|
44
|
+
canDeactivate: 'route guards (canDeactivate)',
|
|
45
|
+
canMatch: 'route guards (canMatch)',
|
|
46
|
+
resolve: 'resolvers (resolve)',
|
|
47
|
+
loadComponent: 'lazy loading (loadComponent)',
|
|
48
|
+
loadChildren: 'lazy loading (loadChildren)',
|
|
49
|
+
};
|
|
50
|
+
const KNOWN_KEYS = new Set(['path', 'component', 'redirectTo', 'pathMatch', 'data', 'title']);
|
|
51
|
+
const FIX_UNSUPPORTED = 'Remove the unsupported key for now; the feature is on the roadmap in docs/ROADMAP.md (Stage 9 supports flat routes with components and full-path redirects only)';
|
|
52
|
+
const FIX_SHAPE = "Write each entry as an object literal: { path: 'mail/:id', component: MailDetailComponent } or { path: '', redirectTo: 'inbox', pathMatch: 'full' }";
|
|
53
|
+
function stringOf(node) {
|
|
54
|
+
if (!node)
|
|
55
|
+
return undefined;
|
|
56
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
57
|
+
return node.text;
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
/** Unwrap `[...] as Routes` / `<Routes>[...]` to the array literal. */
|
|
61
|
+
function arrayOf(node) {
|
|
62
|
+
if (ts.isArrayLiteralExpression(node))
|
|
63
|
+
return node;
|
|
64
|
+
if (ts.isAsExpression(node) || ts.isTypeAssertionExpression(node))
|
|
65
|
+
return arrayOf(node.expression);
|
|
66
|
+
if (ts.isParenthesizedExpression(node))
|
|
67
|
+
return arrayOf(node.expression);
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
function readEntry(node, diagnostics) {
|
|
71
|
+
if (!ts.isObjectLiteralExpression(node)) {
|
|
72
|
+
diagnostics.push((0, diagnostics_1.error)('GEN_ROUTE_UNSUPPORTED', 'A route entry is not an object literal; route entries must be written out in full', undefined, undefined, FIX_SHAPE));
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
let path;
|
|
76
|
+
let componentName;
|
|
77
|
+
let redirectTo;
|
|
78
|
+
let pathMatch;
|
|
79
|
+
const ignoredKeys = [];
|
|
80
|
+
for (const prop of node.properties) {
|
|
81
|
+
if (ts.isSpreadAssignment(prop)) {
|
|
82
|
+
diagnostics.push((0, diagnostics_1.error)('GEN_ROUTE_UNSUPPORTED', 'A route entry uses a spread; spread entries cannot be read statically', undefined, undefined, FIX_SHAPE));
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (!ts.isPropertyAssignment(prop)) {
|
|
86
|
+
diagnostics.push((0, diagnostics_1.error)('GEN_ROUTE_UNSUPPORTED', 'A route entry uses a shorthand or computed property; entries must be plain key: value pairs', undefined, undefined, FIX_SHAPE));
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const key = prop.name.getText().replace(/^['"]|['"]$/g, '');
|
|
90
|
+
const unsupported = UNSUPPORTED_KEYS[key];
|
|
91
|
+
if (unsupported) {
|
|
92
|
+
diagnostics.push((0, diagnostics_1.error)('GEN_ROUTE_UNSUPPORTED', `Route entry uses '${key}', which is ${unsupported}; Rangular routing does not support it yet`, undefined, undefined, FIX_UNSUPPORTED));
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (!KNOWN_KEYS.has(key)) {
|
|
96
|
+
ignoredKeys.push(key);
|
|
97
|
+
diagnostics.push((0, diagnostics_1.warning)('GEN_ROUTE_UNSUPPORTED', `Route entry key '${key}' is not a supported route key and was ignored`, undefined, undefined, "Supported keys are path, component, redirectTo and pathMatch (data and title are accepted and ignored); remove the key or see docs/ROADMAP.md for what is planned"));
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (key === 'path')
|
|
101
|
+
path = stringOf(prop.initializer);
|
|
102
|
+
else if (key === 'redirectTo')
|
|
103
|
+
redirectTo = stringOf(prop.initializer);
|
|
104
|
+
else if (key === 'pathMatch')
|
|
105
|
+
pathMatch = stringOf(prop.initializer);
|
|
106
|
+
else if (key === 'component') {
|
|
107
|
+
componentName = ts.isIdentifier(prop.initializer) ? prop.initializer.text : undefined;
|
|
108
|
+
if (componentName === undefined) {
|
|
109
|
+
diagnostics.push((0, diagnostics_1.error)('GEN_ROUTE_UNSUPPORTED', 'A route\'s component is not a plain class reference; lazy or computed components are not supported', undefined, undefined, 'Name the component class directly and import it in the routes file: { path: \'inbox\', component: InboxComponent }'));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// data / title: accepted, no runtime effect in this version; they
|
|
113
|
+
// are recorded in ignoredKeys so nothing is silently dropped.
|
|
114
|
+
else if (key === 'data' || key === 'title')
|
|
115
|
+
ignoredKeys.push(key);
|
|
116
|
+
}
|
|
117
|
+
if (path === undefined) {
|
|
118
|
+
diagnostics.push((0, diagnostics_1.error)('GEN_ROUTE_UNSUPPORTED', 'A route entry has no string \'path\'; every entry needs one', undefined, undefined, FIX_SHAPE));
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
if (componentName !== undefined && redirectTo !== undefined) {
|
|
122
|
+
diagnostics.push((0, diagnostics_1.error)('GEN_ROUTE_UNSUPPORTED', `Route '${path}' declares both a component and a redirectTo; Angular forbids the combination and so does Rangular`, undefined, undefined, 'Keep one: a component route renders, a redirect route forwards. Split them into two entries if both paths are needed'));
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
if (componentName === undefined && redirectTo === undefined) {
|
|
126
|
+
diagnostics.push((0, diagnostics_1.error)('GEN_ROUTE_UNSUPPORTED', `Route '${path}' declares neither a component nor a redirectTo; there is nothing for it to do`, undefined, undefined, FIX_SHAPE));
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
return { path, componentName, redirectTo, pathMatch, ignoredKeys };
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Read the Routes array of a routes file. Emits diagnostics for
|
|
133
|
+
* unsupported shapes; a file with no Routes array reports found: false
|
|
134
|
+
* and no diagnostics (compile.ts decides what that means).
|
|
135
|
+
*/
|
|
136
|
+
function parseRoutesFile(tsSource, fileName = 'app.routes.ts') {
|
|
137
|
+
const diagnostics = [];
|
|
138
|
+
const sf = ts.createSourceFile(fileName, tsSource, ts.ScriptTarget.Latest, /* setParentNodes */ true);
|
|
139
|
+
let array;
|
|
140
|
+
const visit = (node) => {
|
|
141
|
+
if (array)
|
|
142
|
+
return;
|
|
143
|
+
if (ts.isVariableDeclaration(node) && node.initializer && ts.isIdentifier(node.name)) {
|
|
144
|
+
const name = node.name.text;
|
|
145
|
+
const typeText = node.type ? node.type.getText(sf) : '';
|
|
146
|
+
if (/routes$/i.test(name) || /\bRoutes\b/.test(typeText)) {
|
|
147
|
+
array = arrayOf(node.initializer);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (!array)
|
|
151
|
+
ts.forEachChild(node, visit);
|
|
152
|
+
};
|
|
153
|
+
visit(sf);
|
|
154
|
+
if (!array)
|
|
155
|
+
return { routes: [], diagnostics, found: false };
|
|
156
|
+
const routes = [];
|
|
157
|
+
for (const element of array.elements) {
|
|
158
|
+
const def = readEntry(element, diagnostics);
|
|
159
|
+
if (def)
|
|
160
|
+
routes.push(def);
|
|
161
|
+
}
|
|
162
|
+
return { routes, diagnostics, found: true };
|
|
163
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { CarriedImportBinding, ComponentInfo, DirectiveInfo, PipeInfo, ServiceInfo } from '../class/parseComponent';
|
|
2
|
+
import { ResolvedRoute } from '../class/parseRoutes';
|
|
3
|
+
import { Diagnostic } from '../diagnostics';
|
|
4
|
+
import { ParsedCss } from '../style/css';
|
|
5
|
+
import { TemplateRoot } from '../template/ast';
|
|
6
|
+
import { MethodHost } from './methods';
|
|
7
|
+
import { SourceMapV3 } from './sourceMap';
|
|
8
|
+
export interface GenerateResult {
|
|
9
|
+
code: string;
|
|
10
|
+
diagnostics: Diagnostic[];
|
|
11
|
+
/** Present only when the caller asked for a source map (Stage 5). */
|
|
12
|
+
sourceMap?: SourceMapV3;
|
|
13
|
+
}
|
|
14
|
+
/** A child component taking part in this compilation (Stage 3, M5). */
|
|
15
|
+
export interface ChildUnitInput {
|
|
16
|
+
info: ComponentInfo;
|
|
17
|
+
ast: TemplateRoot;
|
|
18
|
+
css: ParsedCss;
|
|
19
|
+
/** Source file keys for this child (Stage 5, source maps and diagnostics). */
|
|
20
|
+
fileName?: string;
|
|
21
|
+
templateFile?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface GenerateContext {
|
|
24
|
+
/** Every other @Component class resolved through imports, keyed by selector in codegen. */
|
|
25
|
+
children?: ChildUnitInput[];
|
|
26
|
+
/** Every @Injectable class resolved through imports (Stage 3, M6). */
|
|
27
|
+
services?: ServiceInfo[];
|
|
28
|
+
/** Every custom @Pipe class resolved through imports (Stage 12). */
|
|
29
|
+
pipes?: PipeInfo[];
|
|
30
|
+
/** Every attribute @Directive class resolved through imports (Stage 12). */
|
|
31
|
+
directives?: DirectiveInfo[];
|
|
32
|
+
/**
|
|
33
|
+
* The resolved route table (Stage 9), present when a routes file was
|
|
34
|
+
* resolved for this compilation. When routing is also USED (an outlet,
|
|
35
|
+
* a routerLink, or a Router/ActivatedRoute injection), codegen emits
|
|
36
|
+
* the module-level navigator and router and wires the outlet.
|
|
37
|
+
*/
|
|
38
|
+
routes?: ResolvedRoute[];
|
|
39
|
+
/**
|
|
40
|
+
* Literal module constants carried from classless files (0.6.5):
|
|
41
|
+
* compile.ts collects the exported literal consts that contributing
|
|
42
|
+
* classes import and use as values (the environment.ts shape), and
|
|
43
|
+
* codegen emits them verbatim at module level, before the services.
|
|
44
|
+
*/
|
|
45
|
+
consts?: {
|
|
46
|
+
name: string;
|
|
47
|
+
text: string;
|
|
48
|
+
}[];
|
|
49
|
+
/**
|
|
50
|
+
* Third-party imports carried from the contributing source files
|
|
51
|
+
* (the entry component's file, the child component files, the
|
|
52
|
+
* providedIn: 'root' service files, the pipe files): compile.ts
|
|
53
|
+
* merges their bare, non-framework imports across files and passes
|
|
54
|
+
* the surviving bindings. Codegen emits them with its own import
|
|
55
|
+
* lines, dropping any binding whose local name the generated module
|
|
56
|
+
* already defines, with a GEN_IMPORT_COLLISION warning.
|
|
57
|
+
*/
|
|
58
|
+
externalImports?: {
|
|
59
|
+
bindings: CarriedImportBinding[];
|
|
60
|
+
sideEffects: {
|
|
61
|
+
from: string;
|
|
62
|
+
file: string;
|
|
63
|
+
}[];
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Source map request (Stage 5): when present, codegen records block
|
|
67
|
+
* level segments (component function starts and template element
|
|
68
|
+
* starts) and the result carries a source map v3 object.
|
|
69
|
+
*/
|
|
70
|
+
sourceMap?: {
|
|
71
|
+
entryTsFile: string;
|
|
72
|
+
entryTemplateFile: string;
|
|
73
|
+
contents: Map<string, string>;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
export interface TagMapping {
|
|
77
|
+
component: string;
|
|
78
|
+
/** true when the mapping comes from @rangular-framework/ui rather than react-native */
|
|
79
|
+
fromUi: boolean;
|
|
80
|
+
/** Text variant for heading/paragraph mappings */
|
|
81
|
+
variant?: string;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The tag -> generated-component contract. Single source of truth:
|
|
85
|
+
* codegen renders from this table and the catalog is generated from it.
|
|
86
|
+
*/
|
|
87
|
+
export declare const TAG_MAP: Record<string, TagMapping>;
|
|
88
|
+
/** True when an initializer source text is a plain literal we may hoist. */
|
|
89
|
+
export declare function isLiteralInitializer(init: string): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Fields the generated component declares by emitting their initializer
|
|
92
|
+
* verbatim as a plain const (proof fix): a field whose initializer is
|
|
93
|
+
* not a literal and not a reactive form instance, which no method
|
|
94
|
+
* assigns (mutation calls included), and whose initializer does not
|
|
95
|
+
* reference `this` (a generated function component has no `this` to
|
|
96
|
+
* resolve). The classic case is `today = new Date()` read by a date
|
|
97
|
+
* pipe: before this fix it warned GEN_FIELD, emitted only a TODO
|
|
98
|
+
* comment, and the generated code referenced a name it never declared.
|
|
99
|
+
* The preview rebuilds exactly these fields from their initializer
|
|
100
|
+
* source, so the two sides cannot drift.
|
|
101
|
+
*/
|
|
102
|
+
export declare function verbatimInitFields(component: MethodHost & {
|
|
103
|
+
constructorDeps?: {
|
|
104
|
+
propertyName: string;
|
|
105
|
+
typeName: string;
|
|
106
|
+
}[];
|
|
107
|
+
}): {
|
|
108
|
+
name: string;
|
|
109
|
+
initializer: string;
|
|
110
|
+
}[];
|
|
111
|
+
/** StyleSheet keys must be valid JS identifiers: `card-note` -> `cardNote`. */
|
|
112
|
+
export declare function styleKeyName(key: string): string;
|
|
113
|
+
export declare function componentFunctionName(className: string): string;
|
|
114
|
+
/** @Output field name -> generated callback prop name: `saved` -> `onSaved`. */
|
|
115
|
+
export declare function outputPropName(fieldName: string): string;
|
|
116
|
+
/** Template (event) name -> React Native prop name, for known events. */
|
|
117
|
+
export declare const EVENT_PROPS: Record<string, string>;
|
|
118
|
+
/** Map a template (event) to its React Native prop name. */
|
|
119
|
+
export declare function eventPropName(eventName: string, tag: string): {
|
|
120
|
+
prop: string;
|
|
121
|
+
known: boolean;
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* True when an event binding's expression is an assignment statement,
|
|
125
|
+
* e.g. `(close)="sheetOpen = false"` or `(click)="count += 1"`
|
|
126
|
+
* (Stage 10). Evidence from the code: the template expression grammar
|
|
127
|
+
* (template/expr.ts) has no assignment operator at all, its tokenizer
|
|
128
|
+
* does not even produce a bare `=` token, so such a binding cannot be
|
|
129
|
+
* translated; before Stage 10 codegen silently fell back to the raw
|
|
130
|
+
* source, emitting `onClose={() => sheetOpen = false}` against a
|
|
131
|
+
* const state binding, which compiles and then throws at runtime.
|
|
132
|
+
* Assignment bindings are therefore detected explicitly and dropped
|
|
133
|
+
* with a GEN_EVENT_ASSIGNMENT warning: authors write a method
|
|
134
|
+
* (`closeSheet() { this.sheetOpen = false; }`) and bind that instead,
|
|
135
|
+
* the pattern the Stage 6 controls already document.
|
|
136
|
+
*/
|
|
137
|
+
export declare function isAssignmentExpression(source: string): boolean;
|
|
138
|
+
/**
|
|
139
|
+
* The class name a [class.name] toggle binding names, or undefined
|
|
140
|
+
* when the binding name is not a class toggle. Class names follow the
|
|
141
|
+
* CSS subset's class selector grammar (a plain identifier, hyphens
|
|
142
|
+
* allowed), the same shape parseCss accepts for a `.class` key.
|
|
143
|
+
*/
|
|
144
|
+
export declare function classToggleOf(bindingName: string): string | undefined;
|
|
145
|
+
/**
|
|
146
|
+
* True for every binding name that carries class, style or HTML
|
|
147
|
+
* attribute intent: [class], [class.name], [ngClass], [style],
|
|
148
|
+
* [style.prop] (with or without a unit suffix), [ngStyle] and
|
|
149
|
+
* [attr.x]. None of these is a prop of any generated component:
|
|
150
|
+
* [class.name] over a class declared in the component styles is wired
|
|
151
|
+
* through the element's style (see styleFor), and every other form is
|
|
152
|
+
* diagnosed and dropped by the binding scan in generateComponent.
|
|
153
|
+
* Before the gap fix the generic prop loops emitted them verbatim
|
|
154
|
+
* (class.hot={hot}, style.color={colour}, attr.class={...}), props
|
|
155
|
+
* the primitives ignore, so the styling silently never appeared; a
|
|
156
|
+
* [style] object or a static style attribute could even collide with
|
|
157
|
+
* the generated style prop and produce a duplicate-prop JSX error.
|
|
158
|
+
*/
|
|
159
|
+
export declare function isClassStyleBindingName(bindingName: string): boolean;
|
|
160
|
+
/**
|
|
161
|
+
* Generate a React Native .tsx from a parsed component, its template AST
|
|
162
|
+
* and its parsed styles. Stage 3: the optional context carries the child
|
|
163
|
+
* components and services resolved through the entry's imports, everything
|
|
164
|
+
* is emitted into the same file (see the Generator note above).
|
|
165
|
+
*/
|
|
166
|
+
export declare function generateComponent(component: ComponentInfo, ast: TemplateRoot, css: ParsedCss, context?: GenerateContext): GenerateResult;
|