@astryxdesign/cli 0.7.5-canary.04434e7 → 0.7.5-canary.07030cb
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/template/tokenizer-showcase.test.mjs +47 -0
- package/api/theme/build/build.adaptation-enrollment.test.mjs +4 -0
- package/api/theme/build/build.icon-variants.test.mjs +105 -0
- package/api/theme/build/build.mjs +36 -7
- package/api/theme/build/build.test.mjs +27 -0
- package/api/upgrade/_adapter.d.mts +47 -2
- package/api/upgrade/_adapter.mjs +58 -2
- package/api/upgrade/run/failure-receipt.test.mjs +57 -0
- package/api/upgrade/run/outcomes.test.mjs +646 -0
- package/api/upgrade/run/run.d.mts +36 -0
- package/api/upgrade/run/run.mjs +509 -45
- package/api/upgrade/upgrade.doc.mjs +2 -2
- package/api/upgrade/upgrade.type.d.mts +24 -0
- package/api/upgrade/upgrade.type.mjs +15 -3
- package/assets/codemods/__tests__/runner.unparseable.test.mjs +457 -0
- package/assets/codemods/integration-runner.mjs +7 -1
- package/assets/codemods/integration-runner.protection.test.mjs +53 -0
- package/assets/codemods/outcome.mjs +79 -0
- package/assets/codemods/outcome.test.mjs +40 -0
- package/assets/codemods/parse.mjs +94 -0
- package/assets/codemods/run-codemod.mjs +92 -2
- package/assets/codemods/runner.mjs +38 -5
- package/assets/codemods/transforms/next/__tests__/migrate-codeblock-tokenizer-to-language.test.mjs +130 -0
- package/assets/codemods/transforms/next/index.mjs +11 -1
- package/assets/codemods/transforms/next/migrate-codeblock-tokenizer-to-language.mjs +315 -0
- 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__/validate-data-token-ownership.test.mjs +24 -4
- package/assets/codemods/transforms/v0.7.0/migrate-deprecated-theme-surface.mjs +44 -10
- package/assets/codemods/transforms/v0.7.0/validate-data-token-ownership.mjs +6 -3
- package/assets/docs/author-a-theme.doc.mjs +3 -1
- package/assets/docs/icons.doc.mjs +71 -1
- package/assets/docs/theme.doc.dense.mjs +8 -0
- package/assets/docs/theme.doc.mjs +16 -0
- package/assets/docs/theme.doc.zh.mjs +11 -0
- package/assets/docs/tree/template-styles.doc.mjs +1 -1
- package/assets/docs/tree/tokens-color.doc.mjs +145 -1
- package/assets/docs/tree/tokens.doc.mjs +1 -1
- package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherReviewFlow.doc.mjs +6 -4
- package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherReviewFlow.tsx +61 -55
- package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherShowcase.doc.mjs +6 -4
- package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherShowcase.tsx +68 -37
- package/assets/templates/blocks/components/CodeBlock/CodeBlockBashCommand.doc.mjs +1 -1
- package/assets/templates/blocks/components/CodeBlock/CodeBlockBashCommand.tsx +10 -2
- package/assets/templates/blocks/components/Thumbnail/ThumbnailDisabled.doc.mjs +1 -1
- package/assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx +13 -6
- package/assets/templates/pages/dashboard-alert-rail/page.tsx +3 -3
- package/assets/templates/pages/library/page.tsx +1 -2
- package/assets/templates/pages/table-comparison/page.tsx +12 -3
- package/assets/templates/themes/butter/butterPaletteRefs.generated.ts +3 -2
- package/assets/templates/themes/butter/butterTheme.ts +47 -44
- package/assets/templates/themes/chocolate/chocolateTheme.ts +15 -2
- package/assets/templates/themes/gothic/gothicTheme.ts +21 -16
- package/assets/templates/themes/matcha/matchaPaletteRefs.generated.ts +1 -1
- package/assets/templates/themes/matcha/matchaTheme.ts +15 -2
- package/assets/templates/themes/neutral/neutralPaletteRefs.generated.ts +4 -4
- package/assets/templates/themes/neutral/neutralTheme.ts +24 -119
- package/assets/templates/themes/stone/stoneTheme.ts +46 -21
- package/assets/templates/themes/y2k/y2kTheme.ts +20 -16
- package/assets/theme.template.ts +18 -5
- package/authoring/codemod/type.ts +10 -0
- package/clients/cli/commands/build-theme.adaptations.test.mjs +110 -0
- package/clients/cli/commands/upgrade.doc.mjs +30 -2
- package/clients/cli/commands/upgrade.file-protection.test.mjs +146 -0
- package/clients/cli/commands/upgrade.outcomes.test.mjs +350 -0
- package/foundation/agent-docs/agent-docs.mjs +1 -0
- package/foundation/agent-docs/agent-docs.test.mjs +5 -0
- package/foundation/response/response-types.doc.mjs +1 -1
- package/package.json +10 -9
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Codemod outcomes that are not failures.
|
|
5
|
+
*
|
|
6
|
+
* A core transform throws one of these, instead of a plain Error, when a file
|
|
7
|
+
* needs a change the codemod cannot make itself:
|
|
8
|
+
*
|
|
9
|
+
* - `ManualMigrationRequired`: a person must migrate the code (the codemod can
|
|
10
|
+
* identify it but has no safe rewrite). Reported in the upgrade receipt's
|
|
11
|
+
* `declinedCandidates`; it does not fail the run.
|
|
12
|
+
* - `RegenerationRequired`: a generated file is out of date and its generator
|
|
13
|
+
* must rebuild it. Reported in `regenerate` with the command that does; the
|
|
14
|
+
* run is incomplete until it is rebuilt.
|
|
15
|
+
*
|
|
16
|
+
* Anything else a transform throws is still a codemod failure. The upgrade
|
|
17
|
+
* leaf (`api/upgrade/_adapter.mjs` `withCodemodOutcomes`) catches these around
|
|
18
|
+
* each core transform, so the codemod runners never see them.
|
|
19
|
+
*
|
|
20
|
+
* @input A message, and the source line or regeneration commands
|
|
21
|
+
* @output Error subclasses tagged with a stable outcome kind
|
|
22
|
+
* @position Shared by core codemod transforms and the upgrade leaf
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const OUTCOME = Symbol.for('astryx.codemod.outcome');
|
|
26
|
+
|
|
27
|
+
/** A person must migrate this code; the codemod has no safe rewrite. */
|
|
28
|
+
export class ManualMigrationRequired extends Error {
|
|
29
|
+
/**
|
|
30
|
+
* @param {string} message What to change, and how.
|
|
31
|
+
* @param {{line?: number | null}} [options] 1-based source line, when known.
|
|
32
|
+
*/
|
|
33
|
+
constructor(message, {line} = {}) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = 'ManualMigrationRequired';
|
|
36
|
+
/** @type {number | undefined} */
|
|
37
|
+
this.line = typeof line === 'number' ? line : undefined;
|
|
38
|
+
Object.defineProperty(this, OUTCOME, {value: 'manual'});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A generated file is out of date; its generator must rebuild it. */
|
|
43
|
+
export class RegenerationRequired extends Error {
|
|
44
|
+
/**
|
|
45
|
+
* @param {string} message Why the file is out of date.
|
|
46
|
+
* @param {{commands: string[]}} options Commands that rebuild it.
|
|
47
|
+
*/
|
|
48
|
+
constructor(message, {commands}) {
|
|
49
|
+
super(message);
|
|
50
|
+
this.name = 'RegenerationRequired';
|
|
51
|
+
/** @type {string[]} */
|
|
52
|
+
this.commands = [...commands];
|
|
53
|
+
Object.defineProperty(this, OUTCOME, {value: 'regenerate'});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The outcome a thrown value reports, or null for an ordinary failure.
|
|
59
|
+
* @param {unknown} err
|
|
60
|
+
* @returns {{kind: 'manual', message: string, line?: number} | {kind: 'regenerate', message: string, commands: string[]} | null}
|
|
61
|
+
*/
|
|
62
|
+
export function codemodOutcome(err) {
|
|
63
|
+
if (!err || typeof err !== 'object') return null;
|
|
64
|
+
const kind = /** @type {any} */ (err)[OUTCOME];
|
|
65
|
+
const message = /** @type {Error} */ (err).message;
|
|
66
|
+
if (kind === 'manual') {
|
|
67
|
+
const line = /** @type {ManualMigrationRequired} */ (err).line;
|
|
68
|
+
return {kind, message, ...(typeof line === 'number' ? {line} : {})};
|
|
69
|
+
}
|
|
70
|
+
if (kind === 'regenerate') {
|
|
71
|
+
const commands = /** @type {RegenerationRequired} */ (err).commands;
|
|
72
|
+
return {
|
|
73
|
+
kind,
|
|
74
|
+
message,
|
|
75
|
+
commands: Array.isArray(commands) ? [...commands] : [],
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
import {describe, expect, it} from 'vitest';
|
|
4
|
+
import {
|
|
5
|
+
ManualMigrationRequired,
|
|
6
|
+
RegenerationRequired,
|
|
7
|
+
codemodOutcome,
|
|
8
|
+
} from './outcome.mjs';
|
|
9
|
+
|
|
10
|
+
describe('codemodOutcome', () => {
|
|
11
|
+
it('reads a manual migration with and without its line', () => {
|
|
12
|
+
expect(codemodOutcome(new ManualMigrationRequired('m', {line: 3}))).toEqual(
|
|
13
|
+
{kind: 'manual', message: 'm', line: 3},
|
|
14
|
+
);
|
|
15
|
+
expect(codemodOutcome(new ManualMigrationRequired('m'))).toEqual({
|
|
16
|
+
kind: 'manual',
|
|
17
|
+
message: 'm',
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('reads a regeneration with its commands', () => {
|
|
22
|
+
expect(
|
|
23
|
+
codemodOutcome(new RegenerationRequired('r', {commands: ['gen']})),
|
|
24
|
+
).toEqual({kind: 'regenerate', message: 'r', commands: ['gen']});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('treats anything else as an ordinary failure', () => {
|
|
28
|
+
expect(codemodOutcome(new Error('boom'))).toBeNull();
|
|
29
|
+
expect(
|
|
30
|
+
codemodOutcome(Object.assign(new Error('x'), {kind: 'manual'})),
|
|
31
|
+
).toBeNull();
|
|
32
|
+
expect(codemodOutcome('boom')).toBeNull();
|
|
33
|
+
expect(codemodOutcome(undefined)).toBeNull();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('stays an Error, so a runner without the wrapper still reports it', () => {
|
|
37
|
+
expect(new ManualMigrationRequired('m')).toBeInstanceOf(Error);
|
|
38
|
+
expect(new RegenerationRequired('r', {commands: []})).toBeInstanceOf(Error);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -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
|
+
}
|
|
@@ -38,6 +38,7 @@ import {
|
|
|
38
38
|
isIgnoredLinkedDirectory,
|
|
39
39
|
} from './runner.mjs';
|
|
40
40
|
import {createFileProtectionResolver} from '../../foundation/fs/file-protection.mjs';
|
|
41
|
+
import {isCodemodParseError, markingSourceParseErrors} from './parse.mjs';
|
|
41
42
|
|
|
42
43
|
export const DEFAULT_CODE_EXTENSIONS = [
|
|
43
44
|
'.tsx',
|
|
@@ -72,6 +73,69 @@ function protectedResult(filePath, name, protection) {
|
|
|
72
73
|
}));
|
|
73
74
|
}
|
|
74
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
|
+
|
|
75
139
|
/**
|
|
76
140
|
* Print one blocked candidate without implying that the protected bytes changed.
|
|
77
141
|
* @param {import('../../authoring/codemod/type').CliLog} log
|
|
@@ -299,6 +363,8 @@ export function runCodeCodemod(
|
|
|
299
363
|
const protectedFiles = [];
|
|
300
364
|
/** @type {Array<{file: string, codemod: string, error: string}>} */
|
|
301
365
|
const errors = [];
|
|
366
|
+
/** @type {DeclinedCandidate[]} */
|
|
367
|
+
const declinedCandidates = [];
|
|
302
368
|
/** @type {Array<{filePath: string, relativePath: string, result: string}>} */
|
|
303
369
|
const candidates = [];
|
|
304
370
|
|
|
@@ -314,7 +380,11 @@ export function runCodeCodemod(
|
|
|
314
380
|
contents?.get(filePath) ?? fs.readFileSync(filePath, 'utf-8');
|
|
315
381
|
const parser = ext === '.tsx' || ext === '.ts' ? 'tsx' : 'babel';
|
|
316
382
|
const j = jscodeshift.withParser(parser);
|
|
317
|
-
const api = {
|
|
383
|
+
const api = {
|
|
384
|
+
jscodeshift: markingSourceParseErrors(j, source),
|
|
385
|
+
stats: () => {},
|
|
386
|
+
report: () => {},
|
|
387
|
+
};
|
|
318
388
|
let result = codemod.transform({source, path: filePath}, api);
|
|
319
389
|
|
|
320
390
|
if (result == null || result === source) continue;
|
|
@@ -334,6 +404,19 @@ export function runCodeCodemod(
|
|
|
334
404
|
}
|
|
335
405
|
candidates.push({filePath, relativePath, result});
|
|
336
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
|
+
}
|
|
337
420
|
const message = /** @type {any} */ (err).message;
|
|
338
421
|
log.error(` ✗ ${relativePath} — ${message}`);
|
|
339
422
|
errors.push({file: relativePath, codemod: name, error: message});
|
|
@@ -386,5 +469,12 @@ export function runCodeCodemod(
|
|
|
386
469
|
}
|
|
387
470
|
}
|
|
388
471
|
|
|
389
|
-
return {
|
|
472
|
+
return {
|
|
473
|
+
filesChanged,
|
|
474
|
+
changedFiles,
|
|
475
|
+
writtenFiles,
|
|
476
|
+
protectedFiles,
|
|
477
|
+
declinedCandidates,
|
|
478
|
+
errors,
|
|
479
|
+
};
|
|
390
480
|
}
|
|
@@ -215,6 +215,22 @@ function findSourceFiles(dir, root = dir) {
|
|
|
215
215
|
return results.sort();
|
|
216
216
|
}
|
|
217
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
|
+
|
|
218
234
|
/**
|
|
219
235
|
* Validate transform output before writing to disk.
|
|
220
236
|
*
|
|
@@ -492,7 +508,8 @@ async function runProjectCodemod(
|
|
|
492
508
|
* @param {string} [options.root] - Project root a project codemod reads (default: the process cwd)
|
|
493
509
|
* @param {{root: string, classify: (file: string) => import('../../foundation/fs/file-protection.mjs').FileProtection[]}} [options.protection] - Preloaded protection resolver
|
|
494
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.
|
|
495
|
-
* @
|
|
511
|
+
* @param {unknown[]} [options.outcomes] - Findings the transforms reported instead of changing a file (manual migrations, files to regenerate), filled while the run goes. A run with any does not say the code is up to date; the caller lists them.
|
|
512
|
+
* @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}>}
|
|
496
513
|
*/
|
|
497
514
|
export async function runCodemods(
|
|
498
515
|
versionManifests,
|
|
@@ -505,6 +522,7 @@ export async function runCodemods(
|
|
|
505
522
|
root = process.cwd(),
|
|
506
523
|
protection: providedProtection,
|
|
507
524
|
coversTarget = true,
|
|
525
|
+
outcomes,
|
|
508
526
|
},
|
|
509
527
|
) {
|
|
510
528
|
// No-op stub object so silent mode skips log output entirely without
|
|
@@ -573,6 +591,8 @@ export async function runCodemods(
|
|
|
573
591
|
const writtenFiles = [];
|
|
574
592
|
/** @type {import('../../authoring/codemod/type').CodemodRunResult['protectedFiles']} */
|
|
575
593
|
const protectedFiles = [];
|
|
594
|
+
/** @type {NonNullable<import('../../authoring/codemod/type').CodemodRunResult['declinedCandidates']>} */
|
|
595
|
+
const declinedCandidates = [];
|
|
576
596
|
/** @type {Array<{name: string, meta: {title: string, description?: string, fileExtensions?: string[], codemodType?: string}, version: string}>} */
|
|
577
597
|
const skippedOptional = [];
|
|
578
598
|
for (const {version, transforms} of versionManifests) {
|
|
@@ -662,6 +682,7 @@ export async function runCodemods(
|
|
|
662
682
|
);
|
|
663
683
|
errors.push(...result.errors);
|
|
664
684
|
protectedFiles.push(...result.protectedFiles);
|
|
685
|
+
declinedCandidates.push(...(result.declinedCandidates ?? []));
|
|
665
686
|
changedFiles.push(...result.changedFiles);
|
|
666
687
|
writtenFiles.push(...result.writtenFiles);
|
|
667
688
|
totalTransformsApplied += result.filesChanged;
|
|
@@ -713,19 +734,30 @@ export async function runCodemods(
|
|
|
713
734
|
);
|
|
714
735
|
}
|
|
715
736
|
|
|
737
|
+
// A later codemod can block a file an earlier one declined. The caller
|
|
738
|
+
// prints declined files once it has reconciled every runner's results.
|
|
739
|
+
const reportedDeclined = withoutProtected(declinedCandidates, protectedFiles);
|
|
740
|
+
// A generated file a transform reported out of date keeps the run from
|
|
741
|
+
// looking done, as a blocked protected file does.
|
|
742
|
+
const pendingRegeneration = (outcomes ?? []).some(
|
|
743
|
+
outcome =>
|
|
744
|
+
/** @type {{kind?: string} | null} */ (outcome)?.kind === 'regenerate',
|
|
745
|
+
);
|
|
746
|
+
|
|
716
747
|
if (
|
|
717
748
|
totalFilesChanged === 0 &&
|
|
718
749
|
errors.length === 0 &&
|
|
719
|
-
protectedFiles.length === 0
|
|
750
|
+
protectedFiles.length === 0 &&
|
|
751
|
+
reportedDeclined.length === 0
|
|
720
752
|
) {
|
|
721
753
|
// Codemods that stop before the installed target cannot vouch for the
|
|
722
754
|
// code; the caller says what is missing.
|
|
723
|
-
if (coversTarget) {
|
|
755
|
+
if (coversTarget && !(outcomes && outcomes.length > 0)) {
|
|
724
756
|
log.success('No changes needed — your code is already up to date!');
|
|
725
757
|
}
|
|
726
|
-
} else if (apply && protectedFiles.length > 0) {
|
|
758
|
+
} else if (apply && (protectedFiles.length > 0 || pendingRegeneration)) {
|
|
727
759
|
log.warn(
|
|
728
|
-
`Applied ${totalTransformsApplied} owned change${totalTransformsApplied === 1 ? '' : 's'} across ${totalFilesChanged} file${totalFilesChanged === 1 ? '' : 's'}; protected changes remain.`,
|
|
760
|
+
`Applied ${totalTransformsApplied} owned change${totalTransformsApplied === 1 ? '' : 's'} across ${totalFilesChanged} file${totalFilesChanged === 1 ? '' : 's'}; ${protectedFiles.length > 0 ? 'protected changes remain' : 'generated files to regenerate remain'}.`,
|
|
729
761
|
);
|
|
730
762
|
} else if (apply) {
|
|
731
763
|
log.success(
|
|
@@ -766,6 +798,7 @@ export async function runCodemods(
|
|
|
766
798
|
changedFiles,
|
|
767
799
|
writtenFiles,
|
|
768
800
|
protectedFiles,
|
|
801
|
+
declinedCandidates: reportedDeclined,
|
|
769
802
|
stagedContents: virtualContents,
|
|
770
803
|
errors,
|
|
771
804
|
skippedOptional,
|
package/assets/codemods/transforms/next/__tests__/migrate-codeblock-tokenizer-to-language.test.mjs
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
import {describe, expect, it} from 'vitest';
|
|
4
|
+
import transform from '../migrate-codeblock-tokenizer-to-language.mjs';
|
|
5
|
+
|
|
6
|
+
async function applyTransform(source) {
|
|
7
|
+
const jscodeshift = (await import('jscodeshift')).default;
|
|
8
|
+
const j = jscodeshift.withParser('tsx');
|
|
9
|
+
const api = {jscodeshift: j, stats: () => {}, report: () => {}};
|
|
10
|
+
return transform({source, path: 'test.tsx'}, api) ?? source;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Collapse whitespace so assertions ignore the printer's line breaks. */
|
|
14
|
+
const flat = text => text.replace(/\s+/g, ' ');
|
|
15
|
+
|
|
16
|
+
describe('migrate-codeblock-tokenizer-to-language', () => {
|
|
17
|
+
it('rewrites tokenizer beside a static language to a custom language', async () => {
|
|
18
|
+
const output = flat(
|
|
19
|
+
await applyTransform(`import {CodeBlock} from '@astryxdesign/core/CodeBlock';
|
|
20
|
+
const a = <CodeBlock code={src} language="zig" tokenizer={zigTokens} />;
|
|
21
|
+
const b = <CodeBlock code={src} language={'nim'} tokenizer={(c) => tokens(c)} />;`),
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
expect(output).not.toContain('tokenizer=');
|
|
25
|
+
expect(output).toContain(
|
|
26
|
+
"import { CodeBlock, createCodeLanguage } from '@astryxdesign/core/CodeBlock';",
|
|
27
|
+
);
|
|
28
|
+
expect(output).toContain(
|
|
29
|
+
"language={zigTokens ? createCodeLanguage({ name: 'zig', identifiers: ['zig'], tokenize: zigTokens }) : 'zig'}",
|
|
30
|
+
);
|
|
31
|
+
expect(output).toContain(
|
|
32
|
+
"language={createCodeLanguage({ name: 'nim', identifiers: ['nim'], tokenize: (c) => tokens(c) })}",
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('adds the import beside a root-package import and follows aliases', async () => {
|
|
37
|
+
const output = flat(
|
|
38
|
+
await applyTransform(`import {CodeBlock as Code} from '@astryxdesign/core';
|
|
39
|
+
const a = <Code code={src} language="zig" tokenizer={zigTokens} />;`),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
expect(output).toContain(
|
|
43
|
+
"import { createCodeLanguage } from '@astryxdesign/core/CodeBlock';",
|
|
44
|
+
);
|
|
45
|
+
expect(output).toContain("createCodeLanguage({ name: 'zig'");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('adds a separate import beside a namespace import', async () => {
|
|
49
|
+
const output =
|
|
50
|
+
await applyTransform(`import * as CB from '@astryxdesign/core/CodeBlock';
|
|
51
|
+
const a = <CB.CodeBlock code={src} language="zig" tokenizer={zigTokens} />;`);
|
|
52
|
+
expect(output).toContain(
|
|
53
|
+
"import * as CB from '@astryxdesign/core/CodeBlock';",
|
|
54
|
+
);
|
|
55
|
+
expect(flat(output)).toContain(
|
|
56
|
+
"import { createCodeLanguage } from '@astryxdesign/core/CodeBlock';",
|
|
57
|
+
);
|
|
58
|
+
const jscodeshift = (await import('jscodeshift')).default;
|
|
59
|
+
expect(() => jscodeshift.withParser('tsx')(output)).not.toThrow();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('guards a name or property that may be undefined', async () => {
|
|
63
|
+
const output = flat(
|
|
64
|
+
await applyTransform(`import {CodeBlock} from '@astryxdesign/core/CodeBlock';
|
|
65
|
+
const a = <CodeBlock code={src} language="zig" tokenizer={props.tokenizer} />;`),
|
|
66
|
+
);
|
|
67
|
+
expect(output).toContain(
|
|
68
|
+
"language={props.tokenizer ? createCodeLanguage({ name: 'zig', identifiers: ['zig'], tokenize: props.tokenizer }) : 'zig'}",
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('imports under a free alias when the file binds the name already', async () => {
|
|
73
|
+
const fromLocal = flat(
|
|
74
|
+
await applyTransform(`import {CodeBlock} from '@astryxdesign/core/CodeBlock';
|
|
75
|
+
import {createCodeLanguage} from './my-languages';
|
|
76
|
+
const a = <CodeBlock code={src} language="zig" tokenizer={(c) => t(c)} />;`),
|
|
77
|
+
);
|
|
78
|
+
expect(fromLocal).toContain(
|
|
79
|
+
"import { CodeBlock, createCodeLanguage as createCodeLanguage2 } from '@astryxdesign/core/CodeBlock';",
|
|
80
|
+
);
|
|
81
|
+
expect(fromLocal).toContain(
|
|
82
|
+
"import {createCodeLanguage} from './my-languages';",
|
|
83
|
+
);
|
|
84
|
+
expect(fromLocal).toContain("language={createCodeLanguage2({ name: 'zig'");
|
|
85
|
+
|
|
86
|
+
const fromFunction = flat(
|
|
87
|
+
await applyTransform(`import {CodeBlock} from '@astryxdesign/core/CodeBlock';
|
|
88
|
+
function createCodeLanguage() {}
|
|
89
|
+
const a = <CodeBlock code={src} language="zig" tokenizer={(c) => t(c)} />;`),
|
|
90
|
+
);
|
|
91
|
+
expect(fromFunction).toContain('createCodeLanguage as createCodeLanguage2');
|
|
92
|
+
const jscodeshift = (await import('jscodeshift')).default;
|
|
93
|
+
const parse = jscodeshift.withParser('tsx');
|
|
94
|
+
expect(() => parse(fromLocal)).not.toThrow();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('reports a tokenizer value it cannot read twice, naming the tokenizer', async () => {
|
|
98
|
+
const output =
|
|
99
|
+
await applyTransform(`import {CodeBlock} from '@astryxdesign/core/CodeBlock';
|
|
100
|
+
const a = <CodeBlock code={src} language="zig" tokenizer={registry.get('zig').tokenize} />;
|
|
101
|
+
const b = <CodeBlock code={src} language="zig" tokenizer={enabled ? t : undefined} />;`);
|
|
102
|
+
expect(output.match(/this tokenizer value is computed/g)).toHaveLength(2);
|
|
103
|
+
expect(output).not.toContain('the language here is computed');
|
|
104
|
+
expect(output).toContain("tokenizer={registry.get('zig').tokenize}");
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('reports what it cannot prove and leaves it unchanged', async () => {
|
|
108
|
+
const source = `import {CodeBlock} from '@astryxdesign/core/CodeBlock';
|
|
109
|
+
const a = <CodeBlock code={src} language={lang} tokenizer={t} />;
|
|
110
|
+
const b = <CodeBlock code={src} tokenizer={t} />;
|
|
111
|
+
const c = <CodeBlock code={src} language="plaintext" tokenizer={t} />;
|
|
112
|
+
const d = <CodeBlock {...rest} code={src} language="zig" tokenizer={t} />;
|
|
113
|
+
const e = <CodeBlock code={src} language="zig" tokenizer={enabled ? t : undefined} />;
|
|
114
|
+
const f = <CodeBlock code={src} language="zig" tokenizer={enabled && t} />;`;
|
|
115
|
+
const output = await applyTransform(source);
|
|
116
|
+
|
|
117
|
+
expect(output.match(/TODO\(astryx upgrade\)/g)).toHaveLength(6);
|
|
118
|
+
expect(output.match(/tokenizer=\{t\}/g)).toHaveLength(4);
|
|
119
|
+
expect(output).toContain('tokenizer={enabled ? t : undefined}');
|
|
120
|
+
expect(output).toContain('tokenizer={enabled && t}');
|
|
121
|
+
expect(output).not.toMatch(/import \{[^}]*createCodeLanguage/);
|
|
122
|
+
expect(await applyTransform(output)).toBe(output);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('leaves files without CodeBlock tokenizer alone', async () => {
|
|
126
|
+
const source = `import {CodeBlock} from '@astryxdesign/core/CodeBlock';
|
|
127
|
+
const a = <CodeBlock code={src} language="ts" />;`;
|
|
128
|
+
expect(await applyTransform(source)).toBe(source);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
@@ -7,4 +7,14 @@
|
|
|
7
7
|
* this file into the resolved version folder.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
import migrateCodeBlockTokenizerToLanguage, {
|
|
11
|
+
meta as migrateCodeBlockTokenizerToLanguageMeta,
|
|
12
|
+
} from './migrate-codeblock-tokenizer-to-language.mjs';
|
|
13
|
+
|
|
14
|
+
export default [
|
|
15
|
+
{
|
|
16
|
+
name: 'migrate-codeblock-tokenizer-to-language',
|
|
17
|
+
transform: migrateCodeBlockTokenizerToLanguage,
|
|
18
|
+
meta: migrateCodeBlockTokenizerToLanguageMeta,
|
|
19
|
+
},
|
|
20
|
+
];
|