@lithent/lithent-template-parser 0.1.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.
Files changed (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +159 -0
  3. package/dist/codegen/createElement.d.ts +10 -0
  4. package/dist/codegen/index.d.ts +1 -0
  5. package/dist/compiler.d.ts +26 -0
  6. package/dist/docPipe/index.d.ts +40 -0
  7. package/dist/docPipe/scan.d.ts +6 -0
  8. package/dist/index.d.ts +13 -0
  9. package/dist/index.mjs +2302 -0
  10. package/dist/index.mjs.map +1 -0
  11. package/dist/index.umd.cjs +13 -0
  12. package/dist/index.umd.cjs.map +1 -0
  13. package/dist/parser/ast.d.ts +207 -0
  14. package/dist/parser/lexer.d.ts +109 -0
  15. package/dist/parser/parser.d.ts +79 -0
  16. package/dist/parser/tokens.d.ts +51 -0
  17. package/dist/transform/directives.d.ts +30 -0
  18. package/dist/transform/index.d.ts +14 -0
  19. package/dist/transform/optimize.d.ts +21 -0
  20. package/package.json +53 -0
  21. package/src/codegen/createElement.ts +661 -0
  22. package/src/codegen/index.ts +1 -0
  23. package/src/compiler.ts +70 -0
  24. package/src/docPipe/index.ts +104 -0
  25. package/src/docPipe/scan.ts +318 -0
  26. package/src/index.ts +16 -0
  27. package/src/parser/ast.ts +478 -0
  28. package/src/parser/lexer.ts +561 -0
  29. package/src/parser/parser.ts +514 -0
  30. package/src/parser/tokens.ts +84 -0
  31. package/src/playground/main.ts +206 -0
  32. package/src/playground/style.css +135 -0
  33. package/src/shims/babel-env.ts +17 -0
  34. package/src/tests/compiler.test.ts +434 -0
  35. package/src/tests/docPipe.test.ts +41 -0
  36. package/src/tests/helpers.ts +96 -0
  37. package/src/tests/lexer.test.ts +282 -0
  38. package/src/tests/parser.test.ts +691 -0
  39. package/src/transform/directives.ts +160 -0
  40. package/src/transform/index.ts +29 -0
  41. package/src/transform/optimize.ts +203 -0
@@ -0,0 +1,282 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { tokenize } from '../parser/lexer';
3
+ import { TokenType } from '../parser/tokens';
4
+
5
+ describe('Lexer', () => {
6
+ describe('Basic tags', () => {
7
+ it('should tokenize simple opening tag', () => {
8
+ const tokens = tokenize('<div>');
9
+ expect(tokens[0].type).toBe(TokenType.TAG_OPEN_START);
10
+ expect(tokens[1].type).toBe(TokenType.IDENTIFIER);
11
+ expect(tokens[1].value).toBe('div');
12
+ expect(tokens[2].type).toBe(TokenType.TAG_OPEN_END);
13
+ });
14
+
15
+ it('should tokenize self-closing tag', () => {
16
+ const tokens = tokenize('<img />');
17
+ const filtered = tokens.filter(t => t.type !== TokenType.WHITESPACE);
18
+
19
+ expect(filtered[0].type).toBe(TokenType.TAG_OPEN_START);
20
+ expect(filtered[1].type).toBe(TokenType.IDENTIFIER);
21
+ expect(filtered[1].value).toBe('img');
22
+ expect(filtered[2].type).toBe(TokenType.TAG_SELF_CLOSE);
23
+ });
24
+
25
+ it('should tokenize closing tag', () => {
26
+ const tokens = tokenize('</div>');
27
+ expect(tokens[0].type).toBe(TokenType.TAG_CLOSE_START);
28
+ expect(tokens[1].type).toBe(TokenType.IDENTIFIER);
29
+ expect(tokens[1].value).toBe('div');
30
+ expect(tokens[2].type).toBe(TokenType.TAG_OPEN_END);
31
+ });
32
+
33
+ it('should tokenize component (PascalCase)', () => {
34
+ const tokens = tokenize('<MyComponent>');
35
+ expect(tokens[1].type).toBe(TokenType.IDENTIFIER);
36
+ expect(tokens[1].value).toBe('MyComponent');
37
+ });
38
+
39
+ it('should tokenize compound component tag', () => {
40
+ const tokens = tokenize('<Card.Header>');
41
+ expect(tokens[1].type).toBe(TokenType.IDENTIFIER);
42
+ expect(tokens[1].value).toBe('Card.Header');
43
+ });
44
+ });
45
+
46
+ describe('Attributes', () => {
47
+ it('should tokenize static attribute', () => {
48
+ const tokens = tokenize('<div class="container">');
49
+ const filtered = tokens.filter(t => t.type !== TokenType.WHITESPACE);
50
+
51
+ expect(filtered[2].type).toBe(TokenType.IDENTIFIER);
52
+ expect(filtered[2].value).toBe('class');
53
+ expect(filtered[3].type).toBe(TokenType.ATTRIBUTE_EQUALS);
54
+ expect(filtered[4].type).toBe(TokenType.STRING_LITERAL);
55
+ expect(filtered[4].value).toBe('"container"');
56
+ });
57
+
58
+ it('should tokenize dynamic attribute', () => {
59
+ const tokens = tokenize('<div class={className}>');
60
+ const filtered = tokens.filter(t => t.type !== TokenType.WHITESPACE);
61
+
62
+ expect(filtered[2].type).toBe(TokenType.IDENTIFIER);
63
+ expect(filtered[2].value).toBe('class');
64
+ expect(filtered[3].type).toBe(TokenType.ATTRIBUTE_EQUALS);
65
+ expect(filtered[4].type).toBe(TokenType.EXPRESSION_START);
66
+ expect(filtered[5].type).toBe(TokenType.EXPRESSION_CONTENT);
67
+ expect(filtered[5].value).toBe('className');
68
+ expect(filtered[6].type).toBe(TokenType.EXPRESSION_END);
69
+ });
70
+
71
+ it('should tokenize ref attribute', () => {
72
+ const tokens = tokenize('<div ref={myRef}>');
73
+ const filtered = tokens.filter(t => t.type !== TokenType.WHITESPACE);
74
+
75
+ expect(filtered[2].type).toBe(TokenType.KEYWORD_REF);
76
+ expect(filtered[2].value).toBe('ref');
77
+ });
78
+
79
+ it('should tokenize template literal expressions', () => {
80
+ const tokens = tokenize('<div class={`greet-${name}`} />');
81
+ const filtered = tokens.filter(t => t.type !== TokenType.WHITESPACE);
82
+
83
+ const exprContent = filtered.find(
84
+ t => t.type === TokenType.EXPRESSION_CONTENT
85
+ );
86
+ expect(exprContent?.value).toContain('greet-${name}');
87
+ });
88
+
89
+ it('should tokenize nested template literal expressions', () => {
90
+ const template =
91
+ '<div data-text={`outer ${items.map(item => `inner ${item}`)} end`} />';
92
+ const tokens = tokenize(template);
93
+ const filtered = tokens.filter(t => t.type !== TokenType.WHITESPACE);
94
+
95
+ const exprContent = filtered.find(
96
+ t => t.type === TokenType.EXPRESSION_CONTENT
97
+ );
98
+ expect(exprContent?.value).toContain('items.map');
99
+ expect(exprContent?.value).toContain('inner ${item}');
100
+ expect(exprContent?.value.trim().endsWith('end`')).toBe(true);
101
+ });
102
+ });
103
+
104
+ describe('Directives', () => {
105
+ it('should tokenize l-if directive', () => {
106
+ const tokens = tokenize('<div l-if={count > 0}>');
107
+ const filtered = tokens.filter(t => t.type !== TokenType.WHITESPACE);
108
+
109
+ expect(filtered[2].type).toBe(TokenType.DIRECTIVE_IF);
110
+ expect(filtered[2].value).toBe('l-if');
111
+ expect(filtered[4].type).toBe(TokenType.EXPRESSION_START);
112
+ expect(filtered[5].value.trim()).toBe('count > 0');
113
+ });
114
+
115
+ it('should tokenize l-else-if directive', () => {
116
+ const tokens = tokenize('<div l-else-if={count === 0}>');
117
+ const filtered = tokens.filter(t => t.type !== TokenType.WHITESPACE);
118
+
119
+ expect(filtered[2].type).toBe(TokenType.DIRECTIVE_ELSE_IF);
120
+ expect(filtered[2].value).toBe('l-else-if');
121
+ });
122
+
123
+ it('should tokenize l-else directive', () => {
124
+ const tokens = tokenize('<div l-else>');
125
+ const filtered = tokens.filter(t => t.type !== TokenType.WHITESPACE);
126
+
127
+ expect(filtered[2].type).toBe(TokenType.DIRECTIVE_ELSE);
128
+ expect(filtered[2].value).toBe('l-else');
129
+ });
130
+
131
+ it('should tokenize l-for directive', () => {
132
+ const tokens = tokenize('<div l-for={item in items}>');
133
+ const filtered = tokens.filter(t => t.type !== TokenType.WHITESPACE);
134
+
135
+ expect(filtered[2].type).toBe(TokenType.DIRECTIVE_FOR);
136
+ expect(filtered[2].value).toBe('l-for');
137
+ expect(filtered[5].value.trim()).toBe('item in items');
138
+ });
139
+ });
140
+
141
+ describe('Fragments', () => {
142
+ it('should tokenize empty fragment', () => {
143
+ const tokens = tokenize('<></>');
144
+ const filtered = tokens.filter(t => t.type !== TokenType.WHITESPACE);
145
+
146
+ expect(filtered[0].type).toBe(TokenType.FRAGMENT_OPEN);
147
+ expect(filtered[1].type).toBe(TokenType.FRAGMENT_CLOSE);
148
+ });
149
+
150
+ it('should tokenize fragment with children', () => {
151
+ const tokens = tokenize('<><span>Hi</span></>');
152
+ const filtered = tokens.filter(t => t.type !== TokenType.WHITESPACE);
153
+
154
+ expect(filtered[0].type).toBe(TokenType.FRAGMENT_OPEN);
155
+ expect(filtered[1].type).toBe(TokenType.TAG_OPEN_START);
156
+ expect(filtered.some(t => t.type === TokenType.FRAGMENT_CLOSE)).toBe(
157
+ true
158
+ );
159
+ });
160
+ });
161
+
162
+ describe('Text and interpolation', () => {
163
+ it('should tokenize plain text', () => {
164
+ const tokens = tokenize('Hello World');
165
+ expect(tokens[0].type).toBe(TokenType.TEXT);
166
+ expect(tokens[0].value).toBe('Hello World');
167
+ });
168
+
169
+ it('should tokenize interpolation', () => {
170
+ const tokens = tokenize('{message}');
171
+ expect(tokens[0].type).toBe(TokenType.EXPRESSION_START);
172
+ expect(tokens[1].type).toBe(TokenType.EXPRESSION_CONTENT);
173
+ expect(tokens[1].value).toBe('message');
174
+ expect(tokens[2].type).toBe(TokenType.EXPRESSION_END);
175
+ });
176
+
177
+ it('should tokenize mixed text and interpolation', () => {
178
+ const tokens = tokenize('Hello {name}!');
179
+ expect(tokens[0].type).toBe(TokenType.TEXT);
180
+ expect(tokens[0].value).toBe('Hello ');
181
+ expect(tokens[1].type).toBe(TokenType.EXPRESSION_START);
182
+ expect(tokens[2].type).toBe(TokenType.EXPRESSION_CONTENT);
183
+ expect(tokens[2].value).toBe('name');
184
+ expect(tokens[3].type).toBe(TokenType.EXPRESSION_END);
185
+ expect(tokens[4].type).toBe(TokenType.TEXT);
186
+ expect(tokens[4].value).toBe('!');
187
+ });
188
+
189
+ it('should handle nested braces in expressions', () => {
190
+ const tokens = tokenize('{items.map(i => { return i.name })}');
191
+ const filtered = tokens.filter(t => t.type !== TokenType.WHITESPACE);
192
+
193
+ expect(filtered[0].type).toBe(TokenType.EXPRESSION_START);
194
+ expect(filtered[1].type).toBe(TokenType.EXPRESSION_CONTENT);
195
+ expect(filtered[1].value).toContain('items.map');
196
+ expect(filtered[2].type).toBe(TokenType.EXPRESSION_END);
197
+ });
198
+ });
199
+
200
+ describe('Comments', () => {
201
+ it('should tokenize HTML comments', () => {
202
+ const tokens = tokenize('<!-- This is a comment -->');
203
+ expect(tokens[0].type).toBe(TokenType.COMMENT);
204
+ expect(tokens[0].value).toBe('<!-- This is a comment -->');
205
+ });
206
+ });
207
+
208
+ describe('Complex templates', () => {
209
+ it('should tokenize complete component', () => {
210
+ const template = `
211
+ <div class="container">
212
+ <h1>{title}</h1>
213
+ <ul>
214
+ <li l-for={item in items}>
215
+ {item.name}
216
+ </li>
217
+ </ul>
218
+ </div>
219
+ `.trim();
220
+
221
+ const tokens = tokenize(template);
222
+
223
+ // Should have proper structure
224
+ expect(tokens[0].type).toBe(TokenType.TAG_OPEN_START);
225
+ expect(tokens.some(t => t.type === TokenType.DIRECTIVE_FOR)).toBe(true);
226
+ expect(
227
+ tokens.some(
228
+ t =>
229
+ t.type === TokenType.EXPRESSION_CONTENT && t.value.includes('title')
230
+ )
231
+ ).toBe(true);
232
+ expect(tokens[tokens.length - 1].type).toBe(TokenType.EOF);
233
+ });
234
+
235
+ it('should handle conditional rendering', () => {
236
+ const template = `
237
+ <div l-if={isLoading}>Loading...</div>
238
+ <div l-else-if={hasError}>Error</div>
239
+ <div l-else>Content</div>
240
+ `.trim();
241
+
242
+ const tokens = tokenize(template);
243
+ const directives = tokens.filter(
244
+ t =>
245
+ t.type === TokenType.DIRECTIVE_IF ||
246
+ t.type === TokenType.DIRECTIVE_ELSE_IF ||
247
+ t.type === TokenType.DIRECTIVE_ELSE
248
+ );
249
+
250
+ expect(directives).toHaveLength(3);
251
+ expect(directives[0].type).toBe(TokenType.DIRECTIVE_IF);
252
+ expect(directives[1].type).toBe(TokenType.DIRECTIVE_ELSE_IF);
253
+ expect(directives[2].type).toBe(TokenType.DIRECTIVE_ELSE);
254
+ });
255
+ });
256
+
257
+ describe('Edge cases', () => {
258
+ it('should handle empty template', () => {
259
+ const tokens = tokenize('');
260
+ expect(tokens).toHaveLength(1);
261
+ expect(tokens[0].type).toBe(TokenType.EOF);
262
+ });
263
+
264
+ it('should handle whitespace only', () => {
265
+ const tokens = tokenize(' \n \t ');
266
+ expect(tokens.some(t => t.type === TokenType.WHITESPACE)).toBe(true);
267
+ });
268
+
269
+ it('should handle quoted strings with special characters', () => {
270
+ const tokens = tokenize('<div title="Hello \\"World\\"">');
271
+ const stringToken = tokens.find(t => t.type === TokenType.STRING_LITERAL);
272
+ expect(stringToken?.value).toContain('Hello');
273
+ });
274
+
275
+ it('should recover from invalid attribute characters', () => {
276
+ const template = '<div foo?="bar"></div>';
277
+ const tokens = tokenize(template);
278
+
279
+ expect(tokens[tokens.length - 1].type).toBe(TokenType.EOF);
280
+ });
281
+ });
282
+ });