@astryxdesign/cli 0.7.5-canary.d26cdf3 → 0.7.5-canary.d6004fd
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/api/template/tokenizer-showcase.test.mjs +47 -0
- package/api/theme/build/build.mjs +9 -4
- 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/docs/author-a-theme.doc.mjs +2 -1
- package/assets/docs/icons.doc.mjs +24 -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/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/Thumbnail/ThumbnailDisabled.doc.mjs +1 -1
- package/assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx +13 -6
- package/assets/templates/pages/dashboard-alert-rail/page.tsx +3 -3
- package/assets/templates/pages/table-comparison/page.tsx +12 -3
- package/assets/templates/themes/butter/butterPaletteRefs.generated.ts +3 -2
- package/assets/templates/themes/butter/butterTheme.ts +47 -44
- package/assets/templates/themes/chocolate/chocolateTheme.ts +15 -2
- package/assets/templates/themes/gothic/gothicTheme.ts +21 -16
- package/assets/templates/themes/matcha/matchaPaletteRefs.generated.ts +1 -1
- package/assets/templates/themes/matcha/matchaTheme.ts +15 -2
- package/assets/templates/themes/neutral/neutralPaletteRefs.generated.ts +4 -4
- package/assets/templates/themes/neutral/neutralTheme.ts +24 -119
- package/assets/templates/themes/stone/stoneTheme.ts +46 -21
- package/assets/templates/themes/y2k/y2kTheme.ts +20 -16
- package/assets/theme.template.ts +9 -5
- package/clients/cli/commands/build-theme.adaptations.test.mjs +110 -0
- package/foundation/agent-docs/agent-docs.mjs +1 -0
- package/foundation/agent-docs/agent-docs.test.mjs +5 -0
- package/package.json +9 -9
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Contract test for the Tokenizer showcase block.
|
|
5
|
+
* @input The shipped TokenizerShowcase block source.
|
|
6
|
+
* @output Guards the block's search source: typing in the showcase must list
|
|
7
|
+
* matching tags rather than an empty menu.
|
|
8
|
+
* @position CLI template regression coverage; runs in the node test project.
|
|
9
|
+
* The block is the hero example of the Tokenizer docs page. Like the other
|
|
10
|
+
* template contract tests here it reads the block's source instead of
|
|
11
|
+
* importing it, so it needs no built `@astryxdesign/core`.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import {fileURLToPath} from 'node:url';
|
|
17
|
+
import {describe, expect, it} from 'vitest';
|
|
18
|
+
|
|
19
|
+
const sourcePath = path.resolve(
|
|
20
|
+
path.dirname(fileURLToPath(import.meta.url)),
|
|
21
|
+
'../../assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx',
|
|
22
|
+
);
|
|
23
|
+
const source = fs.readFileSync(sourcePath, 'utf8');
|
|
24
|
+
|
|
25
|
+
// Evaluate the block's tag list and search source on their own, with the
|
|
26
|
+
// type annotations removed, so the test exercises the shipped logic.
|
|
27
|
+
function loadSearchSource() {
|
|
28
|
+
const tags = source.match(/const tags[^=]*=\s*(\[[\s\S]*?\n\]);/)?.[1];
|
|
29
|
+
const searchSource = source.match(
|
|
30
|
+
/const source[^=]*=\s*(\{[\s\S]*?\n\});/,
|
|
31
|
+
)?.[1];
|
|
32
|
+
if (tags == null || searchSource == null) {
|
|
33
|
+
throw new Error('TokenizerShowcase no longer declares `tags` and `source`');
|
|
34
|
+
}
|
|
35
|
+
const untyped = searchSource.replace(/\((\w+):\s*string\)/g, '($1)');
|
|
36
|
+
return new Function(`const tags = ${tags}; return ${untyped};`)();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe('Tokenizer showcase block', () => {
|
|
40
|
+
it('lists the tags that match a typed query, ignoring case', async () => {
|
|
41
|
+
const {search, bootstrap} = loadSearchSource();
|
|
42
|
+
const labels = async query => (await search(query)).map(tag => tag.label);
|
|
43
|
+
expect(await labels('mar')).toEqual(['Marketing']);
|
|
44
|
+
expect(await labels('A')).toEqual(['Marketing', 'Research', 'Sales']);
|
|
45
|
+
expect((await bootstrap()).length).toBeGreaterThan(0);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -2870,10 +2870,15 @@ async function themeBuildInternal(
|
|
|
2870
2870
|
/** @type {string[]} */
|
|
2871
2871
|
const droppedDeclarations = [];
|
|
2872
2872
|
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
droppedDeclarations
|
|
2876
|
-
)
|
|
2873
|
+
let themeRules;
|
|
2874
|
+
try {
|
|
2875
|
+
themeRules = _generateThemeRulesSplit(resolvedTheme, droppedDeclarations);
|
|
2876
|
+
} catch (error) {
|
|
2877
|
+
const message =
|
|
2878
|
+
error instanceof Error ? error.message : 'Theme normalization failed.';
|
|
2879
|
+
throw new AstryxError(message, undefined, ERROR_CODES.ERR_THEME_INVALID);
|
|
2880
|
+
}
|
|
2881
|
+
const {component, prose} = themeRules;
|
|
2877
2882
|
const cssParts = [];
|
|
2878
2883
|
// Prose element defaults always ship — the `<Theme>` runtime
|
|
2879
2884
|
// (generateThemeCSS) always emits them, so the build must too, or the
|
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
|
+
}
|
|
@@ -57,7 +57,8 @@ export const docs = {
|
|
|
57
57
|
['icons', 'Shallow-merged: child icons override matching names from the base.'],
|
|
58
58
|
['indicators', 'Shallow-merged: child indicators override matching names from the base.'],
|
|
59
59
|
['onDark, onLight', "Deep-merged per surface: the base's resolved surface first, then the child's overrides."],
|
|
60
|
-
['
|
|
60
|
+
['motion', "Field by field: the child's `fast`, `medium` and `ratio` regenerate the fast and medium duration tokens; `slow` and `easing` left unset keep the base theme's tokens, so an inherited slow band stays at the base's ratio."],
|
|
61
|
+
['typography, radius, color', 'Child config replaces base entirely (these are scale inputs, not additive).'],
|
|
61
62
|
['adaptations', 'Width-breakpoint overrides merge by fixed name. Inherited ordered rules keep their relative order; child rules append and re-resolve against the child root axes.'],
|
|
62
63
|
]},
|
|
63
64
|
{type: 'prose', text: "Inheritance is resolved when the theme is defined, so an extended theme is flat: `{@command theme build}` emits one self-contained stylesheet holding everything the child inherited, and the base theme's CSS does not need to be loaded next to it. A base that is not a theme (most often an import that missed) throws when `defineTheme` runs, rather than producing a theme that silently inherits nothing."},
|
|
@@ -101,11 +101,34 @@ export const brandTheme = defineTheme({
|
|
|
101
101
|
},
|
|
102
102
|
});`,
|
|
103
103
|
},
|
|
104
|
+
{
|
|
105
|
+
type: 'prose',
|
|
106
|
+
text: "An entry can also be a function of the request, so the theme maps Icon's size to its icon library's own props or artwork. `Icon` calls it with `{size, variant}`, where `size` is the size it renders at (`xsm`, `sm`, `md`, or `lg`: an explicit `size`, else the owning component's default, else `md`). Reads that return one node — `getIcon()`, `useIcon()`, `getIconRegistry()`, and the theme's `icons` field — call it with `{size: 'md'}`. The same entry shape works in `registerIcons()`.",
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
type: 'code',
|
|
110
|
+
lang: 'tsx',
|
|
111
|
+
label: 'A size-aware icon entry',
|
|
112
|
+
code: `import {defineTheme} from '@astryxdesign/core/theme';
|
|
113
|
+
import {Info} from 'lucide-react';
|
|
114
|
+
|
|
115
|
+
export const brandTheme = defineTheme({
|
|
116
|
+
name: 'brand',
|
|
117
|
+
icons: {
|
|
118
|
+
// A heavier stroke keeps the small size legible.
|
|
119
|
+
info: ({size}) => <Info strokeWidth={size === 'sm' || size === 'xsm' ? 2.5 : 2} />,
|
|
120
|
+
},
|
|
121
|
+
});`,
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
type: 'prose',
|
|
125
|
+
text: 'Keep the function pure and synchronous: it computes its result from the request only, with no hooks, DOM or SVG measurement, browser globals, network access, mutable module state, or side effects. Its result is final. Returning `null` draws no glyph instead of falling back to a registered or built-in icon, while a `null` or `undefined` node entry still falls back. A child theme that sets an entry for a key replaces the inherited entry for that key.',
|
|
126
|
+
},
|
|
104
127
|
],
|
|
105
128
|
},
|
|
106
129
|
{
|
|
107
130
|
title: 'Component and Library Icons',
|
|
108
|
-
|
|
131
|
+
category: 'foundations',
|
|
109
132
|
content: [
|
|
110
133
|
{
|
|
111
134
|
type: 'prose',
|