@openwebf/webf 0.23.7 → 0.24.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/constants.js DELETED
@@ -1,242 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.generateUnionConstantsValuesTs = generateUnionConstantsValuesTs;
7
- exports.generateUnionConstantsDts = generateUnionConstantsDts;
8
- exports.generateDeclaredConstantsValuesTs = generateDeclaredConstantsValuesTs;
9
- exports.generateDeclaredConstantsDts = generateDeclaredConstantsDts;
10
- const lodash_1 = __importDefault(require("lodash"));
11
- const typescript_1 = __importDefault(require("typescript"));
12
- const dart_1 = require("./dart");
13
- // Generate constant name from component (Properties/Bindings trimmed) and prop name
14
- function getConstName(className, propName) {
15
- const baseName = className.replace(/Properties$|Bindings$/, '');
16
- return baseName + lodash_1.default.upperFirst(lodash_1.default.camelCase(propName));
17
- }
18
- function collectUnionStringProps(blobs) {
19
- const results = [];
20
- for (const blob of blobs) {
21
- const classObjects = (blob.objects || []);
22
- const properties = classObjects.filter(obj => obj.name && obj.name.endsWith('Properties'));
23
- if (!properties.length)
24
- continue;
25
- const componentProps = properties[0];
26
- const componentName = componentProps.name.replace(/Properties$/, '');
27
- for (const prop of componentProps.props || []) {
28
- if (!(0, dart_1.isStringUnionType)(prop.type))
29
- continue;
30
- const values = (0, dart_1.getUnionStringValues)(prop, blob);
31
- if (!values || values.length === 0)
32
- continue;
33
- const constName = getConstName(componentProps.name, prop.name);
34
- results.push({ constName, values, className: componentName, propName: prop.name });
35
- }
36
- }
37
- return results;
38
- }
39
- function generateUnionConstantsValuesTs(blobs) {
40
- const items = collectUnionStringProps(blobs);
41
- if (!items.length)
42
- return '';
43
- const header = `// Auto-generated by WebF CLI\n// Constants for string-union properties extracted from .d.ts definitions\n`;
44
- const blocks = items.map(item => {
45
- const entries = item.values.map(v => ` '${v}': '${v}',`).join('\n');
46
- return `// ${item.className}.${item.propName}\nexport const ${item.constName} = {\n${entries}\n} as const;`;
47
- });
48
- return [header, ...blocks].join('\n\n') + '\n';
49
- }
50
- function generateUnionConstantsDts(blobs) {
51
- const items = collectUnionStringProps(blobs);
52
- if (!items.length)
53
- return '';
54
- const header = `// Auto-generated by WebF CLI\n// Type declarations for constants representing string-union property values\n`;
55
- const blocks = items.map(item => {
56
- const entries = item.values.map(v => ` readonly '${v}': '${v}';`).join('\n');
57
- return `// ${item.className}.${item.propName}\nexport declare const ${item.constName}: {\n${entries}\n};`;
58
- });
59
- return [header, ...blocks].join('\n\n') + '\n';
60
- }
61
- function parseLiteralFromType(node) {
62
- if (typescript_1.default.isLiteralTypeNode(node)) {
63
- const lit = node.literal;
64
- if (typescript_1.default.isStringLiteral(lit))
65
- return lit.text;
66
- if (typescript_1.default.isNumericLiteral(lit))
67
- return Number(lit.text);
68
- if (lit.kind === typescript_1.default.SyntaxKind.TrueKeyword)
69
- return true;
70
- if (lit.kind === typescript_1.default.SyntaxKind.FalseKeyword)
71
- return false;
72
- return null;
73
- }
74
- if (typescript_1.default.isTypeQueryNode(node)) {
75
- // typeof Identifier
76
- if (typescript_1.default.isIdentifier(node.exprName)) {
77
- return { typeofRef: node.exprName.text };
78
- }
79
- }
80
- return null;
81
- }
82
- function collectDeclaredConstsFromSource(content, fileName = 'index.d.ts') {
83
- const source = typescript_1.default.createSourceFile(fileName, content, typescript_1.default.ScriptTarget.ES2020, true, typescript_1.default.ScriptKind.TS);
84
- const results = [];
85
- const handleVariableStatement = (stmt) => {
86
- // Only consider const declarations
87
- if ((stmt.declarationList.flags & typescript_1.default.NodeFlags.Const) === 0)
88
- return;
89
- for (const decl of stmt.declarationList.declarations) {
90
- if (!typescript_1.default.isIdentifier(decl.name) || !decl.type)
91
- continue;
92
- const name = decl.name.text;
93
- const val = parseLiteralFromType(decl.type);
94
- if (val == null)
95
- continue;
96
- results.push({ kind: 'const', name, value: val });
97
- }
98
- };
99
- const handleClass = (cls) => {
100
- var _a, _b, _c;
101
- const container = (_a = cls.name) === null || _a === void 0 ? void 0 : _a.text;
102
- if (!container)
103
- return;
104
- for (const m of cls.members) {
105
- if (!typescript_1.default.isPropertyDeclaration(m))
106
- continue;
107
- const isStatic = (_b = m.modifiers) === null || _b === void 0 ? void 0 : _b.some(mod => mod.kind === typescript_1.default.SyntaxKind.StaticKeyword);
108
- const isReadonly = (_c = m.modifiers) === null || _c === void 0 ? void 0 : _c.some(mod => mod.kind === typescript_1.default.SyntaxKind.ReadonlyKeyword);
109
- if (!isStatic || !isReadonly || !m.type)
110
- continue;
111
- if (!typescript_1.default.isIdentifier(m.name))
112
- continue;
113
- const name = m.name.text;
114
- const val = parseLiteralFromType(m.type);
115
- if (val == null)
116
- continue;
117
- results.push({ kind: 'container', container, name, value: val });
118
- }
119
- };
120
- const handleModule = (mod) => {
121
- const container = mod.name.getText(source).replace(/['"]/g, '');
122
- if (!mod.body || !typescript_1.default.isModuleBlock(mod.body))
123
- return;
124
- for (const stmt of mod.body.statements) {
125
- if (typescript_1.default.isVariableStatement(stmt)) {
126
- if ((stmt.declarationList.flags & typescript_1.default.NodeFlags.Const) === 0)
127
- continue;
128
- for (const decl of stmt.declarationList.declarations) {
129
- if (!typescript_1.default.isIdentifier(decl.name) || !decl.type)
130
- continue;
131
- const name = decl.name.text;
132
- const val = parseLiteralFromType(decl.type);
133
- if (val == null)
134
- continue;
135
- results.push({ kind: 'container', container, name, value: val });
136
- }
137
- }
138
- }
139
- };
140
- for (const stmt of source.statements) {
141
- if (typescript_1.default.isVariableStatement(stmt))
142
- handleVariableStatement(stmt);
143
- else if (typescript_1.default.isClassDeclaration(stmt))
144
- handleClass(stmt);
145
- else if (typescript_1.default.isModuleDeclaration(stmt))
146
- handleModule(stmt);
147
- }
148
- return results;
149
- }
150
- function collectDeclaredConsts(blobs) {
151
- const all = [];
152
- for (const blob of blobs) {
153
- const raw = blob.raw || '';
154
- if (!raw)
155
- continue;
156
- try {
157
- const items = collectDeclaredConstsFromSource(raw, blob.filename + '.d.ts');
158
- all.push(...items);
159
- }
160
- catch (_) {
161
- // ignore parse errors per file
162
- }
163
- }
164
- return all;
165
- }
166
- function literalToTsValue(val) {
167
- if (typeof val === 'string')
168
- return `'${val.replace(/'/g, "\\'")}'`;
169
- if (typeof val === 'number')
170
- return String(val);
171
- if (typeof val === 'boolean')
172
- return val ? 'true' : 'false';
173
- if (typeof val.typeofRef === 'string')
174
- return val.typeofRef;
175
- return 'undefined';
176
- }
177
- function generateDeclaredConstantsValuesTs(blobs) {
178
- const items = collectDeclaredConsts(blobs);
179
- if (!items.length)
180
- return '';
181
- const header = `// Auto-generated by WebF CLI\n// Runtime constants mirrored from .d.ts 'declare const' definitions\n`;
182
- const topLevel = [];
183
- const containers = new Map();
184
- for (const it of items) {
185
- if (it.kind === 'const') {
186
- topLevel.push(`export const ${it.name} = ${literalToTsValue(it.value)} as const;`);
187
- }
188
- else {
189
- if (!containers.has(it.container))
190
- containers.set(it.container, []);
191
- containers.get(it.container).push(it);
192
- }
193
- }
194
- const containerBlocks = [];
195
- for (const [container, arr] of containers) {
196
- const lines = arr
197
- .sort((a, b) => a.name.localeCompare(b.name))
198
- .map(a => ` ${a.name}: ${literalToTsValue(a.value)},`) // keep plain object
199
- .join('\n');
200
- containerBlocks.push(`export const ${container} = {\n${lines}\n} as const;`);
201
- }
202
- return [header, ...topLevel, ...containerBlocks].filter(Boolean).join('\n\n') + '\n';
203
- }
204
- function generateDeclaredConstantsDts(blobs) {
205
- const items = collectDeclaredConsts(blobs);
206
- if (!items.length)
207
- return '';
208
- const header = `// Auto-generated by WebF CLI\n// Type declarations for 'declare const' values mirrored into JS runtime\n`;
209
- const topLevel = [];
210
- const containers = new Map();
211
- for (const it of items) {
212
- if (it.kind === 'const') {
213
- const val = typeof it.value === 'object' && it.value.typeofRef
214
- ? `typeof ${it.value.typeofRef}`
215
- : typeof it.value === 'string'
216
- ? `'${it.value.replace(/'/g, "\\'")}'`
217
- : String(it.value);
218
- topLevel.push(`export declare const ${it.name}: ${val};`);
219
- }
220
- else {
221
- if (!containers.has(it.container))
222
- containers.set(it.container, []);
223
- containers.get(it.container).push(it);
224
- }
225
- }
226
- const containerBlocks = [];
227
- for (const [container, arr] of containers) {
228
- const lines = arr
229
- .sort((a, b) => a.name.localeCompare(b.name))
230
- .map(a => {
231
- const v = typeof a.value === 'object' && a.value.typeofRef
232
- ? `typeof ${a.value.typeofRef}`
233
- : typeof a.value === 'string'
234
- ? `'${a.value.replace(/'/g, "\\'")}'`
235
- : String(a.value);
236
- return ` readonly ${a.name}: ${v};`;
237
- })
238
- .join('\n');
239
- containerBlocks.push(`export declare const ${container}: {\n${lines}\n};`);
240
- }
241
- return [header, ...topLevel, ...containerBlocks].filter(Boolean).join('\n\n') + '\n';
242
- }