@lithent/lithent-template-parser 0.1.0 → 0.1.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 +1 -1
- package/dist/codegen/createElement.d.cts +10 -0
- package/dist/codegen/createElement.d.ts +1 -1
- package/dist/codegen/index.d.cts +1 -0
- package/dist/codegen/index.d.ts +1 -1
- package/dist/compiler.d.cts +26 -0
- package/dist/compiler.d.ts +8 -8
- package/dist/docPipe/index.d.cts +40 -0
- package/dist/docPipe/index.d.ts +2 -2
- package/dist/docPipe/scan.d.cts +8 -0
- package/dist/docPipe/scan.d.ts +2 -0
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +9 -9
- package/dist/index.mjs +955 -1192
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.cjs +12 -12
- package/dist/index.umd.cjs.map +1 -1
- package/dist/parser/ast.d.cts +207 -0
- package/dist/parser/ast.d.ts +1 -1
- package/dist/parser/lexer.d.cts +109 -0
- package/dist/parser/lexer.d.ts +1 -1
- package/dist/parser/parser.d.cts +80 -0
- package/dist/parser/parser.d.ts +5 -4
- package/dist/parser/tokens.d.cts +51 -0
- package/dist/transform/directives.d.cts +30 -0
- package/dist/transform/directives.d.ts +1 -1
- package/dist/transform/index.d.cts +14 -0
- package/dist/transform/index.d.ts +5 -5
- package/dist/transform/optimize.d.cts +21 -0
- package/dist/transform/optimize.d.ts +1 -1
- package/package.json +2 -2
- package/src/codegen/createElement.ts +48 -332
- package/src/compiler.ts +1 -1
- package/src/docPipe/scan.ts +209 -246
- package/src/parser/lexer.ts +41 -91
- package/src/parser/parser.ts +45 -39
- package/src/tests/compiler.test.ts +2 -2
- package/src/tests/parser.test.ts +2 -2
- package/src/tests/regressions.test.ts +200 -0
- package/src/transform/directives.ts +25 -2
- package/src/transform/optimize.ts +37 -25
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
NodeType,
|
|
8
8
|
createTextNode,
|
|
9
9
|
} from '../parser/ast';
|
|
10
|
+
import { getConditionalGroup } from './directives';
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Optimize the AST
|
|
@@ -48,10 +49,27 @@ function optimizeNodeList(
|
|
|
48
49
|
result = mergeTextNodes(result);
|
|
49
50
|
}
|
|
50
51
|
|
|
51
|
-
//
|
|
52
|
+
// Only discard multiline indentation; a single space between inline nodes is meaningful.
|
|
52
53
|
if (options.removeWhitespace) {
|
|
53
54
|
result = removeWhitespaceNodes(result);
|
|
54
55
|
}
|
|
56
|
+
if (options.trimText) {
|
|
57
|
+
result = result.map((node, index) =>
|
|
58
|
+
node.type === NodeType.TEXT
|
|
59
|
+
? {
|
|
60
|
+
...node,
|
|
61
|
+
content:
|
|
62
|
+
result.length === 1
|
|
63
|
+
? node.content.trim()
|
|
64
|
+
: index === 0
|
|
65
|
+
? node.content.trimStart()
|
|
66
|
+
: index === result.length - 1
|
|
67
|
+
? node.content.trimEnd()
|
|
68
|
+
: node.content,
|
|
69
|
+
}
|
|
70
|
+
: node
|
|
71
|
+
);
|
|
72
|
+
}
|
|
55
73
|
|
|
56
74
|
return result;
|
|
57
75
|
}
|
|
@@ -72,7 +90,7 @@ function optimizeNode(
|
|
|
72
90
|
}
|
|
73
91
|
|
|
74
92
|
if (node.type === NodeType.TEXT) {
|
|
75
|
-
return
|
|
93
|
+
return node;
|
|
76
94
|
}
|
|
77
95
|
|
|
78
96
|
return node;
|
|
@@ -85,10 +103,24 @@ function optimizeElementNode(
|
|
|
85
103
|
node: ElementNode,
|
|
86
104
|
options: Required<OptimizeOptions>
|
|
87
105
|
): ElementNode {
|
|
88
|
-
|
|
106
|
+
const childOptions = /^(pre|textarea)$/.test(node.tag)
|
|
107
|
+
? { ...options, trimText: false, removeWhitespace: false }
|
|
108
|
+
: options;
|
|
109
|
+
const result = {
|
|
89
110
|
...node,
|
|
90
|
-
children: optimizeNodeList(node.children,
|
|
111
|
+
children: optimizeNodeList(node.children, childOptions),
|
|
91
112
|
};
|
|
113
|
+
const group = getConditionalGroup(node);
|
|
114
|
+
if (group) {
|
|
115
|
+
(result as any).__conditionalGroup = {
|
|
116
|
+
if: optimizeElementNode(group.if, options),
|
|
117
|
+
elseIfs: group.elseIfs.map(branch =>
|
|
118
|
+
optimizeElementNode(branch, options)
|
|
119
|
+
),
|
|
120
|
+
else: group.else ? optimizeElementNode(group.else, options) : undefined,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return result;
|
|
92
124
|
}
|
|
93
125
|
|
|
94
126
|
/**
|
|
@@ -104,26 +136,6 @@ function optimizeFragmentNode(
|
|
|
104
136
|
};
|
|
105
137
|
}
|
|
106
138
|
|
|
107
|
-
/**
|
|
108
|
-
* Optimize a text node
|
|
109
|
-
*/
|
|
110
|
-
function optimizeTextNode(
|
|
111
|
-
node: TextNode,
|
|
112
|
-
options: Required<OptimizeOptions>
|
|
113
|
-
): TextNode {
|
|
114
|
-
let content = node.content;
|
|
115
|
-
|
|
116
|
-
// Trim text if enabled
|
|
117
|
-
if (options.trimText) {
|
|
118
|
-
content = content.trim();
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return {
|
|
122
|
-
...node,
|
|
123
|
-
content,
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
139
|
/**
|
|
128
140
|
* Merge consecutive text nodes
|
|
129
141
|
*/
|
|
@@ -167,7 +179,7 @@ function mergeTextNodes(nodes: TemplateNode[]): TemplateNode[] {
|
|
|
167
179
|
function removeWhitespaceNodes(nodes: TemplateNode[]): TemplateNode[] {
|
|
168
180
|
return nodes.filter(node => {
|
|
169
181
|
if (node.type === NodeType.TEXT) {
|
|
170
|
-
return node.content.trim().length > 0;
|
|
182
|
+
return node.content.trim().length > 0 || !/[\r\n]/.test(node.content);
|
|
171
183
|
}
|
|
172
184
|
return true;
|
|
173
185
|
});
|