@astryxdesign/cli 0.7.4 → 0.7.5-canary.0418c47
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 +20 -20
- package/api/component/_adapter.mjs +16 -1
- package/api/component/component.test.mjs +31 -0
- package/api/discover/_adapter.d.mts +1 -1
- 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/experimental.d.mts +5 -0
- package/api/experimental.mjs +15 -0
- package/api/hook/list/list.d.mts +1 -1
- package/api/icon/_adapter.d.mts +3 -2
- package/api/icon/_adapter.mjs +16 -3
- package/api/icon/_authoring.d.mts +5 -1
- package/api/icon/_authoring.mjs +478 -63
- package/api/icon/_options.d.mts +11 -0
- package/api/icon/_options.mjs +39 -4
- package/api/icon/_records.d.mts +19 -0
- package/api/icon/_records.mjs +25 -0
- package/api/icon/add/add.d.mts +3 -3
- package/api/icon/add/add.mjs +227 -14
- package/api/icon/all-or-nothing.test.mjs +283 -0
- package/api/icon/detail/detail.mjs +2 -13
- package/api/icon/error-codes.test.mjs +14 -7
- package/api/icon/icon.d.mts +2 -2
- package/api/icon/icon.doc.mjs +30 -2
- package/api/icon/icon.mjs +57 -8
- package/api/icon/icon.test.mjs +133 -1
- package/api/icon/icon.type.d.mts +126 -1
- package/api/icon/icon.type.mjs +67 -3
- package/api/icon/iconAdd.doc.mjs +22 -6
- package/api/icon/iconMap.doc.mjs +24 -8
- package/api/icon/iconNewDefinition.doc.mjs +2 -2
- package/api/icon/iconNewSet.doc.mjs +30 -2
- package/api/icon/iconUse.doc.mjs +4 -1
- package/api/icon/map/map.d.mts +4 -4
- package/api/icon/map/map.mjs +134 -9
- package/api/icon/new-definition/new-definition.mjs +6 -2
- package/api/icon/new-set/new-set.mjs +2 -2
- package/api/icon/new-set/new-set.test.mjs +60 -10
- package/api/icon/option-pairs.test.mjs +368 -0
- package/api/icon/search/search.d.mts +10 -0
- package/api/icon/search/search.mjs +55 -14
- package/api/icon/use/use.mjs +2 -13
- package/api/integration/add-helpers.d.mts +15 -1
- package/api/integration/add-helpers.mjs +1 -1
- 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/integration/validate-integration.type.d.mts +3 -2
- package/api/integration/validate-integration.type.mjs +3 -2
- package/api/render/_adapter.d.mts +28 -0
- package/api/render/_adapter.mjs +540 -0
- package/api/render/render.d.mts +15 -0
- package/api/render/render.doc.d.mts +9 -0
- package/api/render/render.doc.mjs +112 -0
- package/api/render/render.mjs +113 -0
- package/api/render/render.test.mjs +1155 -0
- package/api/render/render.type.d.mts +74 -0
- package/api/render/render.type.mjs +64 -0
- package/api/search/_adapter.test.mjs +21 -2
- package/api/search/_result.d.mts +5 -5
- package/api/search/search.d.mts +1 -1
- package/api/template/template.mjs +3 -1
- package/api/template/template.type.d.mts +2 -1
- package/api/template/template.type.mjs +1 -1
- package/api/template/tokenizer-showcase.test.mjs +47 -0
- package/api/template/unknown-name.test.mjs +63 -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 +212 -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 +670 -44
- package/api/upgrade/upgrade.doc.mjs +4 -4
- package/api/upgrade/upgrade.mjs +19 -4
- package/api/upgrade/upgrade.type.d.mts +36 -1
- package/api/upgrade/upgrade.type.mjs +29 -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/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 +109 -5
- package/assets/codemods/runner.mjs +119 -13
- package/assets/codemods/transform-prop.mjs +99 -49
- package/assets/codemods/transform-prop.test.mjs +36 -1
- 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__/migrate-deprecated-theme-surface.test.mjs +207 -6
- package/assets/codemods/transforms/v0.7.0/__tests__/validate-data-token-ownership.test.mjs +143 -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 +23 -78
- package/assets/discover/official-packages.json +4 -0
- 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/icon-definitions.doc.mjs +2 -2
- package/assets/docs/tree/icon-set-files.doc.mjs +1 -1
- package/assets/docs/tree/icon-sets.doc.mjs +50 -20
- package/assets/docs/tree/render-pages.doc.mjs +94 -0
- package/assets/docs/tree/styling-advanced.doc.mjs +1 -1
- 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/icons/definitions/activity.doc.mjs +1 -1
- package/assets/icons/definitions/alignCenter.doc.mjs +9 -0
- package/assets/icons/definitions/alignEnd.doc.mjs +10 -0
- package/assets/icons/definitions/alignStart.doc.mjs +10 -0
- package/assets/icons/definitions/archive.doc.mjs +1 -1
- package/assets/icons/definitions/arrowsUpDown.doc.mjs +1 -1
- package/assets/icons/definitions/attach.doc.mjs +1 -1
- package/assets/icons/definitions/audio.doc.mjs +1 -1
- package/assets/icons/definitions/cart.doc.mjs +1 -1
- package/assets/icons/definitions/checkDouble.doc.mjs +1 -1
- package/assets/icons/definitions/chevronsLeft.doc.mjs +1 -1
- package/assets/icons/definitions/chevronsRight.doc.mjs +1 -1
- package/assets/icons/definitions/close.doc.mjs +1 -1
- package/assets/icons/definitions/collapse.doc.mjs +1 -1
- package/assets/icons/definitions/compose.doc.mjs +1 -1
- package/assets/icons/definitions/conversation.doc.mjs +1 -1
- package/assets/icons/definitions/copy.doc.mjs +1 -1
- package/assets/icons/definitions/cpu.doc.mjs +9 -0
- package/assets/icons/definitions/database.doc.mjs +1 -1
- package/assets/icons/definitions/desktop.doc.mjs +1 -1
- package/assets/icons/definitions/discount.doc.mjs +1 -1
- package/assets/icons/definitions/download.doc.mjs +1 -1
- package/assets/icons/definitions/emoji.doc.mjs +1 -1
- package/assets/icons/definitions/error.doc.mjs +1 -1
- package/assets/icons/definitions/expand.doc.mjs +1 -1
- package/assets/icons/definitions/experiment.doc.mjs +1 -1
- package/assets/icons/definitions/externalLink.doc.mjs +7 -1
- package/assets/icons/definitions/eyeSlash.doc.mjs +1 -1
- package/assets/icons/definitions/flipHorizontal.doc.mjs +9 -0
- package/assets/icons/definitions/flipVertical.doc.mjs +9 -0
- package/assets/icons/definitions/folderAdd.doc.mjs +1 -1
- package/assets/icons/definitions/gitBranch.doc.mjs +9 -0
- package/assets/icons/definitions/gitFork.doc.mjs +9 -0
- package/assets/icons/definitions/info.doc.mjs +1 -1
- package/assets/icons/definitions/location.doc.mjs +1 -1
- package/assets/icons/definitions/mention.doc.mjs +1 -1
- package/assets/icons/definitions/menu.doc.mjs +1 -1
- package/assets/icons/definitions/microphone.doc.mjs +1 -1
- package/assets/icons/definitions/mobile.doc.mjs +1 -1
- package/assets/icons/definitions/money.doc.mjs +1 -1
- package/assets/icons/definitions/moreHorizontal.doc.mjs +1 -1
- package/assets/icons/definitions/notificationActive.doc.mjs +1 -1
- package/assets/icons/definitions/notificationOff.doc.mjs +1 -1
- package/assets/icons/definitions/panelLeft.doc.mjs +10 -0
- package/assets/icons/definitions/panelRight.doc.mjs +10 -0
- package/assets/icons/definitions/raiseHand.doc.mjs +1 -1
- package/assets/icons/definitions/redo.doc.mjs +1 -1
- package/assets/icons/definitions/refresh.doc.mjs +1 -1
- package/assets/icons/definitions/rotate.doc.mjs +9 -0
- package/assets/icons/definitions/scale.doc.mjs +10 -0
- package/assets/icons/definitions/search.doc.mjs +1 -1
- package/assets/icons/definitions/send.doc.mjs +1 -1
- package/assets/icons/definitions/settings.doc.mjs +1 -1
- package/assets/icons/definitions/star.doc.mjs +1 -1
- package/assets/icons/definitions/stop.doc.mjs +1 -1
- package/assets/icons/definitions/success.doc.mjs +1 -1
- package/assets/icons/definitions/textCase.doc.mjs +9 -0
- package/assets/icons/definitions/thumbsDown.doc.mjs +1 -1
- package/assets/icons/definitions/thumbsUp.doc.mjs +1 -1
- package/assets/icons/definitions/trendDown.doc.mjs +7 -1
- package/assets/icons/definitions/trendUp.doc.mjs +1 -1
- package/assets/icons/definitions/undo.doc.mjs +1 -1
- package/assets/icons/definitions/upload.doc.mjs +1 -1
- package/assets/icons/definitions/userAdd.doc.mjs +1 -1
- package/assets/icons/definitions/warning.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/TokenizerClear.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerCreatable.tsx +30 -30
- package/assets/templates/blocks/components/Tokenizer/TokenizerEndContent.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerIcon.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerMaxEntries.tsx +17 -16
- package/assets/templates/blocks/components/Tokenizer/TokenizerOverflow.tsx +29 -29
- package/assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx +22 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerStates.tsx +39 -41
- package/assets/templates/pages/canvas-editor/template.doc.mjs +9 -0
- package/assets/templates/pages/dashboard-alert-rail/page.tsx +3 -3
- package/assets/templates/pages/form-wizard-inline/template.doc.mjs +6 -1
- package/assets/templates/pages/library/page.tsx +1 -2
- package/assets/templates/pages/table-comparison/page.tsx +12 -3
- package/assets/templates/pages/table-comparison/template.doc.mjs +1 -0
- package/assets/templates/pages/table-tree/template.doc.mjs +3 -0
- 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/authoring/debug/parse.d.mts +2 -2
- package/authoring/doctypes/icon-definition/icon-definition.doc.mjs +4 -4
- package/authoring/doctypes/icon-set/icon-set-data.doc.mjs +6 -0
- package/authoring/doctypes/icon-set/parse.d.mts +1 -0
- package/authoring/doctypes/icon-set/parse.mjs +1 -0
- package/authoring/doctypes/icon-set/type.ts +5 -0
- package/authoring/doctypes/template/template.doc.mjs +1 -1
- package/authoring/doctypes/template/type.ts +2 -1
- package/clients/cli/commands/build-theme.adaptations.test.mjs +110 -0
- package/clients/cli/commands/doctor.mjs +16 -17
- package/clients/cli/commands/experimental/icon.mjs +167 -35
- package/clients/cli/commands/experimental/index.mjs +2 -0
- package/clients/cli/commands/experimental/render.mjs +113 -0
- package/clients/cli/commands/experimental-icon-add.doc.mjs +14 -4
- package/clients/cli/commands/experimental-icon-map.doc.mjs +12 -2
- package/clients/cli/commands/experimental-icon-new-definition.doc.mjs +1 -1
- package/clients/cli/commands/experimental-icon-new-set.doc.mjs +28 -1
- package/clients/cli/commands/experimental-icon.doc.mjs +18 -2
- package/clients/cli/commands/experimental-render.doc.mjs +60 -0
- package/clients/cli/commands/experimental.doc.mjs +4 -2
- package/clients/cli/commands/icon-authoring.test.mjs +1823 -13
- package/clients/cli/commands/icon-options-wiring.test.mjs +158 -0
- package/clients/cli/commands/icon.test.mjs +1 -1
- package/clients/cli/commands/template.mjs +4 -1
- package/clients/cli/commands/text-json-parity.test.mjs +46 -0
- package/clients/cli/commands/upgrade.doc.mjs +47 -2
- 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/clients/cli/commands/upgrade.outcomes.test.mjs +350 -0
- package/clients/cli/index.mjs +1 -0
- package/foundation/agent-docs/agent-docs.mjs +1 -0
- package/foundation/agent-docs/agent-docs.test.mjs +5 -0
- package/foundation/discovery/component-discovery.d.mts +2 -1
- package/foundation/discovery/component-discovery.export-names.test.mjs +41 -0
- package/foundation/discovery/component-discovery.mjs +13 -7
- package/foundation/discovery/icon-set-checks.d.mts +43 -9
- package/foundation/discovery/icon-set-checks.mjs +296 -21
- package/foundation/discovery/icon-set-checks.test.mjs +125 -12
- package/foundation/discovery/icon-set-writer.d.mts +192 -3
- package/foundation/discovery/icon-set-writer.mjs +608 -34
- package/foundation/discovery/icon-set-writer.test.mjs +26 -0
- package/foundation/discovery/module-exports.mjs +82 -25
- package/foundation/discovery/module-exports.test.mjs +38 -1
- package/foundation/discovery/package-exports.d.mts +13 -0
- package/foundation/discovery/package-exports.mjs +40 -1
- package/foundation/discovery/template-icons.d.mts +6 -0
- package/foundation/discovery/template-icons.mjs +98 -10
- package/foundation/discovery/template-icons.test.mjs +103 -0
- package/foundation/doc-compiler/doc-loads.test.mjs +20 -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/doc-compiler/tree.mjs +3 -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/render/assets.d.mts +31 -0
- package/foundation/render/assets.mjs +102 -0
- package/foundation/render/component-map.d.mts +52 -0
- package/foundation/render/component-map.mjs +141 -0
- package/foundation/render/css-cut.d.mts +29 -0
- package/foundation/render/css-cut.mjs +144 -0
- package/foundation/render/embedded-files.d.mts +28 -0
- package/foundation/render/embedded-files.mjs +108 -0
- package/foundation/render/embedded-imports.d.mts +61 -0
- package/foundation/render/embedded-imports.mjs +315 -0
- package/foundation/render/html-reader.d.mts +37 -0
- package/foundation/render/html-reader.mjs +121 -0
- package/foundation/render/page-worker.d.mts +7 -0
- package/foundation/render/page-worker.mjs +519 -0
- package/foundation/render/rebuild.d.mts +19 -0
- package/foundation/render/rebuild.mjs +269 -0
- package/foundation/render/render-page.d.mts +85 -0
- package/foundation/render/render-page.mjs +612 -0
- package/foundation/render/render.test.mjs +117 -0
- package/foundation/render/source-block.d.mts +52 -0
- package/foundation/render/source-block.mjs +106 -0
- package/foundation/response/base.d.ts +7 -0
- package/foundation/response/error-codes.d.mts +10 -1
- package/foundation/response/error-codes.doc.mjs +59 -2
- package/foundation/response/error-codes.mjs +62 -1
- package/foundation/response/json.d.mts +10 -3
- package/foundation/response/json.mjs +16 -4
- package/foundation/response/response-types.doc.mjs +39 -2
- package/foundation/text/string-utils.mjs +33 -0
- package/foundation/text/string-utils.test.mjs +17 -0
- 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 +15 -9
|
@@ -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,8 @@ 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
|
-
* @
|
|
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}>}
|
|
423
513
|
*/
|
|
424
514
|
export async function runCodemods(
|
|
425
515
|
versionManifests,
|
|
@@ -432,6 +522,7 @@ export async function runCodemods(
|
|
|
432
522
|
root = process.cwd(),
|
|
433
523
|
protection: providedProtection,
|
|
434
524
|
coversTarget = true,
|
|
525
|
+
outcomes,
|
|
435
526
|
},
|
|
436
527
|
) {
|
|
437
528
|
// No-op stub object so silent mode skips log output entirely without
|
|
@@ -472,7 +563,7 @@ export async function runCodemods(
|
|
|
472
563
|
let files = [];
|
|
473
564
|
if (sourcePathExists) {
|
|
474
565
|
log.step(`Scanning ${resolvedPath} for source files...`);
|
|
475
|
-
files = findSourceFiles(resolvedPath);
|
|
566
|
+
files = findSourceFiles(resolvedPath, path.resolve(root));
|
|
476
567
|
if (files.length === 0) {
|
|
477
568
|
log.warn('No source files found.');
|
|
478
569
|
} else {
|
|
@@ -500,6 +591,8 @@ export async function runCodemods(
|
|
|
500
591
|
const writtenFiles = [];
|
|
501
592
|
/** @type {import('../../authoring/codemod/type').CodemodRunResult['protectedFiles']} */
|
|
502
593
|
const protectedFiles = [];
|
|
594
|
+
/** @type {NonNullable<import('../../authoring/codemod/type').CodemodRunResult['declinedCandidates']>} */
|
|
595
|
+
const declinedCandidates = [];
|
|
503
596
|
/** @type {Array<{name: string, meta: {title: string, description?: string, fileExtensions?: string[], codemodType?: string}, version: string}>} */
|
|
504
597
|
const skippedOptional = [];
|
|
505
598
|
for (const {version, transforms} of versionManifests) {
|
|
@@ -589,6 +682,7 @@ export async function runCodemods(
|
|
|
589
682
|
);
|
|
590
683
|
errors.push(...result.errors);
|
|
591
684
|
protectedFiles.push(...result.protectedFiles);
|
|
685
|
+
declinedCandidates.push(...(result.declinedCandidates ?? []));
|
|
592
686
|
changedFiles.push(...result.changedFiles);
|
|
593
687
|
writtenFiles.push(...result.writtenFiles);
|
|
594
688
|
totalTransformsApplied += result.filesChanged;
|
|
@@ -640,19 +734,30 @@ export async function runCodemods(
|
|
|
640
734
|
);
|
|
641
735
|
}
|
|
642
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
|
+
|
|
643
747
|
if (
|
|
644
748
|
totalFilesChanged === 0 &&
|
|
645
749
|
errors.length === 0 &&
|
|
646
|
-
protectedFiles.length === 0
|
|
750
|
+
protectedFiles.length === 0 &&
|
|
751
|
+
reportedDeclined.length === 0
|
|
647
752
|
) {
|
|
648
753
|
// Codemods that stop before the installed target cannot vouch for the
|
|
649
754
|
// code; the caller says what is missing.
|
|
650
|
-
if (coversTarget) {
|
|
755
|
+
if (coversTarget && !(outcomes && outcomes.length > 0)) {
|
|
651
756
|
log.success('No changes needed — your code is already up to date!');
|
|
652
757
|
}
|
|
653
|
-
} else if (apply && protectedFiles.length > 0) {
|
|
758
|
+
} else if (apply && (protectedFiles.length > 0 || pendingRegeneration)) {
|
|
654
759
|
log.warn(
|
|
655
|
-
`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'}.`,
|
|
656
761
|
);
|
|
657
762
|
} else if (apply) {
|
|
658
763
|
log.success(
|
|
@@ -693,6 +798,7 @@ export async function runCodemods(
|
|
|
693
798
|
changedFiles,
|
|
694
799
|
writtenFiles,
|
|
695
800
|
protectedFiles,
|
|
801
|
+
declinedCandidates: reportedDeclined,
|
|
696
802
|
stagedContents: virtualContents,
|
|
697
803
|
errors,
|
|
698
804
|
skippedOptional,
|
|
@@ -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];
|