@ecopages/codemod 0.0.0-stage → 0.9.0-rc.0
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/README.md +132 -2
- package/package.json +61 -4
- package/src/transforms/migrate-core-entrypoints.d.ts +3 -0
- package/src/transforms/migrate-core-entrypoints.js +303 -0
- package/src/transforms/migrate-data-hooks.d.ts +36 -0
- package/src/transforms/migrate-data-hooks.js +477 -0
- package/src/transforms/migrate-to-eco-component.d.ts +18 -0
- package/src/transforms/migrate-to-eco-component.js +224 -0
- package/src/transforms/migrate-to-eco-page.d.ts +23 -0
- package/src/transforms/migrate-to-eco-page.js +444 -0
- package/src/transforms/print-options.d.ts +13 -0
- package/src/transforms/print-options.js +15 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codemod: migrate-to-eco-component
|
|
3
|
+
*
|
|
4
|
+
* Transforms components from the legacy pattern with .config assignment to eco.component() API.
|
|
5
|
+
*
|
|
6
|
+
* Before:
|
|
7
|
+
* export const Counter: EcoComponent<Props> = ({ count }) => <my-counter />;
|
|
8
|
+
* Counter.config = { dependencies: { scripts, stylesheets } };
|
|
9
|
+
*
|
|
10
|
+
* After:
|
|
11
|
+
* export const Counter = eco.component<Props>({
|
|
12
|
+
* dependencies: { scripts, stylesheets },
|
|
13
|
+
* render: ({ count }) => <my-counter />,
|
|
14
|
+
* });
|
|
15
|
+
*/
|
|
16
|
+
import { printOptions } from './print-options.js';
|
|
17
|
+
export const parser = 'tsx';
|
|
18
|
+
function identifierFromTypeReference(node) {
|
|
19
|
+
if (node.type !== 'TSTypeReference' || node.typeName?.type !== 'Identifier') {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
return node.typeName.name ?? null;
|
|
23
|
+
}
|
|
24
|
+
function extractEcoComponentPropsType(declarator) {
|
|
25
|
+
const typeRef = declarator.typeAnnotation?.typeAnnotation;
|
|
26
|
+
if (typeRef?.type !== 'TSTypeReference') {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
const firstParam = typeRef.typeParameters?.params?.[0];
|
|
30
|
+
if (!firstParam) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const nestedParam = firstParam.typeParameters?.params?.[0];
|
|
34
|
+
if (nestedParam) {
|
|
35
|
+
return identifierFromTypeReference(nestedParam);
|
|
36
|
+
}
|
|
37
|
+
return identifierFromTypeReference(firstParam);
|
|
38
|
+
}
|
|
39
|
+
function tryExtractComponentFromVariableDeclaration(j, varPath, componentName, dependencies) {
|
|
40
|
+
const node = varPath.node;
|
|
41
|
+
const declarator = node.declarations?.[0];
|
|
42
|
+
if (declarator?.type !== 'VariableDeclarator' ||
|
|
43
|
+
declarator.id?.type !== 'Identifier' ||
|
|
44
|
+
declarator.id.name !== componentName) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
if (declarator.init?.type !== 'ArrowFunctionExpression' && declarator.init?.type !== 'FunctionExpression') {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const propsType = extractEcoComponentPropsType(declarator.id);
|
|
51
|
+
const parentNode = varPath.parent?.node;
|
|
52
|
+
const isExported = parentNode?.type === 'ExportNamedDeclaration';
|
|
53
|
+
return {
|
|
54
|
+
componentName,
|
|
55
|
+
propsType,
|
|
56
|
+
renderBody: declarator.init,
|
|
57
|
+
dependencies,
|
|
58
|
+
exportType: isExported ? 'named' : 'default',
|
|
59
|
+
declarationPath: isExported ? varPath.parent : varPath,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function extractDependenciesFromConfig(configObject) {
|
|
63
|
+
for (const prop of configObject.properties ?? []) {
|
|
64
|
+
const objectProp = prop;
|
|
65
|
+
if (objectProp.type === 'ObjectProperty' &&
|
|
66
|
+
objectProp.key?.type === 'Identifier' &&
|
|
67
|
+
objectProp.key.name === 'dependencies') {
|
|
68
|
+
return objectProp.value;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
function isComponentConfigAssignment(expr) {
|
|
74
|
+
if (expr.type !== 'AssignmentExpression') {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
const left = expr.left;
|
|
78
|
+
if (left.type !== 'MemberExpression' ||
|
|
79
|
+
left.object?.type !== 'Identifier' ||
|
|
80
|
+
left.property?.type !== 'Identifier' ||
|
|
81
|
+
left.property.name !== 'config' ||
|
|
82
|
+
expr.right?.type !== 'ObjectExpression') {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
componentName: left.object.name ?? '',
|
|
87
|
+
configObject: expr.right,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function componentAlreadyUsesEco(existingEcoComponent, componentName) {
|
|
91
|
+
let alreadyMigrated = false;
|
|
92
|
+
existingEcoComponent.forEach((ecoPath) => {
|
|
93
|
+
const parent = ecoPath.parent?.node;
|
|
94
|
+
if (parent?.type === 'VariableDeclarator' && parent.id?.name === componentName) {
|
|
95
|
+
alreadyMigrated = true;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
return alreadyMigrated;
|
|
99
|
+
}
|
|
100
|
+
function replaceComponentDeclaration(j, root, comp) {
|
|
101
|
+
const componentProperties = [];
|
|
102
|
+
if (comp.dependencies) {
|
|
103
|
+
componentProperties.push(j.objectProperty(j.identifier('dependencies'), comp.dependencies));
|
|
104
|
+
}
|
|
105
|
+
componentProperties.push(j.objectProperty(j.identifier('render'), comp.renderBody));
|
|
106
|
+
const ecoComponentCall = j.callExpression(j.memberExpression(j.identifier('eco'), j.identifier('component')), [
|
|
107
|
+
j.objectExpression(componentProperties),
|
|
108
|
+
]);
|
|
109
|
+
if (comp.propsType) {
|
|
110
|
+
ecoComponentCall.typeParameters = j.tsTypeParameterInstantiation([
|
|
111
|
+
j.tsTypeReference(j.identifier(comp.propsType)),
|
|
112
|
+
]);
|
|
113
|
+
}
|
|
114
|
+
const newDeclarator = j.variableDeclarator(j.identifier(comp.componentName), ecoComponentCall);
|
|
115
|
+
const newDeclaration = j.variableDeclaration('const', [newDeclarator]);
|
|
116
|
+
if (comp.exportType === 'named') {
|
|
117
|
+
j(comp.declarationPath).replaceWith(j.exportNamedDeclaration(newDeclaration));
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
j(comp.declarationPath).replaceWith(newDeclaration);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function ensureEcoImport(j, root) {
|
|
124
|
+
const ecoImports = root.find(j.ImportDeclaration, {
|
|
125
|
+
source: { value: '@ecopages/core' },
|
|
126
|
+
});
|
|
127
|
+
let hasEcoImport = false;
|
|
128
|
+
ecoImports.forEach((path) => {
|
|
129
|
+
if (path.node.importKind === 'type') {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
path.node.specifiers?.forEach((spec) => {
|
|
133
|
+
if (spec.type === 'ImportSpecifier' &&
|
|
134
|
+
spec.imported.type === 'Identifier' &&
|
|
135
|
+
spec.imported.name === 'eco') {
|
|
136
|
+
hasEcoImport = true;
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
if (hasEcoImport) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const newImport = j.importDeclaration([j.importSpecifier(j.identifier('eco'))], j.literal('@ecopages/core'));
|
|
144
|
+
const firstImport = root.find(j.ImportDeclaration).at(0);
|
|
145
|
+
if (firstImport.length > 0) {
|
|
146
|
+
firstImport.insertBefore(newImport);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
root.get().node.program.body.unshift(newImport);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function pruneUnusedLegacyComponentTypeImports(j, root) {
|
|
153
|
+
const unusedTypeNames = new Set(['EcoComponent', 'PageProps']);
|
|
154
|
+
const ecoImports = root.find(j.ImportDeclaration, {
|
|
155
|
+
source: { value: '@ecopages/core' },
|
|
156
|
+
});
|
|
157
|
+
ecoImports.forEach((path) => {
|
|
158
|
+
if (!path.node.specifiers) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
path.node.specifiers = path.node.specifiers.filter((spec) => {
|
|
162
|
+
if (spec.type === 'ImportSpecifier' && spec.imported.type === 'Identifier') {
|
|
163
|
+
const importedName = spec.imported.name;
|
|
164
|
+
if (unusedTypeNames.has(importedName)) {
|
|
165
|
+
const usages = root.find(j.TSTypeReference, {
|
|
166
|
+
typeName: { name: importedName },
|
|
167
|
+
});
|
|
168
|
+
return usages.length > 0;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return true;
|
|
172
|
+
});
|
|
173
|
+
if (path.node.specifiers.length === 0) {
|
|
174
|
+
j(path).remove();
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
export default function transformer(file, api, _options) {
|
|
179
|
+
const j = api.jscodeshift;
|
|
180
|
+
const root = j(file.source);
|
|
181
|
+
let hasChanges = false;
|
|
182
|
+
const existingEcoComponent = root.find(j.CallExpression, {
|
|
183
|
+
callee: {
|
|
184
|
+
type: 'MemberExpression',
|
|
185
|
+
object: { name: 'eco' },
|
|
186
|
+
property: { name: 'component' },
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
const componentsToTransform = [];
|
|
190
|
+
root.find(j.ExpressionStatement).forEach((path) => {
|
|
191
|
+
const assignment = isComponentConfigAssignment(path.node.expression);
|
|
192
|
+
if (!assignment) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const { componentName, configObject } = assignment;
|
|
196
|
+
if (componentAlreadyUsesEco(existingEcoComponent, componentName)) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const dependencies = extractDependenciesFromConfig(configObject);
|
|
200
|
+
let componentFound = false;
|
|
201
|
+
root.find(j.VariableDeclaration).forEach((varPath) => {
|
|
202
|
+
const extracted = tryExtractComponentFromVariableDeclaration(j, varPath, componentName, dependencies);
|
|
203
|
+
if (!extracted) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
componentsToTransform.push(extracted);
|
|
207
|
+
componentFound = true;
|
|
208
|
+
});
|
|
209
|
+
if (componentFound) {
|
|
210
|
+
j(path).remove();
|
|
211
|
+
hasChanges = true;
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
for (const comp of componentsToTransform) {
|
|
215
|
+
replaceComponentDeclaration(j, root, comp);
|
|
216
|
+
hasChanges = true;
|
|
217
|
+
}
|
|
218
|
+
if (!hasChanges) {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
ensureEcoImport(j, root);
|
|
222
|
+
pruneUnusedLegacyComponentTypeImports(j, root);
|
|
223
|
+
return root.toSource(printOptions(file.source));
|
|
224
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codemod: migrate-to-eco-page
|
|
3
|
+
*
|
|
4
|
+
* Transforms pages from the legacy pattern with separate exports to the consolidated eco.page() API.
|
|
5
|
+
*
|
|
6
|
+
* Before:
|
|
7
|
+
* export const getStaticPaths = ...
|
|
8
|
+
* export const getStaticProps = ...
|
|
9
|
+
* export const getMetadata = ...
|
|
10
|
+
* const Page: EcoComponent = () => ...
|
|
11
|
+
* Page.config = { layout, dependencies }
|
|
12
|
+
* export default Page
|
|
13
|
+
*
|
|
14
|
+
* After:
|
|
15
|
+
* export default eco.page({ layout, dependencies, paths, props, metadata, render })
|
|
16
|
+
*/
|
|
17
|
+
import type { API, FileInfo, Options } from 'jscodeshift';
|
|
18
|
+
export declare const parser = "tsx";
|
|
19
|
+
/**
|
|
20
|
+
* Consolidates the legacy exports into `eco.page()`, then renames the data hooks and drops their envelopes
|
|
21
|
+
* with `migrate-data-hooks`, so a page ends on the current API in one run.
|
|
22
|
+
*/
|
|
23
|
+
export default function transformer(file: FileInfo, api: API, options: Options): string | null;
|
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codemod: migrate-to-eco-page
|
|
3
|
+
*
|
|
4
|
+
* Transforms pages from the legacy pattern with separate exports to the consolidated eco.page() API.
|
|
5
|
+
*
|
|
6
|
+
* Before:
|
|
7
|
+
* export const getStaticPaths = ...
|
|
8
|
+
* export const getStaticProps = ...
|
|
9
|
+
* export const getMetadata = ...
|
|
10
|
+
* const Page: EcoComponent = () => ...
|
|
11
|
+
* Page.config = { layout, dependencies }
|
|
12
|
+
* export default Page
|
|
13
|
+
*
|
|
14
|
+
* After:
|
|
15
|
+
* export default eco.page({ layout, dependencies, paths, props, metadata, render })
|
|
16
|
+
*/
|
|
17
|
+
import migrateDataHooks from './migrate-data-hooks.js';
|
|
18
|
+
import { printOptions } from './print-options.js';
|
|
19
|
+
export const parser = 'tsx';
|
|
20
|
+
function readPagePropsTypeName(typeRef) {
|
|
21
|
+
if (typeRef?.type !== 'TSTypeReference' || typeRef.typeName?.type !== 'Identifier') {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
return typeRef.typeName.name ?? null;
|
|
25
|
+
}
|
|
26
|
+
function extractPagePropsType(id) {
|
|
27
|
+
const typeRef = id.typeAnnotation?.typeAnnotation;
|
|
28
|
+
if (typeRef?.type !== 'TSTypeReference' || !typeRef.typeParameters?.params?.length) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
const firstParam = typeRef.typeParameters.params[0];
|
|
32
|
+
if (firstParam.type !== 'TSTypeReference' || !firstParam.typeParameters?.params?.length) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
return readPagePropsTypeName(firstParam.typeParameters.params[0]);
|
|
36
|
+
}
|
|
37
|
+
function applyComponentDeclaratorToPageInfo(declarator, componentName, info) {
|
|
38
|
+
if (declarator.type !== 'VariableDeclarator' ||
|
|
39
|
+
declarator.id?.type !== 'Identifier' ||
|
|
40
|
+
declarator.id.name !== componentName) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
info.propsType = extractPagePropsType(declarator.id);
|
|
44
|
+
if (declarator.init?.type === 'ArrowFunctionExpression' || declarator.init?.type === 'FunctionExpression') {
|
|
45
|
+
info.renderBody = declarator.init;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const LEGACY_EXPORT_TARGETS = {
|
|
49
|
+
getStaticPaths: 'staticPaths',
|
|
50
|
+
getStaticProps: 'staticProps',
|
|
51
|
+
getMetadata: 'metadata',
|
|
52
|
+
};
|
|
53
|
+
const LEGACY_WRAPPER_NAMES = new Set(['staticPaths', 'staticProps', 'metadata']);
|
|
54
|
+
function readLegacyTarget(exportName) {
|
|
55
|
+
if (exportName === undefined || !Object.prototype.hasOwnProperty.call(LEGACY_EXPORT_TARGETS, exportName)) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
return LEGACY_EXPORT_TARGETS[exportName];
|
|
59
|
+
}
|
|
60
|
+
/** Returns the function inside an `eco.staticPaths(fn)`, `eco.staticProps(fn)` or `eco.metadata(fn)` wrapper. */
|
|
61
|
+
function unwrapLegacyWrapper(value) {
|
|
62
|
+
const call = value;
|
|
63
|
+
if (call?.type === 'CallExpression' &&
|
|
64
|
+
call.callee?.type === 'MemberExpression' &&
|
|
65
|
+
call.callee.object?.name === 'eco' &&
|
|
66
|
+
LEGACY_WRAPPER_NAMES.has(call.callee.property?.name ?? '') &&
|
|
67
|
+
call.arguments?.length === 1) {
|
|
68
|
+
return call.arguments[0];
|
|
69
|
+
}
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
function isReferencedByTypeof(j, root, name) {
|
|
73
|
+
return (root.find(j.TSTypeQuery).filter((path) => {
|
|
74
|
+
const exprName = path.node.exprName;
|
|
75
|
+
return exprName.type === 'Identifier' && exprName.name === name;
|
|
76
|
+
}).length > 0);
|
|
77
|
+
}
|
|
78
|
+
function toFunctionExpression(j, declaration) {
|
|
79
|
+
const expression = j.functionExpression(null, declaration.params, declaration.body, Boolean(declaration.generator));
|
|
80
|
+
expression.async = Boolean(declaration.async);
|
|
81
|
+
expression.returnType = declaration.returnType;
|
|
82
|
+
expression.typeParameters = declaration.typeParameters;
|
|
83
|
+
return expression;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Moves one `export const|function getX` declaration into the page info.
|
|
87
|
+
*
|
|
88
|
+
* @remarks
|
|
89
|
+
* A declaration that a `typeof getX` type still names, or that the existing `eco.page(...)` call
|
|
90
|
+
* already references, stays in the file unexported. Only the first case also sets the page option.
|
|
91
|
+
*/
|
|
92
|
+
function applyDeclaredPageExport(j, root, path, info) {
|
|
93
|
+
const declaration = path.node.declaration;
|
|
94
|
+
if (declaration?.type === 'VariableDeclaration') {
|
|
95
|
+
return applyVariableExport(j, root, path, declaration, info);
|
|
96
|
+
}
|
|
97
|
+
if (declaration?.type === 'FunctionDeclaration') {
|
|
98
|
+
return applyFunctionExport(j, root, path, declaration, info);
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
function applyVariableExport(j, root, path, declaration, info) {
|
|
103
|
+
const declarator = declaration.declarations?.[0];
|
|
104
|
+
if (declarator?.type !== 'VariableDeclarator' || declarator.id?.type !== 'Identifier' || !declarator.init) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
const targetKey = readLegacyTarget(declarator.id.name);
|
|
108
|
+
if (!targetKey) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
const value = unwrapLegacyWrapper(declarator.init);
|
|
112
|
+
const isDeclared = info.declared.has(targetKey);
|
|
113
|
+
if (isDeclared || isReferencedByTypeof(j, root, declarator.id.name)) {
|
|
114
|
+
declarator.init = value;
|
|
115
|
+
if (!isDeclared) {
|
|
116
|
+
declarator.id.typeAnnotation = null;
|
|
117
|
+
info[targetKey] = j.identifier(declarator.id.name);
|
|
118
|
+
}
|
|
119
|
+
j(path).replaceWith(declaration);
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
info[targetKey] = value;
|
|
123
|
+
j(path).remove();
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
function applyFunctionExport(j, root, path, declaration, info) {
|
|
127
|
+
const targetKey = readLegacyTarget(declaration.id?.name);
|
|
128
|
+
if (!targetKey) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
const isDeclared = info.declared.has(targetKey);
|
|
132
|
+
if (isDeclared || isReferencedByTypeof(j, root, declaration.id.name)) {
|
|
133
|
+
if (!isDeclared) {
|
|
134
|
+
info[targetKey] = j.identifier(declaration.id.name);
|
|
135
|
+
}
|
|
136
|
+
j(path).replaceWith(declaration);
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
info[targetKey] = toFunctionExpression(j, declaration);
|
|
140
|
+
j(path).remove();
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
function findTopLevelDeclarator(root, j, name) {
|
|
144
|
+
let found = null;
|
|
145
|
+
root.find(j.VariableDeclarator).forEach((path) => {
|
|
146
|
+
const id = path.node.id;
|
|
147
|
+
const grandParent = path.parent?.parent?.node;
|
|
148
|
+
if (id.type === 'Identifier' && id.name === name && grandParent?.type === 'Program') {
|
|
149
|
+
found = path.node;
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
return found;
|
|
153
|
+
}
|
|
154
|
+
/** Moves the `export { local as getX }` specifiers into the page info and drops them from the list. */
|
|
155
|
+
function applyExportListSpecifiers(j, root, path, info) {
|
|
156
|
+
const node = path.node;
|
|
157
|
+
if (node.declaration || node.source || !node.specifiers) {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
let moved = false;
|
|
161
|
+
node.specifiers = node.specifiers.filter((specifier) => {
|
|
162
|
+
const exportedName = specifier.exported?.name;
|
|
163
|
+
const localName = specifier.local?.name;
|
|
164
|
+
const targetKey = readLegacyTarget(exportedName);
|
|
165
|
+
if (!targetKey || !localName) {
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
const declarator = findTopLevelDeclarator(root, j, localName);
|
|
169
|
+
if (declarator?.init) {
|
|
170
|
+
declarator.init = unwrapLegacyWrapper(declarator.init);
|
|
171
|
+
}
|
|
172
|
+
if (!info.declared.has(targetKey)) {
|
|
173
|
+
info[targetKey] = j.identifier(localName);
|
|
174
|
+
}
|
|
175
|
+
moved = true;
|
|
176
|
+
return false;
|
|
177
|
+
});
|
|
178
|
+
if (moved && node.specifiers.length === 0) {
|
|
179
|
+
j(path).remove();
|
|
180
|
+
}
|
|
181
|
+
return moved;
|
|
182
|
+
}
|
|
183
|
+
function extractNamedPageExports(j, root, info) {
|
|
184
|
+
let hasChanges = false;
|
|
185
|
+
root.find(j.ExportNamedDeclaration).forEach((path) => {
|
|
186
|
+
if (applyDeclaredPageExport(j, root, path, info) || applyExportListSpecifiers(j, root, path, info)) {
|
|
187
|
+
hasChanges = true;
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
return hasChanges;
|
|
191
|
+
}
|
|
192
|
+
function isPageConfigAssignment(expr, componentName) {
|
|
193
|
+
return (expr.type === 'AssignmentExpression' &&
|
|
194
|
+
expr.left?.type === 'MemberExpression' &&
|
|
195
|
+
expr.left.object?.type === 'Identifier' &&
|
|
196
|
+
expr.left.object.name === componentName &&
|
|
197
|
+
expr.left.property?.type === 'Identifier' &&
|
|
198
|
+
expr.left.property.name === 'config' &&
|
|
199
|
+
expr.right?.type === 'ObjectExpression');
|
|
200
|
+
}
|
|
201
|
+
function readLayoutAndDependenciesFromConfig(configObject, info) {
|
|
202
|
+
for (const prop of configObject.properties ?? []) {
|
|
203
|
+
const objectProp = prop;
|
|
204
|
+
if (objectProp.type !== 'ObjectProperty' || objectProp.key?.type !== 'Identifier') {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (objectProp.key.name === 'layout') {
|
|
208
|
+
info.layout = objectProp.value;
|
|
209
|
+
}
|
|
210
|
+
else if (objectProp.key.name === 'dependencies') {
|
|
211
|
+
info.dependencies = objectProp.value;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function applyPageConfigAssignment(j, root, componentName, info) {
|
|
216
|
+
let hasChanges = false;
|
|
217
|
+
root.find(j.ExpressionStatement).forEach((path) => {
|
|
218
|
+
const expr = path.node.expression;
|
|
219
|
+
if (!isPageConfigAssignment(expr, componentName)) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
readLayoutAndDependenciesFromConfig(expr.right, info);
|
|
223
|
+
j(path).remove();
|
|
224
|
+
hasChanges = true;
|
|
225
|
+
});
|
|
226
|
+
return hasChanges;
|
|
227
|
+
}
|
|
228
|
+
function removeComponentVariableDeclaration(j, root, componentName) {
|
|
229
|
+
let hasChanges = false;
|
|
230
|
+
root.find(j.VariableDeclaration).forEach((path) => {
|
|
231
|
+
const declarator = path.node.declarations?.[0];
|
|
232
|
+
if (declarator?.type !== 'VariableDeclarator' ||
|
|
233
|
+
declarator.id?.type !== 'Identifier' ||
|
|
234
|
+
declarator.id.name !== componentName) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const parentNode = path.parent?.node;
|
|
238
|
+
if (parentNode?.type === 'ExportNamedDeclaration') {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
j(path).remove();
|
|
242
|
+
hasChanges = true;
|
|
243
|
+
});
|
|
244
|
+
return hasChanges;
|
|
245
|
+
}
|
|
246
|
+
function buildEcoPageExport(j, info, defaultExportPath) {
|
|
247
|
+
const pageProperties = [];
|
|
248
|
+
if (info.layout) {
|
|
249
|
+
pageProperties.push(j.objectProperty(j.identifier('layout'), info.layout));
|
|
250
|
+
}
|
|
251
|
+
if (info.dependencies) {
|
|
252
|
+
pageProperties.push(j.objectProperty(j.identifier('dependencies'), info.dependencies));
|
|
253
|
+
}
|
|
254
|
+
if (info.staticPaths) {
|
|
255
|
+
pageProperties.push(j.objectProperty(j.identifier('staticPaths'), info.staticPaths));
|
|
256
|
+
}
|
|
257
|
+
if (info.staticProps) {
|
|
258
|
+
pageProperties.push(j.objectProperty(j.identifier('staticProps'), info.staticProps));
|
|
259
|
+
}
|
|
260
|
+
if (info.metadata) {
|
|
261
|
+
pageProperties.push(j.objectProperty(j.identifier('metadata'), info.metadata));
|
|
262
|
+
}
|
|
263
|
+
pageProperties.push(j.objectProperty(j.identifier('render'), info.renderBody));
|
|
264
|
+
const ecoPageCall = j.callExpression(j.memberExpression(j.identifier('eco'), j.identifier('page')), [
|
|
265
|
+
j.objectExpression(pageProperties),
|
|
266
|
+
]);
|
|
267
|
+
if (info.propsType) {
|
|
268
|
+
ecoPageCall.typeParameters = j.tsTypeParameterInstantiation([
|
|
269
|
+
j.tsTypeReference(j.identifier(info.propsType)),
|
|
270
|
+
]);
|
|
271
|
+
}
|
|
272
|
+
if (defaultExportPath) {
|
|
273
|
+
j(defaultExportPath).replaceWith(j.exportDefaultDeclaration(ecoPageCall));
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
function ensureEcoImport(j, root) {
|
|
277
|
+
const ecoImports = root.find(j.ImportDeclaration, {
|
|
278
|
+
source: { value: '@ecopages/core' },
|
|
279
|
+
});
|
|
280
|
+
let hasEcoImport = false;
|
|
281
|
+
ecoImports.forEach((path) => {
|
|
282
|
+
if (path.node.importKind === 'type') {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
path.node.specifiers?.forEach((spec) => {
|
|
286
|
+
if (spec.type === 'ImportSpecifier' &&
|
|
287
|
+
spec.imported.type === 'Identifier' &&
|
|
288
|
+
spec.imported.name === 'eco') {
|
|
289
|
+
hasEcoImport = true;
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
if (hasEcoImport) {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
const newImport = j.importDeclaration([j.importSpecifier(j.identifier('eco'))], j.literal('@ecopages/core'));
|
|
297
|
+
const firstImport = root.find(j.ImportDeclaration).at(0);
|
|
298
|
+
if (firstImport.length > 0) {
|
|
299
|
+
firstImport.insertBefore(newImport);
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
root.get().node.program.body.unshift(newImport);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
function pruneLegacyPageTypeImports(j, root) {
|
|
306
|
+
const ecoImports = root.find(j.ImportDeclaration, {
|
|
307
|
+
source: { value: '@ecopages/core' },
|
|
308
|
+
});
|
|
309
|
+
const typesToRemove = ['EcoComponent', 'GetStaticPaths', 'GetStaticProps', 'GetMetadata', 'PageProps'];
|
|
310
|
+
ecoImports.forEach((path) => {
|
|
311
|
+
if (!path.node.specifiers) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
path.node.specifiers = path.node.specifiers.filter((spec) => {
|
|
315
|
+
if (spec.type === 'ImportSpecifier' && spec.imported.type === 'Identifier') {
|
|
316
|
+
return !typesToRemove.includes(spec.imported.name);
|
|
317
|
+
}
|
|
318
|
+
return true;
|
|
319
|
+
});
|
|
320
|
+
if (path.node.specifiers.length === 0) {
|
|
321
|
+
j(path).remove();
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
/** Whether `name` is still named anywhere in the file besides its import specifier. */
|
|
326
|
+
function isStillReferenced(j, root, name) {
|
|
327
|
+
return (root.find(j.Identifier, { name }).filter((path) => {
|
|
328
|
+
const parentType = path.parent?.node?.type;
|
|
329
|
+
return parentType !== 'ImportSpecifier';
|
|
330
|
+
}).length > 0);
|
|
331
|
+
}
|
|
332
|
+
function pruneUnusedLegacyTypeImports(j, root) {
|
|
333
|
+
root.find(j.ImportDeclaration, { source: { value: '@ecopages/core' } }).forEach((path) => {
|
|
334
|
+
path.node.specifiers = path.node.specifiers?.filter((spec) => {
|
|
335
|
+
if (spec.type !== 'ImportSpecifier' || spec.imported.type !== 'Identifier') {
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
const name = spec.imported.name;
|
|
339
|
+
return (!['GetStaticPaths', 'GetStaticProps', 'GetMetadata'].includes(name) || isStillReferenced(j, root, name));
|
|
340
|
+
});
|
|
341
|
+
if (path.node.specifiers?.length === 0) {
|
|
342
|
+
j(path).remove();
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
/** Adds the extracted data hooks to an existing `eco.page({...})` options object, ahead of `render`. */
|
|
347
|
+
function mergeIntoExistingEcoPage(j, ecoPageCall, info) {
|
|
348
|
+
let merged = false;
|
|
349
|
+
ecoPageCall.forEach((path) => {
|
|
350
|
+
const options = path.node
|
|
351
|
+
.arguments?.[0];
|
|
352
|
+
if (options?.type !== 'ObjectExpression' || !options.properties) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const present = new Set(options.properties.map((property) => property.key?.name));
|
|
356
|
+
const additions = ['staticPaths', 'staticProps', 'metadata']
|
|
357
|
+
.filter((key) => info[key] && !present.has(key))
|
|
358
|
+
.map((key) => j.objectProperty(j.identifier(key), info[key]));
|
|
359
|
+
if (additions.length === 0) {
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
const renderIndex = options.properties.findIndex((property) => property.key?.name === 'render');
|
|
363
|
+
options.properties.splice(renderIndex === -1 ? options.properties.length : renderIndex, 0, ...additions);
|
|
364
|
+
merged = true;
|
|
365
|
+
});
|
|
366
|
+
return merged;
|
|
367
|
+
}
|
|
368
|
+
function migratePage(file, api) {
|
|
369
|
+
const j = api.jscodeshift;
|
|
370
|
+
const root = j(file.source);
|
|
371
|
+
const existingEcoPage = root.find(j.CallExpression, {
|
|
372
|
+
callee: {
|
|
373
|
+
type: 'MemberExpression',
|
|
374
|
+
object: { name: 'eco' },
|
|
375
|
+
property: { name: 'page' },
|
|
376
|
+
},
|
|
377
|
+
});
|
|
378
|
+
const info = {
|
|
379
|
+
componentName: null,
|
|
380
|
+
propsType: null,
|
|
381
|
+
renderBody: null,
|
|
382
|
+
layout: null,
|
|
383
|
+
dependencies: null,
|
|
384
|
+
staticPaths: null,
|
|
385
|
+
staticProps: null,
|
|
386
|
+
metadata: null,
|
|
387
|
+
declared: new Set(),
|
|
388
|
+
};
|
|
389
|
+
existingEcoPage.forEach((path) => {
|
|
390
|
+
const options = path.node
|
|
391
|
+
.arguments?.[0];
|
|
392
|
+
for (const property of options?.type === 'ObjectExpression' ? (options.properties ?? []) : []) {
|
|
393
|
+
const optionName = property.key?.name;
|
|
394
|
+
if (optionName === 'staticPaths' || optionName === 'staticProps' || optionName === 'metadata') {
|
|
395
|
+
info.declared.add(optionName);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
let hasChanges = extractNamedPageExports(j, root, info);
|
|
400
|
+
if (existingEcoPage.length > 0) {
|
|
401
|
+
if (!hasChanges) {
|
|
402
|
+
return null;
|
|
403
|
+
}
|
|
404
|
+
mergeIntoExistingEcoPage(j, existingEcoPage, info);
|
|
405
|
+
pruneUnusedLegacyTypeImports(j, root);
|
|
406
|
+
return root.toSource(printOptions(file.source));
|
|
407
|
+
}
|
|
408
|
+
let defaultExportPath = null;
|
|
409
|
+
root.find(j.ExportDefaultDeclaration).forEach((path) => {
|
|
410
|
+
defaultExportPath = path;
|
|
411
|
+
const declaration = path.node.declaration;
|
|
412
|
+
if (declaration?.type === 'Identifier') {
|
|
413
|
+
info.componentName = declaration.name ?? null;
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
if (info.componentName) {
|
|
417
|
+
root.find(j.VariableDeclaration).forEach((path) => {
|
|
418
|
+
const declarator = path.node.declarations?.[0];
|
|
419
|
+
if (declarator?.type === 'VariableDeclarator') {
|
|
420
|
+
applyComponentDeclaratorToPageInfo(declarator, info.componentName, info);
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
hasChanges = applyPageConfigAssignment(j, root, info.componentName, info) || hasChanges;
|
|
424
|
+
hasChanges = removeComponentVariableDeclaration(j, root, info.componentName) || hasChanges;
|
|
425
|
+
}
|
|
426
|
+
if (!hasChanges || !info.renderBody) {
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
buildEcoPageExport(j, info, defaultExportPath);
|
|
430
|
+
ensureEcoImport(j, root);
|
|
431
|
+
pruneLegacyPageTypeImports(j, root);
|
|
432
|
+
return root.toSource(printOptions(file.source));
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Consolidates the legacy exports into `eco.page()`, then renames the data hooks and drops their envelopes
|
|
436
|
+
* with `migrate-data-hooks`, so a page ends on the current API in one run.
|
|
437
|
+
*/
|
|
438
|
+
export default function transformer(file, api, options) {
|
|
439
|
+
const migrated = migratePage(file, api);
|
|
440
|
+
if (migrated === null) {
|
|
441
|
+
return null;
|
|
442
|
+
}
|
|
443
|
+
return migrateDataHooks({ ...file, source: migrated }, api, options) ?? migrated;
|
|
444
|
+
}
|