@astryxdesign/cli 0.7.16 → 0.7.17-canary.g16fe6a8
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 +76 -86
- package/api/build/_adapter.d.mts +12 -0
- package/api/build/_adapter.mjs +51 -34
- package/api/build/blocks/blocks.d.mts +7 -5
- package/api/build/blocks/blocks.mjs +113 -26
- package/api/build/build.doc.mjs +4 -3
- package/api/build/build.type.d.mts +2 -2
- package/api/build/build.type.mjs +3 -3
- package/api/build/kit/kind.d.mts +2 -1
- package/api/build/kit/kind.mjs +199 -10
- package/api/build/kit/kit.mjs +6 -4
- package/api/build/kit/rank.d.mts +4 -2
- package/api/build/kit/rank.mjs +125 -24
- package/api/build/kit/start.d.mts +5 -4
- package/api/build/kit/start.mjs +12 -8
- package/api/build/kit/weights.d.mts +39 -3
- package/api/build/kit/weights.json +1 -1
- package/api/build/kit/weights.mjs +340 -43
- package/api/build/kit/word-model.d.mts +23 -0
- package/api/build/kit/word-model.mjs +46 -0
- package/api/build/kit/words.d.mts +48 -0
- package/api/build/kit/words.mjs +90 -1
- package/api/component/__fixtures__/resolutions/modules.txt +4 -5
- package/api/component/list/list.d.mts +1 -1
- package/api/component/list/list.mjs +48 -6
- package/api/discover/_adapter.d.mts +1 -1
- package/api/discover/search/search.mjs +3 -2
- package/api/docs/_adapter.d.mts +28 -2
- package/api/docs/_adapter.mjs +249 -10
- package/api/docs/docs.d.mts +3 -0
- package/api/docs/docs.doc.mjs +7 -1
- package/api/docs/docs.mjs +12 -0
- package/api/docs/docs.type.d.mts +5 -0
- package/api/docs/docs.type.mjs +2 -0
- package/api/docs/node/node.mjs +1 -1
- package/api/doctor/_adapter.d.mts +27 -5
- package/api/doctor/_adapter.mjs +83 -10
- package/api/doctor/built-checks.mjs +40 -14
- package/api/doctor/doctor.d.mts +4 -3
- package/api/doctor/doctor.mjs +14 -7
- package/api/doctor/theme-checks.mjs +121 -17
- package/api/icon/_records.mjs +2 -1
- package/api/index.d.mts +2 -0
- package/api/index.mjs +2 -0
- package/api/integration/_adapter.d.mts +44 -0
- package/api/integration/_adapter.mjs +77 -1
- package/api/integration/authoring-checks.mjs +6 -6
- package/api/integration/example-scope.d.mts +14 -0
- package/api/integration/example-scope.mjs +44 -0
- package/api/integration/example-scope.type.d.mts +29 -0
- package/api/integration/example-scope.type.mjs +25 -0
- package/api/integration/integrationExampleScope.doc.d.mts +11 -0
- package/api/integration/integrationExampleScope.doc.mjs +59 -0
- package/api/integration/validate-integration.mjs +19 -1
- package/api/integration/validate-integration.type.d.mts +9 -0
- package/api/integration/validate-integration.type.mjs +1 -1
- package/api/integration/validateIntegration.doc.mjs +1 -1
- package/api/render/_adapter.mjs +591 -195
- package/api/render/render.doc.mjs +11 -3
- package/api/render/render.type.d.mts +52 -0
- package/api/render/render.type.mjs +31 -1
- package/api/search/ORDERING.md +1 -1
- package/api/search/_adapter.d.mts +9 -0
- package/api/search/_adapter.mjs +85 -0
- package/api/search/_candidate-score.mjs +51 -32
- package/api/search/_fit-order.mjs +3 -3
- package/api/search/_gather-components.d.mts +3 -2
- package/api/search/_gather-components.mjs +12 -3
- package/api/search/_lexical.d.mts +5 -0
- package/api/search/_lexical.mjs +8 -2
- package/api/search/_result.d.mts +11 -9
- package/api/search/_result.mjs +4 -4
- package/api/search/_score.d.mts +6 -1
- package/api/search/_score.mjs +15 -3
- package/api/search/search-index.mjs +7 -1
- package/api/search/search.d.mts +1 -1
- package/api/theme/_adapter.mjs +12 -1
- package/api/theme/build/build.d.mts +132 -1
- package/api/theme/build/build.mjs +482 -157
- package/api/theme/build/core-interception.d.mts +10 -0
- package/api/theme/build/core-interception.mjs +10 -0
- package/api/theme/build/font-warning.d.mts +44 -5
- package/api/theme/build/font-warning.mjs +118 -10
- package/api/theme/build/icon-imports.mjs +51 -9
- package/api/theme/theme.type.d.mts +10 -0
- package/api/theme/theme.type.mjs +7 -1
- package/api/theme-edit/_adapter.d.mts +115 -4
- package/api/theme-edit/_adapter.mjs +997 -83
- package/api/theme-edit/_catalog.d.mts +20 -2
- package/api/theme-edit/_catalog.mjs +50 -10
- package/api/theme-edit/_check.d.mts +75 -24
- package/api/theme-edit/_check.mjs +2672 -422
- package/api/theme-edit/_color.d.mts +10 -0
- package/api/theme-edit/_color.mjs +54 -1
- package/api/theme-edit/_compiler.d.mts +1 -0
- package/api/theme-edit/_compiler.mjs +133 -15
- package/api/theme-edit/_context.d.mts +14 -3
- package/api/theme-edit/_context.mjs +225 -65
- package/api/theme-edit/_css.d.mts +14 -0
- package/api/theme-edit/_css.mjs +46 -1
- package/api/theme-edit/_edit.d.mts +54 -4
- package/api/theme-edit/_edit.mjs +2966 -286
- package/api/theme-edit/_how-commands.d.mts +72 -1
- package/api/theme-edit/_how-commands.mjs +770 -9
- package/api/theme-edit/_how-question.d.mts +82 -5
- package/api/theme-edit/_how-question.mjs +6983 -701
- package/api/theme-edit/_how.d.mts +2 -1
- package/api/theme-edit/_how.mjs +237 -41
- package/api/theme-edit/_importers.d.mts +38 -0
- package/api/theme-edit/_importers.mjs +188 -0
- package/api/theme-edit/_marks.d.mts +31 -5
- package/api/theme-edit/_marks.mjs +57 -5
- package/api/theme-edit/_name-rule.d.mts +11 -0
- package/api/theme-edit/_name-rule.mjs +12 -0
- package/api/theme-edit/_normal.d.mts +303 -0
- package/api/theme-edit/_normal.mjs +1517 -0
- package/api/theme-edit/_palette.d.mts +12 -0
- package/api/theme-edit/_palette.mjs +37 -4
- package/api/theme-edit/_part-words.d.mts +31 -0
- package/api/theme-edit/_part-words.mjs +126 -0
- package/api/theme-edit/_parts.d.mts +28 -2
- package/api/theme-edit/_parts.mjs +267 -31
- package/api/theme-edit/_preview-app.d.mts +7 -13
- package/api/theme-edit/_preview-app.mjs +814 -406
- package/api/theme-edit/_preview.d.mts +116 -16
- package/api/theme-edit/_preview.mjs +649 -152
- package/api/theme-edit/_project.d.mts +22 -1
- package/api/theme-edit/_project.mjs +83 -3
- package/api/theme-edit/_settings.d.mts +467 -42
- package/api/theme-edit/_settings.mjs +3527 -468
- package/api/theme-edit/_tables.d.mts +21 -0
- package/api/theme-edit/_tables.mjs +97 -3
- package/api/theme-edit/_vet.d.mts +31 -0
- package/api/theme-edit/_vet.mjs +114 -8
- package/api/theme-edit/theme-edit.type.d.mts +16 -2
- package/api/theme-edit/theme-edit.type.mjs +6 -3
- package/api/theme-edit/themeEditHow.doc.mjs +1 -1
- package/api/theme-edit/themeEditNew.doc.d.mts +0 -1
- package/api/theme-edit/themeEditNew.doc.mjs +7 -4
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +3 -3
- package/api/theme-edit/themeEditPreviewRemove.doc.mjs +2 -2
- package/api/upgrade/run/run.mjs +4 -3
- package/assets/codemods/integration-discovery.mjs +2 -1
- package/assets/codemods/run-codemod.mjs +87 -7
- package/assets/codemods/transforms/next/index.mjs +11 -1
- package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
- package/assets/component-list.generated.json +1 -0
- package/assets/docs/author-a-theme.doc.mjs +1 -0
- package/assets/docs/theme.doc.dense.mjs +2 -2
- package/assets/docs/theme.doc.mjs +1 -1
- package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
- package/assets/docs/tree/checks.doc.mjs +1 -1
- package/assets/docs/tree/cli-command.doc.mjs +2 -3
- package/assets/docs/tree/render-pages.doc.mjs +44 -3
- package/assets/docs/tree/share-a-page.doc.mjs +157 -0
- package/assets/docs/tree/single-component.doc.mjs +4 -0
- package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
- package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
- package/assets/docs/use-a-theme.doc.mjs +1 -1
- package/assets/docs.generated.json +2 -2
- package/assets/search-catalog.generated.json +2 -0
- package/assets/self-audit.generated.json +1 -1
- package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
- package/assets/templates/blocks/components/Hooks/useContainerRevealHookUsage.tsx +2 -2
- package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
- package/assets/templates/components.generated.json +1 -1
- package/assets/templates/pages/ai-chat/template.doc.mjs +5 -4
- package/assets/templates/pages/ai-chat-landing/template.doc.mjs +5 -1
- package/assets/templates/pages/blank/template.doc.mjs +1 -1
- package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
- package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
- package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
- package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
- package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
- package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
- package/assets/templates/pages/documentation/template.doc.mjs +4 -1
- package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
- package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
- package/assets/templates/pages/editor/template.doc.mjs +2 -8
- package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
- package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
- package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
- package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
- package/assets/templates/pages/ide/page.tsx +24 -2
- package/assets/templates/pages/kanban-board/template.doc.mjs +1 -9
- package/assets/templates/pages/library/template.doc.mjs +4 -1
- package/assets/templates/pages/login/template.doc.mjs +2 -1
- package/assets/templates/pages/login-card/template.doc.mjs +2 -1
- package/assets/templates/pages/login-split/template.doc.mjs +2 -1
- package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
- package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
- package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
- package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
- package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
- package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
- package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
- package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
- package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
- package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
- package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
- package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
- package/assets/templates/pages/table-page/template.doc.mjs +5 -4
- package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
- package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -9
- package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
- package/assets/theme.template.ts +6 -0
- package/authoring/debug/parse.d.mts +3 -3
- package/authoring/doctypes/_schema.d.mts +3 -3
- package/authoring/doctypes/command/command.doc.mjs +1 -1
- package/authoring/doctypes/component/categories.d.mts +37 -0
- package/authoring/doctypes/component/categories.mjs +54 -0
- package/authoring/doctypes/template/template.doc.mjs +1 -1
- package/authoring/doctypes/template/type.ts +9 -0
- package/authoring/example/example.doc.d.mts +12 -0
- package/authoring/example/example.doc.mjs +69 -0
- package/authoring/example/preview.d.mts +92 -0
- package/authoring/example/preview.mjs +994 -0
- package/authoring/example/type.ts +102 -0
- package/authoring/index.d.mts +1 -0
- package/authoring/index.d.ts +12 -0
- package/authoring/index.mjs +3 -1
- package/clients/cli/bin/astryx.mjs +6 -9
- package/clients/cli/build-id.generated.json +1 -1
- package/clients/cli/commands/component.doc.mjs +1 -1
- package/clients/cli/commands/component.mjs +22 -3
- package/clients/cli/commands/docs.doc.mjs +7 -1
- package/clients/cli/commands/docs.mjs +3 -1
- package/clients/cli/commands/doctor.mjs +32 -14
- package/clients/cli/commands/experimental/render.mjs +42 -1
- package/clients/cli/commands/experimental/theme.mjs +573 -119
- package/clients/cli/commands/experimental-render.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
- package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +87 -1
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +10 -3
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-preview.doc.mjs +2 -1
- package/clients/cli/commands/experimental-theme.doc.mjs +5 -1
- package/clients/cli/commands/search.doc.mjs +2 -1
- package/clients/cli/commands/search.mjs +6 -1
- package/clients/cli/commands/template.mjs +4 -2
- package/clients/cli/commands/theme-build.doc.mjs +5 -1
- package/clients/cli/commands/theme.doc.mjs +1 -1
- package/clients/cli/commands/theme.mjs +718 -438
- package/clients/cli/commands/upgrade.mjs +42 -7
- package/clients/cli/index.mjs +1 -1
- package/clients/cli/lib/cli-error.mjs +2 -1
- package/clients/cli/lib/component-format.mjs +205 -98
- package/clients/cli/lib/hook-format.mjs +9 -19
- package/clients/cli/lib/json-shim.mjs +132 -24
- package/clients/cli/lib/manifest.mjs +3 -2
- package/clients/cli/manifest.generated.json +1 -1
- package/foundation/agent-docs/agent-docs.mjs +1 -1
- package/foundation/config/theme-state.d.mts +13 -1
- package/foundation/config/theme-state.mjs +47 -7
- package/foundation/discovery/authoring-self-docs.mjs +1 -0
- package/foundation/discovery/component-discovery.d.mts +11 -0
- package/foundation/discovery/component-discovery.mjs +56 -8
- package/foundation/discovery/component-replacement.mjs +130 -1
- package/foundation/discovery/copied-passages.mjs +2 -1
- package/foundation/discovery/core-component-docs.d.mts +29 -0
- package/foundation/discovery/core-component-docs.mjs +83 -0
- package/foundation/discovery/doc-types.mjs +4 -3
- package/foundation/discovery/entry-name-files.d.mts +43 -0
- package/foundation/discovery/entry-name-files.mjs +264 -0
- package/foundation/discovery/hook-discovery.d.mts +8 -2
- package/foundation/discovery/hook-discovery.mjs +22 -8
- package/foundation/discovery/icon-definition-discovery.mjs +2 -1
- package/foundation/discovery/icon-set-checks.mjs +2 -1
- package/foundation/discovery/icon-set-discovery.mjs +2 -1
- package/foundation/discovery/integration-doc-files.mjs +2 -1
- package/foundation/discovery/package-exports.d.mts +10 -0
- package/foundation/discovery/package-exports.mjs +56 -3
- package/foundation/discovery/prop-drift.mjs +9 -9
- package/foundation/discovery/runner-allowlist.d.mts +1 -0
- package/foundation/discovery/runner-allowlist.mjs +2 -0
- package/foundation/discovery/stored-component-list.d.mts +43 -0
- package/foundation/discovery/stored-component-list.mjs +90 -0
- package/foundation/discovery/stored-search-catalog.d.mts +115 -0
- package/foundation/discovery/stored-search-catalog.mjs +297 -0
- package/foundation/discovery/stored-self-audit.d.mts +26 -10
- package/foundation/discovery/stored-self-audit.mjs +38 -18
- package/foundation/discovery/template-adapter.d.mts +53 -1
- package/foundation/discovery/template-adapter.mjs +176 -12
- package/foundation/discovery/template-icons.mjs +2 -1
- package/foundation/discovery/theme-discovery.mjs +31 -4
- package/foundation/discovery/theme-imports.d.mts +14 -0
- package/foundation/discovery/theme-imports.mjs +43 -1
- package/foundation/discovery/theming-targets.d.mts +6 -5
- package/foundation/discovery/theming-targets.mjs +31 -47
- package/foundation/doc-compiler/import.mjs +7 -1
- package/foundation/doc-compiler/inputs.mjs +3 -2
- package/foundation/doc-compiler/parse-readable.mjs +4 -1
- package/foundation/doc-compiler/stored-docs.d.mts +22 -0
- package/foundation/doc-compiler/stored-docs.mjs +49 -0
- package/foundation/env/semver.mjs +3 -1
- package/foundation/fonts/font-stylesheet.d.mts +38 -0
- package/foundation/fonts/font-stylesheet.mjs +141 -0
- package/foundation/fs/cache-eviction.d.mts +21 -0
- package/foundation/fs/cache-eviction.mjs +73 -0
- package/foundation/fs/exact-codec.d.mts +12 -2
- package/foundation/fs/exact-codec.mjs +23 -6
- package/foundation/fs/file-protection.mjs +4 -3
- package/foundation/fs/gather-cache.d.mts +62 -4
- package/foundation/fs/gather-cache.mjs +517 -91
- package/foundation/fs/jiti-cache.d.mts +19 -1
- package/foundation/fs/jiti-cache.mjs +130 -6
- package/foundation/fs/kept-tree.d.mts +60 -0
- package/foundation/fs/kept-tree.mjs +154 -0
- package/foundation/fs/listing-cache.d.mts +7 -0
- package/foundation/fs/listing-cache.mjs +10 -0
- package/foundation/fs/module-loader.d.mts +24 -2
- package/foundation/fs/module-loader.mjs +94 -21
- package/foundation/fs/paths.d.mts +18 -0
- package/foundation/fs/paths.mjs +15 -2
- package/foundation/fs/read-record.d.mts +9 -0
- package/foundation/fs/read-record.mjs +19 -11
- package/foundation/fs/stop-cleanup.d.mts +12 -0
- package/foundation/fs/stop-cleanup.mjs +66 -0
- package/foundation/identity/provider-identity.d.mts +6 -0
- package/foundation/identity/provider-identity.mjs +10 -1
- package/foundation/integrations/contribution-inventory.mjs +4 -3
- package/foundation/integrations/example-code.d.mts +63 -0
- package/foundation/integrations/example-code.mjs +416 -0
- package/foundation/integrations/public-imports.d.mts +9 -0
- package/foundation/integrations/public-imports.mjs +3 -2
- package/foundation/integrations/validate-contributions.mjs +17 -45
- package/foundation/render/embedded-files.d.mts +6 -0
- package/foundation/render/embedded-files.mjs +25 -5
- package/foundation/render/embedded-imports.d.mts +39 -7
- package/foundation/render/embedded-imports.mjs +66 -27
- package/foundation/render/file-header.d.mts +66 -0
- package/foundation/render/file-header.mjs +118 -0
- package/foundation/render/host-answer.d.mts +18 -0
- package/foundation/render/host-answer.mjs +46 -0
- package/foundation/render/host-exit.d.mts +1 -0
- package/foundation/render/host-exit.mjs +6 -1
- package/foundation/render/html-reader.d.mts +55 -2
- package/foundation/render/html-reader.mjs +145 -26
- package/foundation/render/markup-edits.d.mts +57 -0
- package/foundation/render/markup-edits.mjs +182 -0
- package/foundation/render/page-host.mjs +29 -12
- package/foundation/render/page-worker.mjs +121 -13
- package/foundation/render/picture-chrome.d.mts +4 -0
- package/foundation/render/picture-chrome.mjs +185 -0
- package/foundation/render/picture-playwright.d.mts +4 -0
- package/foundation/render/picture-playwright.mjs +34 -0
- package/foundation/render/rebuild.mjs +2 -1
- package/foundation/render/render-page.d.mts +35 -3
- package/foundation/render/render-page.mjs +641 -96
- package/foundation/render/source-block.d.mts +85 -6
- package/foundation/render/source-block.mjs +211 -20
- package/foundation/render/ts-paths.d.mts +69 -6
- package/foundation/render/ts-paths.mjs +326 -40
- package/foundation/response/error-codes.d.mts +3 -1
- package/foundation/response/error-codes.d.ts +1 -0
- package/foundation/response/error-codes.doc.mjs +12 -1
- package/foundation/response/error-codes.mjs +11 -1
- package/foundation/response/response-types.doc.mjs +2 -2
- package/foundation/text/compare-text.d.mts +14 -0
- package/foundation/text/compare-text.mjs +68 -0
- package/foundation/text/css-escape.d.mts +18 -0
- package/foundation/text/css-escape.mjs +35 -0
- package/foundation/text/string-utils.d.mts +3 -2
- package/foundation/text/string-utils.mjs +18 -4
- package/foundation/text/suggest-order.d.mts +9 -0
- package/foundation/text/suggest-order.mjs +15 -1
- package/foundation/xle/expand.mjs +2 -1
- package/host/index.mjs +8 -6
- package/package.json +7 -6
|
@@ -0,0 +1,994 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Whether a doc example runs on its own, and the module that runs it.
|
|
5
|
+
* @input One doc example (`{code, kind?}`) and its ExampleScope: the names it
|
|
6
|
+
* may use without importing them, and the packages it may import.
|
|
7
|
+
* @output A PreviewableExample: the module a preview bundles, or why the
|
|
8
|
+
* example cannot preview.
|
|
9
|
+
* @position packages/cli/authoring/example — the one definition Doctor's
|
|
10
|
+
* example checks and a preview builder share, so they cannot disagree.
|
|
11
|
+
*
|
|
12
|
+
* A `demo` example runs on its own (`ExampleKind`). Here that means: the code
|
|
13
|
+
* compiles as a TSX module; it renders something (a default export, the last
|
|
14
|
+
* component it declares, or the JSX it ends with); it imports only what an app
|
|
15
|
+
* that installs the package can load in a browser; and every name it uses is
|
|
16
|
+
* defined in it, imported, given by its scope, or a JavaScript or browser
|
|
17
|
+
* global (the `globals` package's `builtin` and `browser` sets).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import {builtinModules, createRequire} from 'node:module';
|
|
21
|
+
|
|
22
|
+
const require = createRequire(import.meta.url);
|
|
23
|
+
// Resolved, not loaded: a missing package fails here, at import. Most runs
|
|
24
|
+
// never read an example, so the packages load on first use.
|
|
25
|
+
for (const pkg of [
|
|
26
|
+
'@babel/parser',
|
|
27
|
+
'@babel/core',
|
|
28
|
+
'@babel/plugin-transform-typescript',
|
|
29
|
+
'globals',
|
|
30
|
+
]) {
|
|
31
|
+
require.resolve(pkg);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** @type {typeof import('@babel/parser') | undefined} */
|
|
35
|
+
let parserModule;
|
|
36
|
+
/** @type {typeof import('@babel/core') | undefined} */
|
|
37
|
+
let coreModule;
|
|
38
|
+
/** @type {import('@babel/core').PluginItem | undefined} */
|
|
39
|
+
let typeScriptPlugin;
|
|
40
|
+
/** @type {Set<string> | undefined} */
|
|
41
|
+
let standardGlobals;
|
|
42
|
+
|
|
43
|
+
const parser = () => (parserModule ??= require('@babel/parser'));
|
|
44
|
+
const babel = () => (coreModule ??= require('@babel/core'));
|
|
45
|
+
const stripTypes = () =>
|
|
46
|
+
(typeScriptPlugin ??= require('@babel/plugin-transform-typescript').default);
|
|
47
|
+
|
|
48
|
+
/** The JavaScript and browser globals an example may use. */
|
|
49
|
+
const globalNames = () => {
|
|
50
|
+
if (standardGlobals) return standardGlobals;
|
|
51
|
+
/** @type {{builtin: Record<string, unknown>, browser: Record<string, unknown>}} */
|
|
52
|
+
const sets = require('globals');
|
|
53
|
+
standardGlobals = new Set([
|
|
54
|
+
...Object.keys(sets.builtin),
|
|
55
|
+
...Object.keys(sets.browser),
|
|
56
|
+
]);
|
|
57
|
+
return standardGlobals;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @typedef {import('./type.ts').ExampleScope} ExampleScope
|
|
62
|
+
* @typedef {import('./type.ts').ExampleProblem} ExampleProblem
|
|
63
|
+
* @typedef {import('./type.ts').PreviewableExample} PreviewableExample
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
/** @type {import('@babel/parser').ParserOptions} */
|
|
67
|
+
const PARSE_OPTIONS = {sourceType: 'module', plugins: ['jsx', 'typescript']};
|
|
68
|
+
|
|
69
|
+
/** Imports a browser cannot load, besides an uninstalled Node built-in. */
|
|
70
|
+
const SERVER_ONLY_IMPORT =
|
|
71
|
+
/^(?:server-only|next\/headers|next\/server|next\/cache|node:.+)$/u;
|
|
72
|
+
|
|
73
|
+
/** Packages whose names a scope provides, so an example may import them. */
|
|
74
|
+
const ALWAYS_IMPORTABLE = ['react', '@astryxdesign/core'];
|
|
75
|
+
|
|
76
|
+
/** The specifier the `React` namespace binds from. */
|
|
77
|
+
const REACT = 'react';
|
|
78
|
+
|
|
79
|
+
/** The package.json fields whose packages an app installs with a package. */
|
|
80
|
+
const INSTALLED_WITH = [
|
|
81
|
+
'dependencies',
|
|
82
|
+
'peerDependencies',
|
|
83
|
+
'optionalDependencies',
|
|
84
|
+
];
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The packages an app gets when it installs a package: those its package.json
|
|
88
|
+
* declares in `dependencies`, `peerDependencies` and `optionalDependencies`.
|
|
89
|
+
* `devDependencies` are installed only where the package is developed.
|
|
90
|
+
* @param {unknown} manifest a parsed package.json
|
|
91
|
+
* @returns {string[]}
|
|
92
|
+
*/
|
|
93
|
+
export function packagesInstalledWith(manifest) {
|
|
94
|
+
const fields = /** @type {Record<string, unknown>} */ (
|
|
95
|
+
manifest !== null && typeof manifest === 'object' ? manifest : {}
|
|
96
|
+
);
|
|
97
|
+
return INSTALLED_WITH.flatMap(field => {
|
|
98
|
+
const value = fields[field];
|
|
99
|
+
return value !== null && typeof value === 'object'
|
|
100
|
+
? Object.keys(value)
|
|
101
|
+
: [];
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Whether an import names code a browser cannot run: a server-only module,
|
|
107
|
+
* or a Node built-in the package does not install from npm under that name
|
|
108
|
+
* (an installed `buffer` or `events` is that package, which bundlers ship).
|
|
109
|
+
* @param {string} specifier
|
|
110
|
+
* @param {ReadonlyArray<string>} dependencies the packages the package declares
|
|
111
|
+
*/
|
|
112
|
+
export function isServerOnlyImport(specifier, dependencies) {
|
|
113
|
+
if (SERVER_ONLY_IMPORT.test(specifier)) return true;
|
|
114
|
+
return (
|
|
115
|
+
builtinModules.includes(specifier) && !dependencies.includes(specifier)
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Whether a member expression is `process.env`, the server's environment.
|
|
121
|
+
* @param {any} node a Babel MemberExpression
|
|
122
|
+
*/
|
|
123
|
+
export function isProcessEnv(node) {
|
|
124
|
+
const object = node?.object;
|
|
125
|
+
return (
|
|
126
|
+
object?.type === 'Identifier' &&
|
|
127
|
+
object.name === 'process' &&
|
|
128
|
+
!node.computed &&
|
|
129
|
+
node.property?.name === 'env'
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Whether a member expression is `process.env.NODE_ENV`, which bundlers
|
|
135
|
+
* replace, so it is not a read of the server's environment.
|
|
136
|
+
* @param {any} node a Babel MemberExpression
|
|
137
|
+
*/
|
|
138
|
+
export function isNodeEnvRead(node) {
|
|
139
|
+
return (
|
|
140
|
+
isProcessEnv(node?.object) &&
|
|
141
|
+
!node.computed &&
|
|
142
|
+
node.property?.name === 'NODE_ENV'
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @typedef {object} ScopeTable
|
|
148
|
+
* @property {Map<string, string>} names name → specifier, own > Core > React
|
|
149
|
+
* @property {Set<string>} importable packages an example may import
|
|
150
|
+
* @property {ReadonlyArray<string>} dependencies
|
|
151
|
+
* @property {Set<string>} serverOnly the package's own server-only specifiers
|
|
152
|
+
*/
|
|
153
|
+
|
|
154
|
+
/** @type {WeakMap<ExampleScope, ScopeTable>} */
|
|
155
|
+
const TABLES = new WeakMap();
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* @param {ExampleScope} scope
|
|
159
|
+
* @returns {ScopeTable}
|
|
160
|
+
*/
|
|
161
|
+
function scopeTable(scope) {
|
|
162
|
+
const cached = TABLES.get(scope);
|
|
163
|
+
if (cached) return cached;
|
|
164
|
+
/** @type {Map<string, string>} */
|
|
165
|
+
const names = new Map();
|
|
166
|
+
for (const list of [scope.packageNames ?? [], scope.coreNames ?? []]) {
|
|
167
|
+
for (const entry of list) {
|
|
168
|
+
if (!names.has(entry.name)) names.set(entry.name, entry.import);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
const reactNames = scope.reactNames ?? [];
|
|
172
|
+
for (const name of reactNames) {
|
|
173
|
+
if (name !== 'default' && !names.has(name)) names.set(name, REACT);
|
|
174
|
+
}
|
|
175
|
+
if (reactNames.length > 0 && !names.has('React')) names.set('React', REACT);
|
|
176
|
+
const dependencies = scope.dependencies ?? [];
|
|
177
|
+
const table = {
|
|
178
|
+
names,
|
|
179
|
+
importable: new Set([
|
|
180
|
+
scope.packageName,
|
|
181
|
+
...dependencies,
|
|
182
|
+
...ALWAYS_IMPORTABLE,
|
|
183
|
+
]),
|
|
184
|
+
dependencies,
|
|
185
|
+
serverOnly: new Set(scope.serverOnlyModules ?? []),
|
|
186
|
+
};
|
|
187
|
+
TABLES.set(scope, table);
|
|
188
|
+
return table;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The package a bare specifier names: `@scope/name` or `name`.
|
|
193
|
+
* @param {string} specifier
|
|
194
|
+
*/
|
|
195
|
+
function packageOf(specifier) {
|
|
196
|
+
const parts = specifier.split('/');
|
|
197
|
+
return specifier.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** @param {string} specifier */
|
|
201
|
+
function isBare(specifier) {
|
|
202
|
+
return !/^(?:\.{0,2}\/|[a-z][a-z0-9+.-]*:)/iu.test(specifier);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* @param {string} code
|
|
207
|
+
* @returns {{ast: any} | {error: any}}
|
|
208
|
+
*/
|
|
209
|
+
function tryParse(code) {
|
|
210
|
+
try {
|
|
211
|
+
return {ast: parser().parse(code, PARSE_OPTIONS)};
|
|
212
|
+
} catch (error) {
|
|
213
|
+
return {error};
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** @param {any} node */
|
|
218
|
+
function isJsxStatement(node) {
|
|
219
|
+
return (
|
|
220
|
+
node?.type === 'ExpressionStatement' &&
|
|
221
|
+
(node.expression.type === 'JSXElement' ||
|
|
222
|
+
node.expression.type === 'JSXFragment')
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** @param {any} node */
|
|
227
|
+
function isDefaultExport(node) {
|
|
228
|
+
return (
|
|
229
|
+
node.type === 'ExportDefaultDeclaration' ||
|
|
230
|
+
(node.type === 'ExportNamedDeclaration' &&
|
|
231
|
+
(node.specifiers ?? []).some(
|
|
232
|
+
(/** @type {any} */ s) =>
|
|
233
|
+
(s.exported?.name ?? s.exported?.value) === 'default',
|
|
234
|
+
))
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** A component name: a capital first letter and a lowercase letter. */
|
|
239
|
+
const COMPONENT_NAME = /^[A-Z](?=.*[a-z])/u;
|
|
240
|
+
|
|
241
|
+
/** The React calls that return a component made from the one they take. */
|
|
242
|
+
const COMPONENT_WRAPPERS = new Set(['memo', 'forwardRef', 'lazy']);
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Whether a call is `memo(…)`, `forwardRef(…)` or `lazy(…)`, bare or on
|
|
246
|
+
* `React`.
|
|
247
|
+
* @param {any} callee
|
|
248
|
+
*/
|
|
249
|
+
const isComponentWrapper = callee =>
|
|
250
|
+
(callee?.type === 'Identifier' && COMPONENT_WRAPPERS.has(callee.name)) ||
|
|
251
|
+
(callee?.type === 'MemberExpression' &&
|
|
252
|
+
!callee.computed &&
|
|
253
|
+
callee.object?.type === 'Identifier' &&
|
|
254
|
+
callee.object.name === 'React' &&
|
|
255
|
+
callee.property?.type === 'Identifier' &&
|
|
256
|
+
COMPONENT_WRAPPERS.has(callee.property.name));
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* The function a component is declared with: a function or class, or
|
|
260
|
+
* `memo`, `forwardRef` or `lazy` called on one, seen through parentheses and
|
|
261
|
+
* type assertions.
|
|
262
|
+
* @param {any} node an initializer
|
|
263
|
+
* @returns {boolean}
|
|
264
|
+
*/
|
|
265
|
+
function isComponentValue(node) {
|
|
266
|
+
let value = node;
|
|
267
|
+
while (
|
|
268
|
+
value?.type === 'TSAsExpression' ||
|
|
269
|
+
value?.type === 'TSSatisfiesExpression' ||
|
|
270
|
+
value?.type === 'TSNonNullExpression' ||
|
|
271
|
+
value?.type === 'ParenthesizedExpression'
|
|
272
|
+
) {
|
|
273
|
+
value = value.expression;
|
|
274
|
+
}
|
|
275
|
+
if (
|
|
276
|
+
value?.type === 'ArrowFunctionExpression' ||
|
|
277
|
+
value?.type === 'FunctionExpression' ||
|
|
278
|
+
value?.type === 'ClassExpression'
|
|
279
|
+
) {
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
return (
|
|
283
|
+
value?.type === 'CallExpression' &&
|
|
284
|
+
isComponentWrapper(value.callee) &&
|
|
285
|
+
isComponentValue(value.arguments[0])
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* The component a statement declares, if it declares one: a function or
|
|
291
|
+
* class with a component name, or a variable with a component name that holds
|
|
292
|
+
* one.
|
|
293
|
+
* @param {any} node
|
|
294
|
+
* @returns {string | null}
|
|
295
|
+
*/
|
|
296
|
+
function declaredComponent(node) {
|
|
297
|
+
const declaration =
|
|
298
|
+
node.type === 'ExportNamedDeclaration' ? node.declaration : node;
|
|
299
|
+
if (!declaration) return null;
|
|
300
|
+
if (
|
|
301
|
+
(declaration.type === 'FunctionDeclaration' ||
|
|
302
|
+
declaration.type === 'ClassDeclaration') &&
|
|
303
|
+
declaration.id &&
|
|
304
|
+
COMPONENT_NAME.test(declaration.id.name)
|
|
305
|
+
) {
|
|
306
|
+
return declaration.id.name;
|
|
307
|
+
}
|
|
308
|
+
if (declaration.type === 'VariableDeclaration') {
|
|
309
|
+
const [first] = declaration.declarations;
|
|
310
|
+
if (
|
|
311
|
+
declaration.declarations.length === 1 &&
|
|
312
|
+
first.id.type === 'Identifier' &&
|
|
313
|
+
COMPONENT_NAME.test(first.id.name) &&
|
|
314
|
+
isComponentValue(first.init)
|
|
315
|
+
) {
|
|
316
|
+
return first.id.name;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Whether a statement is a block that holds only comments, as a `{/* … *\/}`
|
|
324
|
+
* line between JSX elements parses at the top level.
|
|
325
|
+
* @param {any} node
|
|
326
|
+
*/
|
|
327
|
+
const isCommentBlock = node =>
|
|
328
|
+
node?.type === 'BlockStatement' &&
|
|
329
|
+
node.body.length === 0 &&
|
|
330
|
+
(node.directives ?? []).length === 0;
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Whether a statement is a block that holds one expression, as a `{…}` line
|
|
334
|
+
* such as `{items.map(…)}` between JSX elements parses at the top level.
|
|
335
|
+
* @param {any} node
|
|
336
|
+
*/
|
|
337
|
+
const isExpressionBlock = node =>
|
|
338
|
+
node?.type === 'BlockStatement' &&
|
|
339
|
+
node.body.length === 1 &&
|
|
340
|
+
node.body[0].type === 'ExpressionStatement' &&
|
|
341
|
+
(node.directives ?? []).length === 0;
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Whether a statement can be part of the JSX an example ends with: an
|
|
345
|
+
* element, or a `{…}` line between or after elements.
|
|
346
|
+
* @param {any} node
|
|
347
|
+
*/
|
|
348
|
+
const isMarkupStatement = node =>
|
|
349
|
+
isJsxStatement(node) || isCommentBlock(node) || isExpressionBlock(node);
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Where the JSX a list of statements ends with begins: the first of the
|
|
353
|
+
* trailing JSX statements and the `{…}` lines between or after them. The
|
|
354
|
+
* list's length when it does not end with JSX.
|
|
355
|
+
* @param {any[]} body
|
|
356
|
+
*/
|
|
357
|
+
function trailingMarkupStart(body) {
|
|
358
|
+
let start = body.length;
|
|
359
|
+
while (start > 0 && isMarkupStatement(body[start - 1])) start -= 1;
|
|
360
|
+
while (start < body.length && !isJsxStatement(body[start])) start += 1;
|
|
361
|
+
return start;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* A trailing JSX statement as a fragment child: the element, or a `{…}` line
|
|
366
|
+
* as it is written (`{/* … *\/}` and `{items.map(…)}` are JSX children too).
|
|
367
|
+
* @param {string} code
|
|
368
|
+
* @param {any} node
|
|
369
|
+
* @param {number} [offset] where `code` starts in the example's code
|
|
370
|
+
* @returns {Piece}
|
|
371
|
+
*/
|
|
372
|
+
function markupPiece(code, node, offset = 0) {
|
|
373
|
+
const part = isJsxStatement(node) ? node.expression : node;
|
|
374
|
+
return {text: code.slice(part.start, part.end), from: offset + part.start};
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* @typedef {object} ExampleModule
|
|
379
|
+
* @property {string} module
|
|
380
|
+
* @property {boolean} renders
|
|
381
|
+
* @property {boolean} [wrapped] whether the module runs the code inside a
|
|
382
|
+
* component it adds, so an error can come from that component
|
|
383
|
+
* @property {(position: number) => number | null} toExample the position in
|
|
384
|
+
* the example's code that a position in `module` comes from, or null for
|
|
385
|
+
* text the module adds around it
|
|
386
|
+
*/
|
|
387
|
+
|
|
388
|
+
/** @param {number} position */
|
|
389
|
+
const samePosition = position => position;
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* The module an example's code stands for, before its scope names are
|
|
393
|
+
* imported: the code itself when it default-exports what it renders; the
|
|
394
|
+
* code plus a default export of the last component it declares; or, for
|
|
395
|
+
* code that ends with JSX, a component whose body is the code before that
|
|
396
|
+
* JSX and which returns it (several sibling elements become one fragment).
|
|
397
|
+
* Directives, imports and re-exports stay at module scope; a declaration the
|
|
398
|
+
* code exports moves into the component without its `export`.
|
|
399
|
+
* @param {string} code
|
|
400
|
+
* @returns {ExampleModule | {error: any}}
|
|
401
|
+
*/
|
|
402
|
+
function moduleFor(code) {
|
|
403
|
+
const parsed = tryParse(code);
|
|
404
|
+
if ('ast' in parsed) {
|
|
405
|
+
const program = parsed.ast.program;
|
|
406
|
+
const body = /** @type {any[]} */ (program.body);
|
|
407
|
+
if (body.some(isDefaultExport)) {
|
|
408
|
+
return {module: code, renders: true, toExample: samePosition};
|
|
409
|
+
}
|
|
410
|
+
const markupFrom = trailingMarkupStart(body);
|
|
411
|
+
if (markupFrom < body.length) {
|
|
412
|
+
const markup = body
|
|
413
|
+
.slice(markupFrom)
|
|
414
|
+
.map(node => markupPiece(code, node));
|
|
415
|
+
return composed(
|
|
416
|
+
code,
|
|
417
|
+
program.directives,
|
|
418
|
+
body.slice(0, markupFrom),
|
|
419
|
+
markup.length === 1 ? markup : fragment(markup),
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
const component = body.map(declaredComponent).filter(Boolean).at(-1);
|
|
423
|
+
if (component) {
|
|
424
|
+
return {
|
|
425
|
+
module: `${code}\n\nexport default ${component};\n`,
|
|
426
|
+
renders: true,
|
|
427
|
+
toExample: samePosition,
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
return {module: code, renders: false, toExample: samePosition};
|
|
431
|
+
}
|
|
432
|
+
const siblings = trailingSiblings(code);
|
|
433
|
+
if (siblings) {
|
|
434
|
+
return composed(
|
|
435
|
+
code,
|
|
436
|
+
siblings.directives,
|
|
437
|
+
siblings.statements,
|
|
438
|
+
fragment(siblings.elements),
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
return {error: parsed.error};
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Code that ends with sibling elements, split where the parser says one
|
|
446
|
+
* element ends and the next begins: the statements before them, and each
|
|
447
|
+
* element, in order, with a `{…}` line between or after them kept as a JSX
|
|
448
|
+
* child. Null when the code fails to parse for any other reason, or when
|
|
449
|
+
* something else follows the first sibling.
|
|
450
|
+
* @param {string} code
|
|
451
|
+
* @returns {{directives: any[], statements: any[], elements: Piece[]} | null}
|
|
452
|
+
*/
|
|
453
|
+
function trailingSiblings(code) {
|
|
454
|
+
/** @type {any[]} */
|
|
455
|
+
let directives = [];
|
|
456
|
+
/** @type {any[]} */
|
|
457
|
+
let statements = [];
|
|
458
|
+
/** @type {Piece[]} */
|
|
459
|
+
const elements = [];
|
|
460
|
+
let offset = 0;
|
|
461
|
+
for (;;) {
|
|
462
|
+
const rest = code.slice(offset);
|
|
463
|
+
const whole = tryParse(rest);
|
|
464
|
+
if ('ast' in whole) {
|
|
465
|
+
if (offset === 0) return null;
|
|
466
|
+
const body = /** @type {any[]} */ (whole.ast.program.body);
|
|
467
|
+
if (!body.every(isMarkupStatement)) return null;
|
|
468
|
+
for (const node of body) elements.push(markupPiece(rest, node, offset));
|
|
469
|
+
return {directives, statements, elements};
|
|
470
|
+
}
|
|
471
|
+
const error = whole.error;
|
|
472
|
+
if (
|
|
473
|
+
error?.reasonCode !== 'UnwrappedAdjacentJSXElements' ||
|
|
474
|
+
typeof error.pos !== 'number' ||
|
|
475
|
+
error.pos === 0
|
|
476
|
+
) {
|
|
477
|
+
return null;
|
|
478
|
+
}
|
|
479
|
+
const segment = tryParse(rest.slice(0, error.pos));
|
|
480
|
+
if (!('ast' in segment)) return null;
|
|
481
|
+
const body = /** @type {any[]} */ (segment.ast.program.body);
|
|
482
|
+
if (!isJsxStatement(body.at(-1))) return null;
|
|
483
|
+
const markupFrom = offset === 0 ? trailingMarkupStart(body) : 0;
|
|
484
|
+
if (offset === 0) {
|
|
485
|
+
directives = segment.ast.program.directives;
|
|
486
|
+
statements = body.slice(0, markupFrom);
|
|
487
|
+
} else if (!body.every(isMarkupStatement)) {
|
|
488
|
+
return null;
|
|
489
|
+
}
|
|
490
|
+
for (const node of body.slice(markupFrom)) {
|
|
491
|
+
elements.push(markupPiece(rest, node, offset));
|
|
492
|
+
}
|
|
493
|
+
offset += error.pos;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* @typedef {string | {text: string, from: number}} Piece text the module
|
|
499
|
+
* adds, or text of the example and where it starts in the example's code
|
|
500
|
+
*/
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* @param {string} code
|
|
504
|
+
* @param {any} node
|
|
505
|
+
* @returns {Piece}
|
|
506
|
+
*/
|
|
507
|
+
const piece = (code, node) => ({
|
|
508
|
+
text: code.slice(node.start, node.end),
|
|
509
|
+
from: node.start,
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Sibling elements as one fragment.
|
|
514
|
+
* @param {Piece[]} elements
|
|
515
|
+
* @returns {Piece[]}
|
|
516
|
+
*/
|
|
517
|
+
const fragment = elements => ['<>\n', ...joined(elements, '\n'), '\n</>'];
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* @param {Piece[]} pieces
|
|
521
|
+
* @param {string} separator
|
|
522
|
+
* @returns {Piece[]}
|
|
523
|
+
*/
|
|
524
|
+
const joined = (pieces, separator) =>
|
|
525
|
+
pieces.flatMap((each, index) => (index === 0 ? [each] : [separator, each]));
|
|
526
|
+
|
|
527
|
+
/** Statements that stay at module scope: imports and re-exports. */
|
|
528
|
+
const MODULE_SCOPE = new Set([
|
|
529
|
+
'ImportDeclaration',
|
|
530
|
+
'TSImportEqualsDeclaration',
|
|
531
|
+
'ExportAllDeclaration',
|
|
532
|
+
]);
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* The component an example's statements and trailing JSX stand for. Its
|
|
536
|
+
* directives, imports and re-exports stay at module scope; every other
|
|
537
|
+
* statement runs in the component, in order, a declaration it exports
|
|
538
|
+
* without its `export` (an `export {…}` list of its own names is dropped).
|
|
539
|
+
* @param {string} code
|
|
540
|
+
* @param {any[]} directives
|
|
541
|
+
* @param {any[]} statements
|
|
542
|
+
* @param {Piece[]} element the JSX the component returns
|
|
543
|
+
* @returns {ExampleModule}
|
|
544
|
+
*/
|
|
545
|
+
function composed(code, directives, statements, element) {
|
|
546
|
+
// The component takes a name the example's code does not use, so no
|
|
547
|
+
// import or declaration of the example can clash with it.
|
|
548
|
+
const used = new Set(code.match(/\bExample\d*\b/gu) ?? []);
|
|
549
|
+
let name = 'Example';
|
|
550
|
+
for (let count = 2; used.has(name); count += 1) name = `Example${count}`;
|
|
551
|
+
/** @type {Piece[]} */
|
|
552
|
+
const prelude = directives.map(node => piece(code, node));
|
|
553
|
+
/** @type {Piece[]} */
|
|
554
|
+
const inside = [];
|
|
555
|
+
for (const node of statements) {
|
|
556
|
+
if (
|
|
557
|
+
MODULE_SCOPE.has(node.type) ||
|
|
558
|
+
(node.type === 'ExportNamedDeclaration' && node.source)
|
|
559
|
+
) {
|
|
560
|
+
prelude.push(piece(code, node));
|
|
561
|
+
} else if (node.type === 'ExportNamedDeclaration') {
|
|
562
|
+
if (node.declaration) inside.push(piece(code, node.declaration));
|
|
563
|
+
} else {
|
|
564
|
+
inside.push(piece(code, node));
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
return {
|
|
568
|
+
...assembled([
|
|
569
|
+
...joined(prelude, '\n'),
|
|
570
|
+
prelude.length > 0 ? '\n\n' : '',
|
|
571
|
+
`export default function ${name}() {\n`,
|
|
572
|
+
...joined(inside, '\n'),
|
|
573
|
+
inside.length > 0 ? '\n' : '',
|
|
574
|
+
'return (\n',
|
|
575
|
+
...element,
|
|
576
|
+
'\n);\n}\n',
|
|
577
|
+
]),
|
|
578
|
+
renders: true,
|
|
579
|
+
wrapped: true,
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Join pieces into a module, keeping the way back from a position in it to
|
|
585
|
+
* the position in the example's code it comes from.
|
|
586
|
+
* @param {Piece[]} pieces
|
|
587
|
+
* @returns {{module: string, toExample: (position: number) => number | null}}
|
|
588
|
+
*/
|
|
589
|
+
function assembled(pieces) {
|
|
590
|
+
let module = '';
|
|
591
|
+
/** @type {Array<{at: number, from: number, length: number}>} */
|
|
592
|
+
const spans = [];
|
|
593
|
+
for (const each of pieces) {
|
|
594
|
+
if (typeof each === 'string') {
|
|
595
|
+
module += each;
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
spans.push({at: module.length, from: each.from, length: each.text.length});
|
|
599
|
+
module += each.text;
|
|
600
|
+
}
|
|
601
|
+
return {
|
|
602
|
+
module,
|
|
603
|
+
toExample: position => {
|
|
604
|
+
const span = spans.find(
|
|
605
|
+
each => position >= each.at && position <= each.at + each.length,
|
|
606
|
+
);
|
|
607
|
+
return span ? span.from + position - span.at : null;
|
|
608
|
+
},
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* The 1-based line and 0-based column of a position in some text.
|
|
614
|
+
* @param {string} text
|
|
615
|
+
* @param {number} position
|
|
616
|
+
*/
|
|
617
|
+
function lineAndColumn(text, position) {
|
|
618
|
+
const before = text.slice(0, position);
|
|
619
|
+
const lineStart = before.lastIndexOf('\n') + 1;
|
|
620
|
+
return {
|
|
621
|
+
line: before.split('\n').length,
|
|
622
|
+
column: position - lineStart,
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* The problem a parse error is, placed in the example's own code. An error
|
|
628
|
+
* in the module but not in the code means a statement before the trailing
|
|
629
|
+
* JSX cannot run inside the component that renders it, such as a top-level
|
|
630
|
+
* `await`.
|
|
631
|
+
* @param {any} error a Babel parse error
|
|
632
|
+
* @param {string} code the example's code
|
|
633
|
+
* @param {ExampleModule} [read] the module the error is in, when it is not
|
|
634
|
+
* the code itself
|
|
635
|
+
* @returns {ExampleProblem}
|
|
636
|
+
*/
|
|
637
|
+
function compileProblem(error, code, read) {
|
|
638
|
+
// A transform error starts with the file it read and ends with a code
|
|
639
|
+
// frame of the module; the note keeps only what went wrong.
|
|
640
|
+
const message = String(error?.message ?? error)
|
|
641
|
+
.split('\n')[0]
|
|
642
|
+
.replace(/^.*?example\.tsx: /u, '')
|
|
643
|
+
.replace(/\s*\(\d+:\d+\)$/u, '')
|
|
644
|
+
.replace(/\.$/u, '');
|
|
645
|
+
const position =
|
|
646
|
+
typeof error?.pos !== 'number'
|
|
647
|
+
? null
|
|
648
|
+
: read
|
|
649
|
+
? read.toExample(error.pos)
|
|
650
|
+
: error.pos;
|
|
651
|
+
const place = position == null ? null : lineAndColumn(code, position);
|
|
652
|
+
const where = place ? ` (line ${place.line})` : '';
|
|
653
|
+
const located = place ? {line: place.line, column: place.column} : {};
|
|
654
|
+
if (read) {
|
|
655
|
+
return {
|
|
656
|
+
code: 'example_does_not_compile',
|
|
657
|
+
message: `does not compile once its code runs inside the component that renders its JSX: ${message}${where}`,
|
|
658
|
+
fix: "Declare the component yourself and default-export it, with that code where it can run, or set `kind: 'setup'` if it is not meant to run on its own.",
|
|
659
|
+
...located,
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
const atEnd = position != null && position >= code.trimEnd().length - 1;
|
|
663
|
+
const fix =
|
|
664
|
+
error?.reasonCode === 'IllegalReturn'
|
|
665
|
+
? "End the example with the JSX it renders instead of a `return`, or put the code in a component. If it is part of a function in an app, set `kind: 'setup'`."
|
|
666
|
+
: atEnd
|
|
667
|
+
? "Show the whole statement, or set `kind: 'setup'` if the code is an excerpt of a larger file."
|
|
668
|
+
: "Make it a complete TSX module, or set `kind: 'setup'` if it is not meant to run on its own.";
|
|
669
|
+
return {
|
|
670
|
+
code: 'example_does_not_compile',
|
|
671
|
+
message: atEnd
|
|
672
|
+
? `does not compile: the code stops before its last statement ends${where}`
|
|
673
|
+
: `does not compile: ${message}${where}`,
|
|
674
|
+
fix,
|
|
675
|
+
...located,
|
|
676
|
+
};
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* The value imports of a module: static imports that are not type-only,
|
|
681
|
+
* re-exports, and literal `import()` and `require()` calls.
|
|
682
|
+
* @param {any} ast
|
|
683
|
+
* @returns {string[]}
|
|
684
|
+
*/
|
|
685
|
+
function valueImports(ast) {
|
|
686
|
+
/** @type {string[]} */
|
|
687
|
+
const found = [];
|
|
688
|
+
babel().traverse(ast, {
|
|
689
|
+
ImportDeclaration(/** @type {any} */ path) {
|
|
690
|
+
const node = /** @type {any} */ (path.node);
|
|
691
|
+
const specifiers = node.specifiers ?? [];
|
|
692
|
+
const typeOnly =
|
|
693
|
+
node.importKind === 'type' ||
|
|
694
|
+
(specifiers.length > 0 &&
|
|
695
|
+
specifiers.every((/** @type {any} */ s) => s.importKind === 'type'));
|
|
696
|
+
if (!typeOnly) found.push(String(node.source.value));
|
|
697
|
+
},
|
|
698
|
+
ExportNamedDeclaration(/** @type {any} */ path) {
|
|
699
|
+
const node = /** @type {any} */ (path.node);
|
|
700
|
+
if (node.source && node.exportKind !== 'type') {
|
|
701
|
+
found.push(String(node.source.value));
|
|
702
|
+
}
|
|
703
|
+
},
|
|
704
|
+
ExportAllDeclaration(/** @type {any} */ path) {
|
|
705
|
+
const node = /** @type {any} */ (path.node);
|
|
706
|
+
if (node.exportKind !== 'type') found.push(String(node.source.value));
|
|
707
|
+
},
|
|
708
|
+
CallExpression(/** @type {any} */ path) {
|
|
709
|
+
const node = /** @type {any} */ (path.node);
|
|
710
|
+
const isRequire =
|
|
711
|
+
node.callee.type === 'Identifier' && node.callee.name === 'require';
|
|
712
|
+
const isImport = node.callee.type === 'Import';
|
|
713
|
+
const [first] = node.arguments;
|
|
714
|
+
if ((isRequire || isImport) && first?.type === 'StringLiteral') {
|
|
715
|
+
found.push(first.value);
|
|
716
|
+
}
|
|
717
|
+
},
|
|
718
|
+
ImportExpression(/** @type {any} */ path) {
|
|
719
|
+
const node = /** @type {any} */ (path.node);
|
|
720
|
+
if (node.source?.type === 'StringLiteral') found.push(node.source.value);
|
|
721
|
+
},
|
|
722
|
+
});
|
|
723
|
+
return found;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* The names a module uses but never binds, in order of first use, once its
|
|
728
|
+
* types are stripped (a type is not a runtime name), and which of them name
|
|
729
|
+
* an element; the imported names it uses, with the module each comes from;
|
|
730
|
+
* and whether it reads `process.env` past `NODE_ENV`.
|
|
731
|
+
* @param {string} module
|
|
732
|
+
* @returns {{free: string[], elements: Set<string>, imported: Array<{source: string, name: string}>, readsEnvironment: boolean} | {error: any}}
|
|
733
|
+
*/
|
|
734
|
+
function unboundNames(module) {
|
|
735
|
+
/** @type {any} */
|
|
736
|
+
let ast;
|
|
737
|
+
try {
|
|
738
|
+
ast = babel().transformSync(module, {
|
|
739
|
+
babelrc: false,
|
|
740
|
+
configFile: false,
|
|
741
|
+
browserslistConfigFile: false,
|
|
742
|
+
filename: 'example.tsx',
|
|
743
|
+
sourceType: 'module',
|
|
744
|
+
parserOpts: {plugins: ['jsx', 'typescript']},
|
|
745
|
+
plugins: [
|
|
746
|
+
[
|
|
747
|
+
stripTypes(),
|
|
748
|
+
{isTSX: true, allExtensions: true, onlyRemoveTypeImports: true},
|
|
749
|
+
],
|
|
750
|
+
],
|
|
751
|
+
ast: true,
|
|
752
|
+
code: false,
|
|
753
|
+
})?.ast;
|
|
754
|
+
} catch (error) {
|
|
755
|
+
return {error};
|
|
756
|
+
}
|
|
757
|
+
/** @type {string[]} */
|
|
758
|
+
const free = [];
|
|
759
|
+
/** @type {Set<string>} */
|
|
760
|
+
const elements = new Set();
|
|
761
|
+
/** @type {Array<{source: string, name: string}>} */
|
|
762
|
+
const imported = [];
|
|
763
|
+
let readsEnvironment = false;
|
|
764
|
+
babel().traverse(ast, {
|
|
765
|
+
MemberExpression(/** @type {any} */ path) {
|
|
766
|
+
const node = /** @type {any} */ (path.node);
|
|
767
|
+
if (!isProcessEnv(node)) return;
|
|
768
|
+
const parent = /** @type {any} */ (path.parent);
|
|
769
|
+
if (!(parent?.object === node && isNodeEnvRead(parent))) {
|
|
770
|
+
readsEnvironment = true;
|
|
771
|
+
}
|
|
772
|
+
},
|
|
773
|
+
ReferencedIdentifier(/** @type {any} */ path) {
|
|
774
|
+
const node = /** @type {any} */ (path.node);
|
|
775
|
+
const name = String(node.name);
|
|
776
|
+
// An intrinsic tag (`<div>`) is a string, not a reference.
|
|
777
|
+
if (
|
|
778
|
+
path.isJSXIdentifier() &&
|
|
779
|
+
!path.parentPath?.isJSXMemberExpression() &&
|
|
780
|
+
!/^[A-Z_$]/u.test(name)
|
|
781
|
+
) {
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
if (path.parentPath?.isUnaryExpression({operator: 'typeof'})) return;
|
|
785
|
+
if (name === 'arguments' && path.scope.getFunctionParent()) return;
|
|
786
|
+
// `process.env` is its own finding; `process.env.NODE_ENV` is replaced.
|
|
787
|
+
const parent = /** @type {any} */ (path.parent);
|
|
788
|
+
if (name === 'process' && isProcessEnv(parent)) return;
|
|
789
|
+
const binding = path.scope.getBinding(name);
|
|
790
|
+
if (binding?.kind === 'module') {
|
|
791
|
+
const specifier = /** @type {any} */ (binding.path.node);
|
|
792
|
+
const source = String(
|
|
793
|
+
/** @type {any} */ (binding.path.parent)?.source?.value,
|
|
794
|
+
);
|
|
795
|
+
const importedName =
|
|
796
|
+
specifier.type === 'ImportSpecifier'
|
|
797
|
+
? (specifier.imported.name ?? specifier.imported.value)
|
|
798
|
+
: specifier.type === 'ImportDefaultSpecifier'
|
|
799
|
+
? 'default'
|
|
800
|
+
: '*';
|
|
801
|
+
if (
|
|
802
|
+
!imported.some(
|
|
803
|
+
use => use.source === source && use.name === importedName,
|
|
804
|
+
)
|
|
805
|
+
) {
|
|
806
|
+
imported.push({source, name: importedName});
|
|
807
|
+
}
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
if (binding || path.scope.hasBinding(name, true)) return;
|
|
811
|
+
if (!free.includes(name)) free.push(name);
|
|
812
|
+
if (path.isJSXIdentifier()) elements.add(name);
|
|
813
|
+
},
|
|
814
|
+
});
|
|
815
|
+
return {free, elements, imported, readsEnvironment};
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
/**
|
|
819
|
+
* @param {ExampleProblem[]} problems
|
|
820
|
+
* @returns {PreviewableExample}
|
|
821
|
+
*/
|
|
822
|
+
function notPreviewable(problems) {
|
|
823
|
+
return {previewable: false, setup: false, problems};
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* Read a doc example as the module a live preview runs.
|
|
828
|
+
*
|
|
829
|
+
* A `kind: 'setup'` example is code only, by its author's choice. Any other
|
|
830
|
+
* example must run on its own: it compiles as a TSX module, renders
|
|
831
|
+
* something, imports only what a browser can load from the package and the
|
|
832
|
+
* packages it declares (React and Core always), and uses only names it
|
|
833
|
+
* defines, imports, or takes from its scope or the JavaScript and browser
|
|
834
|
+
* globals. A previewable example's `module` imports each scope name it uses
|
|
835
|
+
* (the package's own components, then Core's components and hooks, then
|
|
836
|
+
* React's exports)
|
|
837
|
+
* and default-exports what it renders, so a bundler can build it as is.
|
|
838
|
+
*
|
|
839
|
+
* @param {{code: string, kind?: unknown}} example
|
|
840
|
+
* @param {ExampleScope} scope
|
|
841
|
+
* @returns {PreviewableExample}
|
|
842
|
+
*/
|
|
843
|
+
export function previewableExample(example, scope) {
|
|
844
|
+
if (example?.kind === 'setup') return {previewable: false, setup: true};
|
|
845
|
+
const code = typeof example?.code === 'string' ? example.code : '';
|
|
846
|
+
const read = moduleFor(code);
|
|
847
|
+
if ('error' in read) {
|
|
848
|
+
return notPreviewable([compileProblem(read.error, code)]);
|
|
849
|
+
}
|
|
850
|
+
const wrapper = read.wrapped ? read : undefined;
|
|
851
|
+
const parsed = tryParse(read.module);
|
|
852
|
+
if ('error' in parsed) {
|
|
853
|
+
return notPreviewable([compileProblem(parsed.error, code, wrapper)]);
|
|
854
|
+
}
|
|
855
|
+
const unbound = unboundNames(read.module);
|
|
856
|
+
if ('error' in unbound) {
|
|
857
|
+
return notPreviewable([compileProblem(unbound.error, code, wrapper)]);
|
|
858
|
+
}
|
|
859
|
+
const table = scopeTable(scope);
|
|
860
|
+
/** @type {ExampleProblem[]} */
|
|
861
|
+
const problems = [];
|
|
862
|
+
if (!read.renders) {
|
|
863
|
+
problems.push({
|
|
864
|
+
code: 'example_renders_nothing',
|
|
865
|
+
message:
|
|
866
|
+
'renders nothing: it has no default export, declares no component, and does not end with JSX',
|
|
867
|
+
fix: "End it with the JSX it renders, or set `kind: 'setup'` if it is code for an app to copy.",
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
for (const specifier of new Set(valueImports(parsed.ast))) {
|
|
871
|
+
if (
|
|
872
|
+
isServerOnlyImport(specifier, table.dependencies) ||
|
|
873
|
+
table.serverOnly.has(specifier)
|
|
874
|
+
) {
|
|
875
|
+
problems.push({
|
|
876
|
+
code: 'example_server_only',
|
|
877
|
+
message: `imports '${specifier}', which runs only on a server, so a live preview in a browser cannot load it`,
|
|
878
|
+
fix: "Set `kind: 'setup'` to show it as code only, or move the server code into the app and pass its values in as props.",
|
|
879
|
+
specifier,
|
|
880
|
+
});
|
|
881
|
+
} else if (!isBare(specifier)) {
|
|
882
|
+
problems.push({
|
|
883
|
+
code: 'example_import_unresolvable',
|
|
884
|
+
message: `imports '${specifier}', a file next to the code in an app, which a live preview does not have`,
|
|
885
|
+
fix: "Drop the import, or set `kind: 'setup'` if the code is a file in an app.",
|
|
886
|
+
specifier,
|
|
887
|
+
});
|
|
888
|
+
} else if (!table.importable.has(packageOf(specifier))) {
|
|
889
|
+
problems.push({
|
|
890
|
+
code: 'example_import_unresolvable',
|
|
891
|
+
message: `imports '${specifier}', but ${scope.packageName} does not declare '${packageOf(specifier)}' in dependencies, peerDependencies or optionalDependencies, so an app that installs it may not have it`,
|
|
892
|
+
fix: "Declare the package, import from one the package declares, or set `kind: 'setup'`.",
|
|
893
|
+
specifier,
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
if (unbound.readsEnvironment) {
|
|
898
|
+
problems.push({
|
|
899
|
+
code: 'example_server_only',
|
|
900
|
+
message:
|
|
901
|
+
'reads process.env, which only a server has, so a live preview in a browser cannot run it',
|
|
902
|
+
fix: "Pass the value in as a prop or a constant, or set `kind: 'setup'` to show it as code only.",
|
|
903
|
+
});
|
|
904
|
+
}
|
|
905
|
+
const serverComponents = new Set(scope.serverComponents ?? []);
|
|
906
|
+
const rendered = [
|
|
907
|
+
...unbound.free.filter(
|
|
908
|
+
name =>
|
|
909
|
+
serverComponents.has(name) &&
|
|
910
|
+
packageOf(table.names.get(name) ?? '') === scope.packageName,
|
|
911
|
+
),
|
|
912
|
+
...unbound.imported
|
|
913
|
+
.filter(
|
|
914
|
+
use =>
|
|
915
|
+
isBare(use.source) &&
|
|
916
|
+
packageOf(use.source) === scope.packageName &&
|
|
917
|
+
serverComponents.has(use.name),
|
|
918
|
+
)
|
|
919
|
+
.map(use => use.name),
|
|
920
|
+
];
|
|
921
|
+
for (const name of new Set(rendered)) {
|
|
922
|
+
problems.push({
|
|
923
|
+
code: 'example_server_only',
|
|
924
|
+
message: `renders \`${name}\`, an async component that only a server can render, so a live preview in a browser cannot show it`,
|
|
925
|
+
fix: "Set `kind: 'setup'` to show it as code only, or show a component that renders in a browser instead.",
|
|
926
|
+
names: [name],
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
// A browser global such as `Image` or `Map` is not a component, so an
|
|
930
|
+
// element takes its name only from the example or its scope.
|
|
931
|
+
const globals = globalNames();
|
|
932
|
+
const undefinedNames = unbound.free.filter(
|
|
933
|
+
name =>
|
|
934
|
+
!table.names.has(name) &&
|
|
935
|
+
(unbound.elements.has(name) || !globals.has(name)),
|
|
936
|
+
);
|
|
937
|
+
if (undefinedNames.length > 0) {
|
|
938
|
+
const shown = undefinedNames.map(name => `\`${name}\``).join(', ');
|
|
939
|
+
const notComponents = undefinedNames.filter(name => globals.has(name));
|
|
940
|
+
problems.push({
|
|
941
|
+
code: 'example_undefined_name',
|
|
942
|
+
message:
|
|
943
|
+
notComponents.length === 0
|
|
944
|
+
? `uses ${shown} but never defines or imports ${undefinedNames.length === 1 ? 'it' : 'them'}, so a live preview throws a ReferenceError when that code runs`
|
|
945
|
+
: `uses ${shown} but never defines or imports ${undefinedNames.length === 1 ? 'it' : 'them'}, so a live preview throws when that code runs (the browser's ${notComponents.map(name => `\`${name}\``).join(', ')} ${notComponents.length === 1 ? 'is' : 'are'} not a component)`,
|
|
946
|
+
fix: "Define each one in the example (a sample value is enough) or import it; only the package's documented components, Core's documented components and hooks, React's exports and browser globals need no import. If the code needs values from an app, set `kind: 'setup'` to show it as code only.",
|
|
947
|
+
names: undefinedNames,
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
if (problems.length > 0) return notPreviewable(problems);
|
|
951
|
+
const directives = /** @type {any[]} */ (parsed.ast.program.directives);
|
|
952
|
+
return {
|
|
953
|
+
previewable: true,
|
|
954
|
+
module: withScopeImports(
|
|
955
|
+
read.module,
|
|
956
|
+
unbound.free.filter(name => table.names.has(name)),
|
|
957
|
+
table,
|
|
958
|
+
directives.length > 0 ? directives[directives.length - 1].end : 0,
|
|
959
|
+
),
|
|
960
|
+
};
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
/**
|
|
964
|
+
* The module with one import line per specifier for the scope names it
|
|
965
|
+
* uses, `React` as the namespace, after its directive prologue so a
|
|
966
|
+
* directive such as `'use client'` stays one.
|
|
967
|
+
* @param {string} module
|
|
968
|
+
* @param {string[]} used
|
|
969
|
+
* @param {ScopeTable} table
|
|
970
|
+
* @param {number} prologueEnd where the module's directives end
|
|
971
|
+
*/
|
|
972
|
+
function withScopeImports(module, used, table, prologueEnd) {
|
|
973
|
+
/** @type {Map<string, string[]>} */
|
|
974
|
+
const bySpecifier = new Map();
|
|
975
|
+
const lines = [];
|
|
976
|
+
for (const name of used) {
|
|
977
|
+
const specifier = /** @type {string} */ (table.names.get(name));
|
|
978
|
+
if (name === 'React' && specifier === REACT) {
|
|
979
|
+
lines.push(`import * as React from '${REACT}';`);
|
|
980
|
+
continue;
|
|
981
|
+
}
|
|
982
|
+
const names = bySpecifier.get(specifier) ?? [];
|
|
983
|
+
names.push(name);
|
|
984
|
+
bySpecifier.set(specifier, names);
|
|
985
|
+
}
|
|
986
|
+
for (const [specifier, names] of [...bySpecifier].sort(([a], [b]) =>
|
|
987
|
+
a < b ? -1 : a > b ? 1 : 0,
|
|
988
|
+
)) {
|
|
989
|
+
lines.push(`import {${names.sort().join(', ')}} from '${specifier}';`);
|
|
990
|
+
}
|
|
991
|
+
if (lines.length === 0) return module;
|
|
992
|
+
if (prologueEnd === 0) return `${lines.join('\n')}\n${module}`;
|
|
993
|
+
return `${module.slice(0, prologueEnd)}\n${lines.join('\n')}${module.slice(prologueEnd)}`;
|
|
994
|
+
}
|