@typestyles/migrate 0.4.0 → 0.5.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/dist/cli.cjs CHANGED
@@ -302,7 +302,7 @@ function buildVariantStyles(properties) {
302
302
  )
303
303
  );
304
304
  }
305
- function buildVariantComponentConfig(baseObject, booleanInterpolations) {
305
+ function buildVariantRecipeInput(name, baseObject, booleanInterpolations) {
306
306
  const variantGroups = /* @__PURE__ */ new Map();
307
307
  for (const interpolation of booleanInterpolations) {
308
308
  const group = variantGroups.get(interpolation.propName) ?? { true: {}, false: {} };
@@ -319,13 +319,24 @@ function buildVariantComponentConfig(baseObject, booleanInterpolations) {
319
319
  ])
320
320
  )
321
321
  );
322
- const defaultVariantProperties = [...variantGroups.keys()].map(
323
- (propName) => t4__namespace.objectProperty(t4__namespace.identifier(propName), t4__namespace.booleanLiteral(false))
322
+ const axisProperties = [...variantGroups.keys()].map(
323
+ (propName) => t4__namespace.objectProperty(
324
+ t4__namespace.identifier(propName),
325
+ t4__namespace.callExpression(t4__namespace.memberExpression(t4__namespace.identifier("variant"), t4__namespace.identifier("boolean")), [
326
+ t4__namespace.objectExpression([t4__namespace.objectProperty(t4__namespace.identifier("default"), t4__namespace.booleanLiteral(false))])
327
+ ])
328
+ )
324
329
  );
325
330
  return t4__namespace.objectExpression([
326
- t4__namespace.objectProperty(t4__namespace.identifier("base"), baseObject),
327
- t4__namespace.objectProperty(t4__namespace.identifier("variants"), t4__namespace.objectExpression(variantsProperties)),
328
- t4__namespace.objectProperty(t4__namespace.identifier("defaultVariants"), t4__namespace.objectExpression(defaultVariantProperties))
331
+ t4__namespace.objectProperty(t4__namespace.identifier("name"), t4__namespace.stringLiteral(name)),
332
+ t4__namespace.objectProperty(t4__namespace.identifier("variants"), t4__namespace.objectExpression(axisProperties)),
333
+ t4__namespace.objectProperty(
334
+ t4__namespace.identifier("styles"),
335
+ t4__namespace.objectExpression([
336
+ t4__namespace.objectProperty(t4__namespace.identifier("base"), baseObject),
337
+ t4__namespace.objectProperty(t4__namespace.identifier("variants"), t4__namespace.objectExpression(variantsProperties))
338
+ ])
339
+ )
329
340
  ]);
330
341
  }
331
342
 
@@ -405,17 +416,38 @@ function updateClassNameAttribute(openingElement, classNameExpression) {
405
416
  );
406
417
  }
407
418
  }
408
- function ensureTypestylesImport(ast, needsVars) {
409
- const requiredSpecifiers = /* @__PURE__ */ new Set(["styles"]);
410
- if (needsVars) {
419
+ function hasLocalBinding(ast, name) {
420
+ for (const statement of ast.program.body) {
421
+ if (!t4__namespace.isVariableDeclaration(statement)) continue;
422
+ for (const declarator of statement.declarations) {
423
+ if (t4__namespace.isIdentifier(declarator.id) && declarator.id.name === name) return true;
424
+ if (t4__namespace.isObjectPattern(declarator.id)) {
425
+ for (const prop of declarator.id.properties) {
426
+ if (t4__namespace.isObjectProperty(prop) && t4__namespace.isIdentifier(prop.value) && prop.value.name === name) {
427
+ return true;
428
+ }
429
+ }
430
+ }
431
+ }
432
+ }
433
+ return false;
434
+ }
435
+ function ensureTypestylesRuntime(ast, needs) {
436
+ const requiredSpecifiers = /* @__PURE__ */ new Set(["createTypeStyles"]);
437
+ if (needs.recipe) requiredSpecifiers.add("variant");
438
+ if (needs.vars) {
411
439
  requiredSpecifiers.add("createVar");
412
440
  requiredSpecifiers.add("assignVars");
413
441
  }
414
442
  let typestylesImport = null;
415
- for (const statement of ast.program.body) {
416
- if (t4__namespace.isImportDeclaration(statement) && statement.source.value === "typestyles") {
417
- typestylesImport = statement;
418
- break;
443
+ let lastImportIndex = -1;
444
+ for (let i = 0; i < ast.program.body.length; i++) {
445
+ const statement = ast.program.body[i];
446
+ if (t4__namespace.isImportDeclaration(statement)) {
447
+ lastImportIndex = i;
448
+ if (statement.source.value === "typestyles" && typestylesImport == null) {
449
+ typestylesImport = statement;
450
+ }
419
451
  }
420
452
  }
421
453
  const buildSpecifiers = (names) => [...names].map((name) => t4__namespace.importSpecifier(t4__namespace.identifier(name), t4__namespace.identifier(name)));
@@ -423,16 +455,36 @@ function ensureTypestylesImport(ast, needsVars) {
423
455
  ast.program.body.unshift(
424
456
  t4__namespace.importDeclaration(buildSpecifiers(requiredSpecifiers), t4__namespace.stringLiteral("typestyles"))
425
457
  );
426
- return;
458
+ lastImportIndex = 0;
459
+ } else {
460
+ for (const name of requiredSpecifiers) {
461
+ const hasSpecifier = typestylesImport.specifiers.some(
462
+ (specifier) => t4__namespace.isImportSpecifier(specifier) && t4__namespace.isIdentifier(specifier.imported, { name }) && t4__namespace.isIdentifier(specifier.local, { name })
463
+ );
464
+ if (!hasSpecifier) {
465
+ typestylesImport.specifiers.push(t4__namespace.importSpecifier(t4__namespace.identifier(name), t4__namespace.identifier(name)));
466
+ }
467
+ }
427
468
  }
428
- for (const name of requiredSpecifiers) {
429
- const hasSpecifier = typestylesImport.specifiers.some(
430
- (specifier) => t4__namespace.isImportSpecifier(specifier) && t4__namespace.isIdentifier(specifier.imported, { name }) && t4__namespace.isIdentifier(specifier.local, { name })
469
+ const destructureProps = [];
470
+ if (needs.style && !hasLocalBinding(ast, "style")) {
471
+ destructureProps.push(
472
+ t4__namespace.objectProperty(t4__namespace.identifier("style"), t4__namespace.identifier("style"), false, true)
473
+ );
474
+ }
475
+ if (needs.recipe && !hasLocalBinding(ast, "recipe")) {
476
+ destructureProps.push(
477
+ t4__namespace.objectProperty(t4__namespace.identifier("recipe"), t4__namespace.identifier("recipe"), false, true)
431
478
  );
432
- if (!hasSpecifier) {
433
- typestylesImport.specifiers.push(t4__namespace.importSpecifier(t4__namespace.identifier(name), t4__namespace.identifier(name)));
434
- }
435
479
  }
480
+ if (destructureProps.length === 0) return;
481
+ const factoryDecl = t4__namespace.variableDeclaration("const", [
482
+ t4__namespace.variableDeclarator(
483
+ t4__namespace.objectPattern(destructureProps),
484
+ t4__namespace.callExpression(t4__namespace.identifier("createTypeStyles"), [])
485
+ )
486
+ ]);
487
+ ast.program.body.splice(lastImportIndex + 1, 0, factoryDecl);
436
488
  }
437
489
  function cleanupUnusedImports(ast) {
438
490
  traverse__default.default(ast, {
@@ -441,7 +493,7 @@ function cleanupUnusedImports(ast) {
441
493
  },
442
494
  ImportDeclaration(path) {
443
495
  const unusedLocals = path.node.specifiers.filter((specifier) => {
444
- if (path.node.source.value === "typestyles" && t4__namespace.isImportSpecifier(specifier) && (specifier.local.name === "styles" || specifier.local.name === "createVar" || specifier.local.name === "assignVars")) {
496
+ if (path.node.source.value === "typestyles" && t4__namespace.isImportSpecifier(specifier) && (specifier.local.name === "createTypeStyles" || specifier.local.name === "createVar" || specifier.local.name === "assignVars")) {
445
497
  return false;
446
498
  }
447
499
  const local = specifier.local.name;
@@ -526,10 +578,8 @@ function removeStyledProps(openingElement, propNames) {
526
578
  }
527
579
  function buildComponentClassNameExpression(componentConstName, variantProps, propValues) {
528
580
  const properties = variantProps.filter((propName) => propValues.has(propName)).map((propName) => t4__namespace.objectProperty(t4__namespace.identifier(propName), propValues.get(propName)));
529
- if (properties.length === 0) {
530
- return t4__namespace.callExpression(t4__namespace.identifier(componentConstName), []);
531
- }
532
- return t4__namespace.callExpression(t4__namespace.identifier(componentConstName), [t4__namespace.objectExpression(properties)]);
581
+ const call = properties.length === 0 ? t4__namespace.callExpression(t4__namespace.identifier(componentConstName), []) : t4__namespace.callExpression(t4__namespace.identifier(componentConstName), [t4__namespace.objectExpression(properties)]);
582
+ return t4__namespace.memberExpression(call, t4__namespace.identifier("className"));
533
583
  }
534
584
  function migrateBooleanVariantTemplate(path, variableName, template, styledTarget, warnings, styledTransforms) {
535
585
  const booleanInterpolations = parseBooleanTemplateInterpolations(template);
@@ -543,7 +593,11 @@ function migrateBooleanVariantTemplate(path, variableName, template, styledTarge
543
593
  const componentConstName = path.scope.generateUidIdentifier(
544
594
  toComponentConstName(variableName)
545
595
  ).name;
546
- const componentConfig = buildVariantComponentConfig(baseObject, booleanInterpolations);
596
+ const recipeInput = buildVariantRecipeInput(
597
+ toKebabCase(variableName),
598
+ baseObject,
599
+ booleanInterpolations
600
+ );
547
601
  const variantProps = [
548
602
  ...new Set(booleanInterpolations.map((interpolation) => interpolation.propName))
549
603
  ];
@@ -552,10 +606,7 @@ function migrateBooleanVariantTemplate(path, variableName, template, styledTarge
552
606
  declaration.node.declarations = [
553
607
  t4__namespace.variableDeclarator(
554
608
  t4__namespace.identifier(componentConstName),
555
- t4__namespace.callExpression(t4__namespace.memberExpression(t4__namespace.identifier("styles"), t4__namespace.identifier("component")), [
556
- t4__namespace.stringLiteral(toKebabCase(variableName)),
557
- componentConfig
558
- ])
609
+ t4__namespace.callExpression(t4__namespace.identifier("recipe"), [recipeInput])
559
610
  )
560
611
  ];
561
612
  styledTransforms.set(variableName, {
@@ -613,7 +664,7 @@ function migrateInterpolatedTemplate(path, variableName, template, styledTarget,
613
664
  ...varDeclarators,
614
665
  t4__namespace.variableDeclarator(
615
666
  t4__namespace.identifier(classConstName),
616
- t4__namespace.callExpression(t4__namespace.memberExpression(t4__namespace.identifier("styles"), t4__namespace.identifier("class")), [
667
+ t4__namespace.callExpression(t4__namespace.identifier("style"), [
617
668
  t4__namespace.stringLiteral(toKebabCase(variableName)),
618
669
  objectExpression4
619
670
  ])
@@ -647,6 +698,8 @@ function migrateSource(filePath, source) {
647
698
  const cssTagNames = /* @__PURE__ */ new Set();
648
699
  const styledTransforms = /* @__PURE__ */ new Map();
649
700
  let needsVars = false;
701
+ let needsStyle = false;
702
+ let needsRecipe = false;
650
703
  traverse__default.default(ast, {
651
704
  ImportDeclaration(path) {
652
705
  if (path.node.source.value === "styled-components" || path.node.source.value === "@emotion/styled") {
@@ -708,6 +761,7 @@ function migrateSource(filePath, source) {
708
761
  warnings,
709
762
  styledTransforms
710
763
  )) {
764
+ needsRecipe = true;
711
765
  changed = true;
712
766
  return;
713
767
  }
@@ -720,6 +774,7 @@ function migrateSource(filePath, source) {
720
774
  styledTransforms
721
775
  )) {
722
776
  needsVars = true;
777
+ needsStyle = true;
723
778
  changed = true;
724
779
  }
725
780
  return;
@@ -746,10 +801,10 @@ function migrateSource(filePath, source) {
746
801
  }
747
802
  const classConstName = path.scope.generateUidIdentifier(`${variableName}Class`).name;
748
803
  path.node.id = t4__namespace.identifier(classConstName);
749
- path.node.init = t4__namespace.callExpression(
750
- t4__namespace.memberExpression(t4__namespace.identifier("styles"), t4__namespace.identifier("class")),
751
- [t4__namespace.stringLiteral(toKebabCase(variableName)), objectExpression4]
752
- );
804
+ path.node.init = t4__namespace.callExpression(t4__namespace.identifier("style"), [
805
+ t4__namespace.stringLiteral(toKebabCase(variableName)),
806
+ objectExpression4
807
+ ]);
753
808
  styledTransforms.set(variableName, {
754
809
  originalName: variableName,
755
810
  mode: "class",
@@ -758,14 +813,16 @@ function migrateSource(filePath, source) {
758
813
  propVars: [],
759
814
  variantProps: []
760
815
  });
816
+ needsStyle = true;
761
817
  changed = true;
762
818
  return;
763
819
  }
764
820
  if (t4__namespace.isIdentifier(path.node.init.tag) && cssTagNames.has(path.node.init.tag.name)) {
765
- path.node.init = t4__namespace.callExpression(
766
- t4__namespace.memberExpression(t4__namespace.identifier("styles"), t4__namespace.identifier("class")),
767
- [t4__namespace.stringLiteral(toKebabCase(variableName)), objectExpression4]
768
- );
821
+ path.node.init = t4__namespace.callExpression(t4__namespace.identifier("style"), [
822
+ t4__namespace.stringLiteral(toKebabCase(variableName)),
823
+ objectExpression4
824
+ ]);
825
+ needsStyle = true;
769
826
  changed = true;
770
827
  }
771
828
  }
@@ -803,7 +860,11 @@ function migrateSource(filePath, source) {
803
860
  });
804
861
  }
805
862
  if (changed) {
806
- ensureTypestylesImport(ast, needsVars);
863
+ ensureTypestylesRuntime(ast, {
864
+ style: needsStyle,
865
+ recipe: needsRecipe,
866
+ vars: needsVars
867
+ });
807
868
  cleanupUnusedImports(ast);
808
869
  }
809
870
  const output = generate__default.default(