solid-optimizer 1.0.0-next.0 → 1.0.0-next.10

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/src/jsx.ts CHANGED
@@ -7,9 +7,9 @@
7
7
  * whenever a replacement would put two texts that were apart next to each
8
8
  * other, an empty `{}` keeps them apart.
9
9
  */
10
- import type { JSXAttribute, JSXChild, JSXElement, JSXFragment, JSXText, Node } from 'oxc-parser';
11
- import { isFilteredText } from './ast';
12
- import type MagicString from 'magic-string';
10
+ import type { JSXAttribute, JSXChild, JSXElement, JSXFragment, Node } from 'oxc-parser';
11
+ import type { Content, Editor, Text } from './text';
12
+ import { getCode, joinText } from './text';
13
13
 
14
14
  /**
15
15
  * What a JSX child turns into.
@@ -18,48 +18,65 @@ export interface Piece {
18
18
  /** The child the piece replaces. */
19
19
  readonly node: JSXChild;
20
20
  /** The replacement text, or `undefined` to keep the child as written. */
21
- text: string | undefined;
22
- /** The JSX text the replacement starts with. */
23
- startText: JSXText | undefined;
24
- /** The JSX text the replacement ends with. */
25
- endText: JSXText | undefined;
21
+ text: Content | undefined;
22
+ /** The JSX text the piece starts with, or `undefined` when it starts with a tag or a container. */
23
+ startText: string | undefined;
24
+ /** The JSX text the piece ends with. */
25
+ endText: string | undefined;
26
26
  }
27
27
 
28
28
  export function keptPiece(node: JSXChild): Piece {
29
- const text = node.type === 'JSXText' ? node : undefined;
29
+ const text = node.type === 'JSXText' ? node.value : undefined;
30
30
  return { node, text: undefined, startText: text, endText: text };
31
31
  }
32
32
 
33
33
  /**
34
34
  * A piece that splices `children` in place of `node`.
35
35
  */
36
- export function splicePiece(s: MagicString, node: JSXChild, children: readonly JSXChild[]): Piece {
36
+ export function splicePiece(s: Editor, node: JSXChild, children: readonly JSXChild[]): Piece {
37
37
  const first = children.at(0);
38
38
  const last = children.at(-1);
39
39
  if (!first || !last) {
40
40
  return replacedPiece(node, '');
41
41
  }
42
+ return replacedPiece(node, s.slice(first.start, last.end));
43
+ }
44
+
45
+ /**
46
+ * A piece that writes `text`, JSX children as source, in place of `node`.
47
+ *
48
+ * The texts it starts and ends with are read from `text` itself, not from
49
+ * the children it came from. A fold inside them in the same round can have
50
+ * turned a child that was an element into text, such as an inner `<Show>`.
51
+ * JSX text holds no `<`, `>`, `{`, or `}`, so the first and last of those
52
+ * mark where the text ends.
53
+ */
54
+ export function replacedPiece(node: JSXChild, text: Content): Piece {
55
+ const code = getCode(text);
56
+ const start = code.search(/[<{]/);
57
+ const end = Math.max(code.lastIndexOf('>'), code.lastIndexOf('}'));
58
+ const startText = start === -1 ? code : code.slice(0, start);
59
+ const endText = code.slice(end + 1);
42
60
  return {
43
61
  node,
44
- text: s.slice(first.start, last.end),
45
- startText: first.type === 'JSXText' ? first : undefined,
46
- endText: last.type === 'JSXText' ? last : undefined,
62
+ text,
63
+ startText: startText === '' ? undefined : startText,
64
+ endText: endText === '' ? undefined : endText,
47
65
  };
48
66
  }
49
67
 
50
- export function replacedPiece(node: JSXChild, text: string): Piece {
51
- return { node, text, startText: undefined, endText: undefined };
68
+ /** Whether JSX drops this text: whitespace that contains a line break. */
69
+ function isDroppedText(text: string): boolean {
70
+ return /^\s*$/.test(text) && /[\r\n]/.test(text);
52
71
  }
53
72
 
54
73
  /**
55
74
  * Whether two texts render differently as one text node than apart.
56
75
  * Two texts that JSX drops, like line breaks and indentation, still drop together.
57
76
  */
58
- function mergeChangesText(before: JSXText | undefined, after: JSXText | undefined): boolean {
77
+ function mergeChangesText(before: string | undefined, after: string | undefined): boolean {
59
78
  return (
60
- before !== undefined &&
61
- after !== undefined &&
62
- !(isFilteredText(before) && isFilteredText(after))
79
+ before !== undefined && after !== undefined && !(isDroppedText(before) && isDroppedText(after))
63
80
  );
64
81
  }
65
82
 
@@ -68,12 +85,12 @@ function mergeChangesText(before: JSXText | undefined, after: JSXText | undefine
68
85
  */
69
86
  function separate(previous: Piece, next: Piece, emptiesBetween: readonly Piece[]): void {
70
87
  if (next.text !== undefined) {
71
- next.text = `{}${next.text}`;
88
+ next.text = joinText`{}${next.text}`;
72
89
  next.startText = undefined;
73
90
  return;
74
91
  }
75
92
  if (previous.text !== undefined) {
76
- previous.text = `${previous.text}{}`;
93
+ previous.text = joinText`${previous.text}{}`;
77
94
  previous.endText = undefined;
78
95
  return;
79
96
  }
@@ -88,13 +105,13 @@ function separate(previous: Piece, next: Piece, emptiesBetween: readonly Piece[]
88
105
  * Writes the replaced pieces of a child list, adding `{}` wherever two texts
89
106
  * would otherwise merge.
90
107
  */
91
- export function writePieces(s: MagicString, pieces: readonly Piece[]): boolean {
108
+ export function writePieces(s: Editor, pieces: readonly Piece[]): boolean {
92
109
  let changed = false;
93
110
  // The last piece that renders anything, and the empty pieces after it.
94
111
  let previous: Piece | undefined;
95
112
  let emptiesSincePrevious: Piece[] = [];
96
113
  for (const piece of pieces) {
97
- if (piece.text === '') {
114
+ if (piece.text !== undefined && getCode(piece.text) === '') {
98
115
  emptiesSincePrevious.push(piece);
99
116
  continue;
100
117
  }
@@ -109,7 +126,7 @@ export function writePieces(s: MagicString, pieces: readonly Piece[]): boolean {
109
126
  continue;
110
127
  }
111
128
  changed = true;
112
- if (piece.text === '') {
129
+ if (getCode(piece.text) === '') {
113
130
  s.remove(piece.node.start, piece.node.end);
114
131
  } else {
115
132
  s.overwrite(piece.node.start, piece.node.end, piece.text);
@@ -132,9 +149,12 @@ export function findAttribute(element: JSXElement, name: string): JSXAttribute |
132
149
  }
133
150
 
134
151
  export function hasSpreadAttribute(element: JSXElement): boolean {
135
- return element.openingElement.attributes.some(
136
- (attribute) => attribute.type === 'JSXSpreadAttribute',
137
- );
152
+ for (const attribute of element.openingElement.attributes) {
153
+ if (attribute.type === 'JSXSpreadAttribute') {
154
+ return true;
155
+ }
156
+ }
157
+ return false;
138
158
  }
139
159
 
140
160
  /**
@@ -142,24 +162,25 @@ export function hasSpreadAttribute(element: JSXElement): boolean {
142
162
  * narrowed value. Folding one away would drop that call.
143
163
  */
144
164
  export function hasCallbackChild(children: readonly JSXChild[]): boolean {
145
- return children.some((child) => {
165
+ for (const child of children) {
146
166
  if (child.type !== 'JSXExpressionContainer') {
147
- return false;
167
+ continue;
148
168
  }
149
169
  let expression: Node = child.expression;
150
170
  while (expression.type === 'ParenthesizedExpression') {
151
171
  expression = expression.expression;
152
172
  }
153
- return (
154
- expression.type === 'ArrowFunctionExpression' || expression.type === 'FunctionExpression'
155
- );
156
- });
173
+ if (expression.type === 'ArrowFunctionExpression' || expression.type === 'FunctionExpression') {
174
+ return true;
175
+ }
176
+ }
177
+ return false;
157
178
  }
158
179
 
159
180
  /**
160
181
  * The text between an element's tags, or an empty string for a self-closing element.
161
182
  */
162
- export function innerText(s: MagicString, node: JSXElement | JSXFragment): string {
183
+ export function innerText(s: Editor, node: JSXElement | JSXFragment): Text | string {
163
184
  if (node.type === 'JSXFragment') {
164
185
  return s.slice(node.openingFragment.end, node.closingFragment.start);
165
186
  }