@astryxdesign/cli 0.7.2-canary.411cbed → 0.7.2-canary.4787e3f
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/foundation/discovery/package-tsconfig.d.mts +14 -0
- package/foundation/discovery/package-tsconfig.mjs +228 -0
- package/foundation/discovery/package-tsconfig.test.mjs +210 -0
- package/foundation/discovery/project-typescript.d.mts +23 -0
- package/foundation/discovery/project-typescript.mjs +61 -0
- package/foundation/discovery/prop-drift.d.mts +165 -0
- package/foundation/discovery/prop-drift.mjs +1421 -0
- package/foundation/discovery/prop-drift.test.mjs +554 -0
- package/foundation/discovery/unresolved-references.d.mts +62 -0
- package/foundation/discovery/unresolved-references.mjs +790 -0
- package/foundation/discovery/unresolved-references.test.mjs +567 -0
- package/foundation/doc-compiler/doc-loads.test.mjs +4 -0
- package/package.json +9 -9
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Documented props against a component's TypeScript props: each kind
|
|
5
|
+
* of finding, and each component it cannot read.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as fs from 'node:fs';
|
|
9
|
+
import * as os from 'node:os';
|
|
10
|
+
import * as path from 'node:path';
|
|
11
|
+
import {createRequire} from 'node:module';
|
|
12
|
+
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
|
|
13
|
+
import {comparePropTables} from './prop-drift.mjs';
|
|
14
|
+
|
|
15
|
+
const require = createRequire(import.meta.url);
|
|
16
|
+
const TYPESCRIPT_DIR = path.dirname(require.resolve('typescript/package.json'));
|
|
17
|
+
|
|
18
|
+
/** @type {string} */
|
|
19
|
+
let pkg;
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
pkg = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-prop-drift-'));
|
|
23
|
+
fs.writeFileSync(
|
|
24
|
+
path.join(pkg, 'package.json'),
|
|
25
|
+
JSON.stringify({name: '@acme/widgets', version: '1.0.0'}),
|
|
26
|
+
);
|
|
27
|
+
fs.writeFileSync(
|
|
28
|
+
path.join(pkg, 'tsconfig.json'),
|
|
29
|
+
JSON.stringify({
|
|
30
|
+
compilerOptions: {
|
|
31
|
+
strict: true,
|
|
32
|
+
jsx: 'react-jsx',
|
|
33
|
+
module: 'esnext',
|
|
34
|
+
moduleResolution: 'bundler',
|
|
35
|
+
target: 'es2022',
|
|
36
|
+
},
|
|
37
|
+
}),
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterEach(() => {
|
|
42
|
+
fs.rmSync(pkg, {recursive: true, force: true});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
function withTypeScript() {
|
|
46
|
+
fs.mkdirSync(path.join(pkg, 'node_modules'), {recursive: true});
|
|
47
|
+
fs.symlinkSync(
|
|
48
|
+
TYPESCRIPT_DIR,
|
|
49
|
+
path.join(pkg, 'node_modules', 'typescript'),
|
|
50
|
+
'dir',
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Write `components/<name>.tsx` and its doc; returns its prop table, which
|
|
56
|
+
* also carries the component record (name, docPath, sourcePath).
|
|
57
|
+
* @param {string} name
|
|
58
|
+
* @param {string} source
|
|
59
|
+
* @param {Record<string, any>} doc
|
|
60
|
+
*/
|
|
61
|
+
function component(name, source, doc) {
|
|
62
|
+
const dir = path.join(pkg, 'components');
|
|
63
|
+
fs.mkdirSync(dir, {recursive: true});
|
|
64
|
+
const sourcePath = path.join(dir, `${name}.tsx`);
|
|
65
|
+
const docPath = path.join(dir, `${name}.doc.mjs`);
|
|
66
|
+
fs.writeFileSync(sourcePath, source);
|
|
67
|
+
fs.writeFileSync(
|
|
68
|
+
docPath,
|
|
69
|
+
`export default ${JSON.stringify({
|
|
70
|
+
type: 'component',
|
|
71
|
+
name,
|
|
72
|
+
displayName: name,
|
|
73
|
+
description: 'Fixture.',
|
|
74
|
+
...doc,
|
|
75
|
+
})};\n`,
|
|
76
|
+
);
|
|
77
|
+
return {
|
|
78
|
+
name,
|
|
79
|
+
docPath,
|
|
80
|
+
sourcePath,
|
|
81
|
+
doc: `components/${name}.doc.mjs`,
|
|
82
|
+
props: doc.props ?? [],
|
|
83
|
+
sources: [sourcePath],
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @param {string} name
|
|
89
|
+
* @param {string} type
|
|
90
|
+
* @param {Record<string, unknown>} [extra]
|
|
91
|
+
*/
|
|
92
|
+
const prop = (name, type, extra = {}) => ({
|
|
93
|
+
name,
|
|
94
|
+
type,
|
|
95
|
+
description: 'Fixture prop.',
|
|
96
|
+
...extra,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
/** @param {import('./prop-drift.mjs').PropDriftTable[]} tables */
|
|
100
|
+
function compare(tables) {
|
|
101
|
+
return comparePropTables(tables, {packageDir: pkg, resolveFrom: [pkg]});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** @param {{findings: Array<{component: string, prop: string, kind: string}>}} report */
|
|
105
|
+
const kinds = report =>
|
|
106
|
+
report.findings.map(f => `${f.component}.${f.prop} ${f.kind}`);
|
|
107
|
+
|
|
108
|
+
describe('comparePropTables', () => {
|
|
109
|
+
it("reports a prop missing from the code, one missing from the doc, and a wrong required flag, each component's together", async () => {
|
|
110
|
+
withTypeScript();
|
|
111
|
+
const badge = component(
|
|
112
|
+
'Badge',
|
|
113
|
+
`export type Tone = 'neutral' | 'info' | 'danger';
|
|
114
|
+
export interface BadgeProps {
|
|
115
|
+
label: string;
|
|
116
|
+
tone?: Tone;
|
|
117
|
+
count?: number;
|
|
118
|
+
}
|
|
119
|
+
export function Badge(props: BadgeProps) { return null; }`,
|
|
120
|
+
{
|
|
121
|
+
props: [
|
|
122
|
+
prop('label', 'string'),
|
|
123
|
+
prop('tone', "'neutral' | 'info'"),
|
|
124
|
+
prop('size', "'sm' | 'md'"),
|
|
125
|
+
],
|
|
126
|
+
},
|
|
127
|
+
);
|
|
128
|
+
// A name that starts with another's sorts after all of the other's.
|
|
129
|
+
const count = component(
|
|
130
|
+
'BadgeCount',
|
|
131
|
+
'export function BadgeCount(props: {amount?: number}) { return null; }',
|
|
132
|
+
{props: []},
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
const report = await compare([count, badge]);
|
|
136
|
+
|
|
137
|
+
expect(report.status).toBe('checked');
|
|
138
|
+
expect(report.typescript).toMatch(/^\d+\.\d+/);
|
|
139
|
+
expect(report.components).toBe(2);
|
|
140
|
+
expect(kinds(report)).toEqual([
|
|
141
|
+
'Badge.count not_in_doc',
|
|
142
|
+
'Badge.label required_mismatch',
|
|
143
|
+
'Badge.size not_in_code',
|
|
144
|
+
'BadgeCount.amount not_in_doc',
|
|
145
|
+
]);
|
|
146
|
+
expect(report.findings.every(f => f.severity === 'warning')).toBe(true);
|
|
147
|
+
expect(
|
|
148
|
+
report.findings.find(f => f.kind === 'required_mismatch'),
|
|
149
|
+
).toMatchObject({
|
|
150
|
+
doc: 'components/Badge.doc.mjs',
|
|
151
|
+
source: 'components/Badge.tsx',
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('reads every overload, one without props included, reads a prop only some members of a union require as optional, and holds every prop one entry names to its `required`', async () => {
|
|
156
|
+
withTypeScript();
|
|
157
|
+
const picker = component(
|
|
158
|
+
'Picker',
|
|
159
|
+
`type Single = {isMulti?: false; value: string; onChange: (value: string) => void};
|
|
160
|
+
type Multi = {isMulti: true; value: string[]; onChange: (value: string[]) => void};
|
|
161
|
+
export type PickerProps = (Single | Multi) & {label: string};
|
|
162
|
+
export function Picker(props: PickerProps) { return null; }`,
|
|
163
|
+
{
|
|
164
|
+
props: [
|
|
165
|
+
prop('label', 'string', {required: true}),
|
|
166
|
+
prop('isMulti', 'boolean'),
|
|
167
|
+
prop('value', 'string', {required: true}),
|
|
168
|
+
prop('onChange', '(value: string) => void', {required: true}),
|
|
169
|
+
],
|
|
170
|
+
},
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
const select = component(
|
|
174
|
+
'Select',
|
|
175
|
+
`type Single = {multiple?: false; value?: string};
|
|
176
|
+
type Multi = {multiple: true; value?: string[]; max?: number};
|
|
177
|
+
export function Select(props: Single): null;
|
|
178
|
+
export function Select(props: Multi): null;
|
|
179
|
+
export function Select(props: Single | Multi) { return null; }`,
|
|
180
|
+
{
|
|
181
|
+
props: [
|
|
182
|
+
prop('multiple', 'boolean'),
|
|
183
|
+
prop('value', 'string | string[]'),
|
|
184
|
+
prop('max', 'number'),
|
|
185
|
+
],
|
|
186
|
+
},
|
|
187
|
+
);
|
|
188
|
+
// An overload that takes no props makes every prop optional; a typed
|
|
189
|
+
// props type beside a `Record<string, any>` or a mapped `any` index is
|
|
190
|
+
// compared as it is beside an inline `[key: string]: any`.
|
|
191
|
+
const propless = component(
|
|
192
|
+
'Propless',
|
|
193
|
+
'export function Propless(): null;\nexport function Propless(props: {label: string}): null;\nexport function Propless(props?: {label: string}) { return null; }',
|
|
194
|
+
{props: [prop('label', 'string', {required: true})]},
|
|
195
|
+
);
|
|
196
|
+
const open = [
|
|
197
|
+
'Record<string, any>',
|
|
198
|
+
'Record<`data-${string}`, any>',
|
|
199
|
+
'{[K in string]?: any}',
|
|
200
|
+
].map((index, i) =>
|
|
201
|
+
component(
|
|
202
|
+
`Open${i}`,
|
|
203
|
+
`export function Open${i}(props: {label?: string; size?: number} & ${index}) { return null; }`,
|
|
204
|
+
{props: [prop('label', 'string')]},
|
|
205
|
+
),
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
// Required in one variant only: a caller may leave it out, so optional.
|
|
209
|
+
const menu = component(
|
|
210
|
+
'Menu',
|
|
211
|
+
'export function Menu(props: {items: string[]} | {alt: string}) { return null; }',
|
|
212
|
+
{props: [prop('items', 'X[]', {required: true}), prop('alt', 'X')]},
|
|
213
|
+
);
|
|
214
|
+
// One entry's `required` is a claim about every prop it names.
|
|
215
|
+
const pairs = ['label: string', 'label?: string'].map((label, i) =>
|
|
216
|
+
component(
|
|
217
|
+
`Pair${i}`,
|
|
218
|
+
`export function Pair${i}(props: {value: string; ${label}}) { return null; }`,
|
|
219
|
+
{props: [prop('value / label', 'string / string', {required: true})]},
|
|
220
|
+
),
|
|
221
|
+
);
|
|
222
|
+
// Typed `any` through the package's own alias of `Omit`, written in the
|
|
223
|
+
// signature, as a named alias, or as an interface's base.
|
|
224
|
+
const without = ['Without<any>', 'P', 'Q', 'R'].map((props, i) =>
|
|
225
|
+
component(
|
|
226
|
+
`Without${i}`,
|
|
227
|
+
`type Without<T> = Omit<T, 'as'>; type P = Without<any>; interface Q extends Omit<any, 'as'> {} type R = Omit<P, 'ref'>;\nexport function Without${i}(props: ${props}) { return null; }`,
|
|
228
|
+
{props: [prop('label', 'string')]},
|
|
229
|
+
),
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
const tables = [
|
|
233
|
+
picker,
|
|
234
|
+
select,
|
|
235
|
+
propless,
|
|
236
|
+
...open,
|
|
237
|
+
menu,
|
|
238
|
+
...pairs,
|
|
239
|
+
...without,
|
|
240
|
+
];
|
|
241
|
+
const report = await compare(tables);
|
|
242
|
+
|
|
243
|
+
expect(kinds(report)).toEqual([
|
|
244
|
+
'Menu.items required_mismatch',
|
|
245
|
+
'Open0.size not_in_doc',
|
|
246
|
+
'Open1.size not_in_doc',
|
|
247
|
+
'Open2.size not_in_doc',
|
|
248
|
+
'Pair1.label required_mismatch',
|
|
249
|
+
'Propless.label required_mismatch',
|
|
250
|
+
]);
|
|
251
|
+
expect(report.components).toBe(9);
|
|
252
|
+
expect(report.propsNotChecked).toBeUndefined();
|
|
253
|
+
expect(report.unchecked.map(entry => entry.reason)).toEqual(
|
|
254
|
+
[0, 1, 2, 3].map(i =>
|
|
255
|
+
expect.stringMatching(`^Without${i}'s props are typed`),
|
|
256
|
+
),
|
|
257
|
+
);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('lists each component whose props it cannot read as not compared', async () => {
|
|
261
|
+
withTypeScript();
|
|
262
|
+
const tsconfig = path.join(pkg, 'tsconfig.json');
|
|
263
|
+
const config = JSON.parse(fs.readFileSync(tsconfig, 'utf8'));
|
|
264
|
+
config.compilerOptions.allowJs = true;
|
|
265
|
+
fs.writeFileSync(tsconfig, JSON.stringify(config));
|
|
266
|
+
/** @param {Record<string, string>} sources each component's props types */
|
|
267
|
+
const missing = sources =>
|
|
268
|
+
Object.entries(sources).map(([name, types]) =>
|
|
269
|
+
component(
|
|
270
|
+
name,
|
|
271
|
+
`import type {MissingProps} from '@acme/not-installed';\n${types}\nexport function ${name}(props: ${name}Props) { return null; }`,
|
|
272
|
+
{props: [prop('label', 'string')]},
|
|
273
|
+
),
|
|
274
|
+
);
|
|
275
|
+
const unread = [
|
|
276
|
+
...missing({
|
|
277
|
+
Ghost:
|
|
278
|
+
'export interface GhostProps extends MissingProps { label?: string }',
|
|
279
|
+
Omitted:
|
|
280
|
+
"export interface OmittedProps extends Omit<MissingProps, 'title'> { label?: string }",
|
|
281
|
+
Phantom:
|
|
282
|
+
'export type PhantomProps = Partial<MissingProps> & {label?: string};',
|
|
283
|
+
Named:
|
|
284
|
+
"type Base = Omit<MissingProps, 'title'>; export interface NamedProps extends Base { label?: string }",
|
|
285
|
+
Joined:
|
|
286
|
+
"type Base = Omit<MissingProps, 'title'>; export type JoinedProps = Base & {label?: string};",
|
|
287
|
+
Bare: "type Base = Omit<MissingProps, 'title'>; export type BareProps = Base;",
|
|
288
|
+
Mapped:
|
|
289
|
+
'export type MappedProps = {[K in keyof MissingProps]?: string};',
|
|
290
|
+
Prefixed:
|
|
291
|
+
'type On<K extends string> = {[P in `on${K}`]?: () => void}; export type PrefixedProps = On<MissingProps> & {label?: string};',
|
|
292
|
+
Picked:
|
|
293
|
+
"type Base = Pick<MissingProps, 'title'>; export type PickedProps = Base;",
|
|
294
|
+
Kept: "type W<T> = T & {label?: string}; export type KeptProps = W<Required<Pick<MissingProps, 'title'>>>;",
|
|
295
|
+
Nested:
|
|
296
|
+
"export type NestedProps = Partial<Omit<MissingProps, 'title'>> & {label?: string};",
|
|
297
|
+
Deep: 'interface L1 extends MissingProps {} interface L2 extends L1 {} interface L3 extends L2 {} interface L4 extends L3 {} interface L5 extends L4 {} export interface DeepProps extends L5 { label?: string }',
|
|
298
|
+
}),
|
|
299
|
+
component(
|
|
300
|
+
'Wrapped',
|
|
301
|
+
`import {forwardRef} from 'react';
|
|
302
|
+
export const Wrapped = forwardRef<HTMLButtonElement, {label?: string}>((props, ref) => null);`,
|
|
303
|
+
{props: [prop('label', 'string')]},
|
|
304
|
+
),
|
|
305
|
+
component(
|
|
306
|
+
'Classy',
|
|
307
|
+
"import {MissingBase} from '@acme/not-installed';\nexport class Classy extends MissingBase<{label?: string}> {}\n",
|
|
308
|
+
{props: [prop('label', 'string')]},
|
|
309
|
+
),
|
|
310
|
+
];
|
|
311
|
+
// Each of these is the only reference in its files that does not resolve:
|
|
312
|
+
// a name its module does not export, a dependency's own missing import, a
|
|
313
|
+
// missing type package, and a side-effect import.
|
|
314
|
+
fs.writeFileSync(
|
|
315
|
+
path.join(pkg, 'components', 'theme.ts'),
|
|
316
|
+
'export interface Theme { tone?: string }\n',
|
|
317
|
+
);
|
|
318
|
+
const dep = path.join(pkg, 'node_modules', '@acme', 'dep');
|
|
319
|
+
fs.mkdirSync(dep, {recursive: true});
|
|
320
|
+
fs.writeFileSync(
|
|
321
|
+
path.join(dep, 'package.json'),
|
|
322
|
+
JSON.stringify({name: '@acme/dep', types: 'index.d.ts'}),
|
|
323
|
+
);
|
|
324
|
+
fs.writeFileSync(
|
|
325
|
+
path.join(dep, 'index.d.ts'),
|
|
326
|
+
"import type {MissingProps} from '@acme/not-installed';\nexport type DepProps = MissingProps & {label?: string};\n",
|
|
327
|
+
);
|
|
328
|
+
const references = Object.entries({
|
|
329
|
+
Unnamed:
|
|
330
|
+
"import type {Gone} from './theme';\nexport function Unnamed(props: {label?: string; tone?: Gone}) { return null; }\n",
|
|
331
|
+
Peered:
|
|
332
|
+
"import type {DepProps} from '@acme/dep';\nexport function Peered(props: DepProps) { return null; }\n",
|
|
333
|
+
Referenced:
|
|
334
|
+
'/// <reference types="@acme/not-installed-types" />\nexport function Referenced(props: {label?: string}) { return null; }\n',
|
|
335
|
+
Effect:
|
|
336
|
+
"import '@acme/not-installed-effect';\nexport function Effect(props: {label?: string}) { return null; }\n",
|
|
337
|
+
// A type name declared nowhere, which only `keyof`, a `Record`'s keys
|
|
338
|
+
// or a template literal key reaches.
|
|
339
|
+
Omitting:
|
|
340
|
+
'export function Omitting(props: Omit<{label: string; size?: number}, keyof NowhereProps>) { return null; }\n',
|
|
341
|
+
Recorded:
|
|
342
|
+
'export function Recorded(props: Partial<Record<keyof NowhereProps, string>>) { return null; }\n',
|
|
343
|
+
Handlers:
|
|
344
|
+
'export function Handlers(props: {[K in `on${NowhereKey}`]?: () => void}) { return null; }\n',
|
|
345
|
+
Aliased:
|
|
346
|
+
'type Keys = keyof NowhereProps;\nexport function Aliased(props: {[K in Keys]?: string}) { return null; }\n',
|
|
347
|
+
// A value declared nowhere behind a binding, and a property that
|
|
348
|
+
// doesn't exist: the compiler can only give either its error type.
|
|
349
|
+
Valued:
|
|
350
|
+
'const d = missingValue;\nexport function Valued(props: Omit<{label: string; size?: number}, keyof typeof d>) { return null; }\n',
|
|
351
|
+
Sized:
|
|
352
|
+
"interface Theme { colors: {a: string} }\nexport function Sized(props: Partial<Record<keyof Theme['sizes'], string>>) { return null; }\n",
|
|
353
|
+
// A value the compiler can't type, spread into an object, makes the
|
|
354
|
+
// object `any`.
|
|
355
|
+
Spread:
|
|
356
|
+
'interface Theme { colors: {a: string} }\ndeclare const theme: Theme;\nconst defaults = {...theme.sizes};\nexport function Spread(props: Omit<{label: string; size?: number}, keyof typeof defaults>) { return null; }\n',
|
|
357
|
+
Toned:
|
|
358
|
+
'const tones = {...undeclaredTones};\nexport function Toned(props: Partial<Record<keyof typeof tones, string>>) { return null; }\n',
|
|
359
|
+
}).map(([name, source]) =>
|
|
360
|
+
component(name, source, {props: [prop('label', 'string')]}),
|
|
361
|
+
);
|
|
362
|
+
// A name declared nowhere in a JavaScript component's JSDoc, which `tsc`
|
|
363
|
+
// doesn't report there.
|
|
364
|
+
const documented = component(
|
|
365
|
+
'Documented',
|
|
366
|
+
'/** @param {Omit<{label: string; size?: number}, keyof NowhereJs>} props */\nexport function Documented(props) { return null; }\n',
|
|
367
|
+
{props: [prop('label', 'string')]},
|
|
368
|
+
);
|
|
369
|
+
const jsx = documented.sourcePath.replace(/\.tsx$/, '.jsx');
|
|
370
|
+
fs.renameSync(documented.sourcePath, jsx);
|
|
371
|
+
references.push({...documented, sourcePath: jsx, sources: [jsx]});
|
|
372
|
+
// A module found without types resolves: its names are `any`.
|
|
373
|
+
const untypedDir = path.join(pkg, 'node_modules', 'untyped-dep');
|
|
374
|
+
fs.mkdirSync(untypedDir, {recursive: true});
|
|
375
|
+
fs.writeFileSync(
|
|
376
|
+
path.join(untypedDir, 'package.json'),
|
|
377
|
+
JSON.stringify({name: 'untyped-dep', main: 'index.js'}),
|
|
378
|
+
);
|
|
379
|
+
fs.writeFileSync(
|
|
380
|
+
path.join(untypedDir, 'index.js'),
|
|
381
|
+
'export const fmt = text => text;\n',
|
|
382
|
+
);
|
|
383
|
+
const untyped = component(
|
|
384
|
+
'Untyped',
|
|
385
|
+
"import {fmt} from 'untyped-dep';\nexport function Untyped(props: {label?: string}) { fmt(props.label); return null; }\n",
|
|
386
|
+
{props: [prop('label', 'string')]},
|
|
387
|
+
);
|
|
388
|
+
const lonely = component(
|
|
389
|
+
'Lonely',
|
|
390
|
+
'export function Other(props: {label: string}) { return null; }\n',
|
|
391
|
+
{props: [prop('label', 'string', {required: true})]},
|
|
392
|
+
);
|
|
393
|
+
// An `any` index signature the package declares itself is no reason.
|
|
394
|
+
const open = component(
|
|
395
|
+
'Open',
|
|
396
|
+
'export function Open(props: {label?: string; [key: string]: any}) { return null; }\n',
|
|
397
|
+
{props: [prop('label', 'string')]},
|
|
398
|
+
);
|
|
399
|
+
// A missing import in the files a component comes from lists it, however
|
|
400
|
+
// the props reach it: only a member's type, a value's property, a
|
|
401
|
+
// conditional, `??`, a comma, or an own HOC that maps over `keyof P`.
|
|
402
|
+
const reached = Object.entries({
|
|
403
|
+
Member:
|
|
404
|
+
'interface Holder<T> { label?: T } export function Member(props: Holder<MissingProps>) { return null; }',
|
|
405
|
+
Header:
|
|
406
|
+
'function Inner(props: {label?: string}) { return null; } const Card = Object.assign(Inner, {Header: Inner}); export const Header = Card.Header;',
|
|
407
|
+
Chosen:
|
|
408
|
+
'function Inner(props: {label?: string}) { return null; } export const Chosen = (globalThis as {x?: 1}).x ? Inner : Inner;',
|
|
409
|
+
Fallback:
|
|
410
|
+
'function Inner(props: {label?: string}) { return null; } export const Fallback = (undefined as typeof Inner | undefined) ?? Inner;',
|
|
411
|
+
Comma:
|
|
412
|
+
'function Inner(props: {label?: string}) { return null; } export const Comma = (0, Inner);',
|
|
413
|
+
Keyed:
|
|
414
|
+
'function withKeys<P>(c: (props: P) => null) { return (props: {[K in keyof P]?: string}) => c(props as P); } export const Keyed = withKeys((props: MissingProps) => null);',
|
|
415
|
+
}).map(([name, source]) =>
|
|
416
|
+
component(
|
|
417
|
+
name,
|
|
418
|
+
`import type {MissingProps} from '@acme/not-installed';\n${source}\n`,
|
|
419
|
+
{props: [prop('label', 'string')]},
|
|
420
|
+
),
|
|
421
|
+
);
|
|
422
|
+
|
|
423
|
+
// Props that take names from a type parameter, such as the element a
|
|
424
|
+
// component renders, accept any name until it is used.
|
|
425
|
+
const PROPS_OF = [
|
|
426
|
+
'interface Intrinsic { div: {title?: string}; span: {lang?: string} }',
|
|
427
|
+
'type ElementType = keyof Intrinsic | ((props: any) => unknown);',
|
|
428
|
+
'type PropsOf<E> = E extends (props: infer P) => unknown ? P : E extends keyof Intrinsic ? Intrinsic[E] : {};',
|
|
429
|
+
"type Prefixed<N extends string> = {[K in 'value' | 'change' as `${N}${Capitalize<K>}`]: string};",
|
|
430
|
+
"type Pair<N extends string> = {label: string} & {[K in 'a' | 'b' as `${N}${K}`]: string};",
|
|
431
|
+
'type Relayed<M extends string> = Prefixed<M>;',
|
|
432
|
+
'type Except<O, X extends keyof O> = {[K in keyof O as K extends X ? never : K]: O[K]};',
|
|
433
|
+
'type PickByValue<O, V> = {[K in keyof O as O[K] extends V ? K : never]: O[K]};',
|
|
434
|
+
];
|
|
435
|
+
const generics = Object.entries({
|
|
436
|
+
Poly: "export type PolyProps<E extends ElementType> = {as?: E; padding?: number} & Omit<PropsOf<E>, 'as' | 'padding'>; export function Poly<E extends ElementType = 'div'>(props: PolyProps<E>) { return null; }",
|
|
437
|
+
Joint:
|
|
438
|
+
"export function Joint<E extends ElementType = 'div'>(props: {as?: E} & PropsOf<E>) { return null; }",
|
|
439
|
+
Partly:
|
|
440
|
+
"export function Partly<E extends ElementType = 'div'>(props: Partial<PropsOf<E>>) { return null; }",
|
|
441
|
+
Remapped:
|
|
442
|
+
'export function Remapped<T extends Record<string, unknown> = {value: string}>(props: {[K in keyof T & string as `on${Capitalize<K>}Change`]?: (value: T[K]) => void}) { return null; }',
|
|
443
|
+
Inferred:
|
|
444
|
+
'interface InferredBase { label?: string } export function Inferred<P extends InferredBase>(props: P) { return null; }',
|
|
445
|
+
// An `as` clause that names props from one: through an alias (in an
|
|
446
|
+
// intersection, or through another alias), inline, or behind a
|
|
447
|
+
// condition; and keys made from one.
|
|
448
|
+
PrefixAlias:
|
|
449
|
+
'export function PrefixAlias<N extends string>(props: Prefixed<N>) { return null; }',
|
|
450
|
+
PrefixNested:
|
|
451
|
+
'export function PrefixNested<N extends string>(props: Pair<N>) { return null; }',
|
|
452
|
+
PrefixDeep:
|
|
453
|
+
'export function PrefixDeep<N extends string>(props: Relayed<N>) { return null; }',
|
|
454
|
+
Templated:
|
|
455
|
+
'export function Templated<K extends string>(props: {[P in `on${K}`]?: () => void}) { return null; }',
|
|
456
|
+
PrefixInline:
|
|
457
|
+
"export function PrefixInline<N extends string>(props: {[K in 'value' | 'change' as `${N}${Capitalize<K>}`]?: string}) { return null; }",
|
|
458
|
+
PrefixGated:
|
|
459
|
+
"export function PrefixGated<T extends boolean>(props: {[K in 'value' | 'change' as T extends true ? K : never]?: string}) { return null; }",
|
|
460
|
+
// Props made from one that show the compiler no members at all.
|
|
461
|
+
Bagged:
|
|
462
|
+
'interface Bag<T> {} export function Bagged<T>(props: Bag<T>) { return null; }',
|
|
463
|
+
}).map(([name, source]) =>
|
|
464
|
+
component(name, [...PROPS_OF, source].join('\n'), {
|
|
465
|
+
props: [prop('elevation', 'number')],
|
|
466
|
+
}),
|
|
467
|
+
);
|
|
468
|
+
// A generic whose own props take any name, or map keys of its own (an
|
|
469
|
+
// `as` clause over its own key, or an alias or key filter given fixed
|
|
470
|
+
// arguments), has names of its own: compared.
|
|
471
|
+
const own = (/** @type {string} */ names) =>
|
|
472
|
+
names.split(' ').map(name => prop(name, 'X', {required: true}));
|
|
473
|
+
const owned = [
|
|
474
|
+
['GenericOpen', '{items: T[]; [key: string]: any}', 'items'],
|
|
475
|
+
['GenericRecord', '{items: T[]} & Record<string, any>', 'items'],
|
|
476
|
+
['GenericRecordKeys', "Record<'value' | 'initial', T>", 'initial value'],
|
|
477
|
+
['GenericFiltered', "{[K in 'a' | 'b' as Exclude<K, 'b'>]: T}", 'a'],
|
|
478
|
+
['GenericPrefixed', "Prefixed<'my'>", 'myChange myValue'],
|
|
479
|
+
['GenericPair', "Pair<'my'>", 'label mya myb'],
|
|
480
|
+
['GenericRelayed', "Relayed<'my'>", 'myChange myValue'],
|
|
481
|
+
[
|
|
482
|
+
'GenericExcept',
|
|
483
|
+
"{items: T[]} & Except<{a: string; b: string}, 'b'>",
|
|
484
|
+
'a items',
|
|
485
|
+
],
|
|
486
|
+
[
|
|
487
|
+
'GenericByValue',
|
|
488
|
+
'{items: T[]} & PickByValue<{onPick: () => void; size: number}, Function>',
|
|
489
|
+
'items onPick',
|
|
490
|
+
],
|
|
491
|
+
].map(([name, props, names]) =>
|
|
492
|
+
component(
|
|
493
|
+
name,
|
|
494
|
+
[
|
|
495
|
+
...PROPS_OF,
|
|
496
|
+
`export function ${name}<T>(props: ${props}) { return null; }`,
|
|
497
|
+
].join('\n'),
|
|
498
|
+
{props: own(names)},
|
|
499
|
+
),
|
|
500
|
+
);
|
|
501
|
+
// Props that are plainly empty, generic or not, are compared: TypeScript
|
|
502
|
+
// takes none of the documented props.
|
|
503
|
+
const empty = ['NoProps', 'NoPropsT<T>'].map(name =>
|
|
504
|
+
component(
|
|
505
|
+
name.replace('<T>', ''),
|
|
506
|
+
`export function ${name}(props: {}) { return null; }\n`,
|
|
507
|
+
{props: [prop('label', 'string')]},
|
|
508
|
+
),
|
|
509
|
+
);
|
|
510
|
+
|
|
511
|
+
const report = await compare([
|
|
512
|
+
...unread,
|
|
513
|
+
lonely,
|
|
514
|
+
open,
|
|
515
|
+
...reached,
|
|
516
|
+
...references,
|
|
517
|
+
untyped,
|
|
518
|
+
...generics,
|
|
519
|
+
...owned,
|
|
520
|
+
...empty,
|
|
521
|
+
]);
|
|
522
|
+
|
|
523
|
+
expect(kinds(report)).toEqual([
|
|
524
|
+
'NoProps.label not_in_code',
|
|
525
|
+
'NoPropsT.label not_in_code',
|
|
526
|
+
]);
|
|
527
|
+
expect(report.components).toBe(13);
|
|
528
|
+
const generic_ =
|
|
529
|
+
'Bagged Inferred Joint Partly Poly PrefixAlias PrefixDeep PrefixGated PrefixInline PrefixNested Remapped Templated'.split(
|
|
530
|
+
' ',
|
|
531
|
+
);
|
|
532
|
+
expect(report.unchecked.map(u => u.component)).toEqual(
|
|
533
|
+
'Aliased Bagged Bare Chosen Classy Comma Deep Documented Effect Fallback Ghost Handlers Header Inferred Joined Joint Kept Keyed Lonely Mapped Member Named Nested Omitted Omitting Partly Peered Phantom Picked Poly PrefixAlias PrefixDeep Prefixed PrefixGated PrefixInline PrefixNested Recorded Referenced Remapped Sized Spread Templated Toned Unnamed Valued Wrapped'.split(
|
|
534
|
+
' ',
|
|
535
|
+
),
|
|
536
|
+
);
|
|
537
|
+
// A type the compiler can't work out, though every reference resolves.
|
|
538
|
+
const untyped_ =
|
|
539
|
+
'Aliased Documented Handlers Omitting Recorded Sized Spread Toned Valued'.split(
|
|
540
|
+
' ',
|
|
541
|
+
);
|
|
542
|
+
for (const {component: name, reason} of report.unchecked) {
|
|
543
|
+
expect(reason).toContain(
|
|
544
|
+
name === 'Lonely'
|
|
545
|
+
? 'Lonely.tsx has no export named Lonely'
|
|
546
|
+
: generic_.includes(name)
|
|
547
|
+
? 'from a type parameter'
|
|
548
|
+
: untyped_.includes(name)
|
|
549
|
+
? "a type the compiler can't work out"
|
|
550
|
+
: 'could not resolve',
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
|
|
2
|
+
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The first reference that does not resolve from the given files, the files
|
|
6
|
+
* they import, and so on, as far as `follow` allows. A reference fails when
|
|
7
|
+
* the program finds no module, no type package or no file for it, when a
|
|
8
|
+
* module does not export a name imported from it, or, in the files `mine`
|
|
9
|
+
* accepts, when a type name resolves to no declaration (TS2304). A module
|
|
10
|
+
* found without types (a JavaScript file) resolves: its names are `any`. A
|
|
11
|
+
* side-effect import counts where the compiler checks one
|
|
12
|
+
* (`noUncheckedSideEffectImports`, on by default from TypeScript 6).
|
|
13
|
+
* @param {any} ts the project's own `typescript`
|
|
14
|
+
* @param {any} program
|
|
15
|
+
* @param {any} checker
|
|
16
|
+
* @param {any[]} roots source files
|
|
17
|
+
* @param {(file: string) => boolean} follow which files it reads past them
|
|
18
|
+
* @param {(file: string) => boolean} mine whose type names it checks
|
|
19
|
+
* @returns {UnresolvedReference | null}
|
|
20
|
+
*/
|
|
21
|
+
export function unresolvedReference(ts: any, program: any, checker: any, roots: any[], follow: (file: string) => boolean, mine: (file: string) => boolean): UnresolvedReference | null;
|
|
22
|
+
/**
|
|
23
|
+
* The first reference that does not resolve from the program's global roots,
|
|
24
|
+
* whose types every file sees: a type package the program loads by name
|
|
25
|
+
* (`compilerOptions.types`, `*` expanded as TypeScript does, or every
|
|
26
|
+
* package in `typeRoots` where TypeScript loads them all) that cannot be
|
|
27
|
+
* found, or a reference in a declaration file the package's own config
|
|
28
|
+
* includes, wherever it sits, or in the package's own type packages, or in a
|
|
29
|
+
* file they import. Of those, only a file that acts globally starts the walk:
|
|
30
|
+
* a script, `declare global`, `declare module`, `export as namespace`, an
|
|
31
|
+
* import that takes no names (a side-effect import, `import {} from`), a
|
|
32
|
+
* bare `export *`, or a `/// <reference>`. A config from above the package,
|
|
33
|
+
* such as a monorepo's root config, may include the whole repository, so
|
|
34
|
+
* only the package's own declaration files start the walk then. A
|
|
35
|
+
* dependency's declarations (below `node_modules`) are not followed: `tsc`
|
|
36
|
+
* skips their errors (`skipLibCheck`), and they reach a component only
|
|
37
|
+
* through the globals they declare.
|
|
38
|
+
* @param {any} ts the project's own `typescript`
|
|
39
|
+
* @param {any} program
|
|
40
|
+
* @param {any} checker
|
|
41
|
+
* @param {string[]} declarations the declaration files the config includes
|
|
42
|
+
* @param {any} options the program's compiler options
|
|
43
|
+
* @param {(file: string) => boolean} isOwnFile whether a file is the
|
|
44
|
+
* package's own
|
|
45
|
+
* @returns {UnresolvedReference | null}
|
|
46
|
+
*/
|
|
47
|
+
export function unresolvedGlobals(ts: any, program: any, checker: any, declarations: string[], options: any, isOwnFile: (file: string) => boolean): UnresolvedReference | null;
|
|
48
|
+
export type UnresolvedReference = {
|
|
49
|
+
/**
|
|
50
|
+
* the reference as written, quoted
|
|
51
|
+
*/
|
|
52
|
+
name: string;
|
|
53
|
+
/**
|
|
54
|
+
* the file that writes it
|
|
55
|
+
*/
|
|
56
|
+
file: string;
|
|
57
|
+
/**
|
|
58
|
+
* a file imports it, a file
|
|
59
|
+
* uses it as a type name, or the config names it
|
|
60
|
+
*/
|
|
61
|
+
how: "imported" | "used" | "named";
|
|
62
|
+
};
|