@astryxdesign/cli 0.7.5-canary.0026f37 → 0.7.5-canary.04434e7
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 +1 -1
- package/api/upgrade/run/failure-receipt.test.mjs +155 -0
- package/api/upgrade/run/run.mjs +170 -8
- package/api/upgrade/upgrade.doc.mjs +4 -4
- package/api/upgrade/upgrade.mjs +19 -4
- package/api/upgrade/upgrade.type.d.mts +12 -1
- package/api/upgrade/upgrade.type.mjs +16 -3
- package/assets/codemods/__tests__/runner.symlinks.test.mjs +205 -0
- package/assets/codemods/integration-runner.mjs +1 -1
- package/assets/codemods/run-codemod.mjs +17 -3
- package/assets/codemods/runner.mjs +81 -8
- package/assets/codemods/transform-prop.mjs +99 -49
- package/assets/codemods/transform-prop.test.mjs +36 -1
- package/assets/codemods/transforms/v0.7.0/__tests__/migrate-deprecated-theme-surface.test.mjs +207 -6
- package/assets/codemods/transforms/v0.7.0/migrate-deprecated-theme-surface-v0.6-data.mjs +6 -1
- package/assets/codemods/transforms/v0.7.0/migrate-deprecated-theme-surface.mjs +200 -32
- package/assets/docs/tree/styling-advanced.doc.mjs +1 -1
- package/clients/cli/commands/upgrade.doc.mjs +17 -0
- package/clients/cli/commands/upgrade.file-protection.test.mjs +29 -0
- package/clients/cli/commands/upgrade.flag-help.test.mjs +37 -0
- package/clients/cli/commands/upgrade.integration-policy.test.mjs +103 -0
- package/clients/cli/commands/upgrade.mjs +9 -2
- package/foundation/response/base.d.ts +7 -0
- package/foundation/response/error-codes.doc.mjs +3 -2
- package/foundation/response/error-codes.mjs +1 -1
- package/foundation/response/json.d.mts +10 -3
- package/foundation/response/json.mjs +16 -4
- package/package.json +9 -9
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file The codemod walkers never follow a symbolic link into installed
|
|
5
|
+
* dependencies, a package store or version-control metadata; other linked
|
|
6
|
+
* folders are still scanned, read-only.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
|
|
10
|
+
import * as fs from 'node:fs';
|
|
11
|
+
import * as path from 'node:path';
|
|
12
|
+
import {runCodemods} from '../runner.mjs';
|
|
13
|
+
import {findSourceFiles as findIntegrationSourceFiles} from '../run-codemod.mjs';
|
|
14
|
+
|
|
15
|
+
let tmpDir;
|
|
16
|
+
let originalCwd;
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
originalCwd = process.cwd();
|
|
20
|
+
tmpDir = fs.mkdtempSync(path.join(process.cwd(), '.astryx-runner-test-'));
|
|
21
|
+
process.chdir(tmpDir);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
process.chdir(originalCwd);
|
|
26
|
+
fs.rmSync(tmpDir, {recursive: true, force: true});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('findSourceFiles — symbolic links into dependency trees', () => {
|
|
30
|
+
const manifests = [
|
|
31
|
+
{
|
|
32
|
+
version: '0.1.3',
|
|
33
|
+
transforms: [
|
|
34
|
+
{
|
|
35
|
+
name: 'p',
|
|
36
|
+
meta: {title: 'p'},
|
|
37
|
+
transform: f => f.source.replace(/foo/g, 'bar'),
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
// A tree a symlinked dependency folder points at: a package store outside
|
|
44
|
+
// the project, holding a file the transform would change.
|
|
45
|
+
function seedStore() {
|
|
46
|
+
const store = path.join(tmpDir, 'store', 'node_modules', 'pkg');
|
|
47
|
+
fs.mkdirSync(store, {recursive: true});
|
|
48
|
+
fs.writeFileSync(path.join(store, 'index.ts'), 'const foo = 1;\n');
|
|
49
|
+
fs.mkdirSync(path.join(tmpDir, 'src'), {recursive: true});
|
|
50
|
+
fs.writeFileSync(path.join(tmpDir, 'src', 'real.ts'), 'const foo = 2;\n');
|
|
51
|
+
return store;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
it('does not descend a symlinked node_modules', async () => {
|
|
55
|
+
const store = seedStore();
|
|
56
|
+
fs.symlinkSync(
|
|
57
|
+
path.dirname(store),
|
|
58
|
+
path.join(tmpDir, 'src', 'node_modules'),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const r = await runCodemods(manifests, {
|
|
62
|
+
apply: true,
|
|
63
|
+
path: './src',
|
|
64
|
+
root: tmpDir,
|
|
65
|
+
silent: true,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
expect(r.writtenFiles.map(f => path.basename(f))).toEqual(['real.ts']);
|
|
69
|
+
expect(r.protectedFiles).toEqual([]);
|
|
70
|
+
expect(
|
|
71
|
+
findIntegrationSourceFiles(path.join(tmpDir, 'src')).map(f =>
|
|
72
|
+
path.relative(tmpDir, f),
|
|
73
|
+
),
|
|
74
|
+
).toEqual([path.join('src', 'real.ts')]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('does not descend a link whose target is inside a dependency tree', async () => {
|
|
78
|
+
const store = seedStore();
|
|
79
|
+
fs.symlinkSync(store, path.join(tmpDir, 'src', 'pkg'));
|
|
80
|
+
|
|
81
|
+
const r = await runCodemods(manifests, {
|
|
82
|
+
apply: true,
|
|
83
|
+
path: './src',
|
|
84
|
+
root: tmpDir,
|
|
85
|
+
silent: true,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
expect(r.writtenFiles.map(f => path.basename(f))).toEqual(['real.ts']);
|
|
89
|
+
expect(r.protectedFiles).toEqual([]);
|
|
90
|
+
expect(
|
|
91
|
+
findIntegrationSourceFiles(path.join(tmpDir, 'src')).map(f =>
|
|
92
|
+
path.relative(tmpDir, f),
|
|
93
|
+
),
|
|
94
|
+
).toEqual([path.join('src', 'real.ts')]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('does not descend a link into a Yarn cache folder', async () => {
|
|
98
|
+
const cached = path.join(tmpDir, '.yarn', 'cache', 'pkg');
|
|
99
|
+
fs.mkdirSync(cached, {recursive: true});
|
|
100
|
+
fs.writeFileSync(path.join(cached, 'index.ts'), 'const foo = 1;\n');
|
|
101
|
+
fs.mkdirSync(path.join(tmpDir, 'src'), {recursive: true});
|
|
102
|
+
fs.writeFileSync(path.join(tmpDir, 'src', 'real.ts'), 'const foo = 2;\n');
|
|
103
|
+
fs.symlinkSync(cached, path.join(tmpDir, 'src', 'pkg'));
|
|
104
|
+
|
|
105
|
+
const r = await runCodemods(manifests, {
|
|
106
|
+
apply: true,
|
|
107
|
+
path: '.',
|
|
108
|
+
root: tmpDir,
|
|
109
|
+
silent: true,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
expect(r.writtenFiles.map(f => path.basename(f))).toEqual(['real.ts']);
|
|
113
|
+
expect(r.protectedFiles).toEqual([]);
|
|
114
|
+
expect(
|
|
115
|
+
findIntegrationSourceFiles(tmpDir).map(f => path.relative(tmpDir, f)),
|
|
116
|
+
).toEqual([path.join('src', 'real.ts')]);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('still descends ordinary symlinked directories in a project below a node_modules folder', async () => {
|
|
120
|
+
const project = path.join(tmpDir, 'node_modules', 'consumer');
|
|
121
|
+
const shared = path.join(project, 'shared');
|
|
122
|
+
fs.mkdirSync(path.join(project, 'src'), {recursive: true});
|
|
123
|
+
fs.mkdirSync(shared, {recursive: true});
|
|
124
|
+
fs.writeFileSync(path.join(shared, 'nested.ts'), 'const foo = 1;\n');
|
|
125
|
+
fs.symlinkSync(shared, path.join(project, 'src', 'linked'));
|
|
126
|
+
|
|
127
|
+
const r = await runCodemods(manifests, {
|
|
128
|
+
apply: true,
|
|
129
|
+
path: path.join(project, 'src'),
|
|
130
|
+
root: project,
|
|
131
|
+
silent: true,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
expect(r.protectedFiles).toEqual([
|
|
135
|
+
expect.objectContaining({
|
|
136
|
+
file: 'src/linked/nested.ts',
|
|
137
|
+
reason: 'symlink',
|
|
138
|
+
}),
|
|
139
|
+
]);
|
|
140
|
+
expect(
|
|
141
|
+
findIntegrationSourceFiles(path.join(project, 'src'), project).map(f =>
|
|
142
|
+
path.relative(project, f),
|
|
143
|
+
),
|
|
144
|
+
).toEqual([path.join('src', 'linked', 'nested.ts')]);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('does not descend a link into a Yarn cache outside the project', async () => {
|
|
148
|
+
const project = path.join(tmpDir, 'project');
|
|
149
|
+
const cached = path.join(tmpDir, 'other-project', '.yarn', 'cache', 'pkg');
|
|
150
|
+
fs.mkdirSync(cached, {recursive: true});
|
|
151
|
+
fs.writeFileSync(path.join(cached, 'index.ts'), 'const foo = 1;\n');
|
|
152
|
+
fs.mkdirSync(path.join(project, 'src'), {recursive: true});
|
|
153
|
+
fs.writeFileSync(path.join(project, 'src', 'real.ts'), 'const foo = 2;\n');
|
|
154
|
+
fs.symlinkSync(
|
|
155
|
+
path.join('..', '..', 'other-project', '.yarn', 'cache', 'pkg'),
|
|
156
|
+
path.join(project, 'src', 'pkg'),
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
const r = await runCodemods(manifests, {
|
|
160
|
+
apply: true,
|
|
161
|
+
path: path.join(project, 'src'),
|
|
162
|
+
root: project,
|
|
163
|
+
silent: true,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
expect(r.writtenFiles.map(f => path.basename(f))).toEqual(['real.ts']);
|
|
167
|
+
expect(r.protectedFiles).toEqual([]);
|
|
168
|
+
expect(
|
|
169
|
+
findIntegrationSourceFiles(path.join(project, 'src')).map(f =>
|
|
170
|
+
path.relative(project, f),
|
|
171
|
+
),
|
|
172
|
+
).toEqual([path.join('src', 'real.ts')]);
|
|
173
|
+
expect(fs.readFileSync(path.join(cached, 'index.ts'), 'utf-8')).toBe(
|
|
174
|
+
'const foo = 1;\n',
|
|
175
|
+
);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('does not descend a link from a project below node_modules to a sibling dependency', async () => {
|
|
179
|
+
const project = path.join(tmpDir, 'node_modules', 'consumer');
|
|
180
|
+
const sibling = path.join(tmpDir, 'node_modules', 'dep');
|
|
181
|
+
fs.mkdirSync(path.join(project, 'src'), {recursive: true});
|
|
182
|
+
fs.mkdirSync(sibling, {recursive: true});
|
|
183
|
+
fs.writeFileSync(path.join(sibling, 'index.ts'), 'const foo = 1;\n');
|
|
184
|
+
fs.writeFileSync(path.join(project, 'src', 'real.ts'), 'const foo = 2;\n');
|
|
185
|
+
fs.symlinkSync(sibling, path.join(project, 'src', 'dep'));
|
|
186
|
+
|
|
187
|
+
const r = await runCodemods(manifests, {
|
|
188
|
+
apply: true,
|
|
189
|
+
path: path.join(project, 'src'),
|
|
190
|
+
root: project,
|
|
191
|
+
silent: true,
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
expect(r.writtenFiles.map(f => path.basename(f))).toEqual(['real.ts']);
|
|
195
|
+
expect(r.protectedFiles).toEqual([]);
|
|
196
|
+
expect(
|
|
197
|
+
findIntegrationSourceFiles(path.join(project, 'src'), project).map(f =>
|
|
198
|
+
path.relative(project, f),
|
|
199
|
+
),
|
|
200
|
+
).toEqual([path.join('src', 'real.ts')]);
|
|
201
|
+
expect(fs.readFileSync(path.join(sibling, 'index.ts'), 'utf-8')).toBe(
|
|
202
|
+
'const foo = 1;\n',
|
|
203
|
+
);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
@@ -125,7 +125,7 @@ export function runIntegrationCodemods(
|
|
|
125
125
|
if (codeEntries.length > 0) {
|
|
126
126
|
const resolvedPath = path.resolve(srcPath);
|
|
127
127
|
const files = fs.existsSync(resolvedPath)
|
|
128
|
-
? findSourceFiles(resolvedPath)
|
|
128
|
+
? findSourceFiles(resolvedPath, path.resolve(root))
|
|
129
129
|
: [];
|
|
130
130
|
for (const entry of codeEntries) {
|
|
131
131
|
if (apply && writtenFiles.length !== protectionWriteCount) {
|
|
@@ -35,6 +35,7 @@ import {
|
|
|
35
35
|
fixDirectiveCorruption,
|
|
36
36
|
validateOutput,
|
|
37
37
|
isIgnoredDirectory,
|
|
38
|
+
isIgnoredLinkedDirectory,
|
|
38
39
|
} from './runner.mjs';
|
|
39
40
|
import {createFileProtectionResolver} from '../../foundation/fs/file-protection.mjs';
|
|
40
41
|
|
|
@@ -86,9 +87,10 @@ function logProtected(log, protections) {
|
|
|
86
87
|
/**
|
|
87
88
|
* Recursively find candidate source files in a directory.
|
|
88
89
|
* @param {string} dir
|
|
90
|
+
* @param {string} [root] the operation's project root (default: `dir`)
|
|
89
91
|
* @returns {string[]}
|
|
90
92
|
*/
|
|
91
|
-
export function findSourceFiles(dir) {
|
|
93
|
+
export function findSourceFiles(dir, root = dir) {
|
|
92
94
|
/** @type {string[]} */
|
|
93
95
|
const results = [];
|
|
94
96
|
/** @param {string} currentDir @param {Set<string>} [ancestors] */
|
|
@@ -111,12 +113,24 @@ export function findSourceFiles(dir) {
|
|
|
111
113
|
for (const entry of entries) {
|
|
112
114
|
const fullPath = path.join(currentDir, entry.name);
|
|
113
115
|
// Symlinked files and descendants remain read-only candidates so a
|
|
114
|
-
// required change can be reported. Real-path ancestors prevent cycles
|
|
116
|
+
// required change can be reported. Real-path ancestors prevent cycles,
|
|
117
|
+
// and a link into a dependency or VCS tree is not descended.
|
|
115
118
|
if (entry.isSymbolicLink()) {
|
|
116
119
|
try {
|
|
117
120
|
const target = fs.statSync(fullPath);
|
|
118
121
|
if (target.isFile()) results.push(fullPath);
|
|
119
|
-
else if (
|
|
122
|
+
else if (
|
|
123
|
+
target.isDirectory() &&
|
|
124
|
+
!isIgnoredLinkedDirectory(
|
|
125
|
+
dir,
|
|
126
|
+
fullPath,
|
|
127
|
+
entry.name,
|
|
128
|
+
fs.realpathSync(fullPath),
|
|
129
|
+
root,
|
|
130
|
+
)
|
|
131
|
+
) {
|
|
132
|
+
walk(fullPath, nextAncestors);
|
|
133
|
+
}
|
|
120
134
|
} catch {
|
|
121
135
|
// Broken links have no source bytes to evaluate.
|
|
122
136
|
}
|
|
@@ -61,22 +61,85 @@ const IGNORED_DIRS = new Set([
|
|
|
61
61
|
]);
|
|
62
62
|
export {IGNORED_DIRS};
|
|
63
63
|
|
|
64
|
+
/** Yarn's install and cache folders, a hard boundary under `.yarn`. */
|
|
65
|
+
const YARN_STORE_DIRS = new Set(['__virtual__', 'cache', 'sdks', 'unplugged']);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The number of path segments a hard boundary starting at `parts[index]`
|
|
69
|
+
* spans: 1 for a name in {@link IGNORED_DIRS}, 2 for `.yarn` followed by one
|
|
70
|
+
* of its store folders, 0 when none starts there.
|
|
71
|
+
* @param {string[]} parts
|
|
72
|
+
* @param {number} index
|
|
73
|
+
*/
|
|
74
|
+
function boundaryLength(parts, index) {
|
|
75
|
+
if (IGNORED_DIRS.has(parts[index])) return 1;
|
|
76
|
+
if (parts[index] === '.yarn' && YARN_STORE_DIRS.has(parts[index + 1])) {
|
|
77
|
+
return 2;
|
|
78
|
+
}
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
|
|
64
82
|
/** @param {string} root @param {string} fullPath @param {string} name */
|
|
65
83
|
export function isIgnoredDirectory(root, fullPath, name) {
|
|
66
84
|
if (IGNORED_DIRS.has(name)) return true;
|
|
67
85
|
const parts = path.relative(root, fullPath).split(path.sep);
|
|
68
|
-
return (
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
86
|
+
return boundaryLength(parts, 0) === 2;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Whether a symbolic link to a directory leads into a tree the scan never
|
|
91
|
+
* descends. The link is checked like a directory by {@link isIgnoredDirectory},
|
|
92
|
+
* and its resolved target's absolute path is searched for the same hard
|
|
93
|
+
* boundaries, wherever the target lives: a symlinked `node_modules`, or a link
|
|
94
|
+
* into a dependency, package-store or version-control tree such as another
|
|
95
|
+
* project's `.yarn/cache`, is not walked. Only a target that stays inside the
|
|
96
|
+
* operation root may sit below a boundary that also contains that root, so a
|
|
97
|
+
* project that itself lives below a `node_modules` folder is still scanned; a
|
|
98
|
+
* link from it to a sibling package under that same folder is not.
|
|
99
|
+
* @param {string} root scan root
|
|
100
|
+
* @param {string} fullPath the link's path
|
|
101
|
+
* @param {string} name the link's name
|
|
102
|
+
* @param {string} realTarget the link's resolved real path
|
|
103
|
+
* @param {string} [operationRoot] the project root of the operation (default: the scan root)
|
|
104
|
+
* @returns {boolean}
|
|
105
|
+
*/
|
|
106
|
+
export function isIgnoredLinkedDirectory(
|
|
107
|
+
root,
|
|
108
|
+
fullPath,
|
|
109
|
+
name,
|
|
110
|
+
realTarget,
|
|
111
|
+
operationRoot = root,
|
|
112
|
+
) {
|
|
113
|
+
if (isIgnoredDirectory(root, fullPath, name)) return true;
|
|
114
|
+
let realRoot = operationRoot;
|
|
115
|
+
try {
|
|
116
|
+
realRoot = fs.realpathSync(operationRoot);
|
|
117
|
+
} catch {
|
|
118
|
+
// An unreadable root has no descendants to compare against.
|
|
119
|
+
}
|
|
120
|
+
const relative = path.relative(realRoot, realTarget);
|
|
121
|
+
const inside =
|
|
122
|
+
relative === '' ||
|
|
123
|
+
(relative !== '..' &&
|
|
124
|
+
!relative.startsWith(`..${path.sep}`) &&
|
|
125
|
+
!path.isAbsolute(relative));
|
|
126
|
+
// Inside the root, only a boundary that ends below the root counts.
|
|
127
|
+
const floor = inside ? realRoot.split(path.sep).length : 0;
|
|
128
|
+
const targetParts = realTarget.split(path.sep);
|
|
129
|
+
for (let index = 0; index < targetParts.length; index++) {
|
|
130
|
+
const length = boundaryLength(targetParts, index);
|
|
131
|
+
if (length > 0 && index + length > floor) return true;
|
|
132
|
+
}
|
|
133
|
+
return false;
|
|
72
134
|
}
|
|
73
135
|
|
|
74
136
|
/**
|
|
75
137
|
* Recursively find all source files in a directory.
|
|
76
138
|
* @param {string} dir
|
|
139
|
+
* @param {string} [root] the operation's project root (default: `dir`)
|
|
77
140
|
* @returns {string[]}
|
|
78
141
|
*/
|
|
79
|
-
function findSourceFiles(dir) {
|
|
142
|
+
function findSourceFiles(dir, root = dir) {
|
|
80
143
|
/** @type {string[]} */
|
|
81
144
|
const results = [];
|
|
82
145
|
const extensions = new Set([
|
|
@@ -112,13 +175,23 @@ function findSourceFiles(dir) {
|
|
|
112
175
|
for (const entry of entries) {
|
|
113
176
|
const fullPath = path.join(currentDir, entry.name);
|
|
114
177
|
// Symlinked files and descendants remain read-only candidates so a
|
|
115
|
-
// required change can be reported. Real-path ancestors prevent cycles
|
|
178
|
+
// required change can be reported. Real-path ancestors prevent cycles,
|
|
179
|
+
// and a link into a dependency or VCS tree is not descended.
|
|
116
180
|
if (entry.isSymbolicLink()) {
|
|
117
181
|
try {
|
|
118
182
|
const target = fs.statSync(fullPath);
|
|
119
183
|
if (target.isFile() && extensions.has(path.extname(entry.name))) {
|
|
120
184
|
results.push(fullPath);
|
|
121
|
-
} else if (
|
|
185
|
+
} else if (
|
|
186
|
+
target.isDirectory() &&
|
|
187
|
+
!isIgnoredLinkedDirectory(
|
|
188
|
+
dir,
|
|
189
|
+
fullPath,
|
|
190
|
+
entry.name,
|
|
191
|
+
fs.realpathSync(fullPath),
|
|
192
|
+
root,
|
|
193
|
+
)
|
|
194
|
+
) {
|
|
122
195
|
walk(fullPath, nextAncestors);
|
|
123
196
|
}
|
|
124
197
|
} catch {
|
|
@@ -472,7 +545,7 @@ export async function runCodemods(
|
|
|
472
545
|
let files = [];
|
|
473
546
|
if (sourcePathExists) {
|
|
474
547
|
log.step(`Scanning ${resolvedPath} for source files...`);
|
|
475
|
-
files = findSourceFiles(resolvedPath);
|
|
548
|
+
files = findSourceFiles(resolvedPath, path.resolve(root));
|
|
476
549
|
if (files.length === 0) {
|
|
477
550
|
log.warn('No source files found.');
|
|
478
551
|
} else {
|
|
@@ -1,27 +1,47 @@
|
|
|
1
1
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Find imported components, resolve their local
|
|
5
|
-
* and yield matching JSX prop paths to a
|
|
4
|
+
* Find imported components, resolve their local names (including aliases and
|
|
5
|
+
* namespace members) at a use site, and yield matching JSX prop paths to a
|
|
6
|
+
* visitor.
|
|
6
7
|
*
|
|
7
|
-
*
|
|
8
|
-
* variables, helper calls, object spreads, wrapper components, or types.
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
* `transformProp` intentionally stops at the JSX boundary. It does not follow
|
|
9
|
+
* variables, helper calls, object spreads, wrapper components, or types. The
|
|
10
|
+
* resolver it is built on (`importedComponents`, `resolveImportedComponent`,
|
|
11
|
+
* `isModuleBinding`) is exported for transforms that prove other use sites.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @typedef {{
|
|
13
16
|
* matchesImport: (source: string) => boolean,
|
|
14
17
|
* components: ReadonlyMap<string, ReadonlySet<string>>,
|
|
15
18
|
* normalizeComponentName?: (name: string) => string,
|
|
16
|
-
* }}
|
|
17
|
-
* @param {(propPath: any, context: {component: string, localName: string}) => void} visit
|
|
19
|
+
* }} ImportOptions
|
|
18
20
|
*/
|
|
19
|
-
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @typedef {{
|
|
24
|
+
* direct: Map<string, {component: string, props: ReadonlySet<string>}>,
|
|
25
|
+
* namespaces: Set<string>,
|
|
26
|
+
* normalize: (name: string) => string,
|
|
27
|
+
* components: ReadonlyMap<string, ReadonlySet<string>>,
|
|
28
|
+
* }} ImportedComponents
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Local names bound by accepted runtime imports: named, aliased and default
|
|
33
|
+
* component imports, and namespace imports.
|
|
34
|
+
* @param {any} root jscodeshift collection for one source file
|
|
35
|
+
* @param {any} j configured jscodeshift instance
|
|
36
|
+
* @param {ImportOptions} options
|
|
37
|
+
* @returns {ImportedComponents}
|
|
38
|
+
*/
|
|
39
|
+
export function importedComponents(root, j, options) {
|
|
20
40
|
const normalize = options.normalizeComponentName ?? (name => name);
|
|
21
|
-
/** @type {
|
|
22
|
-
const
|
|
41
|
+
/** @type {ImportedComponents['direct']} */
|
|
42
|
+
const direct = new Map();
|
|
23
43
|
/** @type {Set<string>} */
|
|
24
|
-
const
|
|
44
|
+
const namespaces = new Set();
|
|
25
45
|
|
|
26
46
|
root.find(j.ImportDeclaration).forEach((/** @type {any} */ importPath) => {
|
|
27
47
|
const source = importPath.node.source.value;
|
|
@@ -46,53 +66,83 @@ export function transformProp(root, j, options, visit) {
|
|
|
46
66
|
const component = normalize(importedName);
|
|
47
67
|
const props = options.components.get(component);
|
|
48
68
|
if (!props) continue;
|
|
49
|
-
|
|
50
|
-
component,
|
|
51
|
-
props,
|
|
52
|
-
});
|
|
69
|
+
direct.set(specifier.local?.name ?? importedName, {component, props});
|
|
53
70
|
} else if (specifier.type === 'ImportDefaultSpecifier' && subpathProps) {
|
|
54
|
-
|
|
71
|
+
direct.set(specifier.local.name, {
|
|
55
72
|
component: subpathName,
|
|
56
73
|
props: subpathProps,
|
|
57
74
|
});
|
|
58
75
|
} else if (specifier.type === 'ImportNamespaceSpecifier') {
|
|
59
|
-
|
|
76
|
+
namespaces.add(specifier.local.name);
|
|
60
77
|
}
|
|
61
78
|
}
|
|
62
79
|
});
|
|
63
80
|
|
|
64
|
-
|
|
81
|
+
return {direct, namespaces, normalize, components: options.components};
|
|
82
|
+
}
|
|
65
83
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
84
|
+
/**
|
|
85
|
+
* True when `localName` at `path` resolves to a module-scope binding. A
|
|
86
|
+
* parameter or local declaration with the same name shadows the import.
|
|
87
|
+
* @param {any} path any node path inside the file
|
|
88
|
+
* @param {string} localName
|
|
89
|
+
*/
|
|
90
|
+
export function isModuleBinding(path, localName) {
|
|
91
|
+
return path.scope?.lookup(localName)?.path.node.type === 'Program';
|
|
92
|
+
}
|
|
71
93
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Resolve a component reference (`Card`, `Lib.Card`, or their JSX forms) to
|
|
96
|
+
* the imported component it names at `scopePath`, or null.
|
|
97
|
+
* @param {any} nameNode Identifier, JSXIdentifier, MemberExpression, or JSXMemberExpression
|
|
98
|
+
* @param {any} scopePath the path whose scope the reference is evaluated in
|
|
99
|
+
* @param {ImportedComponents} imports
|
|
100
|
+
* @returns {{component: string, props: ReadonlySet<string>, localName: string} | null}
|
|
101
|
+
*/
|
|
102
|
+
export function resolveImportedComponent(nameNode, scopePath, imports) {
|
|
103
|
+
let localName;
|
|
104
|
+
let binding;
|
|
105
|
+
if (nameNode?.type === 'JSXIdentifier' || nameNode?.type === 'Identifier') {
|
|
106
|
+
localName = nameNode.name;
|
|
107
|
+
binding = imports.direct.get(localName);
|
|
108
|
+
} else if (
|
|
109
|
+
(nameNode?.type === 'JSXMemberExpression' ||
|
|
110
|
+
(nameNode?.type === 'MemberExpression' && !nameNode.computed)) &&
|
|
111
|
+
(nameNode.object?.type === 'JSXIdentifier' ||
|
|
112
|
+
nameNode.object?.type === 'Identifier') &&
|
|
113
|
+
(nameNode.property?.type === 'JSXIdentifier' ||
|
|
114
|
+
nameNode.property?.type === 'Identifier') &&
|
|
115
|
+
imports.namespaces.has(nameNode.object.name)
|
|
116
|
+
) {
|
|
117
|
+
localName = nameNode.object.name;
|
|
118
|
+
const component = imports.normalize(nameNode.property.name);
|
|
119
|
+
const props = imports.components.get(component);
|
|
120
|
+
if (props) binding = {component, props};
|
|
121
|
+
}
|
|
122
|
+
if (!binding || !isModuleBinding(scopePath, localName)) return null;
|
|
123
|
+
return {...binding, localName};
|
|
124
|
+
}
|
|
90
125
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
126
|
+
/**
|
|
127
|
+
* @param {any} root jscodeshift collection for one source file
|
|
128
|
+
* @param {any} j configured jscodeshift instance
|
|
129
|
+
* @param {ImportOptions} options
|
|
130
|
+
* @param {(propPath: any, context: {component: string, localName: string}) => void} visit
|
|
131
|
+
*/
|
|
132
|
+
export function transformProp(root, j, options, visit) {
|
|
133
|
+
const imports = importedComponents(root, j, options);
|
|
134
|
+
if (imports.direct.size === 0 && imports.namespaces.size === 0) return;
|
|
135
|
+
|
|
136
|
+
root.find(j.JSXOpeningElement).forEach((/** @type {any} */ jsxPath) => {
|
|
137
|
+
// Only JSX tags whose root binding resolves to Program are usages of the
|
|
138
|
+
// import discovered above.
|
|
139
|
+
const resolved = resolveImportedComponent(
|
|
140
|
+
jsxPath.node.name,
|
|
141
|
+
jsxPath,
|
|
142
|
+
imports,
|
|
143
|
+
);
|
|
144
|
+
if (!resolved) return;
|
|
145
|
+
const {component, props, localName} = resolved;
|
|
96
146
|
|
|
97
147
|
for (let index = 0; index < jsxPath.node.attributes.length; index++) {
|
|
98
148
|
const attribute = jsxPath.node.attributes[index];
|
|
@@ -2,7 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
import {describe, expect, it} from 'vitest';
|
|
4
4
|
import jscodeshift from 'jscodeshift';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
importedComponents,
|
|
7
|
+
resolveImportedComponent,
|
|
8
|
+
transformProp,
|
|
9
|
+
} from './transform-prop.mjs';
|
|
6
10
|
|
|
7
11
|
const j = jscodeshift.withParser('tsx');
|
|
8
12
|
const components = new Map([
|
|
@@ -92,4 +96,35 @@ const b = <Group tone="old" size="old" />;`);
|
|
|
92
96
|
expect(output).toContain('<Widget size="old" migratedtone="old"');
|
|
93
97
|
expect(output).toContain('<Group tone="old" migratedsize="old"');
|
|
94
98
|
});
|
|
99
|
+
|
|
100
|
+
it('resolves non-JSX references through the same import and shadow rules', () => {
|
|
101
|
+
const root = j(`import {Widget as Card} from '@example/core';
|
|
102
|
+
import * as Lib from '@example/core';
|
|
103
|
+
const a = Card;
|
|
104
|
+
const b = Lib.Group;
|
|
105
|
+
function shadow(Card, Lib) {
|
|
106
|
+
return [Card, Lib.Group];
|
|
107
|
+
}`);
|
|
108
|
+
const imports = importedComponents(root, j, {
|
|
109
|
+
matchesImport: value => /^@example\/core(?:\/|$)/.test(value),
|
|
110
|
+
components,
|
|
111
|
+
});
|
|
112
|
+
const resolved = root
|
|
113
|
+
.find(j.VariableDeclarator)
|
|
114
|
+
.paths()
|
|
115
|
+
.map(
|
|
116
|
+
path =>
|
|
117
|
+
resolveImportedComponent(path.node.init, path, imports)?.component,
|
|
118
|
+
);
|
|
119
|
+
expect(resolved).toEqual(['Widget', 'Group']);
|
|
120
|
+
const shadowed = root
|
|
121
|
+
.find(j.ArrayExpression)
|
|
122
|
+
.paths()
|
|
123
|
+
.flatMap(path =>
|
|
124
|
+
path.node.elements.map(element =>
|
|
125
|
+
resolveImportedComponent(element, path, imports),
|
|
126
|
+
),
|
|
127
|
+
);
|
|
128
|
+
expect(shadowed).toEqual([null, null]);
|
|
129
|
+
});
|
|
95
130
|
});
|