@astryxdesign/cli 0.7.17 → 0.7.18-canary.g57306fd
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/api/discover/_adapter.d.mts +7 -5
- package/api/discover/_adapter.mjs +12 -11
- package/api/docs/_adapter.d.mts +19 -2
- package/api/docs/_adapter.mjs +66 -5
- package/api/doctor/_adapter.d.mts +11 -62
- package/api/doctor/_adapter.mjs +242 -472
- package/api/doctor/doctor.d.mts +1 -1
- package/api/doctor/doctor.mjs +3 -3
- package/api/doctor/theme-checks.mjs +37 -45
- package/api/error.d.mts +21 -0
- package/api/error.mjs +89 -0
- package/api/hook/list/list.d.mts +1 -1
- package/api/icon/_adapter.d.mts +8 -0
- package/api/icon/_adapter.mjs +17 -0
- package/api/icon/_authoring.mjs +6 -6
- package/api/icon/_default-look.mjs +16 -16
- package/api/icon/add/add.mjs +3 -3
- package/api/icon/exportIconIndex.doc.mjs +2 -2
- package/api/icon/icon.doc.mjs +2 -2
- package/api/icon/icon.type.d.mts +4 -0
- package/api/icon/icon.type.mjs +2 -1
- package/api/icon/search/icon-index.mjs +16 -3
- package/api/icon/search/search.d.mts +15 -5
- package/api/icon/search/search.mjs +74 -16
- package/api/icon/use/use.mjs +4 -4
- package/api/render/_adapter.mjs +233 -40
- package/api/render/render.type.d.mts +1 -1
- package/api/render/render.type.mjs +1 -1
- package/api/search/ORDERING.md +2 -2
- package/api/search/_adapter.mjs +4 -0
- package/api/search/_candidate-score.mjs +32 -8
- package/api/search/_cards.json +1 -1
- package/api/search/_compound.d.mts +0 -1
- package/api/search/_compound.mjs +93 -2
- package/api/search/_icons.d.mts +4 -2
- package/api/search/_icons.mjs +4 -2
- package/api/search/_lexical.mjs +177 -17
- package/api/search/_order.d.mts +3 -1
- package/api/search/_order.mjs +16 -5
- package/api/search/_result.d.mts +9 -0
- package/api/search/_result.mjs +10 -0
- package/api/search/_score.mjs +21 -3
- package/api/swizzle/_moduleGraph.d.mts +8 -0
- package/api/swizzle/_moduleGraph.mjs +61 -3
- package/api/template/copy/copy.mjs +5 -3
- package/api/theme/_adapter.mjs +48 -12
- package/api/theme/build/build.d.mts +4 -0
- package/api/theme/build/build.mjs +69 -18
- package/api/theme/build/style-keys.mjs +4 -5
- package/api/theme/build/theme-object.mjs +5 -0
- package/api/theme/palette/generate/color.d.mts +47 -0
- package/api/theme/palette/generate/color.mjs +58 -1
- package/api/theme/palette/generate/derive-look.d.mts +293 -0
- package/api/theme/palette/generate/derive-look.mjs +1362 -0
- package/api/theme/palette/generate/editor-look.d.mts +62 -0
- package/api/theme/palette/generate/editor-look.mjs +550 -0
- package/api/theme/palette/generate/mapping.d.mts +94 -9
- package/api/theme/palette/generate/mapping.mjs +94 -22
- package/api/theme-edit/_adapter.mjs +13 -2
- package/api/theme-edit/_catalog.d.mts +13 -13
- package/api/theme-edit/_catalog.mjs +66 -28
- package/api/theme-edit/_check.d.mts +82 -6
- package/api/theme-edit/_check.mjs +1270 -238
- package/api/theme-edit/_color.d.mts +74 -0
- package/api/theme-edit/_color.mjs +168 -3
- package/api/theme-edit/_compiler.d.mts +3 -5
- package/api/theme-edit/_compiler.mjs +122 -23
- package/api/theme-edit/_context.d.mts +55 -12
- package/api/theme-edit/_context.mjs +114 -60
- package/api/theme-edit/_css.d.mts +11 -0
- package/api/theme-edit/_css.mjs +40 -0
- package/api/theme-edit/_edit.d.mts +15 -2
- package/api/theme-edit/_edit.mjs +1469 -133
- package/api/theme-edit/_how-commands.d.mts +116 -0
- package/api/theme-edit/_how-commands.mjs +227 -115
- package/api/theme-edit/_how-question.d.mts +74 -29
- package/api/theme-edit/_how-question.mjs +8514 -1256
- package/api/theme-edit/_how-sides.d.mts +22 -0
- package/api/theme-edit/_how-sides.mjs +173 -0
- package/api/theme-edit/_how-steps.d.mts +42 -0
- package/api/theme-edit/_how-steps.mjs +208 -0
- package/api/theme-edit/_how-values.d.mts +29 -0
- package/api/theme-edit/_how-values.mjs +111 -0
- package/api/theme-edit/_how-words.d.mts +15 -0
- package/api/theme-edit/_how-words.mjs +239 -0
- package/api/theme-edit/_how.d.mts +1 -1
- package/api/theme-edit/_how.mjs +422 -37
- package/api/theme-edit/_missed.d.mts +2 -1
- package/api/theme-edit/_missed.mjs +125 -13
- package/api/theme-edit/_normal.d.mts +5 -3
- package/api/theme-edit/_normal.mjs +54 -14
- package/api/theme-edit/_palette.d.mts +204 -4
- package/api/theme-edit/_palette.mjs +901 -25
- package/api/theme-edit/_part-pieces.d.mts +54 -0
- package/api/theme-edit/_part-pieces.mjs +179 -0
- package/api/theme-edit/_part-words.mjs +33 -5
- package/api/theme-edit/_parts.d.mts +133 -0
- package/api/theme-edit/_parts.mjs +599 -10
- package/api/theme-edit/_picture-traits.d.mts +259 -0
- package/api/theme-edit/_picture-traits.mjs +1030 -0
- package/api/theme-edit/_preview.d.mts +95 -4
- package/api/theme-edit/_preview.mjs +284 -31
- package/api/theme-edit/_project.d.mts +14 -4
- package/api/theme-edit/_project.mjs +95 -26
- package/api/theme-edit/_resolved.d.mts +46 -0
- package/api/theme-edit/_resolved.mjs +800 -0
- package/api/theme-edit/_set-line.d.mts +29 -0
- package/api/theme-edit/_set-line.mjs +100 -0
- package/api/theme-edit/_settings.d.mts +625 -28
- package/api/theme-edit/_settings.mjs +4867 -888
- package/api/theme-edit/_tables.d.mts +25 -1
- package/api/theme-edit/_tables.mjs +115 -11
- package/api/theme-edit/_text-looks.d.mts +78 -0
- package/api/theme-edit/_text-looks.mjs +376 -0
- package/api/theme-edit/_vet.d.mts +3 -26
- package/api/theme-edit/_vet.mjs +5 -89
- package/api/theme-edit/how/how.d.mts +1 -1
- package/api/theme-edit/how/how.mjs +1 -1
- package/api/theme-edit/show/show.d.mts +4 -3
- package/api/theme-edit/show/show.mjs +3 -2
- package/api/theme-edit/theme-edit.type.d.mts +117 -1
- package/api/theme-edit/theme-edit.type.mjs +46 -3
- package/api/theme-edit/themeEditHow.doc.mjs +3 -3
- package/api/theme-edit/themeEditNew.doc.mjs +4 -0
- package/api/theme-edit/themeEditSet.doc.mjs +6 -2
- package/api/theme-edit/themeEditShow.doc.mjs +22 -3
- package/api/theme-edit/themeEditUndo.doc.mjs +4 -0
- package/assets/component-list.generated.json +1 -1
- package/assets/docs/author-a-theme.doc.mjs +7 -4
- package/assets/docs/theme.doc.dense.mjs +10 -0
- package/assets/docs/theme.doc.mjs +10 -0
- package/assets/docs/theme.doc.zh.mjs +10 -0
- package/assets/docs/tree/export-pages.doc.mjs +1 -1
- package/assets/docs/tree/icon-sets.doc.mjs +1 -1
- package/assets/docs/use-a-theme.doc.mjs +89 -17
- package/assets/docs-routes.generated.json +1 -1
- package/assets/docs.generated.json +2 -2
- package/assets/icons/definitions/article.doc.mjs +9 -0
- package/assets/icons/template-icons.json +5 -0
- package/assets/search-catalog.generated.json +2 -2
- package/assets/self-audit.generated.json +1 -1
- package/assets/templates/blocks/components/Citation/CitationInlineText.doc.mjs +1 -1
- package/assets/templates/blocks/components/Citation/CitationInlineText.tsx +3 -1
- package/assets/templates/blocks/components/Citation/CitationShowcase.doc.mjs +1 -1
- package/assets/templates/blocks/components/Citation/CitationShowcase.tsx +3 -1
- package/assets/templates/blocks/components/Citation/CitationSourceList.doc.mjs +1 -1
- package/assets/templates/blocks/components/Citation/CitationSourceList.tsx +7 -3
- package/assets/templates/blocks/components/Markdown/MarkdownCitedContent.doc.mjs +1 -1
- package/assets/templates/blocks/components/Markdown/MarkdownCitedContent.tsx +5 -2
- package/assets/templates/components.generated.json +1 -1
- package/assets/templates/pages/ide/page.tsx +5 -10
- package/assets/templates/pages/payment-form/page.tsx +16 -60
- package/assets/templates/themes/neutral/neutralTheme.ts +4 -3
- package/authoring/debug/parse.d.mts +1 -1
- package/authoring/doctypes/_schema.d.mts +13 -13
- package/authoring/gap-report/parse.d.mts +1 -1
- package/clients/cli/build-id.generated.json +1 -1
- package/clients/cli/commands/ensure-core-built.mjs +17 -10
- package/clients/cli/commands/experimental/icon.mjs +7 -7
- package/clients/cli/commands/experimental/theme.mjs +175 -13
- package/clients/cli/commands/experimental-icon.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +11 -3
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +3 -3
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +15 -5
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
- package/clients/cli/commands/hook.mjs +7 -0
- package/clients/cli/commands/theme-build.doc.mjs +1 -1
- package/clients/cli/index.mjs +6 -2
- package/clients/cli/lib/hook-format.mjs +41 -1
- package/clients/cli/lib/json-shim.mjs +34 -14
- package/clients/cli/lib/other-cli-words.mjs +7 -0
- package/clients/cli/lib/unknown-command.mjs +345 -46
- package/clients/cli/manifest.generated.json +1 -1
- package/foundation/agent-docs/agent-docs.d.mts +11 -2
- package/foundation/agent-docs/agent-docs.mjs +51 -8
- package/foundation/config/project.d.mts +11 -1
- package/foundation/config/project.mjs +11 -2
- package/foundation/discovery/component-replacement.d.mts +5 -8
- package/foundation/discovery/component-replacement.mjs +12 -14
- package/foundation/discovery/core-augmentation-points.d.mts +35 -0
- package/foundation/discovery/core-augmentation-points.mjs +410 -9
- package/foundation/discovery/core-elements.d.mts +6 -0
- package/foundation/discovery/core-elements.mjs +10 -2
- package/foundation/discovery/core-prop-types.d.mts +5 -0
- package/foundation/discovery/core-prop-types.mjs +10 -3
- package/foundation/discovery/core-props.d.mts +8 -0
- package/foundation/discovery/core-props.mjs +29 -13
- package/foundation/discovery/icon-ranking.d.mts +5 -2
- package/foundation/discovery/icon-ranking.mjs +101 -18
- package/foundation/discovery/icon-set-checks.mjs +1 -1
- package/foundation/discovery/icon-set-writer.mjs +1 -1
- package/foundation/discovery/official-list.d.mts +17 -0
- package/foundation/discovery/official-list.mjs +77 -0
- package/foundation/discovery/unknown-core-props.d.mts +130 -0
- package/foundation/discovery/unknown-core-props.mjs +601 -0
- package/foundation/discovery/written-prop-values.d.mts +35 -0
- package/foundation/discovery/written-prop-values.mjs +258 -0
- package/foundation/fonts/fontsource-families.generated.d.mts +5 -0
- package/foundation/fonts/fontsource-families.generated.mjs +2110 -0
- package/foundation/fs/package-name.d.mts +10 -0
- package/foundation/fs/package-name.mjs +37 -0
- package/foundation/fs/project-source-files.d.mts +27 -0
- package/foundation/fs/project-source-files.mjs +115 -0
- package/foundation/fs/project-sources.d.mts +25 -0
- package/foundation/fs/project-sources.mjs +83 -0
- package/foundation/fs/read-record.d.mts +18 -0
- package/foundation/fs/read-record.mjs +36 -2
- package/foundation/integrations/integration-warnings.d.mts +39 -7
- package/foundation/integrations/integration-warnings.mjs +141 -19
- package/foundation/integrations/missing-dependency.d.mts +11 -0
- package/foundation/integrations/missing-dependency.mjs +20 -5
- package/foundation/release/folder-lock.d.mts +5 -2
- package/foundation/release/folder-lock.mjs +9 -3
- package/foundation/release/notices.mjs +2 -8
- package/foundation/render/cut-short.d.mts +8 -2
- package/foundation/render/cut-short.mjs +50 -5
- package/foundation/render/drawn-contrast.d.mts +40 -0
- package/foundation/render/drawn-contrast.mjs +355 -0
- package/foundation/render/file-header.mjs +4 -0
- package/foundation/render/layers.d.mts +2 -1
- package/foundation/render/layers.mjs +22 -1
- package/foundation/render/package-resolve-hooks.d.mts +38 -9
- package/foundation/render/package-resolve-hooks.mjs +124 -32
- package/foundation/render/page-worker.mjs +95 -23
- package/foundation/render/part-boxes.d.mts +27 -0
- package/foundation/render/part-boxes.mjs +45 -0
- package/foundation/render/picture-chrome.mjs +36 -4
- package/foundation/render/picture-playwright.mjs +26 -2
- package/foundation/render/picture-window.d.mts +29 -2
- package/foundation/render/picture-window.mjs +99 -1
- package/foundation/render/render-page.d.mts +39 -6
- package/foundation/render/render-page.mjs +26 -10
- package/foundation/render/ts-paths.mjs +2 -1
- package/foundation/response/error-codes.doc.mjs +1 -1
- package/foundation/response/error-codes.mjs +1 -1
- package/foundation/response/response-types.doc.mjs +9 -5
- package/foundation/text/written-string.d.mts +4 -1
- package/foundation/text/written-string.mjs +4 -1
- package/package.json +8 -7
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file The values a page certainly gives a JSX prop, read from the
|
|
5
|
+
* attribute as written.
|
|
6
|
+
* @input a Babel JSX attribute node and the element's scope
|
|
7
|
+
* @output the kinds of value the prop gets and the strings among them, or
|
|
8
|
+
* null when a value is not certain
|
|
9
|
+
* @position Read by the export's page worker
|
|
10
|
+
* (foundation/render/page-worker.mjs) for the unknown-prop-value warning.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {writtenString} from '../text/written-string.mjs';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {import('./core-augmentation-points.mjs').ValueKind} ValueKind
|
|
17
|
+
* @typedef {{kinds: ValueKind[], strings: string[] | null}} WrittenValue
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* What a JSX attribute certainly gives its prop. Every branch of the value
|
|
22
|
+
* must be certain: a literal (a string, a template, a number, a boolean, an
|
|
23
|
+
* object or array literal, `new Date(…)`, a function), a conditional or
|
|
24
|
+
* logical expression of certain branches, a `const` bound to one, or the
|
|
25
|
+
* first value of `useState(<certain value>)`, which is what the first paint
|
|
26
|
+
* gets. A bare attribute (`<Button isDisabled>`) is `true`. A branch that is
|
|
27
|
+
* `null` or `undefined` gives no value. `strings` lists the string values
|
|
28
|
+
* when every string branch is a literal, else null. Null when any branch is
|
|
29
|
+
* not certain: such a prop is not checked.
|
|
30
|
+
* @param {any} attr a JSXAttribute node
|
|
31
|
+
* @param {any} scope the element's Babel scope
|
|
32
|
+
* @returns {WrittenValue | null}
|
|
33
|
+
*/
|
|
34
|
+
export function writtenValue(attr, scope) {
|
|
35
|
+
const value = attr.value;
|
|
36
|
+
/** @type {Leaf[] | null} */
|
|
37
|
+
let leaves;
|
|
38
|
+
if (!value) leaves = [{kind: 'boolean'}];
|
|
39
|
+
else if (value.type === 'JSXExpressionContainer')
|
|
40
|
+
leaves = branches(value.expression, scope, 0, new Set());
|
|
41
|
+
else leaves = branches(value, scope, 0, new Set());
|
|
42
|
+
const values = leaves?.filter(leaf => leaf.kind !== 'nullish');
|
|
43
|
+
if (!values || values.length === 0) return null;
|
|
44
|
+
const strings = values.filter(leaf => leaf.kind === 'string');
|
|
45
|
+
return {
|
|
46
|
+
kinds: /** @type {ValueKind[]} */ ([
|
|
47
|
+
...new Set(values.map(leaf => leaf.kind)),
|
|
48
|
+
]),
|
|
49
|
+
strings: strings.every(leaf => leaf.text !== undefined)
|
|
50
|
+
? strings.map(leaf => /** @type {string} */ (leaf.text))
|
|
51
|
+
: null,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* One value a branch can give. `nullish` is `null` or `undefined`, which
|
|
57
|
+
* gives the prop no value.
|
|
58
|
+
* @typedef {{kind: ValueKind | 'nullish', text?: string}} Leaf
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @param {any} node
|
|
63
|
+
* @param {any} scope
|
|
64
|
+
* @param {number} depth
|
|
65
|
+
* @param {Set<any>} seen bindings already followed, so a cycle ends
|
|
66
|
+
* @returns {Leaf[] | null}
|
|
67
|
+
*/
|
|
68
|
+
function branches(node, scope, depth, seen) {
|
|
69
|
+
if (!node || depth > 16) return null;
|
|
70
|
+
/** @param {any} next */
|
|
71
|
+
const deeper = next => branches(next, scope, depth + 1, seen);
|
|
72
|
+
const text = writtenString(node);
|
|
73
|
+
if (text !== null) return [{kind: 'string', text}];
|
|
74
|
+
switch (node.type) {
|
|
75
|
+
case 'TemplateLiteral':
|
|
76
|
+
return [{kind: 'string'}];
|
|
77
|
+
case 'NumericLiteral':
|
|
78
|
+
return [{kind: 'number'}];
|
|
79
|
+
case 'UnaryExpression':
|
|
80
|
+
return node.operator === '-' && node.argument.type === 'NumericLiteral'
|
|
81
|
+
? [{kind: 'number'}]
|
|
82
|
+
: null;
|
|
83
|
+
case 'BooleanLiteral':
|
|
84
|
+
return [{kind: 'boolean'}];
|
|
85
|
+
case 'ObjectExpression':
|
|
86
|
+
return [{kind: 'object'}];
|
|
87
|
+
case 'ArrayExpression':
|
|
88
|
+
return [{kind: 'array'}];
|
|
89
|
+
case 'ArrowFunctionExpression':
|
|
90
|
+
case 'FunctionExpression':
|
|
91
|
+
return [{kind: 'function'}];
|
|
92
|
+
case 'NewExpression':
|
|
93
|
+
return node.callee.type === 'Identifier' &&
|
|
94
|
+
node.callee.name === 'Date' &&
|
|
95
|
+
!scope.getBinding('Date')
|
|
96
|
+
? [{kind: 'Date'}]
|
|
97
|
+
: null;
|
|
98
|
+
case 'NullLiteral':
|
|
99
|
+
return [{kind: 'nullish'}];
|
|
100
|
+
case 'TSAsExpression':
|
|
101
|
+
// A cast to `never`, `any` or `unknown` says the author knows the
|
|
102
|
+
// value does not fit the prop's type: it is not checked.
|
|
103
|
+
return ['TSNeverKeyword', 'TSAnyKeyword', 'TSUnknownKeyword'].includes(
|
|
104
|
+
node.typeAnnotation.type,
|
|
105
|
+
)
|
|
106
|
+
? null
|
|
107
|
+
: deeper(node.expression);
|
|
108
|
+
case 'ParenthesizedExpression':
|
|
109
|
+
case 'TSSatisfiesExpression':
|
|
110
|
+
case 'TSNonNullExpression':
|
|
111
|
+
return deeper(node.expression);
|
|
112
|
+
case 'ConditionalExpression':
|
|
113
|
+
return both(deeper(node.consequent), deeper(node.alternate));
|
|
114
|
+
case 'LogicalExpression':
|
|
115
|
+
return logical(node.operator, deeper(node.left), () =>
|
|
116
|
+
deeper(node.right),
|
|
117
|
+
);
|
|
118
|
+
case 'Identifier': {
|
|
119
|
+
if (node.name === 'undefined' && !scope.getBinding('undefined'))
|
|
120
|
+
return [{kind: 'nullish'}];
|
|
121
|
+
const binding = scope.getBinding(node.name);
|
|
122
|
+
if (!binding || seen.has(binding)) return null;
|
|
123
|
+
const init = boundValue(binding);
|
|
124
|
+
// Each branch follows its own path, so a const used in two branches
|
|
125
|
+
// is read in both.
|
|
126
|
+
return init
|
|
127
|
+
? branches(
|
|
128
|
+
init,
|
|
129
|
+
binding.path.scope,
|
|
130
|
+
depth + 1,
|
|
131
|
+
new Set([...seen, binding]),
|
|
132
|
+
)
|
|
133
|
+
: null;
|
|
134
|
+
}
|
|
135
|
+
default:
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The expression a binding certainly holds on first paint: the initial value
|
|
142
|
+
* of a `const` that is never reassigned, or the argument of React's
|
|
143
|
+
* `useState(…)` for the first name of `const [value, setValue] = useState(…)`.
|
|
144
|
+
* @param {any} binding
|
|
145
|
+
* @returns {any} the expression, or null
|
|
146
|
+
*/
|
|
147
|
+
function boundValue(binding) {
|
|
148
|
+
const declarator = binding.path;
|
|
149
|
+
if (
|
|
150
|
+
binding.kind !== 'const' ||
|
|
151
|
+
!declarator.isVariableDeclarator() ||
|
|
152
|
+
binding.constantViolations.length > 0
|
|
153
|
+
)
|
|
154
|
+
return null;
|
|
155
|
+
const {id, init} = declarator.node;
|
|
156
|
+
if (id.type === 'Identifier') return init;
|
|
157
|
+
if (
|
|
158
|
+
id.type !== 'ArrayPattern' ||
|
|
159
|
+
id.elements[0]?.type !== 'Identifier' ||
|
|
160
|
+
id.elements[0].name !== binding.identifier.name
|
|
161
|
+
)
|
|
162
|
+
return null;
|
|
163
|
+
if (
|
|
164
|
+
init?.type !== 'CallExpression' ||
|
|
165
|
+
!isReactUseState(init.callee, declarator.scope)
|
|
166
|
+
)
|
|
167
|
+
return null;
|
|
168
|
+
const [initial] = init.arguments;
|
|
169
|
+
// A function given to useState is called for the value: what it returns is
|
|
170
|
+
// not read here.
|
|
171
|
+
if (
|
|
172
|
+
init.arguments.length !== 1 ||
|
|
173
|
+
initial.type === 'ArrowFunctionExpression' ||
|
|
174
|
+
initial.type === 'FunctionExpression'
|
|
175
|
+
)
|
|
176
|
+
return null;
|
|
177
|
+
return initial;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Whether a callee is React's `useState`: imported from `react` by name, or
|
|
182
|
+
* a member of React's default or namespace import.
|
|
183
|
+
* @param {any} callee
|
|
184
|
+
* @param {any} scope
|
|
185
|
+
* @returns {boolean}
|
|
186
|
+
*/
|
|
187
|
+
function isReactUseState(callee, scope) {
|
|
188
|
+
/** @param {string} name */
|
|
189
|
+
const reactImport = name => {
|
|
190
|
+
const spec = scope.getBinding(name)?.path;
|
|
191
|
+
return spec?.parent?.type === 'ImportDeclaration' &&
|
|
192
|
+
spec.parent.source.value === 'react'
|
|
193
|
+
? spec
|
|
194
|
+
: null;
|
|
195
|
+
};
|
|
196
|
+
if (callee.type === 'Identifier') {
|
|
197
|
+
const spec = reactImport(callee.name);
|
|
198
|
+
return Boolean(
|
|
199
|
+
spec?.isImportSpecifier() &&
|
|
200
|
+
(spec.node.imported.name ?? spec.node.imported.value) === 'useState',
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
if (
|
|
204
|
+
callee.type !== 'MemberExpression' ||
|
|
205
|
+
callee.computed ||
|
|
206
|
+
callee.object.type !== 'Identifier'
|
|
207
|
+
)
|
|
208
|
+
return false;
|
|
209
|
+
const spec = reactImport(callee.object.name);
|
|
210
|
+
return Boolean(
|
|
211
|
+
spec && !spec.isImportSpecifier() && callee.property.name === 'useState',
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The values a logical expression gives, as JavaScript picks them: `a || b`
|
|
217
|
+
* gives each truthy value of `a`, and `b` when `a` can be falsy; `a && b`
|
|
218
|
+
* gives each falsy value of `a`, and `b` when `a` can be truthy; `a ?? b`
|
|
219
|
+
* gives each value of `a` that is not `null` or `undefined`, and `b` when
|
|
220
|
+
* it can be. Null unless the truthiness of every value of `a` is known (a
|
|
221
|
+
* string literal, `null` or `undefined`).
|
|
222
|
+
* @param {string} operator
|
|
223
|
+
* @param {Leaf[] | null} left
|
|
224
|
+
* @param {() => Leaf[] | null} right read only when the left can give way
|
|
225
|
+
* @returns {Leaf[] | null}
|
|
226
|
+
*/
|
|
227
|
+
function logical(operator, left, right) {
|
|
228
|
+
if (!left) return null;
|
|
229
|
+
/** @param {Leaf} leaf */
|
|
230
|
+
const truthy = leaf =>
|
|
231
|
+
leaf.kind === 'nullish'
|
|
232
|
+
? false
|
|
233
|
+
: leaf.kind === 'string' && leaf.text !== undefined
|
|
234
|
+
? leaf.text !== ''
|
|
235
|
+
: null;
|
|
236
|
+
if (operator === '??') {
|
|
237
|
+
const kept = left.filter(leaf => leaf.kind !== 'nullish');
|
|
238
|
+
return kept.length === left.length ? kept : both(kept, right());
|
|
239
|
+
}
|
|
240
|
+
const known = left.map(truthy);
|
|
241
|
+
if (known.includes(null)) return null;
|
|
242
|
+
const stays =
|
|
243
|
+
operator === '||'
|
|
244
|
+
? left.filter((_, i) => known[i])
|
|
245
|
+
: left.filter((_, i) => !known[i]);
|
|
246
|
+
const givesWay =
|
|
247
|
+
operator === '||' ? known.includes(false) : known.includes(true);
|
|
248
|
+
return givesWay ? both(stays, right()) : stays;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* @param {Leaf[] | null} a
|
|
253
|
+
* @param {Leaf[] | null} b
|
|
254
|
+
* @returns {Leaf[] | null}
|
|
255
|
+
*/
|
|
256
|
+
function both(a, b) {
|
|
257
|
+
return a && b ? [...a, ...b] : null;
|
|
258
|
+
}
|