@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
|
@@ -2,7 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
import {describe, expect, it} from 'vitest';
|
|
4
4
|
import jscodeshift from 'jscodeshift';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
importedComponents,
|
|
7
|
+
resolveImportedComponent,
|
|
8
|
+
transformProp,
|
|
9
|
+
} from './transform-prop.mjs';
|
|
6
10
|
|
|
7
11
|
const j = jscodeshift.withParser('tsx');
|
|
8
12
|
const components = new Map([
|
|
@@ -92,4 +96,35 @@ const b = <Group tone="old" size="old" />;`);
|
|
|
92
96
|
expect(output).toContain('<Widget size="old" migratedtone="old"');
|
|
93
97
|
expect(output).toContain('<Group tone="old" migratedsize="old"');
|
|
94
98
|
});
|
|
99
|
+
|
|
100
|
+
it('resolves non-JSX references through the same import and shadow rules', () => {
|
|
101
|
+
const root = j(`import {Widget as Card} from '@example/core';
|
|
102
|
+
import * as Lib from '@example/core';
|
|
103
|
+
const a = Card;
|
|
104
|
+
const b = Lib.Group;
|
|
105
|
+
function shadow(Card, Lib) {
|
|
106
|
+
return [Card, Lib.Group];
|
|
107
|
+
}`);
|
|
108
|
+
const imports = importedComponents(root, j, {
|
|
109
|
+
matchesImport: value => /^@example\/core(?:\/|$)/.test(value),
|
|
110
|
+
components,
|
|
111
|
+
});
|
|
112
|
+
const resolved = root
|
|
113
|
+
.find(j.VariableDeclarator)
|
|
114
|
+
.paths()
|
|
115
|
+
.map(
|
|
116
|
+
path =>
|
|
117
|
+
resolveImportedComponent(path.node.init, path, imports)?.component,
|
|
118
|
+
);
|
|
119
|
+
expect(resolved).toEqual(['Widget', 'Group']);
|
|
120
|
+
const shadowed = root
|
|
121
|
+
.find(j.ArrayExpression)
|
|
122
|
+
.paths()
|
|
123
|
+
.flatMap(path =>
|
|
124
|
+
path.node.elements.map(element =>
|
|
125
|
+
resolveImportedComponent(element, path, imports),
|
|
126
|
+
),
|
|
127
|
+
);
|
|
128
|
+
expect(shadowed).toEqual([null, null]);
|
|
129
|
+
});
|
|
95
130
|
});
|
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
|
+
];
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Codemod: migrate CodeBlock tokenizer to a custom language value
|
|
5
|
+
*
|
|
6
|
+
* `tokenizer={fn}` beside a static `language="x"` other than `plaintext`
|
|
7
|
+
* becomes `language={createCodeLanguage({name: 'x', identifiers: ['x'],
|
|
8
|
+
* tokenize: fn})}` (`component:CodeBlock` FR13). The custom language paints
|
|
9
|
+
* with the same function, labels `x`, and reflects `data-language="x"`, so the
|
|
10
|
+
* block looks the same. `createCodeLanguage` is imported from
|
|
11
|
+
* `@astryxdesign/core/CodeBlock` when the file does not import it yet, under
|
|
12
|
+
* a free alias when the file already binds the name to something else. A
|
|
13
|
+
* `tokenizer` that is a name or a call-free property may be undefined at run
|
|
14
|
+
* time, so it is guarded: `fn ? createCodeLanguage({...}) : 'x'`.
|
|
15
|
+
*
|
|
16
|
+
* Built on the shared `transformProp` helper, so it does not follow
|
|
17
|
+
* variables, helpers, wrappers, or types. Where a CodeBlock writes
|
|
18
|
+
* `tokenizer` but the codemod cannot prove the result, it keeps the old prop
|
|
19
|
+
* and leaves a TODO instead of guessing: a computed or missing `language`, a
|
|
20
|
+
* `tokenizer` value that is neither a function literal nor a name or property
|
|
21
|
+
* (a name or property is guarded with `fn ? createCodeLanguage(...) : 'x'`,
|
|
22
|
+
* since it may be undefined), a
|
|
23
|
+
* `plaintext` language (which shows no label today, while a language value
|
|
24
|
+
* does), and any CodeBlock that also spreads props. The deprecated prop keeps
|
|
25
|
+
* working there. Comments are the codemod's only report channel, and each is
|
|
26
|
+
* added once, so a second run changes nothing.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import {transformProp} from '../../transform-prop.mjs';
|
|
30
|
+
|
|
31
|
+
export const meta = {
|
|
32
|
+
title: 'Migrate CodeBlock tokenizer to a custom language',
|
|
33
|
+
description:
|
|
34
|
+
'Replaces `tokenizer={fn}` beside a static `language="x"` with ' +
|
|
35
|
+
"`language={createCodeLanguage({name: 'x', identifiers: ['x'], tokenize: fn})}`. " +
|
|
36
|
+
'Keeps the old prop with a TODO where the language is computed, missing, ' +
|
|
37
|
+
'or `plaintext`, and where the CodeBlock also spreads props; the ' +
|
|
38
|
+
'deprecated prop still works there.',
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const OLD_NAME = 'tokenizer';
|
|
42
|
+
const CORE_IMPORT_RE = /^@(astryxdesign|xds)\/core(?:\/|$)/;
|
|
43
|
+
const CODEBLOCK_SOURCE = '@astryxdesign/core/CodeBlock';
|
|
44
|
+
const COMPONENT_PROPS = new Map([['CodeBlock', new Set([OLD_NAME])]]);
|
|
45
|
+
|
|
46
|
+
const UNPROVEN_COMMENT =
|
|
47
|
+
' TODO(astryx upgrade): tokenizer is deprecated. Pass ' +
|
|
48
|
+
'language={createCodeLanguage({name, identifiers, tokenize})} instead, ' +
|
|
49
|
+
'with this function as tokenize; the language here is computed, missing, ' +
|
|
50
|
+
'or plaintext, so it was left unchanged. ';
|
|
51
|
+
const VALUE_COMMENT =
|
|
52
|
+
' TODO(astryx upgrade): tokenizer is deprecated. Pass ' +
|
|
53
|
+
'language={createCodeLanguage({name, identifiers, tokenize})} instead; ' +
|
|
54
|
+
'this tokenizer value is computed (not a function, a name, or a ' +
|
|
55
|
+
'call-free property), so it was left unchanged. ';
|
|
56
|
+
const SPREAD_COMMENT =
|
|
57
|
+
' TODO(astryx upgrade): tokenizer is deprecated, but this CodeBlock also ' +
|
|
58
|
+
'spreads props that may set language or tokenizer, so it was left ' +
|
|
59
|
+
'unchanged. Pass language={createCodeLanguage({name, identifiers, ' +
|
|
60
|
+
'tokenize})} by hand. ';
|
|
61
|
+
|
|
62
|
+
const TYPE_WRAPPERS = new Set([
|
|
63
|
+
'TSAsExpression',
|
|
64
|
+
'TSSatisfiesExpression',
|
|
65
|
+
'TSTypeAssertion',
|
|
66
|
+
'TSNonNullExpression',
|
|
67
|
+
'TypeCastExpression',
|
|
68
|
+
'ParenthesizedExpression',
|
|
69
|
+
]);
|
|
70
|
+
|
|
71
|
+
/** @param {any} node */
|
|
72
|
+
function unwrap(node) {
|
|
73
|
+
let current = node;
|
|
74
|
+
while (current && TYPE_WRAPPERS.has(current.type)) {
|
|
75
|
+
current = current.expression;
|
|
76
|
+
}
|
|
77
|
+
return current;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** @param {any} attribute */
|
|
81
|
+
function attributeValue(attribute) {
|
|
82
|
+
return attribute.value?.type === 'JSXExpressionContainer'
|
|
83
|
+
? attribute.value.expression
|
|
84
|
+
: attribute.value;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The static string of a node, or null. @param {any} node */
|
|
88
|
+
function staticString(node) {
|
|
89
|
+
const value = unwrap(node);
|
|
90
|
+
if (
|
|
91
|
+
value != null &&
|
|
92
|
+
(value.type === 'StringLiteral' || value.type === 'Literal') &&
|
|
93
|
+
typeof value.value === 'string'
|
|
94
|
+
) {
|
|
95
|
+
return value.value;
|
|
96
|
+
}
|
|
97
|
+
if (
|
|
98
|
+
value?.type === 'TemplateLiteral' &&
|
|
99
|
+
value.expressions.length === 0 &&
|
|
100
|
+
value.quasis.length === 1
|
|
101
|
+
) {
|
|
102
|
+
return value.quasis[0].value.cooked;
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* A name, or a property chain on one with no call and no computed key other
|
|
109
|
+
* than a literal: safe to read twice, and narrowed by a `?:` guard.
|
|
110
|
+
* @param {any} node
|
|
111
|
+
* @returns {boolean}
|
|
112
|
+
*/
|
|
113
|
+
function isCallFreeChain(node) {
|
|
114
|
+
const value = unwrap(node);
|
|
115
|
+
if (value == null) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
if (value.type === 'Identifier' || value.type === 'ThisExpression') {
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
if (value.type === 'MemberExpression') {
|
|
122
|
+
const key = value.property;
|
|
123
|
+
const literalKey =
|
|
124
|
+
!value.computed ||
|
|
125
|
+
key.type === 'StringLiteral' ||
|
|
126
|
+
key.type === 'NumericLiteral' ||
|
|
127
|
+
key.type === 'Literal';
|
|
128
|
+
return (
|
|
129
|
+
literalKey && value.optional !== true && isCallFreeChain(value.object)
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** @param {any} j @param {any} node @param {string} text */
|
|
136
|
+
function addComment(j, node, text) {
|
|
137
|
+
node.comments ??= [];
|
|
138
|
+
if (node.comments.some((/** @type {any} */ c) => c.value === text)) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
node.comments.push(j.commentBlock(text, true, false));
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The local name `createCodeLanguage` is imported under, adding the import
|
|
147
|
+
* when the file has none.
|
|
148
|
+
* @param {any} j @param {any} root
|
|
149
|
+
*/
|
|
150
|
+
function ensureCreateImport(j, root) {
|
|
151
|
+
let local = null;
|
|
152
|
+
root
|
|
153
|
+
.find(j.ImportDeclaration)
|
|
154
|
+
.filter((/** @type {any} */ p) => CORE_IMPORT_RE.test(p.node.source.value))
|
|
155
|
+
.forEach((/** @type {any} */ p) => {
|
|
156
|
+
for (const specifier of p.node.specifiers ?? []) {
|
|
157
|
+
if (
|
|
158
|
+
specifier.type === 'ImportSpecifier' &&
|
|
159
|
+
specifier.imported.name === 'createCodeLanguage'
|
|
160
|
+
) {
|
|
161
|
+
local = specifier.local?.name ?? 'createCodeLanguage';
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
if (local != null) {
|
|
166
|
+
return local;
|
|
167
|
+
}
|
|
168
|
+
// Any other binding of the name (a local import or function) would clash,
|
|
169
|
+
// so the import takes the first free alias instead.
|
|
170
|
+
const name = freeName(j, root, 'createCodeLanguage');
|
|
171
|
+
const specifier =
|
|
172
|
+
name === 'createCodeLanguage'
|
|
173
|
+
? j.importSpecifier(j.identifier('createCodeLanguage'))
|
|
174
|
+
: j.importSpecifier(
|
|
175
|
+
j.identifier('createCodeLanguage'),
|
|
176
|
+
j.identifier(name),
|
|
177
|
+
);
|
|
178
|
+
const existing = root.find(j.ImportDeclaration).filter(
|
|
179
|
+
(/** @type {any} */ p) =>
|
|
180
|
+
p.node.source.value === CODEBLOCK_SOURCE &&
|
|
181
|
+
p.node.importKind !== 'type' &&
|
|
182
|
+
// A namespace import cannot take a named specifier beside it.
|
|
183
|
+
!(p.node.specifiers ?? []).some(
|
|
184
|
+
(/** @type {any} */ s) => s.type === 'ImportNamespaceSpecifier',
|
|
185
|
+
),
|
|
186
|
+
);
|
|
187
|
+
if (existing.size() > 0) {
|
|
188
|
+
existing.get().node.specifiers.push(specifier);
|
|
189
|
+
} else {
|
|
190
|
+
const declaration = j.importDeclaration(
|
|
191
|
+
[specifier],
|
|
192
|
+
j.stringLiteral(CODEBLOCK_SOURCE),
|
|
193
|
+
);
|
|
194
|
+
const imports = root.find(j.ImportDeclaration);
|
|
195
|
+
if (imports.size() > 0) {
|
|
196
|
+
imports.at(imports.size() - 1).insertAfter(declaration);
|
|
197
|
+
} else {
|
|
198
|
+
root.get().node.program.body.unshift(declaration);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return name;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* `preferred`, or `preferred` with a numeric suffix, whichever no binding or
|
|
206
|
+
* reference in the file already uses.
|
|
207
|
+
* @param {any} j @param {any} root @param {string} preferred
|
|
208
|
+
*/
|
|
209
|
+
function freeName(j, root, preferred) {
|
|
210
|
+
const used = new Set();
|
|
211
|
+
root.find(j.Identifier).forEach((/** @type {any} */ p) => {
|
|
212
|
+
used.add(p.node.name);
|
|
213
|
+
});
|
|
214
|
+
if (!used.has(preferred)) {
|
|
215
|
+
return preferred;
|
|
216
|
+
}
|
|
217
|
+
let n = 2;
|
|
218
|
+
while (used.has(`${preferred}${n}`)) {
|
|
219
|
+
n++;
|
|
220
|
+
}
|
|
221
|
+
return `${preferred}${n}`;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* @param {import('../../../../authoring/codemod/type').AstryxCodemodFile} file
|
|
226
|
+
* @param {import('../../../../authoring/codemod/type').CodemodTransformApi} api
|
|
227
|
+
* @returns {string | null | undefined}
|
|
228
|
+
*/
|
|
229
|
+
export default function transformer(file, api) {
|
|
230
|
+
const j = api.jscodeshift;
|
|
231
|
+
const root = j(file.source);
|
|
232
|
+
let hasChanges = false;
|
|
233
|
+
/** @type {Array<() => void>} */
|
|
234
|
+
const rewrites = [];
|
|
235
|
+
|
|
236
|
+
transformProp(
|
|
237
|
+
root,
|
|
238
|
+
j,
|
|
239
|
+
{
|
|
240
|
+
matchesImport: source => CORE_IMPORT_RE.test(source),
|
|
241
|
+
components: COMPONENT_PROPS,
|
|
242
|
+
},
|
|
243
|
+
propPath => {
|
|
244
|
+
const opening = propPath.parent?.node;
|
|
245
|
+
const attributes = opening?.attributes ?? [];
|
|
246
|
+
const prop = propPath.node;
|
|
247
|
+
if (
|
|
248
|
+
attributes.some(
|
|
249
|
+
(/** @type {any} */ a) => a.type === 'JSXSpreadAttribute',
|
|
250
|
+
)
|
|
251
|
+
) {
|
|
252
|
+
hasChanges = addComment(j, prop, SPREAD_COMMENT) || hasChanges;
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
const languageAttribute = attributes.find(
|
|
256
|
+
(/** @type {any} */ a) =>
|
|
257
|
+
a.type === 'JSXAttribute' && a.name?.name === 'language',
|
|
258
|
+
);
|
|
259
|
+
const language =
|
|
260
|
+
languageAttribute == null
|
|
261
|
+
? null
|
|
262
|
+
: staticString(attributeValue(languageAttribute));
|
|
263
|
+
const tokenize = attributeValue(prop);
|
|
264
|
+
const tokenizeNode = unwrap(tokenize);
|
|
265
|
+
// A function literal is always a function. A name or property may be
|
|
266
|
+
// undefined at run time (a forwarded optional prop), so it is guarded:
|
|
267
|
+
// `fn ? createCodeLanguage({...}) : 'x'` keeps the plain block the
|
|
268
|
+
// released prop drew without one. Anything else is reported.
|
|
269
|
+
const isFunction =
|
|
270
|
+
tokenizeNode?.type === 'ArrowFunctionExpression' ||
|
|
271
|
+
tokenizeNode?.type === 'FunctionExpression';
|
|
272
|
+
const isGuardable = isCallFreeChain(tokenizeNode);
|
|
273
|
+
if (language == null || language === '' || language === 'plaintext') {
|
|
274
|
+
hasChanges = addComment(j, prop, UNPROVEN_COMMENT) || hasChanges;
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (!(isFunction || isGuardable)) {
|
|
278
|
+
hasChanges = addComment(j, prop, VALUE_COMMENT) || hasChanges;
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
rewrites.push(() => {
|
|
282
|
+
const create = ensureCreateImport(j, root);
|
|
283
|
+
const value = j.callExpression(j.identifier(create), [
|
|
284
|
+
j.objectExpression([
|
|
285
|
+
j.objectProperty(j.identifier('name'), j.stringLiteral(language)),
|
|
286
|
+
j.objectProperty(
|
|
287
|
+
j.identifier('identifiers'),
|
|
288
|
+
j.arrayExpression([j.stringLiteral(language)]),
|
|
289
|
+
),
|
|
290
|
+
j.objectProperty(j.identifier('tokenize'), tokenize),
|
|
291
|
+
]),
|
|
292
|
+
]);
|
|
293
|
+
languageAttribute.value = j.jsxExpressionContainer(
|
|
294
|
+
isFunction
|
|
295
|
+
? value
|
|
296
|
+
: j.conditionalExpression(
|
|
297
|
+
tokenize,
|
|
298
|
+
value,
|
|
299
|
+
j.stringLiteral(language),
|
|
300
|
+
),
|
|
301
|
+
);
|
|
302
|
+
opening.attributes = opening.attributes.filter(
|
|
303
|
+
(/** @type {any} */ a) => a !== prop,
|
|
304
|
+
);
|
|
305
|
+
});
|
|
306
|
+
},
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
for (const rewrite of rewrites) {
|
|
310
|
+
rewrite();
|
|
311
|
+
hasChanges = true;
|
|
312
|
+
}
|
|
313
|
+
if (!hasChanges) return undefined;
|
|
314
|
+
return root.toSource({quote: 'single'});
|
|
315
|
+
}
|