@typestyles/migrate 0.0.0-unstable.439960c19a5a → 0.0.0-unstable.446478552a48

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/index.cjs CHANGED
@@ -4,7 +4,7 @@
4
4
  var parser = require('@babel/parser');
5
5
  var generate = require('@babel/generator');
6
6
  var traverse = require('@babel/traverse');
7
- var t2 = require('@babel/types');
7
+ var t4 = require('@babel/types');
8
8
  var postcss = require('postcss');
9
9
  var promises = require('fs/promises');
10
10
  var path = require('path');
@@ -34,7 +34,7 @@ function _interopNamespace(e) {
34
34
 
35
35
  var generate__default = /*#__PURE__*/_interopDefault(generate);
36
36
  var traverse__default = /*#__PURE__*/_interopDefault(traverse);
37
- var t2__namespace = /*#__PURE__*/_interopNamespace(t2);
37
+ var t4__namespace = /*#__PURE__*/_interopNamespace(t4);
38
38
  var postcss__default = /*#__PURE__*/_interopDefault(postcss);
39
39
  var fg__default = /*#__PURE__*/_interopDefault(fg);
40
40
  var picomatch__default = /*#__PURE__*/_interopDefault(picomatch);
@@ -42,42 +42,53 @@ var picomatch__default = /*#__PURE__*/_interopDefault(picomatch);
42
42
  function camelCaseProperty(property) {
43
43
  return property.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
44
44
  }
45
- function toValueNode(value) {
45
+ function toValueNode(value, varReplacements) {
46
46
  const trimmed = value.trim();
47
+ if (varReplacements?.has(trimmed)) {
48
+ return varReplacements.get(trimmed);
49
+ }
47
50
  if (/^-?\d+(\.\d+)?$/.test(trimmed)) {
48
- return t2__namespace.numericLiteral(Number(trimmed));
51
+ return t4__namespace.numericLiteral(Number(trimmed));
49
52
  }
50
- return t2__namespace.stringLiteral(trimmed);
53
+ return t4__namespace.stringLiteral(trimmed);
51
54
  }
52
55
  function toKeyNode(key) {
53
56
  if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key)) {
54
- return t2__namespace.identifier(key);
57
+ return t4__namespace.identifier(key);
55
58
  }
56
- return t2__namespace.stringLiteral(key);
59
+ return t4__namespace.stringLiteral(key);
57
60
  }
58
- function nodesToObject(nodes, warnings) {
61
+ function nodesToObject(nodes, warnings, varReplacements) {
59
62
  const properties = [];
60
63
  for (const node of nodes ?? []) {
61
64
  if (node.type === "decl") {
62
65
  const normalized = node.prop.startsWith("--") ? node.prop : camelCaseProperty(node.prop);
63
- properties.push(t2__namespace.objectProperty(toKeyNode(normalized), toValueNode(node.value)));
66
+ properties.push(
67
+ t4__namespace.objectProperty(toKeyNode(normalized), toValueNode(node.value, varReplacements))
68
+ );
64
69
  continue;
65
70
  }
66
71
  if (node.type === "rule") {
67
- const nested = nodesToObject(node.nodes, warnings);
68
- properties.push(t2__namespace.objectProperty(t2__namespace.stringLiteral(node.selector.trim()), nested));
72
+ const nested = nodesToObject(node.nodes, warnings, varReplacements);
73
+ properties.push(t4__namespace.objectProperty(t4__namespace.stringLiteral(node.selector.trim()), nested));
74
+ continue;
75
+ }
76
+ if (node.type === "atrule") {
77
+ const nested = nodesToObject(node.nodes, warnings, varReplacements);
78
+ const atRuleKey = `@${node.name}${node.params ? ` ${node.params}` : ""}`;
79
+ properties.push(t4__namespace.objectProperty(t4__namespace.stringLiteral(atRuleKey), nested));
69
80
  continue;
70
81
  }
71
82
  warnings.push({
72
83
  message: `Unsupported CSS node "${node.type}" skipped.`
73
84
  });
74
85
  }
75
- return t2__namespace.objectExpression(properties);
86
+ return t4__namespace.objectExpression(properties);
76
87
  }
77
- function cssToObjectExpression(cssText, warnings) {
88
+ function cssToObjectExpression(cssText, warnings, varReplacements) {
78
89
  try {
79
90
  const root = postcss__default.default.parse(cssText);
80
- return nodesToObject(root.nodes, warnings);
91
+ return nodesToObject(root.nodes, warnings, varReplacements);
81
92
  } catch (error) {
82
93
  warnings.push({
83
94
  message: `Could not parse CSS template literal: ${error.message}`
@@ -85,30 +96,226 @@ function cssToObjectExpression(cssText, warnings) {
85
96
  return null;
86
97
  }
87
98
  }
99
+ var PLACEHOLDER_PREFIX = "__ts_migrate_var_";
100
+ function camelCaseProperty2(property) {
101
+ return property.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
102
+ }
103
+ function placeholderToken(index) {
104
+ return `${PLACEHOLDER_PREFIX}${index}__`;
105
+ }
106
+ function extractSuffixFromQuasi(quasiText) {
107
+ const match = quasiText.match(/^([^;\n]*)/);
108
+ return match?.[1] ?? "";
109
+ }
110
+ function literalToCssValue(node) {
111
+ if (t4__namespace.isStringLiteral(node)) return node.value;
112
+ if (t4__namespace.isNumericLiteral(node)) return String(node.value);
113
+ return null;
114
+ }
115
+ function unwrapExpression(expression) {
116
+ if (t4__namespace.isParenthesizedExpression(expression)) {
117
+ return unwrapExpression(expression.expression);
118
+ }
119
+ return expression;
120
+ }
121
+ function extractCssPropertyBeforeInterpolation(quasiText) {
122
+ const match = quasiText.match(/([a-zA-Z-]+)\s*:\s*$/);
123
+ return match?.[1] ?? null;
124
+ }
125
+ function stripTrailingPropertyDeclaration(quasiText) {
126
+ return quasiText.replace(/[a-zA-Z-]+\s*:\s*$/, "");
127
+ }
128
+ function stripLeadingDeclarationRemainder(quasiText) {
129
+ const semiIdx = quasiText.indexOf(";");
130
+ if (semiIdx === -1) return quasiText;
131
+ return quasiText.slice(semiIdx + 1).trimStart();
132
+ }
133
+ function parsePropInterpolation(expression) {
134
+ if (!t4__namespace.isArrowFunctionExpression(expression)) return null;
135
+ const param = expression.params[0];
136
+ if (!param) return null;
137
+ const body = unwrapExpression(expression.body);
138
+ if (!t4__namespace.isExpression(body)) return null;
139
+ if (t4__namespace.isIdentifier(param)) {
140
+ if (t4__namespace.isMemberExpression(body) && !body.computed && t4__namespace.isIdentifier(body.object, { name: param.name }) && t4__namespace.isIdentifier(body.property)) {
141
+ return { propName: body.property.name };
142
+ }
143
+ return null;
144
+ }
145
+ if (t4__namespace.isObjectPattern(param) && t4__namespace.isIdentifier(body)) {
146
+ const propName = body.name;
147
+ const hasProp = param.properties.some((property) => {
148
+ if (!t4__namespace.isObjectProperty(property)) return false;
149
+ const keyName = t4__namespace.isIdentifier(property.key) ? property.key.name : t4__namespace.isStringLiteral(property.key) ? property.key.value : null;
150
+ return keyName === propName;
151
+ });
152
+ if (!hasProp) return null;
153
+ return { propName };
154
+ }
155
+ return null;
156
+ }
157
+ function parseBooleanTernaryInterpolation(expression) {
158
+ if (!t4__namespace.isArrowFunctionExpression(expression)) return null;
159
+ const param = expression.params[0];
160
+ if (!t4__namespace.isIdentifier(param)) return null;
161
+ const body = unwrapExpression(expression.body);
162
+ if (!t4__namespace.isConditionalExpression(body)) return null;
163
+ if (!t4__namespace.isMemberExpression(body.test) || body.test.computed || !t4__namespace.isIdentifier(body.test.object, { name: param.name }) || !t4__namespace.isIdentifier(body.test.property)) {
164
+ return null;
165
+ }
166
+ const trueValue = literalToCssValue(body.consequent);
167
+ const falseValue = literalToCssValue(body.alternate);
168
+ if (trueValue === null || falseValue === null) return null;
169
+ return {
170
+ propName: body.test.property.name,
171
+ trueValue,
172
+ falseValue
173
+ };
174
+ }
175
+ function parseTemplateInterpolations(template) {
176
+ const interpolations = [];
177
+ for (let i = 0; i < template.expressions.length; i++) {
178
+ const expression = template.expressions[i];
179
+ if (!t4__namespace.isExpression(expression)) return null;
180
+ const parsed = parsePropInterpolation(expression);
181
+ if (!parsed) return null;
182
+ const followingQuasi = template.quasis[i + 1]?.value.cooked ?? "";
183
+ interpolations.push({
184
+ index: i,
185
+ propName: parsed.propName,
186
+ suffix: extractSuffixFromQuasi(followingQuasi)
187
+ });
188
+ }
189
+ return interpolations;
190
+ }
191
+ function parseBooleanTemplateInterpolations(template) {
192
+ const interpolations = [];
193
+ for (let i = 0; i < template.expressions.length; i++) {
194
+ const expression = template.expressions[i];
195
+ if (!t4__namespace.isExpression(expression)) return null;
196
+ const parsed = parseBooleanTernaryInterpolation(expression);
197
+ if (!parsed) return null;
198
+ const quasiBefore = template.quasis[i]?.value.cooked ?? "";
199
+ const cssProperty = extractCssPropertyBeforeInterpolation(quasiBefore);
200
+ if (!cssProperty) return null;
201
+ interpolations.push({
202
+ index: i,
203
+ propName: parsed.propName,
204
+ trueValue: parsed.trueValue,
205
+ falseValue: parsed.falseValue,
206
+ cssProperty: camelCaseProperty2(cssProperty)
207
+ });
208
+ }
209
+ return interpolations;
210
+ }
211
+ function reconstructInterpolatedCss(template, interpolations) {
212
+ let result = "";
213
+ for (let i = 0; i < template.quasis.length; i++) {
214
+ let quasiText = template.quasis[i].value.cooked ?? "";
215
+ if (i > 0) {
216
+ const interpolation = interpolations[i - 1];
217
+ if (interpolation?.suffix && quasiText.startsWith(interpolation.suffix)) {
218
+ quasiText = quasiText.slice(interpolation.suffix.length);
219
+ }
220
+ }
221
+ result += quasiText;
222
+ if (i < interpolations.length) {
223
+ result += placeholderToken(interpolations[i].index);
224
+ }
225
+ }
226
+ return result;
227
+ }
228
+ function reconstructStaticCssWithoutVariants(template, booleanInterpolations) {
229
+ const skipIndices = new Set(booleanInterpolations.map((interpolation) => interpolation.index));
230
+ let result = "";
231
+ for (let i = 0; i < template.quasis.length; i++) {
232
+ let quasiText = template.quasis[i].value.cooked ?? "";
233
+ if (skipIndices.has(i)) {
234
+ quasiText = stripTrailingPropertyDeclaration(quasiText);
235
+ }
236
+ if (i > 0 && skipIndices.has(i - 1)) {
237
+ quasiText = stripLeadingDeclarationRemainder(quasiText);
238
+ }
239
+ result += quasiText;
240
+ }
241
+ return result.trim();
242
+ }
243
+ function toVarDebugName(componentName, propName) {
244
+ return `${componentName}${propName.charAt(0).toUpperCase()}${propName.slice(1)}`;
245
+ }
246
+ function toComponentConstName(componentName) {
247
+ if (!componentName) return componentName;
248
+ return componentName.charAt(0).toLowerCase() + componentName.slice(1);
249
+ }
250
+ function toCssValueNode(value) {
251
+ if (/^-?\d+(\.\d+)?$/.test(value.trim())) {
252
+ return t4__namespace.numericLiteral(Number(value));
253
+ }
254
+ return t4__namespace.stringLiteral(value);
255
+ }
256
+ function toKeyNode2(key) {
257
+ if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key)) {
258
+ return t4__namespace.identifier(key);
259
+ }
260
+ return t4__namespace.stringLiteral(key);
261
+ }
262
+ function buildVariantStyles(properties) {
263
+ return t4__namespace.objectExpression(
264
+ Object.entries(properties).map(
265
+ ([key, value]) => t4__namespace.objectProperty(toKeyNode2(key), toCssValueNode(value))
266
+ )
267
+ );
268
+ }
269
+ function buildVariantComponentConfig(baseObject, booleanInterpolations) {
270
+ const variantGroups = /* @__PURE__ */ new Map();
271
+ for (const interpolation of booleanInterpolations) {
272
+ const group = variantGroups.get(interpolation.propName) ?? { true: {}, false: {} };
273
+ group.true[interpolation.cssProperty] = interpolation.trueValue;
274
+ group.false[interpolation.cssProperty] = interpolation.falseValue;
275
+ variantGroups.set(interpolation.propName, group);
276
+ }
277
+ const variantsProperties = [...variantGroups.entries()].map(
278
+ ([propName, values]) => t4__namespace.objectProperty(
279
+ t4__namespace.identifier(propName),
280
+ t4__namespace.objectExpression([
281
+ t4__namespace.objectProperty(t4__namespace.identifier("true"), buildVariantStyles(values.true)),
282
+ t4__namespace.objectProperty(t4__namespace.identifier("false"), buildVariantStyles(values.false))
283
+ ])
284
+ )
285
+ );
286
+ const defaultVariantProperties = [...variantGroups.keys()].map(
287
+ (propName) => t4__namespace.objectProperty(t4__namespace.identifier(propName), t4__namespace.booleanLiteral(false))
288
+ );
289
+ return t4__namespace.objectExpression([
290
+ t4__namespace.objectProperty(t4__namespace.identifier("base"), baseObject),
291
+ t4__namespace.objectProperty(t4__namespace.identifier("variants"), t4__namespace.objectExpression(variantsProperties)),
292
+ t4__namespace.objectProperty(t4__namespace.identifier("defaultVariants"), t4__namespace.objectExpression(defaultVariantProperties))
293
+ ]);
294
+ }
88
295
 
89
296
  // src/transform.ts
90
297
  function toKebabCase(input) {
91
298
  return input.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/[_\s]+/g, "-").toLowerCase();
92
299
  }
93
300
  function memberExpressionToJsxName(expression) {
94
- if (t2__namespace.isIdentifier(expression)) {
95
- return t2__namespace.jsxIdentifier(expression.name);
301
+ if (t4__namespace.isIdentifier(expression)) {
302
+ return t4__namespace.jsxIdentifier(expression.name);
96
303
  }
97
- if (t2__namespace.isMemberExpression(expression) && !expression.computed) {
304
+ if (t4__namespace.isMemberExpression(expression) && !expression.computed) {
98
305
  const left = memberExpressionToJsxName(expression.object);
99
- const right = t2__namespace.isIdentifier(expression.property) ? t2__namespace.jsxIdentifier(expression.property.name) : null;
306
+ const right = t4__namespace.isIdentifier(expression.property) ? t4__namespace.jsxIdentifier(expression.property.name) : null;
100
307
  if (!left || !right) return null;
101
- return t2__namespace.jsxMemberExpression(left, right);
308
+ return t4__namespace.jsxMemberExpression(left, right);
102
309
  }
103
310
  return null;
104
311
  }
105
312
  function parseStyledTarget(tag, styledNames) {
106
- if (t2__namespace.isMemberExpression(tag) && t2__namespace.isIdentifier(tag.object) && styledNames.has(tag.object.name) && t2__namespace.isIdentifier(tag.property)) {
107
- return { kind: "intrinsic", jsxName: t2__namespace.jsxIdentifier(tag.property.name) };
313
+ if (t4__namespace.isMemberExpression(tag) && t4__namespace.isIdentifier(tag.object) && styledNames.has(tag.object.name) && t4__namespace.isIdentifier(tag.property)) {
314
+ return { kind: "intrinsic", jsxName: t4__namespace.jsxIdentifier(tag.property.name) };
108
315
  }
109
- if (t2__namespace.isCallExpression(tag) && t2__namespace.isIdentifier(tag.callee) && styledNames.has(tag.callee.name)) {
316
+ if (t4__namespace.isCallExpression(tag) && t4__namespace.isIdentifier(tag.callee) && styledNames.has(tag.callee.name)) {
110
317
  const firstArg = tag.arguments[0];
111
- if (!firstArg || !t2__namespace.isExpression(firstArg)) return null;
318
+ if (!firstArg || !t4__namespace.isExpression(firstArg)) return null;
112
319
  const jsxName = memberExpressionToJsxName(firstArg);
113
320
  if (!jsxName) return null;
114
321
  return { kind: "component", jsxName };
@@ -118,75 +325,118 @@ function parseStyledTarget(tag, styledNames) {
118
325
  function addWarning(warnings, message, nodeName) {
119
326
  warnings.push({ message, nodeName });
120
327
  }
121
- function createMergedClassExpression(existing, classNameIdentifier) {
122
- return t2__namespace.callExpression(
123
- t2__namespace.memberExpression(
124
- t2__namespace.callExpression(
125
- t2__namespace.memberExpression(
126
- t2__namespace.arrayExpression([existing, classNameIdentifier]),
127
- t2__namespace.identifier("filter")
328
+ function createMergedClassExpression(existing, classNameExpression) {
329
+ return t4__namespace.callExpression(
330
+ t4__namespace.memberExpression(
331
+ t4__namespace.callExpression(
332
+ t4__namespace.memberExpression(
333
+ t4__namespace.arrayExpression([existing, classNameExpression]),
334
+ t4__namespace.identifier("filter")
128
335
  ),
129
- [t2__namespace.identifier("Boolean")]
336
+ [t4__namespace.identifier("Boolean")]
130
337
  ),
131
- t2__namespace.identifier("join")
338
+ t4__namespace.identifier("join")
132
339
  ),
133
- [t2__namespace.stringLiteral(" ")]
340
+ [t4__namespace.stringLiteral(" ")]
134
341
  );
135
342
  }
136
- function updateClassNameAttribute(openingElement, classNameIdentifier) {
343
+ function updateClassNameAttribute(openingElement, classNameExpression) {
137
344
  const existingAttr = openingElement.attributes.find(
138
- (attribute) => t2__namespace.isJSXAttribute(attribute) && t2__namespace.isJSXIdentifier(attribute.name, { name: "className" })
345
+ (attribute) => t4__namespace.isJSXAttribute(attribute) && t4__namespace.isJSXIdentifier(attribute.name, { name: "className" })
139
346
  );
140
347
  if (!existingAttr) {
141
348
  openingElement.attributes.push(
142
- t2__namespace.jsxAttribute(t2__namespace.jsxIdentifier("className"), t2__namespace.jsxExpressionContainer(classNameIdentifier))
349
+ t4__namespace.jsxAttribute(t4__namespace.jsxIdentifier("className"), t4__namespace.jsxExpressionContainer(classNameExpression))
143
350
  );
144
351
  return;
145
352
  }
146
353
  if (!existingAttr.value) {
147
- existingAttr.value = t2__namespace.jsxExpressionContainer(classNameIdentifier);
354
+ existingAttr.value = t4__namespace.jsxExpressionContainer(classNameExpression);
148
355
  return;
149
356
  }
150
- if (t2__namespace.isStringLiteral(existingAttr.value)) {
151
- existingAttr.value = t2__namespace.jsxExpressionContainer(
152
- createMergedClassExpression(t2__namespace.stringLiteral(existingAttr.value.value), classNameIdentifier)
357
+ if (t4__namespace.isStringLiteral(existingAttr.value)) {
358
+ existingAttr.value = t4__namespace.jsxExpressionContainer(
359
+ createMergedClassExpression(t4__namespace.stringLiteral(existingAttr.value.value), classNameExpression)
153
360
  );
154
361
  return;
155
362
  }
156
- if (t2__namespace.isJSXExpressionContainer(existingAttr.value)) {
157
- existingAttr.value = t2__namespace.jsxExpressionContainer(
363
+ if (t4__namespace.isJSXExpressionContainer(existingAttr.value)) {
364
+ existingAttr.value = t4__namespace.jsxExpressionContainer(
158
365
  createMergedClassExpression(
159
366
  existingAttr.value.expression,
160
- classNameIdentifier
367
+ classNameExpression
161
368
  )
162
369
  );
163
370
  }
164
371
  }
165
- function ensureStylesImport(ast) {
166
- let typestylesImport = null;
372
+ function hasLocalBinding(ast, name) {
167
373
  for (const statement of ast.program.body) {
168
- if (t2__namespace.isImportDeclaration(statement) && statement.source.value === "typestyles") {
169
- typestylesImport = statement;
170
- break;
374
+ if (!t4__namespace.isVariableDeclaration(statement)) continue;
375
+ for (const declarator of statement.declarations) {
376
+ if (t4__namespace.isIdentifier(declarator.id) && declarator.id.name === name) return true;
377
+ if (t4__namespace.isObjectPattern(declarator.id)) {
378
+ for (const prop of declarator.id.properties) {
379
+ if (t4__namespace.isObjectProperty(prop) && t4__namespace.isIdentifier(prop.value) && prop.value.name === name) {
380
+ return true;
381
+ }
382
+ }
383
+ }
384
+ }
385
+ }
386
+ return false;
387
+ }
388
+ function ensureTypestylesRuntime(ast, needs) {
389
+ const requiredSpecifiers = /* @__PURE__ */ new Set(["createTypeStyles"]);
390
+ if (needs.vars) {
391
+ requiredSpecifiers.add("createVar");
392
+ requiredSpecifiers.add("assignVars");
393
+ }
394
+ let typestylesImport = null;
395
+ let lastImportIndex = -1;
396
+ for (let i = 0; i < ast.program.body.length; i++) {
397
+ const statement = ast.program.body[i];
398
+ if (t4__namespace.isImportDeclaration(statement)) {
399
+ lastImportIndex = i;
400
+ if (statement.source.value === "typestyles" && typestylesImport == null) {
401
+ typestylesImport = statement;
402
+ }
171
403
  }
172
404
  }
405
+ const buildSpecifiers = (names) => [...names].map((name) => t4__namespace.importSpecifier(t4__namespace.identifier(name), t4__namespace.identifier(name)));
173
406
  if (!typestylesImport) {
174
407
  ast.program.body.unshift(
175
- t2__namespace.importDeclaration(
176
- [t2__namespace.importSpecifier(t2__namespace.identifier("styles"), t2__namespace.identifier("styles"))],
177
- t2__namespace.stringLiteral("typestyles")
178
- )
408
+ t4__namespace.importDeclaration(buildSpecifiers(requiredSpecifiers), t4__namespace.stringLiteral("typestyles"))
179
409
  );
180
- return;
410
+ lastImportIndex = 0;
411
+ } else {
412
+ for (const name of requiredSpecifiers) {
413
+ const hasSpecifier = typestylesImport.specifiers.some(
414
+ (specifier) => t4__namespace.isImportSpecifier(specifier) && t4__namespace.isIdentifier(specifier.imported, { name }) && t4__namespace.isIdentifier(specifier.local, { name })
415
+ );
416
+ if (!hasSpecifier) {
417
+ typestylesImport.specifiers.push(t4__namespace.importSpecifier(t4__namespace.identifier(name), t4__namespace.identifier(name)));
418
+ }
419
+ }
181
420
  }
182
- const hasStylesSpecifier = typestylesImport.specifiers.some(
183
- (specifier) => t2__namespace.isImportSpecifier(specifier) && t2__namespace.isIdentifier(specifier.imported, { name: "styles" }) && t2__namespace.isIdentifier(specifier.local, { name: "styles" })
184
- );
185
- if (!hasStylesSpecifier) {
186
- typestylesImport.specifiers.push(
187
- t2__namespace.importSpecifier(t2__namespace.identifier("styles"), t2__namespace.identifier("styles"))
421
+ const destructureProps = [];
422
+ if (needs.style && !hasLocalBinding(ast, "style")) {
423
+ destructureProps.push(
424
+ t4__namespace.objectProperty(t4__namespace.identifier("style"), t4__namespace.identifier("style"), false, true)
188
425
  );
189
426
  }
427
+ if (needs.recipe && !hasLocalBinding(ast, "recipe")) {
428
+ destructureProps.push(
429
+ t4__namespace.objectProperty(t4__namespace.identifier("recipe"), t4__namespace.identifier("recipe"), false, true)
430
+ );
431
+ }
432
+ if (destructureProps.length === 0) return;
433
+ const factoryDecl = t4__namespace.variableDeclaration("const", [
434
+ t4__namespace.variableDeclarator(
435
+ t4__namespace.objectPattern(destructureProps),
436
+ t4__namespace.callExpression(t4__namespace.identifier("createTypeStyles"), [])
437
+ )
438
+ ]);
439
+ ast.program.body.splice(lastImportIndex + 1, 0, factoryDecl);
190
440
  }
191
441
  function cleanupUnusedImports(ast) {
192
442
  traverse__default.default(ast, {
@@ -195,7 +445,7 @@ function cleanupUnusedImports(ast) {
195
445
  },
196
446
  ImportDeclaration(path) {
197
447
  const unusedLocals = path.node.specifiers.filter((specifier) => {
198
- if (path.node.source.value === "typestyles" && t2__namespace.isImportSpecifier(specifier) && t2__namespace.isIdentifier(specifier.local, { name: "styles" })) {
448
+ if (path.node.source.value === "typestyles" && t4__namespace.isImportSpecifier(specifier) && (specifier.local.name === "createTypeStyles" || specifier.local.name === "createVar" || specifier.local.name === "assignVars")) {
199
449
  return false;
200
450
  }
201
451
  const local = specifier.local.name;
@@ -212,6 +462,177 @@ function cleanupUnusedImports(ast) {
212
462
  }
213
463
  });
214
464
  }
465
+ function createAssignVarValue(propExpression, suffix) {
466
+ if (!suffix) return propExpression;
467
+ if (t4__namespace.isStringLiteral(propExpression)) {
468
+ return t4__namespace.stringLiteral(`${propExpression.value}${suffix}`);
469
+ }
470
+ return t4__namespace.binaryExpression("+", propExpression, t4__namespace.stringLiteral(suffix));
471
+ }
472
+ function buildAssignVarsCall(propVars, propValues) {
473
+ const properties = propVars.filter((binding) => propValues.has(binding.propName)).map((binding) => {
474
+ const propExpression = propValues.get(binding.propName);
475
+ return t4__namespace.objectProperty(
476
+ t4__namespace.identifier(binding.varConstName),
477
+ createAssignVarValue(propExpression, binding.suffix),
478
+ true
479
+ );
480
+ });
481
+ return t4__namespace.callExpression(t4__namespace.identifier("assignVars"), [t4__namespace.objectExpression(properties)]);
482
+ }
483
+ function mergeStyleExpressions(existing, assignVarsCall) {
484
+ return t4__namespace.objectExpression([t4__namespace.spreadElement(existing), t4__namespace.spreadElement(assignVarsCall)]);
485
+ }
486
+ function updateStyleAttribute(openingElement, assignVarsCall) {
487
+ const existingAttr = openingElement.attributes.find(
488
+ (attribute) => t4__namespace.isJSXAttribute(attribute) && t4__namespace.isJSXIdentifier(attribute.name, { name: "style" })
489
+ );
490
+ if (!existingAttr) {
491
+ openingElement.attributes.push(
492
+ t4__namespace.jsxAttribute(t4__namespace.jsxIdentifier("style"), t4__namespace.jsxExpressionContainer(assignVarsCall))
493
+ );
494
+ return;
495
+ }
496
+ if (!existingAttr.value) {
497
+ existingAttr.value = t4__namespace.jsxExpressionContainer(assignVarsCall);
498
+ return;
499
+ }
500
+ if (t4__namespace.isJSXExpressionContainer(existingAttr.value)) {
501
+ existingAttr.value = t4__namespace.jsxExpressionContainer(
502
+ mergeStyleExpressions(existingAttr.value.expression, assignVarsCall)
503
+ );
504
+ }
505
+ }
506
+ function collectPropValuesFromJsx(openingElement, propNames) {
507
+ const values = /* @__PURE__ */ new Map();
508
+ for (const attribute of openingElement.attributes) {
509
+ if (!t4__namespace.isJSXAttribute(attribute) || !t4__namespace.isJSXIdentifier(attribute.name)) continue;
510
+ if (!propNames.has(attribute.name.name)) continue;
511
+ if (!attribute.value) {
512
+ values.set(attribute.name.name, t4__namespace.booleanLiteral(true));
513
+ continue;
514
+ }
515
+ if (t4__namespace.isStringLiteral(attribute.value)) {
516
+ values.set(attribute.name.name, t4__namespace.stringLiteral(attribute.value.value));
517
+ continue;
518
+ }
519
+ if (t4__namespace.isJSXExpressionContainer(attribute.value)) {
520
+ values.set(attribute.name.name, attribute.value.expression);
521
+ }
522
+ }
523
+ return values;
524
+ }
525
+ function removeStyledProps(openingElement, propNames) {
526
+ openingElement.attributes = openingElement.attributes.filter((attribute) => {
527
+ if (!t4__namespace.isJSXAttribute(attribute) || !t4__namespace.isJSXIdentifier(attribute.name)) return true;
528
+ return !propNames.has(attribute.name.name);
529
+ });
530
+ }
531
+ function buildComponentClassNameExpression(componentConstName, variantProps, propValues) {
532
+ const properties = variantProps.filter((propName) => propValues.has(propName)).map((propName) => t4__namespace.objectProperty(t4__namespace.identifier(propName), propValues.get(propName)));
533
+ if (properties.length === 0) {
534
+ return t4__namespace.callExpression(t4__namespace.identifier(componentConstName), []);
535
+ }
536
+ return t4__namespace.callExpression(t4__namespace.identifier(componentConstName), [t4__namespace.objectExpression(properties)]);
537
+ }
538
+ function migrateBooleanVariantTemplate(path, variableName, template, styledTarget, warnings, styledTransforms) {
539
+ const booleanInterpolations = parseBooleanTemplateInterpolations(template);
540
+ if (!booleanInterpolations) return false;
541
+ const staticCss = reconstructStaticCssWithoutVariants(template, booleanInterpolations);
542
+ const baseObject = cssToObjectExpression(staticCss, warnings);
543
+ if (!baseObject) {
544
+ addWarning(warnings, "Skipped because CSS could not be parsed.", variableName);
545
+ return false;
546
+ }
547
+ const componentConstName = path.scope.generateUidIdentifier(
548
+ toComponentConstName(variableName)
549
+ ).name;
550
+ const componentConfig = buildVariantComponentConfig(baseObject, booleanInterpolations);
551
+ const variantProps = [
552
+ ...new Set(booleanInterpolations.map((interpolation) => interpolation.propName))
553
+ ];
554
+ const declaration = path.parentPath;
555
+ if (!declaration.isVariableDeclaration()) return false;
556
+ declaration.node.declarations = [
557
+ t4__namespace.variableDeclarator(
558
+ t4__namespace.identifier(componentConstName),
559
+ t4__namespace.callExpression(t4__namespace.identifier("recipe"), [
560
+ t4__namespace.stringLiteral(toKebabCase(variableName)),
561
+ componentConfig
562
+ ])
563
+ )
564
+ ];
565
+ styledTransforms.set(variableName, {
566
+ originalName: variableName,
567
+ mode: "component",
568
+ classConstName: componentConstName,
569
+ target: styledTarget,
570
+ propVars: [],
571
+ variantProps
572
+ });
573
+ return true;
574
+ }
575
+ function migrateInterpolatedTemplate(path, variableName, template, styledTarget, warnings, styledTransforms) {
576
+ const interpolations = parseTemplateInterpolations(template);
577
+ if (!interpolations) {
578
+ addWarning(
579
+ warnings,
580
+ "Skipped template literal with unsupported interpolations. Only prop-based patterns like `${props => props.color}` are migrated.",
581
+ variableName
582
+ );
583
+ return false;
584
+ }
585
+ const cssText = reconstructInterpolatedCss(template, interpolations);
586
+ const propVars = interpolations.map((interpolation) => ({
587
+ propName: interpolation.propName,
588
+ varConstName: path.scope.generateUidIdentifier(
589
+ `${variableName}${interpolation.propName.charAt(0).toUpperCase()}${interpolation.propName.slice(1)}Var`
590
+ ).name,
591
+ suffix: interpolation.suffix
592
+ }));
593
+ const varReplacements = /* @__PURE__ */ new Map();
594
+ for (let i = 0; i < interpolations.length; i++) {
595
+ varReplacements.set(
596
+ placeholderToken(interpolations[i].index),
597
+ t4__namespace.identifier(propVars[i].varConstName)
598
+ );
599
+ }
600
+ const objectExpression4 = cssToObjectExpression(cssText, warnings, varReplacements);
601
+ if (!objectExpression4) {
602
+ addWarning(warnings, "Skipped because CSS could not be parsed.", variableName);
603
+ return false;
604
+ }
605
+ const classConstName = path.scope.generateUidIdentifier(`${variableName}Class`).name;
606
+ const varDeclarators = propVars.map(
607
+ (propVar) => t4__namespace.variableDeclarator(
608
+ t4__namespace.identifier(propVar.varConstName),
609
+ t4__namespace.callExpression(t4__namespace.identifier("createVar"), [
610
+ t4__namespace.stringLiteral(toVarDebugName(variableName, propVar.propName))
611
+ ])
612
+ )
613
+ );
614
+ const declaration = path.parentPath;
615
+ if (!declaration.isVariableDeclaration()) return false;
616
+ declaration.node.declarations = [
617
+ ...varDeclarators,
618
+ t4__namespace.variableDeclarator(
619
+ t4__namespace.identifier(classConstName),
620
+ t4__namespace.callExpression(t4__namespace.identifier("style"), [
621
+ t4__namespace.stringLiteral(toKebabCase(variableName)),
622
+ objectExpression4
623
+ ])
624
+ )
625
+ ];
626
+ styledTransforms.set(variableName, {
627
+ originalName: variableName,
628
+ mode: "class",
629
+ classConstName,
630
+ target: styledTarget,
631
+ propVars,
632
+ variantProps: []
633
+ });
634
+ return true;
635
+ }
215
636
  function isOnlyJsxReferences(binding) {
216
637
  return binding.referencePaths.every((referencePath) => {
217
638
  const parent = referencePath.parentPath;
@@ -229,21 +650,24 @@ function migrateSource(filePath, source) {
229
650
  const styledNames = /* @__PURE__ */ new Set();
230
651
  const cssTagNames = /* @__PURE__ */ new Set();
231
652
  const styledTransforms = /* @__PURE__ */ new Map();
653
+ let needsVars = false;
654
+ let needsStyle = false;
655
+ let needsRecipe = false;
232
656
  traverse__default.default(ast, {
233
657
  ImportDeclaration(path) {
234
658
  if (path.node.source.value === "styled-components" || path.node.source.value === "@emotion/styled") {
235
659
  for (const specifier of path.node.specifiers) {
236
- if (t2__namespace.isImportDefaultSpecifier(specifier)) {
660
+ if (t4__namespace.isImportDefaultSpecifier(specifier)) {
237
661
  styledNames.add(specifier.local.name);
238
662
  }
239
- if (t2__namespace.isImportSpecifier(specifier) && t2__namespace.isIdentifier(specifier.imported, { name: "css" })) {
663
+ if (t4__namespace.isImportSpecifier(specifier) && t4__namespace.isIdentifier(specifier.imported, { name: "css" })) {
240
664
  cssTagNames.add(specifier.local.name);
241
665
  }
242
666
  }
243
667
  }
244
668
  if (path.node.source.value === "@emotion/react" || path.node.source.value === "@emotion/css") {
245
669
  for (const specifier of path.node.specifiers) {
246
- if (t2__namespace.isImportSpecifier(specifier) && t2__namespace.isIdentifier(specifier.imported, { name: "css" })) {
670
+ if (t4__namespace.isImportSpecifier(specifier) && t4__namespace.isIdentifier(specifier.imported, { name: "css" })) {
247
671
  cssTagNames.add(specifier.local.name);
248
672
  }
249
673
  }
@@ -252,27 +676,68 @@ function migrateSource(filePath, source) {
252
676
  });
253
677
  traverse__default.default(ast, {
254
678
  VariableDeclarator(path) {
255
- if (!t2__namespace.isIdentifier(path.node.id)) return;
256
- if (!t2__namespace.isTaggedTemplateExpression(path.node.init)) return;
679
+ if (!t4__namespace.isIdentifier(path.node.id)) return;
680
+ if (!t4__namespace.isTaggedTemplateExpression(path.node.init)) return;
257
681
  const variableName = path.node.id.name;
258
682
  const binding = path.scope.getBinding(variableName);
259
683
  if (!binding) return;
260
684
  const template = path.node.init.quasi;
261
- if (template.expressions.length > 0) {
262
- addWarning(
685
+ const hasInterpolations = template.expressions.length > 0;
686
+ const styledTarget = parseStyledTarget(path.node.init.tag, styledNames);
687
+ if (hasInterpolations) {
688
+ if (!styledTarget) {
689
+ addWarning(
690
+ warnings,
691
+ "Skipped template literal with interpolations. Only styled-components prop patterns are migrated.",
692
+ variableName
693
+ );
694
+ return;
695
+ }
696
+ const declaration = path.parentPath;
697
+ if (declaration.parentPath?.isExportNamedDeclaration() || declaration.parentPath?.isExportDefaultDeclaration()) {
698
+ addWarning(
699
+ warnings,
700
+ "Skipped exported styled component to avoid changing external API shape.",
701
+ variableName
702
+ );
703
+ return;
704
+ }
705
+ if (!isOnlyJsxReferences(binding)) {
706
+ addWarning(warnings, "Skipped styled component with non-JSX references.", variableName);
707
+ return;
708
+ }
709
+ if (migrateBooleanVariantTemplate(
710
+ path,
711
+ variableName,
712
+ template,
713
+ styledTarget,
263
714
  warnings,
264
- "Skipped template literal with interpolations. Only static templates are migrated.",
265
- variableName
266
- );
715
+ styledTransforms
716
+ )) {
717
+ needsRecipe = true;
718
+ changed = true;
719
+ return;
720
+ }
721
+ if (migrateInterpolatedTemplate(
722
+ path,
723
+ variableName,
724
+ template,
725
+ styledTarget,
726
+ warnings,
727
+ styledTransforms
728
+ )) {
729
+ needsVars = true;
730
+ needsStyle = true;
731
+ changed = true;
732
+ }
267
733
  return;
268
734
  }
269
735
  const cssText = template.quasis.map((quasi) => quasi.value.cooked ?? "").join("");
270
- const objectExpression2 = cssToObjectExpression(cssText, warnings);
271
- if (!objectExpression2) {
736
+ const objectExpression4 = cssToObjectExpression(cssText, warnings);
737
+ if (!objectExpression4) {
272
738
  addWarning(warnings, "Skipped because CSS could not be parsed.", variableName);
273
739
  return;
274
740
  }
275
- const styledTarget = parseStyledTarget(path.node.init.tag, styledNames);
276
741
  if (styledTarget) {
277
742
  const declaration = path.parentPath;
278
743
  if (declaration.parentPath?.isExportNamedDeclaration() || declaration.parentPath?.isExportDefaultDeclaration()) {
@@ -288,24 +753,29 @@ function migrateSource(filePath, source) {
288
753
  return;
289
754
  }
290
755
  const classConstName = path.scope.generateUidIdentifier(`${variableName}Class`).name;
291
- path.node.id = t2__namespace.identifier(classConstName);
292
- path.node.init = t2__namespace.callExpression(
293
- t2__namespace.memberExpression(t2__namespace.identifier("styles"), t2__namespace.identifier("class")),
294
- [t2__namespace.stringLiteral(toKebabCase(variableName)), objectExpression2]
295
- );
756
+ path.node.id = t4__namespace.identifier(classConstName);
757
+ path.node.init = t4__namespace.callExpression(t4__namespace.identifier("style"), [
758
+ t4__namespace.stringLiteral(toKebabCase(variableName)),
759
+ objectExpression4
760
+ ]);
296
761
  styledTransforms.set(variableName, {
297
762
  originalName: variableName,
763
+ mode: "class",
298
764
  classConstName,
299
- target: styledTarget
765
+ target: styledTarget,
766
+ propVars: [],
767
+ variantProps: []
300
768
  });
769
+ needsStyle = true;
301
770
  changed = true;
302
771
  return;
303
772
  }
304
- if (t2__namespace.isIdentifier(path.node.init.tag) && cssTagNames.has(path.node.init.tag.name)) {
305
- path.node.init = t2__namespace.callExpression(
306
- t2__namespace.memberExpression(t2__namespace.identifier("styles"), t2__namespace.identifier("class")),
307
- [t2__namespace.stringLiteral(toKebabCase(variableName)), objectExpression2]
308
- );
773
+ if (t4__namespace.isIdentifier(path.node.init.tag) && cssTagNames.has(path.node.init.tag.name)) {
774
+ path.node.init = t4__namespace.callExpression(t4__namespace.identifier("style"), [
775
+ t4__namespace.stringLiteral(toKebabCase(variableName)),
776
+ objectExpression4
777
+ ]);
778
+ needsStyle = true;
309
779
  changed = true;
310
780
  }
311
781
  }
@@ -314,19 +784,40 @@ function migrateSource(filePath, source) {
314
784
  traverse__default.default(ast, {
315
785
  JSXElement(path) {
316
786
  const openingName = path.node.openingElement.name;
317
- if (!t2__namespace.isJSXIdentifier(openingName)) return;
787
+ if (!t4__namespace.isJSXIdentifier(openingName)) return;
318
788
  const transform = styledTransforms.get(openingName.name);
319
789
  if (!transform) return;
320
790
  path.node.openingElement.name = transform.target.jsxName;
321
791
  if (path.node.closingElement) {
322
792
  path.node.closingElement.name = transform.target.jsxName;
323
793
  }
324
- updateClassNameAttribute(path.node.openingElement, t2__namespace.identifier(transform.classConstName));
794
+ const classNameExpression = transform.mode === "component" ? buildComponentClassNameExpression(
795
+ transform.classConstName,
796
+ transform.variantProps,
797
+ collectPropValuesFromJsx(path.node.openingElement, new Set(transform.variantProps))
798
+ ) : t4__namespace.identifier(transform.classConstName);
799
+ updateClassNameAttribute(path.node.openingElement, classNameExpression);
800
+ if (transform.propVars.length > 0) {
801
+ const propNames = new Set(transform.propVars.map((propVar) => propVar.propName));
802
+ const propValues = collectPropValuesFromJsx(path.node.openingElement, propNames);
803
+ if (propValues.size > 0) {
804
+ const assignVarsCall = buildAssignVarsCall(transform.propVars, propValues);
805
+ updateStyleAttribute(path.node.openingElement, assignVarsCall);
806
+ removeStyledProps(path.node.openingElement, propNames);
807
+ }
808
+ }
809
+ if (transform.variantProps.length > 0) {
810
+ removeStyledProps(path.node.openingElement, new Set(transform.variantProps));
811
+ }
325
812
  }
326
813
  });
327
814
  }
328
815
  if (changed) {
329
- ensureStylesImport(ast);
816
+ ensureTypestylesRuntime(ast, {
817
+ style: needsStyle,
818
+ recipe: needsRecipe,
819
+ vars: needsVars
820
+ });
330
821
  cleanupUnusedImports(ast);
331
822
  }
332
823
  const output = generate__default.default(