solid-optimizer 0.3.0 → 0.4.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/src/provider.ts CHANGED
@@ -40,7 +40,7 @@ import type {
40
40
  } from 'oxc-parser';
41
41
  import { forEachChild, isFunctionNode, isIntrinsicTag, unwrap } from './ast';
42
42
  import type { PassContext } from './context';
43
- import { isConstantDeclaration, solidExport } from './context';
43
+ import { isConstantDeclaration, recordSkip, solidExport } from './context';
44
44
  import type { Binding } from './scope';
45
45
  import { lookup, scopeAt } from './scope';
46
46
  import { evaluate, isSideEffectFree, literalText } from './value';
@@ -301,6 +301,8 @@ export interface ClosedOptions {
301
301
  readonly props?: Binding;
302
302
  /** The value a read of a context gets, when the provider is known. */
303
303
  readonly contextValue?: (target: Binding) => Expression | undefined;
304
+ /** Called with a short code for the code that is not visible, for the report. */
305
+ readonly onOpen?: (reason: string) => void;
304
306
  }
305
307
 
306
308
  /**
@@ -314,6 +316,10 @@ export interface ClosedOptions {
314
316
  */
315
317
  export function isClosed(context: PassContext, root: Node, options: ClosedOptions): boolean {
316
318
  let closed = true;
319
+ const open = (reason: string): void => {
320
+ closed = false;
321
+ options.onOpen?.(reason);
322
+ };
317
323
  const visit = (node: Node): void => {
318
324
  if (!closed) {
319
325
  return;
@@ -327,25 +333,27 @@ export function isClosed(context: PassContext, root: Node, options: ClosedOption
327
333
  case 'JSXSpreadAttribute':
328
334
  // A view of the component's own props is expanded when it inlines.
329
335
  if (!isOwnView(context, node.argument, options.props, 0)) {
330
- closed = false;
336
+ open('open-spread');
331
337
  return;
332
338
  }
333
339
  break;
334
- case 'JSXSpreadChild':
335
340
  case 'NewExpression':
341
+ open('open-new');
342
+ return;
343
+ case 'JSXSpreadChild':
336
344
  case 'TaggedTemplateExpression':
337
345
  case 'ImportExpression':
338
- closed = false;
346
+ open('open-other');
339
347
  return;
340
348
  case 'JSXElement':
341
349
  if (!isVisibleElement(context, node, options)) {
342
- closed = false;
350
+ open('open-component');
343
351
  return;
344
352
  }
345
353
  break;
346
354
  case 'CallExpression':
347
355
  if (!isVisibleCall(context, node, options)) {
348
- closed = false;
356
+ open('open-call');
349
357
  return;
350
358
  }
351
359
  break;
@@ -355,7 +363,7 @@ export function isClosed(context: PassContext, root: Node, options: ClosedOption
355
363
  // binding declared under `root`, like a callback's parameter, is a value.
356
364
  const binding = context.scopes.references.get(node);
357
365
  if (binding && runsOutsideCode(context, binding, root, options, 0)) {
358
- closed = false;
366
+ open('open-outer');
359
367
  return;
360
368
  }
361
369
  break;
@@ -784,22 +792,69 @@ class ProviderRemover {
784
792
  const overlaps = this.removed.some(
785
793
  (other) => element.start < other.end && other.start < element.end,
786
794
  );
787
- if (!overlaps && !hasNestedProvider(this.context, provider) && this.tryRemove(provider)) {
795
+ if (overlaps) {
796
+ continue;
797
+ }
798
+ if (hasNestedProvider(this.context, provider)) {
799
+ this.skip(provider, 'provider-nested');
800
+ } else if (this.tryRemove(provider)) {
788
801
  this.removed.push(element);
789
802
  }
790
803
  }
804
+ if (this.context.skips) {
805
+ this.reportShapes();
806
+ }
807
+ }
808
+
809
+ private skip(provider: Provider, reason: string): void {
810
+ recordSkip(this.context, 'contexts', reason, provider.context.name);
811
+ }
812
+
813
+ /**
814
+ * Records each context tag that is not a provider this pass understands,
815
+ * such as one with more attributes than `value`.
816
+ */
817
+ private reportShapes(): void {
818
+ const visit = (node: Node): void => {
819
+ if (node.type === 'JSXElement') {
820
+ const binding = providerContext(this.context, node.openingElement.name);
821
+ if (binding && !providerOf(this.context, node)) {
822
+ // A lone `value` is turned down because it could be `undefined`.
823
+ const { attributes } = node.openingElement;
824
+ const attribute = attributes.at(0);
825
+ const onlyValue =
826
+ attributes.length === 1 &&
827
+ attribute?.type === 'JSXAttribute' &&
828
+ attribute.name.type === 'JSXIdentifier' &&
829
+ attribute.name.name === 'value';
830
+ recordSkip(
831
+ this.context,
832
+ 'contexts',
833
+ onlyValue && !node.openingElement.selfClosing ? 'provider-undefined' : 'provider-shape',
834
+ binding.name,
835
+ );
836
+ }
837
+ }
838
+ forEachChild(node, visit);
839
+ };
840
+ visit(this.context.program);
791
841
  }
792
842
 
793
843
  private tryRemove(provider: Provider): boolean {
794
844
  const { element } = provider;
795
845
  const children = element.children;
846
+ let reason = 'open-other';
796
847
  const closed = children.every((child) =>
797
848
  isClosed(this.context, child, {
798
849
  component: () => false,
799
850
  contextValue: (target) => (target === provider.context ? provider.value : undefined),
851
+ onOpen: (open) => {
852
+ reason = open;
853
+ },
800
854
  }),
801
855
  );
802
856
  if (!closed) {
857
+ this.skip(provider, reason);
803
858
  return false;
804
859
  }
805
860
  const reads = children.flatMap((child) => contextReads(this.context, child, provider.context));
@@ -807,6 +862,7 @@ class ProviderRemover {
807
862
  for (const read of reads) {
808
863
  const text = stableValueText(this.context, provider, read);
809
864
  if (text === undefined) {
865
+ this.skip(provider, 'provider-value');
810
866
  return false;
811
867
  }
812
868
  texts.push(text);
@@ -815,6 +871,7 @@ class ProviderRemover {
815
871
  if (reads.length === 0 && !isSideEffectFree(provider.value)) {
816
872
  const stable = stableValueText(this.context, provider, element);
817
873
  if (stable === undefined) {
874
+ this.skip(provider, 'provider-value');
818
875
  return false;
819
876
  }
820
877
  }
package/src/size.ts CHANGED
@@ -278,6 +278,8 @@ export function copySize(
278
278
  parents: collectParents(program),
279
279
  scopes: analyzeScopes(program),
280
280
  options,
281
+ // Only the copy is measured, so nothing it skips is reported.
282
+ skips: undefined,
281
283
  });
282
284
  if (!changed) {
283
285
  break;