what-compiler 0.13.11 → 0.15.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/LICENSE +21 -0
- package/README.md +181 -2
- package/babel.d.ts +78 -2
- package/dist/babel/auto-selector.min.js +94 -0
- package/dist/babel/common.min.js +2053 -0
- package/dist/babel/dev-metadata.min.js +106 -0
- package/dist/babel/dom.min.js +1669 -0
- package/dist/babel/jsx-root.min.js +13 -0
- package/dist/babel/lists.min.js +492 -0
- package/dist/babel/nesting.min.js +328 -0
- package/dist/babel/reactivity.min.js +1218 -0
- package/dist/babel/server-actions.min.js +85 -0
- package/dist/babel/ssr.min.js +496 -0
- package/dist/babel/types.min.js +9 -0
- package/dist/babel-plugin.min.js +101 -1
- package/dist/file-router.min.js +368 -12
- package/dist/framework-files.min.js +62 -0
- package/dist/hot.min.js +315 -0
- package/dist/index.min.js +13 -414
- package/dist/release.min.js +78 -0
- package/dist/runtime.min.js +9 -1
- package/dist/tables/import-changes.min.js +272 -0
- package/dist/tables/markup-rules.min.js +213 -0
- package/dist/vite-plugin.min.js +432 -399
- package/file-router.d.ts +3 -1
- package/index.d.ts +1 -1
- package/package.json +11 -2
- package/runtime.d.ts +2 -1
- package/src/babel/auto-selector.js +248 -0
- package/src/babel/common.js +3800 -0
- package/src/babel/dev-metadata.js +167 -0
- package/src/babel/dom.js +3386 -0
- package/src/babel/jsx-root.js +27 -0
- package/src/babel/lists.js +704 -0
- package/src/babel/nesting.js +260 -0
- package/src/babel/reactivity.js +1998 -0
- package/src/babel/server-actions.js +144 -0
- package/src/babel/ssr.js +870 -0
- package/src/babel/types.js +10 -0
- package/src/babel-plugin.js +185 -3598
- package/src/file-router.js +58 -38
- package/src/framework-files.js +106 -0
- package/src/hot.js +523 -0
- package/src/release.js +127 -0
- package/src/runtime.js +7 -7
- package/src/tables/import-changes.js +157 -0
- package/src/tables/markup-rules.js +388 -0
- package/src/vite-plugin.js +460 -175
- package/vite.d.ts +47 -3
- package/src/error-overlay.js +0 -428
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
// Compile-time HTML nesting rules.
|
|
2
|
+
//
|
|
3
|
+
// Static markup is parsed by a real HTML parser: the DOM back end's templates
|
|
4
|
+
// through innerHTML, the server back end's output by the browser that receives
|
|
5
|
+
// it. The parser restructures some nestings instead of keeping them, so the
|
|
6
|
+
// tree it builds no longer matches the JSX. Compiled DOM output then walks a
|
|
7
|
+
// tree with different children (ERR_INVALID_HTML_NESTING at first render, in
|
|
8
|
+
// development only), and server markup hydrates against the wrong nodes. Every
|
|
9
|
+
// rule below is one the parser actually enforces; content-model rules it does
|
|
10
|
+
// not enforce (a <div> in a <span>, a <button> in an <a>) are not errors here.
|
|
11
|
+
//
|
|
12
|
+
// Only nesting fixed by the source is checked: elements written inside
|
|
13
|
+
// elements in one JSX tree, through fragments, in the HTML namespace. A
|
|
14
|
+
// component starts a new tree, because what it renders is not known here. So
|
|
15
|
+
// does JSX in an expression container for the DOM back end, which inserts it
|
|
16
|
+
// at run time; the server back end writes it into its parent's markup, so
|
|
17
|
+
// there an element written as a child expression (`{ok && <div/>}`, the rows
|
|
18
|
+
// of `{rows.map(r => <tr/>)}`) is checked where it stands, and so is static
|
|
19
|
+
// text written as one (`{"x"}`).
|
|
20
|
+
|
|
21
|
+
import { VOID_ELEMENTS } from '../tables/markup-rules.js';
|
|
22
|
+
import { t } from './types.js';
|
|
23
|
+
import { codedCompileError, isComponentElement, normalizeJsxText } from './common.js';
|
|
24
|
+
|
|
25
|
+
// Start tags that close an open <p> ("close a p element" in the HTML parser).
|
|
26
|
+
const CLOSES_P = new Set([
|
|
27
|
+
'address', 'article', 'aside', 'blockquote', 'center', 'details', 'dialog', 'dir', 'div', 'dl',
|
|
28
|
+
'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
|
|
29
|
+
'header', 'hgroup', 'hr', 'listing', 'main', 'menu', 'nav', 'ol', 'p', 'plaintext', 'pre',
|
|
30
|
+
'search', 'section', 'summary', 'table', 'ul', 'xmp',
|
|
31
|
+
]);
|
|
32
|
+
const HEADINGS = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']);
|
|
33
|
+
// Elements that end the parser's "in scope" searches.
|
|
34
|
+
const SCOPE_BOUNDARIES = new Set(['applet', 'caption', 'html', 'table', 'td', 'th', 'marquee', 'object', 'template']);
|
|
35
|
+
// The "special" category, which stops the search an <li>, <dd> or <dt> start
|
|
36
|
+
// tag makes for an open element of its own kind (address, div and p excepted).
|
|
37
|
+
const SPECIAL = new Set([
|
|
38
|
+
'applet', 'area', 'article', 'aside', 'base', 'basefont', 'bgsound', 'blockquote', 'body', 'br',
|
|
39
|
+
'button', 'caption', 'center', 'col', 'colgroup', 'dd', 'details', 'dir', 'dl', 'dt', 'embed',
|
|
40
|
+
'fieldset', 'figcaption', 'figure', 'footer', 'form', 'frame', 'frameset', 'h1', 'h2', 'h3',
|
|
41
|
+
'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html', 'iframe', 'img', 'input', 'keygen',
|
|
42
|
+
'li', 'link', 'listing', 'main', 'marquee', 'menu', 'meta', 'nav', 'noembed', 'noframes',
|
|
43
|
+
'noscript', 'object', 'ol', 'param', 'plaintext', 'pre', 'script', 'search', 'section', 'select',
|
|
44
|
+
'source', 'style', 'summary', 'table', 'tbody', 'td', 'template', 'textarea', 'tfoot', 'th',
|
|
45
|
+
'thead', 'title', 'tr', 'track', 'ul', 'wbr', 'xmp',
|
|
46
|
+
]);
|
|
47
|
+
// Elements whose content the parser reads as text, so an element inside one
|
|
48
|
+
// becomes literal text.
|
|
49
|
+
const TEXT_ONLY = new Set(['textarea', 'title', 'style', 'script', 'xmp', 'iframe', 'noembed', 'noframes', 'plaintext']);
|
|
50
|
+
// Table structure: what each table element may hold, and the elements that are
|
|
51
|
+
// only valid inside one of those.
|
|
52
|
+
const TABLE_CONTENT = {
|
|
53
|
+
table: new Set(['caption', 'colgroup', 'thead', 'tbody', 'tfoot', 'script', 'template', 'style']),
|
|
54
|
+
thead: new Set(['tr', 'script', 'template', 'style']),
|
|
55
|
+
tbody: new Set(['tr', 'script', 'template', 'style']),
|
|
56
|
+
tfoot: new Set(['tr', 'script', 'template', 'style']),
|
|
57
|
+
tr: new Set(['td', 'th', 'script', 'template', 'style']),
|
|
58
|
+
colgroup: new Set(['col', 'template']),
|
|
59
|
+
};
|
|
60
|
+
const TABLE_PARENT = {
|
|
61
|
+
caption: 'table', colgroup: 'table', thead: 'table', tbody: 'table', tfoot: 'table',
|
|
62
|
+
tr: 'a table section (<tbody>, <thead> or <tfoot>)', td: '<tr>', th: '<tr>', col: '<colgroup>',
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
function fail(node, state, parent, child, why) {
|
|
66
|
+
return codedCompileError(node, state, { code: 'ERR_INVALID_HTML_NESTING' },
|
|
67
|
+
`<${parent}> cannot contain <${child}>: ${why} ` +
|
|
68
|
+
'The browser would build a different tree than this JSX describes. ' +
|
|
69
|
+
"Run what_fix({ error: 'ERR_INVALID_HTML_NESTING' }) for the worked example.");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// HTML start tags that end SVG or MathML content wherever they appear in it
|
|
73
|
+
// (outside an HTML integration point): the parser closes the foreign element
|
|
74
|
+
// and creates the tag as HTML beside it.
|
|
75
|
+
const BREAKS_FOREIGN_CONTENT = new Set([
|
|
76
|
+
'b', 'big', 'blockquote', 'body', 'br', 'center', 'code', 'dd', 'div', 'dl', 'dt', 'em', 'embed',
|
|
77
|
+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'hr', 'i', 'img', 'li', 'listing', 'menu', 'meta',
|
|
78
|
+
'nobr', 'ol', 'p', 'pre', 'ruby', 's', 'small', 'span', 'strong', 'strike', 'sub', 'sup', 'table',
|
|
79
|
+
'tt', 'u', 'ul', 'var',
|
|
80
|
+
]);
|
|
81
|
+
const TABLE_SECTIONS = new Set(['thead', 'tbody', 'tfoot']);
|
|
82
|
+
|
|
83
|
+
// <input type="hidden"> is the one input a table holds in place: any other is
|
|
84
|
+
// moved in front of the table.
|
|
85
|
+
function isHiddenInput(node) {
|
|
86
|
+
return node.openingElement.attributes.some((attr) => t.isJSXAttribute(attr)
|
|
87
|
+
&& t.isJSXIdentifier(attr.name, { name: 'type' })
|
|
88
|
+
&& t.isStringLiteral(attr.value) && attr.value.value.toLowerCase() === 'hidden');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// <font> with a color, face or size attribute ends SVG or MathML content like
|
|
92
|
+
// the tags in BREAKS_FOREIGN_CONTENT do. Only a static attribute is certain
|
|
93
|
+
// to be written.
|
|
94
|
+
function isBreakoutFont(node) {
|
|
95
|
+
return node.openingElement.attributes.some((attr) => t.isJSXAttribute(attr)
|
|
96
|
+
&& t.isJSXIdentifier(attr.name) && ['color', 'face', 'size'].includes(attr.name.name.toLowerCase())
|
|
97
|
+
&& (!attr.value || t.isStringLiteral(attr.value)));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function describeAllowed(set) {
|
|
101
|
+
return [...set].filter(n => n !== 'script' && n !== 'template' && n !== 'style').map(n => `<${n}>`).join(', ');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// `stack` is the chain of open HTML elements in this tree, outermost first.
|
|
105
|
+
function checkElement(node, stack, state) {
|
|
106
|
+
const tag = node.openingElement.name.name;
|
|
107
|
+
const parent = stack.length > 0 ? stack[stack.length - 1].tag : null;
|
|
108
|
+
|
|
109
|
+
if (parent !== null) {
|
|
110
|
+
if (TEXT_ONLY.has(parent)) throw fail(node, state, parent, tag, `the parser reads the content of <${parent}> as text, so the element becomes literal characters.`);
|
|
111
|
+
if (VOID_ELEMENTS.has(parent)) throw fail(node, state, parent, tag, `<${parent}> is a void element and has no content.`);
|
|
112
|
+
const allowed = TABLE_CONTENT[parent];
|
|
113
|
+
if (allowed && !allowed.has(tag) && !(tag === 'input' && parent !== 'colgroup' && isHiddenInput(node))) {
|
|
114
|
+
const why = tag === 'tr' && parent === 'table' ? 'the parser inserts a <tbody> around the row. Write the <tbody> yourself.'
|
|
115
|
+
: (tag === 'td' || tag === 'th') && TABLE_SECTIONS.has(parent) ? 'the parser inserts a <tr> around the cell. Write the <tr> yourself.'
|
|
116
|
+
: `the parser moves it out of the table. <${parent}> holds only ${describeAllowed(allowed)}.`;
|
|
117
|
+
throw fail(node, state, parent, tag, why);
|
|
118
|
+
}
|
|
119
|
+
if (TABLE_PARENT[tag] && !TABLE_CONTENT[parent] && parent !== 'template') {
|
|
120
|
+
throw fail(node, state, parent, tag, `outside its table parent the parser drops the <${tag}> tag. It belongs in ${TABLE_PARENT[tag]}.`);
|
|
121
|
+
}
|
|
122
|
+
if (HEADINGS.has(tag) && HEADINGS.has(parent)) throw fail(node, state, parent, tag, 'a heading start tag closes the heading that is open.');
|
|
123
|
+
if (tag === 'option' && parent === 'option') throw fail(node, state, parent, tag, 'an <option> start tag closes the open <option>.');
|
|
124
|
+
if ((tag === 'rp' || tag === 'rt') && (parent === 'rb' || parent === 'rp' || parent === 'rt') && stack.some(open => open.tag === 'ruby')) {
|
|
125
|
+
throw fail(node, state, parent, tag, `an <${tag}> start tag inside a <ruby> closes the open <${parent}>.`);
|
|
126
|
+
}
|
|
127
|
+
if (tag === 'optgroup' && (parent === 'option' || parent === 'optgroup')) throw fail(node, state, parent, tag, `an <optgroup> start tag closes the open <${parent}>.`);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (CLOSES_P.has(tag)) {
|
|
131
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
132
|
+
const open = stack[i].tag;
|
|
133
|
+
if (open === 'p') throw fail(node, state, 'p', tag, `<p> holds only phrasing content, and a <${tag}> start tag closes it.`);
|
|
134
|
+
if (open === 'button' || SCOPE_BOUNDARIES.has(open)) break;
|
|
135
|
+
// Inside a <select>, an <hr> is a separator, not a paragraph break.
|
|
136
|
+
if (tag === 'hr' && open === 'select') break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (tag === 'nobr') {
|
|
140
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
141
|
+
const open = stack[i].tag;
|
|
142
|
+
if (open === 'nobr') throw fail(node, state, 'nobr', 'nobr', 'a <nobr> start tag closes the open <nobr>.');
|
|
143
|
+
if (SCOPE_BOUNDARIES.has(open)) break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (tag === 'a') {
|
|
147
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
148
|
+
const open = stack[i].tag;
|
|
149
|
+
if (open === 'a') throw fail(node, state, 'a', 'a', 'a link cannot hold another link; the parser closes the outer one.');
|
|
150
|
+
if (open === 'td' || open === 'th' || open === 'caption' || open === 'object' || open === 'applet' || open === 'marquee' || open === 'template') break;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (tag === 'form') {
|
|
154
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
155
|
+
const open = stack[i].tag;
|
|
156
|
+
if (open === 'form') throw fail(node, state, 'form', 'form', 'the parser ignores a <form> start tag inside an open form.');
|
|
157
|
+
if (open === 'template') break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (tag === 'button') {
|
|
161
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
162
|
+
const open = stack[i].tag;
|
|
163
|
+
if (open === 'button') throw fail(node, state, 'button', 'button', 'a <button> start tag closes the open button.');
|
|
164
|
+
if (SCOPE_BOUNDARIES.has(open)) break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (tag === 'li' || tag === 'dd' || tag === 'dt') {
|
|
168
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
169
|
+
const open = stack[i].tag;
|
|
170
|
+
if (tag === 'li' ? open === 'li' : open === 'dd' || open === 'dt') {
|
|
171
|
+
throw fail(node, state, open, tag, `a <${tag}> start tag closes the open <${open}>; nest a list between them.`);
|
|
172
|
+
}
|
|
173
|
+
if (SPECIAL.has(open) && open !== 'address' && open !== 'div' && open !== 'p') break;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (tag === 'select' && stack.some(open => open.tag === 'select')) {
|
|
177
|
+
throw fail(node, state, 'select', 'select', 'a <select> start tag inside a select closes it.');
|
|
178
|
+
}
|
|
179
|
+
if (tag === 'input' && stack.some(open => open.tag === 'select')) {
|
|
180
|
+
throw fail(node, state, 'select', 'input', 'an <input> start tag inside a select closes it, and the input is created after it.');
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Static text the parser would move or drop: inside a table structure element
|
|
185
|
+
// it is moved in front of the table, inside a void element it has nowhere to go.
|
|
186
|
+
function checkText(node, text, stack, state) {
|
|
187
|
+
if (stack.length === 0) return;
|
|
188
|
+
const parent = stack[stack.length - 1].tag;
|
|
189
|
+
if (!TABLE_CONTENT[parent] && !VOID_ELEMENTS.has(parent)) return;
|
|
190
|
+
if (!text) return;
|
|
191
|
+
throw fail(node, state, parent, '#text', VOID_ELEMENTS.has(parent)
|
|
192
|
+
? `<${parent}> is a void element and has no content.`
|
|
193
|
+
: `the parser moves text out of <${parent}> to before the table.`);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Walk one JSX tree: elements written inside elements, through fragments and
|
|
197
|
+
// component children. JSX in an expression container or an attribute value
|
|
198
|
+
// is a root of its own and is checked when it is lowered.
|
|
199
|
+
function walk(node, stack, state, foreign) {
|
|
200
|
+
if (t.isJSXText(node)) {
|
|
201
|
+
checkText(node, normalizeJsxText(node.value), stack, state);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (t.isJSXExpressionContainer(node)) {
|
|
205
|
+
if (state.ssr) walkExpression(node.expression, stack, state, foreign);
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
if (t.isJSXFragment(node)) {
|
|
209
|
+
node._whatChecked = true;
|
|
210
|
+
for (const child of node.children) walk(child, stack, state, foreign);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (!t.isJSXElement(node)) return;
|
|
214
|
+
node._whatChecked = true;
|
|
215
|
+
if (isComponentElement(node)) {
|
|
216
|
+
for (const child of node.children) walk(child, [], state, []);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const tag = node.openingElement.name.name;
|
|
220
|
+
const html = node._whatNs === 'html';
|
|
221
|
+
if (html) checkElement(node, stack, state);
|
|
222
|
+
else if (foreign.length > 0 && (BREAKS_FOREIGN_CONTENT.has(tag) || (tag === 'font' && isBreakoutFont(node)))) {
|
|
223
|
+
throw fail(node, state, foreign[foreign.length - 1], tag,
|
|
224
|
+
'an HTML element inside SVG or MathML content closes it, and the parser creates the element as HTML beside it. Wrap HTML in <foreignObject> (SVG) or <mtext> (MathML).');
|
|
225
|
+
}
|
|
226
|
+
const inner = html ? [...stack, { tag }] : [];
|
|
227
|
+
const innerForeign = html ? [] : [...foreign, tag];
|
|
228
|
+
for (const child of node.children) walk(child, inner, state, innerForeign);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// The server back end writes a child expression's JSX and static text into the
|
|
232
|
+
// parent's markup, so it is checked in the parent's place: a literal, an
|
|
233
|
+
// element written as the expression, either arm of a conditional, the operands
|
|
234
|
+
// of a logical expression, and the rows a .map() callback returns.
|
|
235
|
+
function walkExpression(expr, stack, state, foreign) {
|
|
236
|
+
if (t.isStringLiteral(expr) || t.isNumericLiteral(expr)) {
|
|
237
|
+
if (foreign.length === 0) checkText(expr, String(expr.value), stack, state);
|
|
238
|
+
} else if (t.isJSXElement(expr) || t.isJSXFragment(expr)) {
|
|
239
|
+
walk(expr, stack, state, foreign);
|
|
240
|
+
} else if (t.isConditionalExpression(expr)) {
|
|
241
|
+
walkExpression(expr.consequent, stack, state, foreign);
|
|
242
|
+
walkExpression(expr.alternate, stack, state, foreign);
|
|
243
|
+
} else if (t.isLogicalExpression(expr)) {
|
|
244
|
+
walkExpression(expr.left, stack, state, foreign);
|
|
245
|
+
walkExpression(expr.right, stack, state, foreign);
|
|
246
|
+
} else if (t.isArrowFunctionExpression(expr) && expr.params.length === 0) {
|
|
247
|
+
walkExpression(expr.body, stack, state, foreign);
|
|
248
|
+
} else if (t.isCallExpression(expr) && t.isMemberExpression(expr.callee)
|
|
249
|
+
&& t.isIdentifier(expr.callee.property, { name: 'map' }) && t.isFunction(expr.arguments[0])) {
|
|
250
|
+
const fn = expr.arguments[0];
|
|
251
|
+
if (t.isExpression(fn.body)) walkExpression(fn.body, stack, state, foreign);
|
|
252
|
+
else for (const statement of fn.body.body) if (t.isReturnStatement(statement) && statement.argument) walkExpression(statement.argument, stack, state, foreign);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function validateNesting(root, state) {
|
|
257
|
+
if (root._whatChecked) return;
|
|
258
|
+
const ns = root._whatNs;
|
|
259
|
+
walk(root, [], state, ns === 'svg' || ns === 'math' ? [ns] : []);
|
|
260
|
+
}
|