@astryxdesign/cli 0.7.0-canary.450c68d → 0.7.0-canary.4ec1f4e
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 +30 -33
- package/api/doctor/doctor.doc.mjs +1 -1
- package/api/doctor/doctor.test.mjs +28 -0
- package/api/doctor/theme-checks.mjs +10 -10
- package/api/doctor/theme-checks.test.mjs +5 -4
- package/api/gap-report/gap-report.doc.mjs +1 -1
- package/api/gap-report/gap-report.mjs +15 -0
- package/api/gap-report/gap-report.test.mjs +1 -1
- package/api/index.d.mts +0 -2
- package/api/index.mjs +0 -2
- package/api/init/init.doc.mjs +5 -1
- package/api/init/init.mjs +22 -0
- package/api/init/init.type.d.mts +4 -1
- package/api/init/init.type.mjs +1 -1
- package/api/init/remove/remove.mjs +8 -3
- package/api/init/remove/remove.test.mjs +12 -0
- package/api/init/run/run.d.mts +1 -1
- package/api/init/run/run.mjs +3 -3
- package/api/integration/validate-integration.d.mts +4 -0
- package/api/integration/validate-integration.mjs +5 -2
- package/api/integration/validateIntegration.doc.mjs +1 -1
- package/api/json/index.ts +0 -1
- package/api/search/search.doc.mjs +1 -1
- package/api/search/search.mjs +1 -1
- package/api/search/search.test.mjs +1 -1
- package/api/search/search.type.d.mts +1 -1
- package/api/search/search.type.mjs +1 -1
- package/api/template/template-integration.test.mjs +37 -100
- package/api/template/template.doc.mjs +5 -1
- package/api/template/template.mjs +45 -0
- package/api/theme/_adapter.mjs +3 -3
- package/api/theme/add/add.d.mts +11 -5
- package/api/theme/add/add.mjs +19 -192
- package/api/theme/add/add.test.mjs +68 -258
- package/api/theme/add/add.types.test.mjs +0 -1
- package/api/theme/build/build.d.mts +3 -3
- package/api/theme/build/build.mjs +22 -36
- package/api/theme/build/build.test.mjs +11 -24
- package/api/theme/eject/eject.copy-compat.test.mjs +168 -0
- package/api/theme/integration-themes.test.mjs +3 -3
- package/api/theme/remove/remove.test.mjs +1 -1
- package/api/theme/targets/targets.test.mjs +0 -4
- package/api/theme/template/template.mjs +1 -1
- package/api/theme/theme.type.d.mts +1 -30
- package/api/theme/theme.type.mjs +3 -13
- package/api/theme/themeAdd.doc.mjs +19 -27
- package/api/theme/themeTargets.doc.mjs +3 -4
- package/api/theme/themeTemplate.doc.mjs +1 -1
- package/api/theme/use/use.test.mjs +1 -1
- package/api/upgrade/upgrade.doc.mjs +1 -1
- package/api/upgrade/upgrade.mjs +19 -0
- package/assets/codemods/transforms/next/README.md +15 -17
- package/assets/codemods/transforms/next/__tests__/migrate-deprecated-theme-surface.test.mjs +322 -0
- package/assets/codemods/transforms/next/index.mjs +11 -1
- package/assets/codemods/transforms/next/migrate-deprecated-theme-surface-v0.6-data.mjs +229 -0
- package/assets/codemods/transforms/next/migrate-deprecated-theme-surface.mjs +624 -0
- package/assets/codemods/transforms/v0.6.0/migrate-astryx-theme-selectors-to-data-attrs.mjs +38 -18
- package/assets/docs/author-a-theme.doc.mjs +1 -1
- package/assets/docs/getting-started.doc.mjs +2 -2
- package/assets/docs/theme.doc.dense.mjs +1 -1
- package/assets/docs/theme.doc.mjs +2 -2
- package/assets/docs/tree/add-a-theme.doc.mjs +1 -1
- package/assets/docs/tree/document-the-theme.doc.mjs +2 -2
- package/assets/docs/tree/fonts-and-assets.doc.mjs +3 -3
- package/assets/docs/tree/styling-advanced.doc.mjs +21 -14
- package/assets/docs/tree/themes.doc.mjs +1 -1
- package/assets/docs/tree/use-a-theme-in-an-app.doc.mjs +2 -2
- package/assets/docs/tree/versioning.doc.mjs +1 -4
- package/assets/docs/use-a-theme.doc.mjs +16 -16
- package/authoring/debug/type.ts +2 -2
- package/authoring/doctypes/base/type.ts +0 -9
- package/authoring/integration/integration.doc.mjs +1 -1
- package/clients/cli/command-result-coverage.test.mjs +1 -1
- package/clients/cli/commands/build-theme.adaptations.test.mjs +4 -0
- package/clients/cli/commands/build-theme.ascii-output.test.mjs +35 -8
- package/clients/cli/commands/build-theme.mjs +31 -67
- package/clients/cli/commands/build-theme.registry.test.mjs +0 -10
- package/clients/cli/commands/debug-result-summary.test.mjs +0 -16
- package/clients/cli/commands/discover.mjs +2 -1
- package/clients/cli/commands/doctor.mjs +5 -2
- package/clients/cli/commands/empty-argument.test.mjs +16 -0
- package/clients/cli/commands/gap-report.doc.mjs +1 -1
- package/clients/cli/commands/gap-report.test.mjs +2 -2
- package/clients/cli/commands/init.doc.mjs +4 -4
- package/clients/cli/commands/init.flag-help.test.mjs +7 -10
- package/clients/cli/commands/init.next-steps.test.mjs +1 -1
- package/clients/cli/commands/option-pair-refusals.test.mjs +37 -0
- package/clients/cli/commands/search.test.mjs +1 -1
- package/clients/cli/commands/swizzle.mjs +4 -1
- package/clients/cli/commands/template.doc.mjs +3 -3
- package/clients/cli/commands/template.flag-help.test.mjs +18 -11
- package/clients/cli/commands/template.mjs +7 -1
- package/clients/cli/commands/text-json-parity.test.mjs +0 -17
- package/clients/cli/commands/theme-add-lifecycle.test.mjs +142 -49
- package/clients/cli/commands/theme-add.doc.mjs +10 -17
- package/clients/cli/commands/theme-list.behavior.test.mjs +2 -1
- package/clients/cli/commands/theme-targets.behavior.test.mjs +8 -6
- package/clients/cli/commands/theme-template.doc.mjs +1 -1
- package/clients/cli/commands/theme.doc.mjs +2 -2
- package/clients/cli/commands/upgrade.doc.mjs +1 -1
- package/clients/cli/commands/upgrade.flag-help.test.mjs +7 -11
- package/clients/cli/commands/validate-nothing-exits-nonzero.test.mjs +67 -0
- package/clients/cli/index.mjs +0 -4
- package/clients/cli/lib/cli-error.mjs +19 -0
- package/clients/cli/lib/component-format.mjs +111 -57
- package/clients/cli/lib/component-format.test.mjs +28 -39
- package/clients/cli/lib/doc-text-ascii.test.mjs +5 -2
- package/clients/cli/lib/exit-codes.test.mjs +0 -7
- package/clients/cli/lib/json-shim.test.mjs +0 -24
- package/foundation/agent-docs/agent-docs.d.mts +2 -1
- package/foundation/agent-docs/agent-docs.mjs +6 -1
- package/foundation/agent-docs/app-theme-workflow.test.mjs +3 -0
- package/foundation/config/theme-state.mjs +3 -3
- package/foundation/debug/debug-fields.test.mjs +13 -35
- package/foundation/debug/event.mjs +13 -78
- package/foundation/debug/privacy-contract.test.mjs +20 -12
- package/foundation/debug/redact.d.mts +2 -2
- package/foundation/debug/redact.mjs +2 -2
- package/foundation/discovery/theme-imports.mjs +1 -1
- package/foundation/discovery/theming-targets.d.mts +1 -20
- package/foundation/discovery/theming-targets.mjs +1 -38
- package/foundation/discovery/theming-targets.test.mjs +9 -175
- package/foundation/doc-compiler/doc-loads.test.mjs +0 -12
- package/foundation/response/error-codes.doc.mjs +1 -1
- package/foundation/response/response-types.doc.mjs +11 -30
- package/foundation/response/response-types.doc.test.mjs +36 -14
- package/foundation/response/response.doc.mjs +1 -1
- package/foundation/xle/browser.d.mts +2 -2
- package/foundation/xle/browser.mjs +2 -2
- package/foundation/xle/expand.mjs +1 -1
- package/foundation/xle/print.mjs +1 -1
- package/package.json +9 -9
- package/api/layout/_adapter.d.mts +0 -34
- package/api/layout/_adapter.mjs +0 -148
- package/api/layout/check/check.d.mts +0 -17
- package/api/layout/check/check.mjs +0 -41
- package/api/layout/expand/expand.d.mts +0 -23
- package/api/layout/expand/expand.mjs +0 -161
- package/api/layout/expand/expand.path-safety.test.mjs +0 -53
- package/api/layout/expand/expand.receipt.test.mjs +0 -74
- package/api/layout/grammar/grammar.d.mts +0 -14
- package/api/layout/grammar/grammar.mjs +0 -88
- package/api/layout/layout.d.mts +0 -6
- package/api/layout/layout.mjs +0 -17
- package/api/layout/layout.test.mjs +0 -297
- package/api/layout/layout.type.d.mts +0 -90
- package/api/layout/layout.type.mjs +0 -104
- package/api/layout/layoutCheck.doc.d.mts +0 -11
- package/api/layout/layoutCheck.doc.mjs +0 -85
- package/api/layout/layoutExpand.doc.d.mts +0 -11
- package/api/layout/layoutExpand.doc.mjs +0 -107
- package/api/layout/layoutGrammar.doc.d.mts +0 -11
- package/api/layout/layoutGrammar.doc.mjs +0 -57
- package/api/theme/add/add.binary.test.mjs +0 -157
- package/api/theme/add/add.rollback.test.mjs +0 -158
- package/api/theme/add/add.staging.test.mjs +0 -83
- package/clients/cli/commands/layout-check.doc.mjs +0 -67
- package/clients/cli/commands/layout-expand.doc.mjs +0 -87
- package/clients/cli/commands/layout-grammar.doc.mjs +0 -32
- package/clients/cli/commands/layout.deprecation-lifecycle.test.mjs +0 -140
- package/clients/cli/commands/layout.doc.mjs +0 -36
- package/clients/cli/commands/layout.error-codes.test.mjs +0 -66
- package/clients/cli/commands/layout.exit-parity.test.mjs +0 -41
- package/clients/cli/commands/layout.expand-receipt.test.mjs +0 -94
- package/clients/cli/commands/layout.mjs +0 -327
- package/clients/cli/commands/layout.path-help.test.mjs +0 -33
- package/clients/cli/commands/layout.stdin-cap.test.mjs +0 -47
- package/clients/cli/commands/layout.text-fields.test.mjs +0 -39
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Migrate the component-theming compatibility surface removed in 0.7.
|
|
5
|
+
*
|
|
6
|
+
* Static keys directly inside a theme's `components` object move to their
|
|
7
|
+
* canonical target. A theme is a `defineTheme()` call imported from Astryx, an
|
|
8
|
+
* object typed as an Astryx theme (annotation, `as`, or `satisfies`), or a
|
|
9
|
+
* direct theme object with a static `name` whose component entries are style
|
|
10
|
+
* objects. CSS selectors move the same target classes, keep the owner scope of
|
|
11
|
+
* the per-component clear-icon aliases, and replace target-qualified bare
|
|
12
|
+
* prop/state classes with reflected `data-*` selectors using the final 0.6
|
|
13
|
+
* runtime contract. A known bare class that cannot be classified for its target
|
|
14
|
+
* gets a TODO. Dynamic theme keys, unqualified classes, declarations, comments,
|
|
15
|
+
* and strings containing CSS are left for manual review.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
import postcss from 'postcss';
|
|
20
|
+
import selectorParser from 'postcss-selector-parser';
|
|
21
|
+
import {migrateAstryxThemeSelectors} from '../v0.6.0/migrate-astryx-theme-selectors-to-data-attrs.mjs';
|
|
22
|
+
import {
|
|
23
|
+
V06_THEME_SELECTOR_DYNAMIC_AXES,
|
|
24
|
+
V06_THEME_SELECTOR_TARGETS,
|
|
25
|
+
} from './migrate-deprecated-theme-surface-v0.6-data.mjs';
|
|
26
|
+
|
|
27
|
+
/** @type {Readonly<Record<string, string>>} */
|
|
28
|
+
export const DEPRECATED_THEME_TARGETS = Object.freeze({
|
|
29
|
+
'base-table': 'table',
|
|
30
|
+
checkbox: 'checkbox-indicator',
|
|
31
|
+
codeblock: 'code-block',
|
|
32
|
+
'codeblock-copy-button': 'code-block-copy-button',
|
|
33
|
+
'codeblock-header': 'code-block-header',
|
|
34
|
+
'codeblock-title': 'code-block-title',
|
|
35
|
+
'date-input-clear-icon': 'input-clear-icon',
|
|
36
|
+
'date-range-input-clear-icon': 'input-clear-icon',
|
|
37
|
+
hovercard: 'hover-card',
|
|
38
|
+
'multi-selector-clear-icon': 'input-clear-icon',
|
|
39
|
+
navicon: 'nav-icon',
|
|
40
|
+
'popover-surface': 'popover',
|
|
41
|
+
progressbar: 'progress-bar',
|
|
42
|
+
'progressbar-fill': 'progress-bar-fill',
|
|
43
|
+
'progressbar-mark': 'progress-bar-mark',
|
|
44
|
+
'progressbar-track': 'progress-bar-track',
|
|
45
|
+
radio: 'radio-indicator',
|
|
46
|
+
'radio-dot': 'radio-indicator-dot',
|
|
47
|
+
'selector-clear-icon': 'input-clear-icon',
|
|
48
|
+
statusdot: 'status-dot',
|
|
49
|
+
textarea: 'text-area',
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Clear-icon aliases each styled one owner's clear glyph. The shared target
|
|
54
|
+
* styles every input's clear glyph, so CSS keeps the owner as an ancestor scope.
|
|
55
|
+
* @type {Readonly<Record<string, string>>}
|
|
56
|
+
*/
|
|
57
|
+
export const SCOPED_CLEAR_ICON_OWNERS = Object.freeze({
|
|
58
|
+
'date-input-clear-icon': 'date-input',
|
|
59
|
+
'date-range-input-clear-icon': 'date-range-input',
|
|
60
|
+
'multi-selector-clear-icon': 'multi-selector',
|
|
61
|
+
'selector-clear-icon': 'selector',
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const SELECTOR_DATA = {
|
|
65
|
+
targets: V06_THEME_SELECTOR_TARGETS,
|
|
66
|
+
dynamicAxes: V06_THEME_SELECTOR_DYNAMIC_AXES,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const meta = {
|
|
70
|
+
title: 'Migrate removed component theme aliases and selectors',
|
|
71
|
+
description:
|
|
72
|
+
'Renames all 21 removed component target aliases in static theme maps and CSS, and replaces target-qualified bare prop/state classes with reflected data attributes.',
|
|
73
|
+
fileExtensions: ['.tsx', '.ts', '.jsx', '.js', '.mjs', '.cjs', '.css'],
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const TODO_PREFIX = ' TODO(astryx upgrade): merge deprecated theme target ';
|
|
77
|
+
const SCOPE_TODO_PREFIX = ' TODO(astryx upgrade): ';
|
|
78
|
+
const KNOWN_SELECTOR_TARGETS = new Set([
|
|
79
|
+
...Object.keys(V06_THEME_SELECTOR_TARGETS),
|
|
80
|
+
...Object.keys(V06_THEME_SELECTOR_DYNAMIC_AXES),
|
|
81
|
+
]);
|
|
82
|
+
/** Every bare class token any 0.6 target emitted. */
|
|
83
|
+
const KNOWN_BARE_TOKENS = new Set(
|
|
84
|
+
Object.values(V06_THEME_SELECTOR_TARGETS).flatMap(tokens =>
|
|
85
|
+
Object.keys(tokens),
|
|
86
|
+
),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
/** @param {string} target @param {string} token */
|
|
90
|
+
function targetEmittedBareClass(target, token) {
|
|
91
|
+
if (V06_THEME_SELECTOR_TARGETS[target]?.[token] != null) return true;
|
|
92
|
+
return (V06_THEME_SELECTOR_DYNAMIC_AXES[target] ?? []).some(rule =>
|
|
93
|
+
rule.token === 'class-value'
|
|
94
|
+
? true
|
|
95
|
+
: typeof rule.prefix === 'string' &&
|
|
96
|
+
token.startsWith(rule.prefix) &&
|
|
97
|
+
/^\d/.test(token.slice(rule.prefix.length)),
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** @param {any} pseudo @returns {Set<string>} */
|
|
102
|
+
function compoundTargetsForPseudo(pseudo) {
|
|
103
|
+
const parent = pseudo.parent;
|
|
104
|
+
if (parent?.type !== 'selector' || !Array.isArray(parent.nodes)) {
|
|
105
|
+
return new Set();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const index = parent.nodes.indexOf(pseudo);
|
|
109
|
+
const compound = [];
|
|
110
|
+
for (let cursor = index - 1; cursor >= 0; cursor--) {
|
|
111
|
+
const node = parent.nodes[cursor];
|
|
112
|
+
if (node.type === 'combinator') break;
|
|
113
|
+
compound.push(node);
|
|
114
|
+
}
|
|
115
|
+
for (let cursor = index + 1; cursor < parent.nodes.length; cursor++) {
|
|
116
|
+
const node = parent.nodes[cursor];
|
|
117
|
+
if (node.type === 'combinator') break;
|
|
118
|
+
compound.push(node);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const targets = new Set();
|
|
122
|
+
for (const node of compound) {
|
|
123
|
+
if (node.type !== 'class' || !node.value.startsWith('astryx-')) continue;
|
|
124
|
+
const target = node.value.slice('astryx-'.length);
|
|
125
|
+
if (KNOWN_SELECTOR_TARGETS.has(target)) targets.add(target);
|
|
126
|
+
}
|
|
127
|
+
return targets;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** @param {any} selector */
|
|
131
|
+
function finishSelectorMigration(selector) {
|
|
132
|
+
selector.walkPseudos((/** @type {any} */ pseudo) => {
|
|
133
|
+
if (pseudo.value !== ':is' || !Array.isArray(pseudo.nodes)) return;
|
|
134
|
+
const targets = compoundTargetsForPseudo(pseudo);
|
|
135
|
+
if (targets.size === 0) return;
|
|
136
|
+
const hasAttributeArm = pseudo.nodes.some(
|
|
137
|
+
(/** @type {any} */ arm) =>
|
|
138
|
+
arm.type === 'selector' &&
|
|
139
|
+
arm.nodes.some((/** @type {any} */ node) => node.type === 'attribute'),
|
|
140
|
+
);
|
|
141
|
+
if (!hasAttributeArm) return;
|
|
142
|
+
|
|
143
|
+
for (const arm of [...pseudo.nodes]) {
|
|
144
|
+
if (arm.type !== 'selector' || arm.nodes.length !== 1) continue;
|
|
145
|
+
const only = arm.nodes[0];
|
|
146
|
+
if (
|
|
147
|
+
only.type !== 'class' ||
|
|
148
|
+
only.value.startsWith('astryx-') ||
|
|
149
|
+
![...targets].some(target => targetEmittedBareClass(target, only.value))
|
|
150
|
+
) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
arm.remove();
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
selector.walkClasses((/** @type {any} */ classNode) => {
|
|
158
|
+
if (!classNode.value.startsWith('astryx-')) return;
|
|
159
|
+
const oldKey = classNode.value.slice('astryx-'.length);
|
|
160
|
+
const newKey = DEPRECATED_THEME_TARGETS[oldKey];
|
|
161
|
+
if (!newKey) return;
|
|
162
|
+
classNode.value = `astryx-${newKey}`;
|
|
163
|
+
const owner = SCOPED_CLEAR_ICON_OWNERS[oldKey];
|
|
164
|
+
if (owner) classNode.parent.insertAfter(classNode, ownerScope(owner));
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* `:where(.astryx-<owner> *)` keeps the alias's owner scope without changing
|
|
170
|
+
* the selector's specificity.
|
|
171
|
+
* @param {string} owner
|
|
172
|
+
*/
|
|
173
|
+
function ownerScope(owner) {
|
|
174
|
+
return selectorParser.pseudo({
|
|
175
|
+
value: ':where',
|
|
176
|
+
nodes: [
|
|
177
|
+
selectorParser.selector({
|
|
178
|
+
value: '',
|
|
179
|
+
nodes: [
|
|
180
|
+
selectorParser.className({value: `astryx-${owner}`}),
|
|
181
|
+
selectorParser.combinator({value: ' '}),
|
|
182
|
+
selectorParser.universal({value: '*'}),
|
|
183
|
+
],
|
|
184
|
+
}),
|
|
185
|
+
],
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Bare classes that still qualify a known target after migration and that some
|
|
191
|
+
* 0.6 target emitted. They can no longer match Astryx output.
|
|
192
|
+
* @param {string} selectorText
|
|
193
|
+
* @returns {string[]}
|
|
194
|
+
*/
|
|
195
|
+
function unclassifiedBareClasses(selectorText) {
|
|
196
|
+
/** @type {Set<string>} */
|
|
197
|
+
const found = new Set();
|
|
198
|
+
selectorParser(selectors => {
|
|
199
|
+
selectors.walk((/** @type {any} */ node) => {
|
|
200
|
+
if (node.type !== 'selector') return;
|
|
201
|
+
/** @type {any[][]} */
|
|
202
|
+
const compounds = [[]];
|
|
203
|
+
for (const child of node.nodes) {
|
|
204
|
+
if (child.type === 'combinator') compounds.push([]);
|
|
205
|
+
else compounds[compounds.length - 1].push(child);
|
|
206
|
+
}
|
|
207
|
+
for (const compound of compounds) {
|
|
208
|
+
const hasTarget = compound.some(
|
|
209
|
+
child =>
|
|
210
|
+
child.type === 'class' &&
|
|
211
|
+
child.value.startsWith('astryx-') &&
|
|
212
|
+
KNOWN_SELECTOR_TARGETS.has(child.value.slice('astryx-'.length)),
|
|
213
|
+
);
|
|
214
|
+
if (!hasTarget) continue;
|
|
215
|
+
for (const child of compound) {
|
|
216
|
+
if (
|
|
217
|
+
child.type === 'class' &&
|
|
218
|
+
!child.value.startsWith('astryx-') &&
|
|
219
|
+
KNOWN_BARE_TOKENS.has(child.value)
|
|
220
|
+
) {
|
|
221
|
+
found.add(child.value);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
}).processSync(selectorText, {lossless: true});
|
|
227
|
+
return [...found].sort();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** @param {string} selector */
|
|
231
|
+
function finishSelectorText(selector) {
|
|
232
|
+
return selectorParser(selectors => {
|
|
233
|
+
selectors.each(finishSelectorMigration);
|
|
234
|
+
}).processSync(selector, {lossless: true});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** @param {string} params */
|
|
238
|
+
function finishScopeParams(params) {
|
|
239
|
+
let result = '';
|
|
240
|
+
let last = 0;
|
|
241
|
+
let depth = 0;
|
|
242
|
+
let groupStart = -1;
|
|
243
|
+
let quote = '';
|
|
244
|
+
let escaped = false;
|
|
245
|
+
|
|
246
|
+
for (let index = 0; index < params.length; index++) {
|
|
247
|
+
const char = params[index];
|
|
248
|
+
if (escaped) {
|
|
249
|
+
escaped = false;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (char === '\\') {
|
|
253
|
+
escaped = true;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (quote) {
|
|
257
|
+
if (char === quote) quote = '';
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (char === '"' || char === "'") {
|
|
261
|
+
quote = char;
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (char === '(') {
|
|
265
|
+
if (depth === 0) {
|
|
266
|
+
result += params.slice(last, index + 1);
|
|
267
|
+
groupStart = index + 1;
|
|
268
|
+
}
|
|
269
|
+
depth++;
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
if (char === ')' && depth > 0) {
|
|
273
|
+
depth--;
|
|
274
|
+
if (depth === 0 && groupStart >= 0) {
|
|
275
|
+
result += finishSelectorText(params.slice(groupStart, index));
|
|
276
|
+
result += ')';
|
|
277
|
+
last = index + 1;
|
|
278
|
+
groupStart = -1;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return result + params.slice(last);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** @param {string} source @param {string} from */
|
|
286
|
+
function migrateCss(source, from) {
|
|
287
|
+
const migrated = migrateAstryxThemeSelectors(source, from, SELECTOR_DATA);
|
|
288
|
+
const root = postcss.parse(migrated, {from});
|
|
289
|
+
/** @type {Set<string>} */
|
|
290
|
+
const existingComments = new Set();
|
|
291
|
+
root.walkComments(comment => {
|
|
292
|
+
existingComments.add(comment.text.trim());
|
|
293
|
+
});
|
|
294
|
+
root.walkRules(rule => {
|
|
295
|
+
rule.selector = finishSelectorText(rule.selector);
|
|
296
|
+
const tokens = unclassifiedBareClasses(rule.selector);
|
|
297
|
+
if (tokens.length === 0) return;
|
|
298
|
+
const list = tokens.map(token => `.${token}`).join(', ');
|
|
299
|
+
const text =
|
|
300
|
+
`${SCOPE_TODO_PREFIX}Astryx 0.7 no longer emits bare prop/state classes, and ${list} had no known 0.6 meaning on this target. Replace it with the target's data-* attribute or remove it.`.trim();
|
|
301
|
+
if (existingComments.has(text)) return;
|
|
302
|
+
rule.before(postcss.comment({text}));
|
|
303
|
+
existingComments.add(text);
|
|
304
|
+
});
|
|
305
|
+
root.walkAtRules('scope', atRule => {
|
|
306
|
+
atRule.params = finishScopeParams(atRule.params);
|
|
307
|
+
});
|
|
308
|
+
return root.toString();
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** @param {any} property @returns {string | null} */
|
|
312
|
+
function staticPropertyName(property) {
|
|
313
|
+
const key = property?.key;
|
|
314
|
+
if (!key) return null;
|
|
315
|
+
if (!property.computed && key.type === 'Identifier') return key.name;
|
|
316
|
+
if (
|
|
317
|
+
(key.type === 'StringLiteral' || key.type === 'Literal') &&
|
|
318
|
+
typeof key.value === 'string'
|
|
319
|
+
) {
|
|
320
|
+
return key.value;
|
|
321
|
+
}
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const ASTRYX_MODULE = /^@astryxdesign\//u;
|
|
326
|
+
const THEME_TYPE_NAMES = new Set([
|
|
327
|
+
'DefineThemeInput',
|
|
328
|
+
'DefinedTheme',
|
|
329
|
+
'ResolvedDefinedTheme',
|
|
330
|
+
]);
|
|
331
|
+
const TS_WRAPPERS = new Set([
|
|
332
|
+
'TSAsExpression',
|
|
333
|
+
'TSSatisfiesExpression',
|
|
334
|
+
'TSNonNullExpression',
|
|
335
|
+
'TSTypeAssertion',
|
|
336
|
+
'TypeCastExpression',
|
|
337
|
+
'ParenthesizedExpression',
|
|
338
|
+
]);
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Local names that refer to Astryx's `defineTheme`, its theme types, or an
|
|
342
|
+
* Astryx namespace import.
|
|
343
|
+
* @param {any} root @param {any} j
|
|
344
|
+
*/
|
|
345
|
+
function astryxThemeBindings(root, j) {
|
|
346
|
+
const bindings = {
|
|
347
|
+
defineTheme: new Set(),
|
|
348
|
+
themeTypes: new Set(),
|
|
349
|
+
namespaces: new Set(),
|
|
350
|
+
};
|
|
351
|
+
root.find(j.ImportDeclaration).forEach((/** @type {any} */ importPath) => {
|
|
352
|
+
const source = importPath.node.source?.value;
|
|
353
|
+
if (typeof source !== 'string' || !ASTRYX_MODULE.test(source)) return;
|
|
354
|
+
for (const specifier of importPath.node.specifiers ?? []) {
|
|
355
|
+
const local = specifier.local?.name;
|
|
356
|
+
if (!local) continue;
|
|
357
|
+
if (specifier.type === 'ImportNamespaceSpecifier') {
|
|
358
|
+
bindings.namespaces.add(local);
|
|
359
|
+
} else if (specifier.type === 'ImportSpecifier') {
|
|
360
|
+
const imported = specifier.imported?.name ?? specifier.imported?.value;
|
|
361
|
+
if (imported === 'defineTheme') bindings.defineTheme.add(local);
|
|
362
|
+
if (THEME_TYPE_NAMES.has(imported)) bindings.themeTypes.add(local);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
return bindings;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** @param {any} name @param {Set<string>} locals @param {Set<string>} namespaces @param {Set<string>} members */
|
|
370
|
+
function namesBinding(name, locals, namespaces, members) {
|
|
371
|
+
if (name?.type === 'Identifier') return locals.has(name.name);
|
|
372
|
+
const object = name?.object ?? name?.left;
|
|
373
|
+
const property = name?.property ?? name?.right;
|
|
374
|
+
return (
|
|
375
|
+
object?.type === 'Identifier' &&
|
|
376
|
+
namespaces.has(object.name) &&
|
|
377
|
+
property?.type === 'Identifier' &&
|
|
378
|
+
members.has(property.name)
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** @param {any} typeNode @param {ReturnType<typeof astryxThemeBindings>} bindings */
|
|
383
|
+
function isAstryxThemeType(typeNode, bindings) {
|
|
384
|
+
const node = typeNode?.typeAnnotation ?? typeNode;
|
|
385
|
+
return (
|
|
386
|
+
node?.type === 'TSTypeReference' &&
|
|
387
|
+
namesBinding(
|
|
388
|
+
node.typeName,
|
|
389
|
+
bindings.themeTypes,
|
|
390
|
+
bindings.namespaces,
|
|
391
|
+
THEME_TYPE_NAMES,
|
|
392
|
+
)
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* True for a theme object whose shape alone identifies it: a static string
|
|
398
|
+
* `name` beside a `components` map whose entries are all style objects.
|
|
399
|
+
* @param {any} node
|
|
400
|
+
*/
|
|
401
|
+
function isDirectThemeObject(node) {
|
|
402
|
+
const properties = node.properties ?? [];
|
|
403
|
+
const name = properties.find(
|
|
404
|
+
(/** @type {any} */ property) => staticPropertyName(property) === 'name',
|
|
405
|
+
);
|
|
406
|
+
if (!(
|
|
407
|
+
(name?.value?.type === 'StringLiteral' ||
|
|
408
|
+
name?.value?.type === 'Literal') &&
|
|
409
|
+
typeof name.value.value === 'string'
|
|
410
|
+
)) {
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
const components = properties.find(
|
|
414
|
+
(/** @type {any} */ property) =>
|
|
415
|
+
staticPropertyName(property) === 'components',
|
|
416
|
+
);
|
|
417
|
+
const map = components?.value;
|
|
418
|
+
if (map?.type === 'Identifier') return true;
|
|
419
|
+
return (
|
|
420
|
+
map?.type === 'ObjectExpression' &&
|
|
421
|
+
map.properties.every(
|
|
422
|
+
(/** @type {any} */ entry) =>
|
|
423
|
+
entry.type === 'SpreadElement' ||
|
|
424
|
+
entry.value?.type === 'ObjectExpression',
|
|
425
|
+
)
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* True when this object literal is an Astryx theme: the argument of an
|
|
431
|
+
* imported `defineTheme()`, typed as an Astryx theme, or a direct theme object.
|
|
432
|
+
* @param {any} objectPath @param {ReturnType<typeof astryxThemeBindings>} bindings
|
|
433
|
+
*/
|
|
434
|
+
function isThemeRoot(objectPath, bindings) {
|
|
435
|
+
let child = objectPath;
|
|
436
|
+
let parent = objectPath.parent;
|
|
437
|
+
while (parent != null && TS_WRAPPERS.has(parent.node.type)) {
|
|
438
|
+
if (isAstryxThemeType(parent.node.typeAnnotation, bindings)) return true;
|
|
439
|
+
child = parent;
|
|
440
|
+
parent = parent.parent;
|
|
441
|
+
}
|
|
442
|
+
const node = parent?.node;
|
|
443
|
+
if (
|
|
444
|
+
node?.type === 'CallExpression' &&
|
|
445
|
+
node.arguments?.[0] === child.node &&
|
|
446
|
+
namesBinding(
|
|
447
|
+
node.callee,
|
|
448
|
+
bindings.defineTheme,
|
|
449
|
+
bindings.namespaces,
|
|
450
|
+
new Set(['defineTheme']),
|
|
451
|
+
)
|
|
452
|
+
) {
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
if (
|
|
456
|
+
node?.type === 'VariableDeclarator' &&
|
|
457
|
+
isAstryxThemeType(node.id?.typeAnnotation, bindings)
|
|
458
|
+
) {
|
|
459
|
+
return true;
|
|
460
|
+
}
|
|
461
|
+
return isDirectThemeObject(objectPath.node);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Walk outward from the object that owns a `components` property, through
|
|
466
|
+
* nested theme sections such as `onDark` and `adaptations.rules[].value`,
|
|
467
|
+
* until reaching a theme root. Leaving literal object/array structure ends
|
|
468
|
+
* the search.
|
|
469
|
+
* @param {any} ownerPath @param {ReturnType<typeof astryxThemeBindings>} bindings
|
|
470
|
+
*/
|
|
471
|
+
function hasThemeContext(ownerPath, bindings) {
|
|
472
|
+
for (let current = ownerPath; current != null;) {
|
|
473
|
+
if (current.node.type === 'ObjectExpression') {
|
|
474
|
+
if (isThemeRoot(current, bindings)) return true;
|
|
475
|
+
}
|
|
476
|
+
let parent = current.parent;
|
|
477
|
+
while (parent != null && TS_WRAPPERS.has(parent.node.type)) {
|
|
478
|
+
parent = parent.parent;
|
|
479
|
+
}
|
|
480
|
+
const type = parent?.node.type;
|
|
481
|
+
if (type === 'ObjectProperty' || type === 'Property') {
|
|
482
|
+
current = parent.parent;
|
|
483
|
+
} else if (type === 'ArrayExpression') {
|
|
484
|
+
current = parent;
|
|
485
|
+
} else {
|
|
486
|
+
return false;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return false;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** @param {any} objectPath @param {any} root @param {any} j @param {ReturnType<typeof astryxThemeBindings>} bindings */
|
|
493
|
+
function componentsVariableFeedsTheme(objectPath, root, j, bindings) {
|
|
494
|
+
const parent = objectPath.parent?.node;
|
|
495
|
+
if (
|
|
496
|
+
parent?.type !== 'VariableDeclarator' ||
|
|
497
|
+
parent.id?.type !== 'Identifier' ||
|
|
498
|
+
parent.id.name !== 'components'
|
|
499
|
+
) {
|
|
500
|
+
return false;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const bindingScope = objectPath.scope.lookup('components');
|
|
504
|
+
let feedsTheme = false;
|
|
505
|
+
root.find(j.ObjectProperty).forEach((/** @type {any} */ propertyPath) => {
|
|
506
|
+
if (feedsTheme) return;
|
|
507
|
+
const property = propertyPath.node;
|
|
508
|
+
if (
|
|
509
|
+
staticPropertyName(property) !== 'components' ||
|
|
510
|
+
property.value?.type !== 'Identifier' ||
|
|
511
|
+
property.value.name !== 'components'
|
|
512
|
+
) {
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
if (
|
|
516
|
+
bindingScope != null &&
|
|
517
|
+
propertyPath.scope.lookup('components') !== bindingScope
|
|
518
|
+
) {
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
if (hasThemeContext(propertyPath.parent, bindings)) feedsTheme = true;
|
|
522
|
+
});
|
|
523
|
+
return feedsTheme;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/** @param {any} path @param {any} root @param {any} j @param {ReturnType<typeof astryxThemeBindings>} bindings */
|
|
527
|
+
function isComponentsMap(path, root, j, bindings) {
|
|
528
|
+
let parentPath = path.parent;
|
|
529
|
+
while (parentPath != null && TS_WRAPPERS.has(parentPath.node.type)) {
|
|
530
|
+
parentPath = parentPath.parent;
|
|
531
|
+
}
|
|
532
|
+
const parent = parentPath?.node;
|
|
533
|
+
if (
|
|
534
|
+
(parent?.type === 'ObjectProperty' || parent?.type === 'Property') &&
|
|
535
|
+
staticPropertyName(parent) === 'components'
|
|
536
|
+
) {
|
|
537
|
+
return hasThemeContext(parentPath.parent, bindings);
|
|
538
|
+
}
|
|
539
|
+
return componentsVariableFeedsTheme(path, root, j, bindings);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/** @param {any} property @param {string} oldKey @param {string} newKey @param {any} j */
|
|
543
|
+
function addCollisionTodo(property, oldKey, newKey, j) {
|
|
544
|
+
const message = `${TODO_PREFIX}\`${oldKey}\` into \`${newKey}\`; both keys are present. `;
|
|
545
|
+
if (
|
|
546
|
+
property.comments?.some(
|
|
547
|
+
(/** @type {any} */ comment) => comment.value === message,
|
|
548
|
+
)
|
|
549
|
+
)
|
|
550
|
+
return;
|
|
551
|
+
property.comments = [
|
|
552
|
+
...(property.comments ?? []),
|
|
553
|
+
j.commentBlock(message, true, false),
|
|
554
|
+
];
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* A theme key cannot carry an ancestor scope, so a renamed clear-icon alias now
|
|
559
|
+
* styles every input's clear icon. Say so where the rename happened.
|
|
560
|
+
* @param {any} property @param {string} oldKey @param {string} owner @param {any} j
|
|
561
|
+
*/
|
|
562
|
+
function addScopeTodo(property, oldKey, owner, j) {
|
|
563
|
+
const message = `${SCOPE_TODO_PREFIX}\`${oldKey}\` became \`input-clear-icon\`, which styles the clear icon of every input. To keep these styles on one component, move them to CSS under \`.astryx-input-clear-icon:where(.astryx-${owner} *)\`. `;
|
|
564
|
+
property.comments = [
|
|
565
|
+
...(property.comments ?? []),
|
|
566
|
+
j.commentBlock(message, true, false),
|
|
567
|
+
];
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/** @param {string} source @param {any} j */
|
|
571
|
+
function migrateStaticThemeKeys(source, j) {
|
|
572
|
+
const root = j(source);
|
|
573
|
+
const bindings = astryxThemeBindings(root, j);
|
|
574
|
+
let changed = false;
|
|
575
|
+
|
|
576
|
+
root.find(j.ObjectExpression).forEach((/** @type {any} */ objectPath) => {
|
|
577
|
+
if (!isComponentsMap(objectPath, root, j, bindings)) return;
|
|
578
|
+
const properties = objectPath.node.properties ?? [];
|
|
579
|
+
const staticKeys = new Set(
|
|
580
|
+
properties.map(staticPropertyName).filter(Boolean),
|
|
581
|
+
);
|
|
582
|
+
|
|
583
|
+
for (const property of properties) {
|
|
584
|
+
if (property.type !== 'ObjectProperty' && property.type !== 'Property') {
|
|
585
|
+
continue;
|
|
586
|
+
}
|
|
587
|
+
const oldKey = staticPropertyName(property);
|
|
588
|
+
const newKey = oldKey ? DEPRECATED_THEME_TARGETS[oldKey] : undefined;
|
|
589
|
+
if (!oldKey || !newKey) continue;
|
|
590
|
+
|
|
591
|
+
if (staticKeys.has(newKey)) {
|
|
592
|
+
const before = property.comments?.length ?? 0;
|
|
593
|
+
addCollisionTodo(property, oldKey, newKey, j);
|
|
594
|
+
if ((property.comments?.length ?? 0) !== before) changed = true;
|
|
595
|
+
continue;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
property.key = j.stringLiteral(newKey);
|
|
599
|
+
property.computed = false;
|
|
600
|
+
property.shorthand = false;
|
|
601
|
+
const owner = SCOPED_CLEAR_ICON_OWNERS[oldKey];
|
|
602
|
+
if (owner) addScopeTodo(property, oldKey, owner, j);
|
|
603
|
+
staticKeys.delete(oldKey);
|
|
604
|
+
staticKeys.add(newKey);
|
|
605
|
+
changed = true;
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
return changed ? root.toSource({quote: 'single'}) : undefined;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* @param {import('../../../../authoring/codemod/type').AstryxCodemodFile} file
|
|
614
|
+
* @param {import('../../../../authoring/codemod/type').CodemodTransformApi} api
|
|
615
|
+
* @returns {string | null | undefined}
|
|
616
|
+
*/
|
|
617
|
+
export default function migrateDeprecatedThemeSurface(file, api) {
|
|
618
|
+
const extension = path.extname(file.path).toLowerCase();
|
|
619
|
+
if (extension === '.css') {
|
|
620
|
+
const result = migrateCss(file.source, file.path);
|
|
621
|
+
return result === file.source ? undefined : result;
|
|
622
|
+
}
|
|
623
|
+
return migrateStaticThemeKeys(file.source, api.jscodeshift);
|
|
624
|
+
}
|