@ecopages/codemod 0.0.0-stage → 0.9.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 +27 -2
- package/package.json +63 -3
- package/src/cli.d.ts +18 -0
- package/src/cli.js +62 -0
- 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
package/README.md
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ecopages/codemod
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Codemods that migrate an Ecopages project to the current patterns. Each transform is a [jscodeshift](https://github.com/facebook/jscodeshift) module.
|
|
4
|
+
|
|
5
|
+
The package follows the Ecopages release train. Use the version that matches the `@ecopages/core` you upgrade to.
|
|
6
|
+
|
|
7
|
+
## Run
|
|
8
|
+
|
|
9
|
+
From the project root, with no install:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npx @ecopages/codemod <transform> [paths...]
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Paths default to `src`. Extra flags pass through to jscodeshift, so `--dry --print` previews without writing files. `npx @ecopages/codemod --list` prints the transforms. Format the result afterwards, since the raw output indents rewritten lines with extra tabs.
|
|
16
|
+
|
|
17
|
+
## Transforms
|
|
18
|
+
|
|
19
|
+
| Transform | Added in | Migrates |
|
|
20
|
+
| -------------------------- | -------- | ------------------------------------------------------------------------------- |
|
|
21
|
+
| `migrate-data-hooks` | 0.9.0 | `staticProps` and `staticPaths` of `eco.page()` to `props` and `paths` |
|
|
22
|
+
| `migrate-to-eco-page` | 0.3.0 | Module-level data exports and `Page.config` to `eco.page()`; runs the one above |
|
|
23
|
+
| `migrate-to-eco-component` | 0.3.0 | `Component.config` assignments to `eco.component()` |
|
|
24
|
+
| `migrate-core-entrypoints` | 0.3.0 | Imports from `@ecopages/core` to the role-based entry points |
|
|
25
|
+
|
|
26
|
+
A transform leaves code it cannot rewrite safely unchanged and reports it, so the old API still fails loudly. Rename those by hand. Transforms leave already-migrated code unchanged, so running one twice is safe.
|
|
27
|
+
|
|
28
|
+
The release notes of each core version name the transform that goes with its breaking changes.
|
package/package.json
CHANGED
|
@@ -1,6 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecopages/codemod",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "Codemods for migrating ecopages to latest patterns",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
"./migrate-core-entrypoints": {
|
|
9
|
+
"import": "./src/transforms/migrate-core-entrypoints.js",
|
|
10
|
+
"types": "./src/transforms/migrate-core-entrypoints.d.ts",
|
|
11
|
+
"default": "./src/transforms/migrate-core-entrypoints.js"
|
|
12
|
+
},
|
|
13
|
+
"./migrate-data-hooks": {
|
|
14
|
+
"import": "./src/transforms/migrate-data-hooks.js",
|
|
15
|
+
"types": "./src/transforms/migrate-data-hooks.d.ts",
|
|
16
|
+
"default": "./src/transforms/migrate-data-hooks.js"
|
|
17
|
+
},
|
|
18
|
+
"./migrate-to-eco-component": {
|
|
19
|
+
"import": "./src/transforms/migrate-to-eco-component.js",
|
|
20
|
+
"types": "./src/transforms/migrate-to-eco-component.d.ts",
|
|
21
|
+
"default": "./src/transforms/migrate-to-eco-component.js"
|
|
22
|
+
},
|
|
23
|
+
"./migrate-to-eco-page": {
|
|
24
|
+
"import": "./src/transforms/migrate-to-eco-page.js",
|
|
25
|
+
"types": "./src/transforms/migrate-to-eco-page.d.ts",
|
|
26
|
+
"default": "./src/transforms/migrate-to-eco-page.js"
|
|
27
|
+
},
|
|
28
|
+
"./migrate-core-entrypoints.ts": {
|
|
29
|
+
"import": "./src/transforms/migrate-core-entrypoints.js",
|
|
30
|
+
"types": "./src/transforms/migrate-core-entrypoints.d.ts",
|
|
31
|
+
"default": "./src/transforms/migrate-core-entrypoints.js"
|
|
32
|
+
},
|
|
33
|
+
"./migrate-data-hooks.ts": {
|
|
34
|
+
"import": "./src/transforms/migrate-data-hooks.js",
|
|
35
|
+
"types": "./src/transforms/migrate-data-hooks.d.ts",
|
|
36
|
+
"default": "./src/transforms/migrate-data-hooks.js"
|
|
37
|
+
},
|
|
38
|
+
"./migrate-to-eco-component.ts": {
|
|
39
|
+
"import": "./src/transforms/migrate-to-eco-component.js",
|
|
40
|
+
"types": "./src/transforms/migrate-to-eco-component.d.ts",
|
|
41
|
+
"default": "./src/transforms/migrate-to-eco-component.js"
|
|
42
|
+
},
|
|
43
|
+
"./migrate-to-eco-page.ts": {
|
|
44
|
+
"import": "./src/transforms/migrate-to-eco-page.js",
|
|
45
|
+
"types": "./src/transforms/migrate-to-eco-page.d.ts",
|
|
46
|
+
"default": "./src/transforms/migrate-to-eco-page.js"
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"bin": {
|
|
50
|
+
"ecopages-codemod": "./src/cli.js"
|
|
51
|
+
},
|
|
52
|
+
"publishConfig": {
|
|
53
|
+
"access": "public"
|
|
54
|
+
},
|
|
55
|
+
"repository": {
|
|
56
|
+
"type": "git",
|
|
57
|
+
"url": "git+https://github.com/ecopages/ecopages.git",
|
|
58
|
+
"directory": "packages/codemod"
|
|
59
|
+
},
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"jscodeshift": "^17.3.0"
|
|
62
|
+
},
|
|
63
|
+
"peerDependencies": {
|
|
64
|
+
"typescript": "^5.0.0"
|
|
65
|
+
}
|
|
6
66
|
}
|
package/src/cli.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Transforms the CLI can run, each described for `--list`.
|
|
4
|
+
*/
|
|
5
|
+
export declare const TRANSFORMS: {
|
|
6
|
+
readonly 'migrate-data-hooks': "staticProps and staticPaths of eco.page() to props and paths";
|
|
7
|
+
readonly 'migrate-to-eco-page': "module-level data exports and Page.config to eco.page()";
|
|
8
|
+
readonly 'migrate-to-eco-component': "Component.config assignments to eco.component()";
|
|
9
|
+
readonly 'migrate-core-entrypoints': "@ecopages/core imports to the role-based entry points";
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Runs a transform through the bundled jscodeshift and returns its exit code.
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* The transform file keeps this module's own extension, so the CLI works both from the published
|
|
16
|
+
* `.js` build and from the `.ts` sources.
|
|
17
|
+
*/
|
|
18
|
+
export declare function run(argv: string[]): number;
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { extname } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
/**
|
|
7
|
+
* Transforms the CLI can run, each described for `--list`.
|
|
8
|
+
*/
|
|
9
|
+
export const TRANSFORMS = {
|
|
10
|
+
'migrate-data-hooks': 'staticProps and staticPaths of eco.page() to props and paths',
|
|
11
|
+
'migrate-to-eco-page': 'module-level data exports and Page.config to eco.page()',
|
|
12
|
+
'migrate-to-eco-component': 'Component.config assignments to eco.component()',
|
|
13
|
+
'migrate-core-entrypoints': '@ecopages/core imports to the role-based entry points',
|
|
14
|
+
};
|
|
15
|
+
const USAGE = `Usage: ecopages-codemod <transform> [paths...] [jscodeshift options]
|
|
16
|
+
|
|
17
|
+
Runs a transform over the given paths (default: src). Pass --dry --print to preview.
|
|
18
|
+
|
|
19
|
+
Transforms:
|
|
20
|
+
${Object.entries(TRANSFORMS)
|
|
21
|
+
.map(([name, description]) => ` ${name.padEnd(28)}${description}`)
|
|
22
|
+
.join('\n')}
|
|
23
|
+
`;
|
|
24
|
+
/**
|
|
25
|
+
* Runs a transform through the bundled jscodeshift and returns its exit code.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* The transform file keeps this module's own extension, so the CLI works both from the published
|
|
29
|
+
* `.js` build and from the `.ts` sources.
|
|
30
|
+
*/
|
|
31
|
+
export function run(argv) {
|
|
32
|
+
const [name, ...rest] = argv;
|
|
33
|
+
if (!name || name === '--help' || name === '-h') {
|
|
34
|
+
process.stdout.write(USAGE);
|
|
35
|
+
return name ? 0 : 1;
|
|
36
|
+
}
|
|
37
|
+
if (name === '--list') {
|
|
38
|
+
process.stdout.write(`${Object.keys(TRANSFORMS).join('\n')}\n`);
|
|
39
|
+
return 0;
|
|
40
|
+
}
|
|
41
|
+
if (!Object.hasOwn(TRANSFORMS, name)) {
|
|
42
|
+
process.stderr.write(`Unknown transform "${name}".\n\n${USAGE}`);
|
|
43
|
+
return 1;
|
|
44
|
+
}
|
|
45
|
+
const transform = fileURLToPath(new URL(`./transforms/${name}${extname(import.meta.url)}`, import.meta.url));
|
|
46
|
+
const jscodeshift = createRequire(import.meta.url).resolve('jscodeshift/bin/jscodeshift.js');
|
|
47
|
+
const hasFlag = (flag) => rest.some((arg) => arg === flag || arg.startsWith(`${flag}=`));
|
|
48
|
+
const hasPath = rest.some((arg) => !arg.startsWith('-'));
|
|
49
|
+
const args = [
|
|
50
|
+
jscodeshift,
|
|
51
|
+
'-t',
|
|
52
|
+
transform,
|
|
53
|
+
...(hasFlag('--parser') ? [] : ['--parser', 'tsx']),
|
|
54
|
+
...(hasFlag('--extensions') ? [] : ['--extensions=ts,tsx,js,jsx']),
|
|
55
|
+
...rest,
|
|
56
|
+
...(hasPath ? [] : ['src']),
|
|
57
|
+
];
|
|
58
|
+
return spawnSync(process.execPath, args, { stdio: 'inherit' }).status ?? 1;
|
|
59
|
+
}
|
|
60
|
+
if (import.meta.main) {
|
|
61
|
+
process.exitCode = run(process.argv.slice(2));
|
|
62
|
+
}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
import { printOptions } from './print-options.js';
|
|
5
|
+
export const parser = 'tsx';
|
|
6
|
+
const coreEntryBySubpath = {
|
|
7
|
+
'assets/hashed-browser-asset': 'processor',
|
|
8
|
+
'build/browser-runtime-manifest': 'build',
|
|
9
|
+
'build/browser-runtime-plugin': 'build',
|
|
10
|
+
'build/build-adapter': 'build',
|
|
11
|
+
'build/build-queue': 'build',
|
|
12
|
+
'build/contracts/content-virtual-modules': 'build/contracts',
|
|
13
|
+
'build/contracts/server-only-specifier': 'build/contracts',
|
|
14
|
+
'build/jsx-ownership-plugins': 'build',
|
|
15
|
+
'build/lit-static-render-worker-context': 'build',
|
|
16
|
+
'build/server-module': 'build',
|
|
17
|
+
cache: 'build',
|
|
18
|
+
'client/navigation-scripts': 'client',
|
|
19
|
+
'client/scroll': 'client',
|
|
20
|
+
'client/view-transitions': 'client',
|
|
21
|
+
'config/app-runtime': 'integration',
|
|
22
|
+
constants: 'build',
|
|
23
|
+
'dev-toolbar/build-error-contract': 'client',
|
|
24
|
+
'dev-toolbar/define-dev-tool': 'client',
|
|
25
|
+
'dev-toolbar/dev-toolbar-manifest-contract': 'client',
|
|
26
|
+
'dev/development-restart': 'build',
|
|
27
|
+
'dev/transform-server': 'integration',
|
|
28
|
+
'diagnostics/request-pipeline-metrics': 'build',
|
|
29
|
+
'eco/page-layout-normalization': 'client',
|
|
30
|
+
hash: 'build',
|
|
31
|
+
'hmr/hmr-entrypoint-output': 'integration',
|
|
32
|
+
'hmr/hmr-registered-entrypoints-context': 'integration',
|
|
33
|
+
'hmr/hmr-strategy': 'integration',
|
|
34
|
+
'internal/finalize-config': 'config',
|
|
35
|
+
'plugins/define-integration': 'integration',
|
|
36
|
+
'plugins/integration-plugin': 'integration',
|
|
37
|
+
'plugins/jsx-import-source.utils': 'integration',
|
|
38
|
+
'plugins/package-specifier': 'build',
|
|
39
|
+
'plugins/processor': 'processor',
|
|
40
|
+
'plugins/tsconfig-import-resolver': 'build',
|
|
41
|
+
'route-renderer/orchestration/custom-element-scripts/custom-element-script-preloader': 'integration',
|
|
42
|
+
'route-renderer/orchestration/custom-element-scripts/custom-element-server-module-importer': 'integration',
|
|
43
|
+
'route-renderer/orchestration/custom-element-scripts/custom-element-ssr-preload-entrypoint': 'integration',
|
|
44
|
+
'route-renderer/orchestration/document-shell/document-shell-render.service': 'integration',
|
|
45
|
+
'route-renderer/orchestration/document-shell/layout-shell-props.service': 'integration',
|
|
46
|
+
'route-renderer/orchestration/foreign-child/component-render-context': 'integration',
|
|
47
|
+
'route-renderer/orchestration/foreign-child/foreign-subtree-execution.service': 'integration',
|
|
48
|
+
'route-renderer/orchestration/integration-renderer': 'integration',
|
|
49
|
+
'route-renderer/orchestration/page-browser-graph/page-browser-graph-merge.utils': 'integration',
|
|
50
|
+
'route-renderer/orchestration/string-markup-renderer': 'integration',
|
|
51
|
+
'route-renderer/page-loading/file-scoped-dependency-components': 'integration',
|
|
52
|
+
'route-renderer/route-renderer': 'integration',
|
|
53
|
+
'router/link-intent': 'client',
|
|
54
|
+
'router/link-navigation-policy': 'client',
|
|
55
|
+
'router/navigation-coordinator': 'client',
|
|
56
|
+
'router/navigation-lifecycle': 'client',
|
|
57
|
+
'router/navigation-pipeline': 'client',
|
|
58
|
+
'services/asset-processing-service': 'integration',
|
|
59
|
+
'utils/path-containment': 'build',
|
|
60
|
+
'utils/resolve-entry-file': 'build',
|
|
61
|
+
};
|
|
62
|
+
function entrypointFor(specifier) {
|
|
63
|
+
const prefix = '@ecopages/core/';
|
|
64
|
+
if (!specifier.startsWith(prefix)) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
const entry = coreEntryBySubpath[specifier.slice(prefix.length)];
|
|
68
|
+
return entry ? `${prefix}${entry}` : undefined;
|
|
69
|
+
}
|
|
70
|
+
function findCoreSourceRoot() {
|
|
71
|
+
let directory = process.cwd();
|
|
72
|
+
while (true) {
|
|
73
|
+
const sourceRoot = path.join(directory, 'packages/core/src');
|
|
74
|
+
if (existsSync(path.join(sourceRoot, 'client.ts'))) {
|
|
75
|
+
return sourceRoot;
|
|
76
|
+
}
|
|
77
|
+
const parent = path.dirname(directory);
|
|
78
|
+
if (parent === directory) {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
directory = parent;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function resolveCoreModule(sourceRoot, fromFile, moduleSpecifier) {
|
|
85
|
+
const resolvedPath = path.resolve(path.dirname(fromFile), moduleSpecifier);
|
|
86
|
+
const candidates = [
|
|
87
|
+
resolvedPath,
|
|
88
|
+
`${resolvedPath}.ts`,
|
|
89
|
+
`${resolvedPath}.tsx`,
|
|
90
|
+
path.join(resolvedPath, 'index.ts'),
|
|
91
|
+
path.join(resolvedPath, 'index.tsx'),
|
|
92
|
+
];
|
|
93
|
+
return candidates.find((candidate) => candidate.startsWith(sourceRoot) && existsSync(candidate) && statSync(candidate).isFile());
|
|
94
|
+
}
|
|
95
|
+
function namesFromExportDeclaration(statement, sourceFile, sourceRoot, visited) {
|
|
96
|
+
const names = new Set();
|
|
97
|
+
if (!statement.moduleSpecifier || !ts.isStringLiteral(statement.moduleSpecifier)) {
|
|
98
|
+
if (statement.exportClause && ts.isNamedExports(statement.exportClause)) {
|
|
99
|
+
for (const specifier of statement.exportClause.elements) {
|
|
100
|
+
names.add(specifier.name.text);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return names;
|
|
104
|
+
}
|
|
105
|
+
const target = resolveCoreModule(sourceRoot, sourceFile, statement.moduleSpecifier.text);
|
|
106
|
+
if (!target) {
|
|
107
|
+
return names;
|
|
108
|
+
}
|
|
109
|
+
if (!statement.exportClause) {
|
|
110
|
+
return exportedNames(target, sourceRoot, visited);
|
|
111
|
+
}
|
|
112
|
+
if (ts.isNamedExports(statement.exportClause)) {
|
|
113
|
+
for (const specifier of statement.exportClause.elements) {
|
|
114
|
+
names.add(specifier.name.text);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
names.add(statement.exportClause.name.text);
|
|
119
|
+
}
|
|
120
|
+
return names;
|
|
121
|
+
}
|
|
122
|
+
function namesFromDeclaredExport(statement) {
|
|
123
|
+
const names = new Set();
|
|
124
|
+
const modifiers = ts.canHaveModifiers(statement) ? ts.getModifiers(statement) : undefined;
|
|
125
|
+
if (!modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)) {
|
|
126
|
+
return names;
|
|
127
|
+
}
|
|
128
|
+
if (modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword)) {
|
|
129
|
+
names.add('default');
|
|
130
|
+
}
|
|
131
|
+
else if (ts.isVariableStatement(statement)) {
|
|
132
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
133
|
+
if (ts.isIdentifier(declaration.name)) {
|
|
134
|
+
names.add(declaration.name.text);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
else if (ts.isFunctionDeclaration(statement) ||
|
|
139
|
+
ts.isClassDeclaration(statement) ||
|
|
140
|
+
ts.isInterfaceDeclaration(statement) ||
|
|
141
|
+
ts.isTypeAliasDeclaration(statement) ||
|
|
142
|
+
ts.isEnumDeclaration(statement)) {
|
|
143
|
+
if (statement.name) {
|
|
144
|
+
names.add(statement.name.text);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return names;
|
|
148
|
+
}
|
|
149
|
+
function exportedNames(sourceFile, sourceRoot, visited = new Set()) {
|
|
150
|
+
if (visited.has(sourceFile)) {
|
|
151
|
+
return new Set();
|
|
152
|
+
}
|
|
153
|
+
visited.add(sourceFile);
|
|
154
|
+
const source = readFileSync(sourceFile, 'utf8');
|
|
155
|
+
const parsed = ts.createSourceFile(sourceFile, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
156
|
+
const names = new Set();
|
|
157
|
+
for (const statement of parsed.statements) {
|
|
158
|
+
const statementNames = ts.isExportDeclaration(statement)
|
|
159
|
+
? namesFromExportDeclaration(statement, sourceFile, sourceRoot, visited)
|
|
160
|
+
: namesFromDeclaredExport(statement);
|
|
161
|
+
for (const name of statementNames) {
|
|
162
|
+
names.add(name);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return names;
|
|
166
|
+
}
|
|
167
|
+
function coreEntrypointNames(entrypoint, sourceRoot) {
|
|
168
|
+
const entryPath = entrypoint.replace('@ecopages/core/', '');
|
|
169
|
+
const entryFile = resolveCoreModule(sourceRoot, path.join(sourceRoot, '__entry__.ts'), `./${entryPath}`);
|
|
170
|
+
return entryFile ? exportedNames(entryFile, sourceRoot) : undefined;
|
|
171
|
+
}
|
|
172
|
+
function requestedNames(node) {
|
|
173
|
+
const declaration = node;
|
|
174
|
+
if (declaration.type === 'ExportAllDeclaration') {
|
|
175
|
+
return { reason: 'wildcard re-exports cannot be verified' };
|
|
176
|
+
}
|
|
177
|
+
const specifiers = declaration.specifiers ?? [];
|
|
178
|
+
if (specifiers.length === 0) {
|
|
179
|
+
return { reason: 'side-effect imports cannot be verified' };
|
|
180
|
+
}
|
|
181
|
+
const names = [];
|
|
182
|
+
for (const specifier of specifiers) {
|
|
183
|
+
if (specifier.type === 'ImportNamespaceSpecifier') {
|
|
184
|
+
return { reason: 'namespace imports cannot be verified' };
|
|
185
|
+
}
|
|
186
|
+
const imported = specifier.imported ??
|
|
187
|
+
(specifier.type === 'ExportSpecifier' ? specifier.local : undefined) ??
|
|
188
|
+
(specifier.type === 'ImportDefaultSpecifier' ? { name: 'default' } : undefined);
|
|
189
|
+
const name = imported?.name ?? imported?.value;
|
|
190
|
+
if (!name) {
|
|
191
|
+
return { reason: 'the imported name cannot be verified' };
|
|
192
|
+
}
|
|
193
|
+
names.push(name);
|
|
194
|
+
}
|
|
195
|
+
return { names };
|
|
196
|
+
}
|
|
197
|
+
function warnUnsupported(file, specifier, entrypoint, detail) {
|
|
198
|
+
console.warn(`[migrate-core-entrypoints] ${file.path}: left ${specifier} unchanged for ${entrypoint}; ${detail}. Review this import manually.`);
|
|
199
|
+
}
|
|
200
|
+
function literalValue(node) {
|
|
201
|
+
if (!node || typeof node !== 'object') {
|
|
202
|
+
return undefined;
|
|
203
|
+
}
|
|
204
|
+
const literal = node;
|
|
205
|
+
return (literal.type === 'StringLiteral' || literal.type === 'Literal') && typeof literal.value === 'string'
|
|
206
|
+
? literal.value
|
|
207
|
+
: undefined;
|
|
208
|
+
}
|
|
209
|
+
function rewriteLiteral(node) {
|
|
210
|
+
const value = literalValue(node);
|
|
211
|
+
const entrypoint = value ? entrypointFor(value) : undefined;
|
|
212
|
+
if (!entrypoint || !node || typeof node !== 'object') {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
node.value = entrypoint;
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
function warnUnverifiedDynamicAccess(node, file) {
|
|
219
|
+
const specifier = literalValue(node);
|
|
220
|
+
const entrypoint = specifier ? entrypointFor(specifier) : undefined;
|
|
221
|
+
if (specifier && entrypoint) {
|
|
222
|
+
warnUnsupported(file, specifier, entrypoint, 'dynamic module access cannot be verified');
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function isImportMetaResolve(callee) {
|
|
226
|
+
const call = callee;
|
|
227
|
+
return (call?.type === 'MemberExpression' &&
|
|
228
|
+
call.property?.name === 'resolve' &&
|
|
229
|
+
call.object?.type === 'MetaProperty' &&
|
|
230
|
+
call.object.property?.name === 'meta' &&
|
|
231
|
+
call.object.meta?.type === 'Identifier' &&
|
|
232
|
+
call.object.meta.name === 'import');
|
|
233
|
+
}
|
|
234
|
+
function isRequireResolve(callee) {
|
|
235
|
+
const call = callee;
|
|
236
|
+
return (call?.type === 'MemberExpression' &&
|
|
237
|
+
call.property?.name === 'resolve' &&
|
|
238
|
+
call.object?.type === 'Identifier' &&
|
|
239
|
+
call.object.name === 'require');
|
|
240
|
+
}
|
|
241
|
+
function rewriteModuleImports(root, j, file, sourceRoot) {
|
|
242
|
+
let changed = false;
|
|
243
|
+
const staticDeclarations = [
|
|
244
|
+
root.find(j.ImportDeclaration),
|
|
245
|
+
root.find(j.ExportNamedDeclaration),
|
|
246
|
+
root.find(j.ExportAllDeclaration),
|
|
247
|
+
];
|
|
248
|
+
for (const collection of staticDeclarations) {
|
|
249
|
+
collection.forEach((nodePath) => {
|
|
250
|
+
const declaration = nodePath.node;
|
|
251
|
+
const specifier = literalValue(declaration.source);
|
|
252
|
+
const entrypoint = specifier ? entrypointFor(specifier) : undefined;
|
|
253
|
+
if (!specifier || !entrypoint) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (!sourceRoot) {
|
|
257
|
+
warnUnsupported(file, specifier, entrypoint, 'the matching Ecopages source checkout was not found');
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const requested = requestedNames(nodePath.node);
|
|
261
|
+
if ('reason' in requested) {
|
|
262
|
+
warnUnsupported(file, specifier, entrypoint, requested.reason);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const availableNames = coreEntrypointNames(entrypoint, sourceRoot);
|
|
266
|
+
if (!availableNames) {
|
|
267
|
+
warnUnsupported(file, specifier, entrypoint, `the ${entrypoint} entry could not be inspected`);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const missing = requested.names.filter((name) => !availableNames.has(name));
|
|
271
|
+
if (missing.length > 0) {
|
|
272
|
+
warnUnsupported(file, specifier, entrypoint, `${missing.map((name) => `'${name}'`).join(', ')} ${missing.length === 1 ? 'is' : 'are'} not exported`);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
changed = rewriteLiteral(declaration.source) || changed;
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
root.find(j.CallExpression).forEach((path) => {
|
|
279
|
+
const call = path.node;
|
|
280
|
+
if (!call.arguments?.length) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (isImportMetaResolve(call.callee) || isRequireResolve(call.callee)) {
|
|
284
|
+
changed = rewriteLiteral(call.arguments[0]) || changed;
|
|
285
|
+
}
|
|
286
|
+
else if (call.callee?.type === 'Import' ||
|
|
287
|
+
(call.callee?.type === 'Identifier' && call.callee.name === 'require')) {
|
|
288
|
+
warnUnverifiedDynamicAccess(call.arguments[0], file);
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
root.find(j.ImportExpression).forEach((path) => {
|
|
292
|
+
warnUnverifiedDynamicAccess(path.node.source, file);
|
|
293
|
+
});
|
|
294
|
+
return changed;
|
|
295
|
+
}
|
|
296
|
+
export default function transformer(file, api, _options) {
|
|
297
|
+
const j = api.jscodeshift;
|
|
298
|
+
const root = j(file.source);
|
|
299
|
+
if (!rewriteModuleImports(root, j, file, findCoreSourceRoot())) {
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
return root.toSource(printOptions(file.source));
|
|
303
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codemod: migrate-data-hooks
|
|
3
|
+
*
|
|
4
|
+
* Renames the `eco.page()` data hooks and drops their envelopes.
|
|
5
|
+
*
|
|
6
|
+
* Before:
|
|
7
|
+
* eco.page({
|
|
8
|
+
* staticPaths: async () => ({ paths: [{ params: { slug: 'a' } }] }),
|
|
9
|
+
* staticProps: async ({ pathname: { params } }) => ({ props: { title: params.slug } }),
|
|
10
|
+
* render: ...
|
|
11
|
+
* })
|
|
12
|
+
*
|
|
13
|
+
* After:
|
|
14
|
+
* eco.page({
|
|
15
|
+
* paths: async () => [{ params: { slug: 'a' } }],
|
|
16
|
+
* props: async ({ params }) => ({ title: params.slug }),
|
|
17
|
+
* render: ...
|
|
18
|
+
* })
|
|
19
|
+
*
|
|
20
|
+
* A hook is rewritten only when every value it returns can be unwrapped: an object literal holding just the
|
|
21
|
+
* envelope key, or any expression inside an `async` function, which becomes `(await expr).props`. A hook it
|
|
22
|
+
* cannot rewrite is left unchanged and reported, so the old name still fails loudly with a pointer to the migration docs.
|
|
23
|
+
* The `GetStaticProps`, `GetStaticPaths` and `StaticPath` types are renamed to `GetProps`, `GetPaths` and `PagePath`.
|
|
24
|
+
*
|
|
25
|
+
* The props context passes `params` where it passed `pathname: { params }`, so in the `props` hook the first
|
|
26
|
+
* parameter is rewritten when it is destructured as `{ pathname: { params } }` or `{ pathname }`, or is a
|
|
27
|
+
* plain identifier read as `ctx.pathname.params`. A hook that reads `pathname` any other way, or declares a
|
|
28
|
+
* `params` binding already, keeps its `pathname` and is reported.
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* This file is deliberately self-contained, with no import beyond `jscodeshift` types, because
|
|
32
|
+
* `@ecopages/codemod` is not published: users run it from its raw GitHub URL with `npx jscodeshift`.
|
|
33
|
+
*/
|
|
34
|
+
import type { API, FileInfo, Options } from 'jscodeshift';
|
|
35
|
+
export declare const parser = "tsx";
|
|
36
|
+
export default function transform(file: FileInfo, api: API, _options: Options): string | null;
|