solid-optimizer 1.0.0-next.9 → 1.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.
- package/README.md +14 -19
- package/dist/{compile-X_SMDFr2.d.mts → compile-BKA77zb8.d.mts} +10 -2
- package/dist/compile-BKA77zb8.d.mts.map +1 -0
- package/dist/{compile-DptOANJi.mjs → compile-D2f8eXFk.mjs} +548 -161
- package/dist/compile-D2f8eXFk.mjs.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +1 -1
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +55 -14
- package/dist/vite.mjs.map +1 -1
- package/package.json +6 -6
- package/src/compile.ts +29 -8
- package/src/conditional.ts +29 -22
- package/src/context.ts +3 -3
- package/src/copy-size.ts +6 -5
- package/src/fold.ts +57 -53
- package/src/inline.ts +67 -59
- package/src/jsx.ts +16 -14
- package/src/memo.ts +12 -6
- package/src/text.ts +297 -0
- package/src/tidy.ts +390 -0
- package/src/vite/index.ts +66 -12
- package/dist/compile-DptOANJi.mjs.map +0 -1
- package/dist/compile-X_SMDFr2.d.mts.map +0 -1
package/src/text.ts
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import type { DecodedSourceMap, SourceMapSegment } from '@jridgewell/remapping';
|
|
2
|
+
import MagicString from 'magic-string';
|
|
3
|
+
|
|
4
|
+
// Each character of a text has an origin, written as two UTF-16 code units
|
|
5
|
+
// that hold a 32-bit value:
|
|
6
|
+
// - an offset in the pass's input that the character was copied from,
|
|
7
|
+
// - `WRITTEN | offset` for a character a pass wrote at that offset, or
|
|
8
|
+
// - `LOOSE` for a character that takes the offset of the edit it ends up in.
|
|
9
|
+
const WRITTEN = 0x8000_0000;
|
|
10
|
+
const LOOSE = 0xffff_ffff;
|
|
11
|
+
const LOOSE_UNITS = '\uffff\uffff';
|
|
12
|
+
|
|
13
|
+
function encodeOrigin(value: number): string {
|
|
14
|
+
return String.fromCharCode(value >>> 16, value & 0xffff);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function decodeOrigin(origins: string, index: number): number {
|
|
18
|
+
return ((origins.charCodeAt(index * 2) << 16) | origins.charCodeAt(index * 2 + 1)) >>> 0;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function repeatOrigin(value: number, length: number): string {
|
|
22
|
+
return encodeOrigin(value).repeat(length);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The line of the original that holds an offset. */
|
|
26
|
+
function findLine(lineStarts: readonly number[], offset: number): number {
|
|
27
|
+
let low = 0;
|
|
28
|
+
let high = lineStarts.length - 1;
|
|
29
|
+
while (low < high) {
|
|
30
|
+
const middle = (low + high + 1) >> 1;
|
|
31
|
+
if ((lineStarts[middle] ?? 0) <= offset) {
|
|
32
|
+
low = middle;
|
|
33
|
+
} else {
|
|
34
|
+
high = middle - 1;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return low;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// The passes of one compile and the copies of one pass start from the same
|
|
41
|
+
// code, so the last identity is kept.
|
|
42
|
+
let identityCode: string | undefined;
|
|
43
|
+
let identity = '';
|
|
44
|
+
|
|
45
|
+
/** The origins of unedited code: each character its own offset. */
|
|
46
|
+
function createIdentity(code: string): string {
|
|
47
|
+
if (identityCode === code) {
|
|
48
|
+
return identity;
|
|
49
|
+
}
|
|
50
|
+
const chunks: string[] = [];
|
|
51
|
+
const units = new Uint16Array(Math.min(code.length, 4096) * 2);
|
|
52
|
+
for (let start = 0; start < code.length; start += 4096) {
|
|
53
|
+
const end = Math.min(code.length, start + 4096);
|
|
54
|
+
for (let index = start; index < end; index += 1) {
|
|
55
|
+
units[(index - start) * 2] = index >>> 16;
|
|
56
|
+
units[(index - start) * 2 + 1] = index & 0xffff;
|
|
57
|
+
}
|
|
58
|
+
chunks.push(String.fromCharCode(...units.subarray(0, (end - start) * 2)));
|
|
59
|
+
}
|
|
60
|
+
identityCode = code;
|
|
61
|
+
identity = chunks.join('');
|
|
62
|
+
return identity;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Code with the origin of each of its characters, so that the source map
|
|
67
|
+
* can point a copied character back at the one it copies.
|
|
68
|
+
*
|
|
69
|
+
* `origins` is `undefined` when the compile writes no source map.
|
|
70
|
+
*/
|
|
71
|
+
export class Text {
|
|
72
|
+
constructor(
|
|
73
|
+
readonly code: string,
|
|
74
|
+
readonly origins: string | undefined,
|
|
75
|
+
) {}
|
|
76
|
+
|
|
77
|
+
toString(): string {
|
|
78
|
+
return this.code;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export type Content = Text | string;
|
|
83
|
+
|
|
84
|
+
/** The code of a content. */
|
|
85
|
+
export function getCode(content: Content): string {
|
|
86
|
+
return typeof content === 'string' ? content : content.code;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Joins contents into one text. The characters of a string take the offset
|
|
91
|
+
* of the edit that writes the text.
|
|
92
|
+
*/
|
|
93
|
+
export function join(parts: readonly Content[], separator = ''): Text {
|
|
94
|
+
let code = '';
|
|
95
|
+
let origins: string | undefined = '';
|
|
96
|
+
for (let index = 0; index < parts.length; index += 1) {
|
|
97
|
+
const part = parts[index] ?? '';
|
|
98
|
+
if (index > 0 && separator) {
|
|
99
|
+
code += separator;
|
|
100
|
+
if (origins !== undefined) {
|
|
101
|
+
origins += repeatOrigin(LOOSE, separator.length);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (typeof part === 'string') {
|
|
105
|
+
code += part;
|
|
106
|
+
if (origins !== undefined) {
|
|
107
|
+
origins += repeatOrigin(LOOSE, part.length);
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
code += part.code;
|
|
111
|
+
// A text without origins comes from a compile that writes no map.
|
|
112
|
+
origins = part.origins === undefined ? undefined : origins + part.origins;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return new Text(code, origins);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Builds a text from a template, keeping the origins of the texts it holds. */
|
|
119
|
+
export function joinText(strings: TemplateStringsArray, ...values: readonly Content[]): Text {
|
|
120
|
+
const parts: Content[] = [];
|
|
121
|
+
for (let index = 0; index < strings.length; index += 1) {
|
|
122
|
+
parts.push(strings[index] ?? '');
|
|
123
|
+
if (index < values.length) {
|
|
124
|
+
parts.push(values[index] ?? '');
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return join(parts);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The origins of a content written at an offset. */
|
|
131
|
+
function resolveOrigins(content: Content, at: number): string {
|
|
132
|
+
if (typeof content === 'string') {
|
|
133
|
+
return repeatOrigin(WRITTEN | at, content.length);
|
|
134
|
+
}
|
|
135
|
+
const { origins } = content;
|
|
136
|
+
if (origins === undefined) {
|
|
137
|
+
return repeatOrigin(WRITTEN | at, content.code.length);
|
|
138
|
+
}
|
|
139
|
+
// Only an origin that starts at an even unit is one; an odd match spans two.
|
|
140
|
+
let result = '';
|
|
141
|
+
let from = 0;
|
|
142
|
+
const written = encodeOrigin(WRITTEN | at);
|
|
143
|
+
let found = origins.indexOf(LOOSE_UNITS);
|
|
144
|
+
while (found !== -1) {
|
|
145
|
+
if (found % 2 === 1) {
|
|
146
|
+
found = origins.indexOf(LOOSE_UNITS, found + 1);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
result += origins.slice(from, found) + written;
|
|
150
|
+
from = found + 2;
|
|
151
|
+
found = origins.indexOf(LOOSE_UNITS, from);
|
|
152
|
+
}
|
|
153
|
+
return from === 0 ? origins : result + origins.slice(from);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Edits code like `MagicString`, and keeps the origin of each character of
|
|
158
|
+
* the result.
|
|
159
|
+
*
|
|
160
|
+
* A second `MagicString` holds the origins, two code units per character,
|
|
161
|
+
* and takes every edit the first one takes.
|
|
162
|
+
*/
|
|
163
|
+
export class Editor {
|
|
164
|
+
private readonly s: MagicString;
|
|
165
|
+
/** The origins, made at the first edit, since most passes change nothing. */
|
|
166
|
+
private o: MagicString | undefined;
|
|
167
|
+
|
|
168
|
+
constructor(
|
|
169
|
+
readonly original: string,
|
|
170
|
+
private readonly tracksOrigins: boolean,
|
|
171
|
+
) {
|
|
172
|
+
this.s = new MagicString(original);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** A new editor on the same original code, which keeps origins when this one does. */
|
|
176
|
+
fresh(): Editor {
|
|
177
|
+
return new Editor(this.original, this.tracksOrigins);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** The origins to edit, or `undefined` when this editor keeps none. */
|
|
181
|
+
private editOrigins(): MagicString | undefined {
|
|
182
|
+
if (this.tracksOrigins && !this.o) {
|
|
183
|
+
this.o = new MagicString(createIdentity(this.original));
|
|
184
|
+
}
|
|
185
|
+
return this.o;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Code that a pass writes in place of what is at `at`, mapped there. */
|
|
189
|
+
written(code: string, at: number): Text {
|
|
190
|
+
return new Text(code, this.tracksOrigins ? repeatOrigin(WRITTEN | at, code.length) : undefined);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
overwrite(start: number, end: number, content: Content): this {
|
|
194
|
+
this.s.overwrite(start, end, getCode(content));
|
|
195
|
+
this.editOrigins()?.overwrite(start * 2, end * 2, resolveOrigins(content, start));
|
|
196
|
+
return this;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
remove(start: number, end: number): this {
|
|
200
|
+
this.s.remove(start, end);
|
|
201
|
+
this.editOrigins()?.remove(start * 2, end * 2);
|
|
202
|
+
return this;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
prependLeft(index: number, content: Content): this {
|
|
206
|
+
this.s.prependLeft(index, getCode(content));
|
|
207
|
+
this.editOrigins()?.prependLeft(index * 2, resolveOrigins(content, index));
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
prependRight(index: number, content: Content): this {
|
|
212
|
+
this.s.prependRight(index, getCode(content));
|
|
213
|
+
this.editOrigins()?.prependRight(index * 2, resolveOrigins(content, index));
|
|
214
|
+
return this;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
appendLeft(index: number, content: Content): this {
|
|
218
|
+
this.s.appendLeft(index, getCode(content));
|
|
219
|
+
this.editOrigins()?.appendLeft(index * 2, resolveOrigins(content, index));
|
|
220
|
+
return this;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** The edited code of characters `start` to `end` of the original, with origins. */
|
|
224
|
+
slice(start: number, end: number): Text {
|
|
225
|
+
let origins: string | undefined;
|
|
226
|
+
if (this.o) {
|
|
227
|
+
origins = this.o.slice(start * 2, end * 2);
|
|
228
|
+
} else if (this.tracksOrigins) {
|
|
229
|
+
origins = createIdentity(this.original).slice(start * 2, end * 2);
|
|
230
|
+
}
|
|
231
|
+
return new Text(this.s.slice(start, end), origins);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
toString(): string {
|
|
235
|
+
return this.s.toString();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* A map from the edited code to the original. A copied character maps to
|
|
240
|
+
* the one it copies, and a written run maps to where it was written.
|
|
241
|
+
*/
|
|
242
|
+
generateMap(source: string): DecodedSourceMap {
|
|
243
|
+
const code = this.s.toString();
|
|
244
|
+
const origins = this.o?.toString() ?? createIdentity(this.original);
|
|
245
|
+
|
|
246
|
+
// Where each line of the original starts, to turn an offset into a line and column.
|
|
247
|
+
const lineStarts = [0];
|
|
248
|
+
for (let index = 0; index < this.original.length; index += 1) {
|
|
249
|
+
if (this.original.charCodeAt(index) === 10) {
|
|
250
|
+
lineStarts.push(index + 1);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Each copied character maps to its own, and a written run maps from its
|
|
255
|
+
// first character on each line. A copied run continues on the line of
|
|
256
|
+
// the character before it, so only a jump needs a search.
|
|
257
|
+
const mappings: SourceMapSegment[][] = [[]];
|
|
258
|
+
let line = mappings[0] ?? [];
|
|
259
|
+
let column = 0;
|
|
260
|
+
// No origin is `LOOSE` here, so the first character never continues a run.
|
|
261
|
+
let previous = LOOSE;
|
|
262
|
+
let sourceLine = 0;
|
|
263
|
+
let sourceColumn = 0;
|
|
264
|
+
for (let index = 0; index < code.length; index += 1) {
|
|
265
|
+
const origin = decodeOrigin(origins, index);
|
|
266
|
+
if (origin < WRITTEN) {
|
|
267
|
+
if (origin === previous + 1 && this.original.charCodeAt(previous) !== 10) {
|
|
268
|
+
sourceColumn += 1;
|
|
269
|
+
} else {
|
|
270
|
+
sourceLine = findLine(lineStarts, origin);
|
|
271
|
+
sourceColumn = origin - (lineStarts[sourceLine] ?? 0);
|
|
272
|
+
}
|
|
273
|
+
line.push([column, 0, sourceLine, sourceColumn]);
|
|
274
|
+
} else if (origin !== previous || column === 0) {
|
|
275
|
+
const offset = origin - WRITTEN;
|
|
276
|
+
const written = findLine(lineStarts, offset);
|
|
277
|
+
line.push([column, 0, written, offset - (lineStarts[written] ?? 0)]);
|
|
278
|
+
}
|
|
279
|
+
previous = origin;
|
|
280
|
+
if (code.charCodeAt(index) === 10) {
|
|
281
|
+
line = [];
|
|
282
|
+
mappings.push(line);
|
|
283
|
+
column = 0;
|
|
284
|
+
} else {
|
|
285
|
+
column += 1;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
version: 3,
|
|
290
|
+
file: null,
|
|
291
|
+
names: [],
|
|
292
|
+
sources: [source],
|
|
293
|
+
sourcesContent: [this.original],
|
|
294
|
+
mappings,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
}
|
package/src/tidy.ts
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tidies the JSX the other passes wrote, without changing what it renders.
|
|
3
|
+
*
|
|
4
|
+
* - An empty `{}` child goes. The passes write one to keep two texts apart,
|
|
5
|
+
* so it only goes when the texts on both sides read the same without it.
|
|
6
|
+
* - A string in braces becomes plain JSX: `class={'card'}` becomes
|
|
7
|
+
* `class="card"`, and a `{'Hello'}` child becomes the text `Hello`.
|
|
8
|
+
* - A fragment that holds one element, with only line breaks around it,
|
|
9
|
+
* becomes that element.
|
|
10
|
+
*
|
|
11
|
+
* Each edit follows a model of how Solid's JSX transform reads JSX: its
|
|
12
|
+
* whitespace rule, which strings it writes as they are, and where a fragment
|
|
13
|
+
* around one element lowers the same as the element. Anything outside the
|
|
14
|
+
* model, such as unusual whitespace, stays as it is. The tests lower the
|
|
15
|
+
* tidied and untidied output and expect the same code.
|
|
16
|
+
*/
|
|
17
|
+
import type {
|
|
18
|
+
JSXAttribute,
|
|
19
|
+
JSXElement,
|
|
20
|
+
JSXExpressionContainer,
|
|
21
|
+
JSXFragment,
|
|
22
|
+
Node,
|
|
23
|
+
} from 'oxc-parser';
|
|
24
|
+
import { forEachChild } from './ast';
|
|
25
|
+
import type { PassContext } from './context';
|
|
26
|
+
|
|
27
|
+
type Child = JSXElement['children'][number];
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The text Solid's JSX transform makes of a raw text child: when the text
|
|
31
|
+
* has a line break, each line after the first loses its leading spaces, lines
|
|
32
|
+
* of only spaces drop out and the rest join with a space; then every run of
|
|
33
|
+
* spaces becomes one space. This differs from React's rule, which keeps runs
|
|
34
|
+
* of spaces on one line.
|
|
35
|
+
*/
|
|
36
|
+
export function cleanText(raw: string): string {
|
|
37
|
+
let text = raw.replaceAll('\r', '');
|
|
38
|
+
if (text.includes('\n')) {
|
|
39
|
+
const kept: string[] = [];
|
|
40
|
+
for (const [index, line] of text.split('\n').entries()) {
|
|
41
|
+
const trimmed = index === 0 ? line : line.replace(/^[ \t]+/, '');
|
|
42
|
+
if (/[^ \t]/.test(trimmed)) {
|
|
43
|
+
kept.push(trimmed);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
text = kept.join(' ');
|
|
47
|
+
}
|
|
48
|
+
return text.replaceAll(/[ \t\n]+/g, ' ');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether a text holds whitespace or format characters other than space,
|
|
53
|
+
* tab and line breaks, such as U+0085 or a byte order mark. Solid's JSX
|
|
54
|
+
* transform and JavaScript disagree on some of them, so such text stays as it is.
|
|
55
|
+
*/
|
|
56
|
+
function hasUnusualSpace(text: string): boolean {
|
|
57
|
+
return /(?![ \t\n\r])[\s\p{White_Space}\p{Cf}]/u.test(text);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The string a container holds when it is a plain string written without
|
|
62
|
+
* escapes, such as `{'a'}` or `{`a`}`. The characters between the quotes are
|
|
63
|
+
* then the string itself, so tidying keeps them, and their source map
|
|
64
|
+
* positions, and only drops what is around them.
|
|
65
|
+
*/
|
|
66
|
+
function readString(container: JSXExpressionContainer, code: string): string | undefined {
|
|
67
|
+
const { expression } = container;
|
|
68
|
+
let value: string | undefined;
|
|
69
|
+
if (expression.type === 'Literal' && typeof expression.value === 'string') {
|
|
70
|
+
value = expression.value;
|
|
71
|
+
} else if (
|
|
72
|
+
expression.type === 'TemplateLiteral' &&
|
|
73
|
+
expression.expressions.length === 0 &&
|
|
74
|
+
expression.quasis.length === 1
|
|
75
|
+
) {
|
|
76
|
+
value = expression.quasis[0].value.cooked ?? undefined;
|
|
77
|
+
}
|
|
78
|
+
if (value === undefined || code.slice(expression.start + 1, expression.end - 1) !== value) {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
// Braces that hold a comment beside the string keep it.
|
|
82
|
+
const around =
|
|
83
|
+
code.slice(container.start + 1, expression.start) +
|
|
84
|
+
code.slice(expression.end, container.end - 1);
|
|
85
|
+
if (around.trim() !== '') {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
return value;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Replaces the braces and quotes around a string with `before` and `after`,
|
|
93
|
+
* keeping the string's own characters where they are.
|
|
94
|
+
*/
|
|
95
|
+
function unwrapString(
|
|
96
|
+
container: JSXExpressionContainer,
|
|
97
|
+
before: string,
|
|
98
|
+
after: string,
|
|
99
|
+
context: PassContext,
|
|
100
|
+
): void {
|
|
101
|
+
const { expression } = container;
|
|
102
|
+
const { code, s } = context;
|
|
103
|
+
// Removing keeps the mapping of the string's characters, and of quotes
|
|
104
|
+
// that stay. Only a quote that changes is written.
|
|
105
|
+
s.remove(container.start, expression.start);
|
|
106
|
+
s.remove(expression.end, container.end);
|
|
107
|
+
const open = code[expression.start];
|
|
108
|
+
const close = code[expression.end - 1];
|
|
109
|
+
if (before === open) {
|
|
110
|
+
// The quote stays.
|
|
111
|
+
} else if (before === '') {
|
|
112
|
+
s.remove(expression.start, expression.start + 1);
|
|
113
|
+
} else {
|
|
114
|
+
s.overwrite(expression.start, expression.start + 1, s.written(before, expression.start));
|
|
115
|
+
}
|
|
116
|
+
if (after === close) {
|
|
117
|
+
// The quote stays.
|
|
118
|
+
} else if (after === '') {
|
|
119
|
+
s.remove(expression.end - 1, expression.end);
|
|
120
|
+
} else {
|
|
121
|
+
s.overwrite(expression.end - 1, expression.end, s.written(after, expression.end - 1));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Whether a container holds nothing, not even a comment. */
|
|
126
|
+
function isEmpty(container: JSXExpressionContainer, code: string): boolean {
|
|
127
|
+
return (
|
|
128
|
+
container.expression.type === 'JSXEmptyExpression' &&
|
|
129
|
+
code.slice(container.start + 1, container.end - 1).trim() === ''
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* A child as it renders: raw text, which JSX cleans together with the raw
|
|
135
|
+
* text next to it; a literal string, which renders as it is and keeps the
|
|
136
|
+
* texts on its sides apart; or something else, such as an element.
|
|
137
|
+
*/
|
|
138
|
+
type Part = { raw: string } | { literal: string } | { other: number };
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The pieces a list of children renders: each text that is not empty, and a
|
|
142
|
+
* mark for each child that is not text.
|
|
143
|
+
*/
|
|
144
|
+
function renderPieces(parts: readonly (Part | undefined)[]): string[] {
|
|
145
|
+
const pieces: string[] = [];
|
|
146
|
+
let raw: string | undefined;
|
|
147
|
+
const flush = (): void => {
|
|
148
|
+
if (raw !== undefined) {
|
|
149
|
+
const text = cleanText(raw);
|
|
150
|
+
if (text !== '') {
|
|
151
|
+
pieces.push(text);
|
|
152
|
+
}
|
|
153
|
+
raw = undefined;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
for (const part of parts) {
|
|
157
|
+
if (!part) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if ('raw' in part) {
|
|
161
|
+
raw = (raw ?? '') + part.raw;
|
|
162
|
+
} else if ('literal' in part) {
|
|
163
|
+
flush();
|
|
164
|
+
if (part.literal !== '') {
|
|
165
|
+
pieces.push(part.literal);
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
flush();
|
|
169
|
+
pieces.push(`\0${String(part.other)}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
flush();
|
|
173
|
+
return pieces;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* What a list of children renders. In an element, Solid's JSX transform
|
|
178
|
+
* writes the texts next to each other into the template as one text, so
|
|
179
|
+
* only the joined text counts. Anywhere else, such as a fragment, each text
|
|
180
|
+
* stays its own string, so the pieces must stay apart too.
|
|
181
|
+
*/
|
|
182
|
+
function render(parts: readonly (Part | undefined)[], inElement: boolean): string {
|
|
183
|
+
const pieces = renderPieces(parts);
|
|
184
|
+
if (!inElement) {
|
|
185
|
+
return JSON.stringify(pieces);
|
|
186
|
+
}
|
|
187
|
+
let joined = '';
|
|
188
|
+
for (const piece of pieces) {
|
|
189
|
+
joined += piece.startsWith('\0') ? `${piece}\0` : piece;
|
|
190
|
+
}
|
|
191
|
+
return joined;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Whether an element is an HTML or SVG element, which Solid lowers into a template. */
|
|
195
|
+
function isIntrinsic(element: JSXElement): boolean {
|
|
196
|
+
const { name } = element.openingElement;
|
|
197
|
+
if (name.type === 'JSXNamespacedName') {
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
return name.type === 'JSXIdentifier' && /^[a-z]/.test(name.name);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** A string that can be written as JSX text: no syntax, no entities, no line breaks. */
|
|
204
|
+
function isPlainText(value: string): boolean {
|
|
205
|
+
return (
|
|
206
|
+
value !== '' &&
|
|
207
|
+
!/[{}<>&\r\n\t]/.test(value) &&
|
|
208
|
+
!hasUnusualSpace(value) &&
|
|
209
|
+
value.trim() === value
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** A string that can be written as a JSX attribute in double quotes. */
|
|
214
|
+
function isPlainAttribute(value: string): boolean {
|
|
215
|
+
return !/["&\r\n]/.test(value) && !hasUnusualSpace(value);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Attributes whose string Solid's JSX transform treats differently once it
|
|
220
|
+
* is written as a plain value: it moves a static `style` last, and `ref`,
|
|
221
|
+
* `classList`, event handlers (`on` in any case) and namespaced attributes do
|
|
222
|
+
* not take a string.
|
|
223
|
+
*/
|
|
224
|
+
function isTidyAttribute(attribute: JSXAttribute): boolean {
|
|
225
|
+
const { name } = attribute;
|
|
226
|
+
return (
|
|
227
|
+
name.type === 'JSXIdentifier' &&
|
|
228
|
+
name.name !== 'style' &&
|
|
229
|
+
name.name !== 'ref' &&
|
|
230
|
+
name.name !== 'classList' &&
|
|
231
|
+
!name.name.startsWith('on')
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Whether an element can get children from an attribute: a `children`
|
|
237
|
+
* attribute or a spread. Its own children then decide whether those show, so
|
|
238
|
+
* even an empty `{}` among them counts, and they stay as they are.
|
|
239
|
+
*/
|
|
240
|
+
function hasChildrenAttribute(element: JSXElement): boolean {
|
|
241
|
+
for (const attribute of element.openingElement.attributes) {
|
|
242
|
+
if (
|
|
243
|
+
attribute.type === 'JSXSpreadAttribute' ||
|
|
244
|
+
(attribute.name.type === 'JSXIdentifier' && attribute.name.name === 'children')
|
|
245
|
+
) {
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Removes empty braces and writes plain strings as text, one at a time. Each
|
|
254
|
+
* edit is kept only when the children still render the same with every edit
|
|
255
|
+
* kept so far.
|
|
256
|
+
*/
|
|
257
|
+
function tidyChildren(
|
|
258
|
+
children: readonly Child[],
|
|
259
|
+
inElement: boolean,
|
|
260
|
+
context: PassContext,
|
|
261
|
+
): boolean {
|
|
262
|
+
const { code, s } = context;
|
|
263
|
+
for (const child of children) {
|
|
264
|
+
if (child.type === 'JSXText' && hasUnusualSpace(code.slice(child.start, child.end))) {
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
const parts: (Part | undefined)[] = [];
|
|
269
|
+
for (const [index, child] of children.entries()) {
|
|
270
|
+
if (child.type === 'JSXText') {
|
|
271
|
+
parts.push({ raw: code.slice(child.start, child.end) });
|
|
272
|
+
} else if (child.type === 'JSXExpressionContainer' && isEmpty(child, code)) {
|
|
273
|
+
parts.push({ literal: '' });
|
|
274
|
+
} else if (child.type === 'JSXExpressionContainer' && readString(child, code) !== undefined) {
|
|
275
|
+
parts.push({ literal: readString(child, code) ?? '' });
|
|
276
|
+
} else {
|
|
277
|
+
parts.push({ other: index });
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
const expected = render(parts, inElement);
|
|
281
|
+
let changed = false;
|
|
282
|
+
for (const [index, child] of children.entries()) {
|
|
283
|
+
if (child.type !== 'JSXExpressionContainer') {
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const empty = isEmpty(child, code);
|
|
287
|
+
const value = empty ? undefined : readString(child, code);
|
|
288
|
+
if (!empty && (value === undefined || !isPlainText(value))) {
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
// Without the braces, the texts on both sides meet, and a string joins them as text.
|
|
292
|
+
const before = parts[index];
|
|
293
|
+
parts[index] = empty ? undefined : { raw: value ?? '' };
|
|
294
|
+
if (render(parts, inElement) === expected) {
|
|
295
|
+
if (empty) {
|
|
296
|
+
s.remove(child.start, child.end);
|
|
297
|
+
} else {
|
|
298
|
+
unwrapString(child, '', '', context);
|
|
299
|
+
}
|
|
300
|
+
changed = true;
|
|
301
|
+
} else {
|
|
302
|
+
parts[index] = before;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return changed;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** The one element a fragment holds, when everything else in it is line breaks or empty braces. */
|
|
309
|
+
function findOnlyElement(fragment: JSXFragment, code: string): JSXElement | undefined {
|
|
310
|
+
let only: JSXElement | undefined;
|
|
311
|
+
for (const child of fragment.children) {
|
|
312
|
+
if (child.type === 'JSXElement') {
|
|
313
|
+
if (only) {
|
|
314
|
+
return undefined;
|
|
315
|
+
}
|
|
316
|
+
only = child;
|
|
317
|
+
} else if (child.type === 'JSXText') {
|
|
318
|
+
// Spaces without a line break are text: `<> <b /></>` renders a space.
|
|
319
|
+
if (!/^[ \t\r\n]*$/.test(child.value) || cleanText(child.value) !== '') {
|
|
320
|
+
return undefined;
|
|
321
|
+
}
|
|
322
|
+
} else if (!(child.type === 'JSXExpressionContainer' && isEmpty(child, code))) {
|
|
323
|
+
return undefined;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return only;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function tidyAttribute(attribute: JSXAttribute, context: PassContext): boolean {
|
|
330
|
+
const { value } = attribute;
|
|
331
|
+
if (value?.type !== 'JSXExpressionContainer' || !isTidyAttribute(attribute)) {
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
const text = readString(value, context.code);
|
|
335
|
+
if (text === undefined || !isPlainAttribute(text)) {
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
unwrapString(value, '"', '"', context);
|
|
339
|
+
return true;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Whether a fragment is a branch of `? :`, or the right side of `&&`, `||`
|
|
344
|
+
* or `??`, where the lowering of `<Show>` and `<Switch>` leaves them. There a
|
|
345
|
+
* fragment around one element lowers the same as the element. Elsewhere,
|
|
346
|
+
* such as a function's return, Solid's JSX transform writes different code.
|
|
347
|
+
*/
|
|
348
|
+
function isBranch(fragment: JSXFragment, context: PassContext): boolean {
|
|
349
|
+
let node: Node = fragment;
|
|
350
|
+
let parent = context.parents.get(node);
|
|
351
|
+
while (parent?.type === 'ParenthesizedExpression') {
|
|
352
|
+
node = parent;
|
|
353
|
+
parent = context.parents.get(node);
|
|
354
|
+
}
|
|
355
|
+
if (parent?.type === 'ConditionalExpression') {
|
|
356
|
+
return parent.consequent === node || parent.alternate === node;
|
|
357
|
+
}
|
|
358
|
+
return parent?.type === 'LogicalExpression' && parent.right === node;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export function tidyJSX(context: PassContext): boolean {
|
|
362
|
+
let changed = false;
|
|
363
|
+
const visit = (node: Node): void => {
|
|
364
|
+
if (node.type === 'JSXFragment' && isBranch(node, context)) {
|
|
365
|
+
const only = findOnlyElement(node, context.code);
|
|
366
|
+
if (only) {
|
|
367
|
+
// Everything around the element is line breaks or empty braces, so it all goes.
|
|
368
|
+
context.s.remove(node.start, only.start);
|
|
369
|
+
context.s.remove(only.end, node.end);
|
|
370
|
+
changed = true;
|
|
371
|
+
visit(only);
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
const childrenFixed = node.type === 'JSXElement' && hasChildrenAttribute(node);
|
|
376
|
+
if ((node.type === 'JSXElement' || node.type === 'JSXFragment') && !childrenFixed) {
|
|
377
|
+
const inElement = node.type === 'JSXElement' && isIntrinsic(node);
|
|
378
|
+
if (tidyChildren(node.children, inElement, context)) {
|
|
379
|
+
changed = true;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
if (node.type === 'JSXAttribute' && tidyAttribute(node, context)) {
|
|
383
|
+
changed = true;
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
forEachChild(node, visit);
|
|
387
|
+
};
|
|
388
|
+
visit(context.program);
|
|
389
|
+
return changed;
|
|
390
|
+
}
|