@typestyles/migrate 0.0.0-unstable.eee07e5f0c37 → 0.0.0-unstable.f58fa2ab20b7
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/LICENSE +204 -0
- package/README.md +169 -0
- package/dist/cli.cjs +56 -509
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.d.cts +1 -1
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +57 -59
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +4 -456
- package/dist/index.d.cts +32 -27
- package/dist/index.d.ts +32 -27
- package/dist/index.js +1 -3
- package/dist/migrate-B3MfAQI5.cjs +728 -0
- package/dist/migrate-B3MfAQI5.cjs.map +1 -0
- package/dist/migrate-DUG0GWWo.js +689 -0
- package/dist/migrate-DUG0GWWo.js.map +1 -0
- package/package.json +13 -15
- package/dist/chunk-RUEGREQK.js +0 -427
- package/dist/chunk-RUEGREQK.js.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
package/dist/cli.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
|
|
2
|
-
export {
|
|
2
|
+
export {}
|
package/dist/cli.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
|
|
2
|
-
export {
|
|
2
|
+
export {}
|
package/dist/cli.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { runMigration } from
|
|
3
|
-
import { resolve } from
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
import { t as runMigration } from "./migrate-DUG0GWWo.js";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
//#region src/cli.ts
|
|
5
|
+
const HELP_TEXT = `typestyles-migrate <paths...> [options]
|
|
6
6
|
|
|
7
7
|
Options:
|
|
8
8
|
--write Apply changes in-place (default is dry-run)
|
|
@@ -13,64 +13,62 @@ Options:
|
|
|
13
13
|
--help Show this help
|
|
14
14
|
`;
|
|
15
15
|
function parseCliArgs(argv) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
reportPath
|
|
63
|
-
};
|
|
16
|
+
if (argv.includes("--help")) {
|
|
17
|
+
process.stdout.write(HELP_TEXT);
|
|
18
|
+
process.exit(0);
|
|
19
|
+
}
|
|
20
|
+
const targets = [];
|
|
21
|
+
const include = [];
|
|
22
|
+
const exclude = [];
|
|
23
|
+
let extensions = [".ts", ".tsx"];
|
|
24
|
+
let reportPath;
|
|
25
|
+
let write = false;
|
|
26
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
27
|
+
const token = argv[i];
|
|
28
|
+
if (token === "--write") {
|
|
29
|
+
write = true;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (token === "--include" && argv[i + 1]) {
|
|
33
|
+
include.push(argv[i + 1]);
|
|
34
|
+
i += 1;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (token === "--exclude" && argv[i + 1]) {
|
|
38
|
+
exclude.push(argv[i + 1]);
|
|
39
|
+
i += 1;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (token === "--extensions" && argv[i + 1]) {
|
|
43
|
+
extensions = argv[i + 1].split(",").map((item) => item.trim()).filter(Boolean);
|
|
44
|
+
i += 1;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (token === "--report" && argv[i + 1]) {
|
|
48
|
+
reportPath = resolve(process.cwd(), argv[i + 1]);
|
|
49
|
+
i += 1;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (!token.startsWith("--")) targets.push(token);
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
targets: targets.length > 0 ? targets : ["."],
|
|
56
|
+
write,
|
|
57
|
+
include,
|
|
58
|
+
exclude,
|
|
59
|
+
extensions,
|
|
60
|
+
reportPath
|
|
61
|
+
};
|
|
64
62
|
}
|
|
65
63
|
async function main() {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
await runMigration(process.cwd(), options);
|
|
64
|
+
const options = parseCliArgs(process.argv.slice(2));
|
|
65
|
+
await runMigration(process.cwd(), options);
|
|
69
66
|
}
|
|
70
67
|
main().catch((error) => {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
process.exit(1);
|
|
68
|
+
process.stderr.write(`${error.message}\n`);
|
|
69
|
+
process.exit(1);
|
|
74
70
|
});
|
|
75
|
-
//#
|
|
71
|
+
//#endregion
|
|
72
|
+
export {};
|
|
73
|
+
|
|
76
74
|
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"cli.js","names":[],"sources":["../src/cli.ts"],"sourcesContent":["import { resolve } from 'node:path';\nimport type { CliOptions } from './types';\nimport { runMigration } from './migrate';\n\nconst HELP_TEXT = `typestyles-migrate <paths...> [options]\n\nOptions:\n --write Apply changes in-place (default is dry-run)\n --include <glob> Include glob (repeatable)\n --exclude <glob> Exclude glob (repeatable)\n --extensions <list> Comma-separated extensions (default: .ts,.tsx)\n --report <path> Write JSON report to the provided path\n --help Show this help\n`;\n\nfunction parseCliArgs(argv: string[]): CliOptions {\n if (argv.includes('--help')) {\n process.stdout.write(HELP_TEXT);\n process.exit(0);\n }\n\n const targets: string[] = [];\n const include: string[] = [];\n const exclude: string[] = [];\n let extensions: string[] = ['.ts', '.tsx'];\n let reportPath: string | undefined;\n let write = false;\n\n for (let i = 0; i < argv.length; i += 1) {\n const token = argv[i];\n if (token === '--write') {\n write = true;\n continue;\n }\n\n if (token === '--include' && argv[i + 1]) {\n include.push(argv[i + 1]);\n i += 1;\n continue;\n }\n\n if (token === '--exclude' && argv[i + 1]) {\n exclude.push(argv[i + 1]);\n i += 1;\n continue;\n }\n\n if (token === '--extensions' && argv[i + 1]) {\n extensions = argv[i + 1]\n .split(',')\n .map((item) => item.trim())\n .filter(Boolean);\n i += 1;\n continue;\n }\n\n if (token === '--report' && argv[i + 1]) {\n reportPath = resolve(process.cwd(), argv[i + 1]);\n i += 1;\n continue;\n }\n\n if (!token.startsWith('--')) {\n targets.push(token);\n }\n }\n\n return {\n targets: targets.length > 0 ? targets : ['.'],\n write,\n include,\n exclude,\n extensions,\n reportPath,\n };\n}\n\nasync function main(): Promise<void> {\n const args = process.argv.slice(2);\n const options = parseCliArgs(args);\n await runMigration(process.cwd(), options);\n}\n\nmain().catch((error) => {\n process.stderr.write(`${(error as Error).message}\\n`);\n process.exit(1);\n});\n"],"mappings":";;;;AAIA,MAAM,YAAY;;;;;;;;;;AAWlB,SAAS,aAAa,MAA4B;CAChD,IAAI,KAAK,SAAS,QAAQ,GAAG;EAC3B,QAAQ,OAAO,MAAM,SAAS;EAC9B,QAAQ,KAAK,CAAC;CAChB;CAEA,MAAM,UAAoB,CAAC;CAC3B,MAAM,UAAoB,CAAC;CAC3B,MAAM,UAAoB,CAAC;CAC3B,IAAI,aAAuB,CAAC,OAAO,MAAM;CACzC,IAAI;CACJ,IAAI,QAAQ;CAEZ,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,GAAG;EACvC,MAAM,QAAQ,KAAK;EACnB,IAAI,UAAU,WAAW;GACvB,QAAQ;GACR;EACF;EAEA,IAAI,UAAU,eAAe,KAAK,IAAI,IAAI;GACxC,QAAQ,KAAK,KAAK,IAAI,EAAE;GACxB,KAAK;GACL;EACF;EAEA,IAAI,UAAU,eAAe,KAAK,IAAI,IAAI;GACxC,QAAQ,KAAK,KAAK,IAAI,EAAE;GACxB,KAAK;GACL;EACF;EAEA,IAAI,UAAU,kBAAkB,KAAK,IAAI,IAAI;GAC3C,aAAa,KAAK,IAAI,EAAE,CACrB,MAAM,GAAG,CAAC,CACV,KAAK,SAAS,KAAK,KAAK,CAAC,CAAC,CAC1B,OAAO,OAAO;GACjB,KAAK;GACL;EACF;EAEA,IAAI,UAAU,cAAc,KAAK,IAAI,IAAI;GACvC,aAAa,QAAQ,QAAQ,IAAI,GAAG,KAAK,IAAI,EAAE;GAC/C,KAAK;GACL;EACF;EAEA,IAAI,CAAC,MAAM,WAAW,IAAI,GACxB,QAAQ,KAAK,KAAK;CAEtB;CAEA,OAAO;EACL,SAAS,QAAQ,SAAS,IAAI,UAAU,CAAC,GAAG;EAC5C;EACA;EACA;EACA;EACA;CACF;AACF;AAEA,eAAe,OAAsB;CAEnC,MAAM,UAAU,aADH,QAAQ,KAAK,MAAM,CACA,CAAC;CACjC,MAAM,aAAa,QAAQ,IAAI,GAAG,OAAO;AAC3C;AAEA,KAAK,CAAC,CAAC,OAAO,UAAU;CACtB,QAAQ,OAAO,MAAM,GAAI,MAAgB,QAAQ,GAAG;CACpD,QAAQ,KAAK,CAAC;AAChB,CAAC"}
|
package/dist/index.cjs
CHANGED
|
@@ -1,457 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var traverse = require('@babel/traverse');
|
|
7
|
-
var t2 = require('@babel/types');
|
|
8
|
-
var postcss = require('postcss');
|
|
9
|
-
var promises = require('fs/promises');
|
|
10
|
-
var path = require('path');
|
|
11
|
-
var diff = require('diff');
|
|
12
|
-
var fg = require('fast-glob');
|
|
13
|
-
var picomatch = require('picomatch');
|
|
14
|
-
|
|
15
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
16
|
-
|
|
17
|
-
function _interopNamespace(e) {
|
|
18
|
-
if (e && e.__esModule) return e;
|
|
19
|
-
var n = Object.create(null);
|
|
20
|
-
if (e) {
|
|
21
|
-
Object.keys(e).forEach(function (k) {
|
|
22
|
-
if (k !== 'default') {
|
|
23
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
24
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
get: function () { return e[k]; }
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
n.default = e;
|
|
32
|
-
return Object.freeze(n);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
var generate__default = /*#__PURE__*/_interopDefault(generate);
|
|
36
|
-
var traverse__default = /*#__PURE__*/_interopDefault(traverse);
|
|
37
|
-
var t2__namespace = /*#__PURE__*/_interopNamespace(t2);
|
|
38
|
-
var postcss__default = /*#__PURE__*/_interopDefault(postcss);
|
|
39
|
-
var fg__default = /*#__PURE__*/_interopDefault(fg);
|
|
40
|
-
var picomatch__default = /*#__PURE__*/_interopDefault(picomatch);
|
|
41
|
-
|
|
42
|
-
function camelCaseProperty(property) {
|
|
43
|
-
return property.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
44
|
-
}
|
|
45
|
-
function toValueNode(value) {
|
|
46
|
-
const trimmed = value.trim();
|
|
47
|
-
if (/^-?\d+(\.\d+)?$/.test(trimmed)) {
|
|
48
|
-
return t2__namespace.numericLiteral(Number(trimmed));
|
|
49
|
-
}
|
|
50
|
-
return t2__namespace.stringLiteral(trimmed);
|
|
51
|
-
}
|
|
52
|
-
function toKeyNode(key) {
|
|
53
|
-
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key)) {
|
|
54
|
-
return t2__namespace.identifier(key);
|
|
55
|
-
}
|
|
56
|
-
return t2__namespace.stringLiteral(key);
|
|
57
|
-
}
|
|
58
|
-
function nodesToObject(nodes, warnings) {
|
|
59
|
-
const properties = [];
|
|
60
|
-
for (const node of nodes ?? []) {
|
|
61
|
-
if (node.type === "decl") {
|
|
62
|
-
const normalized = node.prop.startsWith("--") ? node.prop : camelCaseProperty(node.prop);
|
|
63
|
-
properties.push(t2__namespace.objectProperty(toKeyNode(normalized), toValueNode(node.value)));
|
|
64
|
-
continue;
|
|
65
|
-
}
|
|
66
|
-
if (node.type === "rule") {
|
|
67
|
-
const nested = nodesToObject(node.nodes, warnings);
|
|
68
|
-
properties.push(t2__namespace.objectProperty(t2__namespace.stringLiteral(node.selector.trim()), nested));
|
|
69
|
-
continue;
|
|
70
|
-
}
|
|
71
|
-
warnings.push({
|
|
72
|
-
message: `Unsupported CSS node "${node.type}" skipped.`
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
return t2__namespace.objectExpression(properties);
|
|
76
|
-
}
|
|
77
|
-
function cssToObjectExpression(cssText, warnings) {
|
|
78
|
-
try {
|
|
79
|
-
const root = postcss__default.default.parse(cssText);
|
|
80
|
-
return nodesToObject(root.nodes, warnings);
|
|
81
|
-
} catch (error) {
|
|
82
|
-
warnings.push({
|
|
83
|
-
message: `Could not parse CSS template literal: ${error.message}`
|
|
84
|
-
});
|
|
85
|
-
return null;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// src/transform.ts
|
|
90
|
-
function toKebabCase(input) {
|
|
91
|
-
return input.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/[_\s]+/g, "-").toLowerCase();
|
|
92
|
-
}
|
|
93
|
-
function memberExpressionToJsxName(expression) {
|
|
94
|
-
if (t2__namespace.isIdentifier(expression)) {
|
|
95
|
-
return t2__namespace.jsxIdentifier(expression.name);
|
|
96
|
-
}
|
|
97
|
-
if (t2__namespace.isMemberExpression(expression) && !expression.computed) {
|
|
98
|
-
const left = memberExpressionToJsxName(expression.object);
|
|
99
|
-
const right = t2__namespace.isIdentifier(expression.property) ? t2__namespace.jsxIdentifier(expression.property.name) : null;
|
|
100
|
-
if (!left || !right) return null;
|
|
101
|
-
return t2__namespace.jsxMemberExpression(left, right);
|
|
102
|
-
}
|
|
103
|
-
return null;
|
|
104
|
-
}
|
|
105
|
-
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) };
|
|
108
|
-
}
|
|
109
|
-
if (t2__namespace.isCallExpression(tag) && t2__namespace.isIdentifier(tag.callee) && styledNames.has(tag.callee.name)) {
|
|
110
|
-
const firstArg = tag.arguments[0];
|
|
111
|
-
if (!firstArg || !t2__namespace.isExpression(firstArg)) return null;
|
|
112
|
-
const jsxName = memberExpressionToJsxName(firstArg);
|
|
113
|
-
if (!jsxName) return null;
|
|
114
|
-
return { kind: "component", jsxName };
|
|
115
|
-
}
|
|
116
|
-
return null;
|
|
117
|
-
}
|
|
118
|
-
function addWarning(warnings, message, nodeName) {
|
|
119
|
-
warnings.push({ message, nodeName });
|
|
120
|
-
}
|
|
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")
|
|
128
|
-
),
|
|
129
|
-
[t2__namespace.identifier("Boolean")]
|
|
130
|
-
),
|
|
131
|
-
t2__namespace.identifier("join")
|
|
132
|
-
),
|
|
133
|
-
[t2__namespace.stringLiteral(" ")]
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
function updateClassNameAttribute(openingElement, classNameIdentifier) {
|
|
137
|
-
const existingAttr = openingElement.attributes.find(
|
|
138
|
-
(attribute) => t2__namespace.isJSXAttribute(attribute) && t2__namespace.isJSXIdentifier(attribute.name, { name: "className" })
|
|
139
|
-
);
|
|
140
|
-
if (!existingAttr) {
|
|
141
|
-
openingElement.attributes.push(
|
|
142
|
-
t2__namespace.jsxAttribute(t2__namespace.jsxIdentifier("className"), t2__namespace.jsxExpressionContainer(classNameIdentifier))
|
|
143
|
-
);
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
if (!existingAttr.value) {
|
|
147
|
-
existingAttr.value = t2__namespace.jsxExpressionContainer(classNameIdentifier);
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
if (t2__namespace.isStringLiteral(existingAttr.value)) {
|
|
151
|
-
existingAttr.value = t2__namespace.jsxExpressionContainer(
|
|
152
|
-
createMergedClassExpression(t2__namespace.stringLiteral(existingAttr.value.value), classNameIdentifier)
|
|
153
|
-
);
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
if (t2__namespace.isJSXExpressionContainer(existingAttr.value)) {
|
|
157
|
-
existingAttr.value = t2__namespace.jsxExpressionContainer(
|
|
158
|
-
createMergedClassExpression(
|
|
159
|
-
existingAttr.value.expression,
|
|
160
|
-
classNameIdentifier
|
|
161
|
-
)
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
function ensureStylesImport(ast) {
|
|
166
|
-
let typestylesImport = null;
|
|
167
|
-
for (const statement of ast.program.body) {
|
|
168
|
-
if (t2__namespace.isImportDeclaration(statement) && statement.source.value === "typestyles") {
|
|
169
|
-
typestylesImport = statement;
|
|
170
|
-
break;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
if (!typestylesImport) {
|
|
174
|
-
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
|
-
)
|
|
179
|
-
);
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
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"))
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
function cleanupUnusedImports(ast) {
|
|
192
|
-
traverse__default.default(ast, {
|
|
193
|
-
Program(path) {
|
|
194
|
-
path.scope.crawl();
|
|
195
|
-
},
|
|
196
|
-
ImportDeclaration(path) {
|
|
197
|
-
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" })) {
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
const local = specifier.local.name;
|
|
202
|
-
const binding = path.scope.getBinding(local);
|
|
203
|
-
return !binding || binding.referencePaths.length === 0;
|
|
204
|
-
});
|
|
205
|
-
if (unusedLocals.length === 0) return;
|
|
206
|
-
path.node.specifiers = path.node.specifiers.filter(
|
|
207
|
-
(specifier) => !unusedLocals.includes(specifier)
|
|
208
|
-
);
|
|
209
|
-
if (path.node.specifiers.length === 0) {
|
|
210
|
-
path.remove();
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
function isOnlyJsxReferences(binding) {
|
|
216
|
-
return binding.referencePaths.every((referencePath) => {
|
|
217
|
-
const parent = referencePath.parentPath;
|
|
218
|
-
if (!parent) return false;
|
|
219
|
-
return parent.isJSXOpeningElement() && parent.get("name") === referencePath || parent.isJSXClosingElement() && parent.get("name") === referencePath;
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
function migrateSource(filePath, source) {
|
|
223
|
-
const warnings = [];
|
|
224
|
-
let changed = false;
|
|
225
|
-
const ast = parser.parse(source, {
|
|
226
|
-
sourceType: "module",
|
|
227
|
-
plugins: ["typescript", "jsx"]
|
|
228
|
-
});
|
|
229
|
-
const styledNames = /* @__PURE__ */ new Set();
|
|
230
|
-
const cssTagNames = /* @__PURE__ */ new Set();
|
|
231
|
-
const styledTransforms = /* @__PURE__ */ new Map();
|
|
232
|
-
traverse__default.default(ast, {
|
|
233
|
-
ImportDeclaration(path) {
|
|
234
|
-
if (path.node.source.value === "styled-components" || path.node.source.value === "@emotion/styled") {
|
|
235
|
-
for (const specifier of path.node.specifiers) {
|
|
236
|
-
if (t2__namespace.isImportDefaultSpecifier(specifier)) {
|
|
237
|
-
styledNames.add(specifier.local.name);
|
|
238
|
-
}
|
|
239
|
-
if (t2__namespace.isImportSpecifier(specifier) && t2__namespace.isIdentifier(specifier.imported, { name: "css" })) {
|
|
240
|
-
cssTagNames.add(specifier.local.name);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
if (path.node.source.value === "@emotion/react" || path.node.source.value === "@emotion/css") {
|
|
245
|
-
for (const specifier of path.node.specifiers) {
|
|
246
|
-
if (t2__namespace.isImportSpecifier(specifier) && t2__namespace.isIdentifier(specifier.imported, { name: "css" })) {
|
|
247
|
-
cssTagNames.add(specifier.local.name);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
});
|
|
253
|
-
traverse__default.default(ast, {
|
|
254
|
-
VariableDeclarator(path) {
|
|
255
|
-
if (!t2__namespace.isIdentifier(path.node.id)) return;
|
|
256
|
-
if (!t2__namespace.isTaggedTemplateExpression(path.node.init)) return;
|
|
257
|
-
const variableName = path.node.id.name;
|
|
258
|
-
const binding = path.scope.getBinding(variableName);
|
|
259
|
-
if (!binding) return;
|
|
260
|
-
const template = path.node.init.quasi;
|
|
261
|
-
if (template.expressions.length > 0) {
|
|
262
|
-
addWarning(
|
|
263
|
-
warnings,
|
|
264
|
-
"Skipped template literal with interpolations. Only static templates are migrated.",
|
|
265
|
-
variableName
|
|
266
|
-
);
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
const cssText = template.quasis.map((quasi) => quasi.value.cooked ?? "").join("");
|
|
270
|
-
const objectExpression2 = cssToObjectExpression(cssText, warnings);
|
|
271
|
-
if (!objectExpression2) {
|
|
272
|
-
addWarning(warnings, "Skipped because CSS could not be parsed.", variableName);
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
275
|
-
const styledTarget = parseStyledTarget(path.node.init.tag, styledNames);
|
|
276
|
-
if (styledTarget) {
|
|
277
|
-
const declaration = path.parentPath;
|
|
278
|
-
if (declaration.parentPath?.isExportNamedDeclaration() || declaration.parentPath?.isExportDefaultDeclaration()) {
|
|
279
|
-
addWarning(
|
|
280
|
-
warnings,
|
|
281
|
-
"Skipped exported styled component to avoid changing external API shape.",
|
|
282
|
-
variableName
|
|
283
|
-
);
|
|
284
|
-
return;
|
|
285
|
-
}
|
|
286
|
-
if (!isOnlyJsxReferences(binding)) {
|
|
287
|
-
addWarning(warnings, "Skipped styled component with non-JSX references.", variableName);
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
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
|
-
);
|
|
296
|
-
styledTransforms.set(variableName, {
|
|
297
|
-
originalName: variableName,
|
|
298
|
-
classConstName,
|
|
299
|
-
target: styledTarget
|
|
300
|
-
});
|
|
301
|
-
changed = true;
|
|
302
|
-
return;
|
|
303
|
-
}
|
|
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
|
-
);
|
|
309
|
-
changed = true;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
});
|
|
313
|
-
if (styledTransforms.size > 0) {
|
|
314
|
-
traverse__default.default(ast, {
|
|
315
|
-
JSXElement(path) {
|
|
316
|
-
const openingName = path.node.openingElement.name;
|
|
317
|
-
if (!t2__namespace.isJSXIdentifier(openingName)) return;
|
|
318
|
-
const transform = styledTransforms.get(openingName.name);
|
|
319
|
-
if (!transform) return;
|
|
320
|
-
path.node.openingElement.name = transform.target.jsxName;
|
|
321
|
-
if (path.node.closingElement) {
|
|
322
|
-
path.node.closingElement.name = transform.target.jsxName;
|
|
323
|
-
}
|
|
324
|
-
updateClassNameAttribute(path.node.openingElement, t2__namespace.identifier(transform.classConstName));
|
|
325
|
-
}
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
if (changed) {
|
|
329
|
-
ensureStylesImport(ast);
|
|
330
|
-
cleanupUnusedImports(ast);
|
|
331
|
-
}
|
|
332
|
-
const output = generate__default.default(
|
|
333
|
-
ast,
|
|
334
|
-
{
|
|
335
|
-
retainLines: false,
|
|
336
|
-
comments: true,
|
|
337
|
-
concise: false
|
|
338
|
-
},
|
|
339
|
-
source
|
|
340
|
-
);
|
|
341
|
-
return {
|
|
342
|
-
filePath,
|
|
343
|
-
changed: changed && output.code !== source,
|
|
344
|
-
code: output.code,
|
|
345
|
-
warnings
|
|
346
|
-
};
|
|
347
|
-
}
|
|
348
|
-
var DEFAULT_EXCLUDES = ["**/node_modules/**", "**/dist/**", "**/.next/**", "**/.turbo/**"];
|
|
349
|
-
function normalizeExtension(extension) {
|
|
350
|
-
return extension.startsWith(".") ? extension : `.${extension}`;
|
|
351
|
-
}
|
|
352
|
-
async function collectTargetFiles(cwd, targets, extensions, include, exclude) {
|
|
353
|
-
const normalizedTargets = targets.length > 0 ? targets : ["."];
|
|
354
|
-
const literalFiles = [];
|
|
355
|
-
const dynamicPatterns = [];
|
|
356
|
-
for (const target of normalizedTargets) {
|
|
357
|
-
const absolute = path.resolve(cwd, target);
|
|
358
|
-
try {
|
|
359
|
-
const targetStat = await promises.stat(absolute);
|
|
360
|
-
if (targetStat.isFile()) {
|
|
361
|
-
literalFiles.push(absolute);
|
|
362
|
-
} else if (targetStat.isDirectory()) {
|
|
363
|
-
dynamicPatterns.push(`${absolute.split("\\").join("/")}/**/*`);
|
|
364
|
-
}
|
|
365
|
-
} catch {
|
|
366
|
-
dynamicPatterns.push(target);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
const globbed = await fg__default.default(dynamicPatterns, {
|
|
370
|
-
cwd,
|
|
371
|
-
absolute: true,
|
|
372
|
-
onlyFiles: true,
|
|
373
|
-
ignore: [...DEFAULT_EXCLUDES, ...exclude]
|
|
374
|
-
});
|
|
375
|
-
const extensionSet = new Set(extensions.map(normalizeExtension));
|
|
376
|
-
const includeMatchers = include.length > 0 ? include.map((pattern) => picomatch__default.default(pattern)) : null;
|
|
377
|
-
const unique = /* @__PURE__ */ new Set([...literalFiles, ...globbed]);
|
|
378
|
-
return Array.from(unique).filter((filePath) => extensionSet.has(path.extname(filePath))).filter((filePath) => {
|
|
379
|
-
if (!includeMatchers) return true;
|
|
380
|
-
const rel = path.relative(cwd, filePath).split("\\").join("/");
|
|
381
|
-
return includeMatchers.some((matcher) => matcher(rel));
|
|
382
|
-
}).sort();
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
// src/migrate.ts
|
|
386
|
-
function renderPatch(filePath, before, after) {
|
|
387
|
-
return diff.createPatch(filePath, before, after, "before", "after").split("\n").slice(2).join("\n");
|
|
388
|
-
}
|
|
389
|
-
async function runMigration(cwd, options) {
|
|
390
|
-
const files = await collectTargetFiles(
|
|
391
|
-
cwd,
|
|
392
|
-
options.targets,
|
|
393
|
-
options.extensions,
|
|
394
|
-
options.include,
|
|
395
|
-
options.exclude
|
|
396
|
-
);
|
|
397
|
-
const reportEntries = [];
|
|
398
|
-
let changedCount = 0;
|
|
399
|
-
let warningCount = 0;
|
|
400
|
-
for (const filePath of files) {
|
|
401
|
-
const before = await promises.readFile(filePath, "utf8");
|
|
402
|
-
const result = migrateSource(filePath, before);
|
|
403
|
-
const relativePath = path.relative(cwd, filePath);
|
|
404
|
-
warningCount += result.warnings.length;
|
|
405
|
-
if (result.changed) {
|
|
406
|
-
changedCount += 1;
|
|
407
|
-
if (options.write) {
|
|
408
|
-
await promises.writeFile(filePath, result.code, "utf8");
|
|
409
|
-
process.stdout.write(`updated ${relativePath}
|
|
410
|
-
`);
|
|
411
|
-
} else {
|
|
412
|
-
process.stdout.write(`
|
|
413
|
-
--- ${relativePath} (dry-run) ---
|
|
414
|
-
`);
|
|
415
|
-
process.stdout.write(`${renderPatch(relativePath, before, result.code)}
|
|
416
|
-
`);
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
for (const warning of result.warnings) {
|
|
420
|
-
const nodeLabel = warning.nodeName ? ` (${warning.nodeName})` : "";
|
|
421
|
-
process.stdout.write(`warning ${relativePath}${nodeLabel}: ${warning.message}
|
|
422
|
-
`);
|
|
423
|
-
}
|
|
424
|
-
reportEntries.push({
|
|
425
|
-
filePath: relativePath,
|
|
426
|
-
changed: result.changed,
|
|
427
|
-
warnings: result.warnings
|
|
428
|
-
});
|
|
429
|
-
}
|
|
430
|
-
const summary = {
|
|
431
|
-
filesScanned: files.length,
|
|
432
|
-
filesChanged: changedCount,
|
|
433
|
-
warnings: warningCount
|
|
434
|
-
};
|
|
435
|
-
process.stdout.write(
|
|
436
|
-
`
|
|
437
|
-
Scanned ${summary.filesScanned} files, changed ${summary.filesChanged}, warnings ${summary.warnings}.
|
|
438
|
-
`
|
|
439
|
-
);
|
|
440
|
-
const report = {
|
|
441
|
-
summary,
|
|
442
|
-
files: reportEntries
|
|
443
|
-
};
|
|
444
|
-
if (options.reportPath) {
|
|
445
|
-
await promises.mkdir(path.dirname(options.reportPath), { recursive: true });
|
|
446
|
-
await promises.writeFile(options.reportPath, `${JSON.stringify(report, null, 2)}
|
|
447
|
-
`, "utf8");
|
|
448
|
-
process.stdout.write(`Report written to ${options.reportPath}
|
|
449
|
-
`);
|
|
450
|
-
}
|
|
451
|
-
return report;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
exports.migrateSource = migrateSource;
|
|
455
|
-
exports.runMigration = runMigration;
|
|
456
|
-
//# sourceMappingURL=index.cjs.map
|
|
457
|
-
//# sourceMappingURL=index.cjs.map
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const require_migrate = require("./migrate-B3MfAQI5.cjs");
|
|
4
|
+
exports.migrateSource = require_migrate.migrateSource;
|
|
5
|
+
exports.runMigration = require_migrate.runMigration;
|