@sprlab/wccompiler 0.0.3 → 0.2.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/README.md +268 -51
- package/bin/wcc.js +65 -100
- package/bin/wcc.test.js +119 -0
- package/lib/codegen.js +882 -166
- package/lib/compiler.js +172 -33
- package/lib/config.js +33 -43
- package/lib/css-scoper.js +13 -0
- package/lib/dev-server.js +19 -0
- package/lib/parser.js +1001 -109
- package/lib/printer.js +92 -78
- package/lib/reactive-runtime.js +1 -0
- package/lib/tree-walker.js +721 -43
- package/lib/types.js +215 -0
- package/lib/wcc-runtime.js +26 -0
- package/package.json +14 -9
- package/types/wcc.d.ts +27 -0
- package/types/wcc.test.js +46 -0
package/lib/printer.js
CHANGED
|
@@ -1,104 +1,118 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Pretty Printer —
|
|
2
|
+
* Pretty Printer — serializes a ParseResult IR back to valid .js source format.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Used for round-trip testing: parse → prettyPrint → parse should yield
|
|
5
|
+
* an equivalent IR.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
* Reconstruct the <template> block from the IR.
|
|
10
|
-
* Uses the original template string (before tree-walking) since it
|
|
11
|
-
* already contains {{var}} bindings, @event attributes, and <slot> elements.
|
|
12
|
-
*
|
|
13
|
-
* @param {import('./parser.js').ParseResult} ir
|
|
14
|
-
* @returns {string}
|
|
15
|
-
*/
|
|
16
|
-
function reconstructTemplate(ir) {
|
|
17
|
-
return ir.template;
|
|
18
|
-
}
|
|
8
|
+
/** @import { ParseResult } from './types.js' */
|
|
19
9
|
|
|
20
10
|
/**
|
|
21
|
-
*
|
|
11
|
+
* Pretty-print a ParseResult IR back to component source format.
|
|
22
12
|
*
|
|
23
|
-
* @param {
|
|
24
|
-
* @returns {string}
|
|
13
|
+
* @param {ParseResult} ir — The intermediate representation
|
|
14
|
+
* @returns {string} Reconstructed source code
|
|
25
15
|
*/
|
|
26
|
-
function
|
|
27
|
-
const
|
|
16
|
+
export function prettyPrint(ir) {
|
|
17
|
+
const sections = [];
|
|
28
18
|
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
19
|
+
// 1. Import statement — include only macros actually used
|
|
20
|
+
const macros = ['defineComponent'];
|
|
21
|
+
if ((ir.propDefs || []).length > 0) macros.push('defineProps');
|
|
22
|
+
if ((ir.emits || []).length > 0) macros.push('defineEmits');
|
|
23
|
+
if (ir.signals.length > 0) macros.push('signal');
|
|
24
|
+
if (ir.computeds.length > 0) macros.push('computed');
|
|
25
|
+
if (ir.effects.length > 0) macros.push('effect');
|
|
26
|
+
if ((ir.onMountHooks || []).length > 0) macros.push('onMount');
|
|
27
|
+
if ((ir.onDestroyHooks || []).length > 0) macros.push('onDestroy');
|
|
28
|
+
sections.push(`import { ${macros.join(', ')} } from 'wcc'`);
|
|
35
29
|
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
// 2. defineComponent call
|
|
31
|
+
const defParts = [];
|
|
32
|
+
defParts.push(` tag: '${ir.tagName}',`);
|
|
33
|
+
defParts.push(` template: './${ir.tagName}.html',`);
|
|
34
|
+
if (ir.style !== '') {
|
|
35
|
+
defParts.push(` styles: './${ir.tagName}.css',`);
|
|
39
36
|
}
|
|
40
|
-
|
|
37
|
+
sections.push(`export default defineComponent({\n${defParts.join('\n')}\n})`);
|
|
41
38
|
|
|
42
|
-
//
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
// 3. defineProps (if present)
|
|
40
|
+
if ((ir.propDefs || []).length > 0) {
|
|
41
|
+
const propsObjName = ir.propsObjectName || 'props';
|
|
42
|
+
const propEntries = ir.propDefs.map(p => `${p.name}: ${p.default}`);
|
|
43
|
+
sections.push(`const ${propsObjName} = defineProps({ ${propEntries.join(', ')} })`);
|
|
45
44
|
}
|
|
46
|
-
if (ir.computeds.length > 0) lines.push('');
|
|
47
45
|
|
|
48
|
-
//
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
lines.push(` ${bodyLine}`);
|
|
54
|
-
}
|
|
55
|
-
lines.push(' })');
|
|
46
|
+
// 3b. defineEmits (if present)
|
|
47
|
+
if ((ir.emits || []).length > 0) {
|
|
48
|
+
const emitsObjName = ir.emitsObjectName || 'emit';
|
|
49
|
+
const emitEntries = ir.emits.map(e => `'${e}'`).join(', ');
|
|
50
|
+
sections.push(`const ${emitsObjName} = defineEmits([${emitEntries}])`);
|
|
56
51
|
}
|
|
57
|
-
if (ir.watchers.length > 0) lines.push('');
|
|
58
52
|
|
|
59
|
-
//
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
lines.push(' }');
|
|
53
|
+
// 4. Signal declarations
|
|
54
|
+
if (ir.signals.length > 0) {
|
|
55
|
+
const signalLines = ir.signals.map(
|
|
56
|
+
s => `const ${s.name} = signal(${s.value})`
|
|
57
|
+
);
|
|
58
|
+
sections.push(signalLines.join('\n'));
|
|
66
59
|
}
|
|
67
60
|
|
|
68
|
-
|
|
69
|
-
|
|
61
|
+
// 5. Computed declarations
|
|
62
|
+
if (ir.computeds.length > 0) {
|
|
63
|
+
const computedLines = ir.computeds.map(
|
|
64
|
+
c => `const ${c.name} = computed(() => ${c.body})`
|
|
65
|
+
);
|
|
66
|
+
sections.push(computedLines.join('\n'));
|
|
67
|
+
}
|
|
70
68
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
69
|
+
// 6. Effect declarations
|
|
70
|
+
if (ir.effects.length > 0) {
|
|
71
|
+
const effectBlocks = ir.effects.map(e => {
|
|
72
|
+
const indentedBody = e.body
|
|
73
|
+
.split('\n')
|
|
74
|
+
.map(line => ` ${line}`)
|
|
75
|
+
.join('\n');
|
|
76
|
+
return `effect(() => {\n${indentedBody}\n})`;
|
|
77
|
+
});
|
|
78
|
+
sections.push(effectBlocks.join('\n\n'));
|
|
79
|
+
}
|
|
79
80
|
|
|
80
|
-
//
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
81
|
+
// 7. Function declarations
|
|
82
|
+
if (ir.methods.length > 0) {
|
|
83
|
+
const fnBlocks = ir.methods.map(m => {
|
|
84
|
+
const indentedBody = m.body
|
|
85
|
+
.split('\n')
|
|
86
|
+
.map(line => ` ${line}`)
|
|
87
|
+
.join('\n');
|
|
88
|
+
return `function ${m.name}(${m.params}) {\n${indentedBody}\n}`;
|
|
89
|
+
});
|
|
90
|
+
sections.push(fnBlocks.join('\n\n'));
|
|
91
|
+
}
|
|
84
92
|
|
|
85
|
-
//
|
|
86
|
-
if (ir.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
93
|
+
// 8. Lifecycle hooks — onMount
|
|
94
|
+
if ((ir.onMountHooks || []).length > 0) {
|
|
95
|
+
const mountBlocks = ir.onMountHooks.map(h => {
|
|
96
|
+
const indentedBody = h.body
|
|
97
|
+
.split('\n')
|
|
98
|
+
.map(line => ` ${line}`)
|
|
99
|
+
.join('\n');
|
|
100
|
+
return `onMount(() => {\n${indentedBody}\n})`;
|
|
101
|
+
});
|
|
102
|
+
sections.push(mountBlocks.join('\n\n'));
|
|
91
103
|
}
|
|
92
104
|
|
|
93
|
-
//
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
105
|
+
// 9. Lifecycle hooks — onDestroy
|
|
106
|
+
if ((ir.onDestroyHooks || []).length > 0) {
|
|
107
|
+
const destroyBlocks = ir.onDestroyHooks.map(h => {
|
|
108
|
+
const indentedBody = h.body
|
|
109
|
+
.split('\n')
|
|
110
|
+
.map(line => ` ${line}`)
|
|
111
|
+
.join('\n');
|
|
112
|
+
return `onDestroy(() => {\n${indentedBody}\n})`;
|
|
113
|
+
});
|
|
114
|
+
sections.push(destroyBlocks.join('\n\n'));
|
|
100
115
|
}
|
|
101
116
|
|
|
102
|
-
|
|
103
|
-
return parts.join('\n');
|
|
117
|
+
return sections.join('\n\n') + '\n';
|
|
104
118
|
}
|