@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.
Files changed (247) hide show
  1. package/README.md +74 -73
  2. package/api/build/help/help.mjs +1 -1
  3. package/api/discover/_adapter.d.mts +119 -12
  4. package/api/discover/_adapter.mjs +449 -10
  5. package/api/discover/_adapter.test.mjs +599 -17
  6. package/api/discover/_catalog-view.d.mts +16 -5
  7. package/api/discover/_catalog-view.mjs +32 -8
  8. package/api/discover/discover.d.mts +11 -0
  9. package/api/discover/discover.doc.mjs +7 -6
  10. package/api/discover/discover.mjs +107 -19
  11. package/api/discover/discover.type.d.mts +18 -7
  12. package/api/discover/discover.type.mjs +15 -7
  13. package/api/discover/list/list.d.mts +4 -3
  14. package/api/discover/list/list.mjs +17 -10
  15. package/api/docs/_adapter.d.mts +11 -0
  16. package/api/docs/_adapter.mjs +244 -1
  17. package/api/docs/integrationDocs.test.mjs +124 -0
  18. package/api/doctor/doctor.mjs +27 -3
  19. package/api/doctor/doctor.test.mjs +43 -0
  20. package/api/doctor/theme-checks.mjs +1 -1
  21. package/api/doctor/theme-checks.test.mjs +136 -0
  22. package/api/gap-report/gap-report.d.mts +3 -0
  23. package/api/gap-report/gap-report.doc.mjs +11 -3
  24. package/api/gap-report/gap-report.mjs +152 -48
  25. package/api/gap-report/gap-report.test.mjs +233 -0
  26. package/api/gap-report/gap-report.type.d.mts +41 -0
  27. package/api/gap-report/gap-report.type.mjs +18 -0
  28. package/api/index.d.mts +2 -0
  29. package/api/index.mjs +2 -0
  30. package/api/init/run/run.mjs +2 -2
  31. package/api/integration/_adapter.d.mts +32 -0
  32. package/api/integration/_adapter.mjs +80 -0
  33. package/api/integration/add-contribution.mjs +1 -1
  34. package/api/integration/add-contribution.test.mjs +4 -2
  35. package/api/integration/authoring-checks.mjs +44 -4
  36. package/api/integration/authoring-checks.test.mjs +515 -0
  37. package/api/integration/authoring-checks.type.d.mts +95 -0
  38. package/api/integration/authoring-checks.type.mjs +53 -2
  39. package/api/integration/check/check.d.mts +96 -0
  40. package/api/integration/check/check.mjs +357 -0
  41. package/api/integration/check/check.test.mjs +489 -0
  42. package/api/integration/integration-check.type.d.mts +118 -0
  43. package/api/integration/integration-check.type.mjs +84 -0
  44. package/api/integration/integrationCheck.doc.d.mts +10 -0
  45. package/api/integration/integrationCheck.doc.mjs +73 -0
  46. package/api/integration/integrationComponentConflicts.doc.mjs +43 -6
  47. package/api/integration/integrationDocConflicts.doc.mjs +19 -5
  48. package/api/integration/pack-check.mjs +292 -7
  49. package/api/integration/pack-check.test.mjs +341 -0
  50. package/api/integration/validate-integration.mjs +8 -0
  51. package/api/integration/validate-integration.test.mjs +11 -0
  52. package/api/integration/validate-integration.type.d.mts +8 -0
  53. package/api/integration/validate-integration.type.mjs +12 -1
  54. package/api/integration/validateIntegration.doc.mjs +19 -3
  55. package/api/search/search.d.mts +72 -12
  56. package/api/search/search.domain-priority.test.mjs +108 -18
  57. package/api/search/search.integration-check.test.mjs +24 -0
  58. package/api/search/search.mjs +501 -37
  59. package/api/search/search.test.mjs +139 -0
  60. package/api/theme/build/build.family.test.mjs +54 -6
  61. package/api/theme/build/build.mjs +114 -17
  62. package/api/theme/themeBuild.doc.mjs +4 -4
  63. package/api/upgrade/_adapter.d.mts +40 -2
  64. package/api/upgrade/_adapter.mjs +117 -4
  65. package/api/upgrade/run/cli-behind-core.test.mjs +438 -0
  66. package/api/upgrade/run/run.mjs +30 -1
  67. package/api/upgrade/status/status.d.mts +21 -5
  68. package/api/upgrade/status/status.mjs +45 -8
  69. package/api/upgrade/upgrade.doc.mjs +5 -3
  70. package/api/upgrade/upgrade.type.d.mts +23 -0
  71. package/api/upgrade/upgrade.type.mjs +16 -1
  72. package/assets/codemods/runner.mjs +7 -1
  73. package/assets/discover/official-packages.json +367 -0
  74. package/assets/docs/README.md +10 -0
  75. package/assets/docs/author-a-theme.doc.mjs +3 -3
  76. package/assets/docs/browser-support.doc.mjs +1 -1
  77. package/assets/docs/getting-started.doc.mjs +3 -3
  78. package/assets/docs/styling-overview.doc.mjs +1 -1
  79. package/assets/docs/theme.doc.dense.mjs +1 -1
  80. package/assets/docs/theme.doc.mjs +2 -2
  81. package/assets/docs/theme.doc.zh.mjs +1 -1
  82. package/assets/docs/tree/add-a-component.doc.mjs +1 -1
  83. package/assets/docs/tree/add-a-theme.doc.mjs +1 -1
  84. package/assets/docs/tree/add-a-topic.doc.mjs +11 -8
  85. package/assets/docs/tree/agent-guidance.doc.mjs +2 -2
  86. package/assets/docs/tree/block-template.doc.mjs +8 -20
  87. package/assets/docs/tree/build-the-template.doc.mjs +1 -1
  88. package/assets/docs/tree/check-the-props.doc.mjs +196 -0
  89. package/assets/docs/tree/check-your-docs.doc.mjs +61 -7
  90. package/assets/docs/tree/checks.doc.mjs +25 -17
  91. package/assets/docs/tree/codemods.doc.mjs +4 -4
  92. package/assets/docs/tree/component-family.doc.mjs +3 -5
  93. package/assets/docs/tree/component-imports.doc.mjs +2 -2
  94. package/assets/docs/tree/components.doc.mjs +1 -1
  95. package/assets/docs/tree/debug-and-gap-reports.doc.mjs +30 -21
  96. package/assets/docs/tree/define-the-theme.doc.mjs +6 -5
  97. package/assets/docs/tree/describe-the-component.doc.mjs +2 -2
  98. package/assets/docs/tree/docs.doc.mjs +3 -3
  99. package/assets/docs/tree/document-the-template.doc.mjs +1 -1
  100. package/assets/docs/tree/document-the-theme.doc.mjs +1 -1
  101. package/assets/docs/tree/export-template-assets.doc.mjs +1 -1
  102. package/assets/docs/tree/extend-or-replace.doc.mjs +6 -6
  103. package/assets/docs/tree/fonts-and-assets.doc.mjs +2 -2
  104. package/assets/docs/tree/generate-a-palette.doc.mjs +5 -3
  105. package/assets/docs/tree/grade-template-with-agent.doc.mjs +1 -1
  106. package/assets/docs/tree/integrations.doc.mjs +8 -0
  107. package/assets/docs/tree/layout-spacing.doc.mjs +2 -3
  108. package/assets/docs/tree/layout.doc.mjs +6 -6
  109. package/assets/docs/tree/links.doc.mjs +6 -5
  110. package/assets/docs/tree/migration-setup.doc.mjs +3 -2
  111. package/assets/docs/tree/migration-verification.doc.mjs +0 -1
  112. package/assets/docs/tree/migration.doc.mjs +0 -1
  113. package/assets/docs/tree/package-and-test.doc.mjs +1 -1
  114. package/assets/docs/tree/page-template.doc.mjs +4 -8
  115. package/assets/docs/tree/publishing.doc.mjs +4 -4
  116. package/assets/docs/tree/quick-start.doc.mjs +38 -22
  117. package/assets/docs/tree/replace-a-core-component.doc.mjs +4 -6
  118. package/assets/docs/tree/replace-a-core-template.doc.mjs +6 -8
  119. package/assets/docs/tree/sections-and-placement.doc.mjs +6 -8
  120. package/assets/docs/tree/see-it-in-an-app.doc.mjs +4 -2
  121. package/assets/docs/tree/short-and-findable.doc.mjs +6 -5
  122. package/assets/docs/tree/single-component.doc.mjs +14 -5
  123. package/assets/docs/tree/start-a-template.doc.mjs +3 -3
  124. package/assets/docs/tree/subcomponent.doc.mjs +6 -6
  125. package/assets/docs/tree/template-assets.doc.mjs +2 -2
  126. package/assets/docs/tree/template-doc-overview.doc.mjs +5 -16
  127. package/assets/docs/tree/template-fonts.doc.mjs +1 -1
  128. package/assets/docs/tree/template-icons.doc.mjs +1 -1
  129. package/assets/docs/tree/template-images-media.doc.mjs +1 -1
  130. package/assets/docs/tree/template-styles.doc.mjs +2 -2
  131. package/assets/docs/tree/templates.doc.mjs +1 -1
  132. package/assets/docs/tree/test-in-an-app.doc.mjs +3 -3
  133. package/assets/docs/tree/test-template-in-app.doc.mjs +1 -1
  134. package/assets/docs/tree/tokens-and-setup.doc.mjs +19 -15
  135. package/assets/docs/tree/troubleshooting.doc.mjs +8 -8
  136. package/assets/docs/tree/type-scale.doc.mjs +3 -3
  137. package/assets/docs/tree/upgrading.doc.mjs +4 -4
  138. package/assets/docs/tree/use-a-theme-in-an-app.doc.mjs +3 -1
  139. package/assets/docs/tree/verify-packed-template.doc.mjs +1 -1
  140. package/assets/docs/tree/versioning.doc.mjs +6 -6
  141. package/assets/docs/tree/write-the-template-file.doc.mjs +4 -3
  142. package/assets/docs/use-a-theme.doc.mjs +2 -2
  143. package/assets/theme.template.ts +1 -1
  144. package/authoring/codemod/codemod.doc.mjs +7 -3
  145. package/authoring/config/config.doc.mjs +9 -6
  146. package/authoring/config/type.ts +4 -2
  147. package/authoring/discover/discover.doc.mjs +1 -1
  148. package/authoring/doctypes/base/graph-fields.doc.mjs +6 -4
  149. package/authoring/doctypes/command/command.doc.mjs +14 -12
  150. package/authoring/doctypes/component/component.doc.mjs +29 -26
  151. package/authoring/doctypes/component/type.ts +7 -5
  152. package/authoring/doctypes/enum/enum.doc.mjs +7 -4
  153. package/authoring/doctypes/function/function.doc.mjs +9 -8
  154. package/authoring/doctypes/hook/hook.doc.mjs +5 -4
  155. package/authoring/doctypes/namespace/namespace.doc.mjs +7 -6
  156. package/authoring/doctypes/reference/reference.doc.mjs +11 -10
  157. package/authoring/doctypes/schema/schema.doc.mjs +9 -9
  158. package/authoring/doctypes/template/integration-template.doc.d.mts +11 -0
  159. package/authoring/doctypes/template/integration-template.doc.mjs +49 -0
  160. package/authoring/doctypes/template/parse.d.mts +6 -2
  161. package/authoring/doctypes/template/parse.mjs +3 -1
  162. package/authoring/doctypes/template/template.doc.mjs +3 -3
  163. package/authoring/doctypes/template/type.ts +22 -0
  164. package/authoring/doctypes/theme/theme.doc.mjs +2 -2
  165. package/authoring/gap-report/gap-report.doc.mjs +2 -2
  166. package/authoring/identity/identity.doc.mjs +3 -3
  167. package/authoring/index.d.ts +1 -0
  168. package/authoring/integration/integration.doc.mjs +12 -4
  169. package/authoring/integration/parse.test.mjs +10 -0
  170. package/authoring/integration/schema.d.mts +2 -0
  171. package/authoring/integration/schema.mjs +1 -0
  172. package/authoring/integration/type.ts +4 -0
  173. package/clients/cli/commands/build-theme.batch-report.test.mjs +3 -1
  174. package/clients/cli/commands/build-theme.mjs +1 -1
  175. package/clients/cli/commands/build-theme.path-safety.test.mjs +155 -21
  176. package/clients/cli/commands/cli-postinstall.test.mjs +49 -1
  177. package/clients/cli/commands/discover.broken-integration.test.mjs +4 -1
  178. package/clients/cli/commands/discover.doc.mjs +1 -1
  179. package/clients/cli/commands/discover.mjs +144 -93
  180. package/clients/cli/commands/discover.no-source.test.mjs +447 -33
  181. package/clients/cli/commands/discover.sources.test.mjs +20 -1
  182. package/clients/cli/commands/doctor-integration-components.doc.mjs +17 -4
  183. package/clients/cli/commands/doctor-integration-docs.doc.mjs +18 -5
  184. package/clients/cli/commands/doctor-integration-validate.doc.mjs +13 -2
  185. package/clients/cli/commands/doctor-integration.test.mjs +837 -0
  186. package/clients/cli/commands/doctor.mjs +250 -3
  187. package/clients/cli/commands/gap-report.doc.mjs +14 -2
  188. package/clients/cli/commands/gap-report.mjs +24 -2
  189. package/clients/cli/commands/gap-report.test.mjs +79 -0
  190. package/clients/cli/commands/integration-check.doc.mjs +85 -0
  191. package/clients/cli/commands/integration-check.test.mjs +370 -0
  192. package/clients/cli/commands/integration.doc.mjs +6 -2
  193. package/clients/cli/commands/integration.mjs +106 -1
  194. package/clients/cli/commands/json-contract.test.mjs +5 -1
  195. package/clients/cli/commands/setup-nudge.test.mjs +79 -1
  196. package/clients/cli/commands/swizzle.mjs +5 -3
  197. package/clients/cli/commands/swizzle.routing.test.mjs +18 -1
  198. package/clients/cli/commands/text-json-parity.test.mjs +5 -0
  199. package/clients/cli/commands/theme-build.doc.mjs +2 -2
  200. package/clients/cli/commands/upgrade.doc.mjs +14 -0
  201. package/clients/cli/commands/upgrade.older-cli.test.mjs +160 -0
  202. package/clients/cli/index.mjs +30 -5
  203. package/foundation/agent-docs/agent-doc-state.d.mts +14 -0
  204. package/foundation/agent-docs/agent-doc-state.mjs +38 -0
  205. package/foundation/agent-docs/agent-docs.d.mts +2 -1
  206. package/foundation/agent-docs/agent-docs.mjs +8 -6
  207. package/foundation/config/project.mjs +82 -0
  208. package/foundation/discovery/authoring-self-docs.mjs +2 -1
  209. package/foundation/discovery/component-discovery.d.mts +18 -0
  210. package/foundation/discovery/component-discovery.mjs +2 -2
  211. package/foundation/discovery/copied-passages.d.mts +216 -0
  212. package/foundation/discovery/copied-passages.mjs +720 -0
  213. package/foundation/discovery/copied-passages.test.mjs +124 -0
  214. package/foundation/discovery/doc-fluff.d.mts +112 -0
  215. package/foundation/discovery/doc-fluff.mjs +328 -0
  216. package/foundation/discovery/doc-fluff.test.mjs +142 -0
  217. package/foundation/discovery/doc-types.d.mts +157 -0
  218. package/foundation/discovery/doc-types.mjs +1224 -0
  219. package/foundation/discovery/doc-types.test.mjs +700 -0
  220. package/foundation/discovery/docs-discovery.d.mts +88 -0
  221. package/foundation/discovery/docs-discovery.mjs +83 -2
  222. package/foundation/discovery/integration-doc-files.d.mts +22 -0
  223. package/foundation/discovery/integration-doc-files.mjs +231 -0
  224. package/foundation/discovery/integration-doc-files.test.mjs +246 -0
  225. package/foundation/discovery/project-typescript.d.mts +11 -2
  226. package/foundation/discovery/project-typescript.mjs +32 -4
  227. package/foundation/discovery/prop-drift.d.mts +116 -3
  228. package/foundation/discovery/prop-drift.mjs +883 -41
  229. package/foundation/discovery/prop-drift.test.mjs +1738 -20
  230. package/foundation/discovery/type-match.d.mts +53 -0
  231. package/foundation/discovery/type-match.mjs +660 -0
  232. package/foundation/discovery/type-match.test.mjs +169 -0
  233. package/foundation/discovery/type-text.d.mts +68 -0
  234. package/foundation/discovery/type-text.mjs +658 -0
  235. package/foundation/discovery/type-text.test.mjs +196 -0
  236. package/foundation/doc-compiler/doc-loads.test.mjs +16 -2
  237. package/foundation/doc-compiler/links.d.mts +9 -5
  238. package/foundation/doc-compiler/links.mjs +4 -2
  239. package/foundation/env/package-manager.d.mts +32 -0
  240. package/foundation/env/package-manager.mjs +47 -0
  241. package/foundation/env/package-manager.test.mjs +194 -7
  242. package/foundation/fs/module-loader.d.mts +2 -1
  243. package/foundation/fs/module-loader.mjs +60 -4
  244. package/foundation/fs/module-loader.test.mjs +194 -0
  245. package/foundation/response/response-types.doc.mjs +14 -8
  246. package/package.json +11 -11
  247. package/scripts/postinstall.mjs +3 -3
@@ -7,6 +7,12 @@
7
7
  * The compiler is the project's own `typescript`. Without one it can use, the
8
8
  * comparison is skipped and says why. Every finding is a warning.
9
9
  *
10
+ * `comparePropDocs` finds each component doc's prop tables first: a doc's
11
+ * own `props` document the export its `name` names; each entry of a
12
+ * multi-component doc documents its own export, which a member record
13
+ * compares against its own source. Hook entries (`use…`) document parameters
14
+ * and returns, not props, and are not compared.
15
+ *
10
16
  * Each table (a doc's `props`, or a multi-component doc's entry, with the
11
17
  * export it documents and the files that may hold it) is compared with that
12
18
  * export's props type:
@@ -22,29 +28,37 @@
22
28
  * pass the prop (one only some members of the props type require, or that
23
29
  * the package's `defaultProps` fills, is optional, as it is to JSX
24
30
  * callers; an entry that names several props claims its `required` for
25
- * each).
31
+ * each);
32
+ * - `type_mismatch`: for a prop declared in the package, the documented type
33
+ * differs from the code's where both sides read exactly (type-match.mjs
34
+ * states the rules); every other type is listed as not compared.
26
35
  *
27
- * @input Prop tables, the package root, and the directories to resolve
28
- * `typescript` from.
36
+ * @input Component records (name, docPath, sourcePath), or prop tables, the
37
+ * package root, and the directories to resolve `typescript` from.
29
38
  * @output A PropDriftReport.
30
- * @position Foundation discovery: it reads the package's TypeScript and
31
- * returns a report, and prints nothing itself.
39
+ * @position Shared by `astryx doctor integration components` and the Astryx
40
+ * repository's test over Core's own component docs.
32
41
  */
33
42
 
34
43
  import * as fs from 'node:fs';
35
44
  import * as path from 'node:path';
45
+ import {folderSourceFor} from './component-discovery.mjs';
46
+ import {loadComponentDoc} from './component-loader.mjs';
36
47
  import {readPackageTsconfig} from './package-tsconfig.mjs';
37
48
  import {
38
49
  resolveProjectTypeScript,
50
+ TYPESCRIPT_FIX,
39
51
  unusableTypeScriptReason,
40
52
  } from './project-typescript.mjs';
53
+ import {judgeType} from './type-match.mjs';
54
+ import {typeTextTooOld} from './type-text.mjs';
41
55
  import {
42
56
  unresolvedGlobals,
43
57
  unresolvedReference,
44
58
  } from './unresolved-references.mjs';
45
59
 
46
60
  /**
47
- * @typedef {'not_in_code' | 'not_in_doc' | 'required_mismatch'} PropDriftKind
61
+ * @typedef {'not_in_code' | 'not_in_doc' | 'required_mismatch' | 'type_mismatch'} PropDriftKind
48
62
  */
49
63
 
50
64
  /**
@@ -56,6 +70,10 @@ import {
56
70
  * @property {string} message
57
71
  * @property {string} doc the doc file, relative to the package root
58
72
  * @property {string} source the source file, relative to the package root
73
+ * @property {string} [docType] the documented type, as written
74
+ * @property {string} [codeType] the type as the source declares it
75
+ * @property {boolean} [docRequired]
76
+ * @property {boolean} [codeRequired]
59
77
  */
60
78
 
61
79
  /**
@@ -65,6 +83,16 @@ import {
65
83
  * @property {string} reason why its props could not be read
66
84
  */
67
85
 
86
+ /**
87
+ * @typedef {object} PropDriftTypesNotCompared
88
+ * @property {string} component
89
+ * @property {string} doc the doc file, relative to the package root
90
+ * @property {string} reason why: not a form the check judges exactly, a
91
+ * field the prop's variants give different types, a standard prop
92
+ * (`className`, `style`, …), or a prop declared outside the package
93
+ * @property {string[]} props documented props whose types were not compared
94
+ */
95
+
68
96
  /**
69
97
  * @typedef {object} PropDriftReport
70
98
  * @property {'checked' | 'skipped'} status `skipped` means nothing was
@@ -72,12 +100,16 @@ import {
72
100
  * @property {string} [reason]
73
101
  * @property {string} [typescript] the version of the compiler that read the props
74
102
  * @property {number} components how many documented prop tables were compared
103
+ * @property {number} typesCompared how many documented prop types were judged
104
+ * @property {string} [typesSkipped] why no prop type was judged
75
105
  * @property {PropDriftFinding[]} findings
76
106
  * @property {PropDriftUnchecked[]} unchecked documented components whose props
77
107
  * could not be read from the source
78
108
  * @property {Array<PropDriftUnchecked & {props: string[]}>} [propsNotChecked]
79
109
  * props a mapped type builds from keys the check can't trace, which a doc
80
110
  * leaving out draws no warning for
111
+ * @property {PropDriftTypesNotCompared[]} typesNotCompared every other
112
+ * documented prop type, grouped by why it was not judged
81
113
  */
82
114
 
83
115
  /**
@@ -88,6 +120,15 @@ import {
88
120
  * @property {string[]} sources the files that may hold the export, in order
89
121
  */
90
122
 
123
+ /**
124
+ * @typedef {object} PropDriftComponent
125
+ * @property {string} name
126
+ * @property {string} docPath absolute path to the component's `.doc.*` file
127
+ * @property {string | null} sourcePath absolute path to its same-stem
128
+ * source, or null when no file in the package declares it (a family member
129
+ * only another folder or built output defines)
130
+ */
131
+
91
132
  /**
92
133
  * Props a doc never needs to list: the internal and styling props the
93
134
  * `ComponentPropDoc` authoring guidance says to skip, `ref`, which Core's
@@ -104,10 +145,35 @@ export const PROPS_NOT_DOCUMENTED = Object.freeze([
104
145
 
105
146
  /** The guidance's internal props: a declaration tagged `@internal`. */
106
147
  const SKIP_TAGS = new Set(['internal']);
148
+ /** Why a documented prop type was not judged (`typesNotCompared`). */
149
+ const NOT_EXACT = 'not a form the check judges exactly';
150
+ /** A field documented with one variant's type, where they differ. */
151
+ const VARIANTS = "the prop's variants give this field different types";
152
+ /** @type {(name: string, code?: CodeProp) => string} why a prop is not judged */
153
+ const outside = (name, code) =>
154
+ code?.untraced
155
+ ? NOT_EXACT
156
+ : PROPS_NOT_DOCUMENTED.includes(name)
157
+ ? 'standard prop, not compared'
158
+ : 'declared outside the package, not compared';
159
+ const HOOK_NAME = /^use[A-Z0-9]/;
107
160
 
108
161
  /** The line both surfaces print when the compiler is missing. */
109
- export const TYPESCRIPT_MISSING_REASON =
110
- 'TypeScript is not installed in this project, so documented props were not compared with the code. Install typescript and run this check again.';
162
+ export const TYPESCRIPT_MISSING_REASON = `TypeScript is not installed in this project, so documented props were not compared with the code. ${TYPESCRIPT_FIX}`;
163
+
164
+ /**
165
+ * @template T
166
+ * @param {() => T} compute
167
+ * @returns {() => T}
168
+ */
169
+ function memo(compute) {
170
+ /** @type {{value: T} | null} */
171
+ let cached = null;
172
+ return () => {
173
+ if (cached == null) cached = {value: compute()};
174
+ return cached.value;
175
+ };
176
+ }
111
177
 
112
178
  /** @type {WeakMap<Function, (file: string) => boolean>} */
113
179
  const MINE = new WeakMap();
@@ -164,7 +230,15 @@ function ownFileTest(root) {
164
230
  * from keys it writes
165
231
  * @property {boolean} [untraced] built from keys the check can't trace
166
232
  * @property {boolean} skipTagged its declaration carries an internal JSDoc tag
233
+ * @property {string[]} declared the type as written, per own declaration
234
+ * @property {() => string[][]} renderings the compiler's renderings, computed on demand
167
235
  * @property {() => any[]} types the prop's type in each member of the props type
236
+ * @property {boolean} intersected one member declares it more than once (an
237
+ * intersection), so no one declaration's text is its type
238
+ * @property {() => any[] | null} indexed the type an index signature gives
239
+ * the name in each member that lacks the prop; null when two signatures
240
+ * in one member take it
241
+ * @property {any} location where names in the prop's type resolve
168
242
  */
169
243
 
170
244
  /**
@@ -178,7 +252,7 @@ function ownFileTest(root) {
178
252
  * @param {import('./unresolved-references.mjs').UnresolvedReference | null} ambient
179
253
  * what the program's other roots cannot resolve, which any component may
180
254
  * use
181
- * @returns {{ok: true, props: Map<string, CodeProp>, accepts: (name: string) => boolean} | {ok: false, reason: string}}
255
+ * @returns {{ok: true, props: Map<string, CodeProp>, accepts: (name: string) => any} | {ok: false, reason: string}}
182
256
  */
183
257
  function readCodeProps(
184
258
  ts,
@@ -290,7 +364,7 @@ function readCodeProps(
290
364
  }
291
365
  /** @type {Map<string, CodeProp>} */
292
366
  const props = new Map();
293
- if (propsTypes.length === 0) return {ok: true, props, accepts: () => false};
367
+ if (propsTypes.length === 0) return {ok: true, props, accepts: () => []};
294
368
 
295
369
  // Props that are `any`, written so or made from it (`FC<any>`, or
296
370
  // `forwardRef<E, any>`'s `Omit<any, 'ref'>`).
@@ -340,6 +414,14 @@ function readCodeProps(
340
414
  reason: `${exportName}'s props take some of their names from a type parameter, such as the element it renders, so which props it accepts is known only where it is used`,
341
415
  };
342
416
  }
417
+ // Each member of a prop's type is printed on its own, and long prints are
418
+ // truncated: a type that expands to a huge structure (StyleX styles, say)
419
+ // never matches a documented type anyway, and printing it whole can take
420
+ // gigabytes.
421
+ const flags =
422
+ ts.TypeFormatFlags.UseSingleQuotesForStringLiteralType |
423
+ ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope;
424
+
343
425
  // `defaultProps` the package sets (a class's static field, one assigned to a
344
426
  // function, or the wrapped component's through up to two layers of `memo`
345
427
  // and `lazy`, as React's JSX types read it) lets JSX callers leave its props
@@ -428,27 +510,266 @@ function readCodeProps(
428
510
  /** @param {any} tag */ tag => SKIP_TAGS.has(tag.tagName.text),
429
511
  ),
430
512
  ),
513
+ // A property's `type` is the prop's type; a method's is its return
514
+ // type, so a method prop reads as the compiler renders it. A
515
+ // declaration's text counts only for its own symbol, or where a mapping
516
+ // copies its type unchanged (`Pick`, `Omit`): one that rewrites it
517
+ // (`{[K in keyof T]: T[K][]}`, `Required<T>`) is judged by its type.
518
+ declared: symbols.flatMap(
519
+ /** @param {any} s */ s =>
520
+ (s.declarations ?? [])
521
+ .filter(
522
+ /** @param {any} d */ d =>
523
+ ownDeclarations.includes(d) &&
524
+ d.type &&
525
+ !ts.isMethodSignature(d) &&
526
+ !ts.isMethodDeclaration(d) &&
527
+ copiesType(ts, checker, s, d, sourceFile),
528
+ )
529
+ .map(/** @param {any} d */ d => d.type.getText(d.getSourceFile())),
530
+ ),
431
531
  types: () =>
432
532
  symbols.map(
433
533
  /** @param {any} s */ s =>
434
534
  checker.getTypeOfSymbolAtLocation(s, sourceFile),
435
535
  ),
536
+ intersected: symbols.some(
537
+ /** @param {any} s */ s =>
538
+ (s.declarations ?? []).filter(/** @param {any} d */ d => d.type)
539
+ .length > 1,
540
+ ),
541
+ indexed: memo(() => {
542
+ const lacking = members.filter(
543
+ /** @param {any} m */ m => !checker.getPropertyOfType(m, name),
544
+ );
545
+ const each = lacking.map(
546
+ /** @param {any} m */ m => indexesFor(ts, checker, [m], name),
547
+ );
548
+ return each.some(indexes => indexes.length > 1)
549
+ ? null
550
+ : each.flatMap(indexes => indexes.map(info => info.type));
551
+ }),
552
+ location: ownDeclarations[0] ?? sourceFile,
553
+ // Printed where the prop is declared, where a documented type's names
554
+ // resolve: a type another file imports under another name prints as
555
+ // that file names it.
556
+ renderings: memo(() => {
557
+ const at = ownDeclarations[0] ?? sourceFile;
558
+ /** @type {string[]} */
559
+ const printed = [];
560
+ /** @type {string[]} */
561
+ const expanded = [];
562
+ for (const s of symbols) {
563
+ const propType = checker.getTypeOfSymbolAtLocation(s, sourceFile);
564
+ for (const part of inDeclaredOrder(ts, checker, propType, s)) {
565
+ printed.push(checker.typeToString(part, at, flags));
566
+ expanded.push(
567
+ checker.typeToString(
568
+ part,
569
+ at,
570
+ flags | ts.TypeFormatFlags.InTypeAlias,
571
+ ),
572
+ );
573
+ }
574
+ }
575
+ return [printed, expanded];
576
+ }),
436
577
  });
437
578
  }
438
579
 
439
- /** Does an index signature of the props type accept this name? */
580
+ /** The index signatures of the props type that take this name. */
440
581
  const accepts = (/** @type {string} */ name) =>
441
- members.some(
442
- /** @param {any} member */ member =>
443
- (checker.getIndexInfosOfType?.(member) ?? []).some(
444
- /** @param {any} info */ info =>
445
- !holdsNothing(ts, info.type) &&
446
- keyAccepts(ts, checker, info.keyType, name),
447
- ),
448
- );
582
+ indexesFor(ts, checker, members, name);
449
583
  return {ok: true, props, accepts};
450
584
  }
451
585
 
586
+ /**
587
+ * A prop type's union members in the order the source declares them,
588
+ * through the type aliases it names; members it doesn't reach, such as the
589
+ * `undefined` an optional prop adds, follow, sorted by their text. The
590
+ * compiler's own order follows when each type was first made, so it would
591
+ * move with whatever read the types first.
592
+ * @param {any} ts
593
+ * @param {any} checker
594
+ * @param {any} type
595
+ * @param {any} symbol the prop
596
+ * @returns {any[]}
597
+ */
598
+ function inDeclaredOrder(ts, checker, type, symbol) {
599
+ if (!type.isUnion()) return [type];
600
+ /** @type {any[]} */
601
+ const order = [];
602
+ const seen = new Set();
603
+ /** @type {(node: any) => void} */
604
+ const visit = node => {
605
+ if (ts.isParenthesizedTypeNode(node)) return visit(node.type);
606
+ if (ts.isUnionTypeNode(node)) return node.types.forEach(visit);
607
+ if (ts.isTypeReferenceNode(node) && !node.typeArguments) {
608
+ let target = checker.getSymbolAtLocation(node.typeName);
609
+ if (target && target.flags & ts.SymbolFlags.Alias) {
610
+ target = checker.getAliasedSymbol(target);
611
+ }
612
+ const alias = target?.declarations?.find(ts.isTypeAliasDeclaration);
613
+ if (alias && !alias.typeParameters) {
614
+ if (!seen.has(alias)) {
615
+ seen.add(alias);
616
+ visit(alias.type);
617
+ }
618
+ return;
619
+ }
620
+ }
621
+ const found = checker.getTypeFromTypeNode(node);
622
+ order.push(...(found.isUnion() ? found.types : [found]));
623
+ };
624
+ for (const declaration of symbol.declarations ?? []) {
625
+ if (declaration.type) visit(declaration.type);
626
+ }
627
+ /** @type {(t: any) => number} */
628
+ const place = t => {
629
+ const at = order.indexOf(t);
630
+ return at === -1 ? order.length : at;
631
+ };
632
+ /** @type {(t: any) => string} */
633
+ const text = t => checker.typeToString(t);
634
+ return [...type.types].sort(
635
+ (a, b) => place(a) - place(b) || text(a).localeCompare(text(b)),
636
+ );
637
+ }
638
+
639
+ /**
640
+ * Whether a declaration's text reads as the prop's type: it is the prop's own
641
+ * declaration, or the generic type's prop it instantiates when the result
642
+ * prints as the declared type in the component's file (a generic component
643
+ * passing its own type parameters on), or a mapping copies its type
644
+ * unchanged. Instantiated with another type (`Box<string>`, `ListProps<T[]>`,
645
+ * or an imported `Item` the file names another way), its text names a type
646
+ * the prop no longer has.
647
+ * @param {any} ts
648
+ * @param {any} checker
649
+ * @param {any} symbol the prop
650
+ * @param {any} declaration one of its declarations
651
+ * @param {any} location
652
+ */
653
+ function copiesType(ts, checker, symbol, declaration, location) {
654
+ const own = declaration.name && checker.getSymbolAtLocation(declaration.name);
655
+ if (!own) return false;
656
+ if (own === symbol) return true;
657
+ const type = checker.getTypeOfSymbolAtLocation(symbol, location);
658
+ const declared = checker.getTypeOfSymbolAtLocation(own, location);
659
+ /** @param {any} t */
660
+ const print = t =>
661
+ checker.typeToString(t, location, ts.TypeFormatFlags.NoTruncation);
662
+ return (
663
+ declared === type ||
664
+ (own === (symbol.links ?? symbol).target && print(type) === print(declared))
665
+ );
666
+ }
667
+
668
+ /**
669
+ * Does the code take `undefined` itself, written or through an alias? With
670
+ * `exactOptionalPropertyTypes` that is part of the type a message shows;
671
+ * otherwise an optional prop takes `undefined` anyway.
672
+ * @param {any} checker
673
+ * @param {CodeProp} code
674
+ * @param {boolean} exactOptional
675
+ */
676
+ function ownUndefined(checker, code, exactOptional) {
677
+ const undefinedType = checker.getUndefinedType?.();
678
+ return (
679
+ exactOptional &&
680
+ (code.declared.some(text => /\bundefined\b/.test(text)) ||
681
+ code
682
+ .types()
683
+ .some(t => (t.isUnion() ? t.types : [t]).includes(undefinedType)))
684
+ );
685
+ }
686
+
687
+ /**
688
+ * One of a prop's renderings for a message, without the `undefined` an
689
+ * optional prop adds, unless the code takes it itself.
690
+ * @param {CodeProp} code
691
+ * @param {number} which 0 as printed, 1 expanded
692
+ * @param {boolean} keepUndefined
693
+ */
694
+ function rendered(code, which, keepUndefined) {
695
+ const parts = code.renderings()[which];
696
+ return [
697
+ ...new Set(
698
+ parts.filter(
699
+ part =>
700
+ !(code.required !== true && part === 'undefined' && !keepUndefined),
701
+ ),
702
+ ),
703
+ ].join(' | ');
704
+ }
705
+
706
+ /**
707
+ * The prop's type for a message: as the source writes it when its
708
+ * declarations agree, else (a method or accessor, or declarations that differ
709
+ * or narrow each other in an intersection) as the compiler prints it.
710
+ * @param {CodeProp} code
711
+ * @param {boolean} keepUndefined
712
+ */
713
+ function codeTypeText(code, keepUndefined) {
714
+ const written = [...new Set(code.declared)];
715
+ const text =
716
+ written.length === 1 ? written[0] : rendered(code, 0, keepUndefined);
717
+ return text.replace(/\s+/g, ' ').trim();
718
+ }
719
+
720
+ /**
721
+ * Judge a documented type against a prop's code type (type-match.mjs).
722
+ * @param {any} ts
723
+ * @param {any} checker
724
+ * @param {string} docType
725
+ * @param {CodeProp} code
726
+ * @param {{nullable: boolean, exactOptional: boolean, isDefaultLibrary: (file: any) => boolean}} reading
727
+ * how the project checks null, and which files are TypeScript's own
728
+ */
729
+ function judgeProp(ts, checker, docType, code, reading) {
730
+ // A member that lacks the prop may take its name through an index
731
+ // signature, which a doc may describe too.
732
+ const indexed = code.indexed();
733
+ if (indexed == null) return 'not_compared';
734
+ return judgeMembers(docType, [...code.types(), ...indexed], {
735
+ ts,
736
+ checker,
737
+ location: code.location,
738
+ optional: code.required !== true && !reading.exactOptional,
739
+ ...reading,
740
+ // Declared text reads as the type only when each declaration is one
741
+ // member's; intersected declarations narrow each other.
742
+ written: code.intersected
743
+ ? code.renderings()
744
+ : [
745
+ code.declared,
746
+ ...code.declared.map(text => [text]),
747
+ ...code.renderings(),
748
+ ],
749
+ });
750
+ }
751
+
752
+ /**
753
+ * Judge a documented type against the types a prop has in the members of a
754
+ * union of props types: a doc may describe one member, so any member's type,
755
+ * or their union, matches, and it is a mismatch only against every one.
756
+ * @param {string} docType
757
+ * @param {any[]} types the type in each member
758
+ * @param {import('./type-match.mjs').JudgeContext} ctx
759
+ * @returns {import('./type-match.mjs').TypeVerdict}
760
+ */
761
+ function judgeMembers(docType, types, ctx) {
762
+ const candidates =
763
+ types.length > 1 ? [types, ...types.map(type => [type])] : [types];
764
+ const verdicts = candidates.map(candidate =>
765
+ judgeType(docType, candidate, ctx),
766
+ );
767
+ if (verdicts.includes('match')) return 'match';
768
+ return verdicts.every(verdict => verdict === 'mismatch')
769
+ ? 'mismatch'
770
+ : 'not_compared';
771
+ }
772
+
452
773
  /**
453
774
  * The props one doc entry names. `a / b` documents each of `a` and `b`, with
454
775
  * the entry's `required` (and the type split the same way when it splits
@@ -514,37 +835,101 @@ function holders(ts, checker, type, depth = 0) {
514
835
  }
515
836
 
516
837
  /**
517
- * Does a field path exist in a prop's type, in at least one member of a
518
- * union (through arrays)?
838
+ * Whether a type takes `undefined` (an index signature's value type, which
839
+ * TypeScript holds an explicit `undefined` to).
840
+ * @param {any} ts
841
+ * @param {any} type
842
+ */
843
+ function takesUndefined(ts, type) {
844
+ return (type.isUnion() ? type.types : [type]).some(
845
+ /** @param {any} t */ t => t.flags & ts.TypeFlags.Undefined,
846
+ );
847
+ }
848
+
849
+ /**
850
+ * The types a field path reaches in a prop's type (in each member of a union
851
+ * that has it, through arrays at any depth), and whether its last field is
852
+ * optional: null when no member has the path, `any` when a type on the way
853
+ * is `any` or `unknown`, which holds any field.
519
854
  * @param {any} ts
520
855
  * @param {any} checker
521
856
  * @param {CodeProp} code
522
857
  * @param {string[]} fieldPath
858
+ * @returns {{types: any[], optional: boolean, ambiguous: boolean, indexed: boolean} | 'any' | null}
859
+ * `ambiguous` when two index signatures take a field on the path, and
860
+ * `indexed` when only index signatures take the last field
523
861
  */
524
- function fieldExists(ts, checker, code, fieldPath) {
862
+ function fieldTypes(ts, checker, code, fieldPath) {
525
863
  /** @type {any[]} */
526
864
  let current = code.types();
865
+ let optional = false;
866
+ let ambiguous = false;
867
+ let indexed = false;
527
868
  for (const field of fieldPath) {
528
869
  /** @type {any[]} */
529
870
  const next = [];
871
+ optional = false;
872
+ let byName = false;
530
873
  for (const holder of current.flatMap(t => holders(ts, checker, t))) {
531
- if (holder.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) return true;
874
+ if (holder.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
875
+ return 'any';
876
+ }
532
877
  const prop = checker.getPropertyOfType(holder, field);
533
- // Or a field an index signature takes (`Record<string, string>`).
534
- const index = prop
535
- ? null
536
- : (checker.getIndexInfosOfType?.(holder) ?? []).find(
537
- /** @param {any} info */ info =>
538
- !holdsNothing(ts, info.type) &&
539
- keyAccepts(ts, checker, info.keyType, field),
540
- );
541
- if (prop) next.push(checker.getTypeOfSymbol(prop));
542
- else if (index) next.push(index.type);
878
+ if (prop) {
879
+ next.push(checker.getTypeOfSymbol(prop));
880
+ if (prop.flags & ts.SymbolFlags.Optional) optional = true;
881
+ byName = true;
882
+ continue;
883
+ }
884
+ // A field an index signature takes (`Record<string, string>`) is of the
885
+ // index's type, which takes `undefined` only when it says so; two that
886
+ // take it hold it to both.
887
+ const indexes = indexesFor(ts, checker, [holder], field);
888
+ if (indexes.length > 1) ambiguous = true;
889
+ if (indexes.length > 0) {
890
+ next.push(indexes[0].type);
891
+ if (takesUndefined(ts, indexes[0].type)) optional = true;
892
+ }
543
893
  }
544
- if (next.length === 0) return false;
894
+ if (next.length === 0) return null;
545
895
  current = next;
896
+ indexed = !byName;
546
897
  }
547
- return true;
898
+ return {types: current, optional, ambiguous, indexed};
899
+ }
900
+
901
+ /**
902
+ * The types a field path reaches, printed where the prop is declared, where
903
+ * a documented type's names resolve.
904
+ * @param {any} ts
905
+ * @param {any} checker
906
+ * @param {{types: any[], optional: boolean}} reached
907
+ * @param {boolean} exactOptional the project sets `exactOptionalPropertyTypes`
908
+ * @param {any} location where the prop is declared
909
+ * @returns {string[]}
910
+ */
911
+ function fieldTypeText(ts, checker, reached, exactOptional, location) {
912
+ // An optional field's `undefined` is the compiler's own; with
913
+ // `exactOptionalPropertyTypes` it is a marker of its own, and one the code
914
+ // declares stays.
915
+ const own = /** @param {any} t */ t =>
916
+ reached.optional &&
917
+ t.flags & ts.TypeFlags.Undefined &&
918
+ (!exactOptional || t !== checker.getUndefinedType?.());
919
+ return [
920
+ ...new Set(
921
+ reached.types
922
+ .flatMap(t => (t.isUnion() ? t.types : [t]))
923
+ .filter(t => !own(t))
924
+ .map(t =>
925
+ checker.typeToString(
926
+ t,
927
+ location,
928
+ ts.TypeFormatFlags.UseSingleQuotesForStringLiteralType,
929
+ ),
930
+ ),
931
+ ),
932
+ ];
548
933
  }
549
934
 
550
935
  /**
@@ -760,6 +1145,33 @@ function keyAccepts(ts, checker, key, name) {
760
1145
  return new RegExp(`^${pattern}$`).test(name);
761
1146
  }
762
1147
 
1148
+ /**
1149
+ * The index signatures of these types that take a name, as TypeScript
1150
+ * matches keys. TypeScript holds a value to every one of them.
1151
+ * @param {any} ts
1152
+ * @param {any} checker
1153
+ * @param {any[]} types
1154
+ * @param {string} name
1155
+ * @returns {any[]}
1156
+ */
1157
+ function indexesFor(ts, checker, types, name) {
1158
+ // One signature seen through several union members is one signature.
1159
+ const seen = new Set();
1160
+ return types
1161
+ .flatMap(type => checker.getIndexInfosOfType?.(type) ?? [])
1162
+ .filter(
1163
+ /** @param {any} info */ info => {
1164
+ const key = `${checker.typeToString(info.keyType)}\0${checker.typeToString(info.type)}`;
1165
+ if (seen.has(key)) return false;
1166
+ seen.add(key);
1167
+ return (
1168
+ !holdsNothing(ts, info.type) &&
1169
+ keyAccepts(ts, checker, info.keyType, name)
1170
+ );
1171
+ },
1172
+ );
1173
+ }
1174
+
763
1175
  /**
764
1176
  * Can a value of this type be passed at all? `never`, or only `undefined`
765
1177
  * (`Record<string, never>`, the usual way to write "no props"), cannot.
@@ -1206,8 +1618,10 @@ export function comparePropTables(tables, options) {
1206
1618
  )
1207
1619
  : TYPESCRIPT_MISSING_REASON,
1208
1620
  components: 0,
1621
+ typesCompared: 0,
1209
1622
  findings: [],
1210
1623
  unchecked: [],
1624
+ typesNotCompared: [],
1211
1625
  };
1212
1626
  }
1213
1627
  const {ts, version} = compiler;
@@ -1227,8 +1641,10 @@ export function comparePropTables(tables, options) {
1227
1641
  status: 'checked',
1228
1642
  typescript: version,
1229
1643
  components: 0,
1644
+ typesCompared: 0,
1230
1645
  findings: [],
1231
1646
  unchecked,
1647
+ typesNotCompared: [],
1232
1648
  };
1233
1649
  }
1234
1650
 
@@ -1251,8 +1667,10 @@ export function comparePropTables(tables, options) {
1251
1667
  status: 'checked',
1252
1668
  typescript: version,
1253
1669
  components: 0,
1670
+ typesCompared: 0,
1254
1671
  findings: [],
1255
1672
  unchecked,
1673
+ typesNotCompared: [],
1256
1674
  };
1257
1675
  }
1258
1676
  const program = ts.createProgram({
@@ -1274,12 +1692,30 @@ export function comparePropTables(tables, options) {
1274
1692
  project.options,
1275
1693
  isOwnFile,
1276
1694
  );
1695
+ // Without strict null checks the compiler drops `null` and `undefined`.
1696
+ // Whether they are on by default depends on the TypeScript version.
1697
+ const compilerOptions = program.getCompilerOptions();
1698
+ const nullable = !(typeof ts.getStrictOptionValue === 'function'
1699
+ ? ts.getStrictOptionValue(compilerOptions, 'strictNullChecks')
1700
+ : (compilerOptions.strictNullChecks ?? compilerOptions.strict ?? false));
1701
+ const reading = {
1702
+ nullable,
1703
+ // An optional prop takes `undefined` only when the project lets it.
1704
+ exactOptional:
1705
+ !nullable && compilerOptions.exactOptionalPropertyTypes === true,
1706
+ isDefaultLibrary: (/** @type {any} */ file) =>
1707
+ program.isSourceFileDefaultLibrary(file),
1708
+ };
1277
1709
 
1278
1710
  /** @type {PropDriftFinding[]} */
1279
1711
  const findings = [];
1280
1712
  /** @type {Array<PropDriftUnchecked & {props: string[]}>} */
1281
1713
  const propsNotChecked = [];
1282
1714
  let compared = 0;
1715
+ let typesCompared = 0;
1716
+ /** @type {PropDriftTypesNotCompared[]} */
1717
+ const typesNotCompared = [];
1718
+ const typesSkipped = typeTextTooOld(ts);
1283
1719
  for (const table of tables) {
1284
1720
  /** @type {ReturnType<typeof readCodeProps> | null} */
1285
1721
  let read = null;
@@ -1314,14 +1750,41 @@ export function comparePropTables(tables, options) {
1314
1750
  compared++;
1315
1751
  const source = rel(sourcePath);
1316
1752
  const documented = new Set();
1753
+ /** @type {Map<string, Set<string>>} */
1754
+ const notCompared = new Map();
1755
+ /** @type {(reason: string, name: string) => void} */
1756
+ const skip = (reason, name) => {
1757
+ notCompared.set(reason, (notCompared.get(reason) ?? new Set()).add(name));
1758
+ };
1317
1759
  for (const raw of table.props) {
1318
1760
  if (!raw || typeof raw.name !== 'string') continue;
1319
1761
  const entries = documentedProps(raw);
1320
1762
  for (const entry of entries) {
1321
1763
  const propName = entry.name;
1322
1764
  if (entry.spread) continue;
1765
+ const docType = entry.type;
1766
+ if (typesSkipped) entry.type = undefined;
1323
1767
  documented.add(propName);
1324
1768
  const code = read.props.get(propName);
1769
+ const reached =
1770
+ code && entry.path.length > 0
1771
+ ? fieldTypes(ts, checker, code, entry.path)
1772
+ : null;
1773
+ if (
1774
+ entry.type === undefined &&
1775
+ !typesSkipped &&
1776
+ typeof raw.type === 'string' &&
1777
+ (code && entry.path.length > 0
1778
+ ? reached
1779
+ : code || read.accepts(propName).length > 0)
1780
+ ) {
1781
+ // `a / b` with one type that does not split across the names (a
1782
+ // field only once it is found).
1783
+ skip(
1784
+ code && !code.own ? outside(propName, code) : NOT_EXACT,
1785
+ entry.written,
1786
+ );
1787
+ }
1325
1788
  const base = {
1326
1789
  component: table.name,
1327
1790
  prop: entry.written,
@@ -1330,11 +1793,74 @@ export function comparePropTables(tables, options) {
1330
1793
  source,
1331
1794
  };
1332
1795
  if (entry.path.length > 0) {
1333
- if (
1334
- code
1335
- ? fieldExists(ts, checker, code, entry.path)
1336
- : read.accepts(propName)
1337
- ) {
1796
+ // Listed only once the field is found; a missing one is reported.
1797
+ if (code && !code.own && reached && typeof entry.type === 'string') {
1798
+ skip(outside(propName, code), entry.written);
1799
+ }
1800
+ if (reached === 'any') {
1801
+ // A field of an `any` or `unknown` prop has no type to judge.
1802
+ if (code?.own && typeof entry.type === 'string') {
1803
+ skip(NOT_EXACT, entry.written);
1804
+ }
1805
+ continue;
1806
+ }
1807
+ if (code && reached) {
1808
+ const fieldText = fieldTypeText(
1809
+ ts,
1810
+ checker,
1811
+ reached,
1812
+ reading.exactOptional,
1813
+ code.location,
1814
+ );
1815
+ const ctx = {
1816
+ ts,
1817
+ checker,
1818
+ location: code.location,
1819
+ // A name an index signature takes sets the index's
1820
+ // `undefined` aside, as a prop of that name does.
1821
+ optional:
1822
+ reached.optional && (!reading.exactOptional || reached.indexed),
1823
+ ...reading,
1824
+ written: [fieldText],
1825
+ };
1826
+ const verdict =
1827
+ code.own && typeof entry.type === 'string'
1828
+ ? reached.ambiguous
1829
+ ? 'not_compared'
1830
+ : judgeType(entry.type, reached.types, ctx)
1831
+ : null;
1832
+ // The field takes every variant's type: the types of only some
1833
+ // variants are neither its type nor wrong. Past six, the sets of
1834
+ // them are not tried, and the type is never reported.
1835
+ const variants = [...new Set(reached.types)];
1836
+ const someVariants =
1837
+ (verdict === 'mismatch' || verdict === 'not_compared') &&
1838
+ !reached.ambiguous &&
1839
+ variants.length > 1 &&
1840
+ (variants.length > 6 ||
1841
+ Array.from({length: 2 ** variants.length - 2}, (_, i) =>
1842
+ variants.filter((__, j) => ((i + 1) >> j) & 1),
1843
+ ).some(
1844
+ some => judgeType(entry.type ?? '', some, ctx) === 'match',
1845
+ ));
1846
+ if (someVariants) skip(VARIANTS, entry.written);
1847
+ else if (verdict === 'not_compared') skip(NOT_EXACT, entry.written);
1848
+ else if (verdict) typesCompared++;
1849
+ if (verdict === 'mismatch' && !someVariants) {
1850
+ const codeType = fieldText.join(' | ');
1851
+ findings.push({
1852
+ ...base,
1853
+ kind: 'type_mismatch',
1854
+ message: `"${entry.written}" is documented as ${entry.type}, but the code declares ${codeType}.`,
1855
+ docType: entry.type,
1856
+ codeType,
1857
+ });
1858
+ }
1859
+ continue;
1860
+ }
1861
+ if (!code && read.accepts(propName).length > 0) {
1862
+ // A field of a name only an index signature takes.
1863
+ if (typeof entry.type === 'string') skip(NOT_EXACT, entry.written);
1338
1864
  continue;
1339
1865
  }
1340
1866
  findings.push({
@@ -1347,11 +1873,70 @@ export function comparePropTables(tables, options) {
1347
1873
  continue;
1348
1874
  }
1349
1875
  if (!code) {
1350
- if (read.accepts(propName)) continue;
1876
+ // A name an index signature takes is a prop of the index's type;
1877
+ // TypeScript holds it to every signature that takes it, so with
1878
+ // several its type is not compared. Another package's signature is
1879
+ // not the package's to judge, and a mapped type's (`Record`) has no
1880
+ // declaration to say whose it is.
1881
+ const indexes = read.accepts(propName);
1882
+ const own = indexes.filter(
1883
+ (/** @type {any} */ i) =>
1884
+ i.declaration &&
1885
+ isOwnFile(i.declaration.getSourceFile().fileName),
1886
+ );
1887
+ const index = indexes.length === 1 ? own[0] : null;
1888
+ if (indexes.length > 0 && !index && typeof entry.type === 'string') {
1889
+ const declared = indexes.every(
1890
+ (/** @type {any} */ i) => i.declaration,
1891
+ );
1892
+ skip(
1893
+ own.length || !declared ? NOT_EXACT : outside(propName),
1894
+ propName,
1895
+ );
1896
+ }
1897
+ if (index && typeof entry.type === 'string') {
1898
+ const verdict = judgeType(entry.type, [index.type], {
1899
+ ts,
1900
+ checker,
1901
+ location: index.declaration,
1902
+ // An index signature takes `undefined` only when its type does.
1903
+ optional: takesUndefined(ts, index.type),
1904
+ ...reading,
1905
+ // Printed where the index is declared, as the doc's names
1906
+ // resolve there.
1907
+ written: [
1908
+ [
1909
+ checker.typeToString(
1910
+ index.type,
1911
+ index.declaration,
1912
+ ts.TypeFormatFlags.UseSingleQuotesForStringLiteralType,
1913
+ ),
1914
+ ],
1915
+ ],
1916
+ });
1917
+ if (verdict === 'not_compared') skip(NOT_EXACT, propName);
1918
+ else typesCompared++;
1919
+ if (verdict === 'mismatch') {
1920
+ const codeType = checker.typeToString(
1921
+ index.type,
1922
+ index.declaration,
1923
+ ts.TypeFormatFlags.UseSingleQuotesForStringLiteralType,
1924
+ );
1925
+ findings.push({
1926
+ ...base,
1927
+ kind: 'type_mismatch',
1928
+ message: `"${propName}" is documented as ${entry.type}, but ${table.name}'s props take it as ${codeType}.`,
1929
+ docType: entry.type,
1930
+ codeType,
1931
+ });
1932
+ }
1933
+ }
1934
+ if (indexes.length > 0) continue;
1351
1935
  findings.push({
1352
1936
  ...base,
1353
1937
  kind: 'not_in_code',
1354
1938
  message: `"${propName}" is documented, but ${table.name}'s props have no "${propName}".`,
1939
+ ...(typeof docType === 'string' ? {docType} : {}),
1355
1940
  });
1356
1941
  continue;
1357
1942
  }
@@ -1363,8 +1948,46 @@ export function comparePropTables(tables, options) {
1363
1948
  message: code.required
1364
1949
  ? `"${propName}" is required in the code, but the doc does not mark it required.`
1365
1950
  : `"${propName}" is documented as required, but it is optional in the code.`,
1951
+ docRequired,
1952
+ codeRequired: code.required,
1366
1953
  });
1367
1954
  }
1955
+ if (!code.own && typeof entry.type === 'string') {
1956
+ skip(outside(propName, code), propName);
1957
+ }
1958
+ if (code.own && typeof entry.type === 'string') {
1959
+ const verdict = judgeProp(
1960
+ ts,
1961
+ checker,
1962
+ String(entry.type),
1963
+ code,
1964
+ reading,
1965
+ );
1966
+ if (verdict === 'not_compared') skip(NOT_EXACT, propName);
1967
+ else typesCompared++;
1968
+ if (verdict === 'mismatch') {
1969
+ const keep = ownUndefined(checker, code, reading.exactOptional);
1970
+ const codeType = codeTypeText(code, keep);
1971
+ const expanded = rendered(code, 1, keep);
1972
+ // The optional prop's `undefined` the expansion sets aside is no
1973
+ // difference to show.
1974
+ const bare = codeType
1975
+ .split(' | ')
1976
+ .filter(part => part !== 'undefined')
1977
+ .join(' | ');
1978
+ const shown =
1979
+ expanded && expanded !== codeType && expanded !== bare
1980
+ ? `${codeType} (${expanded.length <= 400 ? expanded : `${expanded.slice(0, 400)}…`})`
1981
+ : codeType;
1982
+ findings.push({
1983
+ ...base,
1984
+ kind: 'type_mismatch',
1985
+ message: `"${propName}" is documented as ${entry.type}, but the code declares ${shown}.`,
1986
+ docType: entry.type,
1987
+ codeType,
1988
+ });
1989
+ }
1990
+ }
1368
1991
  }
1369
1992
  }
1370
1993
  /** @type {string[]} */
@@ -1381,6 +2004,10 @@ export function comparePropTables(tables, options) {
1381
2004
  if (code.untraced) untraced.push(code.name);
1382
2005
  continue;
1383
2006
  }
2007
+ const codeType = codeTypeText(
2008
+ code,
2009
+ ownUndefined(checker, code, reading.exactOptional),
2010
+ );
1384
2011
  findings.push({
1385
2012
  component: table.name,
1386
2013
  prop: code.name,
@@ -1389,6 +2016,8 @@ export function comparePropTables(tables, options) {
1389
2016
  message: `${table.name} declares "${code.name}", but the doc does not list it.`,
1390
2017
  doc: table.doc,
1391
2018
  source,
2019
+ codeType,
2020
+ codeRequired: code.required,
1392
2021
  });
1393
2022
  }
1394
2023
  if (untraced.length > 0) {
@@ -1399,6 +2028,14 @@ export function comparePropTables(tables, options) {
1399
2028
  props: untraced.sort(),
1400
2029
  });
1401
2030
  }
2031
+ for (const [reason, props] of notCompared) {
2032
+ typesNotCompared.push({
2033
+ component: table.name,
2034
+ doc: table.doc,
2035
+ reason,
2036
+ props: [...props].sort(),
2037
+ });
2038
+ }
1402
2039
  }
1403
2040
 
1404
2041
  // Field by field: collation ignores a separator such as NUL, which would
@@ -1410,12 +2047,217 @@ export function comparePropTables(tables, options) {
1410
2047
  a.kind.localeCompare(b.kind),
1411
2048
  );
1412
2049
  unchecked.sort((a, b) => a.component.localeCompare(b.component));
2050
+ typesNotCompared.sort(
2051
+ (a, b) =>
2052
+ a.component.localeCompare(b.component) ||
2053
+ a.reason.localeCompare(b.reason),
2054
+ );
1413
2055
  return {
1414
2056
  status: 'checked',
1415
2057
  typescript: version,
1416
2058
  components: compared,
2059
+ typesCompared,
1417
2060
  findings,
1418
2061
  unchecked,
2062
+ typesNotCompared,
2063
+ ...(typesSkipped ? {typesSkipped} : {}),
1419
2064
  ...(propsNotChecked.length > 0 ? {propsNotChecked} : {}),
1420
2065
  };
1421
2066
  }
2067
+
2068
+ /**
2069
+ * The prop tables one doc file documents, each with the export it describes.
2070
+ * A multi-component doc names each entry's export; an entry without `props`
2071
+ * (a reference to a sibling doc) and a hook entry are not prop tables.
2072
+ * @param {any} doc
2073
+ * @param {PropDriftComponent} component
2074
+ * @returns {Array<{name: string, props: any[]}>}
2075
+ */
2076
+ function propTables(doc, component) {
2077
+ if (!doc || typeof doc !== 'object') return [];
2078
+ /** @type {Array<{name: string, props: any[]}>} */
2079
+ const tables = [];
2080
+ // A doc's own props document the export its `name` names, which can be
2081
+ // another file's export than the one its file is named after.
2082
+ const own = ownName(doc, component);
2083
+ if (Array.isArray(doc.props) && !HOOK_NAME.test(own)) {
2084
+ tables.push({name: own, props: doc.props});
2085
+ }
2086
+ if (Array.isArray(doc.components)) {
2087
+ for (const entry of doc.components) {
2088
+ if (
2089
+ entry &&
2090
+ typeof entry.name === 'string' &&
2091
+ Array.isArray(entry.props) &&
2092
+ !(Array.isArray(doc.props) && entry.name === own) &&
2093
+ !HOOK_NAME.test(entry.name)
2094
+ ) {
2095
+ tables.push({name: entry.name, props: entry.props});
2096
+ }
2097
+ }
2098
+ }
2099
+ return tables;
2100
+ }
2101
+
2102
+ /**
2103
+ * The export a doc's own props document: its `name`, else its record's.
2104
+ * @param {any} doc
2105
+ * @param {PropDriftComponent} component
2106
+ */
2107
+ function ownName(doc, component) {
2108
+ return typeof doc.name === 'string' && doc.name !== ''
2109
+ ? doc.name
2110
+ : component.name;
2111
+ }
2112
+
2113
+ /**
2114
+ * The stem of a `<stem>.doc.{ts,mjs,js}` file: the name of the record that
2115
+ * owns the doc.
2116
+ * @param {string} docPath
2117
+ */
2118
+ function docStem(docPath) {
2119
+ return path.basename(docPath).replace(/\.doc\.(ts|mjs|js)$/, '');
2120
+ }
2121
+
2122
+ /**
2123
+ * Where an entry's export lives: `<entry>.tsx` beside the doc, else the doc's
2124
+ * own same-stem source, else the directory's `index.ts`/`index.tsx`, else the
2125
+ * `.tsx` in the doc's folder that declares it, as discovery finds a member's.
2126
+ * @param {string} name
2127
+ * @param {PropDriftComponent} component
2128
+ * @returns {string[]} candidates, in order
2129
+ */
2130
+ function sourcesForEntry(name, component) {
2131
+ const own = component.sourcePath ? [component.sourcePath] : [];
2132
+ if (name === component.name) return own;
2133
+ const dir = path.dirname(component.docPath);
2134
+ return [
2135
+ path.join(dir, `${name}.tsx`),
2136
+ ...own,
2137
+ path.join(dir, 'index.ts'),
2138
+ path.join(dir, 'index.tsx'),
2139
+ folderSourceFor(dir, name),
2140
+ ].filter(
2141
+ /** @type {(file: string | null, i: number, all: Array<string | null>) => file is string} */ (
2142
+ (file, i, all) =>
2143
+ file != null && all.indexOf(file) === i && fs.existsSync(file)
2144
+ ),
2145
+ );
2146
+ }
2147
+
2148
+ /**
2149
+ * Compare documented props with each component's TypeScript props: find each
2150
+ * doc's prop tables, then compare them (`comparePropTables`).
2151
+ * @param {PropDriftComponent[]} components
2152
+ * @param {{packageDir: string, resolveFrom: Array<string | null | undefined>, loadDoc?: (docPath: string) => Promise<any>}} options
2153
+ * @returns {Promise<PropDriftReport>}
2154
+ */
2155
+ export async function comparePropDocs(components, options) {
2156
+ const {packageDir, resolveFrom, loadDoc = loadComponentDoc} = options;
2157
+ const rel = (/** @type {string} */ file) =>
2158
+ path.relative(packageDir, file).split(path.sep).join('/');
2159
+
2160
+ /** @type {PropDriftTable[]} */
2161
+ const tables = [];
2162
+ /** @type {PropDriftUnchecked[]} */
2163
+ const unchecked = [];
2164
+ const seen = new Set();
2165
+ // A family doc can stand behind several records: its own, named after the
2166
+ // doc, and one per member entry that discovery records as a component of
2167
+ // its own. A member record compares only its own entry, against its own
2168
+ // source; the family record leaves those entries to the members.
2169
+ /** @type {Map<string, Set<string>>} */
2170
+ const namesByDoc = new Map();
2171
+ for (const component of components) {
2172
+ const names = namesByDoc.get(component.docPath) ?? new Set();
2173
+ names.add(component.name);
2174
+ namesByDoc.set(component.docPath, names);
2175
+ }
2176
+ /** @type {Map<string, Promise<any>>} */
2177
+ const docs = new Map();
2178
+ /** @type {Set<string>} docs whose own record is here */
2179
+ const owned = new Set();
2180
+ // Its own props, and every entry no member record compares: a hidden one,
2181
+ // or one discovery doesn't record. One whose source isn't found is listed
2182
+ // as not compared.
2183
+ const compareOwn = (
2184
+ /** @type {PropDriftComponent} */ component,
2185
+ /** @type {any} */ doc,
2186
+ ) => {
2187
+ const members = namesByDoc.get(component.docPath) ?? new Set();
2188
+ for (const table of propTables(doc, component)) {
2189
+ if (table.name !== component.name && members.has(table.name)) continue;
2190
+ const sources = sourcesForEntry(table.name, component);
2191
+ // One table per doc: two docs that each document a component both
2192
+ // get compared.
2193
+ const key = `${component.docPath}\0${sources[0]}\0${table.name}`;
2194
+ if (seen.has(key)) continue;
2195
+ seen.add(key);
2196
+ tables.push({doc: rel(component.docPath), ...table, sources});
2197
+ }
2198
+ };
2199
+ for (const component of components) {
2200
+ let doc;
2201
+ try {
2202
+ let loading = docs.get(component.docPath);
2203
+ if (!loading) {
2204
+ loading = Promise.resolve(loadDoc(component.docPath));
2205
+ docs.set(component.docPath, loading);
2206
+ }
2207
+ doc = await loading;
2208
+ } catch (err) {
2209
+ unchecked.push({
2210
+ component: component.name,
2211
+ doc: rel(component.docPath),
2212
+ reason: `its doc does not load: ${err instanceof Error ? err.message : String(err)}`,
2213
+ });
2214
+ continue;
2215
+ }
2216
+ // The doc's own record is named after its file, or after the export its
2217
+ // `name` names; any other record is a family member's.
2218
+ const ownRecord =
2219
+ component.name === docStem(component.docPath) ||
2220
+ (doc != null &&
2221
+ typeof doc === 'object' &&
2222
+ ownName(doc, component) === component.name);
2223
+ if (!ownRecord) {
2224
+ // A family member compares its own entry.
2225
+ const entry = Array.isArray(doc?.components)
2226
+ ? doc.components.find(
2227
+ (/** @type {any} */ candidate) =>
2228
+ candidate?.name === component.name &&
2229
+ Array.isArray(candidate.props),
2230
+ )
2231
+ : undefined;
2232
+ if (!entry || HOOK_NAME.test(component.name)) continue;
2233
+ const key = `${component.docPath}\0${component.sourcePath}\0${component.name}`;
2234
+ if (seen.has(key)) continue;
2235
+ seen.add(key);
2236
+ tables.push({
2237
+ doc: rel(component.docPath),
2238
+ name: component.name,
2239
+ props: entry.props,
2240
+ sources: component.sourcePath ? [component.sourcePath] : [],
2241
+ });
2242
+ continue;
2243
+ }
2244
+ owned.add(component.docPath);
2245
+ compareOwn(component, doc);
2246
+ }
2247
+ // A family doc whose own record is missing, because its same-stem source
2248
+ // is, still has its own props and unrecorded entries compared or listed.
2249
+ for (const [docPath, loading] of docs) {
2250
+ if (owned.has(docPath)) continue;
2251
+ const doc = await loading.catch(() => null);
2252
+ if (doc == null || typeof doc !== 'object') continue;
2253
+ compareOwn({name: docStem(docPath), docPath, sourcePath: null}, doc);
2254
+ }
2255
+ const report = comparePropTables(tables, {packageDir, resolveFrom});
2256
+ if (report.status === 'skipped') return report;
2257
+ return {
2258
+ ...report,
2259
+ unchecked: [...unchecked, ...report.unchecked].sort((a, b) =>
2260
+ a.component.localeCompare(b.component),
2261
+ ),
2262
+ };
2263
+ }