@astryxdesign/cli 0.7.5-canary.ae842c8 → 0.7.5-canary.b7ea471
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/component/_adapter.mjs +16 -1
- package/api/component/component.test.mjs +31 -0
- package/api/docs/_adapter.d.mts +5 -2
- package/api/docs/_adapter.mjs +24 -16
- package/api/docs/docs.test.mjs +18 -0
- package/api/docs/integrationDocs.test.mjs +2 -2
- package/api/integration/pack-check.lifecycle-output.test.mjs +24 -1
- package/api/integration/pack-check.mjs +10 -4
- package/api/integration/pack-check.test.mjs +5 -1
- package/api/search/_adapter.test.mjs +21 -2
- package/api/template/template.mjs +3 -1
- package/api/template/unknown-name.test.mjs +63 -0
- package/api/upgrade/_adapter.d.mts +2 -0
- package/api/upgrade/run/failure-receipt.test.mjs +212 -0
- package/api/upgrade/run/run.mjs +251 -10
- 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 +17 -4
- package/assets/codemods/__tests__/runner.symlinks.test.mjs +205 -0
- package/assets/codemods/__tests__/runner.unparseable.test.mjs +457 -0
- package/assets/codemods/integration-runner.mjs +8 -2
- package/assets/codemods/integration-runner.protection.test.mjs +53 -0
- package/assets/codemods/parse.mjs +94 -0
- package/assets/codemods/run-codemod.mjs +109 -5
- package/assets/codemods/runner.mjs +108 -10
- package/assets/codemods/transform-prop.mjs +99 -49
- package/assets/codemods/transform-prop.test.mjs +36 -1
- package/assets/codemods/transforms/v0.1.3/__tests__/migrate-layout-components-to-experimental.test.mjs +216 -2
- package/assets/codemods/transforms/v0.1.3/migrate-layout-components-to-experimental.mjs +88 -6
- package/assets/codemods/transforms/v0.6.0/migrate-astryx-theme-selectors-to-data-attrs.mjs +4 -3
- package/assets/codemods/transforms/v0.7.0/__tests__/migrate-deprecated-theme-surface.test.mjs +207 -6
- package/assets/codemods/transforms/v0.7.0/__tests__/validate-data-token-ownership.test.mjs +123 -70
- 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 +244 -42
- package/assets/codemods/transforms/v0.7.0/validate-data-token-ownership.mjs +19 -77
- package/assets/docs/tree/styling-advanced.doc.mjs +1 -1
- package/assets/templates/blocks/components/CodeBlock/CodeBlockBashCommand.doc.mjs +1 -1
- package/assets/templates/blocks/components/CodeBlock/CodeBlockBashCommand.tsx +10 -2
- package/assets/templates/pages/library/page.tsx +1 -2
- package/authoring/codemod/type.ts +10 -0
- package/clients/cli/commands/upgrade.doc.mjs +27 -0
- package/clients/cli/commands/upgrade.file-protection.test.mjs +175 -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/doc-compiler/doc-loads.test.mjs +5 -5
- package/foundation/doc-compiler/import.mjs +81 -8
- package/foundation/doc-compiler/read.d.mts +4 -3
- package/foundation/doc-compiler/read.mjs +15 -10
- package/foundation/doc-compiler/read.test.mjs +59 -1
- package/foundation/fs/module-loader.d.mts +3 -2
- package/foundation/fs/module-loader.mjs +3 -2
- package/foundation/fs/publish-file-eperm-mutation.test.mjs +2 -0
- package/foundation/fs/read-record.d.mts +27 -0
- package/foundation/fs/read-record.mjs +83 -15
- package/foundation/fs/read-record.test.mjs +88 -1
- 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/foundation/text/suggest-order.d.mts +17 -3
- package/foundation/text/suggest-order.mjs +58 -13
- package/foundation/text/suggest-order.test.mjs +53 -0
- package/package.json +10 -9
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
runCodeCodemod,
|
|
25
25
|
runConfigCodemod,
|
|
26
26
|
} from './run-codemod.mjs';
|
|
27
|
+
import {withoutProtected} from './runner.mjs';
|
|
27
28
|
import {createFileProtectionResolver} from '../../foundation/fs/file-protection.mjs';
|
|
28
29
|
|
|
29
30
|
/**
|
|
@@ -44,7 +45,7 @@ import {createFileProtectionResolver} from '../../foundation/fs/file-protection.
|
|
|
44
45
|
* @param {string} [options.root]
|
|
45
46
|
* @param {Map<string, string>} [options.contents] staged bytes from earlier dry-run phases
|
|
46
47
|
* @param {{root: string, classify: (file: string) => import('../../foundation/fs/file-protection.mjs').FileProtection[]}} [options.protection]
|
|
47
|
-
* @returns {{totalFilesChanged: number, totalTransformsApplied: number, changedFiles: string[], writtenFiles: string[], stagedContents: Map<string, string>, protectedFiles: import('../../authoring/codemod/type').CodemodRunResult['protectedFiles'], errors: Array<{file: string, codemod: string, error: string}>, skippedOptional: Array<import('../../authoring/codemod/type').CodemodEntry>}}
|
|
48
|
+
* @returns {{totalFilesChanged: number, totalTransformsApplied: number, changedFiles: string[], writtenFiles: string[], stagedContents: Map<string, string>, protectedFiles: import('../../authoring/codemod/type').CodemodRunResult['protectedFiles'], declinedCandidates: NonNullable<import('../../authoring/codemod/type').CodemodRunResult['declinedCandidates']>, errors: Array<{file: string, codemod: string, error: string}>, skippedOptional: Array<import('../../authoring/codemod/type').CodemodEntry>}}
|
|
48
49
|
*/
|
|
49
50
|
export function runIntegrationCodemods(
|
|
50
51
|
versionGroups,
|
|
@@ -74,6 +75,8 @@ export function runIntegrationCodemods(
|
|
|
74
75
|
const writtenFiles = [];
|
|
75
76
|
/** @type {import('../../authoring/codemod/type').CodemodRunResult['protectedFiles']} */
|
|
76
77
|
const protectedFiles = [];
|
|
78
|
+
/** @type {NonNullable<import('../../authoring/codemod/type').CodemodRunResult['declinedCandidates']>} */
|
|
79
|
+
const declinedCandidates = [];
|
|
77
80
|
/** @type {Array<{file: string, codemod: string, error: string}>} */
|
|
78
81
|
const errors = [];
|
|
79
82
|
/** @type {Array<import('../../authoring/codemod/type').CodemodEntry>} */
|
|
@@ -125,7 +128,7 @@ export function runIntegrationCodemods(
|
|
|
125
128
|
if (codeEntries.length > 0) {
|
|
126
129
|
const resolvedPath = path.resolve(srcPath);
|
|
127
130
|
const files = fs.existsSync(resolvedPath)
|
|
128
|
-
? findSourceFiles(resolvedPath)
|
|
131
|
+
? findSourceFiles(resolvedPath, path.resolve(root))
|
|
129
132
|
: [];
|
|
130
133
|
for (const entry of codeEntries) {
|
|
131
134
|
if (apply && writtenFiles.length !== protectionWriteCount) {
|
|
@@ -147,9 +150,11 @@ export function runIntegrationCodemods(
|
|
|
147
150
|
changedFiles.push(...r.changedFiles);
|
|
148
151
|
writtenFiles.push(...r.writtenFiles);
|
|
149
152
|
protectedFiles.push(...r.protectedFiles);
|
|
153
|
+
declinedCandidates.push(...(r.declinedCandidates ?? []));
|
|
150
154
|
errors.push(...r.errors);
|
|
151
155
|
}
|
|
152
156
|
}
|
|
157
|
+
const reportedDeclined = withoutProtected(declinedCandidates, protectedFiles);
|
|
153
158
|
|
|
154
159
|
// A file several codemods changed is one file; transforms count each change.
|
|
155
160
|
const totalFilesChanged = new Set(changedFiles).size;
|
|
@@ -161,6 +166,7 @@ export function runIntegrationCodemods(
|
|
|
161
166
|
writtenFiles,
|
|
162
167
|
stagedContents: virtualContents,
|
|
163
168
|
protectedFiles,
|
|
169
|
+
declinedCandidates: reportedDeclined,
|
|
164
170
|
errors,
|
|
165
171
|
skippedOptional,
|
|
166
172
|
};
|
|
@@ -150,4 +150,57 @@ describe('integration codemod file protection', () => {
|
|
|
150
150
|
}),
|
|
151
151
|
]);
|
|
152
152
|
});
|
|
153
|
+
|
|
154
|
+
it('declines a protected file that does not parse, as the core runner does', async () => {
|
|
155
|
+
write('.gitattributes', 'src/vendor/** linguist-vendored\n');
|
|
156
|
+
write('src/owned.js', "export const value = 'old';\n");
|
|
157
|
+
write('src/vendor/bundle.js', 'export const value = {;\n');
|
|
158
|
+
write('src/broken.js', 'export const value = {;\n');
|
|
159
|
+
|
|
160
|
+
const groups = [
|
|
161
|
+
{
|
|
162
|
+
version: '1.0.0',
|
|
163
|
+
codemods: [
|
|
164
|
+
{
|
|
165
|
+
id: 'code',
|
|
166
|
+
type: 'code',
|
|
167
|
+
package: '@acme/integration',
|
|
168
|
+
codemod: {
|
|
169
|
+
title: 'code',
|
|
170
|
+
fileExtensions: ['.js'],
|
|
171
|
+
transform: (file, {jscodeshift: j}) => {
|
|
172
|
+
const ast = j(file.source);
|
|
173
|
+
ast.find(j.Identifier, {name: 'value'}).forEach(p => {
|
|
174
|
+
p.node.name = 'renamed';
|
|
175
|
+
});
|
|
176
|
+
return ast.toSource();
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
],
|
|
181
|
+
},
|
|
182
|
+
];
|
|
183
|
+
const jscodeshift = (await import('jscodeshift')).default;
|
|
184
|
+
const result = runIntegrationCodemods(groups, {
|
|
185
|
+
apply: true,
|
|
186
|
+
path: path.join(root, 'src'),
|
|
187
|
+
root,
|
|
188
|
+
jscodeshift,
|
|
189
|
+
silent: true,
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
expect(fs.readFileSync(path.join(root, 'src/owned.js'), 'utf8')).toContain(
|
|
193
|
+
'renamed',
|
|
194
|
+
);
|
|
195
|
+
expect(result.declinedCandidates).toEqual([
|
|
196
|
+
expect.objectContaining({
|
|
197
|
+
file: path.join('src', 'vendor', 'bundle.js'),
|
|
198
|
+
codemod: '@acme/integration:code',
|
|
199
|
+
location: '1:23',
|
|
200
|
+
}),
|
|
201
|
+
]);
|
|
202
|
+
expect(result.errors).toEqual([
|
|
203
|
+
expect.objectContaining({file: path.join('src', 'broken.js')}),
|
|
204
|
+
]);
|
|
205
|
+
});
|
|
153
206
|
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Parse entry points that mark their own failures.
|
|
5
|
+
*
|
|
6
|
+
* @input Source text a codemod reads: a file's JavaScript or TypeScript, a
|
|
7
|
+
* stylesheet, or one CSS selector.
|
|
8
|
+
* @output The parsed tree; a parse failure is rethrown with
|
|
9
|
+
* `code: 'ERR_CODEMOD_PARSE'`.
|
|
10
|
+
* @position Shared by the codemod runner and the core CSS codemods. The runner
|
|
11
|
+
* skips a protected file only when its error carries this code, so any other
|
|
12
|
+
* error a codemod throws still fails the run.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import postcss from 'postcss';
|
|
16
|
+
import selectorParser from 'postcss-selector-parser';
|
|
17
|
+
|
|
18
|
+
export const CODEMOD_PARSE_ERROR = 'ERR_CODEMOD_PARSE';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Mark an error as a parse failure and return it.
|
|
22
|
+
* @template T
|
|
23
|
+
* @param {T} err
|
|
24
|
+
* @returns {T}
|
|
25
|
+
*/
|
|
26
|
+
export function markParseError(err) {
|
|
27
|
+
if (err && typeof err === 'object') {
|
|
28
|
+
/** @type {any} */ (err).code = CODEMOD_PARSE_ERROR;
|
|
29
|
+
}
|
|
30
|
+
return err;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {unknown} err
|
|
35
|
+
* @returns {boolean}
|
|
36
|
+
*/
|
|
37
|
+
export function isCodemodParseError(err) {
|
|
38
|
+
return /** @type {any} */ (err)?.code === CODEMOD_PARSE_ERROR;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* `postcss.parse`, with a failure marked as a parse error.
|
|
43
|
+
* @param {string} source
|
|
44
|
+
* @param {import('postcss').ProcessOptions} [options]
|
|
45
|
+
*/
|
|
46
|
+
export function parseCss(source, options) {
|
|
47
|
+
try {
|
|
48
|
+
return postcss.parse(source, options);
|
|
49
|
+
} catch (err) {
|
|
50
|
+
throw markParseError(err);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Parse one selector, hand its tree to `callback`, and return the tree as
|
|
56
|
+
* text, like `selectorParser(callback).processSync(selector, {lossless:
|
|
57
|
+
* true})`. Pass the PostCSS rule rather than its selector text when there is
|
|
58
|
+
* one: a failure then reports its line and column in the stylesheet. Only a
|
|
59
|
+
* failure to parse is marked; an error `callback` throws is passed through
|
|
60
|
+
* unchanged.
|
|
61
|
+
* @param {string | import('postcss').Rule} selector selector text, or the rule that holds it
|
|
62
|
+
* @param {(root: import('postcss-selector-parser').Root) => void} callback
|
|
63
|
+
* @returns {string}
|
|
64
|
+
*/
|
|
65
|
+
export function processSelector(selector, callback) {
|
|
66
|
+
let root;
|
|
67
|
+
try {
|
|
68
|
+
root = selectorParser().astSync(selector, {lossless: true});
|
|
69
|
+
} catch (err) {
|
|
70
|
+
throw markParseError(err);
|
|
71
|
+
}
|
|
72
|
+
callback(root);
|
|
73
|
+
return root.toString();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The jscodeshift a code codemod receives for one file. Parsing that file's
|
|
78
|
+
* own source (`j(file.source)`) marks a failure as a parse error; every other
|
|
79
|
+
* call and property behaves as jscodeshift does.
|
|
80
|
+
* @param {any} j jscodeshift bound to the file's parser
|
|
81
|
+
* @param {string} source the file's source
|
|
82
|
+
* @returns {any}
|
|
83
|
+
*/
|
|
84
|
+
export function markingSourceParseErrors(j, source) {
|
|
85
|
+
return new Proxy(j, {
|
|
86
|
+
apply(target, thisArg, args) {
|
|
87
|
+
try {
|
|
88
|
+
return Reflect.apply(target, thisArg, args);
|
|
89
|
+
} catch (err) {
|
|
90
|
+
throw args[0] === source ? markParseError(err) : err;
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|
|
@@ -35,8 +35,10 @@ 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';
|
|
41
|
+
import {isCodemodParseError, markingSourceParseErrors} from './parse.mjs';
|
|
40
42
|
|
|
41
43
|
export const DEFAULT_CODE_EXTENSIONS = [
|
|
42
44
|
'.tsx',
|
|
@@ -71,6 +73,69 @@ function protectedResult(filePath, name, protection) {
|
|
|
71
73
|
}));
|
|
72
74
|
}
|
|
73
75
|
|
|
76
|
+
/**
|
|
77
|
+
* @typedef {{file: string, codemod: string, location?: string, reason: string}} DeclinedCandidate
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Where a parse error points, as 1-based `line:column`, and its message
|
|
82
|
+
* without the position Babel appends. Babel reports a 0-based column on
|
|
83
|
+
* `loc`; PostCSS reports 1-based `line` and `column`.
|
|
84
|
+
* @param {any} err
|
|
85
|
+
* @returns {{message: string, location?: string}}
|
|
86
|
+
*/
|
|
87
|
+
function describeParseError(err) {
|
|
88
|
+
if (err?.name === 'CssSyntaxError') {
|
|
89
|
+
return {
|
|
90
|
+
message: String(err.reason ?? err.message),
|
|
91
|
+
...(typeof err.line === 'number'
|
|
92
|
+
? {location: `${err.line}:${err.column ?? 1}`}
|
|
93
|
+
: {}),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const loc = err?.loc;
|
|
97
|
+
return {
|
|
98
|
+
message: String(err?.message ?? err).replace(/\s*\(\d+:\d+\)$/, ''),
|
|
99
|
+
...(loc && typeof loc.line === 'number'
|
|
100
|
+
? {location: `${loc.line}:${(loc.column ?? 0) + 1}`}
|
|
101
|
+
: {}),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Decline a protected file a codemod could not parse. A codemod can prove
|
|
107
|
+
* nothing in bytes it cannot parse, and the file is never written anyway, so
|
|
108
|
+
* it is listed for manual review instead of failing the run. Only an error a
|
|
109
|
+
* parse entry point marked (see `parse.mjs`) counts: owned source that does
|
|
110
|
+
* not parse, and every other error a codemod throws, still fail.
|
|
111
|
+
* @param {unknown} err
|
|
112
|
+
* @param {string} filePath
|
|
113
|
+
* @param {string} relativePath
|
|
114
|
+
* @param {string} name
|
|
115
|
+
* @param {{root: string, classify: (file: string) => import('../../foundation/fs/file-protection.mjs').FileProtection[]}} protection
|
|
116
|
+
* @returns {DeclinedCandidate|null}
|
|
117
|
+
*/
|
|
118
|
+
function declineUnparseableProtected(
|
|
119
|
+
err,
|
|
120
|
+
filePath,
|
|
121
|
+
relativePath,
|
|
122
|
+
name,
|
|
123
|
+
protection,
|
|
124
|
+
) {
|
|
125
|
+
if (!isCodemodParseError(err)) return null;
|
|
126
|
+
const protections = protection.classify(filePath);
|
|
127
|
+
if (protections.length === 0) return null;
|
|
128
|
+
const {message, location} = describeParseError(err);
|
|
129
|
+
return {
|
|
130
|
+
file: relativePath,
|
|
131
|
+
codemod: name,
|
|
132
|
+
...(location ? {location} : {}),
|
|
133
|
+
// The category, not the declaration: a declaration names its source
|
|
134
|
+
// file from the project root, and `file` may be rebased elsewhere.
|
|
135
|
+
reason: `unparseable protected file (${[...new Set(protections.map(item => item.reason))].join(', ')}): ${message}`,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
74
139
|
/**
|
|
75
140
|
* Print one blocked candidate without implying that the protected bytes changed.
|
|
76
141
|
* @param {import('../../authoring/codemod/type').CliLog} log
|
|
@@ -86,9 +151,10 @@ function logProtected(log, protections) {
|
|
|
86
151
|
/**
|
|
87
152
|
* Recursively find candidate source files in a directory.
|
|
88
153
|
* @param {string} dir
|
|
154
|
+
* @param {string} [root] the operation's project root (default: `dir`)
|
|
89
155
|
* @returns {string[]}
|
|
90
156
|
*/
|
|
91
|
-
export function findSourceFiles(dir) {
|
|
157
|
+
export function findSourceFiles(dir, root = dir) {
|
|
92
158
|
/** @type {string[]} */
|
|
93
159
|
const results = [];
|
|
94
160
|
/** @param {string} currentDir @param {Set<string>} [ancestors] */
|
|
@@ -111,12 +177,24 @@ export function findSourceFiles(dir) {
|
|
|
111
177
|
for (const entry of entries) {
|
|
112
178
|
const fullPath = path.join(currentDir, entry.name);
|
|
113
179
|
// Symlinked files and descendants remain read-only candidates so a
|
|
114
|
-
// required change can be reported. Real-path ancestors prevent cycles
|
|
180
|
+
// required change can be reported. Real-path ancestors prevent cycles,
|
|
181
|
+
// and a link into a dependency or VCS tree is not descended.
|
|
115
182
|
if (entry.isSymbolicLink()) {
|
|
116
183
|
try {
|
|
117
184
|
const target = fs.statSync(fullPath);
|
|
118
185
|
if (target.isFile()) results.push(fullPath);
|
|
119
|
-
else if (
|
|
186
|
+
else if (
|
|
187
|
+
target.isDirectory() &&
|
|
188
|
+
!isIgnoredLinkedDirectory(
|
|
189
|
+
dir,
|
|
190
|
+
fullPath,
|
|
191
|
+
entry.name,
|
|
192
|
+
fs.realpathSync(fullPath),
|
|
193
|
+
root,
|
|
194
|
+
)
|
|
195
|
+
) {
|
|
196
|
+
walk(fullPath, nextAncestors);
|
|
197
|
+
}
|
|
120
198
|
} catch {
|
|
121
199
|
// Broken links have no source bytes to evaluate.
|
|
122
200
|
}
|
|
@@ -285,6 +363,8 @@ export function runCodeCodemod(
|
|
|
285
363
|
const protectedFiles = [];
|
|
286
364
|
/** @type {Array<{file: string, codemod: string, error: string}>} */
|
|
287
365
|
const errors = [];
|
|
366
|
+
/** @type {DeclinedCandidate[]} */
|
|
367
|
+
const declinedCandidates = [];
|
|
288
368
|
/** @type {Array<{filePath: string, relativePath: string, result: string}>} */
|
|
289
369
|
const candidates = [];
|
|
290
370
|
|
|
@@ -300,7 +380,11 @@ export function runCodeCodemod(
|
|
|
300
380
|
contents?.get(filePath) ?? fs.readFileSync(filePath, 'utf-8');
|
|
301
381
|
const parser = ext === '.tsx' || ext === '.ts' ? 'tsx' : 'babel';
|
|
302
382
|
const j = jscodeshift.withParser(parser);
|
|
303
|
-
const api = {
|
|
383
|
+
const api = {
|
|
384
|
+
jscodeshift: markingSourceParseErrors(j, source),
|
|
385
|
+
stats: () => {},
|
|
386
|
+
report: () => {},
|
|
387
|
+
};
|
|
304
388
|
let result = codemod.transform({source, path: filePath}, api);
|
|
305
389
|
|
|
306
390
|
if (result == null || result === source) continue;
|
|
@@ -320,6 +404,19 @@ export function runCodeCodemod(
|
|
|
320
404
|
}
|
|
321
405
|
candidates.push({filePath, relativePath, result});
|
|
322
406
|
} catch (err) {
|
|
407
|
+
const declined = declineUnparseableProtected(
|
|
408
|
+
err,
|
|
409
|
+
filePath,
|
|
410
|
+
relativePath,
|
|
411
|
+
name,
|
|
412
|
+
resolver,
|
|
413
|
+
);
|
|
414
|
+
if (declined) {
|
|
415
|
+
// Printed by the caller once every codemod has run: a later codemod
|
|
416
|
+
// can still make this file a protected one, listed only as such.
|
|
417
|
+
declinedCandidates.push(declined);
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
323
420
|
const message = /** @type {any} */ (err).message;
|
|
324
421
|
log.error(` ✗ ${relativePath} — ${message}`);
|
|
325
422
|
errors.push({file: relativePath, codemod: name, error: message});
|
|
@@ -372,5 +469,12 @@ export function runCodeCodemod(
|
|
|
372
469
|
}
|
|
373
470
|
}
|
|
374
471
|
|
|
375
|
-
return {
|
|
472
|
+
return {
|
|
473
|
+
filesChanged,
|
|
474
|
+
changedFiles,
|
|
475
|
+
writtenFiles,
|
|
476
|
+
protectedFiles,
|
|
477
|
+
declinedCandidates,
|
|
478
|
+
errors,
|
|
479
|
+
};
|
|
376
480
|
}
|
|
@@ -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 {
|
|
@@ -142,6 +215,22 @@ function findSourceFiles(dir) {
|
|
|
142
215
|
return results.sort();
|
|
143
216
|
}
|
|
144
217
|
|
|
218
|
+
/**
|
|
219
|
+
* The declined candidates that are not also protected files the run reports.
|
|
220
|
+
* A file another codemod still requires a change to is listed once, under
|
|
221
|
+
* `protectedFiles`, which already makes the run incomplete.
|
|
222
|
+
* @template {{file: string}} T
|
|
223
|
+
* @param {T[]} declined
|
|
224
|
+
* @param {Array<{file: string}>} protectedRows
|
|
225
|
+
* @returns {T[]}
|
|
226
|
+
*/
|
|
227
|
+
export function withoutProtected(declined, protectedRows) {
|
|
228
|
+
/** @param {string} file */
|
|
229
|
+
const key = file => file.split(path.sep).join('/');
|
|
230
|
+
const blocked = new Set(protectedRows.map(item => key(item.file)));
|
|
231
|
+
return declined.filter(item => !blocked.has(key(item.file)));
|
|
232
|
+
}
|
|
233
|
+
|
|
145
234
|
/**
|
|
146
235
|
* Validate transform output before writing to disk.
|
|
147
236
|
*
|
|
@@ -419,7 +508,7 @@ async function runProjectCodemod(
|
|
|
419
508
|
* @param {string} [options.root] - Project root a project codemod reads (default: the process cwd)
|
|
420
509
|
* @param {{root: string, classify: (file: string) => import('../../foundation/fs/file-protection.mjs').FileProtection[]}} [options.protection] - Preloaded protection resolver
|
|
421
510
|
* @param {boolean} [options.coversTarget] - False when these codemods stop before the installed target (the CLI is older than Core), so a run with no changes does not say the code is up to date. Default true.
|
|
422
|
-
* @returns {Promise<{totalFilesChanged: number, totalTransformsApplied: number, totalValidationBlocked: number, changedFiles: string[], writtenFiles: string[], stagedContents: Map<string, string>, protectedFiles: import('../../authoring/codemod/type').CodemodRunResult['protectedFiles'], errors: Array<{file: string, codemod: string, error: string}>, skippedOptional: Array<{name: string, meta: {title: string, description?: string, fileExtensions?: string[], codemodType?: string}, version: string}>} | {ok: false, reason: string, resolvedPath: string}>}
|
|
511
|
+
* @returns {Promise<{totalFilesChanged: number, totalTransformsApplied: number, totalValidationBlocked: number, changedFiles: string[], writtenFiles: string[], stagedContents: Map<string, string>, protectedFiles: import('../../authoring/codemod/type').CodemodRunResult['protectedFiles'], declinedCandidates: NonNullable<import('../../authoring/codemod/type').CodemodRunResult['declinedCandidates']>, errors: Array<{file: string, codemod: string, error: string}>, skippedOptional: Array<{name: string, meta: {title: string, description?: string, fileExtensions?: string[], codemodType?: string}, version: string}>} | {ok: false, reason: string, resolvedPath: string}>}
|
|
423
512
|
*/
|
|
424
513
|
export async function runCodemods(
|
|
425
514
|
versionManifests,
|
|
@@ -472,7 +561,7 @@ export async function runCodemods(
|
|
|
472
561
|
let files = [];
|
|
473
562
|
if (sourcePathExists) {
|
|
474
563
|
log.step(`Scanning ${resolvedPath} for source files...`);
|
|
475
|
-
files = findSourceFiles(resolvedPath);
|
|
564
|
+
files = findSourceFiles(resolvedPath, path.resolve(root));
|
|
476
565
|
if (files.length === 0) {
|
|
477
566
|
log.warn('No source files found.');
|
|
478
567
|
} else {
|
|
@@ -500,6 +589,8 @@ export async function runCodemods(
|
|
|
500
589
|
const writtenFiles = [];
|
|
501
590
|
/** @type {import('../../authoring/codemod/type').CodemodRunResult['protectedFiles']} */
|
|
502
591
|
const protectedFiles = [];
|
|
592
|
+
/** @type {NonNullable<import('../../authoring/codemod/type').CodemodRunResult['declinedCandidates']>} */
|
|
593
|
+
const declinedCandidates = [];
|
|
503
594
|
/** @type {Array<{name: string, meta: {title: string, description?: string, fileExtensions?: string[], codemodType?: string}, version: string}>} */
|
|
504
595
|
const skippedOptional = [];
|
|
505
596
|
for (const {version, transforms} of versionManifests) {
|
|
@@ -589,6 +680,7 @@ export async function runCodemods(
|
|
|
589
680
|
);
|
|
590
681
|
errors.push(...result.errors);
|
|
591
682
|
protectedFiles.push(...result.protectedFiles);
|
|
683
|
+
declinedCandidates.push(...(result.declinedCandidates ?? []));
|
|
592
684
|
changedFiles.push(...result.changedFiles);
|
|
593
685
|
writtenFiles.push(...result.writtenFiles);
|
|
594
686
|
totalTransformsApplied += result.filesChanged;
|
|
@@ -640,10 +732,15 @@ export async function runCodemods(
|
|
|
640
732
|
);
|
|
641
733
|
}
|
|
642
734
|
|
|
735
|
+
// A later codemod can block a file an earlier one declined. The caller
|
|
736
|
+
// prints declined files once it has reconciled every runner's results.
|
|
737
|
+
const reportedDeclined = withoutProtected(declinedCandidates, protectedFiles);
|
|
738
|
+
|
|
643
739
|
if (
|
|
644
740
|
totalFilesChanged === 0 &&
|
|
645
741
|
errors.length === 0 &&
|
|
646
|
-
protectedFiles.length === 0
|
|
742
|
+
protectedFiles.length === 0 &&
|
|
743
|
+
reportedDeclined.length === 0
|
|
647
744
|
) {
|
|
648
745
|
// Codemods that stop before the installed target cannot vouch for the
|
|
649
746
|
// code; the caller says what is missing.
|
|
@@ -693,6 +790,7 @@ export async function runCodemods(
|
|
|
693
790
|
changedFiles,
|
|
694
791
|
writtenFiles,
|
|
695
792
|
protectedFiles,
|
|
793
|
+
declinedCandidates: reportedDeclined,
|
|
696
794
|
stagedContents: virtualContents,
|
|
697
795
|
errors,
|
|
698
796
|
skippedOptional,
|