@astryxdesign/cli 0.7.2-canary.4787e3f → 0.7.2-canary.4a2f608
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 +74 -73
- package/api/build/help/help.mjs +1 -1
- package/api/discover/_adapter.d.mts +119 -12
- package/api/discover/_adapter.mjs +449 -10
- package/api/discover/_adapter.test.mjs +599 -17
- package/api/discover/_catalog-view.d.mts +16 -5
- package/api/discover/_catalog-view.mjs +32 -8
- package/api/discover/discover.d.mts +11 -0
- package/api/discover/discover.doc.mjs +7 -6
- package/api/discover/discover.mjs +107 -19
- package/api/discover/discover.type.d.mts +18 -7
- package/api/discover/discover.type.mjs +15 -7
- package/api/discover/list/list.d.mts +4 -3
- package/api/discover/list/list.mjs +17 -10
- package/api/docs/_adapter.d.mts +11 -0
- package/api/docs/_adapter.mjs +244 -1
- package/api/docs/integrationDocs.test.mjs +124 -0
- package/api/doctor/doctor.mjs +27 -3
- package/api/doctor/doctor.test.mjs +43 -0
- package/api/doctor/theme-checks.mjs +1 -1
- package/api/doctor/theme-checks.test.mjs +136 -0
- package/api/gap-report/gap-report.d.mts +3 -0
- package/api/gap-report/gap-report.doc.mjs +11 -3
- package/api/gap-report/gap-report.mjs +152 -48
- package/api/gap-report/gap-report.test.mjs +233 -0
- package/api/gap-report/gap-report.type.d.mts +41 -0
- package/api/gap-report/gap-report.type.mjs +18 -0
- package/api/index.d.mts +2 -0
- package/api/index.mjs +2 -0
- package/api/init/run/run.mjs +2 -2
- package/api/integration/_adapter.d.mts +32 -0
- package/api/integration/_adapter.mjs +80 -0
- package/api/integration/add-contribution.mjs +1 -1
- package/api/integration/add-contribution.test.mjs +4 -2
- package/api/integration/authoring-checks.mjs +44 -4
- package/api/integration/authoring-checks.test.mjs +515 -0
- package/api/integration/authoring-checks.type.d.mts +95 -0
- package/api/integration/authoring-checks.type.mjs +53 -2
- package/api/integration/check/check.d.mts +96 -0
- package/api/integration/check/check.mjs +357 -0
- package/api/integration/check/check.test.mjs +489 -0
- package/api/integration/integration-check.type.d.mts +118 -0
- package/api/integration/integration-check.type.mjs +84 -0
- package/api/integration/integrationCheck.doc.d.mts +10 -0
- package/api/integration/integrationCheck.doc.mjs +73 -0
- package/api/integration/integrationComponentConflicts.doc.mjs +43 -6
- package/api/integration/integrationDocConflicts.doc.mjs +19 -5
- package/api/integration/pack-check.mjs +292 -7
- package/api/integration/pack-check.test.mjs +341 -0
- package/api/integration/validate-integration.mjs +8 -0
- package/api/integration/validate-integration.test.mjs +11 -0
- package/api/integration/validate-integration.type.d.mts +8 -0
- package/api/integration/validate-integration.type.mjs +12 -1
- package/api/integration/validateIntegration.doc.mjs +19 -3
- package/api/search/search.d.mts +72 -12
- package/api/search/search.domain-priority.test.mjs +108 -18
- package/api/search/search.integration-check.test.mjs +24 -0
- package/api/search/search.mjs +501 -37
- package/api/search/search.test.mjs +139 -0
- package/api/theme/build/build.family.test.mjs +54 -6
- package/api/theme/build/build.mjs +114 -17
- package/api/theme/themeBuild.doc.mjs +4 -4
- package/api/upgrade/_adapter.d.mts +40 -2
- package/api/upgrade/_adapter.mjs +117 -4
- package/api/upgrade/run/cli-behind-core.test.mjs +438 -0
- package/api/upgrade/run/run.mjs +30 -1
- package/api/upgrade/status/status.d.mts +21 -5
- package/api/upgrade/status/status.mjs +45 -8
- package/api/upgrade/upgrade.doc.mjs +5 -3
- package/api/upgrade/upgrade.type.d.mts +23 -0
- package/api/upgrade/upgrade.type.mjs +16 -1
- package/assets/codemods/runner.mjs +7 -1
- package/assets/discover/official-packages.json +367 -0
- package/assets/docs/README.md +10 -0
- package/assets/docs/author-a-theme.doc.mjs +3 -3
- package/assets/docs/browser-support.doc.mjs +1 -1
- package/assets/docs/getting-started.doc.mjs +3 -3
- package/assets/docs/styling-overview.doc.mjs +1 -1
- package/assets/docs/theme.doc.dense.mjs +1 -1
- package/assets/docs/theme.doc.mjs +2 -2
- package/assets/docs/theme.doc.zh.mjs +1 -1
- package/assets/docs/tree/add-a-component.doc.mjs +1 -1
- package/assets/docs/tree/add-a-theme.doc.mjs +1 -1
- package/assets/docs/tree/add-a-topic.doc.mjs +11 -8
- package/assets/docs/tree/agent-guidance.doc.mjs +2 -2
- package/assets/docs/tree/block-template.doc.mjs +8 -20
- package/assets/docs/tree/build-the-template.doc.mjs +1 -1
- package/assets/docs/tree/check-the-props.doc.mjs +196 -0
- package/assets/docs/tree/check-your-docs.doc.mjs +61 -7
- package/assets/docs/tree/checks.doc.mjs +25 -17
- package/assets/docs/tree/codemods.doc.mjs +4 -4
- package/assets/docs/tree/component-family.doc.mjs +3 -5
- package/assets/docs/tree/component-imports.doc.mjs +2 -2
- package/assets/docs/tree/components.doc.mjs +1 -1
- package/assets/docs/tree/debug-and-gap-reports.doc.mjs +30 -21
- package/assets/docs/tree/define-the-theme.doc.mjs +6 -5
- package/assets/docs/tree/describe-the-component.doc.mjs +2 -2
- package/assets/docs/tree/docs.doc.mjs +3 -3
- package/assets/docs/tree/document-the-template.doc.mjs +1 -1
- package/assets/docs/tree/document-the-theme.doc.mjs +1 -1
- package/assets/docs/tree/export-template-assets.doc.mjs +1 -1
- package/assets/docs/tree/extend-or-replace.doc.mjs +6 -6
- package/assets/docs/tree/fonts-and-assets.doc.mjs +2 -2
- package/assets/docs/tree/generate-a-palette.doc.mjs +5 -3
- package/assets/docs/tree/grade-template-with-agent.doc.mjs +1 -1
- package/assets/docs/tree/integrations.doc.mjs +8 -0
- package/assets/docs/tree/layout-spacing.doc.mjs +2 -3
- package/assets/docs/tree/layout.doc.mjs +6 -6
- package/assets/docs/tree/links.doc.mjs +6 -5
- package/assets/docs/tree/migration-setup.doc.mjs +3 -2
- package/assets/docs/tree/migration-verification.doc.mjs +0 -1
- package/assets/docs/tree/migration.doc.mjs +0 -1
- package/assets/docs/tree/package-and-test.doc.mjs +1 -1
- package/assets/docs/tree/page-template.doc.mjs +4 -8
- package/assets/docs/tree/publishing.doc.mjs +4 -4
- package/assets/docs/tree/quick-start.doc.mjs +38 -22
- package/assets/docs/tree/replace-a-core-component.doc.mjs +4 -6
- package/assets/docs/tree/replace-a-core-template.doc.mjs +6 -8
- package/assets/docs/tree/sections-and-placement.doc.mjs +6 -8
- package/assets/docs/tree/see-it-in-an-app.doc.mjs +4 -2
- package/assets/docs/tree/short-and-findable.doc.mjs +6 -5
- package/assets/docs/tree/single-component.doc.mjs +14 -5
- package/assets/docs/tree/start-a-template.doc.mjs +3 -3
- package/assets/docs/tree/subcomponent.doc.mjs +6 -6
- package/assets/docs/tree/template-assets.doc.mjs +2 -2
- package/assets/docs/tree/template-doc-overview.doc.mjs +5 -16
- package/assets/docs/tree/template-fonts.doc.mjs +1 -1
- package/assets/docs/tree/template-icons.doc.mjs +1 -1
- package/assets/docs/tree/template-images-media.doc.mjs +1 -1
- package/assets/docs/tree/template-styles.doc.mjs +2 -2
- package/assets/docs/tree/templates.doc.mjs +1 -1
- package/assets/docs/tree/test-in-an-app.doc.mjs +3 -3
- package/assets/docs/tree/test-template-in-app.doc.mjs +1 -1
- package/assets/docs/tree/tokens-and-setup.doc.mjs +19 -15
- package/assets/docs/tree/troubleshooting.doc.mjs +8 -8
- package/assets/docs/tree/type-scale.doc.mjs +3 -3
- package/assets/docs/tree/upgrading.doc.mjs +4 -4
- package/assets/docs/tree/use-a-theme-in-an-app.doc.mjs +3 -1
- package/assets/docs/tree/verify-packed-template.doc.mjs +1 -1
- package/assets/docs/tree/versioning.doc.mjs +6 -6
- package/assets/docs/tree/write-the-template-file.doc.mjs +4 -3
- package/assets/docs/use-a-theme.doc.mjs +2 -2
- package/assets/theme.template.ts +1 -1
- package/authoring/codemod/codemod.doc.mjs +7 -3
- package/authoring/config/config.doc.mjs +9 -6
- package/authoring/config/type.ts +4 -2
- package/authoring/discover/discover.doc.mjs +1 -1
- package/authoring/doctypes/base/graph-fields.doc.mjs +6 -4
- package/authoring/doctypes/command/command.doc.mjs +14 -12
- package/authoring/doctypes/component/component.doc.mjs +29 -26
- package/authoring/doctypes/component/type.ts +7 -5
- package/authoring/doctypes/enum/enum.doc.mjs +7 -4
- package/authoring/doctypes/function/function.doc.mjs +9 -8
- package/authoring/doctypes/hook/hook.doc.mjs +5 -4
- package/authoring/doctypes/namespace/namespace.doc.mjs +7 -6
- package/authoring/doctypes/reference/reference.doc.mjs +11 -10
- package/authoring/doctypes/schema/schema.doc.mjs +9 -9
- package/authoring/doctypes/template/integration-template.doc.d.mts +11 -0
- package/authoring/doctypes/template/integration-template.doc.mjs +49 -0
- package/authoring/doctypes/template/parse.d.mts +6 -2
- package/authoring/doctypes/template/parse.mjs +3 -1
- package/authoring/doctypes/template/template.doc.mjs +3 -3
- package/authoring/doctypes/template/type.ts +22 -0
- package/authoring/doctypes/theme/theme.doc.mjs +2 -2
- package/authoring/gap-report/gap-report.doc.mjs +2 -2
- package/authoring/identity/identity.doc.mjs +3 -3
- package/authoring/index.d.ts +1 -0
- package/authoring/integration/integration.doc.mjs +12 -4
- package/authoring/integration/parse.test.mjs +10 -0
- package/authoring/integration/schema.d.mts +2 -0
- package/authoring/integration/schema.mjs +1 -0
- package/authoring/integration/type.ts +4 -0
- package/clients/cli/commands/build-theme.batch-report.test.mjs +3 -1
- package/clients/cli/commands/build-theme.mjs +1 -1
- package/clients/cli/commands/build-theme.path-safety.test.mjs +155 -21
- package/clients/cli/commands/cli-postinstall.test.mjs +49 -1
- package/clients/cli/commands/discover.broken-integration.test.mjs +4 -1
- package/clients/cli/commands/discover.doc.mjs +1 -1
- package/clients/cli/commands/discover.mjs +144 -93
- package/clients/cli/commands/discover.no-source.test.mjs +447 -33
- package/clients/cli/commands/discover.sources.test.mjs +20 -1
- package/clients/cli/commands/doctor-integration-components.doc.mjs +17 -4
- package/clients/cli/commands/doctor-integration-docs.doc.mjs +18 -5
- package/clients/cli/commands/doctor-integration-validate.doc.mjs +13 -2
- package/clients/cli/commands/doctor-integration.test.mjs +837 -0
- package/clients/cli/commands/doctor.mjs +250 -3
- package/clients/cli/commands/gap-report.doc.mjs +14 -2
- package/clients/cli/commands/gap-report.mjs +24 -2
- package/clients/cli/commands/gap-report.test.mjs +79 -0
- package/clients/cli/commands/integration-check.doc.mjs +85 -0
- package/clients/cli/commands/integration-check.test.mjs +370 -0
- package/clients/cli/commands/integration.doc.mjs +6 -2
- package/clients/cli/commands/integration.mjs +106 -1
- package/clients/cli/commands/json-contract.test.mjs +5 -1
- package/clients/cli/commands/setup-nudge.test.mjs +79 -1
- package/clients/cli/commands/swizzle.mjs +5 -3
- package/clients/cli/commands/swizzle.routing.test.mjs +18 -1
- package/clients/cli/commands/text-json-parity.test.mjs +5 -0
- package/clients/cli/commands/theme-build.doc.mjs +2 -2
- package/clients/cli/commands/upgrade.doc.mjs +14 -0
- package/clients/cli/commands/upgrade.older-cli.test.mjs +160 -0
- package/clients/cli/index.mjs +30 -5
- package/foundation/agent-docs/agent-doc-state.d.mts +14 -0
- package/foundation/agent-docs/agent-doc-state.mjs +38 -0
- package/foundation/agent-docs/agent-docs.d.mts +2 -1
- package/foundation/agent-docs/agent-docs.mjs +8 -6
- package/foundation/config/project.mjs +82 -0
- package/foundation/discovery/authoring-self-docs.mjs +2 -1
- package/foundation/discovery/component-discovery.d.mts +18 -0
- package/foundation/discovery/component-discovery.mjs +2 -2
- package/foundation/discovery/copied-passages.d.mts +216 -0
- package/foundation/discovery/copied-passages.mjs +720 -0
- package/foundation/discovery/copied-passages.test.mjs +124 -0
- package/foundation/discovery/doc-fluff.d.mts +112 -0
- package/foundation/discovery/doc-fluff.mjs +328 -0
- package/foundation/discovery/doc-fluff.test.mjs +142 -0
- package/foundation/discovery/doc-types.d.mts +157 -0
- package/foundation/discovery/doc-types.mjs +1224 -0
- package/foundation/discovery/doc-types.test.mjs +700 -0
- package/foundation/discovery/docs-discovery.d.mts +88 -0
- package/foundation/discovery/docs-discovery.mjs +83 -2
- package/foundation/discovery/integration-doc-files.d.mts +22 -0
- package/foundation/discovery/integration-doc-files.mjs +231 -0
- package/foundation/discovery/integration-doc-files.test.mjs +246 -0
- package/foundation/discovery/project-typescript.d.mts +11 -2
- package/foundation/discovery/project-typescript.mjs +32 -4
- package/foundation/discovery/prop-drift.d.mts +116 -3
- package/foundation/discovery/prop-drift.mjs +883 -41
- package/foundation/discovery/prop-drift.test.mjs +1738 -20
- package/foundation/discovery/type-match.d.mts +53 -0
- package/foundation/discovery/type-match.mjs +660 -0
- package/foundation/discovery/type-match.test.mjs +169 -0
- package/foundation/discovery/type-text.d.mts +68 -0
- package/foundation/discovery/type-text.mjs +658 -0
- package/foundation/discovery/type-text.test.mjs +196 -0
- package/foundation/doc-compiler/doc-loads.test.mjs +16 -2
- package/foundation/doc-compiler/links.d.mts +9 -5
- package/foundation/doc-compiler/links.mjs +4 -2
- package/foundation/env/package-manager.d.mts +32 -0
- package/foundation/env/package-manager.mjs +47 -0
- package/foundation/env/package-manager.test.mjs +194 -7
- package/foundation/fs/module-loader.d.mts +2 -1
- package/foundation/fs/module-loader.mjs +60 -4
- package/foundation/fs/module-loader.test.mjs +194 -0
- package/foundation/response/response-types.doc.mjs +14 -8
- package/package.json +11 -11
- package/scripts/postinstall.mjs +3 -3
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @file Documented props against a component's TypeScript props: each kind
|
|
5
|
-
* of finding,
|
|
5
|
+
* of finding, the loose type rules, the props a doc never needs, and the
|
|
6
|
+
* visible skip when the project has no TypeScript.
|
|
6
7
|
*/
|
|
7
8
|
|
|
8
9
|
import * as fs from 'node:fs';
|
|
@@ -10,7 +11,13 @@ import * as os from 'node:os';
|
|
|
10
11
|
import * as path from 'node:path';
|
|
11
12
|
import {createRequire} from 'node:module';
|
|
12
13
|
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
|
|
13
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
comparePropDocs,
|
|
16
|
+
comparePropTables,
|
|
17
|
+
PROPS_NOT_DOCUMENTED,
|
|
18
|
+
TYPESCRIPT_MISSING_REASON,
|
|
19
|
+
} from './prop-drift.mjs';
|
|
20
|
+
import {unusableTypeScriptReason} from './project-typescript.mjs';
|
|
14
21
|
|
|
15
22
|
const require = createRequire(import.meta.url);
|
|
16
23
|
const TYPESCRIPT_DIR = path.dirname(require.resolve('typescript/package.json'));
|
|
@@ -51,6 +58,37 @@ function withTypeScript() {
|
|
|
51
58
|
);
|
|
52
59
|
}
|
|
53
60
|
|
|
61
|
+
/**
|
|
62
|
+
* TypeScript 7 (no JavaScript compiler API) with the TypeScript 6 API package
|
|
63
|
+
* beside it, which re-exports a real TypeScript.
|
|
64
|
+
*/
|
|
65
|
+
function withTypeScript7AndItsTypeScript6Api() {
|
|
66
|
+
const ts7 = path.join(pkg, 'node_modules', 'typescript');
|
|
67
|
+
fs.mkdirSync(ts7, {recursive: true});
|
|
68
|
+
fs.writeFileSync(
|
|
69
|
+
path.join(ts7, 'package.json'),
|
|
70
|
+
JSON.stringify({
|
|
71
|
+
name: 'typescript',
|
|
72
|
+
version: '7.0.2',
|
|
73
|
+
exports: {'./package.json': './package.json'},
|
|
74
|
+
}),
|
|
75
|
+
);
|
|
76
|
+
const ts6 = path.join(pkg, 'node_modules', '@typescript', 'typescript6');
|
|
77
|
+
fs.mkdirSync(ts6, {recursive: true});
|
|
78
|
+
fs.writeFileSync(
|
|
79
|
+
path.join(ts6, 'package.json'),
|
|
80
|
+
JSON.stringify({
|
|
81
|
+
name: '@typescript/typescript6',
|
|
82
|
+
version: '6.0.2',
|
|
83
|
+
main: 'index.js',
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
86
|
+
fs.writeFileSync(
|
|
87
|
+
path.join(ts6, 'index.js'),
|
|
88
|
+
`module.exports = require(${JSON.stringify(TYPESCRIPT_DIR)});\n`,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
54
92
|
/**
|
|
55
93
|
* Write `components/<name>.tsx` and its doc; returns its prop table, which
|
|
56
94
|
* also carries the component record (name, docPath, sourcePath).
|
|
@@ -106,7 +144,174 @@ const kinds = report =>
|
|
|
106
144
|
report.findings.map(f => `${f.component}.${f.prop} ${f.kind}`);
|
|
107
145
|
|
|
108
146
|
describe('comparePropTables', () => {
|
|
109
|
-
it(
|
|
147
|
+
it('judges the type of a prop a mapped type of the package builds, and says why one another package builds is not judged', async () => {
|
|
148
|
+
withTypeScript();
|
|
149
|
+
const sizes = path.join(pkg, 'node_modules', '@acme', 'sizes');
|
|
150
|
+
fs.mkdirSync(sizes, {recursive: true});
|
|
151
|
+
fs.writeFileSync(
|
|
152
|
+
path.join(sizes, 'package.json'),
|
|
153
|
+
JSON.stringify({name: '@acme/sizes', types: 'index.d.ts'}),
|
|
154
|
+
);
|
|
155
|
+
fs.writeFileSync(
|
|
156
|
+
path.join(sizes, 'index.d.ts'),
|
|
157
|
+
"export type Sizes = Partial<Record<'sm' | 'md', string>>;\n",
|
|
158
|
+
);
|
|
159
|
+
const tables = [
|
|
160
|
+
component(
|
|
161
|
+
'Mapped',
|
|
162
|
+
"export function Mapped(props: Partial<Record<'a' | 'b', 'x' | 'y'>>) { return null; }",
|
|
163
|
+
{props: [prop('a', "'x' | 'z'"), prop('b', "'x' | 'y'")]},
|
|
164
|
+
),
|
|
165
|
+
component(
|
|
166
|
+
'HandledCell',
|
|
167
|
+
"import type {Sizes} from '@acme/sizes';\nexport function HandledCell(props: Sizes) { return null; }",
|
|
168
|
+
{props: [prop('sm', 'number')]},
|
|
169
|
+
),
|
|
170
|
+
// Keys the check can't trace, and a doc type that does not parse; and
|
|
171
|
+
// a name that starts with another's sorts after it.
|
|
172
|
+
component(
|
|
173
|
+
'Handled',
|
|
174
|
+
"type A = string;\nexport function Handled(props: {[K in `on${'Open' | 'Close'}`]?: () => void} & {items?: ((items: A[]) => A)[]}) { return null; }",
|
|
175
|
+
{props: [prop('onOpen', '() => A'), prop('items', '((A[]) => A)[]')]},
|
|
176
|
+
),
|
|
177
|
+
];
|
|
178
|
+
|
|
179
|
+
const report = await compare(tables);
|
|
180
|
+
|
|
181
|
+
expect(kinds(report)).toEqual(['Mapped.a type_mismatch']);
|
|
182
|
+
expect(report.typesNotCompared).toEqual(
|
|
183
|
+
[
|
|
184
|
+
['Handled', 'not a form the check judges exactly', ['items', 'onOpen']],
|
|
185
|
+
['HandledCell', 'declared outside the package, not compared', ['sm']],
|
|
186
|
+
].map(([component, reason, props]) => ({
|
|
187
|
+
component,
|
|
188
|
+
doc: `components/${component}.doc.mjs`,
|
|
189
|
+
reason,
|
|
190
|
+
props,
|
|
191
|
+
})),
|
|
192
|
+
);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('judges no prop type, and says why, with a TypeScript older than 4.8', async () => {
|
|
196
|
+
const dir = path.join(pkg, 'node_modules', 'typescript');
|
|
197
|
+
fs.mkdirSync(dir, {recursive: true});
|
|
198
|
+
fs.writeFileSync(path.join(dir, 'package.json'), '{"version": "4.7.4"}');
|
|
199
|
+
const real = JSON.stringify(TYPESCRIPT_DIR);
|
|
200
|
+
fs.writeFileSync(
|
|
201
|
+
path.join(dir, 'index.js'),
|
|
202
|
+
`module.exports = {...require(${real}), version: '4.7.4'};`,
|
|
203
|
+
);
|
|
204
|
+
const source = 'export function Badge(p: {size?: string}) { return null; }';
|
|
205
|
+
const doc = {props: [prop('size', 'number')]};
|
|
206
|
+
|
|
207
|
+
expect(await compare([component('Badge', source, doc)])).toMatchObject({
|
|
208
|
+
findings: [],
|
|
209
|
+
typesCompared: 0,
|
|
210
|
+
typesSkipped:
|
|
211
|
+
'TypeScript 4.7.4 is older than 4.8, the oldest the type comparison reads; install TypeScript 5.0 through 6 (`npm install -D typescript@6`), or on TypeScript 7 add `@typescript/typescript6`, to judge them',
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("judges a prop a package's mapping rewrites by its type, and keeps the text of one a mapping copies", async () => {
|
|
216
|
+
withTypeScript();
|
|
217
|
+
const types = `interface Base { size: 'sm' | 'md' }
|
|
218
|
+
interface Opt { tone?: 'info' | undefined }
|
|
219
|
+
type Many<T> = {[K in keyof T]: T[K][]};
|
|
220
|
+
type Nullable<T> = {[K in keyof T]: T[K] | null};
|
|
221
|
+
interface Pickable<T = string> { pick?: (value: T) => void }
|
|
222
|
+
type ChooserProps<T extends string = string> = {pick?: string} & {
|
|
223
|
+
change?: (value: readonly T[]) => void | Promise<void>;
|
|
224
|
+
};
|
|
225
|
+
interface ListProps<T> { value?: T; onChange?: (v: T) => void }
|
|
226
|
+
type MultiProps<T> = ListProps<T[]>;
|
|
227
|
+
import type {Item as Imported} from './item';
|
|
228
|
+
import type * as NS from './item';
|
|
229
|
+
interface Item { a: string }
|
|
230
|
+
interface ItemList<Item> { items?: Item[] }`;
|
|
231
|
+
const size = {props: [prop('size', "'sm' | 'md'", {required: true})]};
|
|
232
|
+
const pick = {props: [prop('pick', '(value: T) => void')]};
|
|
233
|
+
const tables = [
|
|
234
|
+
['Many', 'Many<Base>', size],
|
|
235
|
+
['Nulls', 'Nullable<Base>', size],
|
|
236
|
+
['Picked', "Pick<Base, 'size'>", size],
|
|
237
|
+
[
|
|
238
|
+
'Strict',
|
|
239
|
+
'Required<Opt>',
|
|
240
|
+
{props: [prop('tone', "'info' | undefined", {required: true})]},
|
|
241
|
+
],
|
|
242
|
+
// A generic component's prop keeps its declaration's text; one a
|
|
243
|
+
// concrete type instantiates does not.
|
|
244
|
+
['Boxed', 'Pickable<number>', pick],
|
|
245
|
+
[
|
|
246
|
+
'Chooser',
|
|
247
|
+
'ChooserProps<T>',
|
|
248
|
+
{
|
|
249
|
+
props: [
|
|
250
|
+
prop('pick', 'string'),
|
|
251
|
+
prop('change', '(value: readonly T[]) => void | Promise<void>'),
|
|
252
|
+
],
|
|
253
|
+
},
|
|
254
|
+
'<T extends string = string>',
|
|
255
|
+
],
|
|
256
|
+
// Instantiated with a type built from the component's own parameter,
|
|
257
|
+
// the declaration's `T` is no longer the prop's type.
|
|
258
|
+
// A type argument that prints with the parameter's name elsewhere is
|
|
259
|
+
// still another type here.
|
|
260
|
+
['Renamed', 'ItemList<Imported>', {props: [prop('items', 'Item[]')]}],
|
|
261
|
+
['Spaced', 'ItemList<NS.Item>', {props: [prop('items', 'Item[]')]}],
|
|
262
|
+
['Owned', 'ItemList<Item>', {props: [prop('items', 'Item[]')]}],
|
|
263
|
+
...[
|
|
264
|
+
['Listed', 'ListProps<T[]>', 'T', '(v: T) => void'],
|
|
265
|
+
['Maybe', 'ListProps<T | null>', 'T', '(v: T) => void'],
|
|
266
|
+
['Multi', 'MultiProps<T>', 'T[]', '(v: T[]) => void'],
|
|
267
|
+
].map(([name, props, value, onChange]) => [
|
|
268
|
+
name,
|
|
269
|
+
props,
|
|
270
|
+
{props: [prop('value', value), prop('onChange', onChange)]},
|
|
271
|
+
'<T>',
|
|
272
|
+
]),
|
|
273
|
+
].map(([name, props, doc, generics = '']) =>
|
|
274
|
+
component(
|
|
275
|
+
name,
|
|
276
|
+
`${types}\nexport function ${name}${generics}(props: ${props}) { return null; }`,
|
|
277
|
+
doc,
|
|
278
|
+
),
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
fs.writeFileSync(
|
|
282
|
+
path.join(pkg, 'components', 'item.ts'),
|
|
283
|
+
'export interface Item { b: number }\n',
|
|
284
|
+
);
|
|
285
|
+
const report = await compare(tables);
|
|
286
|
+
|
|
287
|
+
expect(kinds(report)).toEqual([
|
|
288
|
+
'Many.size type_mismatch',
|
|
289
|
+
'Nulls.size type_mismatch',
|
|
290
|
+
'Strict.tone type_mismatch',
|
|
291
|
+
]);
|
|
292
|
+
// A message shows the type the mapping makes, not the source's text.
|
|
293
|
+
expect(report.findings.map(f => f.codeType)).toEqual([
|
|
294
|
+
"('sm' | 'md')[]",
|
|
295
|
+
"'sm' | 'md' | null",
|
|
296
|
+
"'info'",
|
|
297
|
+
]);
|
|
298
|
+
expect(report.typesNotCompared).toEqual(
|
|
299
|
+
[
|
|
300
|
+
['Boxed', ['pick']],
|
|
301
|
+
['Listed', ['onChange', 'value']],
|
|
302
|
+
['Maybe', ['onChange', 'value']],
|
|
303
|
+
['Renamed', ['items']],
|
|
304
|
+
['Spaced', ['items']],
|
|
305
|
+
].map(([component, props]) => ({
|
|
306
|
+
component,
|
|
307
|
+
doc: `components/${component}.doc.mjs`,
|
|
308
|
+
reason: 'not a form the check judges exactly',
|
|
309
|
+
props,
|
|
310
|
+
})),
|
|
311
|
+
);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("reports a prop missing from the code, one missing from the doc, a wrong required flag, and a wrong type, each component's together", async () => {
|
|
110
315
|
withTypeScript();
|
|
111
316
|
const badge = component(
|
|
112
317
|
'Badge',
|
|
@@ -132,27 +337,882 @@ export function Badge(props: BadgeProps) { return null; }`,
|
|
|
132
337
|
{props: []},
|
|
133
338
|
);
|
|
134
339
|
|
|
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);
|
|
340
|
+
const report = await compare([count, badge]);
|
|
341
|
+
|
|
342
|
+
expect(report.status).toBe('checked');
|
|
343
|
+
expect(report.typescript).toMatch(/^\d+\.\d+/);
|
|
344
|
+
expect(report.components).toBe(2);
|
|
345
|
+
expect(kinds(report)).toEqual([
|
|
346
|
+
'Badge.count not_in_doc',
|
|
347
|
+
'Badge.label required_mismatch',
|
|
348
|
+
'Badge.size not_in_code',
|
|
349
|
+
'Badge.tone type_mismatch',
|
|
350
|
+
'BadgeCount.amount not_in_doc',
|
|
351
|
+
]);
|
|
352
|
+
expect(report.findings.every(f => f.severity === 'warning')).toBe(true);
|
|
353
|
+
expect(report.findings.find(f => f.kind === 'type_mismatch')).toMatchObject(
|
|
354
|
+
{
|
|
355
|
+
docType: "'neutral' | 'info'",
|
|
356
|
+
codeType: 'Tone',
|
|
357
|
+
doc: 'components/Badge.doc.mjs',
|
|
358
|
+
source: 'components/Badge.tsx',
|
|
359
|
+
message: expect.stringContaining(
|
|
360
|
+
"Tone ('neutral' | 'info' | 'danger')",
|
|
361
|
+
),
|
|
362
|
+
},
|
|
363
|
+
);
|
|
364
|
+
expect(
|
|
365
|
+
report.findings.find(f => f.kind === 'required_mismatch'),
|
|
366
|
+
).toMatchObject({docRequired: false, codeRequired: true});
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("judges a field, a name an index signature takes, and a prop declared in another file by the type's name where the prop is declared, not by the name the type was declared with", async () => {
|
|
370
|
+
withTypeScript();
|
|
371
|
+
const dir = path.join(pkg, 'components');
|
|
372
|
+
fs.mkdirSync(dir, {recursive: true});
|
|
373
|
+
fs.writeFileSync(
|
|
374
|
+
path.join(dir, 'base.ts'),
|
|
375
|
+
"export interface Item { id: string }\nexport type Size = 'sm' | 'md';\n",
|
|
376
|
+
);
|
|
377
|
+
// Another file imports the same types under other names, and declares
|
|
378
|
+
// its own `Item` and `Size`.
|
|
379
|
+
fs.writeFileSync(
|
|
380
|
+
path.join(dir, 'types.ts'),
|
|
381
|
+
[
|
|
382
|
+
"import type {Item as BaseItem, Size as BaseSize} from './base';",
|
|
383
|
+
'interface Item { label: string }',
|
|
384
|
+
"type Size = 'lg';",
|
|
385
|
+
'export type ElsewhereProps = { size?: BaseSize; item?: BaseItem; onPick?: (item: BaseItem) => void; cfg?: {item: BaseItem}; [key: `size-${string}`]: BaseSize | undefined } & Required<Partial<{tone: BaseSize; entry: BaseItem}>>;',
|
|
386
|
+
].join('\n'),
|
|
387
|
+
);
|
|
388
|
+
const here = component(
|
|
389
|
+
'Renamed',
|
|
390
|
+
[
|
|
391
|
+
"import type {Item as BaseItem, Size as BaseSize} from './base';",
|
|
392
|
+
'interface Item { label: string }',
|
|
393
|
+
"type Size = 'lg';",
|
|
394
|
+
'export function Renamed(props: {cfg?: {item: BaseItem}; [key: `size-${string}`]: BaseSize | undefined}) { return null; }',
|
|
395
|
+
].join('\n'),
|
|
396
|
+
{props: [prop('cfg.item', 'Item'), prop('size-a', 'Size')]},
|
|
397
|
+
);
|
|
398
|
+
const elsewhere = component(
|
|
399
|
+
'Elsewhere',
|
|
400
|
+
"import type {ElsewhereProps} from './types';\nexport function Elsewhere(props: ElsewhereProps) { return null; }",
|
|
401
|
+
{
|
|
402
|
+
props: [
|
|
403
|
+
prop('size', 'Size'),
|
|
404
|
+
prop('tone', 'Size', {required: true}),
|
|
405
|
+
prop('entry', 'Item', {required: true}),
|
|
406
|
+
prop('item', 'Item'),
|
|
407
|
+
prop('onPick', '(item: Item) => void'),
|
|
408
|
+
prop('cfg.item', 'Item'),
|
|
409
|
+
prop('size-a', 'Size'),
|
|
410
|
+
],
|
|
411
|
+
},
|
|
412
|
+
);
|
|
413
|
+
// The same docs, naming the types as each file does, are judged matches.
|
|
414
|
+
const named = component(
|
|
415
|
+
'Named',
|
|
416
|
+
[
|
|
417
|
+
"import type {Item as BaseItem, Size as BaseSize} from './base';",
|
|
418
|
+
'export function Named(props: {cfg?: {item: BaseItem}; [key: `size-${string}`]: BaseSize | undefined}) { return null; }',
|
|
419
|
+
].join('\n'),
|
|
420
|
+
{props: [prop('cfg.item', 'BaseItem'), prop('size-a', 'BaseSize')]},
|
|
421
|
+
);
|
|
422
|
+
|
|
423
|
+
const report = await compare([here, elsewhere, named]);
|
|
424
|
+
|
|
425
|
+
// `Size` is `'lg'` where each prop is declared, which TypeScript rejects
|
|
426
|
+
// for `'sm' | 'md'`; `Item` is an object type there, not compared.
|
|
427
|
+
expect(kinds(report)).toEqual([
|
|
428
|
+
'Elsewhere.size type_mismatch',
|
|
429
|
+
'Elsewhere.size-a type_mismatch',
|
|
430
|
+
'Elsewhere.tone type_mismatch',
|
|
431
|
+
'Renamed.size-a type_mismatch',
|
|
432
|
+
]);
|
|
433
|
+
expect(report.typesNotCompared).toEqual([
|
|
434
|
+
expect.objectContaining({
|
|
435
|
+
component: 'Elsewhere',
|
|
436
|
+
props: ['cfg.item', 'entry', 'item', 'onPick'],
|
|
437
|
+
}),
|
|
438
|
+
expect.objectContaining({component: 'Renamed', props: ['cfg.item']}),
|
|
439
|
+
]);
|
|
440
|
+
expect(report.typesCompared).toBe(6);
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
it("reads `React.X` as React's own where the prop is declared, never as a bare name that means another type there", async () => {
|
|
444
|
+
withTypeScript();
|
|
445
|
+
fs.mkdirSync(path.join(pkg, 'node_modules', '@types'), {recursive: true});
|
|
446
|
+
fs.symlinkSync(
|
|
447
|
+
path.dirname(require.resolve('@types/react/package.json')),
|
|
448
|
+
path.join(pkg, 'node_modules', '@types', 'react'),
|
|
449
|
+
'dir',
|
|
450
|
+
);
|
|
451
|
+
const keys = component(
|
|
452
|
+
'Keys',
|
|
453
|
+
[
|
|
454
|
+
"import * as React from 'react';",
|
|
455
|
+
"type Key = 'a' | 'b';",
|
|
456
|
+
'export function Keys(props: {onKey?: (event: KeyboardEvent) => void; rowKey?: string | number | bigint; pick?: Key; native?: React.Key}) { return null; }',
|
|
457
|
+
].join('\n'),
|
|
458
|
+
{
|
|
459
|
+
props: [
|
|
460
|
+
prop('onKey', '(event: React.KeyboardEvent) => void'),
|
|
461
|
+
prop('rowKey', 'React.Key'),
|
|
462
|
+
prop('pick', 'React.Key'),
|
|
463
|
+
prop('native', 'React.Key'),
|
|
464
|
+
],
|
|
465
|
+
},
|
|
466
|
+
);
|
|
467
|
+
// React's global namespace, with no import.
|
|
468
|
+
const global = component(
|
|
469
|
+
'GlobalKeys',
|
|
470
|
+
'export function GlobalKeys(props: {rowKey?: string | number | bigint}) { return null; }',
|
|
471
|
+
{props: [prop('rowKey', 'React.Key')]},
|
|
472
|
+
);
|
|
473
|
+
|
|
474
|
+
const report = await compare([keys, global]);
|
|
475
|
+
|
|
476
|
+
// The DOM's `KeyboardEvent` is not React's, and the file's own `Key` is
|
|
477
|
+
// not `React.Key`.
|
|
478
|
+
expect(kinds(report)).toEqual(['Keys.pick type_mismatch']);
|
|
479
|
+
expect(report.typesNotCompared).toEqual([
|
|
480
|
+
expect.objectContaining({component: 'Keys', props: ['onKey']}),
|
|
481
|
+
]);
|
|
482
|
+
expect(report.typesCompared).toBe(4);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
it('judges primitives, literals, arrays, string enums, and names for them exactly, in any order', async () => {
|
|
486
|
+
withTypeScript();
|
|
487
|
+
const card = component(
|
|
488
|
+
'Card',
|
|
489
|
+
[
|
|
490
|
+
"type Tone = 'neutral' | 'info';",
|
|
491
|
+
"export enum Color { Red = 'red', Blue = 'blue' } export enum Hue { Red = 'red', Blue = 'blue' }",
|
|
492
|
+
'export interface CardProps {',
|
|
493
|
+
' label: string;',
|
|
494
|
+
' tone?: Tone;',
|
|
495
|
+
' items?: ReadonlyArray<string>;',
|
|
496
|
+
' isOpen?: boolean;',
|
|
497
|
+
" size?: 'sm' | 'md' | number;",
|
|
498
|
+
' color?: Color;',
|
|
499
|
+
' count?: number;',
|
|
500
|
+
' ids?: string[];',
|
|
501
|
+
' hue?: Color;',
|
|
502
|
+
" shade?: Color; shades?: Color[]; pick?: 'red' | 'blue';",
|
|
503
|
+
'}',
|
|
504
|
+
'export function Card(props: CardProps) { return null; }',
|
|
505
|
+
].join('\n'),
|
|
506
|
+
{
|
|
507
|
+
props: [
|
|
508
|
+
prop('label', 'string', {required: true}),
|
|
509
|
+
prop('tone', '\'info\' | "neutral"'),
|
|
510
|
+
prop('items', 'Array<string>'),
|
|
511
|
+
prop('isOpen', 'true | false'),
|
|
512
|
+
prop('size', "number | 'md' | 'sm'"),
|
|
513
|
+
prop('color', 'Color'),
|
|
514
|
+
prop('count', 'string'),
|
|
515
|
+
prop('ids', 'number[]'),
|
|
516
|
+
prop('hue', "'red' | 'blue'"),
|
|
517
|
+
prop('shade', 'Hue'),
|
|
518
|
+
prop('shades', 'Hue[]'),
|
|
519
|
+
prop('pick', 'Color'),
|
|
520
|
+
],
|
|
521
|
+
},
|
|
522
|
+
);
|
|
523
|
+
|
|
524
|
+
const report = await compare([card]);
|
|
525
|
+
|
|
526
|
+
// TypeScript takes neither `'red'` nor another enum's `Hue.Red` where the
|
|
527
|
+
// enum `Color` is expected, and `Color` does not take the `'red'` `pick` does.
|
|
528
|
+
expect(kinds(report)).toEqual([
|
|
529
|
+
'Card.count type_mismatch',
|
|
530
|
+
'Card.hue type_mismatch',
|
|
531
|
+
'Card.ids type_mismatch',
|
|
532
|
+
'Card.pick type_mismatch',
|
|
533
|
+
'Card.shade type_mismatch',
|
|
534
|
+
'Card.shades type_mismatch',
|
|
535
|
+
]);
|
|
536
|
+
expect(report.typesCompared).toBe(12);
|
|
537
|
+
expect(report.typesNotCompared).toEqual([]);
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
it('reads a union the way TypeScript reduces it, and a default after `=` as no alias', async () => {
|
|
541
|
+
withTypeScript();
|
|
542
|
+
const icon = component(
|
|
543
|
+
'Icon',
|
|
544
|
+
[
|
|
545
|
+
"type IconName = 'add' | 'close';",
|
|
546
|
+
"export enum Color { Red = 'red', Blue = 'blue' }",
|
|
547
|
+
'type Step = 1 | 2;',
|
|
548
|
+
"type Size = 'sm' | 'md' | 'lg';",
|
|
549
|
+
'export interface IconProps {',
|
|
550
|
+
' icon?: IconName | string;',
|
|
551
|
+
' color?: Color | string;',
|
|
552
|
+
' step?: Step | number;',
|
|
553
|
+
' isOpen?: boolean;',
|
|
554
|
+
' icons?: string[];',
|
|
555
|
+
' size?: Size;',
|
|
556
|
+
' limit?: 1000 | 2000;',
|
|
557
|
+
' width?: number | string;',
|
|
558
|
+
'}',
|
|
559
|
+
'export function Icon(props: IconProps) { return null; }',
|
|
560
|
+
].join('\n'),
|
|
561
|
+
{
|
|
562
|
+
props: [
|
|
563
|
+
prop('icon', 'IconName | string'),
|
|
564
|
+
prop('color', 'Color | string'),
|
|
565
|
+
prop('step', 'Step | number'),
|
|
566
|
+
prop('isOpen', 'boolean = false'),
|
|
567
|
+
prop('icons', '(IconName | string)[]'),
|
|
568
|
+
prop('size', "Size = 'md'"),
|
|
569
|
+
prop('limit', '1_000 | 2_000'),
|
|
570
|
+
prop('width', 'number (or a CSS length)'),
|
|
571
|
+
],
|
|
572
|
+
},
|
|
573
|
+
);
|
|
574
|
+
|
|
575
|
+
const report = await compare([icon]);
|
|
576
|
+
|
|
577
|
+
expect(report.findings).toEqual([]);
|
|
578
|
+
expect(report.typesCompared).toBe(7);
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
it('sets aside null and undefined when the project has no strict null checks', async () => {
|
|
582
|
+
withTypeScript();
|
|
583
|
+
fs.writeFileSync(
|
|
584
|
+
path.join(pkg, 'tsconfig.json'),
|
|
585
|
+
JSON.stringify({compilerOptions: {strict: false, jsx: 'react-jsx'}}),
|
|
586
|
+
);
|
|
587
|
+
const loose = component(
|
|
588
|
+
'Loose',
|
|
589
|
+
[
|
|
590
|
+
'type MaybeNum = number | null;',
|
|
591
|
+
'export function Loose(props: {count?: MaybeNum; counts?: MaybeNum[]}) { return null; }',
|
|
592
|
+
].join('\n'),
|
|
593
|
+
{
|
|
594
|
+
props: [
|
|
595
|
+
prop('count', 'number | null'),
|
|
596
|
+
prop('counts', '(number | null)[]'),
|
|
597
|
+
],
|
|
598
|
+
},
|
|
599
|
+
);
|
|
600
|
+
|
|
601
|
+
const report = await compare([loose]);
|
|
602
|
+
|
|
603
|
+
expect(report.findings).toEqual([]);
|
|
604
|
+
expect(report.typesCompared).toBe(2);
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
it('respects exactOptionalPropertyTypes: an optional prop takes undefined only when its type says so', async () => {
|
|
608
|
+
withTypeScript();
|
|
609
|
+
const tsconfig = path.join(pkg, 'tsconfig.json');
|
|
610
|
+
const config = JSON.parse(fs.readFileSync(tsconfig, 'utf8'));
|
|
611
|
+
config.compilerOptions.exactOptionalPropertyTypes = true;
|
|
612
|
+
fs.writeFileSync(tsconfig, JSON.stringify(config));
|
|
613
|
+
const exact = component(
|
|
614
|
+
'Exact',
|
|
615
|
+
"type MaybeTone = 'a' | 'b' | 'c' | undefined;\nexport function Exact(props: {size?: 'sm' | 'md'; tone?: 'a' | undefined; mode?: 'x'; status?: {tone?: 'a' | 'b' | undefined; mode?: 'x'}; labels?: Record<string, string | undefined>; hue?: 'a' | 'b' | undefined; t6?: MaybeTone; [key: `x${string}`]: string | undefined}) { return null; }\n",
|
|
616
|
+
{
|
|
617
|
+
props: [
|
|
618
|
+
prop('size', "'sm' | 'md' | undefined"),
|
|
619
|
+
prop('tone', "'a' | undefined"),
|
|
620
|
+
prop('mode', "'x'"),
|
|
621
|
+
prop('status.tone', "'a' | 'b'"),
|
|
622
|
+
prop('status.mode', "'x'"),
|
|
623
|
+
prop('labels.close', 'string'),
|
|
624
|
+
prop('xTone', 'string'),
|
|
625
|
+
prop('hue', "'a' | 'b'"),
|
|
626
|
+
prop('t6', "'a' | 'b' | 'c'"),
|
|
627
|
+
],
|
|
628
|
+
},
|
|
629
|
+
);
|
|
630
|
+
const variant = component(
|
|
631
|
+
'Variant',
|
|
632
|
+
"export function Variant(props: {kind: 'a'; size?: 'sm' | undefined} | {kind: 'b'; size?: 'md' | undefined}) { return null; }\n",
|
|
633
|
+
{
|
|
634
|
+
props: [
|
|
635
|
+
prop('kind', "'a' | 'b'", {required: true}),
|
|
636
|
+
prop('size', "'sm' | 'md'"),
|
|
637
|
+
],
|
|
638
|
+
},
|
|
639
|
+
);
|
|
640
|
+
|
|
641
|
+
const report = await compare([exact, variant]);
|
|
642
|
+
|
|
643
|
+
// TypeScript rejects `size={undefined}` here, and takes the others; a
|
|
644
|
+
// field that declares `undefined` takes it too, unlike its doc. A name an
|
|
645
|
+
// index signature takes is held to the index's type the same way as a
|
|
646
|
+
// field or as a prop.
|
|
647
|
+
expect(kinds(report)).toEqual([
|
|
648
|
+
'Exact.hue type_mismatch',
|
|
649
|
+
'Exact.size type_mismatch',
|
|
650
|
+
'Exact.status.tone type_mismatch',
|
|
651
|
+
'Exact.t6 type_mismatch',
|
|
652
|
+
'Variant.size type_mismatch',
|
|
653
|
+
]);
|
|
654
|
+
expect(report.typesCompared).toBe(11);
|
|
655
|
+
// The message shows the `undefined` the code declares, written once or by
|
|
656
|
+
// each member of a union, in the type and in its expansion.
|
|
657
|
+
for (const finding of report.findings.filter(
|
|
658
|
+
f => f.component === 'Variant' || f.prop === 'hue',
|
|
659
|
+
)) {
|
|
660
|
+
expect(finding.codeType).toMatch(/undefined/);
|
|
661
|
+
const declared = finding.message.split('but the code declares ')[1];
|
|
662
|
+
expect(declared).toMatch(/undefined/);
|
|
663
|
+
expect(declared.match(/\((.*)\)\.$/)?.[1] ?? 'undefined').toMatch(
|
|
664
|
+
/undefined/,
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
// Through an alias, the expansion shows it.
|
|
668
|
+
expect(report.findings.find(f => f.prop === 't6')?.message).toMatch(
|
|
669
|
+
/declares MaybeTone \(.*undefined.*\)\.$/,
|
|
670
|
+
);
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
it('holds a name an index signature takes, a field a Record takes, and a readonly array to the type TypeScript checks, and lists a field only an index takes and an `a / b` entry whose one type does not split', async () => {
|
|
674
|
+
withTypeScript();
|
|
675
|
+
const held = component(
|
|
676
|
+
'Held',
|
|
677
|
+
[
|
|
678
|
+
'export function Held(props: {',
|
|
679
|
+
' labels?: Record<string, string>;',
|
|
680
|
+
' tags?: string[];',
|
|
681
|
+
' ids?: ReadonlyArray<string>;',
|
|
682
|
+
' keys?: readonly string[];',
|
|
683
|
+
' grid?: readonly string[][];',
|
|
684
|
+
' rows?: (readonly string[])[];',
|
|
685
|
+
' onPick?: (values: readonly string[]) => void;',
|
|
686
|
+
' onDrop?: (values: readonly string[]) => void;',
|
|
687
|
+
' pinned?: {readonly ids: readonly string[]};',
|
|
688
|
+
' item?: {tags: readonly string[]};',
|
|
689
|
+
' mix?: string[] | readonly number[];',
|
|
690
|
+
' min?: number; max?: number; status?: {a: number};',
|
|
691
|
+
' [key: `x${string}`]: string; [key: `y${string}`]: {color: string};',
|
|
692
|
+
'}) { return null; }',
|
|
693
|
+
].join('\n'),
|
|
694
|
+
{
|
|
695
|
+
props: [
|
|
696
|
+
prop('labels.close', 'string | undefined'),
|
|
697
|
+
prop('xTone', 'string | undefined'),
|
|
698
|
+
prop('tags', 'readonly string[]'),
|
|
699
|
+
prop('ids', 'readonly string[]'),
|
|
700
|
+
prop('keys', 'string[]'),
|
|
701
|
+
prop('grid', '(readonly string[])[]'),
|
|
702
|
+
prop('rows', 'readonly string[][]'),
|
|
703
|
+
prop('onPick', '(values: string[]) => void'),
|
|
704
|
+
prop('onDrop', '(values: readonly string[])=>void'),
|
|
705
|
+
prop('pinned', '{readonly ids: readonly string[]}'),
|
|
706
|
+
prop('item', '{tags: string[]}'),
|
|
707
|
+
prop('mix', 'readonly string[] | number[]'),
|
|
708
|
+
prop('min / max', 'number'),
|
|
709
|
+
prop('status.a / status.b', 'number'),
|
|
710
|
+
prop('yTone.color', 'string'),
|
|
711
|
+
],
|
|
712
|
+
},
|
|
713
|
+
);
|
|
714
|
+
|
|
715
|
+
const report = await compare([held]);
|
|
716
|
+
|
|
717
|
+
// TypeScript passes no `undefined` through an index that does not take
|
|
718
|
+
// it, and no readonly array where a mutable one is taken; a mutable
|
|
719
|
+
// array passes where a readonly one is.
|
|
720
|
+
expect(kinds(report)).toEqual([
|
|
721
|
+
'Held.labels.close type_mismatch',
|
|
722
|
+
'Held.status.b not_in_code',
|
|
723
|
+
'Held.tags type_mismatch',
|
|
724
|
+
'Held.xTone type_mismatch',
|
|
725
|
+
]);
|
|
726
|
+
expect(report.typesCompared).toBe(7);
|
|
727
|
+
// `readonly` written as the code writes it matches; written somewhere
|
|
728
|
+
// else, at another depth, in a callback's parameter, in an object's
|
|
729
|
+
// field, or on another union member, it is not placed: listed, never a
|
|
730
|
+
// match, as are a field only an index takes and `a / b` with one type.
|
|
731
|
+
expect(
|
|
732
|
+
report.typesNotCompared.map(e => `${e.reason}: ${e.props.join(' ')}`),
|
|
733
|
+
).toEqual([
|
|
734
|
+
'not a form the check judges exactly: grid item max min mix onPick rows status.a yTone.color',
|
|
735
|
+
]);
|
|
736
|
+
});
|
|
737
|
+
|
|
738
|
+
it('reads strict null checks as the TypeScript version defaults them', async () => {
|
|
739
|
+
withTypeScript();
|
|
740
|
+
fs.writeFileSync(
|
|
741
|
+
path.join(pkg, 'tsconfig.json'),
|
|
742
|
+
JSON.stringify({compilerOptions: {jsx: 'react-jsx'}}),
|
|
743
|
+
);
|
|
744
|
+
// The TypeScript the test links in: 6 checks strict null by default.
|
|
745
|
+
const ts = require(TYPESCRIPT_DIR);
|
|
746
|
+
const strictByDefault = ts.getStrictOptionValue({}, 'strictNullChecks');
|
|
747
|
+
const maybe = component(
|
|
748
|
+
'Maybe',
|
|
749
|
+
'export function Maybe(props: {count?: number | null}) { return null; }\n',
|
|
750
|
+
{props: [prop('count', 'number')]},
|
|
751
|
+
);
|
|
752
|
+
|
|
753
|
+
const report = await compare([maybe]);
|
|
754
|
+
|
|
755
|
+
expect(kinds(report)).toEqual(
|
|
756
|
+
strictByDefault ? ['Maybe.count type_mismatch'] : [],
|
|
757
|
+
);
|
|
758
|
+
});
|
|
759
|
+
|
|
760
|
+
it("lists a name two index signatures take and a field documented with one variant's type, and reads a default for one union member", async () => {
|
|
761
|
+
withTypeScript();
|
|
762
|
+
const pick = component(
|
|
763
|
+
'Pick',
|
|
764
|
+
[
|
|
765
|
+
"export enum SizeE { Sm = 'sm', Md = 'md' }",
|
|
766
|
+
'type Sel = {kind: "single"; value: string} | {kind: "multi"; value: string[]};',
|
|
767
|
+
'export type PickProps = {',
|
|
768
|
+
' sizeE?: SizeE;',
|
|
769
|
+
' sel?: Sel;',
|
|
770
|
+
' tri?: {v: string} | {v: number} | {v: boolean};',
|
|
771
|
+
' sev?: {v: 1} | {v: 2} | {v: 3} | {v: 4} | {v: 5} | {v: 6} | {v: 7};',
|
|
772
|
+
' labels?: {close: string} | Record<string, number>;',
|
|
773
|
+
' [key: `data-${string}`]: string | number | undefined;',
|
|
774
|
+
' [key: `data-track${string}`]: string | undefined;',
|
|
775
|
+
'} & ({mode: "str"; [key: `x${string}`]: string} | {mode: "num"; [key: `x${string}`]: number});',
|
|
776
|
+
'export function Pick(props: PickProps) { return null; }',
|
|
777
|
+
].join('\n'),
|
|
778
|
+
{
|
|
779
|
+
props: [
|
|
780
|
+
prop('sizeE', 'SizeE = SizeE.Md'),
|
|
781
|
+
prop('sel.value', 'string'),
|
|
782
|
+
prop('tri.v', 'string | number'),
|
|
783
|
+
prop('sev.v', '1 | 2'),
|
|
784
|
+
prop('labels.close', 'string | number'),
|
|
785
|
+
prop('data-tracker', 'string'),
|
|
786
|
+
prop('mode', "'str' | 'num'", {required: true}),
|
|
787
|
+
prop('xValue', 'number'),
|
|
788
|
+
],
|
|
789
|
+
},
|
|
790
|
+
);
|
|
791
|
+
|
|
792
|
+
const report = await compare([pick]);
|
|
793
|
+
|
|
794
|
+
// TypeScript holds `data-tracker` to both signatures that take it, and
|
|
795
|
+
// `xValue` to the one of the member it is passed with; `sel.value`,
|
|
796
|
+
// `tri.v` and `sev.v` (past six variants, never reported) take every
|
|
797
|
+
// variant's type, and `labels.close` is documented with both.
|
|
798
|
+
expect(report.findings).toEqual([]);
|
|
799
|
+
expect(report.typesNotCompared).toEqual([
|
|
800
|
+
expect.objectContaining({
|
|
801
|
+
props: ['data-tracker', 'xValue'],
|
|
802
|
+
reason: 'not a form the check judges exactly',
|
|
803
|
+
}),
|
|
804
|
+
expect.objectContaining({
|
|
805
|
+
props: ['sel.value', 'sev.v', 'tri.v'],
|
|
806
|
+
reason: "the prop's variants give this field different types",
|
|
807
|
+
}),
|
|
808
|
+
]);
|
|
809
|
+
expect(report.typesCompared).toBe(3);
|
|
810
|
+
});
|
|
811
|
+
|
|
812
|
+
it('reads fields in arrays of unions, a name one member indexes, and names only where the code can mean them', async () => {
|
|
813
|
+
withTypeScript();
|
|
814
|
+
const rows = component(
|
|
815
|
+
'Rows',
|
|
816
|
+
[
|
|
817
|
+
'type Items = number[];',
|
|
818
|
+
'export type RowsProps = {',
|
|
819
|
+
" items?: Array<{kind: 'a'; value: string} | {kind: 'b'; value: number}>;",
|
|
820
|
+
' list?: Items;',
|
|
821
|
+
" behavior?: 'auto' | 'smooth';",
|
|
822
|
+
' [key: `data-${string}`]: string | undefined;',
|
|
823
|
+
"} & ({mode: 'fixed'; xSize: number} | ({mode: 'free'} & {[k: `x${string}`]: string}));",
|
|
824
|
+
'export function Rows(props: RowsProps) { return null; }',
|
|
825
|
+
].join('\n'),
|
|
826
|
+
{
|
|
827
|
+
props: [
|
|
828
|
+
prop('items[].value', 'string | number'),
|
|
829
|
+
prop('list', 'Items = string[]'),
|
|
830
|
+
prop('behavior', 'ScrollBehavior'),
|
|
831
|
+
prop('data-section', 'string'),
|
|
832
|
+
prop('mode', "'fixed' | 'free'", {required: true}),
|
|
833
|
+
prop('xSize', 'number | string'),
|
|
834
|
+
],
|
|
835
|
+
},
|
|
836
|
+
);
|
|
837
|
+
|
|
838
|
+
const report = await compare([rows]);
|
|
839
|
+
|
|
840
|
+
// `ScrollBehavior` is the DOM's, which the code never names.
|
|
841
|
+
expect(kinds(report)).toEqual(['Rows.list type_mismatch']);
|
|
842
|
+
expect(report.typesNotCompared).toEqual([
|
|
843
|
+
expect.objectContaining({props: ['behavior']}),
|
|
844
|
+
]);
|
|
845
|
+
});
|
|
846
|
+
|
|
847
|
+
it('judges a prop two intersected types declare by the type they narrow it to, and undefined on a required prop', async () => {
|
|
848
|
+
withTypeScript();
|
|
849
|
+
const narrow = component(
|
|
850
|
+
'Narrow',
|
|
851
|
+
[
|
|
852
|
+
"type A = {size?: 'sm'; tone?: 'a' | 'b'};",
|
|
853
|
+
"type B = {size?: 'sm' | 'md'; tone?: 'a' | 'b'};",
|
|
854
|
+
'export type NarrowProps = B & A & {isOpen: boolean; label: string | undefined};',
|
|
855
|
+
'export function Narrow(props: NarrowProps) { return null; }',
|
|
856
|
+
].join('\n'),
|
|
857
|
+
{
|
|
858
|
+
props: [
|
|
859
|
+
prop('size', "'sm' | 'md'"),
|
|
860
|
+
prop('tone', "'b' | 'a'"),
|
|
861
|
+
prop('isOpen', 'boolean | undefined', {required: true}),
|
|
862
|
+
prop('label', 'string', {required: true}),
|
|
863
|
+
],
|
|
864
|
+
},
|
|
865
|
+
);
|
|
866
|
+
|
|
867
|
+
const report = await compare([narrow]);
|
|
868
|
+
|
|
869
|
+
// TypeScript takes only 'sm' for `size`, rejects `isOpen={undefined}`,
|
|
870
|
+
// and accepts `label={undefined}`.
|
|
871
|
+
expect(kinds(report)).toEqual([
|
|
872
|
+
'Narrow.isOpen type_mismatch',
|
|
873
|
+
'Narrow.label type_mismatch',
|
|
874
|
+
'Narrow.size type_mismatch',
|
|
875
|
+
]);
|
|
876
|
+
expect(report.findings.find(f => f.prop === 'size')?.codeType).toBe("'sm'");
|
|
877
|
+
});
|
|
878
|
+
|
|
879
|
+
it('lists every other form of type as not compared, never as a mismatch', async () => {
|
|
880
|
+
withTypeScript();
|
|
881
|
+
const thread = component(
|
|
882
|
+
'Thread',
|
|
883
|
+
[
|
|
884
|
+
'interface IconShape { path: string }',
|
|
885
|
+
'type MaybePromise<T> = T | Promise<T>;',
|
|
886
|
+
'type Parts = {a?: string; b?: number};',
|
|
887
|
+
"type A = {a: string}; type B = {b: number}; enum Color { Red = 'red' } namespace Theme { export enum Color { Red = 'red' } }",
|
|
888
|
+
'export interface ThreadProps {',
|
|
889
|
+
' onSubmit?: (text: string, files?: File[]) => MaybePromise<void>;',
|
|
890
|
+
' onPick?: (value: string, index: number) => void;',
|
|
891
|
+
' item?: A & B;',
|
|
892
|
+
' parts?: Partial<Parts>;',
|
|
893
|
+
' key2?: keyof (A & B);',
|
|
894
|
+
' key3?: keyof (A & B);',
|
|
895
|
+
" target?: '_self' | '_blank' | (string & {});",
|
|
896
|
+
' range?: [number, number?];',
|
|
897
|
+
' labels?: Record<string, string>;',
|
|
898
|
+
' icon?: IconShape;',
|
|
899
|
+
' data?: any;',
|
|
900
|
+
' extra?: unknown; tint?: Theme.Color;',
|
|
901
|
+
'}',
|
|
902
|
+
'export function Thread(props: ThreadProps) { return null; }',
|
|
903
|
+
].join('\n'),
|
|
904
|
+
{
|
|
905
|
+
props: [
|
|
906
|
+
prop('onSubmit', '(text: string) => Promise<number>'),
|
|
907
|
+
prop('onPick', '(value: string, index: number) => void'),
|
|
908
|
+
prop('item', '{a: string; b: number}'),
|
|
909
|
+
prop('parts', 'Parts'),
|
|
910
|
+
prop('key2', 'keyof (A & B)'),
|
|
911
|
+
prop('key3', "'a' | 'b'"),
|
|
912
|
+
prop('target', "'_self' | '_blank'"),
|
|
913
|
+
prop('range', '[number]'),
|
|
914
|
+
prop('labels', '{[key: string]: number}'),
|
|
915
|
+
prop('icon', 'IconName'),
|
|
916
|
+
prop('data', 'string'),
|
|
917
|
+
prop('data.id', 'string'),
|
|
918
|
+
prop('extra.kind', 'string'),
|
|
919
|
+
prop('tint', 'Color'),
|
|
920
|
+
],
|
|
921
|
+
},
|
|
922
|
+
);
|
|
923
|
+
|
|
924
|
+
const report = await compare([thread]);
|
|
925
|
+
|
|
926
|
+
expect(report.findings).toEqual([]);
|
|
927
|
+
// Written exactly as the source writes them, or a literal union the
|
|
928
|
+
// code's type resolves to (`keyof (A & B)` is `'a' | 'b'`).
|
|
929
|
+
expect(report.typesCompared).toBe(3);
|
|
930
|
+
expect(
|
|
931
|
+
report.typesNotCompared.map(e => `${e.reason}: ${e.props.join(' ')}`),
|
|
932
|
+
).toEqual([
|
|
933
|
+
'not a form the check judges exactly: data data.id extra.kind icon item labels onSubmit parts range target tint',
|
|
934
|
+
]);
|
|
935
|
+
});
|
|
936
|
+
|
|
937
|
+
it("compares a documented field's type, and a name an index signature takes, with the type the code gives it", async () => {
|
|
938
|
+
withTypeScript();
|
|
939
|
+
const note = component(
|
|
940
|
+
'Note',
|
|
941
|
+
[
|
|
942
|
+
'export interface NoteProps {',
|
|
943
|
+
" status?: {type: 'error' | 'warning'; message?: string};",
|
|
944
|
+
' items?: Array<{id: string; label: string}>;',
|
|
945
|
+
' labels?: Record<string, string>;',
|
|
946
|
+
' [key: `data-${string}`]: string | undefined;',
|
|
947
|
+
' [key: `aria-${string}`]: string | number | undefined;',
|
|
948
|
+
'}',
|
|
949
|
+
'export function Note(props: NoteProps) { return null; }',
|
|
950
|
+
].join('\n'),
|
|
951
|
+
{
|
|
952
|
+
props: [
|
|
953
|
+
prop('status.type', "'error' | 'info'"),
|
|
954
|
+
prop('items[].label', 'number'),
|
|
955
|
+
prop('labels.open', 'string'),
|
|
956
|
+
prop('labels.close', 'number'),
|
|
957
|
+
prop('data-section', 'string'),
|
|
958
|
+
prop('data-count', 'number'),
|
|
959
|
+
prop('aria-busy', 'boolean'),
|
|
960
|
+
],
|
|
961
|
+
},
|
|
962
|
+
);
|
|
963
|
+
|
|
964
|
+
const report = await compare([note]);
|
|
965
|
+
|
|
966
|
+
expect(kinds(report)).toEqual([
|
|
967
|
+
'Note.aria-busy type_mismatch',
|
|
968
|
+
'Note.data-count type_mismatch',
|
|
969
|
+
'Note.items[].label type_mismatch',
|
|
970
|
+
'Note.labels.close type_mismatch',
|
|
971
|
+
'Note.status.type type_mismatch',
|
|
972
|
+
]);
|
|
973
|
+
expect(report.typesCompared).toBe(7);
|
|
974
|
+
});
|
|
975
|
+
|
|
976
|
+
it('accepts an alias spelled out beside its name, and reports one whose spelled-out values are stale', async () => {
|
|
977
|
+
withTypeScript();
|
|
978
|
+
const links = ["'link' | 'token'", "'link' | 'token' | 'button'"].map(
|
|
979
|
+
(values, i) =>
|
|
980
|
+
component(
|
|
981
|
+
`Link${i}`,
|
|
982
|
+
`export type LinkVariant = ${values};\nexport function Link${i}(props: {variant?: LinkVariant; tone?: LinkVariant}) { return null; }`,
|
|
983
|
+
{
|
|
984
|
+
props: [
|
|
985
|
+
prop('variant', "LinkVariant = 'link' | 'token'"),
|
|
986
|
+
prop('tone', "LinkVariant ('link' | 'token')"),
|
|
987
|
+
],
|
|
988
|
+
},
|
|
989
|
+
),
|
|
990
|
+
);
|
|
991
|
+
|
|
992
|
+
expect(kinds(await compare(links))).toEqual([
|
|
993
|
+
'Link1.tone type_mismatch',
|
|
994
|
+
'Link1.variant type_mismatch',
|
|
995
|
+
]);
|
|
996
|
+
});
|
|
997
|
+
|
|
998
|
+
it("reads an optional prop documented with `| undefined` as the code reads it, and names an alias's members in the source's order", async () => {
|
|
999
|
+
withTypeScript();
|
|
1000
|
+
const badge = component(
|
|
1001
|
+
'Badge',
|
|
1002
|
+
[
|
|
1003
|
+
"type Tone = 'neutral' | 'info';",
|
|
1004
|
+
'export function Badge(props: {tone?: Tone}) { return null; }',
|
|
1005
|
+
].join('\n'),
|
|
1006
|
+
{props: [prop('tone', "'neutral' | 'info' | undefined")]},
|
|
1007
|
+
);
|
|
1008
|
+
const chip = component(
|
|
1009
|
+
'Chip',
|
|
1010
|
+
"export function Chip(props: {tone?: 'a' | 'b' | undefined}) { return null; }\n",
|
|
1011
|
+
{props: [prop('tone', "'a' | 'c'")]},
|
|
1012
|
+
);
|
|
1013
|
+
|
|
1014
|
+
// The same literals, made first in another order, would set the
|
|
1015
|
+
// compiler's own order.
|
|
1016
|
+
const status = component(
|
|
1017
|
+
'Status',
|
|
1018
|
+
[
|
|
1019
|
+
"type Other = 'detached' | 'attached' | 'tooltip';",
|
|
1020
|
+
'export type Unrelated = Other;',
|
|
1021
|
+
"type StatusVariant = 'tooltip' | 'attached' | 'detached';",
|
|
1022
|
+
'export function Status(props: {variant?: StatusVariant}) { return null; }',
|
|
1023
|
+
].join('\n'),
|
|
1024
|
+
{props: [prop('variant', "'attached' | 'detached'")]},
|
|
1025
|
+
);
|
|
1026
|
+
|
|
1027
|
+
expect(kinds(await compare([badge]))).toEqual([]);
|
|
1028
|
+
// The optional prop's `undefined` the expansion sets aside is shown once.
|
|
1029
|
+
const [finding] = (await compare([chip])).findings;
|
|
1030
|
+
expect(finding.message).toMatch(/declares 'a' \| 'b' \| undefined\.$/);
|
|
1031
|
+
expect((await compare([status])).findings.map(f => f.message)).toEqual([
|
|
1032
|
+
`"variant" is documented as 'attached' | 'detached', but the code declares StatusVariant ('tooltip' | 'attached' | 'detached').`,
|
|
1033
|
+
]);
|
|
1034
|
+
});
|
|
1035
|
+
|
|
1036
|
+
it('judges a className the package declares, and lists standard props and props from other packages as not compared', async () => {
|
|
1037
|
+
withTypeScript();
|
|
1038
|
+
const base = path.join(pkg, 'node_modules', '@acme', 'base');
|
|
1039
|
+
fs.mkdirSync(base, {recursive: true});
|
|
1040
|
+
fs.writeFileSync(
|
|
1041
|
+
path.join(base, 'package.json'),
|
|
1042
|
+
JSON.stringify({name: '@acme/base', types: 'index.d.ts'}),
|
|
1043
|
+
);
|
|
1044
|
+
fs.writeFileSync(
|
|
1045
|
+
path.join(base, 'index.d.ts'),
|
|
1046
|
+
'export interface BaseProps { style?: object; id?: string; tone?: {level: number}; [key: `data-${string}`]: string | undefined }\n',
|
|
1047
|
+
);
|
|
1048
|
+
// A name only another package's index signature, or a mapped type's,
|
|
1049
|
+
// takes is not judged either.
|
|
1050
|
+
const card = component(
|
|
1051
|
+
'Card',
|
|
1052
|
+
"import type {BaseProps} from '@acme/base';\nexport function Card(props: BaseProps & {className?: string} & Record<`aria-${string}`, string>) { return null; }\n",
|
|
1053
|
+
{
|
|
1054
|
+
props: [
|
|
1055
|
+
prop('className', 'number'),
|
|
1056
|
+
prop('style', 'string'),
|
|
1057
|
+
prop('id', 'number'),
|
|
1058
|
+
prop('tone.level', 'string'),
|
|
1059
|
+
prop('tone.missing', 'string'),
|
|
1060
|
+
prop('data-variant', "'solid' | 'outline'"),
|
|
1061
|
+
prop('aria-note', 'number'),
|
|
1062
|
+
],
|
|
1063
|
+
},
|
|
1064
|
+
);
|
|
1065
|
+
|
|
1066
|
+
const report = await compare([card]);
|
|
1067
|
+
|
|
1068
|
+
expect(kinds(report)).toEqual([
|
|
1069
|
+
'Card.className type_mismatch',
|
|
1070
|
+
'Card.tone.missing not_in_code',
|
|
1071
|
+
]);
|
|
1072
|
+
expect(report.typesCompared).toBe(1);
|
|
1073
|
+
expect(
|
|
1074
|
+
report.typesNotCompared.map(e => `${e.reason}: ${e.props.join(' ')}`),
|
|
1075
|
+
).toEqual([
|
|
1076
|
+
'declared outside the package, not compared: data-variant id tone.level',
|
|
1077
|
+
'not a form the check judges exactly: aria-note',
|
|
1078
|
+
'standard prop, not compared: style',
|
|
1079
|
+
]);
|
|
1080
|
+
});
|
|
1081
|
+
|
|
1082
|
+
it('does not ask a doc for props from other packages, or for the props the authoring guidance skips', async () => {
|
|
1083
|
+
expect(PROPS_NOT_DOCUMENTED).toEqual([
|
|
1084
|
+
'className',
|
|
1085
|
+
'style',
|
|
1086
|
+
'xstyle',
|
|
1087
|
+
'data-testid',
|
|
1088
|
+
'ref',
|
|
1089
|
+
'key',
|
|
1090
|
+
]);
|
|
1091
|
+
withTypeScript();
|
|
1092
|
+
const base = path.join(pkg, 'node_modules', '@acme', 'base');
|
|
1093
|
+
fs.mkdirSync(base, {recursive: true});
|
|
1094
|
+
fs.writeFileSync(
|
|
1095
|
+
path.join(base, 'package.json'),
|
|
1096
|
+
JSON.stringify({name: '@acme/base', types: 'index.d.ts'}),
|
|
1097
|
+
);
|
|
1098
|
+
fs.writeFileSync(
|
|
1099
|
+
path.join(base, 'index.d.ts'),
|
|
1100
|
+
[
|
|
1101
|
+
'export interface BaseProps { id?: string; onBlur?: () => void }',
|
|
1102
|
+
"export type Sizes = Partial<Record<'sm' | 'md', string>>;",
|
|
1103
|
+
"export interface Spacing extends Partial<Record<'gap', number>> {}",
|
|
1104
|
+
"export type Tone = 'info' | 'danger';",
|
|
1105
|
+
'export type Flags<K extends string> = {[P in K]?: boolean};',
|
|
1106
|
+
'export type FC<P> = (props: P) => null;',
|
|
1107
|
+
"export type Responsive<T> = {[K in 'sm' | 'lg']?: T};",
|
|
1108
|
+
"export type Breakpoint = 'sm' | 'lg';",
|
|
1109
|
+
'export declare const BREAKPOINTS: readonly Breakpoint[];',
|
|
1110
|
+
'export declare function memo<T extends (p: any) => null>(c: T): {(p: Parameters<T>[0]): null; $$typeof: symbol; type: T};',
|
|
1111
|
+
'export declare function forwardRef<P>(render: (props: P, ref: unknown) => null): (props: P) => null;',
|
|
1112
|
+
].join('\n'),
|
|
1113
|
+
);
|
|
1114
|
+
const panel = component(
|
|
1115
|
+
'Panel',
|
|
1116
|
+
[
|
|
1117
|
+
"import type {BaseProps} from '@acme/base';",
|
|
1118
|
+
'export interface PanelProps extends BaseProps {',
|
|
1119
|
+
' title?: string;',
|
|
1120
|
+
' className?: string;',
|
|
1121
|
+
' style?: {color?: string};',
|
|
1122
|
+
' xstyle?: unknown;',
|
|
1123
|
+
" 'data-testid'?: string;",
|
|
1124
|
+
' ref?: unknown;',
|
|
1125
|
+
' key?: string;',
|
|
1126
|
+
' /** @deprecated Use `title`. */',
|
|
1127
|
+
' heading?: string;',
|
|
1128
|
+
' /** @internal */',
|
|
1129
|
+
' debugName?: string;',
|
|
1130
|
+
'}',
|
|
1131
|
+
'export function Panel(props: PanelProps) { return null; }',
|
|
1132
|
+
].join('\n'),
|
|
1133
|
+
{props: [prop('title', 'string'), prop('id', 'number')]},
|
|
1134
|
+
);
|
|
1135
|
+
|
|
1136
|
+
// A prop a mapped type builds is the package's own when its key traces
|
|
1137
|
+
// to the package's source: through aliases, `Exclude`, a generic's
|
|
1138
|
+
// arguments, and a component `memo` or `forwardRef` wraps. A key another
|
|
1139
|
+
// package defines, behind an alias or not, is that package's, and one
|
|
1140
|
+
// the check can't trace is listed, not asked for.
|
|
1141
|
+
const mapped = Object.entries({
|
|
1142
|
+
Styled:
|
|
1143
|
+
'(props: {[K in StyleKey]?: BaseProps[K]} & Partial<Record<StyleKey, number>>)',
|
|
1144
|
+
Inlined: '(props: {[K in keyof BaseProps]?: BaseProps[K]})',
|
|
1145
|
+
Spread: '(props: Responsive<number>)',
|
|
1146
|
+
Memoed: ' = memo(Inner)',
|
|
1147
|
+
Forwarded: ' = forwardRef(Impl)',
|
|
1148
|
+
Excluded: "(props: {[K in Exclude<'a' | 'b' | 'c', 'c'>]?: string})",
|
|
1149
|
+
Templated: "(props: {[K in `on${Capitalize<'open'>}`]?: () => void})",
|
|
1150
|
+
// Another package's keys through an own interface's property, an
|
|
1151
|
+
// `infer`, or a constant bound to its value stay that package's.
|
|
1152
|
+
Indexed: "(props: {[K in Opts['size']]?: number})",
|
|
1153
|
+
Inferred: '(props: {[K in KeysOf<Record<Breakpoint, 1>>]?: number})',
|
|
1154
|
+
Rebound: '(props: {[K in (typeof SIZES)[number]]?: number})',
|
|
1155
|
+
Inline: "(props: {[K in 'a' | 'b']?: string})",
|
|
1156
|
+
Recorded: "(props: Partial<Record<'a' | 'b', string>>)",
|
|
1157
|
+
Remapped: "(props: {[K in 'open' as `on${Capitalize<K>}`]?: () => void})",
|
|
1158
|
+
Toned:
|
|
1159
|
+
"(props: Partial<Record<Tone | 'xl', boolean>> & {[K in Tone]?: string} & Partial<Record<keyof BaseProps, string>> & {[K in keyof BaseProps as `x${Capitalize<K>}`]?: boolean})",
|
|
1160
|
+
Flagged: "(props: Flags<'bold'>)",
|
|
1161
|
+
Extended: '(props: ExtendedProps)',
|
|
1162
|
+
Sized: '(props: Spaced & Partial<Sizes>)',
|
|
1163
|
+
}).map(([name, props]) =>
|
|
1164
|
+
component(
|
|
1165
|
+
name,
|
|
1166
|
+
[
|
|
1167
|
+
"import {type BaseProps, type Sizes, type Spacing, type Tone, type Flags, type FC, type Responsive, type Breakpoint, BREAKPOINTS, memo, forwardRef} from '@acme/base';",
|
|
1168
|
+
"interface ExtendedProps extends Partial<Record<'a', string>> {}",
|
|
1169
|
+
'interface Spaced extends Spacing {}',
|
|
1170
|
+
'type StyleKey = keyof BaseProps;',
|
|
1171
|
+
'interface Opts { size: Breakpoint }',
|
|
1172
|
+
'type KeysOf<T> = T extends Record<infer K, unknown> ? K : never;',
|
|
1173
|
+
'const SIZES = BREAKPOINTS;',
|
|
1174
|
+
"function Inner(props: {[K in 'top' | 'bottom']?: number}) { return null; }",
|
|
1175
|
+
"function Impl(props: Partial<Record<'left', number>>, ref: unknown) { return null; }",
|
|
1176
|
+
props.startsWith(' =')
|
|
1177
|
+
? `export const ${name}${props};`
|
|
1178
|
+
: `export function ${name}${props} { return null; }`,
|
|
1179
|
+
`export const Typed: FC<Partial<Record<'a', string>>> = () => null;`,
|
|
1180
|
+
].join('\n'),
|
|
1181
|
+
{props: []},
|
|
1182
|
+
),
|
|
1183
|
+
);
|
|
1184
|
+
|
|
1185
|
+
// A deprecated prop is still a prop the component accepts.
|
|
1186
|
+
const typed = {...mapped[0], name: 'Typed'};
|
|
1187
|
+
const report = await compare([panel, ...mapped, typed]);
|
|
140
1188
|
expect(kinds(report)).toEqual([
|
|
141
|
-
'
|
|
142
|
-
'
|
|
143
|
-
'
|
|
144
|
-
'
|
|
1189
|
+
'Excluded.a not_in_doc',
|
|
1190
|
+
'Excluded.b not_in_doc',
|
|
1191
|
+
'Extended.a not_in_doc',
|
|
1192
|
+
'Flagged.bold not_in_doc',
|
|
1193
|
+
'Forwarded.left not_in_doc',
|
|
1194
|
+
'Inline.a not_in_doc',
|
|
1195
|
+
'Inline.b not_in_doc',
|
|
1196
|
+
'Memoed.bottom not_in_doc',
|
|
1197
|
+
'Memoed.top not_in_doc',
|
|
1198
|
+
'Panel.heading not_in_doc',
|
|
1199
|
+
'Recorded.a not_in_doc',
|
|
1200
|
+
'Recorded.b not_in_doc',
|
|
1201
|
+
'Remapped.onOpen not_in_doc',
|
|
1202
|
+
'Toned.xl not_in_doc',
|
|
1203
|
+
'Typed.a not_in_doc',
|
|
145
1204
|
]);
|
|
146
|
-
expect(report.
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
)
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
1205
|
+
expect(report.propsNotChecked).toEqual([
|
|
1206
|
+
expect.objectContaining({component: 'Templated', props: ['onOpen']}),
|
|
1207
|
+
expect.objectContaining({component: 'Inferred', props: ['lg', 'sm']}),
|
|
1208
|
+
]);
|
|
1209
|
+
// The alias and the inline form give the same answer: nothing asked.
|
|
1210
|
+
for (const name of ['Styled', 'Inlined']) {
|
|
1211
|
+
expect(kinds(report).filter(kind => kind.startsWith(name))).toEqual([]);
|
|
1212
|
+
}
|
|
153
1213
|
});
|
|
154
1214
|
|
|
155
|
-
it('reads every overload, one without props included,
|
|
1215
|
+
it('reads every overload, one without props included, matches a doc that describes one member of a discriminated union, reads a prop only some members require as optional, and holds every prop one entry names to its `required`', async () => {
|
|
156
1216
|
withTypeScript();
|
|
157
1217
|
const picker = component(
|
|
158
1218
|
'Picker',
|
|
@@ -257,6 +1317,125 @@ export function Select(props: Single | Multi) { return null; }`,
|
|
|
257
1317
|
);
|
|
258
1318
|
});
|
|
259
1319
|
|
|
1320
|
+
it('shows a method prop the doc leaves out with its function type, optional or not', async () => {
|
|
1321
|
+
withTypeScript();
|
|
1322
|
+
const picker = component(
|
|
1323
|
+
'Picker',
|
|
1324
|
+
`export interface PickerProps {
|
|
1325
|
+
label?: string;
|
|
1326
|
+
onPick(value: string, index: number): void;
|
|
1327
|
+
onClear?(reason: string): void;
|
|
1328
|
+
get size(): number;
|
|
1329
|
+
}
|
|
1330
|
+
export function Picker(props: PickerProps) { return null; }`,
|
|
1331
|
+
{props: [prop('label', 'string')]},
|
|
1332
|
+
);
|
|
1333
|
+
|
|
1334
|
+
const report = await compare([picker]);
|
|
1335
|
+
|
|
1336
|
+
expect(
|
|
1337
|
+
report.findings.map(f => `${f.prop} ${f.kind}: ${f.codeType}`),
|
|
1338
|
+
).toEqual([
|
|
1339
|
+
'onClear not_in_doc: (reason: string) => void',
|
|
1340
|
+
'onPick not_in_doc: (value: string, index: number) => void',
|
|
1341
|
+
'size not_in_doc: number',
|
|
1342
|
+
]);
|
|
1343
|
+
});
|
|
1344
|
+
|
|
1345
|
+
it("prints a union's members in the order the source declares them", async () => {
|
|
1346
|
+
withTypeScript();
|
|
1347
|
+
// The same literals, made first in another order, would set the
|
|
1348
|
+
// compiler's own order.
|
|
1349
|
+
const ordered = component(
|
|
1350
|
+
'Ordered',
|
|
1351
|
+
[
|
|
1352
|
+
"type Other = 'md' | 'sm' | 'lg';",
|
|
1353
|
+
'export type Unrelated = Other;',
|
|
1354
|
+
"type Small = 'lg' | 'sm';",
|
|
1355
|
+
"export function Ordered(props: {size?: Small} | {size?: 'md'; label?: string}) { return null; }",
|
|
1356
|
+
].join('\n'),
|
|
1357
|
+
{props: [prop('label', 'string')]},
|
|
1358
|
+
);
|
|
1359
|
+
|
|
1360
|
+
const report = await compare([ordered]);
|
|
1361
|
+
|
|
1362
|
+
expect(
|
|
1363
|
+
report.findings.find(finding => finding.prop === 'size')?.codeType,
|
|
1364
|
+
).toBe("'lg' | 'sm' | 'md'");
|
|
1365
|
+
});
|
|
1366
|
+
|
|
1367
|
+
it("reads a prop that the package's defaultProps fills as optional", async () => {
|
|
1368
|
+
withTypeScript();
|
|
1369
|
+
// A library type that declares an optional `defaultProps`, as React's
|
|
1370
|
+
// component types do, fills nothing; without strict null checks it reads
|
|
1371
|
+
// as filling every prop.
|
|
1372
|
+
const lib = path.join(pkg, 'node_modules', '@acme', 'lib');
|
|
1373
|
+
fs.mkdirSync(lib, {recursive: true});
|
|
1374
|
+
fs.writeFileSync(
|
|
1375
|
+
path.join(lib, 'package.json'),
|
|
1376
|
+
JSON.stringify({name: '@acme/lib', types: 'index.d.ts'}),
|
|
1377
|
+
);
|
|
1378
|
+
fs.writeFileSync(
|
|
1379
|
+
path.join(lib, 'index.d.ts'),
|
|
1380
|
+
`export interface Kind<P> { new (props: P): {}; defaultProps?: Partial<P> }
|
|
1381
|
+
export declare function memo<T extends (p: any) => any>(c: T): {(p: Parameters<T>[0]): null; $$typeof: symbol; type: T};
|
|
1382
|
+
export declare function lazy<T extends (p: any) => any>(f: () => Promise<{default: T}>): {(p: Parameters<T>[0]): null; $$typeof: symbol; _result: T};\n`,
|
|
1383
|
+
);
|
|
1384
|
+
fs.writeFileSync(
|
|
1385
|
+
path.join(pkg, 'tsconfig.json'),
|
|
1386
|
+
JSON.stringify({compilerOptions: {strict: false, jsx: 'react-jsx'}}),
|
|
1387
|
+
);
|
|
1388
|
+
const source = `import {type Kind, lazy, memo} from '@acme/lib';
|
|
1389
|
+
declare class Component<P> { constructor(props: P); props: P }
|
|
1390
|
+
export class Klass extends Component<{label: string; size: number}> {
|
|
1391
|
+
static defaultProps = {size: 1};
|
|
1392
|
+
}
|
|
1393
|
+
export function Fn(props: {label: string; size: number}) { return null; }
|
|
1394
|
+
Fn.defaultProps = {size: 1};
|
|
1395
|
+
export function Gn(props: {label: string; size: number}) { return null; }
|
|
1396
|
+
Gn.defaultProps = {size: 1};
|
|
1397
|
+
export declare const Typed: Kind<{label: string; size: number}>;
|
|
1398
|
+
export const Memoed = memo(Fn);
|
|
1399
|
+
export const Lazied = lazy(() => Promise.resolve({default: Fn}));
|
|
1400
|
+
export const Thrice = memo(memo(memo(Fn)));
|
|
1401
|
+
export class Loose extends Component<{label: string; size: number}> { static defaultProps: any = {}; }`;
|
|
1402
|
+
const props = [
|
|
1403
|
+
prop('label', 'string'),
|
|
1404
|
+
prop('size', 'number', {required: true}),
|
|
1405
|
+
];
|
|
1406
|
+
|
|
1407
|
+
const report = await compare([
|
|
1408
|
+
...'Klass Fn Typed Memoed Lazied Thrice Loose'
|
|
1409
|
+
.split(' ')
|
|
1410
|
+
.map(name => component(name, source, {props})),
|
|
1411
|
+
component('Gn', source, {
|
|
1412
|
+
props: [prop('label', 'string'), prop('size', 'number')],
|
|
1413
|
+
}),
|
|
1414
|
+
]);
|
|
1415
|
+
|
|
1416
|
+
// JSX callers may leave out a prop defaultProps fills (for `memo` and
|
|
1417
|
+
// `lazy`, the wrapped component's, two layers deep as React's types read
|
|
1418
|
+
// it; `any` fills every prop), so a doc that marks it required disagrees
|
|
1419
|
+
// with the code, and one that doesn't agrees.
|
|
1420
|
+
expect(kinds(report)).toEqual([
|
|
1421
|
+
'Fn.label required_mismatch',
|
|
1422
|
+
'Fn.size required_mismatch',
|
|
1423
|
+
'Gn.label required_mismatch',
|
|
1424
|
+
'Klass.label required_mismatch',
|
|
1425
|
+
'Klass.size required_mismatch',
|
|
1426
|
+
'Lazied.label required_mismatch',
|
|
1427
|
+
'Lazied.size required_mismatch',
|
|
1428
|
+
'Loose.size required_mismatch',
|
|
1429
|
+
'Memoed.label required_mismatch',
|
|
1430
|
+
'Memoed.size required_mismatch',
|
|
1431
|
+
'Thrice.label required_mismatch',
|
|
1432
|
+
'Typed.label required_mismatch',
|
|
1433
|
+
]);
|
|
1434
|
+
expect(
|
|
1435
|
+
report.findings.find(f => f.component === 'Fn' && f.prop === 'size'),
|
|
1436
|
+
).toMatchObject({docRequired: true, codeRequired: false});
|
|
1437
|
+
});
|
|
1438
|
+
|
|
260
1439
|
it('lists each component whose props it cannot read as not compared', async () => {
|
|
261
1440
|
withTypeScript();
|
|
262
1441
|
const tsconfig = path.join(pkg, 'tsconfig.json');
|
|
@@ -552,3 +1731,542 @@ export const Wrapped = forwardRef<HTMLButtonElement, {label?: string}>((props, r
|
|
|
552
1731
|
}
|
|
553
1732
|
});
|
|
554
1733
|
});
|
|
1734
|
+
|
|
1735
|
+
/**
|
|
1736
|
+
* A multi-component doc's entry.
|
|
1737
|
+
* @param {string} name
|
|
1738
|
+
* @param {any[]} props
|
|
1739
|
+
*/
|
|
1740
|
+
const entry = (name, props) => ({
|
|
1741
|
+
name,
|
|
1742
|
+
displayName: name,
|
|
1743
|
+
description: 'Fixture.',
|
|
1744
|
+
props,
|
|
1745
|
+
});
|
|
1746
|
+
|
|
1747
|
+
/** @param {import('./prop-drift.mjs').PropDriftComponent[]} components */
|
|
1748
|
+
function compareDocs(components) {
|
|
1749
|
+
return comparePropDocs(components, {packageDir: pkg, resolveFrom: [pkg]});
|
|
1750
|
+
}
|
|
1751
|
+
|
|
1752
|
+
describe('comparePropDocs', () => {
|
|
1753
|
+
it('compares each entry of a multi-component doc against its own export, and skips hooks', async () => {
|
|
1754
|
+
withTypeScript();
|
|
1755
|
+
const tabs = component(
|
|
1756
|
+
'Tabs',
|
|
1757
|
+
`export function Tabs(props: {value: string}) { return null; }
|
|
1758
|
+
export function Tab(props: {value: string; isDisabled?: boolean}) { return null; }
|
|
1759
|
+
export function useTabs(options: {value: string}) { return options; }`,
|
|
1760
|
+
{
|
|
1761
|
+
components: [
|
|
1762
|
+
entry('Tabs', [prop('value', 'string', {required: true})]),
|
|
1763
|
+
entry('Tab', [
|
|
1764
|
+
prop('value', 'string', {required: true}),
|
|
1765
|
+
prop('isSelected', 'boolean'),
|
|
1766
|
+
]),
|
|
1767
|
+
{
|
|
1768
|
+
name: 'useTabs',
|
|
1769
|
+
displayName: 'useTabs',
|
|
1770
|
+
description: 'Fixture.',
|
|
1771
|
+
params: [
|
|
1772
|
+
{
|
|
1773
|
+
name: 'options',
|
|
1774
|
+
type: '{value: string}',
|
|
1775
|
+
description: 'Fixture.',
|
|
1776
|
+
},
|
|
1777
|
+
],
|
|
1778
|
+
},
|
|
1779
|
+
],
|
|
1780
|
+
},
|
|
1781
|
+
);
|
|
1782
|
+
|
|
1783
|
+
const report = await compareDocs([tabs]);
|
|
1784
|
+
|
|
1785
|
+
expect(report.components).toBe(2);
|
|
1786
|
+
expect(kinds(report)).toEqual([
|
|
1787
|
+
'Tab.isDisabled not_in_doc',
|
|
1788
|
+
'Tab.isSelected not_in_code',
|
|
1789
|
+
]);
|
|
1790
|
+
});
|
|
1791
|
+
|
|
1792
|
+
it('compares a member record only against its own source, and the family record leaves that member alone', async () => {
|
|
1793
|
+
withTypeScript();
|
|
1794
|
+
const status = component(
|
|
1795
|
+
'Status',
|
|
1796
|
+
'export function Status(props: {value: string}) { return null; }\n',
|
|
1797
|
+
{
|
|
1798
|
+
components: [
|
|
1799
|
+
entry('Status', [prop('value', 'string', {required: true})]),
|
|
1800
|
+
entry('StatusGlyph', [
|
|
1801
|
+
prop('size', 'number'),
|
|
1802
|
+
prop('tone', 'string'),
|
|
1803
|
+
]),
|
|
1804
|
+
],
|
|
1805
|
+
},
|
|
1806
|
+
);
|
|
1807
|
+
const glyphSource = path.join(pkg, 'components', 'StatusView.tsx');
|
|
1808
|
+
fs.writeFileSync(
|
|
1809
|
+
glyphSource,
|
|
1810
|
+
'export function StatusGlyph(props: {size?: number}) { return null; }\n',
|
|
1811
|
+
);
|
|
1812
|
+
|
|
1813
|
+
const report = await compareDocs([
|
|
1814
|
+
status,
|
|
1815
|
+
{...status, name: 'StatusGlyph', sourcePath: glyphSource},
|
|
1816
|
+
]);
|
|
1817
|
+
|
|
1818
|
+
expect(report.unchecked).toEqual([]);
|
|
1819
|
+
expect(report.components).toBe(2);
|
|
1820
|
+
expect(kinds(report)).toEqual(['StatusGlyph.tone not_in_code']);
|
|
1821
|
+
expect(report.findings[0].source).toBe('components/StatusView.tsx');
|
|
1822
|
+
});
|
|
1823
|
+
|
|
1824
|
+
it('lists a member record that no file in the package declares as not compared', async () => {
|
|
1825
|
+
withTypeScript();
|
|
1826
|
+
const status = component(
|
|
1827
|
+
'Status',
|
|
1828
|
+
'export function Status(props: {value: string}) { return null; }\n',
|
|
1829
|
+
{
|
|
1830
|
+
components: [
|
|
1831
|
+
entry('Status', [prop('value', 'string', {required: true})]),
|
|
1832
|
+
entry('StatusGlyph', [prop('size', 'number')]),
|
|
1833
|
+
],
|
|
1834
|
+
},
|
|
1835
|
+
);
|
|
1836
|
+
|
|
1837
|
+
// Only another folder, or built output, declares the member.
|
|
1838
|
+
const report = await compareDocs([
|
|
1839
|
+
status,
|
|
1840
|
+
{...status, name: 'StatusGlyph', sourcePath: null},
|
|
1841
|
+
]);
|
|
1842
|
+
|
|
1843
|
+
expect(report.components).toBe(1);
|
|
1844
|
+
expect(report.findings).toEqual([]);
|
|
1845
|
+
expect(report.unchecked).toEqual([
|
|
1846
|
+
{
|
|
1847
|
+
component: 'StatusGlyph',
|
|
1848
|
+
doc: 'components/Status.doc.mjs',
|
|
1849
|
+
reason: 'no source file for StatusGlyph',
|
|
1850
|
+
},
|
|
1851
|
+
]);
|
|
1852
|
+
});
|
|
1853
|
+
|
|
1854
|
+
it('compares every entry of a family doc named after another export, and lists one with no source as not compared', async () => {
|
|
1855
|
+
withTypeScript();
|
|
1856
|
+
const menu = component(
|
|
1857
|
+
'Menu',
|
|
1858
|
+
[
|
|
1859
|
+
'export function MenuRoot(props: {open?: boolean}) { return null; }',
|
|
1860
|
+
'export function MenuItem(props: {value: string}) { return null; }',
|
|
1861
|
+
].join('\n'),
|
|
1862
|
+
{
|
|
1863
|
+
name: 'MenuRoot',
|
|
1864
|
+
hiddenComponents: ['MenuItem', 'MenuPart'],
|
|
1865
|
+
components: [
|
|
1866
|
+
entry('MenuItem', [prop('label', 'string')]),
|
|
1867
|
+
entry('MenuGhost', [prop('tone', 'string')]),
|
|
1868
|
+
entry('MenuPart', [prop('tone', 'string')]),
|
|
1869
|
+
],
|
|
1870
|
+
},
|
|
1871
|
+
);
|
|
1872
|
+
// A hidden entry declared in another file of the doc's folder.
|
|
1873
|
+
fs.writeFileSync(
|
|
1874
|
+
path.join(pkg, 'components', 'menu-parts.tsx'),
|
|
1875
|
+
'export function MenuPart(props: {depth?: number}) { return null; }\n',
|
|
1876
|
+
);
|
|
1877
|
+
|
|
1878
|
+
// Discovery records the doc under `MenuRoot` and leaves the hidden
|
|
1879
|
+
// `MenuItem` and `MenuPart` and the unbuilt `MenuGhost` without records
|
|
1880
|
+
// of their own.
|
|
1881
|
+
const report = await compareDocs([{...menu, name: 'MenuRoot'}]);
|
|
1882
|
+
|
|
1883
|
+
expect(kinds(report)).toEqual([
|
|
1884
|
+
'MenuItem.label not_in_code',
|
|
1885
|
+
'MenuItem.value not_in_doc',
|
|
1886
|
+
'MenuPart.depth not_in_doc',
|
|
1887
|
+
'MenuPart.tone not_in_code',
|
|
1888
|
+
]);
|
|
1889
|
+
expect(report.unchecked).toEqual([
|
|
1890
|
+
{
|
|
1891
|
+
component: 'MenuGhost',
|
|
1892
|
+
doc: 'components/Menu.doc.mjs',
|
|
1893
|
+
reason: 'Menu.tsx has no export named MenuGhost',
|
|
1894
|
+
},
|
|
1895
|
+
]);
|
|
1896
|
+
});
|
|
1897
|
+
|
|
1898
|
+
it("compares a doc's own props with the export its name names, which another file can hold", async () => {
|
|
1899
|
+
withTypeScript();
|
|
1900
|
+
const status = component(
|
|
1901
|
+
'Status',
|
|
1902
|
+
'export function Status(props: {value: string}) { return null; }\n',
|
|
1903
|
+
{name: 'StatusView', props: [prop('size', 'number')]},
|
|
1904
|
+
);
|
|
1905
|
+
const viewSource = path.join(pkg, 'components', 'StatusView.tsx');
|
|
1906
|
+
fs.writeFileSync(
|
|
1907
|
+
viewSource,
|
|
1908
|
+
'export function StatusView(props: {size?: number}) { return null; }\n',
|
|
1909
|
+
);
|
|
1910
|
+
|
|
1911
|
+
// Discovery names the record after the file, or after the export.
|
|
1912
|
+
const byFile = await compareDocs([status]);
|
|
1913
|
+
const byExport = await compareDocs([
|
|
1914
|
+
{...status, name: 'StatusView', sourcePath: viewSource},
|
|
1915
|
+
]);
|
|
1916
|
+
|
|
1917
|
+
for (const report of [byFile, byExport]) {
|
|
1918
|
+
expect(report.unchecked).toEqual([]);
|
|
1919
|
+
expect(report.components).toBe(1);
|
|
1920
|
+
expect(report.findings).toEqual([]);
|
|
1921
|
+
}
|
|
1922
|
+
});
|
|
1923
|
+
|
|
1924
|
+
it('compares a table for one component in each doc that has one', async () => {
|
|
1925
|
+
withTypeScript();
|
|
1926
|
+
const title = prop('title', 'string', {required: true});
|
|
1927
|
+
const list = component(
|
|
1928
|
+
'List',
|
|
1929
|
+
'export function List(props: {label: string}) { return null; }\n',
|
|
1930
|
+
{
|
|
1931
|
+
components: [
|
|
1932
|
+
entry('List', [prop('label', 'string', {required: true})]),
|
|
1933
|
+
entry('ListGroup', [title]),
|
|
1934
|
+
],
|
|
1935
|
+
},
|
|
1936
|
+
);
|
|
1937
|
+
const group = component(
|
|
1938
|
+
'ListGroup',
|
|
1939
|
+
'export function ListGroup(props: {title: string}) { return null; }\n',
|
|
1940
|
+
{props: [title, prop('stale', 'string')]},
|
|
1941
|
+
);
|
|
1942
|
+
|
|
1943
|
+
const report = await compareDocs([list, group]);
|
|
1944
|
+
|
|
1945
|
+
expect(report.components).toBe(3);
|
|
1946
|
+
expect(kinds(report)).toEqual(['ListGroup.stale not_in_code']);
|
|
1947
|
+
expect(report.findings[0].doc).toBe('components/ListGroup.doc.mjs');
|
|
1948
|
+
});
|
|
1949
|
+
|
|
1950
|
+
it('lists every component as not compared when a declaration file the config includes, or a type package it names, does not resolve', async () => {
|
|
1951
|
+
withTypeScript();
|
|
1952
|
+
const tsconfig = path.join(pkg, 'tsconfig.json');
|
|
1953
|
+
const config = JSON.parse(fs.readFileSync(tsconfig, 'utf8'));
|
|
1954
|
+
/** @param {object} options */
|
|
1955
|
+
const configure = options =>
|
|
1956
|
+
fs.writeFileSync(
|
|
1957
|
+
tsconfig,
|
|
1958
|
+
JSON.stringify({
|
|
1959
|
+
...config,
|
|
1960
|
+
compilerOptions: {...config.compilerOptions, ...options},
|
|
1961
|
+
include: ['components', 'types'],
|
|
1962
|
+
}),
|
|
1963
|
+
);
|
|
1964
|
+
fs.mkdirSync(path.join(pkg, 'types'));
|
|
1965
|
+
const globals = path.join(pkg, 'types', 'globals.d.ts');
|
|
1966
|
+
const tables = () => [
|
|
1967
|
+
component(
|
|
1968
|
+
'Omitted',
|
|
1969
|
+
'export function Omitted(props: Omit<{label: string; size?: number}, keyof MissingProps>) { return null; }\n',
|
|
1970
|
+
{
|
|
1971
|
+
props: [
|
|
1972
|
+
prop('label', 'string', {required: true}),
|
|
1973
|
+
prop('size', 'number'),
|
|
1974
|
+
],
|
|
1975
|
+
},
|
|
1976
|
+
),
|
|
1977
|
+
component(
|
|
1978
|
+
'Plain',
|
|
1979
|
+
'export function Plain(props: {label?: string}) { return null; }\n',
|
|
1980
|
+
{props: [prop('label', 'string')]},
|
|
1981
|
+
),
|
|
1982
|
+
];
|
|
1983
|
+
/** @param {RegExp} reason */
|
|
1984
|
+
const unread = reason =>
|
|
1985
|
+
['Omitted', 'Plain'].map(name => ({
|
|
1986
|
+
component: name,
|
|
1987
|
+
doc: `components/${name}.doc.mjs`,
|
|
1988
|
+
reason: expect.stringMatching(reason),
|
|
1989
|
+
}));
|
|
1990
|
+
|
|
1991
|
+
// A global type made from a package that isn't installed.
|
|
1992
|
+
configure({});
|
|
1993
|
+
fs.writeFileSync(
|
|
1994
|
+
globals,
|
|
1995
|
+
"import type {Props} from '@acme/not-installed';\ndeclare global { type MissingProps = Props; }\n",
|
|
1996
|
+
);
|
|
1997
|
+
let report = await compare(tables());
|
|
1998
|
+
expect(report.findings).toEqual([]);
|
|
1999
|
+
expect(report.unchecked).toEqual(
|
|
2000
|
+
unread(
|
|
2001
|
+
/depends on `@acme\/not-installed` \(imported in \.\.\/types\/globals\.d\.ts\)/,
|
|
2002
|
+
),
|
|
2003
|
+
);
|
|
2004
|
+
|
|
2005
|
+
// A global type made from a name declared nowhere.
|
|
2006
|
+
fs.writeFileSync(
|
|
2007
|
+
globals,
|
|
2008
|
+
'declare global { type MissingProps = NowhereProps; }\nexport {};\n',
|
|
2009
|
+
);
|
|
2010
|
+
report = await compare(tables());
|
|
2011
|
+
expect(report.findings).toEqual([]);
|
|
2012
|
+
expect(report.unchecked).toEqual(
|
|
2013
|
+
unread(
|
|
2014
|
+
/depends on `NowhereProps` \(used in \.\.\/types\/globals\.d\.ts\)/,
|
|
2015
|
+
),
|
|
2016
|
+
);
|
|
2017
|
+
|
|
2018
|
+
// A type package the config names that isn't installed.
|
|
2019
|
+
fs.writeFileSync(
|
|
2020
|
+
globals,
|
|
2021
|
+
'declare global { type MissingProps = {id?: string}; }\nexport {};\n',
|
|
2022
|
+
);
|
|
2023
|
+
configure({types: ['@acme/not-installed-globals']});
|
|
2024
|
+
report = await compare(tables());
|
|
2025
|
+
expect(report.findings).toEqual([]);
|
|
2026
|
+
expect(report.unchecked).toEqual(
|
|
2027
|
+
unread(
|
|
2028
|
+
/depends on the types `@acme\/not-installed-globals` \(named in \.\.\/tsconfig\.json\)/,
|
|
2029
|
+
),
|
|
2030
|
+
);
|
|
2031
|
+
|
|
2032
|
+
// Both resolved: compared.
|
|
2033
|
+
configure({types: []});
|
|
2034
|
+
report = await compare(tables());
|
|
2035
|
+
expect(report.unchecked).toEqual([]);
|
|
2036
|
+
expect(report.components).toBe(2);
|
|
2037
|
+
});
|
|
2038
|
+
|
|
2039
|
+
it("lists every table, with the error, when the package's TypeScript config has an error", async () => {
|
|
2040
|
+
withTypeScript();
|
|
2041
|
+
fs.writeFileSync(
|
|
2042
|
+
path.join(pkg, 'tsconfig.json'),
|
|
2043
|
+
JSON.stringify({extends: './missing-base.json'}),
|
|
2044
|
+
);
|
|
2045
|
+
const titled = component(
|
|
2046
|
+
'Titled',
|
|
2047
|
+
'export function Titled(props: {title?: string}) { return null; }\n',
|
|
2048
|
+
{props: [prop('title', 'string')]},
|
|
2049
|
+
);
|
|
2050
|
+
|
|
2051
|
+
const report = await compare([titled]);
|
|
2052
|
+
|
|
2053
|
+
expect(report.components).toBe(0);
|
|
2054
|
+
expect(report.unchecked).toEqual([
|
|
2055
|
+
expect.objectContaining({
|
|
2056
|
+
component: 'Titled',
|
|
2057
|
+
reason: expect.stringMatching(
|
|
2058
|
+
/config has an error.*missing-base\.json/,
|
|
2059
|
+
),
|
|
2060
|
+
}),
|
|
2061
|
+
]);
|
|
2062
|
+
});
|
|
2063
|
+
|
|
2064
|
+
it('accepts a name through an index signature only as TypeScript would', async () => {
|
|
2065
|
+
withTypeScript();
|
|
2066
|
+
const keyed = component(
|
|
2067
|
+
'Keyed',
|
|
2068
|
+
[
|
|
2069
|
+
'export interface KeyedProps {',
|
|
2070
|
+
' [key: `data-${string}`]: string | undefined;',
|
|
2071
|
+
' [key: `${string}:${string}`]: string;',
|
|
2072
|
+
' [key: `col${number}`]: number;',
|
|
2073
|
+
' [key: `on${Capitalize<string>}`]: () => void;',
|
|
2074
|
+
'}',
|
|
2075
|
+
'export function Keyed(props: KeyedProps) { return null; }',
|
|
2076
|
+
].join('\n'),
|
|
2077
|
+
{
|
|
2078
|
+
props: [
|
|
2079
|
+
prop('data-section', 'string'),
|
|
2080
|
+
prop('a:b', 'string'),
|
|
2081
|
+
prop('col2', 'number'),
|
|
2082
|
+
prop('onClick', '() => void'),
|
|
2083
|
+
prop('elevation', 'string'),
|
|
2084
|
+
prop('colSpan', 'string'),
|
|
2085
|
+
prop('onclick', 'string'),
|
|
2086
|
+
],
|
|
2087
|
+
},
|
|
2088
|
+
);
|
|
2089
|
+
const empty = component(
|
|
2090
|
+
'Empty',
|
|
2091
|
+
`interface EmptyProps { label?: string }
|
|
2092
|
+
export function Empty(props: EmptyProps | Record<string, never>) { return null; }`,
|
|
2093
|
+
{props: [prop('label', 'string'), prop('elevation', 'number')]},
|
|
2094
|
+
);
|
|
2095
|
+
const typedAny = component(
|
|
2096
|
+
'TypedAny',
|
|
2097
|
+
'export function TypedAny(props: any) { return null; }\n',
|
|
2098
|
+
{props: [prop('label', 'string')]},
|
|
2099
|
+
);
|
|
2100
|
+
const anyConst = component(
|
|
2101
|
+
'AnyConst',
|
|
2102
|
+
'export const AnyConst: any = null;\n',
|
|
2103
|
+
{props: [prop('label', 'string')]},
|
|
2104
|
+
);
|
|
2105
|
+
|
|
2106
|
+
const report = await compare([keyed, empty, typedAny, anyConst]);
|
|
2107
|
+
|
|
2108
|
+
expect(kinds(report)).toEqual([
|
|
2109
|
+
'Empty.elevation not_in_code',
|
|
2110
|
+
'Keyed.colSpan not_in_code',
|
|
2111
|
+
'Keyed.elevation not_in_code',
|
|
2112
|
+
'Keyed.onclick not_in_code',
|
|
2113
|
+
]);
|
|
2114
|
+
expect(report.unchecked.map(u => [u.component, u.reason])).toEqual([
|
|
2115
|
+
['AnyConst', expect.stringContaining('typed `any`')],
|
|
2116
|
+
['TypedAny', expect.stringContaining('typed `any`')],
|
|
2117
|
+
]);
|
|
2118
|
+
});
|
|
2119
|
+
|
|
2120
|
+
it("reads props through React's component types, lists the ones typed any or named by the element they render, and loads the ambient types tsconfig includes", async () => {
|
|
2121
|
+
withTypeScript();
|
|
2122
|
+
fs.mkdirSync(path.join(pkg, 'node_modules', '@types'), {recursive: true});
|
|
2123
|
+
fs.symlinkSync(
|
|
2124
|
+
path.dirname(require.resolve('@types/react/package.json')),
|
|
2125
|
+
path.join(pkg, 'node_modules', '@types', 'react'),
|
|
2126
|
+
'dir',
|
|
2127
|
+
);
|
|
2128
|
+
const tsconfig = path.join(pkg, 'tsconfig.json');
|
|
2129
|
+
const config = JSON.parse(fs.readFileSync(tsconfig, 'utf8'));
|
|
2130
|
+
fs.writeFileSync(
|
|
2131
|
+
tsconfig,
|
|
2132
|
+
JSON.stringify({...config, include: ['components', 'types']}),
|
|
2133
|
+
);
|
|
2134
|
+
fs.mkdirSync(path.join(pkg, 'types'));
|
|
2135
|
+
fs.writeFileSync(
|
|
2136
|
+
path.join(pkg, 'types', 'ambient.d.ts'),
|
|
2137
|
+
'interface AmbientProps { label?: string }\n',
|
|
2138
|
+
);
|
|
2139
|
+
const source = `import {forwardRef, memo, type FC, type ComponentType, type ComponentPropsWithoutRef, type ElementType} from 'react';
|
|
2140
|
+
export function Poly<E extends ElementType = 'div'>(props: {as?: E; padding?: number} & Omit<ComponentPropsWithoutRef<E>, 'as' | 'padding'>) { return null; }
|
|
2141
|
+
export const Fc: FC<any> = () => null;
|
|
2142
|
+
export const Memo = memo((props: any) => null);
|
|
2143
|
+
export const Fwd = forwardRef<HTMLDivElement, any>((props, ref) => null);
|
|
2144
|
+
export const FwdOmit = forwardRef<HTMLDivElement, Omit<any, 'as'>>((props, ref) => null);
|
|
2145
|
+
export const FwdPartial = memo(forwardRef<HTMLDivElement, Partial<any>>((props, ref) => null));
|
|
2146
|
+
export function Ambient(props: AmbientProps) { return null; }
|
|
2147
|
+
export const Typed: ComponentType<{label?: string}> = () => null;
|
|
2148
|
+
const withName = <P,>(c: ComponentType<P>): ComponentType<P> => c;
|
|
2149
|
+
export const Hoc = withName((props: {label?: string}) => null);`;
|
|
2150
|
+
const props = [prop('label', 'string'), prop('elevation', 'number')];
|
|
2151
|
+
|
|
2152
|
+
const report = await compare(
|
|
2153
|
+
'Fc Memo Fwd FwdOmit FwdPartial Poly Ambient Typed Hoc'
|
|
2154
|
+
.split(' ')
|
|
2155
|
+
.map(name => component(name, source, {props})),
|
|
2156
|
+
);
|
|
2157
|
+
|
|
2158
|
+
expect(kinds(report)).toEqual(
|
|
2159
|
+
'Ambient Hoc Typed'
|
|
2160
|
+
.split(' ')
|
|
2161
|
+
.map(name => `${name}.elevation not_in_code`),
|
|
2162
|
+
);
|
|
2163
|
+
expect(report.unchecked.map(u => [u.component, u.reason])).toEqual(
|
|
2164
|
+
['Fc', 'Fwd', 'FwdOmit', 'FwdPartial', 'Memo', 'Poly'].map(name => [
|
|
2165
|
+
name,
|
|
2166
|
+
expect.stringContaining(
|
|
2167
|
+
name === 'Poly' ? 'from a type parameter' : 'typed `any`',
|
|
2168
|
+
),
|
|
2169
|
+
]),
|
|
2170
|
+
);
|
|
2171
|
+
});
|
|
2172
|
+
|
|
2173
|
+
it('reads names that list several props, spreads, and fields through arrays, tuples, unions and index signatures', async () => {
|
|
2174
|
+
withTypeScript();
|
|
2175
|
+
const menu = component(
|
|
2176
|
+
'Menu',
|
|
2177
|
+
[
|
|
2178
|
+
'export interface MenuProps {',
|
|
2179
|
+
' onOpen?: () => void;',
|
|
2180
|
+
' onClose?: () => void;',
|
|
2181
|
+
' value?: {label: string; items: ReadonlyArray<{id: string}>};',
|
|
2182
|
+
' labels?: Record<string, string>;',
|
|
2183
|
+
' slots?: {[key: `slot-${string}`]: string};',
|
|
2184
|
+
" rows?: Array<{kind: 'a'; total: string} | {kind: 'b'}>;",
|
|
2185
|
+
' grid?: Array<Array<{id: string}>>;',
|
|
2186
|
+
' pair?: readonly [{id: string}, {id: number}];',
|
|
2187
|
+
'}',
|
|
2188
|
+
'export function Menu(props: MenuProps) { return null; }',
|
|
2189
|
+
].join('\n'),
|
|
2190
|
+
{
|
|
2191
|
+
props: [
|
|
2192
|
+
prop('onOpen / onClose', '() => void / () => void'),
|
|
2193
|
+
prop('...rest', 'Omit<OtherProps, "x">'),
|
|
2194
|
+
...['value.label', 'value.items[].id', 'value.missing'].map(name =>
|
|
2195
|
+
prop(name, 'string'),
|
|
2196
|
+
),
|
|
2197
|
+
...['labels.close', 'slots.slot-a', 'slots.other'].map(name =>
|
|
2198
|
+
prop(name, 'string'),
|
|
2199
|
+
),
|
|
2200
|
+
...['rows[].total', 'grid[][].id', 'grid[][].size', 'pair[].id'].map(
|
|
2201
|
+
name => prop(name, 'string'),
|
|
2202
|
+
),
|
|
2203
|
+
],
|
|
2204
|
+
},
|
|
2205
|
+
);
|
|
2206
|
+
|
|
2207
|
+
expect(kinds(await compare([menu]))).toEqual([
|
|
2208
|
+
'Menu.grid[][].size not_in_code',
|
|
2209
|
+
'Menu.slots.other not_in_code',
|
|
2210
|
+
'Menu.value.missing not_in_code',
|
|
2211
|
+
]);
|
|
2212
|
+
});
|
|
2213
|
+
|
|
2214
|
+
it("compares with TypeScript 7's TypeScript 6 API package when the project has one", async () => {
|
|
2215
|
+
const badge = component(
|
|
2216
|
+
'Badge',
|
|
2217
|
+
'export function Badge(props: {label: string}) { return null; }\n',
|
|
2218
|
+
{props: [prop('label', 'number')]},
|
|
2219
|
+
);
|
|
2220
|
+
withTypeScript7AndItsTypeScript6Api();
|
|
2221
|
+
|
|
2222
|
+
const report = await compare([badge]);
|
|
2223
|
+
|
|
2224
|
+
expect(report.status).toBe('checked');
|
|
2225
|
+
// The comparison ran: the documented `number` is judged against `string`.
|
|
2226
|
+
expect(
|
|
2227
|
+
report.findings.map(f => `${f.component}.${f.prop} ${f.kind}`),
|
|
2228
|
+
).toContain('Badge.label type_mismatch');
|
|
2229
|
+
});
|
|
2230
|
+
|
|
2231
|
+
it('skips with a reason, and compares nothing, without a TypeScript it can use', async () => {
|
|
2232
|
+
const badge = component(
|
|
2233
|
+
'Badge',
|
|
2234
|
+
'export function Badge(props: {label: string}) { return null; }\n',
|
|
2235
|
+
{props: [prop('label', 'number')]},
|
|
2236
|
+
);
|
|
2237
|
+
const skipped = (/** @type {string} */ reason) => ({
|
|
2238
|
+
status: 'skipped',
|
|
2239
|
+
reason,
|
|
2240
|
+
components: 0,
|
|
2241
|
+
typesCompared: 0,
|
|
2242
|
+
findings: [],
|
|
2243
|
+
unchecked: [],
|
|
2244
|
+
typesNotCompared: [],
|
|
2245
|
+
});
|
|
2246
|
+
|
|
2247
|
+
expect(await compare([badge])).toEqual(skipped(TYPESCRIPT_MISSING_REASON));
|
|
2248
|
+
expect(TYPESCRIPT_MISSING_REASON).toContain('npm install -D typescript@6');
|
|
2249
|
+
// TypeScript 7 has no JavaScript compiler API to load.
|
|
2250
|
+
const dir = path.join(pkg, 'node_modules', 'typescript');
|
|
2251
|
+
fs.mkdirSync(dir, {recursive: true});
|
|
2252
|
+
fs.writeFileSync(
|
|
2253
|
+
path.join(dir, 'package.json'),
|
|
2254
|
+
JSON.stringify({
|
|
2255
|
+
name: 'typescript',
|
|
2256
|
+
version: '7.0.0',
|
|
2257
|
+
exports: {'./package.json': './package.json'},
|
|
2258
|
+
}),
|
|
2259
|
+
);
|
|
2260
|
+
const report = await compare([badge]);
|
|
2261
|
+
expect(report.reason).toContain('TypeScript 7.0.0');
|
|
2262
|
+
expect(report.reason).toContain('npm install -D @typescript/typescript6');
|
|
2263
|
+
expect(report).toEqual(
|
|
2264
|
+
skipped(
|
|
2265
|
+
unusableTypeScriptReason(
|
|
2266
|
+
'7.0.0',
|
|
2267
|
+
'documented props were not compared with the code',
|
|
2268
|
+
),
|
|
2269
|
+
),
|
|
2270
|
+
);
|
|
2271
|
+
});
|
|
2272
|
+
});
|