@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.
Files changed (68) hide show
  1. package/dist/compiler/src/catalog.d.ts +63 -0
  2. package/dist/compiler/src/catalog.js +198 -0
  3. package/dist/compiler/src/class/formInit.d.ts +119 -0
  4. package/dist/compiler/src/class/formInit.js +370 -0
  5. package/dist/compiler/src/class/parseComponent.d.ts +232 -0
  6. package/dist/compiler/src/class/parseComponent.js +430 -0
  7. package/dist/compiler/src/class/parseDirective.d.ts +42 -0
  8. package/dist/compiler/src/class/parseDirective.js +227 -0
  9. package/dist/compiler/src/class/parseRoutes.d.ts +59 -0
  10. package/dist/compiler/src/class/parseRoutes.js +163 -0
  11. package/dist/compiler/src/codegen/generate.d.ts +166 -0
  12. package/dist/compiler/src/codegen/generate.js +3466 -0
  13. package/dist/compiler/src/codegen/methods.d.ts +148 -0
  14. package/dist/compiler/src/codegen/methods.js +975 -0
  15. package/dist/compiler/src/codegen/sourceMap.d.ts +42 -0
  16. package/dist/compiler/src/codegen/sourceMap.js +77 -0
  17. package/dist/compiler/src/compile.d.ts +101 -0
  18. package/dist/compiler/src/compile.js +775 -0
  19. package/dist/compiler/src/diagnostics.d.ts +37 -0
  20. package/dist/compiler/src/diagnostics.js +113 -0
  21. package/dist/compiler/src/doctor.d.ts +35 -0
  22. package/dist/compiler/src/doctor.js +144 -0
  23. package/dist/compiler/src/fix.d.ts +21 -0
  24. package/dist/compiler/src/fix.js +44 -0
  25. package/dist/compiler/src/index.d.ts +22 -0
  26. package/dist/compiler/src/index.js +38 -0
  27. package/dist/compiler/src/preview/renderPreview.d.ts +101 -0
  28. package/dist/compiler/src/preview/renderPreview.js +7158 -0
  29. package/dist/compiler/src/preview/styleSnapshot.d.ts +36 -0
  30. package/dist/compiler/src/preview/styleSnapshot.js +268 -0
  31. package/dist/compiler/src/propTable.d.ts +22 -0
  32. package/dist/compiler/src/propTable.js +107 -0
  33. package/dist/compiler/src/style/css.d.ts +23 -0
  34. package/dist/compiler/src/style/css.js +161 -0
  35. package/dist/compiler/src/template/ast.d.ts +66 -0
  36. package/dist/compiler/src/template/ast.js +19 -0
  37. package/dist/compiler/src/template/evaluate.d.ts +45 -0
  38. package/dist/compiler/src/template/evaluate.js +151 -0
  39. package/dist/compiler/src/template/expr.d.ts +94 -0
  40. package/dist/compiler/src/template/expr.js +360 -0
  41. package/dist/compiler/src/template/forms.d.ts +42 -0
  42. package/dist/compiler/src/template/forms.js +80 -0
  43. package/dist/compiler/src/template/literal.d.ts +38 -0
  44. package/dist/compiler/src/template/literal.js +248 -0
  45. package/dist/compiler/src/template/parser.d.ts +14 -0
  46. package/dist/compiler/src/template/parser.js +230 -0
  47. package/dist/compiler/src/template/pipes.d.ts +48 -0
  48. package/dist/compiler/src/template/pipes.js +225 -0
  49. package/dist/compiler/src/template/printAst.d.ts +3 -0
  50. package/dist/compiler/src/template/printAst.js +38 -0
  51. package/dist/compiler/src/template/structural.d.ts +27 -0
  52. package/dist/compiler/src/template/structural.js +51 -0
  53. package/dist/runtime/src/index.d.ts +486 -0
  54. package/dist/runtime/src/index.js +1197 -0
  55. package/dist/ui/src/chart.d.ts +81 -0
  56. package/dist/ui/src/chart.js +196 -0
  57. package/dist/ui/src/datetime.d.ts +92 -0
  58. package/dist/ui/src/datetime.js +240 -0
  59. package/dist/ui/src/icons.d.ts +63 -0
  60. package/dist/ui/src/icons.js +310 -0
  61. package/dist/ui/src/mask.d.ts +38 -0
  62. package/dist/ui/src/mask.js +103 -0
  63. package/dist/ui/src/palette.d.ts +66 -0
  64. package/dist/ui/src/palette.js +141 -0
  65. package/dist/ui/src/tokens.d.ts +129 -0
  66. package/dist/ui/src/tokens.js +173 -0
  67. package/package.json +24 -4
  68. package/README.md +0 -3
@@ -0,0 +1,63 @@
1
+ import { colours, elevation, radius, spacing, themes, typeScale } from '../../ui/src/tokens';
2
+ /**
3
+ * The machine-readable catalog of everything an author (human or AI) can
4
+ * use in Rangular source, generated FROM the code, never hand-maintained:
5
+ *
6
+ * - tags: TAG_MAP in codegen/generate.ts (the same table codegen renders from)
7
+ * - primitives: prop names parsed from packages/ui/src/primitives.tsx with
8
+ * the TypeScript compiler API. The primitives are deliberately
9
+ * typed `any` (shipped source), so the destructured parameter
10
+ * pattern of each exported Rg* function is the reliable,
11
+ * drift-free source of prop names.
12
+ * - tokens: imported from @rangular-framework/ui tokens and serialised.
13
+ * - icons: ICON_NAMES from @rangular-framework/ui icons.ts, the same set the
14
+ * compiler validates <icon> names against.
15
+ * - diagnostics: DIAGNOSTIC_CODES in diagnostics.ts.
16
+ */
17
+ export interface CatalogTag {
18
+ tag: string;
19
+ component: string;
20
+ from: '@rangular-framework/ui' | 'react-native';
21
+ variant?: string;
22
+ }
23
+ export interface CatalogPrimitive {
24
+ name: string;
25
+ props: {
26
+ name: string;
27
+ type: string;
28
+ }[];
29
+ }
30
+ export interface Catalog {
31
+ name: string;
32
+ version: string;
33
+ tags: CatalogTag[];
34
+ events: {
35
+ event: string;
36
+ rnProp: string;
37
+ note?: string;
38
+ }[];
39
+ primitives: CatalogPrimitive[];
40
+ icons: string[];
41
+ tokens: {
42
+ spacing: typeof spacing;
43
+ spacingScale: number[];
44
+ radius: typeof radius;
45
+ radiusScale: number[];
46
+ typeScale: typeof typeScale;
47
+ colours: typeof colours;
48
+ elevation: typeof elevation;
49
+ themes: typeof themes;
50
+ };
51
+ diagnostics: {
52
+ code: string;
53
+ meaning: string;
54
+ }[];
55
+ }
56
+ /** Walk up from a start directory until packages/ui/src/primitives.tsx is found. */
57
+ export declare function findRepoRoot(startDir: string): string | undefined;
58
+ export declare function parsePrimitives(source: string, fileName?: string): CatalogPrimitive[];
59
+ export declare function buildCatalog(options?: {
60
+ primitivesPath?: string;
61
+ fromDir?: string;
62
+ }): Catalog;
63
+ export declare function renderCatalogMarkdown(catalog: Catalog): string;
@@ -0,0 +1,198 @@
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.findRepoRoot = findRepoRoot;
37
+ exports.parsePrimitives = parsePrimitives;
38
+ exports.buildCatalog = buildCatalog;
39
+ exports.renderCatalogMarkdown = renderCatalogMarkdown;
40
+ const fs = __importStar(require("node:fs"));
41
+ const path = __importStar(require("node:path"));
42
+ const ts = __importStar(require("typescript"));
43
+ const diagnostics_1 = require("./diagnostics");
44
+ const generate_1 = require("./codegen/generate");
45
+ const tokens_1 = require("../../ui/src/tokens");
46
+ const icons_1 = require("../../ui/src/icons");
47
+ /** Walk up from a start directory until packages/ui/src/primitives.tsx is found. */
48
+ function findRepoRoot(startDir) {
49
+ let dir = path.resolve(startDir);
50
+ for (let i = 0; i < 8; i++) {
51
+ if (fs.existsSync(path.join(dir, 'packages', 'ui', 'src', 'primitives.tsx')))
52
+ return dir;
53
+ const parent = path.dirname(dir);
54
+ if (parent === dir)
55
+ return undefined;
56
+ dir = parent;
57
+ }
58
+ return undefined;
59
+ }
60
+ function parsePrimitives(source, fileName = 'primitives.tsx') {
61
+ const sf = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
62
+ const primitives = [];
63
+ const visit = (node) => {
64
+ if (ts.isFunctionDeclaration(node) && node.name && /^Rg[A-Z]/.test(node.name.text)
65
+ && node.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)) {
66
+ const param = node.parameters[0];
67
+ const props = [];
68
+ if (param) {
69
+ const typeText = param.type ? param.type.getText(sf) : 'any';
70
+ if (ts.isObjectBindingPattern(param.name)) {
71
+ for (const el of param.name.elements) {
72
+ // A renamed destructure (`{ theme: mode }`) declares the
73
+ // prop `theme` and binds it locally as `mode`; the catalog
74
+ // lists the prop name callers actually pass, which is the
75
+ // propertyName when one is written.
76
+ const propName = el.propertyName ? el.propertyName.getText(sf) : el.name.getText(sf);
77
+ props.push({ name: propName, type: typeText });
78
+ }
79
+ }
80
+ }
81
+ primitives.push({ name: node.name.text, props });
82
+ }
83
+ ts.forEachChild(node, visit);
84
+ };
85
+ visit(sf);
86
+ return primitives;
87
+ }
88
+ function buildCatalog(options = {}) {
89
+ let primitives = [];
90
+ let version = '0.0.0';
91
+ const primPath = options.primitivesPath
92
+ ?? (() => {
93
+ const root = findRepoRoot(options.fromDir ?? __dirname);
94
+ return root ? path.join(root, 'packages', 'ui', 'src', 'primitives.tsx') : undefined;
95
+ })();
96
+ if (primPath && fs.existsSync(primPath)) {
97
+ primitives = parsePrimitives(fs.readFileSync(primPath, 'utf8'), path.basename(primPath));
98
+ const root = findRepoRoot(path.dirname(primPath));
99
+ if (root) {
100
+ try {
101
+ const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'));
102
+ if (typeof pkg.version === 'string')
103
+ version = pkg.version;
104
+ }
105
+ catch { /* version stays 0.0.0 */ }
106
+ }
107
+ }
108
+ const tags = Object.entries(generate_1.TAG_MAP).map(([tag, m]) => ({
109
+ tag,
110
+ component: m.component,
111
+ from: m.fromUi ? '@rangular-framework/ui' : 'react-native',
112
+ ...(m.variant ? { variant: m.variant } : {}),
113
+ }));
114
+ const events = Object.entries(generate_1.EVENT_PROPS).map(([event, rnProp]) => ({ event, rnProp }));
115
+ events.push({
116
+ event: 'input',
117
+ rnProp: 'onChangeText',
118
+ note: 'On <input> only; on any other tag (input) maps to onInput and the compiler notes it has no standard RN prop.',
119
+ });
120
+ return {
121
+ name: 'rangular-framework',
122
+ version,
123
+ tags,
124
+ events,
125
+ primitives,
126
+ icons: [...icons_1.ICON_NAMES],
127
+ tokens: { spacing: tokens_1.spacing, spacingScale: tokens_1.spacingScale, radius: tokens_1.radius, radiusScale: tokens_1.radiusScale, typeScale: tokens_1.typeScale, colours: tokens_1.colours, elevation: tokens_1.elevation, themes: tokens_1.themes },
128
+ diagnostics: Object.entries(diagnostics_1.DIAGNOSTIC_CODES).map(([code, meaning]) => ({ code, meaning })),
129
+ };
130
+ }
131
+ function renderCatalogMarkdown(catalog) {
132
+ const lines = [];
133
+ lines.push('# Rangular catalog');
134
+ lines.push('');
135
+ lines.push(`Generated by \`rg catalog\` from the compiler, UI primitives and tokens, do not edit by hand. Version ${catalog.version}.`);
136
+ lines.push('');
137
+ lines.push('## Template tags');
138
+ lines.push('');
139
+ lines.push('| Tag | Generates | From | Text variant |');
140
+ lines.push('|---|---|---|---|');
141
+ for (const t of catalog.tags) {
142
+ lines.push(`| \`<${t.tag}>\` | \`${t.component}\` | ${t.from} | ${t.variant ?? '-'} |`);
143
+ }
144
+ lines.push('');
145
+ lines.push('Any other Capitalised or dashed tag is treated as an app component and passed through; any other lowercase tag warns (GEN_UNKNOWN_TAG) and becomes <View>.');
146
+ lines.push('');
147
+ lines.push('## Events');
148
+ lines.push('');
149
+ lines.push('| Template event | React Native prop |');
150
+ lines.push('|---|---|');
151
+ for (const e of catalog.events) {
152
+ lines.push(`| \`(${e.event})\` | \`${e.rnProp}\`${e.note ? `, ${e.note}` : ''} |`);
153
+ }
154
+ lines.push('');
155
+ lines.push('Any other (event) maps to on<Event> with a compiler comment that it has no standard RN prop.');
156
+ lines.push('');
157
+ lines.push('## Primitives and their props');
158
+ lines.push('');
159
+ lines.push('Props are parsed from `packages/ui/src/primitives.tsx`; the primitives are shipped source typed `any`, so every prop type is `any` today.');
160
+ lines.push('');
161
+ for (const p of catalog.primitives) {
162
+ lines.push(`### ${p.name}`);
163
+ lines.push('');
164
+ if (!p.props.length)
165
+ lines.push('No props.');
166
+ for (const prop of p.props)
167
+ lines.push(`- \`${prop.name}\`: ${prop.type}`);
168
+ lines.push('');
169
+ }
170
+ lines.push('## Icons');
171
+ lines.push('');
172
+ lines.push(`Icons live in \`packages/ui/src/icons.ts\` as SVG path data (24 by 24 viewBox). \`<icon name="...">\` draws one of these ${catalog.icons.length} names; any other name is a GEN_ICON_UNKNOWN error whose JSON form carries this same list in \`validIcons\`.`);
173
+ lines.push('');
174
+ for (const name of catalog.icons)
175
+ lines.push(`- \`${name}\``);
176
+ lines.push('');
177
+ lines.push('## Design tokens');
178
+ lines.push('');
179
+ lines.push(`- Spacing scale: ${catalog.tokens.spacingScale.join(', ')} (keys: ${Object.keys(catalog.tokens.spacing).join(', ')})`);
180
+ lines.push(`- Radius scale: ${catalog.tokens.radiusScale.join(', ')} (keys: ${Object.keys(catalog.tokens.radius).join(', ')}; full = 9999)`);
181
+ lines.push(`- Type scale: ${Object.entries(catalog.tokens.typeScale).map(([k, v]) => `${k} ${v.fontSize}/${v.lineHeight} w${v.fontWeight}`).join(' · ')}`);
182
+ lines.push(`- Colour roles (same roles in light and dark): ${Object.keys(catalog.tokens.colours.light).join(', ')}`);
183
+ lines.push(`- Elevation presets: ${Object.entries(catalog.tokens.elevation).map(([k, v]) => `${k} (elevation ${v.elevation})`).join(', ')}`);
184
+ lines.push(`- Theming: \`<screen theme="dark">\` compiles to \`<RgScreen theme="dark">\`; primitives read the active theme from context (RgThemeProvider / useRgTheme in @rangular-framework/ui) and default to light. CSS spacing/radius/font-size values snap to the scales above (STYLE_OFF_SCALE).`);
185
+ lines.push('');
186
+ lines.push('Full token values (both colour modes, the light and dark theme objects, elevation presets) are in catalog.json.');
187
+ lines.push('');
188
+ lines.push('## Diagnostic codes');
189
+ lines.push('');
190
+ lines.push('| Code | Meaning |');
191
+ lines.push('|---|---|');
192
+ for (const d of catalog.diagnostics)
193
+ lines.push(`| ${d.code} | ${d.meaning} |`);
194
+ lines.push('');
195
+ lines.push('Diagnostics also carry a `fix:` suggestion in human output and in `rg build --json`.');
196
+ lines.push('');
197
+ return lines.join('\n');
198
+ }
@@ -0,0 +1,119 @@
1
+ import { FormNode } from '../../../runtime/src/index';
2
+ /**
3
+ * Reactive form field initializers (Stage 8; tree and builder shapes
4
+ * since the forms completion pass).
5
+ *
6
+ * A component field initialised with a form tree is a stable forms
7
+ * instance, not state: codegen holds it in a React ref, and the
8
+ * preview builds the real runtime instance so templates can read
9
+ * `profileForm.get('email').invalid` before any interaction.
10
+ *
11
+ * Both jobs need to understand the initializer's shape, and the
12
+ * preview additionally needs to EVALUATE it. The supported subset is
13
+ * exactly:
14
+ *
15
+ * new FormControl(<literal>, Validators.x)
16
+ * new FormControl(<literal>, [Validators.x, Validators.minLength(3), ...])
17
+ * new FormGroup({ name: <any form node expression>, ... })
18
+ * new FormArray([ <any form node expression>, ... ])
19
+ * this.<fb>.group({ name: <entry>, ... }) (<fb> a FormBuilder dep)
20
+ * this.<fb>.control(<literal>, Validators.x)
21
+ * this.<fb>.array([ <entry>, ... ])
22
+ *
23
+ * where a form node expression is any of the `new ...` shapes or a
24
+ * nested builder call, a builder <entry> is a form node expression, a
25
+ * plain literal (a control holding it), or a tuple
26
+ * [ <literal>, Validators.x | [Validators.x, ...] ], a literal is a
27
+ * string, number, boolean, null, or unary-minus number, and validator
28
+ * arguments are literals. Anything richer is reported as unsupported:
29
+ * the generated app still runs the initializer verbatim (it is the
30
+ * author's real code), only the preview cannot build the instance,
31
+ * and compile.ts says so with GEN_FORM_INITIALIZER.
32
+ */
33
+ export type FormKind = 'group' | 'control' | 'array';
34
+ export interface FormFieldInfo {
35
+ kind: FormKind;
36
+ /** Control names of a FormGroup, when its config is an object literal. */
37
+ controlNames: string[];
38
+ /** The kind of each named control of a group, when known. */
39
+ controlKinds: Record<string, FormKind>;
40
+ /** True when the preview evaluator below can build the instance. */
41
+ supported: boolean;
42
+ /** Why not, when supported is false. */
43
+ reason?: string;
44
+ /**
45
+ * The constructor dep the initializer calls, when it is a builder
46
+ * initializer (`this.fb.group(...)`): codegen rewrites `this.fb` to
47
+ * the generated dep local of the same name.
48
+ */
49
+ builderDep?: string;
50
+ }
51
+ /**
52
+ * True for field initializers that build a form tree: a `new
53
+ * FormGroup/FormControl/FormArray(...)`, or a call on a FormBuilder
54
+ * constructor dep (`this.fb.group(...)`) when the dep name is in
55
+ * builderDeps.
56
+ */
57
+ export declare function isFormInitializer(initializer: string | undefined, builderDeps?: Set<string>): boolean;
58
+ export type SignalKind = 'signal' | 'computed';
59
+ /**
60
+ * The kind of a signal field initializer (signals adoption): a field
61
+ * initialised with `signal(...)` or `computed(...)` from
62
+ * '@angular/core'. Like a subject field, a signal field is one stable
63
+ * instance for the component's whole life: codegen holds it in a ref,
64
+ * because a signal recreated per render would lose its value and
65
+ * orphan every reader. In a service the same initializer is a plain
66
+ * class field, emitted verbatim.
67
+ */
68
+ export declare function signalKindOf(initializer: string | undefined): SignalKind | undefined;
69
+ export declare function isSignalInitializer(initializer: string | undefined): boolean;
70
+ /**
71
+ * True when a field initializer constructs an RxJS subject (RxJS
72
+ * adoption). Like a form field, a subject field is one stable
73
+ * instance for the component's whole life: codegen holds it in a
74
+ * ref, because a subject recreated per render would orphan every
75
+ * subscriber of the previous instance.
76
+ */
77
+ export declare function isStreamInitializer(initializer: string | undefined): boolean;
78
+ /** The FormBuilder constructor dep names of a host component or service. */
79
+ export declare function builderDepsOf(host: {
80
+ constructorDeps?: {
81
+ propertyName: string;
82
+ typeName: string;
83
+ }[];
84
+ }): Set<string>;
85
+ /** Kind and group control names from the initializer's shape alone. */
86
+ export declare function formFieldKind(initializer: string, builderDeps?: Set<string>): {
87
+ kind: FormKind;
88
+ controlNames: string[];
89
+ controlKinds: Record<string, FormKind>;
90
+ builderDep?: string;
91
+ };
92
+ /**
93
+ * Rewrite the `this.<dep>` roots of a builder initializer to the bare
94
+ * dep name, for generated code: the generated component holds the
95
+ * injected FormBuilder in a local of the dep's own name, so
96
+ * `this.fb.group(...)` becomes `fb.group(...)`, nested builder calls
97
+ * included. Spans come from the TypeScript AST, so the text of a
98
+ * string literal in the config is never touched.
99
+ */
100
+ export declare function rewriteBuilderInitializer(initializer: string, dep: string): string;
101
+ /**
102
+ * Analyse one form field initializer: its kind, its group control
103
+ * names and kinds, and whether the preview can evaluate it.
104
+ * builderDeps names the component's FormBuilder constructor deps, so
105
+ * `this.fb.group(...)` initializers are recognised as builder calls.
106
+ */
107
+ export declare function analyzeFormInitializer(initializer: string, builderDeps?: Set<string>): FormFieldInfo;
108
+ /**
109
+ * Build the real runtime instance for a form field initializer, for
110
+ * the preview's server-side paint. Fails with { ok: false } on the
111
+ * same shapes analyzeFormInitializer reports as unsupported.
112
+ */
113
+ export declare function evaluateFormInitializer(initializer: string, builderDeps?: Set<string>): {
114
+ ok: true;
115
+ value: FormNode;
116
+ } | {
117
+ ok: false;
118
+ reason: string;
119
+ };