@rangular-framework/cli 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/src/cli.js +1217 -0
- package/dist/compiler/src/catalog.js +198 -0
- package/dist/compiler/src/class/formInit.js +370 -0
- package/dist/compiler/src/class/parseComponent.js +430 -0
- package/dist/compiler/src/class/parseDirective.js +227 -0
- package/dist/compiler/src/class/parseRoutes.js +163 -0
- package/dist/compiler/src/codegen/generate.js +3466 -0
- package/dist/compiler/src/codegen/methods.js +975 -0
- package/dist/compiler/src/codegen/sourceMap.js +77 -0
- package/dist/compiler/src/compile.js +775 -0
- package/dist/compiler/src/diagnostics.js +113 -0
- package/dist/compiler/src/doctor.js +144 -0
- package/dist/compiler/src/fix.js +44 -0
- package/dist/compiler/src/index.js +38 -0
- package/dist/compiler/src/preview/renderPreview.js +7158 -0
- package/dist/compiler/src/preview/styleSnapshot.js +268 -0
- package/dist/compiler/src/propTable.js +107 -0
- package/dist/compiler/src/style/css.js +161 -0
- package/dist/compiler/src/template/ast.js +19 -0
- package/dist/compiler/src/template/evaluate.js +151 -0
- package/dist/compiler/src/template/expr.js +360 -0
- package/dist/compiler/src/template/forms.js +80 -0
- package/dist/compiler/src/template/literal.js +248 -0
- package/dist/compiler/src/template/parser.js +230 -0
- package/dist/compiler/src/template/pipes.js +225 -0
- package/dist/compiler/src/template/printAst.js +38 -0
- package/dist/compiler/src/template/structural.js +51 -0
- package/dist/runtime/src/index.js +1197 -0
- package/dist/ui/src/chart.js +196 -0
- package/dist/ui/src/datetime.js +240 -0
- package/dist/ui/src/icons.js +310 -0
- package/dist/ui/src/mask.js +103 -0
- package/dist/ui/src/palette.js +141 -0
- package/dist/ui/src/tokens.js +173 -0
- package/package.json +28 -4
- package/README.md +0 -3
|
@@ -0,0 +1,430 @@
|
|
|
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.parseClassFile = parseClassFile;
|
|
37
|
+
exports.parseComponent = parseComponent;
|
|
38
|
+
const ts = __importStar(require("typescript"));
|
|
39
|
+
const diagnostics_1 = require("../diagnostics");
|
|
40
|
+
function decoratorName(decorator) {
|
|
41
|
+
const expr = decorator.expression;
|
|
42
|
+
if (ts.isCallExpression(expr) && ts.isIdentifier(expr.expression))
|
|
43
|
+
return expr.expression.text;
|
|
44
|
+
if (ts.isIdentifier(expr))
|
|
45
|
+
return expr.text;
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
function decoratorArgs(decorator) {
|
|
49
|
+
const expr = decorator.expression;
|
|
50
|
+
return ts.isCallExpression(expr) ? [...expr.arguments] : [];
|
|
51
|
+
}
|
|
52
|
+
function hasDecorator(node, name) {
|
|
53
|
+
if (!ts.canHaveDecorators(node))
|
|
54
|
+
return false;
|
|
55
|
+
return (ts.getDecorators(node) ?? []).some((d) => decoratorName(d) === name);
|
|
56
|
+
}
|
|
57
|
+
function stringOf(node) {
|
|
58
|
+
if (!node)
|
|
59
|
+
return undefined;
|
|
60
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
61
|
+
return node.text;
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
function stringArrayOf(node) {
|
|
65
|
+
if (!node || !ts.isArrayLiteralExpression(node))
|
|
66
|
+
return [];
|
|
67
|
+
return node.elements.map((el) => stringOf(el)).filter((s) => s !== undefined);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* True when an expression is a literal, or an array or object built
|
|
71
|
+
* only of literals (0.6.5): the shapes a module const may take and
|
|
72
|
+
* still be carried into the generated module verbatim. References,
|
|
73
|
+
* calls, spreads and shorthand properties are not literals.
|
|
74
|
+
*/
|
|
75
|
+
function isLiteralInitializer(node) {
|
|
76
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
77
|
+
return true;
|
|
78
|
+
if (ts.isNumericLiteral(node))
|
|
79
|
+
return true;
|
|
80
|
+
if (node.kind === ts.SyntaxKind.TrueKeyword || node.kind === ts.SyntaxKind.FalseKeyword
|
|
81
|
+
|| node.kind === ts.SyntaxKind.NullKeyword)
|
|
82
|
+
return true;
|
|
83
|
+
if (ts.isArrayLiteralExpression(node))
|
|
84
|
+
return node.elements.every((el) => isLiteralInitializer(el));
|
|
85
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
86
|
+
return node.properties.every((p) => ts.isPropertyAssignment(p) && isLiteralInitializer(p.initializer));
|
|
87
|
+
}
|
|
88
|
+
if (ts.isPrefixUnaryExpression(node)
|
|
89
|
+
&& (node.operator === ts.SyntaxKind.MinusToken || node.operator === ts.SyntaxKind.PlusToken)) {
|
|
90
|
+
return ts.isNumericLiteral(node.operand);
|
|
91
|
+
}
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
/** Fields + methods of a class, shared by component and service reading. */
|
|
95
|
+
function readMembers(node, sf) {
|
|
96
|
+
const fields = [];
|
|
97
|
+
const methods = [];
|
|
98
|
+
const methodDetails = [];
|
|
99
|
+
for (const member of node.members) {
|
|
100
|
+
if (ts.isPropertyDeclaration(member) && member.name) {
|
|
101
|
+
fields.push({
|
|
102
|
+
name: member.name.getText(sf),
|
|
103
|
+
initializer: member.initializer ? member.initializer.getText(sf) : undefined,
|
|
104
|
+
type: member.type ? member.type.getText(sf) : undefined,
|
|
105
|
+
isInput: hasDecorator(member, 'Input'),
|
|
106
|
+
isOutput: hasDecorator(member, 'Output'),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
else if (ts.isMethodDeclaration(member) && member.name) {
|
|
110
|
+
methods.push(member.name.getText(sf));
|
|
111
|
+
methodDetails.push({
|
|
112
|
+
name: member.name.getText(sf),
|
|
113
|
+
params: member.parameters.map((p) => ({
|
|
114
|
+
name: p.name.getText(sf),
|
|
115
|
+
text: p.getText(sf),
|
|
116
|
+
})),
|
|
117
|
+
body: member.body ? member.body.getText(sf) : undefined,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return { fields, methods, methodDetails };
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Constructor parameter properties (Stage 3 DI): a parameter counts as an
|
|
125
|
+
* injected dependency only when it carries an accessibility/readonly
|
|
126
|
+
* modifier, exactly Angular's rule. Plain parameters are not properties
|
|
127
|
+
* and are ignored.
|
|
128
|
+
*/
|
|
129
|
+
function readConstructor(node, sf) {
|
|
130
|
+
const deps = [];
|
|
131
|
+
let body;
|
|
132
|
+
for (const member of node.members) {
|
|
133
|
+
if (!ts.isConstructorDeclaration(member))
|
|
134
|
+
continue;
|
|
135
|
+
body = member.body ? member.body.getText(sf) : undefined;
|
|
136
|
+
for (const param of member.parameters) {
|
|
137
|
+
const modifiers = ts.getModifiers(param) ?? [];
|
|
138
|
+
const isParamProperty = modifiers.some((m) => m.kind === ts.SyntaxKind.PrivateKeyword ||
|
|
139
|
+
m.kind === ts.SyntaxKind.PublicKeyword ||
|
|
140
|
+
m.kind === ts.SyntaxKind.ProtectedKeyword ||
|
|
141
|
+
m.kind === ts.SyntaxKind.ReadonlyKeyword);
|
|
142
|
+
if (!isParamProperty || !param.type || !ts.isIdentifier(param.name))
|
|
143
|
+
continue;
|
|
144
|
+
deps.push({ propertyName: param.name.text, typeName: param.type.getText(sf).trim() });
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return { deps, body };
|
|
148
|
+
}
|
|
149
|
+
function classDecorators(node) {
|
|
150
|
+
return ts.canHaveDecorators(node) ? ts.getDecorators(node) ?? [] : [];
|
|
151
|
+
}
|
|
152
|
+
function readComponentClass(node, sf) {
|
|
153
|
+
const compDec = classDecorators(node).find((d) => decoratorName(d) === 'Component');
|
|
154
|
+
if (!compDec || !node.name)
|
|
155
|
+
return undefined;
|
|
156
|
+
const info = {
|
|
157
|
+
className: node.name.text,
|
|
158
|
+
sourceLine: sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1,
|
|
159
|
+
styleUrls: [],
|
|
160
|
+
fields: [],
|
|
161
|
+
methods: [],
|
|
162
|
+
methodDetails: [],
|
|
163
|
+
};
|
|
164
|
+
const arg = decoratorArgs(compDec)[0];
|
|
165
|
+
if (arg && ts.isObjectLiteralExpression(arg)) {
|
|
166
|
+
for (const prop of arg.properties) {
|
|
167
|
+
if (!ts.isPropertyAssignment(prop))
|
|
168
|
+
continue;
|
|
169
|
+
const key = prop.name.getText(sf).replace(/['"]/g, '');
|
|
170
|
+
if (key === 'selector')
|
|
171
|
+
info.selector = stringOf(prop.initializer);
|
|
172
|
+
else if (key === 'template')
|
|
173
|
+
info.template = stringOf(prop.initializer);
|
|
174
|
+
else if (key === 'templateUrl')
|
|
175
|
+
info.templateUrl = stringOf(prop.initializer);
|
|
176
|
+
else if (key === 'styleUrls')
|
|
177
|
+
info.styleUrls = stringArrayOf(prop.initializer);
|
|
178
|
+
else if (key === 'styles')
|
|
179
|
+
info.styles = stringArrayOf(prop.initializer).join('\n');
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const members = readMembers(node, sf);
|
|
183
|
+
info.fields = members.fields;
|
|
184
|
+
info.methods = members.methods;
|
|
185
|
+
info.methodDetails = members.methodDetails;
|
|
186
|
+
const ctor = readConstructor(node, sf);
|
|
187
|
+
info.constructorDeps = ctor.deps;
|
|
188
|
+
info.constructorBody = ctor.body;
|
|
189
|
+
return info;
|
|
190
|
+
}
|
|
191
|
+
function readServiceClass(node, sf, fileName) {
|
|
192
|
+
const dec = classDecorators(node).find((d) => decoratorName(d) === 'Injectable');
|
|
193
|
+
if (!dec || !node.name)
|
|
194
|
+
return undefined;
|
|
195
|
+
let providedInRoot = false;
|
|
196
|
+
const arg = decoratorArgs(dec)[0];
|
|
197
|
+
if (arg && ts.isObjectLiteralExpression(arg)) {
|
|
198
|
+
for (const prop of arg.properties) {
|
|
199
|
+
if (!ts.isPropertyAssignment(prop))
|
|
200
|
+
continue;
|
|
201
|
+
const key = prop.name.getText(sf).replace(/['"]/g, '');
|
|
202
|
+
if (key === 'providedIn')
|
|
203
|
+
providedInRoot = stringOf(prop.initializer) === 'root';
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const members = readMembers(node, sf);
|
|
207
|
+
const ctor = readConstructor(node, sf);
|
|
208
|
+
return {
|
|
209
|
+
className: node.name.text,
|
|
210
|
+
providedInRoot,
|
|
211
|
+
fields: members.fields,
|
|
212
|
+
methods: members.methods,
|
|
213
|
+
methodDetails: members.methodDetails,
|
|
214
|
+
constructorDeps: ctor.deps,
|
|
215
|
+
constructorBody: ctor.body,
|
|
216
|
+
fileName,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
function readPipeClass(node, sf, fileName) {
|
|
220
|
+
const dec = classDecorators(node).find((d) => decoratorName(d) === 'Pipe');
|
|
221
|
+
if (!dec || !node.name)
|
|
222
|
+
return undefined;
|
|
223
|
+
let pipeName;
|
|
224
|
+
const arg = decoratorArgs(dec)[0];
|
|
225
|
+
if (arg && ts.isObjectLiteralExpression(arg)) {
|
|
226
|
+
for (const prop of arg.properties) {
|
|
227
|
+
if (!ts.isPropertyAssignment(prop))
|
|
228
|
+
continue;
|
|
229
|
+
const key = prop.name.getText(sf).replace(/['"]/g, '');
|
|
230
|
+
if (key === 'name')
|
|
231
|
+
pipeName = stringOf(prop.initializer);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (!pipeName)
|
|
235
|
+
return undefined;
|
|
236
|
+
const members = readMembers(node, sf);
|
|
237
|
+
const ctor = readConstructor(node, sf);
|
|
238
|
+
let transformParams = [];
|
|
239
|
+
let transformReturn;
|
|
240
|
+
for (const member of node.members) {
|
|
241
|
+
if (!ts.isMethodDeclaration(member) || !member.name || member.name.getText(sf) !== 'transform')
|
|
242
|
+
continue;
|
|
243
|
+
transformParams = member.parameters.map((p) => p.name.getText(sf));
|
|
244
|
+
if (member.body && member.body.statements.length === 1) {
|
|
245
|
+
const only = member.body.statements[0];
|
|
246
|
+
if (ts.isReturnStatement(only) && only.expression) {
|
|
247
|
+
transformReturn = only.expression.getText(sf);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
pipeName,
|
|
253
|
+
className: node.name.text,
|
|
254
|
+
fields: members.fields,
|
|
255
|
+
methods: members.methods,
|
|
256
|
+
methodDetails: members.methodDetails,
|
|
257
|
+
constructorDeps: ctor.deps,
|
|
258
|
+
constructorBody: ctor.body,
|
|
259
|
+
transformParams,
|
|
260
|
+
transformReturn,
|
|
261
|
+
fileName,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function readDirectiveClass(node, sf, fileName) {
|
|
265
|
+
const dec = classDecorators(node).find((d) => decoratorName(d) === 'Directive');
|
|
266
|
+
if (!dec || !node.name)
|
|
267
|
+
return undefined;
|
|
268
|
+
let selector;
|
|
269
|
+
const arg = decoratorArgs(dec)[0];
|
|
270
|
+
if (arg && ts.isObjectLiteralExpression(arg)) {
|
|
271
|
+
for (const prop of arg.properties) {
|
|
272
|
+
if (!ts.isPropertyAssignment(prop))
|
|
273
|
+
continue;
|
|
274
|
+
const key = prop.name.getText(sf).replace(/['"]/g, '');
|
|
275
|
+
if (key === 'selector')
|
|
276
|
+
selector = stringOf(prop.initializer);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const info = {
|
|
280
|
+
className: node.name.text,
|
|
281
|
+
selector,
|
|
282
|
+
inputs: [],
|
|
283
|
+
fileName,
|
|
284
|
+
};
|
|
285
|
+
const bracket = selector ? /^\[\s*([A-Za-z][\w-]*)\s*\]$/.exec(selector.trim()) : null;
|
|
286
|
+
if (bracket)
|
|
287
|
+
info.attrName = bracket[1];
|
|
288
|
+
const ctor = readConstructor(node, sf);
|
|
289
|
+
const elDep = ctor.deps.find((d) => d.typeName === 'ElementRef');
|
|
290
|
+
if (elDep)
|
|
291
|
+
info.elProp = elDep.propertyName;
|
|
292
|
+
for (const member of node.members) {
|
|
293
|
+
if (ts.isPropertyDeclaration(member) && member.name && hasDecorator(member, 'Input')) {
|
|
294
|
+
info.inputs.push({
|
|
295
|
+
name: member.name.getText(sf),
|
|
296
|
+
initializer: member.initializer ? member.initializer.getText(sf) : undefined,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
else if (ts.isMethodDeclaration(member) && member.name
|
|
300
|
+
&& member.name.getText(sf) === 'ngOnInit' && member.body) {
|
|
301
|
+
info.initBody = member.body.getText(sf);
|
|
302
|
+
}
|
|
303
|
+
else if (ts.isSetAccessorDeclaration(member) && member.name && member.body
|
|
304
|
+
&& member.parameters.length === 1) {
|
|
305
|
+
info.setter = {
|
|
306
|
+
prop: member.name.getText(sf),
|
|
307
|
+
paramName: member.parameters[0].name.getText(sf),
|
|
308
|
+
body: member.body.getText(sf),
|
|
309
|
+
};
|
|
310
|
+
// An @Input() setter is also an input: the host binds it by the
|
|
311
|
+
// accessor's name, and the setter parameter stands for its value.
|
|
312
|
+
if (hasDecorator(member, 'Input')
|
|
313
|
+
&& !info.inputs.some((i) => i.name === info.setter.prop)) {
|
|
314
|
+
info.inputs.push({ name: info.setter.prop });
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return info;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Read a whole TypeScript file: every @Component class, every @Injectable
|
|
322
|
+
* class and every import statement. Stage 3's registry (compile.ts) follows
|
|
323
|
+
* relative imports across files using this per-file view. Emits no
|
|
324
|
+
* diagnostics of its own, a file of only services is a valid file.
|
|
325
|
+
*/
|
|
326
|
+
function parseClassFile(tsSource, fileName = 'component.ts') {
|
|
327
|
+
const sf = ts.createSourceFile(fileName, tsSource, ts.ScriptTarget.Latest, /* setParentNodes */ true);
|
|
328
|
+
const parseErrors = (sf.parseDiagnostics ?? [])
|
|
329
|
+
.map((d) => {
|
|
330
|
+
const pos = d.start !== undefined
|
|
331
|
+
? sf.getLineAndCharacterOfPosition(d.start)
|
|
332
|
+
: { line: 0, character: 0 };
|
|
333
|
+
return {
|
|
334
|
+
message: ts.flattenDiagnosticMessageText(d.messageText, ' '),
|
|
335
|
+
line: pos.line + 1,
|
|
336
|
+
column: pos.character + 1,
|
|
337
|
+
};
|
|
338
|
+
});
|
|
339
|
+
const result = { components: [], services: [], pipes: [], directives: [], consts: [], imports: [], importEquals: [], parseErrors };
|
|
340
|
+
const visit = (node) => {
|
|
341
|
+
if (ts.isVariableStatement(node) && node.parent === sf) {
|
|
342
|
+
const exported = (ts.getModifiers(node) ?? []).some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
343
|
+
for (const decl of node.declarationList.declarations) {
|
|
344
|
+
if (!ts.isIdentifier(decl.name) || !decl.initializer)
|
|
345
|
+
continue;
|
|
346
|
+
result.consts.push({
|
|
347
|
+
name: decl.name.text,
|
|
348
|
+
text: `const ${decl.getText(sf)};`,
|
|
349
|
+
initText: decl.initializer.getText(sf),
|
|
350
|
+
exported,
|
|
351
|
+
literal: isLiteralInitializer(decl.initializer),
|
|
352
|
+
line: sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
|
|
358
|
+
const names = [];
|
|
359
|
+
const bindings = [];
|
|
360
|
+
const clause = node.importClause;
|
|
361
|
+
if (clause) {
|
|
362
|
+
if (clause.name) {
|
|
363
|
+
bindings.push({ local: clause.name.text, imported: 'default', typeOnly: clause.isTypeOnly });
|
|
364
|
+
}
|
|
365
|
+
const named = clause.namedBindings;
|
|
366
|
+
if (named && ts.isNamedImports(named)) {
|
|
367
|
+
for (const el of named.elements) {
|
|
368
|
+
names.push(el.name.text);
|
|
369
|
+
bindings.push({
|
|
370
|
+
local: el.name.text,
|
|
371
|
+
imported: el.propertyName ? el.propertyName.text : el.name.text,
|
|
372
|
+
typeOnly: clause.isTypeOnly || el.isTypeOnly,
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
else if (named && ts.isNamespaceImport(named)) {
|
|
377
|
+
bindings.push({ local: named.name.text, imported: '*', typeOnly: clause.isTypeOnly });
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
result.imports.push({
|
|
381
|
+
names,
|
|
382
|
+
from: node.moduleSpecifier.text,
|
|
383
|
+
bindings,
|
|
384
|
+
line: sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1,
|
|
385
|
+
});
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
if (ts.isImportEqualsDeclaration(node)) {
|
|
389
|
+
result.importEquals.push({
|
|
390
|
+
local: node.name.text,
|
|
391
|
+
line: sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1,
|
|
392
|
+
});
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
if (ts.isClassDeclaration(node) && node.name) {
|
|
396
|
+
const comp = readComponentClass(node, sf);
|
|
397
|
+
if (comp)
|
|
398
|
+
result.components.push(comp);
|
|
399
|
+
const svc = readServiceClass(node, sf, fileName);
|
|
400
|
+
if (svc)
|
|
401
|
+
result.services.push(svc);
|
|
402
|
+
const pipe = readPipeClass(node, sf, fileName);
|
|
403
|
+
if (pipe)
|
|
404
|
+
result.pipes.push(pipe);
|
|
405
|
+
const directive = readDirectiveClass(node, sf, fileName);
|
|
406
|
+
if (directive)
|
|
407
|
+
result.directives.push(directive);
|
|
408
|
+
return; // classes are not nested further for our purposes
|
|
409
|
+
}
|
|
410
|
+
ts.forEachChild(node, visit);
|
|
411
|
+
};
|
|
412
|
+
visit(sf);
|
|
413
|
+
return result;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Extract @Component metadata from a TypeScript source file using the
|
|
417
|
+
* official TypeScript compiler API, Rangular never parses TS by hand.
|
|
418
|
+
*/
|
|
419
|
+
function parseComponent(tsSource, fileName = 'component.ts') {
|
|
420
|
+
const diagnostics = [];
|
|
421
|
+
const { components, parseErrors } = parseClassFile(tsSource, fileName);
|
|
422
|
+
for (const pe of parseErrors) {
|
|
423
|
+
diagnostics.push((0, diagnostics_1.error)('CLS_PARSE', `TypeScript could not parse this file: ${pe.message}`, pe.line, pe.column, 'Fix the syntax error; classes and fields after it may be missing from the generated code'));
|
|
424
|
+
}
|
|
425
|
+
const found = components[0];
|
|
426
|
+
if (!found) {
|
|
427
|
+
diagnostics.push((0, diagnostics_1.error)('CLS_NO_COMPONENT', 'No @Component-decorated class found in the source', undefined, undefined, "Decorate the class: @Component({ selector: 'app-x', templateUrl: './x.component.html' }) export class XComponent { ... }, only @Component classes are compiled; @NgModule/@Injectable classes are not entry points"));
|
|
428
|
+
}
|
|
429
|
+
return { component: found, diagnostics };
|
|
430
|
+
}
|
|
@@ -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
|
+
}
|