solid-optimizer 1.0.0-next.4 → 1.0.0-next.5

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
@@ -132,9 +132,12 @@ export function findAttribute(element: JSXElement, name: string): JSXAttribute |
132
132
  }
133
133
 
134
134
  export function hasSpreadAttribute(element: JSXElement): boolean {
135
- return element.openingElement.attributes.some(
136
- (attribute) => attribute.type === 'JSXSpreadAttribute',
137
- );
135
+ for (const attribute of element.openingElement.attributes) {
136
+ if (attribute.type === 'JSXSpreadAttribute') {
137
+ return true;
138
+ }
139
+ }
140
+ return false;
138
141
  }
139
142
 
140
143
  /**
@@ -142,18 +145,19 @@ export function hasSpreadAttribute(element: JSXElement): boolean {
142
145
  * narrowed value. Folding one away would drop that call.
143
146
  */
144
147
  export function hasCallbackChild(children: readonly JSXChild[]): boolean {
145
- return children.some((child) => {
148
+ for (const child of children) {
146
149
  if (child.type !== 'JSXExpressionContainer') {
147
- return false;
150
+ continue;
148
151
  }
149
152
  let expression: Node = child.expression;
150
153
  while (expression.type === 'ParenthesizedExpression') {
151
154
  expression = expression.expression;
152
155
  }
153
- return (
154
- expression.type === 'ArrowFunctionExpression' || expression.type === 'FunctionExpression'
155
- );
156
- });
156
+ if (expression.type === 'ArrowFunctionExpression' || expression.type === 'FunctionExpression') {
157
+ return true;
158
+ }
159
+ }
160
+ return false;
157
161
  }
158
162
 
159
163
  /**
package/src/memo.ts CHANGED
@@ -63,7 +63,7 @@ import type {
63
63
  } from 'oxc-parser';
64
64
  import { isFunctionNode, isIntrinsicTag, unwrap } from './ast';
65
65
  import type { PassContext } from './context';
66
- import { recordSkip, solidExport, textOf } from './context';
66
+ import { getText, recordSkip, solidExport } from './context';
67
67
  import { usesFunctionContext } from './inline';
68
68
  import type { Binding } from './scope';
69
69
  import { lookup, scopeAt } from './scope';
@@ -120,14 +120,14 @@ function constantText(context: PassContext, node: Expression): string | undefine
120
120
  const expression = unwrap(node);
121
121
  switch (expression.type) {
122
122
  case 'Literal':
123
- return 'regex' in expression ? undefined : textOf(context, expression);
123
+ return 'regex' in expression ? undefined : getText(context, expression);
124
124
  case 'TemplateLiteral':
125
- return expression.expressions.length === 0 ? textOf(context, expression) : undefined;
125
+ return expression.expressions.length === 0 ? getText(context, expression) : undefined;
126
126
  case 'UnaryExpression':
127
127
  return expression.operator === '-' &&
128
128
  expression.argument.type === 'Literal' &&
129
129
  typeof expression.argument.value === 'number'
130
- ? textOf(context, expression)
130
+ ? getText(context, expression)
131
131
  : undefined;
132
132
  default:
133
133
  return undefined;
@@ -273,7 +273,7 @@ class MemoInliner {
273
273
  /**
274
274
  * The function a node runs in.
275
275
  */
276
- private functionOf(node: Node): Node | undefined {
276
+ private findEnclosingFunction(node: Node): Node | undefined {
277
277
  let current = this.context.parents.get(node);
278
278
  while (current && !isFunctionNode(current)) {
279
279
  current = this.context.parents.get(current);
@@ -282,7 +282,7 @@ class MemoInliner {
282
282
  }
283
283
 
284
284
  /** The JSX expression container a call is the whole of. */
285
- private containerOf(call: Node): JSXExpressionContainer | undefined {
285
+ private findContainer(call: Node): JSXExpressionContainer | undefined {
286
286
  let parent = this.context.parents.get(call);
287
287
  while (parent?.type === 'ParenthesizedExpression') {
288
288
  parent = this.context.parents.get(parent);
@@ -331,7 +331,7 @@ class MemoInliner {
331
331
  return false;
332
332
  }
333
333
  } else if (
334
- this.builtInOf(element.openingElement.name) !== tag ||
334
+ this.getBuiltIn(element.openingElement.name) !== tag ||
335
335
  TRACKED_PROPS.get(tag) !== attribute
336
336
  ) {
337
337
  // A component can read a prop any number of times.
@@ -343,7 +343,7 @@ class MemoInliner {
343
343
  return this.isStaticJSX(element, fn);
344
344
  }
345
345
 
346
- private builtInOf(name: Node): string | undefined {
346
+ private getBuiltIn(name: Node): string | undefined {
347
347
  if (name.type !== 'JSXIdentifier') {
348
348
  return undefined;
349
349
  }
@@ -382,7 +382,7 @@ class MemoInliner {
382
382
  case 'ParenthesizedExpression':
383
383
  break;
384
384
  case 'ReturnStatement':
385
- return this.functionOf(parent) === fn && !this.inLoop(parent, fn);
385
+ return this.findEnclosingFunction(parent) === fn && !this.inLoop(parent, fn);
386
386
  default:
387
387
  return false;
388
388
  }
@@ -472,7 +472,7 @@ class MemoInliner {
472
472
  return undefined;
473
473
  }
474
474
  const statement = this.context.parents.get(declarator);
475
- const fn = this.functionOf(declarator);
475
+ const fn = this.findEnclosingFunction(declarator);
476
476
  if (statement?.type !== 'VariableDeclaration' || statement.declarations.length !== 1 || !fn) {
477
477
  this.reason = 'memo-declaration';
478
478
  return undefined;
@@ -488,7 +488,7 @@ class MemoInliner {
488
488
  return undefined;
489
489
  }
490
490
  // The resolved children are inserted, so the read has to be a child, not an attribute.
491
- const container = this.containerOf(call);
491
+ const container = this.findContainer(call);
492
492
  const owner = container ? this.context.parents.get(container) : undefined;
493
493
  if (
494
494
  kind === 'children' &&
@@ -506,10 +506,10 @@ class MemoInliner {
506
506
  }
507
507
  if (kind === 'children' && container) {
508
508
  // The JSX takes the place of its container, so it joins the template around it.
509
- this.context.s.overwrite(container.start, container.end, textOf(this.context, expression));
509
+ this.context.s.overwrite(container.start, container.end, getText(this.context, expression));
510
510
  } else {
511
511
  // The read sits in a JSX container, which takes any expression.
512
- this.context.s.overwrite(call.start, call.end, textOf(this.context, expression));
512
+ this.context.s.overwrite(call.start, call.end, getText(this.context, expression));
513
513
  }
514
514
  this.context.s.remove(statement.start, statement.end);
515
515
  this.changed = true;
package/src/provider.ts CHANGED
@@ -86,7 +86,7 @@ export interface Provider {
86
86
  * `useContext` throws when the value and the default are both `undefined`,
87
87
  * so a value that could be `undefined` keeps its provider.
88
88
  */
89
- export function providerOf(context: PassContext, element: JSXElement): Provider | undefined {
89
+ export function findProvider(context: PassContext, element: JSXElement): Provider | undefined {
90
90
  const binding = contextBinding(context, element.openingElement.name);
91
91
  if (!binding || element.openingElement.selfClosing) {
92
92
  return undefined;
@@ -278,8 +278,13 @@ function isAccessor(context: PassContext, binding: Binding): boolean {
278
278
  if (id.type !== 'ArrayPattern' || callee === undefined || !ACCESSOR_PAIRS.has(callee)) {
279
279
  return false;
280
280
  }
281
- const index = id.elements.findIndex((element) => element === binding.identifier);
282
- return index === 0 || index === 1;
281
+ // Only the first two slots of the pair are accessors.
282
+ for (const [index, element] of id.elements.entries()) {
283
+ if (element === binding.identifier) {
284
+ return index === 0 || index === 1;
285
+ }
286
+ }
287
+ return false;
283
288
  }
284
289
 
285
290
  export interface ClosedOptions {
@@ -457,17 +462,20 @@ function isOwnView(
457
462
  if (callee !== 'merge' && callee !== 'omit') {
458
463
  return false;
459
464
  }
460
- return call.arguments.every((argument) => {
465
+ for (const argument of call.arguments) {
461
466
  if (argument.type === 'SpreadElement') {
462
467
  return false;
463
468
  }
464
469
  const inner = unwrap(argument);
465
- return (
466
- inner.type === 'ObjectExpression' ||
467
- (inner.type === 'Literal' && typeof inner.value === 'string') ||
468
- isOwnView(context, inner, props, depth + 1)
469
- );
470
- });
470
+ if (
471
+ inner.type !== 'ObjectExpression' &&
472
+ !(inner.type === 'Literal' && typeof inner.value === 'string') &&
473
+ !isOwnView(context, inner, props, depth + 1)
474
+ ) {
475
+ return false;
476
+ }
477
+ }
478
+ return true;
471
479
  }
472
480
 
473
481
  function isVisibleElement(
@@ -533,7 +541,7 @@ function isAccessorValue(
533
541
  return false;
534
542
  }
535
543
  if (expression.type === 'CallExpression') {
536
- const value = contextValueOf(context, expression, options);
544
+ const value = findContextValue(context, expression, options);
537
545
  return value !== undefined && isAccessorValue(context, value, options, depth + 1);
538
546
  }
539
547
  if (expression.type !== 'Identifier') {
@@ -569,7 +577,7 @@ function constantInit(context: PassContext, binding: Binding): Expression | unde
569
577
  /**
570
578
  * The value `useContext(Ctx)` returns, when the provider is known.
571
579
  */
572
- function contextValueOf(
580
+ function findContextValue(
573
581
  context: PassContext,
574
582
  call: CallExpression,
575
583
  options: ClosedOptions,
@@ -600,18 +608,20 @@ function isVisibleMethod(
600
608
  if (member.computed || member.property.type !== 'Identifier' || member.optional) {
601
609
  return false;
602
610
  }
603
- const object = objectLiteralOf(context, member.object, options, 0);
604
- if (!object || object.properties.some((property) => property.type !== 'Property')) {
611
+ const object = findObjectLiteral(context, member.object, options, 0);
612
+ if (!object) {
605
613
  return false;
606
614
  }
607
615
  const { name } = member.property;
608
- const matches = object.properties.filter(
609
- (property) =>
610
- property.type === 'Property' &&
611
- !property.computed &&
612
- property.key.type === 'Identifier' &&
613
- property.key.name === name,
614
- );
616
+ const matches: typeof object.properties = [];
617
+ for (const property of object.properties) {
618
+ if (property.type !== 'Property') {
619
+ return false;
620
+ }
621
+ if (!property.computed && property.key.type === 'Identifier' && property.key.name === name) {
622
+ matches.push(property);
623
+ }
624
+ }
615
625
  const property = matches.at(0);
616
626
  if (matches.length !== 1 || property?.type !== 'Property' || property.kind !== 'init') {
617
627
  return false;
@@ -637,7 +647,7 @@ function isVisibleMethod(
637
647
  * The object literal an expression evaluates to, through bindings that
638
648
  * never change and reads of a context whose value is known.
639
649
  */
640
- function objectLiteralOf(
650
+ function findObjectLiteral(
641
651
  context: PassContext,
642
652
  node: Expression,
643
653
  options: ClosedOptions,
@@ -651,15 +661,15 @@ function objectLiteralOf(
651
661
  return expression;
652
662
  }
653
663
  if (expression.type === 'CallExpression') {
654
- const value = contextValueOf(context, expression, options);
655
- return value ? objectLiteralOf(context, value, options, depth + 1) : undefined;
664
+ const value = findContextValue(context, expression, options);
665
+ return value ? findObjectLiteral(context, value, options, depth + 1) : undefined;
656
666
  }
657
667
  if (expression.type !== 'Identifier') {
658
668
  return undefined;
659
669
  }
660
670
  const binding = context.scopes.references.get(expression);
661
671
  const init = binding ? constantInit(context, binding) : undefined;
662
- return init ? objectLiteralOf(context, init, options, depth + 1) : undefined;
672
+ return init ? findObjectLiteral(context, init, options, depth + 1) : undefined;
663
673
  }
664
674
 
665
675
  /**
@@ -731,7 +741,7 @@ export function findProviders(context: PassContext): Provider[] {
731
741
  const providers: Provider[] = [];
732
742
  const visit = (node: Node): void => {
733
743
  if (node.type === 'JSXElement') {
734
- const provider = providerOf(context, node);
744
+ const provider = findProvider(context, node);
735
745
  if (provider) {
736
746
  providers.push(provider);
737
747
  }
@@ -754,9 +764,13 @@ class ProviderRemover {
754
764
  // A provider inside one removed in this pass waits for the next pass,
755
765
  // so their edits do not overlap.
756
766
  const { element } = provider;
757
- const overlaps = this.removed.some(
758
- (other) => element.start < other.end && other.start < element.end,
759
- );
767
+ let overlaps = false;
768
+ for (const other of this.removed) {
769
+ if (element.start < other.end && other.start < element.end) {
770
+ overlaps = true;
771
+ break;
772
+ }
773
+ }
760
774
  if (overlaps) {
761
775
  continue;
762
776
  }
@@ -783,7 +797,7 @@ class ProviderRemover {
783
797
  const visit = (node: Node): void => {
784
798
  if (node.type === 'JSXElement') {
785
799
  const binding = contextBinding(this.context, node.openingElement.name);
786
- if (binding && !providerOf(this.context, node)) {
800
+ if (binding && !findProvider(this.context, node)) {
787
801
  // A lone `value` is turned down because it could be `undefined`.
788
802
  const { attributes } = node.openingElement;
789
803
  const attribute = attributes.at(0);
@@ -809,20 +823,28 @@ class ProviderRemover {
809
823
  const { element } = provider;
810
824
  const children = element.children;
811
825
  let reason = 'open-other';
812
- const closed = children.every((child) =>
813
- isClosed(this.context, child, {
814
- component: () => false,
815
- contextValue: (target) => (target === provider.context ? provider.value : undefined),
816
- onOpen: (open) => {
817
- reason = open;
818
- },
819
- }),
820
- );
826
+ const closure: ClosedOptions = {
827
+ component: () => false,
828
+ contextValue: (target) => (target === provider.context ? provider.value : undefined),
829
+ onOpen: (open) => {
830
+ reason = open;
831
+ },
832
+ };
833
+ let closed = true;
834
+ for (const child of children) {
835
+ if (!isClosed(this.context, child, closure)) {
836
+ closed = false;
837
+ break;
838
+ }
839
+ }
821
840
  if (!closed) {
822
841
  this.skip(provider, reason);
823
842
  return false;
824
843
  }
825
- const reads = children.flatMap((child) => contextReads(this.context, child, provider.context));
844
+ const reads: ReturnType<typeof contextReads> = [];
845
+ for (const child of children) {
846
+ reads.push(...contextReads(this.context, child, provider.context));
847
+ }
826
848
  const texts: string[] = [];
827
849
  for (const read of reads) {
828
850
  const text = stableValueText(this.context, provider, read);
package/src/size.ts CHANGED
@@ -248,7 +248,12 @@ export class SizeEstimator {
248
248
  size += name.length + GETTER + this.jsx(value, false);
249
249
  }
250
250
  }
251
- const significant = element.children.filter((child) => !isInsignificant(child));
251
+ const significant: JSXChild[] = [];
252
+ for (const child of element.children) {
253
+ if (!isInsignificant(child)) {
254
+ significant.push(child);
255
+ }
256
+ }
252
257
  if (significant.length > 0) {
253
258
  size += GETTER + 'children'.length + this.children(significant);
254
259
  }
package/src/value.ts CHANGED
@@ -240,6 +240,15 @@ export function truthiness(node: Node, lookup: ConstantLookup): boolean | undefi
240
240
  return value ? !!value.value : undefined;
241
241
  }
242
242
 
243
+ function areSideEffectFree(nodes: readonly Node[]): boolean {
244
+ for (const node of nodes) {
245
+ if (!isSideEffectFree(node)) {
246
+ return false;
247
+ }
248
+ }
249
+ return true;
250
+ }
251
+
243
252
  /**
244
253
  * Whether skipping `node` entirely loses nothing observable.
245
254
  *
@@ -259,7 +268,7 @@ export function isSideEffectFree(node: Node): boolean {
259
268
  case 'ParenthesizedExpression':
260
269
  return isSideEffectFree(node.expression);
261
270
  case 'TemplateLiteral':
262
- return node.expressions.every(isSideEffectFree);
271
+ return areSideEffectFree(node.expressions);
263
272
  case 'UnaryExpression':
264
273
  // `delete` changes its target.
265
274
  return node.operator !== 'delete' && isSideEffectFree(node.argument);
@@ -275,7 +284,7 @@ export function isSideEffectFree(node: Node): boolean {
275
284
  case 'LogicalExpression':
276
285
  return isSideEffectFree(node.left) && isSideEffectFree(node.right);
277
286
  case 'SequenceExpression':
278
- return node.expressions.every(isSideEffectFree);
287
+ return areSideEffectFree(node.expressions);
279
288
  case 'ConditionalExpression':
280
289
  return (
281
290
  isSideEffectFree(node.test) &&
@@ -284,18 +293,24 @@ export function isSideEffectFree(node: Node): boolean {
284
293
  );
285
294
  case 'ArrayExpression':
286
295
  // A spread iterates its argument, which is observable.
287
- return node.elements.every(
288
- (element) =>
289
- element === null || (element.type !== 'SpreadElement' && isSideEffectFree(element)),
290
- );
296
+ for (const element of node.elements) {
297
+ if (element !== null && (element.type === 'SpreadElement' || !isSideEffectFree(element))) {
298
+ return false;
299
+ }
300
+ }
301
+ return true;
291
302
  case 'ObjectExpression':
292
- return node.properties.every(
293
- (property) =>
294
- // A spread reads the source's own properties, which a getter observes.
295
- property.type === 'Property' &&
296
- (!property.computed || isSideEffectFree(property.key)) &&
297
- isSideEffectFree(property.value),
298
- );
303
+ for (const property of node.properties) {
304
+ // A spread reads the source's own properties, which a getter observes.
305
+ if (
306
+ property.type !== 'Property' ||
307
+ (property.computed && !isSideEffectFree(property.key)) ||
308
+ !isSideEffectFree(property.value)
309
+ ) {
310
+ return false;
311
+ }
312
+ }
313
+ return true;
299
314
  default:
300
315
  // A class is never skippable. Evaluating one runs its heritage
301
316
  // expression, computed keys, static fields, and static blocks.
@@ -333,8 +348,10 @@ export function arrayLiteralLength(node: Node): number | undefined {
333
348
  if (node.type !== 'ArrayExpression') {
334
349
  return undefined;
335
350
  }
336
- if (node.elements.some((element) => element?.type === 'SpreadElement')) {
337
- return undefined;
351
+ for (const element of node.elements) {
352
+ if (element?.type === 'SpreadElement') {
353
+ return undefined;
354
+ }
338
355
  }
339
356
  return node.elements.length;
340
357
  }
@@ -24,18 +24,33 @@ export function sharedGlobals(
24
24
  }
25
25
  }
26
26
  const environments = Object.values(config.environments);
27
- const defines =
28
- environments.length > 0
29
- ? environments.map((environment) => environment.define ?? config.define ?? {})
30
- : [config.define ?? {}];
31
- const keys = new Set(defines.flatMap((define) => Object.keys(define)));
27
+ const defines: Record<string, unknown>[] = [];
28
+ if (environments.length > 0) {
29
+ for (const environment of environments) {
30
+ defines.push(environment.define ?? config.define ?? {});
31
+ }
32
+ } else {
33
+ defines.push(config.define ?? {});
34
+ }
35
+ const keys = new Set<string>();
36
+ for (const define of defines) {
37
+ for (const key of Object.keys(define)) {
38
+ keys.add(key);
39
+ }
40
+ }
32
41
  for (const key of keys) {
33
- const values = defines.map((define) => defineValue(define[key]));
34
- const first = values.at(0);
35
- if (
36
- first !== undefined &&
37
- values.every((value) => value !== undefined && Object.is(value.value, first.value))
38
- ) {
42
+ // Every environment must define the key as the same value.
43
+ let first: { value: Primitive } | undefined;
44
+ let same = true;
45
+ for (const define of defines) {
46
+ const value = defineValue(define[key]);
47
+ first ??= value;
48
+ if (value === undefined || !Object.is(value.value, first?.value)) {
49
+ same = false;
50
+ break;
51
+ }
52
+ }
53
+ if (first !== undefined && same) {
39
54
  globals[key] = first.value;
40
55
  }
41
56
  }