@rien7/seji 0.0.0 → 0.6.0
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/bin/design-surface.mjs +6 -0
- package/bin/postinstall.mjs +19 -0
- package/bin/seji.mjs +573 -0
- package/dist/agent/host.d.mts +34 -0
- package/dist/agent/host.mjs +387 -0
- package/dist/agent/messages.d.mts +9 -0
- package/dist/agent/messages.mjs +70 -0
- package/dist/agent/runtimes.mjs +157 -0
- package/dist/cli/app.mjs +321 -0
- package/dist/cli/appScan.mjs +284 -0
- package/dist/cli/catalog.mjs +78 -0
- package/dist/cli/component.mjs +165 -0
- package/dist/cli/consolidate.mjs +159 -0
- package/dist/cli/cssImport.mjs +68 -0
- package/dist/cli/dev.mjs +335 -0
- package/dist/cli/doctor.mjs +306 -0
- package/dist/cli/init.mjs +338 -0
- package/dist/cli/legacyNames.mjs +266 -0
- package/dist/cli/migrate.mjs +163 -0
- package/dist/cli/prompt.mjs +34 -0
- package/dist/cli/propose.mjs +208 -0
- package/dist/cli/providers.mjs +344 -0
- package/dist/cli/reviewOps.mjs +110 -0
- package/dist/cli/semver.mjs +45 -0
- package/dist/cli/setup.mjs +357 -0
- package/dist/cli/sketches.mjs +47 -0
- package/dist/cli/skills.mjs +150 -0
- package/dist/cli/snapshot.mjs +107 -0
- package/dist/cli/tokenOps.mjs +120 -0
- package/dist/cli/types.mjs +191 -0
- package/dist/cli/updater.mjs +274 -0
- package/dist/components/astIndex.mjs +992 -0
- package/dist/components/ops.mjs +1069 -0
- package/dist/editor/AgentProposalCard.d.ts +10 -0
- package/dist/editor/AgentProposalCard.d.ts.map +1 -0
- package/dist/editor/AgentProposalCard.js +31 -0
- package/dist/editor/AgentProposalCard.js.map +1 -0
- package/dist/editor/App.d.ts +2 -0
- package/dist/editor/App.d.ts.map +1 -0
- package/dist/editor/App.js +2832 -0
- package/dist/editor/App.js.map +1 -0
- package/dist/editor/CanvasHud.d.ts +12 -0
- package/dist/editor/CanvasHud.d.ts.map +1 -0
- package/dist/editor/CanvasHud.js +13 -0
- package/dist/editor/CanvasHud.js.map +1 -0
- package/dist/editor/CanvasSurface.d.ts +31 -0
- package/dist/editor/CanvasSurface.d.ts.map +1 -0
- package/dist/editor/CanvasSurface.js +270 -0
- package/dist/editor/CanvasSurface.js.map +1 -0
- package/dist/editor/CommentPopover.d.ts +17 -0
- package/dist/editor/CommentPopover.d.ts.map +1 -0
- package/dist/editor/CommentPopover.js +62 -0
- package/dist/editor/CommentPopover.js.map +1 -0
- package/dist/editor/Comments.d.ts +13 -0
- package/dist/editor/Comments.d.ts.map +1 -0
- package/dist/editor/Comments.js +41 -0
- package/dist/editor/Comments.js.map +1 -0
- package/dist/editor/ComponentPage.d.ts +28 -0
- package/dist/editor/ComponentPage.d.ts.map +1 -0
- package/dist/editor/ComponentPage.js +55 -0
- package/dist/editor/ComponentPage.js.map +1 -0
- package/dist/editor/ComponentPanel.d.ts +38 -0
- package/dist/editor/ComponentPanel.d.ts.map +1 -0
- package/dist/editor/ComponentPanel.js +275 -0
- package/dist/editor/ComponentPanel.js.map +1 -0
- package/dist/editor/Glyph.d.ts +51 -0
- package/dist/editor/Glyph.d.ts.map +1 -0
- package/dist/editor/Glyph.js +50 -0
- package/dist/editor/Glyph.js.map +1 -0
- package/dist/editor/History.d.ts +7 -0
- package/dist/editor/History.d.ts.map +1 -0
- package/dist/editor/History.js +51 -0
- package/dist/editor/History.js.map +1 -0
- package/dist/editor/Inspector.d.ts +56 -0
- package/dist/editor/Inspector.d.ts.map +1 -0
- package/dist/editor/Inspector.js +510 -0
- package/dist/editor/Inspector.js.map +1 -0
- package/dist/editor/LazyMount.d.ts +10 -0
- package/dist/editor/LazyMount.d.ts.map +1 -0
- package/dist/editor/LazyMount.js +29 -0
- package/dist/editor/LazyMount.js.map +1 -0
- package/dist/editor/LocaleSwitch.d.ts +3 -0
- package/dist/editor/LocaleSwitch.d.ts.map +1 -0
- package/dist/editor/LocaleSwitch.js +9 -0
- package/dist/editor/LocaleSwitch.js.map +1 -0
- package/dist/editor/PreviewFrame.d.ts +10 -0
- package/dist/editor/PreviewFrame.d.ts.map +1 -0
- package/dist/editor/PreviewFrame.js +22 -0
- package/dist/editor/PreviewFrame.js.map +1 -0
- package/dist/editor/ProjectIndex.d.ts +12 -0
- package/dist/editor/ProjectIndex.d.ts.map +1 -0
- package/dist/editor/ProjectIndex.js +43 -0
- package/dist/editor/ProjectIndex.js.map +1 -0
- package/dist/editor/PromoteComponent.d.ts +28 -0
- package/dist/editor/PromoteComponent.d.ts.map +1 -0
- package/dist/editor/PromoteComponent.js +33 -0
- package/dist/editor/PromoteComponent.js.map +1 -0
- package/dist/editor/PromoteForm.d.ts +18 -0
- package/dist/editor/PromoteForm.d.ts.map +1 -0
- package/dist/editor/PromoteForm.js +35 -0
- package/dist/editor/PromoteForm.js.map +1 -0
- package/dist/editor/PromoteToken.d.ts +31 -0
- package/dist/editor/PromoteToken.d.ts.map +1 -0
- package/dist/editor/PromoteToken.js +94 -0
- package/dist/editor/PromoteToken.js.map +1 -0
- package/dist/editor/StatePreviews.d.ts +28 -0
- package/dist/editor/StatePreviews.d.ts.map +1 -0
- package/dist/editor/StatePreviews.js +113 -0
- package/dist/editor/StatePreviews.js.map +1 -0
- package/dist/editor/ValueCombo.d.ts +51 -0
- package/dist/editor/ValueCombo.d.ts.map +1 -0
- package/dist/editor/ValueCombo.js +260 -0
- package/dist/editor/ValueCombo.js.map +1 -0
- package/dist/editor/agent/AgentPanel.d.ts +19 -0
- package/dist/editor/agent/AgentPanel.d.ts.map +1 -0
- package/dist/editor/agent/AgentPanel.js +271 -0
- package/dist/editor/agent/AgentPanel.js.map +1 -0
- package/dist/editor/agent/Markdown.d.ts +5 -0
- package/dist/editor/agent/Markdown.d.ts.map +1 -0
- package/dist/editor/agent/Markdown.js +28 -0
- package/dist/editor/agent/Markdown.js.map +1 -0
- package/dist/editor/agent/agentApi.d.ts +106 -0
- package/dist/editor/agent/agentApi.d.ts.map +1 -0
- package/dist/editor/agent/agentApi.js +66 -0
- package/dist/editor/agent/agentApi.js.map +1 -0
- package/dist/editor/agent/toolLines.d.ts +38 -0
- package/dist/editor/agent/toolLines.d.ts.map +1 -0
- package/dist/editor/agent/toolLines.js +159 -0
- package/dist/editor/agent/toolLines.js.map +1 -0
- package/dist/editor/agent/useAgent.d.ts +15 -0
- package/dist/editor/agent/useAgent.d.ts.map +1 -0
- package/dist/editor/agent/useAgent.js +65 -0
- package/dist/editor/agent/useAgent.js.map +1 -0
- package/dist/editor/api.d.ts +378 -0
- package/dist/editor/api.d.ts.map +1 -0
- package/dist/editor/api.js +247 -0
- package/dist/editor/api.js.map +1 -0
- package/dist/editor/camera.d.ts +18 -0
- package/dist/editor/camera.d.ts.map +1 -0
- package/dist/editor/camera.js +40 -0
- package/dist/editor/camera.js.map +1 -0
- package/dist/editor/canvas.d.ts +25 -0
- package/dist/editor/canvas.d.ts.map +1 -0
- package/dist/editor/canvas.js +35 -0
- package/dist/editor/canvas.js.map +1 -0
- package/dist/editor/changes/ChangesPage.d.ts +25 -0
- package/dist/editor/changes/ChangesPage.d.ts.map +1 -0
- package/dist/editor/changes/ChangesPage.js +132 -0
- package/dist/editor/changes/ChangesPage.js.map +1 -0
- package/dist/editor/changes/changeModel.d.ts +25 -0
- package/dist/editor/changes/changeModel.d.ts.map +1 -0
- package/dist/editor/changes/changeModel.js +81 -0
- package/dist/editor/changes/changeModel.js.map +1 -0
- package/dist/editor/changes/changeState.d.mts +29 -0
- package/dist/editor/changes/changeState.mjs +187 -0
- package/dist/editor/chrome/EditorChrome.d.ts +49 -0
- package/dist/editor/chrome/EditorChrome.d.ts.map +1 -0
- package/dist/editor/chrome/EditorChrome.js +21 -0
- package/dist/editor/chrome/EditorChrome.js.map +1 -0
- package/dist/editor/drafts.d.ts +46 -0
- package/dist/editor/drafts.d.ts.map +1 -0
- package/dist/editor/drafts.js +38 -0
- package/dist/editor/drafts.js.map +1 -0
- package/dist/editor/i18n.d.ts +29 -0
- package/dist/editor/i18n.d.ts.map +1 -0
- package/dist/editor/i18n.js +2320 -0
- package/dist/editor/i18n.js.map +1 -0
- package/dist/editor/iconStroke.d.ts +6 -0
- package/dist/editor/iconStroke.d.ts.map +1 -0
- package/dist/editor/iconStroke.js +6 -0
- package/dist/editor/iconStroke.js.map +1 -0
- package/dist/editor/main.d.ts +2 -0
- package/dist/editor/main.d.ts.map +1 -0
- package/dist/editor/main.js +14 -0
- package/dist/editor/main.js.map +1 -0
- package/dist/editor/model.d.ts +16 -0
- package/dist/editor/model.d.ts.map +1 -0
- package/dist/editor/model.js +11 -0
- package/dist/editor/model.js.map +1 -0
- package/dist/editor/reasons.d.ts +44 -0
- package/dist/editor/reasons.d.ts.map +1 -0
- package/dist/editor/reasons.js +131 -0
- package/dist/editor/reasons.js.map +1 -0
- package/dist/editor/reviewLifecycle.d.ts +8 -0
- package/dist/editor/reviewLifecycle.d.ts.map +1 -0
- package/dist/editor/reviewLifecycle.js +21 -0
- package/dist/editor/reviewLifecycle.js.map +1 -0
- package/dist/editor/styles.css +8737 -0
- package/dist/editor/tokenMatch.d.ts +59 -0
- package/dist/editor/tokenMatch.d.ts.map +1 -0
- package/dist/editor/tokenMatch.js +198 -0
- package/dist/editor/tokenMatch.js.map +1 -0
- package/dist/editor/tokens/ConsolidatePage.d.ts +16 -0
- package/dist/editor/tokens/ConsolidatePage.d.ts.map +1 -0
- package/dist/editor/tokens/ConsolidatePage.js +244 -0
- package/dist/editor/tokens/ConsolidatePage.js.map +1 -0
- package/dist/editor/tokens/TokenActivity.d.ts +18 -0
- package/dist/editor/tokens/TokenActivity.d.ts.map +1 -0
- package/dist/editor/tokens/TokenActivity.js +18 -0
- package/dist/editor/tokens/TokenActivity.js.map +1 -0
- package/dist/editor/tokens/TokenDialogs.d.ts +26 -0
- package/dist/editor/tokens/TokenDialogs.d.ts.map +1 -0
- package/dist/editor/tokens/TokenDialogs.js +119 -0
- package/dist/editor/tokens/TokenDialogs.js.map +1 -0
- package/dist/editor/tokens/TokenList.d.ts +21 -0
- package/dist/editor/tokens/TokenList.d.ts.map +1 -0
- package/dist/editor/tokens/TokenList.js +98 -0
- package/dist/editor/tokens/TokenList.js.map +1 -0
- package/dist/editor/tokens/TokenPage.d.ts +25 -0
- package/dist/editor/tokens/TokenPage.d.ts.map +1 -0
- package/dist/editor/tokens/TokenPage.js +108 -0
- package/dist/editor/tokens/TokenPage.js.map +1 -0
- package/dist/editor/tokens/tokenModel.d.ts +35 -0
- package/dist/editor/tokens/tokenModel.d.ts.map +1 -0
- package/dist/editor/tokens/tokenModel.js +72 -0
- package/dist/editor/tokens/tokenModel.js.map +1 -0
- package/dist/editor/ui/Button.d.ts +3 -0
- package/dist/editor/ui/Button.d.ts.map +1 -0
- package/dist/editor/ui/Button.js +6 -0
- package/dist/editor/ui/Button.js.map +1 -0
- package/dist/editor/ui/CommentBox.d.ts +23 -0
- package/dist/editor/ui/CommentBox.d.ts.map +1 -0
- package/dist/editor/ui/CommentBox.js +41 -0
- package/dist/editor/ui/CommentBox.js.map +1 -0
- package/dist/editor/ui/Icon.d.ts +8 -0
- package/dist/editor/ui/Icon.d.ts.map +1 -0
- package/dist/editor/ui/Icon.js +6 -0
- package/dist/editor/ui/Icon.js.map +1 -0
- package/dist/editor/ui/IconButton.d.ts +11 -0
- package/dist/editor/ui/IconButton.d.ts.map +1 -0
- package/dist/editor/ui/IconButton.js +6 -0
- package/dist/editor/ui/IconButton.js.map +1 -0
- package/dist/editor/ui/NumberInput.d.ts +14 -0
- package/dist/editor/ui/NumberInput.d.ts.map +1 -0
- package/dist/editor/ui/NumberInput.js +6 -0
- package/dist/editor/ui/NumberInput.js.map +1 -0
- package/dist/editor/ui/PageLine.d.ts +14 -0
- package/dist/editor/ui/PageLine.d.ts.map +1 -0
- package/dist/editor/ui/PageLine.js +7 -0
- package/dist/editor/ui/PageLine.js.map +1 -0
- package/dist/editor/ui/SearchPicker.d.ts +41 -0
- package/dist/editor/ui/SearchPicker.d.ts.map +1 -0
- package/dist/editor/ui/SearchPicker.js +82 -0
- package/dist/editor/ui/SearchPicker.js.map +1 -0
- package/dist/editor/ui/Select.d.ts +19 -0
- package/dist/editor/ui/Select.d.ts.map +1 -0
- package/dist/editor/ui/Select.js +12 -0
- package/dist/editor/ui/Select.js.map +1 -0
- package/dist/editor/ui/TokenField.d.ts +14 -0
- package/dist/editor/ui/TokenField.d.ts.map +1 -0
- package/dist/editor/ui/TokenField.js +41 -0
- package/dist/editor/ui/TokenField.js.map +1 -0
- package/dist/editor/ui/TokenPicker.d.ts +31 -0
- package/dist/editor/ui/TokenPicker.d.ts.map +1 -0
- package/dist/editor/ui/TokenPicker.js +26 -0
- package/dist/editor/ui/TokenPicker.js.map +1 -0
- package/dist/editor/useBuildError.d.ts +7 -0
- package/dist/editor/useBuildError.d.ts.map +1 -0
- package/dist/editor/useBuildError.js +30 -0
- package/dist/editor/useBuildError.js.map +1 -0
- package/dist/editor/useLatestRequest.d.ts +22 -0
- package/dist/editor/useLatestRequest.d.ts.map +1 -0
- package/dist/editor/useLatestRequest.js +49 -0
- package/dist/editor/useLatestRequest.js.map +1 -0
- package/dist/editor-bundle/editor.css +2 -0
- package/dist/editor-bundle/editor.js +83 -0
- package/dist/editor-ui.d.ts +15 -0
- package/dist/editor-ui.d.ts.map +1 -0
- package/dist/editor-ui.js +14 -0
- package/dist/editor-ui.js.map +1 -0
- package/dist/inspector/runtime.d.ts +18 -0
- package/dist/inspector/runtime.d.ts.map +1 -0
- package/dist/inspector/runtime.js +1280 -0
- package/dist/inspector/runtime.js.map +1 -0
- package/dist/preview/main.d.ts +3 -0
- package/dist/preview/main.d.ts.map +1 -0
- package/dist/preview/main.js +320 -0
- package/dist/preview/main.js.map +1 -0
- package/dist/preview/preview.css +40 -0
- package/dist/runtime/index.d.ts +3 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +2 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/shared/buildError.d.mts +27 -0
- package/dist/shared/buildError.mjs +141 -0
- package/dist/shared/cameraFit.d.mts +16 -0
- package/dist/shared/cameraFit.mjs +47 -0
- package/dist/shared/canvasLayout.d.mts +13 -0
- package/dist/shared/canvasLayout.mjs +171 -0
- package/dist/shared/commentShots.d.mts +12 -0
- package/dist/shared/commentShots.mjs +53 -0
- package/dist/shared/componentExtract.d.mts +43 -0
- package/dist/shared/componentExtract.mjs +136 -0
- package/dist/shared/componentMeta.d.mts +26 -0
- package/dist/shared/componentMeta.mjs +87 -0
- package/dist/shared/componentOps.d.mts +70 -0
- package/dist/shared/componentOps.mjs +407 -0
- package/dist/shared/componentTarget.d.mts +15 -0
- package/dist/shared/componentTarget.mjs +58 -0
- package/dist/shared/components.d.mts +30 -0
- package/dist/shared/components.mjs +106 -0
- package/dist/shared/consolidateCategories.d.mts +2 -0
- package/dist/shared/consolidateCategories.mjs +6 -0
- package/dist/shared/env.d.mts +4 -0
- package/dist/shared/env.mjs +18 -0
- package/dist/shared/gitChanges.d.mts +2 -0
- package/dist/shared/gitChanges.mjs +30 -0
- package/dist/shared/oklab.d.mts +6 -0
- package/dist/shared/oklab.mjs +56 -0
- package/dist/shared/optimizeEntries.d.mts +5 -0
- package/dist/shared/optimizeEntries.mjs +24 -0
- package/dist/shared/packages.d.mts +11 -0
- package/dist/shared/packages.mjs +96 -0
- package/dist/shared/projectFiles.d.mts +23 -0
- package/dist/shared/projectFiles.mjs +72 -0
- package/dist/shared/prompt.d.mts +20 -0
- package/dist/shared/prompt.mjs +201 -0
- package/dist/shared/proposals.d.mts +11 -0
- package/dist/shared/proposals.mjs +42 -0
- package/dist/shared/protocol.d.ts +122 -0
- package/dist/shared/protocol.d.ts.map +1 -0
- package/dist/shared/protocol.js +7 -0
- package/dist/shared/protocol.js.map +1 -0
- package/dist/shared/reviewArchive.d.mts +4 -0
- package/dist/shared/reviewArchive.mjs +19 -0
- package/dist/shared/reviewStore.d.mts +30 -0
- package/dist/shared/reviewStore.mjs +218 -0
- package/dist/shared/reviewVerify.d.mts +21 -0
- package/dist/shared/reviewVerify.mjs +158 -0
- package/dist/shared/sizeModes.d.mts +15 -0
- package/dist/shared/sizeModes.mjs +53 -0
- package/dist/shared/sketches.d.mts +10 -0
- package/dist/shared/sketches.mjs +64 -0
- package/dist/shared/snapshotCheck.d.mts +13 -0
- package/dist/shared/snapshotCheck.mjs +81 -0
- package/dist/shared/timeout.d.mts +1 -0
- package/dist/shared/timeout.mjs +15 -0
- package/dist/shared/tokens.d.ts +2 -0
- package/dist/shared/tokens.d.ts.map +1 -0
- package/dist/shared/tokens.js +2 -0
- package/dist/shared/tokens.js.map +1 -0
- package/dist/shared/types.d.ts +689 -0
- package/dist/shared/types.d.ts.map +1 -0
- package/dist/shared/types.js +13 -0
- package/dist/shared/types.js.map +1 -0
- package/dist/shared/wheelRouting.d.mts +9 -0
- package/dist/shared/wheelRouting.mjs +31 -0
- package/dist/shared/writeScope.mjs +96 -0
- package/dist/snapshots/diff.d.mts +28 -0
- package/dist/snapshots/diff.mjs +263 -0
- package/dist/snapshots/index.d.mts +110 -0
- package/dist/snapshots/index.mjs +638 -0
- package/dist/snapshots/render.d.mts +24 -0
- package/dist/snapshots/render.mjs +250 -0
- package/dist/snapshots/toolOps.d.mts +25 -0
- package/dist/snapshots/toolOps.mjs +69 -0
- package/dist/tokens/adapters/css.mjs +51 -0
- package/dist/tokens/adapters/cssInCode.mjs +314 -0
- package/dist/tokens/adapters/oxcShared.mjs +80 -0
- package/dist/tokens/adapters/preprocessor.mjs +63 -0
- package/dist/tokens/adapters/registry.mjs +53 -0
- package/dist/tokens/adapters/tailwindcss.mjs +424 -0
- package/dist/tokens/adapters/types.mjs +117 -0
- package/dist/tokens/consolidate/index.d.mts +42 -0
- package/dist/tokens/consolidate/index.mjs +111 -0
- package/dist/tokens/consolidate/normalize.mjs +479 -0
- package/dist/tokens/consolidate/plan.mjs +726 -0
- package/dist/tokens/consolidate/scan.mjs +928 -0
- package/dist/tokens/extract.mjs +441 -0
- package/dist/tokens/moduleData.d.mts +14 -0
- package/dist/tokens/moduleData.mjs +32 -0
- package/dist/tokens/opLock.d.mts +4 -0
- package/dist/tokens/opLock.mjs +108 -0
- package/dist/tokens/ops.d.mts +71 -0
- package/dist/tokens/ops.mjs +1511 -0
- package/dist/tokens/pipeline.mjs +813 -0
- package/dist/tokens/report.d.mts +47 -0
- package/dist/tokens/report.mjs +257 -0
- package/dist/tokens/rewrite/code.mjs +172 -0
- package/dist/tokens/rewrite/css.mjs +154 -0
- package/dist/tokens/rewrite/index.mjs +244 -0
- package/dist/tokens/rewrite/tailwind.mjs +288 -0
- package/dist/tokens/rewrite/text.mjs +116 -0
- package/dist/tokens/rewrite/value.mjs +115 -0
- package/dist/tokens/usage.d.mts +97 -0
- package/dist/tokens/usage.mjs +680 -0
- package/dist/vite/agent.d.ts +21 -0
- package/dist/vite/agent.d.ts.map +1 -0
- package/dist/vite/agent.js +165 -0
- package/dist/vite/agent.js.map +1 -0
- package/dist/vite/events.d.ts +27 -0
- package/dist/vite/events.d.ts.map +1 -0
- package/dist/vite/events.js +156 -0
- package/dist/vite/events.js.map +1 -0
- package/dist/vite/layout.d.ts +2 -0
- package/dist/vite/layout.d.ts.map +1 -0
- package/dist/vite/layout.js +4 -0
- package/dist/vite/layout.js.map +1 -0
- package/dist/vite/plugin.d.ts +40 -0
- package/dist/vite/plugin.d.ts.map +1 -0
- package/dist/vite/plugin.js +1255 -0
- package/dist/vite/plugin.js.map +1 -0
- package/dist/vite/requestGuard.d.mts +3 -0
- package/dist/vite/requestGuard.mjs +43 -0
- package/dist/vite/review.d.ts +11 -0
- package/dist/vite/review.d.ts.map +1 -0
- package/dist/vite/review.js +283 -0
- package/dist/vite/review.js.map +1 -0
- package/package.json +106 -1
- package/skills/seji/SKILL.md +395 -0
- package/skills/seji-authoring/SKILL.md +397 -0
- package/skills/seji-tokens/SKILL.md +432 -0
|
@@ -0,0 +1,992 @@
|
|
|
1
|
+
// Component index built from the syntax tree (@babel/parser), never from file text:
|
|
2
|
+
// which components each file declares and exports, which bindings it imports (including
|
|
3
|
+
// aliases, namespaces and default imports) and from which resolved module, and every
|
|
4
|
+
// reference to those bindings (JSX tags, `as={Button}`, `createElement(Button)`, member
|
|
5
|
+
// access on a namespace import, `typeof Button` in types, shorthand properties, local
|
|
6
|
+
// export lists). References respect scopes: a parameter or local variable that shadows an
|
|
7
|
+
// imported name is not a use of it.
|
|
8
|
+
//
|
|
9
|
+
// Module specifiers resolve relative paths, root-relative `/…`, tsconfig `paths`, and
|
|
10
|
+
// workspace packages through node_modules (package.json `exports` / `source` / `main`,
|
|
11
|
+
// with built `dist/…` targets mapped back to `src/…` when that source file exists).
|
|
12
|
+
// Re-exports (`export { X } from`, `export *`) are followed, so a barrel import of a
|
|
13
|
+
// component resolves to the file that declares it.
|
|
14
|
+
//
|
|
15
|
+
// The index is shared by the component report (who renders it, can it be deleted) and the
|
|
16
|
+
// component rewriter (rename / merge / replace usages).
|
|
17
|
+
import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs'
|
|
18
|
+
import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path'
|
|
19
|
+
|
|
20
|
+
import { parseSourceAst } from '../tokens/adapters/oxcShared.mjs'
|
|
21
|
+
|
|
22
|
+
export const CODE_FILE = /\.(?:tsx|jsx|ts|js|mts|cts|mjs|cjs)$/i
|
|
23
|
+
/** Components are declared in TSX / JSX files. */
|
|
24
|
+
export const COMPONENT_FILE = /\.(?:tsx|jsx)$/i
|
|
25
|
+
const DECLARATION_FILE = /\.d\.[cm]?ts$/i
|
|
26
|
+
const PASCAL = /^[A-Z][A-Za-z0-9_$]*$/
|
|
27
|
+
const ALL_CAPS = /^[A-Z][A-Z0-9_]+$/
|
|
28
|
+
/** A PascalCase name that is not an ALL_CAPS constant. */
|
|
29
|
+
export const isComponentName = (name) => typeof name === 'string' && PASCAL.test(name) && !ALL_CAPS.test(name)
|
|
30
|
+
const CLASS_WORD = /^-?[_a-zA-Z][\w-]*$/
|
|
31
|
+
const RESOLVE_EXT = ['.tsx', '.ts', '.jsx', '.js', '.mts', '.mjs', '.cts', '.cjs']
|
|
32
|
+
const INDEX_FILES = RESOLVE_EXT.map((ext) => `/index${ext}`)
|
|
33
|
+
const SKIP_KEYS = new Set([
|
|
34
|
+
'type',
|
|
35
|
+
'start',
|
|
36
|
+
'end',
|
|
37
|
+
'loc',
|
|
38
|
+
'range',
|
|
39
|
+
'extra',
|
|
40
|
+
'comments',
|
|
41
|
+
'leadingComments',
|
|
42
|
+
'trailingComments',
|
|
43
|
+
'innerComments',
|
|
44
|
+
])
|
|
45
|
+
const FUNCTION_TYPES = new Set(['FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression', 'TSDeclareFunction'])
|
|
46
|
+
const KEYED_MEMBERS = new Set([
|
|
47
|
+
'MethodDefinition',
|
|
48
|
+
'PropertyDefinition',
|
|
49
|
+
'AccessorProperty',
|
|
50
|
+
'TSAbstractMethodDefinition',
|
|
51
|
+
'TSAbstractPropertyDefinition',
|
|
52
|
+
'TSPropertySignature',
|
|
53
|
+
'TSMethodSignature',
|
|
54
|
+
'ClassProperty',
|
|
55
|
+
'ClassMethod',
|
|
56
|
+
'ClassPrivateProperty',
|
|
57
|
+
])
|
|
58
|
+
const TS_NAMED_DECLARATIONS = new Set([
|
|
59
|
+
'TSTypeAliasDeclaration',
|
|
60
|
+
'TSInterfaceDeclaration',
|
|
61
|
+
'TSEnumDeclaration',
|
|
62
|
+
'TSModuleDeclaration',
|
|
63
|
+
])
|
|
64
|
+
|
|
65
|
+
const toPosix = (file) => file.split(sep).join('/')
|
|
66
|
+
|
|
67
|
+
/** Names a binding pattern declares (identifiers only), in source order. */
|
|
68
|
+
export function patternNames(pattern, out = []) {
|
|
69
|
+
if (!pattern) return out
|
|
70
|
+
switch (pattern.type) {
|
|
71
|
+
case 'Identifier':
|
|
72
|
+
out.push(pattern)
|
|
73
|
+
break
|
|
74
|
+
case 'ObjectPattern':
|
|
75
|
+
for (const property of pattern.properties ?? []) {
|
|
76
|
+
if (property.type === 'RestElement') patternNames(property.argument, out)
|
|
77
|
+
else patternNames(property.value, out)
|
|
78
|
+
}
|
|
79
|
+
break
|
|
80
|
+
case 'ArrayPattern':
|
|
81
|
+
for (const element of pattern.elements ?? []) patternNames(element, out)
|
|
82
|
+
break
|
|
83
|
+
case 'RestElement':
|
|
84
|
+
patternNames(pattern.argument, out)
|
|
85
|
+
break
|
|
86
|
+
case 'AssignmentPattern':
|
|
87
|
+
patternNames(pattern.left, out)
|
|
88
|
+
break
|
|
89
|
+
case 'TSParameterProperty':
|
|
90
|
+
patternNames(pattern.parameter, out)
|
|
91
|
+
break
|
|
92
|
+
default:
|
|
93
|
+
break
|
|
94
|
+
}
|
|
95
|
+
return out
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Names declared directly in a statement list (let/const/var, functions, classes). */
|
|
99
|
+
function blockNames(statements) {
|
|
100
|
+
const names = new Set()
|
|
101
|
+
for (const statement of statements ?? []) {
|
|
102
|
+
const node =
|
|
103
|
+
statement?.type === 'ExportNamedDeclaration' || statement?.type === 'ExportDefaultDeclaration'
|
|
104
|
+
? statement.declaration
|
|
105
|
+
: statement
|
|
106
|
+
if (!node) continue
|
|
107
|
+
if (node.type === 'VariableDeclaration') {
|
|
108
|
+
for (const declarator of node.declarations ?? []) for (const id of patternNames(declarator.id)) names.add(id.name)
|
|
109
|
+
} else if ((node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') && node.id) {
|
|
110
|
+
names.add(node.id.name)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return names
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Name of what a top-level statement declares (`function Button`, `const frames`), or null. */
|
|
117
|
+
function statementName(statement) {
|
|
118
|
+
let node = statement
|
|
119
|
+
if (node?.type === 'ExportNamedDeclaration' || node?.type === 'ExportDefaultDeclaration') node = node.declaration
|
|
120
|
+
if (!node) return null
|
|
121
|
+
if ((node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') && node.id) return node.id.name
|
|
122
|
+
if (node.type === 'VariableDeclaration') {
|
|
123
|
+
const id = node.declarations?.[0]?.id
|
|
124
|
+
return id?.type === 'Identifier' ? id.name : null
|
|
125
|
+
}
|
|
126
|
+
return null
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const stringValue = (node) =>
|
|
130
|
+
node?.type === 'Literal' && typeof node.value === 'string'
|
|
131
|
+
? node.value
|
|
132
|
+
: node?.type === 'StringLiteral'
|
|
133
|
+
? node.value
|
|
134
|
+
: null
|
|
135
|
+
|
|
136
|
+
const nameOf = (node) => (node?.type === 'Identifier' || node?.type === 'JSXIdentifier' ? node.name : stringValue(node))
|
|
137
|
+
|
|
138
|
+
/** Static class words inside a JSX `className` value (string literals and template text). */
|
|
139
|
+
function classWordsOf(value) {
|
|
140
|
+
const words = []
|
|
141
|
+
const add = (text) => {
|
|
142
|
+
for (const word of String(text).split(/\s+/)) if (CLASS_WORD.test(word)) words.push(word)
|
|
143
|
+
}
|
|
144
|
+
const visit = (node) => {
|
|
145
|
+
if (!node || typeof node !== 'object') return
|
|
146
|
+
if (Array.isArray(node)) {
|
|
147
|
+
node.forEach(visit)
|
|
148
|
+
return
|
|
149
|
+
}
|
|
150
|
+
const text = stringValue(node)
|
|
151
|
+
if (text !== null) {
|
|
152
|
+
add(text)
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
if (node.type === 'TemplateLiteral') {
|
|
156
|
+
for (const quasi of node.quasis ?? []) add(quasi.value?.cooked ?? quasi.value?.raw ?? '')
|
|
157
|
+
}
|
|
158
|
+
for (const [key, child] of Object.entries(node)) {
|
|
159
|
+
if (!SKIP_KEYS.has(key) && child && typeof child === 'object') visit(child)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
visit(value)
|
|
163
|
+
return words
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Analyze one source file: imports, exports, top-level declarations, module specifiers, and
|
|
168
|
+
* scope-aware references to imported / top-level names.
|
|
169
|
+
*/
|
|
170
|
+
export function analyzeSource(file, content, ctx) {
|
|
171
|
+
const { program, errors, locationAt } = parseSourceAst(file, content, ctx)
|
|
172
|
+
const imports = new Map()
|
|
173
|
+
const exports = new Map()
|
|
174
|
+
const stars = []
|
|
175
|
+
const declarations = new Map()
|
|
176
|
+
const moduleRefs = []
|
|
177
|
+
const refs = []
|
|
178
|
+
const strings = []
|
|
179
|
+
const objectKeys = []
|
|
180
|
+
// Words a file can apply as class names: whitespace-separated words of its string
|
|
181
|
+
// literals and template text (className="…", cn('…'), `…`). Identifiers never count.
|
|
182
|
+
const classWords = new Set()
|
|
183
|
+
const addClassWords = (text) => {
|
|
184
|
+
for (const word of String(text).split(/\s+/)) if (CLASS_WORD.test(word)) classWords.add(word)
|
|
185
|
+
}
|
|
186
|
+
const body = program.body ?? []
|
|
187
|
+
const statementNames = body.map(statementName)
|
|
188
|
+
const at = (offset) => locationAt(offset)
|
|
189
|
+
|
|
190
|
+
const addModuleRef = (sourceNode, kind, statement) => {
|
|
191
|
+
const value = stringValue(sourceNode)
|
|
192
|
+
if (value === null) return
|
|
193
|
+
moduleRefs.push({ source: value, start: sourceNode.start, end: sourceNode.end, kind, statement })
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// ---- imports, exports and top-level declarations ------------------------------------
|
|
197
|
+
body.forEach((statement, index) => {
|
|
198
|
+
if (statement.type === 'ImportDeclaration') {
|
|
199
|
+
addModuleRef(statement.source, 'import', statement)
|
|
200
|
+
const typeOnly = statement.importKind === 'type'
|
|
201
|
+
for (const spec of statement.specifiers ?? []) {
|
|
202
|
+
const local = spec.local?.name
|
|
203
|
+
if (!local) continue
|
|
204
|
+
if (spec.type === 'ImportDefaultSpecifier') {
|
|
205
|
+
imports.set(local, { local, imported: 'default', source: stringValue(statement.source), spec, statement, typeOnly })
|
|
206
|
+
} else if (spec.type === 'ImportNamespaceSpecifier') {
|
|
207
|
+
imports.set(local, { local, imported: '*', source: stringValue(statement.source), spec, statement, typeOnly })
|
|
208
|
+
} else {
|
|
209
|
+
const imported = nameOf(spec.imported) ?? local
|
|
210
|
+
imports.set(local, {
|
|
211
|
+
local,
|
|
212
|
+
imported,
|
|
213
|
+
source: stringValue(statement.source),
|
|
214
|
+
spec,
|
|
215
|
+
statement,
|
|
216
|
+
aliased: spec.imported.start !== spec.local.start,
|
|
217
|
+
typeOnly: typeOnly || spec.importKind === 'type',
|
|
218
|
+
})
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return
|
|
222
|
+
}
|
|
223
|
+
if (statement.type === 'ExportAllDeclaration') {
|
|
224
|
+
addModuleRef(statement.source, 'export', statement)
|
|
225
|
+
const exported = nameOf(statement.exported)
|
|
226
|
+
if (exported) exports.set(exported, { kind: 'namespace', source: stringValue(statement.source), statement, node: statement.exported })
|
|
227
|
+
else stars.push({ source: stringValue(statement.source), statement })
|
|
228
|
+
return
|
|
229
|
+
}
|
|
230
|
+
if (statement.type === 'ExportNamedDeclaration') {
|
|
231
|
+
if (statement.source) {
|
|
232
|
+
addModuleRef(statement.source, 'export', statement)
|
|
233
|
+
for (const spec of statement.specifiers ?? []) {
|
|
234
|
+
const exported = nameOf(spec.exported)
|
|
235
|
+
const imported = nameOf(spec.local) ?? exported
|
|
236
|
+
if (!exported) continue
|
|
237
|
+
exports.set(exported, {
|
|
238
|
+
kind: 'reexport',
|
|
239
|
+
source: stringValue(statement.source),
|
|
240
|
+
imported,
|
|
241
|
+
spec,
|
|
242
|
+
statement,
|
|
243
|
+
aliased: spec.exported.start !== spec.local.start,
|
|
244
|
+
})
|
|
245
|
+
}
|
|
246
|
+
return
|
|
247
|
+
}
|
|
248
|
+
for (const spec of statement.specifiers ?? []) {
|
|
249
|
+
const exported = nameOf(spec.exported)
|
|
250
|
+
const local = nameOf(spec.local)
|
|
251
|
+
if (!exported || !local) continue
|
|
252
|
+
exports.set(exported, { kind: 'local', local, spec, statement, aliased: spec.exported.start !== spec.local.start })
|
|
253
|
+
}
|
|
254
|
+
const declaration = statement.declaration
|
|
255
|
+
if (declaration) {
|
|
256
|
+
if ((declaration.type === 'FunctionDeclaration' || declaration.type === 'ClassDeclaration') && declaration.id) {
|
|
257
|
+
exports.set(declaration.id.name, { kind: 'local', local: declaration.id.name, statement, declared: true })
|
|
258
|
+
} else if (declaration.type === 'VariableDeclaration') {
|
|
259
|
+
for (const declarator of declaration.declarations ?? []) {
|
|
260
|
+
for (const id of patternNames(declarator.id)) {
|
|
261
|
+
exports.set(id.name, { kind: 'local', local: id.name, statement, declared: true })
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (statement.type === 'ExportDefaultDeclaration') {
|
|
268
|
+
const declaration = statement.declaration
|
|
269
|
+
const local =
|
|
270
|
+
declaration?.type === 'Identifier'
|
|
271
|
+
? declaration.name
|
|
272
|
+
: (declaration?.type === 'FunctionDeclaration' || declaration?.type === 'ClassDeclaration') && declaration.id
|
|
273
|
+
? declaration.id.name
|
|
274
|
+
: null
|
|
275
|
+
exports.set('default', { kind: 'local', local, statement, declared: Boolean(declaration?.id), anonymous: !local })
|
|
276
|
+
}
|
|
277
|
+
// Top-level declarations (exported or not).
|
|
278
|
+
let node = statement
|
|
279
|
+
if (node.type === 'ExportNamedDeclaration' || node.type === 'ExportDefaultDeclaration') node = node.declaration
|
|
280
|
+
if (!node) return
|
|
281
|
+
if ((node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') && node.id) {
|
|
282
|
+
declarations.set(node.id.name, { name: node.id.name, id: node.id, statement, index })
|
|
283
|
+
} else if (node.type === 'VariableDeclaration') {
|
|
284
|
+
for (const declarator of node.declarations ?? []) {
|
|
285
|
+
for (const id of patternNames(declarator.id)) declarations.set(id.name, { name: id.name, id, statement, index, declarator })
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
// ---- references ----------------------------------------------------------------------
|
|
291
|
+
const interesting = (name) => imports.has(name) || declarations.has(name)
|
|
292
|
+
const shadowed = (scopes, name) => scopes.some((scope) => scope.has(name))
|
|
293
|
+
let topIndex = -1
|
|
294
|
+
const record = (node, kind, scopes, extra = {}) => {
|
|
295
|
+
const name = node.name
|
|
296
|
+
if (!interesting(name) || shadowed(scopes, name)) return
|
|
297
|
+
refs.push({
|
|
298
|
+
name,
|
|
299
|
+
start: node.start,
|
|
300
|
+
end: node.end,
|
|
301
|
+
kind,
|
|
302
|
+
top: topIndex,
|
|
303
|
+
topName: statementNames[topIndex] ?? null,
|
|
304
|
+
...at(node.start),
|
|
305
|
+
...extra,
|
|
306
|
+
})
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const visitPattern = (pattern, scopes) => {
|
|
310
|
+
if (!pattern || typeof pattern !== 'object') return
|
|
311
|
+
switch (pattern.type) {
|
|
312
|
+
case 'Identifier':
|
|
313
|
+
if (pattern.typeAnnotation) visit(pattern.typeAnnotation, pattern, 'typeAnnotation', scopes)
|
|
314
|
+
return
|
|
315
|
+
case 'ObjectPattern':
|
|
316
|
+
for (const property of pattern.properties ?? []) {
|
|
317
|
+
if (property.type === 'RestElement') visitPattern(property.argument, scopes)
|
|
318
|
+
else {
|
|
319
|
+
if (property.computed) visit(property.key, property, 'key', scopes)
|
|
320
|
+
visitPattern(property.value, scopes)
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (pattern.typeAnnotation) visit(pattern.typeAnnotation, pattern, 'typeAnnotation', scopes)
|
|
324
|
+
return
|
|
325
|
+
case 'ArrayPattern':
|
|
326
|
+
for (const element of pattern.elements ?? []) visitPattern(element, scopes)
|
|
327
|
+
if (pattern.typeAnnotation) visit(pattern.typeAnnotation, pattern, 'typeAnnotation', scopes)
|
|
328
|
+
return
|
|
329
|
+
case 'RestElement':
|
|
330
|
+
visitPattern(pattern.argument, scopes)
|
|
331
|
+
return
|
|
332
|
+
case 'AssignmentPattern':
|
|
333
|
+
visitPattern(pattern.left, scopes)
|
|
334
|
+
visit(pattern.right, pattern, 'right', scopes)
|
|
335
|
+
return
|
|
336
|
+
case 'TSParameterProperty':
|
|
337
|
+
visitPattern(pattern.parameter, scopes)
|
|
338
|
+
return
|
|
339
|
+
default:
|
|
340
|
+
visit(pattern, null, null, scopes)
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const visitChildren = (node, scopes, skip) => {
|
|
345
|
+
for (const [key, value] of Object.entries(node)) {
|
|
346
|
+
if (SKIP_KEYS.has(key) || (skip && skip.has(key))) continue
|
|
347
|
+
if (value && typeof value === 'object') visit(value, node, key, scopes)
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const jsxName = (nameNode, scopes, kind, classNames) => {
|
|
352
|
+
if (!nameNode) return
|
|
353
|
+
if (nameNode.type === 'JSXIdentifier') {
|
|
354
|
+
// Lower-case tags are intrinsic elements, not bindings.
|
|
355
|
+
if (/^[a-z]/.test(nameNode.name) || nameNode.name.includes('-')) return
|
|
356
|
+
record(nameNode, kind, scopes, classNames ? { classNames } : {})
|
|
357
|
+
return
|
|
358
|
+
}
|
|
359
|
+
if (nameNode.type === 'JSXMemberExpression') {
|
|
360
|
+
let object = nameNode
|
|
361
|
+
let property = null
|
|
362
|
+
while (object.type === 'JSXMemberExpression') {
|
|
363
|
+
property = object.property
|
|
364
|
+
object = object.object
|
|
365
|
+
}
|
|
366
|
+
if (object.type === 'JSXIdentifier' && property) {
|
|
367
|
+
record(object, kind, scopes, {
|
|
368
|
+
member: { name: property.name, start: property.start, end: property.end },
|
|
369
|
+
...(classNames ? { classNames } : {}),
|
|
370
|
+
})
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function visit(node, parent, key, scopes) {
|
|
376
|
+
if (!node || typeof node !== 'object') return
|
|
377
|
+
if (Array.isArray(node)) {
|
|
378
|
+
for (const item of node) visit(item, parent, key, scopes)
|
|
379
|
+
return
|
|
380
|
+
}
|
|
381
|
+
if (typeof node.type !== 'string') return
|
|
382
|
+
const type = node.type
|
|
383
|
+
|
|
384
|
+
if (FUNCTION_TYPES.has(type)) {
|
|
385
|
+
const scope = new Set()
|
|
386
|
+
if (type === 'FunctionExpression' && node.id) scope.add(node.id.name)
|
|
387
|
+
for (const param of node.params ?? []) for (const id of patternNames(param)) scope.add(id.name)
|
|
388
|
+
const inner = [...scopes, scope]
|
|
389
|
+
for (const param of node.params ?? []) visitPattern(param, inner)
|
|
390
|
+
if (node.returnType) visit(node.returnType, node, 'returnType', inner)
|
|
391
|
+
if (node.body) visit(node.body, node, 'body', inner)
|
|
392
|
+
return
|
|
393
|
+
}
|
|
394
|
+
switch (type) {
|
|
395
|
+
case 'ImportDeclaration':
|
|
396
|
+
case 'ExportAllDeclaration':
|
|
397
|
+
case 'TSImportEqualsDeclaration':
|
|
398
|
+
case 'MetaProperty':
|
|
399
|
+
case 'JSXNamespacedName':
|
|
400
|
+
return
|
|
401
|
+
case 'ExportNamedDeclaration':
|
|
402
|
+
if (node.declaration) visit(node.declaration, node, 'declaration', scopes)
|
|
403
|
+
if (!node.source) {
|
|
404
|
+
for (const spec of node.specifiers ?? []) {
|
|
405
|
+
if (spec.local?.type === 'Identifier') record(spec.local, 'export', scopes, { spec })
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
return
|
|
409
|
+
case 'ExportDefaultDeclaration':
|
|
410
|
+
if (node.declaration?.type === 'Identifier') record(node.declaration, 'export-default', scopes)
|
|
411
|
+
else visit(node.declaration, node, 'declaration', scopes)
|
|
412
|
+
return
|
|
413
|
+
case 'ClassDeclaration':
|
|
414
|
+
case 'ClassExpression': {
|
|
415
|
+
const inner = type === 'ClassExpression' && node.id ? [...scopes, new Set([node.id.name])] : scopes
|
|
416
|
+
visitChildren(node, inner, new Set(['id']))
|
|
417
|
+
return
|
|
418
|
+
}
|
|
419
|
+
case 'BlockStatement':
|
|
420
|
+
case 'StaticBlock':
|
|
421
|
+
case 'TSModuleBlock': {
|
|
422
|
+
const inner = [...scopes, blockNames(node.body)]
|
|
423
|
+
visit(node.body, node, 'body', inner)
|
|
424
|
+
return
|
|
425
|
+
}
|
|
426
|
+
case 'ForStatement':
|
|
427
|
+
case 'ForInStatement':
|
|
428
|
+
case 'ForOfStatement': {
|
|
429
|
+
const head = type === 'ForStatement' ? node.init : node.left
|
|
430
|
+
const scope = new Set()
|
|
431
|
+
if (head?.type === 'VariableDeclaration') {
|
|
432
|
+
for (const declarator of head.declarations ?? []) for (const id of patternNames(declarator.id)) scope.add(id.name)
|
|
433
|
+
}
|
|
434
|
+
visitChildren(node, [...scopes, scope])
|
|
435
|
+
return
|
|
436
|
+
}
|
|
437
|
+
case 'CatchClause': {
|
|
438
|
+
const scope = new Set(patternNames(node.param).map((id) => id.name))
|
|
439
|
+
const inner = [...scopes, scope]
|
|
440
|
+
visitPattern(node.param, inner)
|
|
441
|
+
visit(node.body, node, 'body', inner)
|
|
442
|
+
return
|
|
443
|
+
}
|
|
444
|
+
case 'VariableDeclaration':
|
|
445
|
+
for (const declarator of node.declarations ?? []) {
|
|
446
|
+
visitPattern(declarator.id, scopes)
|
|
447
|
+
if (declarator.init) visit(declarator.init, declarator, 'init', scopes)
|
|
448
|
+
}
|
|
449
|
+
return
|
|
450
|
+
case 'Identifier':
|
|
451
|
+
record(node, parent?.type?.startsWith('TS') && key === 'exprName' ? 'type' : 'ref', scopes)
|
|
452
|
+
return
|
|
453
|
+
case 'JSXOpeningElement': {
|
|
454
|
+
const attribute = (node.attributes ?? []).find(
|
|
455
|
+
(item) => item.type === 'JSXAttribute' && item.name?.type === 'JSXIdentifier' && item.name.name === 'className',
|
|
456
|
+
)
|
|
457
|
+
jsxName(node.name, scopes, 'jsx', attribute ? classWordsOf(attribute.value) : [])
|
|
458
|
+
visit(node.attributes, node, 'attributes', scopes)
|
|
459
|
+
if (node.typeArguments) visit(node.typeArguments, node, 'typeArguments', scopes)
|
|
460
|
+
if (node.typeParameters) visit(node.typeParameters, node, 'typeParameters', scopes)
|
|
461
|
+
return
|
|
462
|
+
}
|
|
463
|
+
case 'JSXClosingElement':
|
|
464
|
+
jsxName(node.name, scopes, 'jsx-close')
|
|
465
|
+
return
|
|
466
|
+
case 'JSXAttribute':
|
|
467
|
+
if (node.value) visit(node.value, node, 'value', scopes)
|
|
468
|
+
return
|
|
469
|
+
case 'MemberExpression':
|
|
470
|
+
case 'OptionalMemberExpression': {
|
|
471
|
+
if (node.object?.type === 'Identifier') {
|
|
472
|
+
const property = node.property
|
|
473
|
+
const member = !node.computed && property?.type === 'Identifier'
|
|
474
|
+
? { name: property.name, start: property.start, end: property.end }
|
|
475
|
+
: node.computed && stringValue(property) !== null
|
|
476
|
+
? { name: stringValue(property), start: property.start, end: property.end, computed: true }
|
|
477
|
+
: null
|
|
478
|
+
record(node.object, 'ref', scopes, member ? { member } : {})
|
|
479
|
+
} else {
|
|
480
|
+
visit(node.object, node, 'object', scopes)
|
|
481
|
+
}
|
|
482
|
+
if (node.computed) visit(node.property, node, 'property', scopes)
|
|
483
|
+
return
|
|
484
|
+
}
|
|
485
|
+
case 'Property':
|
|
486
|
+
case 'ObjectProperty':
|
|
487
|
+
if (node.computed) visit(node.key, node, 'key', scopes)
|
|
488
|
+
else if (!node.shorthand && node.key) {
|
|
489
|
+
const keyName = nameOf(node.key)
|
|
490
|
+
if (keyName && PASCAL.test(keyName)) objectKeys.push({ name: keyName, start: node.key.start, end: node.key.end, ...at(node.key.start) })
|
|
491
|
+
}
|
|
492
|
+
if (node.shorthand && node.value?.type === 'Identifier') {
|
|
493
|
+
record(node.value, 'shorthand', scopes)
|
|
494
|
+
return
|
|
495
|
+
}
|
|
496
|
+
if (node.shorthand && node.value?.type === 'AssignmentPattern') {
|
|
497
|
+
visitPattern(node.value, scopes)
|
|
498
|
+
return
|
|
499
|
+
}
|
|
500
|
+
visit(node.value, node, 'value', scopes)
|
|
501
|
+
return
|
|
502
|
+
case 'TSTypeQuery': {
|
|
503
|
+
let name = node.exprName
|
|
504
|
+
let member = null
|
|
505
|
+
while (name?.type === 'TSQualifiedName') {
|
|
506
|
+
member = name.right
|
|
507
|
+
name = name.left
|
|
508
|
+
}
|
|
509
|
+
if (name?.type === 'Identifier') {
|
|
510
|
+
record(name, 'type', scopes, member ? { member: { name: member.name, start: member.start, end: member.end } } : {})
|
|
511
|
+
}
|
|
512
|
+
if (node.typeArguments) visit(node.typeArguments, node, 'typeArguments', scopes)
|
|
513
|
+
return
|
|
514
|
+
}
|
|
515
|
+
case 'TSTypeReference': {
|
|
516
|
+
let name = node.typeName
|
|
517
|
+
let member = null
|
|
518
|
+
while (name?.type === 'TSQualifiedName') {
|
|
519
|
+
member = name.right
|
|
520
|
+
name = name.left
|
|
521
|
+
}
|
|
522
|
+
if (name?.type === 'Identifier') {
|
|
523
|
+
record(name, 'type', scopes, member ? { member: { name: member.name, start: member.start, end: member.end } } : {})
|
|
524
|
+
}
|
|
525
|
+
if (node.typeArguments) visit(node.typeArguments, node, 'typeArguments', scopes)
|
|
526
|
+
if (node.typeParameters) visit(node.typeParameters, node, 'typeParameters', scopes)
|
|
527
|
+
return
|
|
528
|
+
}
|
|
529
|
+
case 'TSQualifiedName':
|
|
530
|
+
return
|
|
531
|
+
case 'LabeledStatement':
|
|
532
|
+
visit(node.body, node, 'body', scopes)
|
|
533
|
+
return
|
|
534
|
+
case 'BreakStatement':
|
|
535
|
+
case 'ContinueStatement':
|
|
536
|
+
return
|
|
537
|
+
case 'TSEnumMember':
|
|
538
|
+
if (node.initializer) visit(node.initializer, node, 'initializer', scopes)
|
|
539
|
+
return
|
|
540
|
+
case 'TSTypeParameter':
|
|
541
|
+
if (node.constraint) visit(node.constraint, node, 'constraint', scopes)
|
|
542
|
+
if (node.default) visit(node.default, node, 'default', scopes)
|
|
543
|
+
return
|
|
544
|
+
case 'ImportExpression':
|
|
545
|
+
addModuleRef(node.source, 'dynamic', null)
|
|
546
|
+
visitChildren(node, scopes, new Set(['source']))
|
|
547
|
+
return
|
|
548
|
+
case 'CallExpression':
|
|
549
|
+
if (node.callee?.type === 'Import') {
|
|
550
|
+
addModuleRef(node.arguments?.[0], 'dynamic', null)
|
|
551
|
+
return
|
|
552
|
+
}
|
|
553
|
+
visitChildren(node, scopes)
|
|
554
|
+
return
|
|
555
|
+
case 'Literal':
|
|
556
|
+
case 'StringLiteral':
|
|
557
|
+
if (typeof node.value === 'string') {
|
|
558
|
+
addClassWords(node.value)
|
|
559
|
+
if (PASCAL.test(node.value)) strings.push({ value: node.value, start: node.start, end: node.end, ...at(node.start) })
|
|
560
|
+
}
|
|
561
|
+
return
|
|
562
|
+
case 'TemplateElement':
|
|
563
|
+
addClassWords(node.value?.cooked ?? node.value?.raw ?? '')
|
|
564
|
+
return
|
|
565
|
+
default:
|
|
566
|
+
break
|
|
567
|
+
}
|
|
568
|
+
if (TS_NAMED_DECLARATIONS.has(type)) {
|
|
569
|
+
visitChildren(node, scopes, new Set(['id']))
|
|
570
|
+
return
|
|
571
|
+
}
|
|
572
|
+
if (KEYED_MEMBERS.has(type)) {
|
|
573
|
+
if (node.computed) visit(node.key, node, 'key', scopes)
|
|
574
|
+
visitChildren(node, scopes, new Set(['key']))
|
|
575
|
+
return
|
|
576
|
+
}
|
|
577
|
+
visitChildren(node, scopes)
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
body.forEach((statement, index) => {
|
|
581
|
+
topIndex = index
|
|
582
|
+
visit(statement, program, 'body', [])
|
|
583
|
+
})
|
|
584
|
+
|
|
585
|
+
return {
|
|
586
|
+
file,
|
|
587
|
+
content,
|
|
588
|
+
errors: errors ?? [],
|
|
589
|
+
imports,
|
|
590
|
+
exports,
|
|
591
|
+
stars,
|
|
592
|
+
declarations,
|
|
593
|
+
moduleRefs,
|
|
594
|
+
refs,
|
|
595
|
+
strings,
|
|
596
|
+
objectKeys,
|
|
597
|
+
classWords,
|
|
598
|
+
statementNames,
|
|
599
|
+
body,
|
|
600
|
+
locationAt,
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Exported components a file declares itself (`export function Button`, `export { Button }`,
|
|
606
|
+
* `export default function Button`, an anonymous default named after the file), in
|
|
607
|
+
* statement order: `[{ name, exportName, local, line, statementIndex }]`.
|
|
608
|
+
*/
|
|
609
|
+
export function localComponentExports(info, file = info.file) {
|
|
610
|
+
const found = []
|
|
611
|
+
for (const [exportName, entry] of info.exports) {
|
|
612
|
+
if (entry.kind !== 'local') continue
|
|
613
|
+
const declared = entry.local ? info.declarations.get(entry.local) : null
|
|
614
|
+
if (entry.local && !declared) continue
|
|
615
|
+
const name = exportName === 'default' ? entry.local ?? basename(file, extname(file)) : exportName
|
|
616
|
+
if (!isComponentName(name)) continue
|
|
617
|
+
const position = declared ? info.locationAt(declared.id.start) : info.locationAt(entry.statement.start)
|
|
618
|
+
found.push({
|
|
619
|
+
name,
|
|
620
|
+
exportName,
|
|
621
|
+
local: entry.local,
|
|
622
|
+
line: position.line,
|
|
623
|
+
statementIndex: declared ? declared.index : info.body.indexOf(entry.statement),
|
|
624
|
+
})
|
|
625
|
+
}
|
|
626
|
+
return found.sort((a, b) => a.statementIndex - b.statementIndex || a.name.localeCompare(b.name))
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// ---------------------------------------------------------------------------------------
|
|
630
|
+
// Module resolution
|
|
631
|
+
|
|
632
|
+
function readJsonc(file) {
|
|
633
|
+
try {
|
|
634
|
+
const text = readFileSync(file, 'utf8')
|
|
635
|
+
.replace(/\/\*[\s\S]*?\*\//g, '')
|
|
636
|
+
.replace(/(^|[^:"'\\])\/\/.*$/gm, '$1')
|
|
637
|
+
.replace(/,(\s*[}\]])/g, '$1')
|
|
638
|
+
return JSON.parse(text)
|
|
639
|
+
} catch {
|
|
640
|
+
return null
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/** `compilerOptions.paths` of the project's tsconfig.json (following one `extends` level per file). */
|
|
645
|
+
function tsconfigPaths(root) {
|
|
646
|
+
const seen = new Set()
|
|
647
|
+
const load = (file) => {
|
|
648
|
+
if (!file || seen.has(file) || !existsSync(file)) return null
|
|
649
|
+
seen.add(file)
|
|
650
|
+
const json = readJsonc(file)
|
|
651
|
+
if (!json) return null
|
|
652
|
+
const options = json.compilerOptions ?? {}
|
|
653
|
+
if (options.paths) {
|
|
654
|
+
return { baseUrl: resolve(dirname(file), options.baseUrl ?? '.'), paths: options.paths }
|
|
655
|
+
}
|
|
656
|
+
const parent = typeof json.extends === 'string' && json.extends.startsWith('.') ? resolve(dirname(file), json.extends) : null
|
|
657
|
+
return load(parent && !parent.endsWith('.json') ? `${parent}.json` : parent)
|
|
658
|
+
}
|
|
659
|
+
return load(join(root, 'tsconfig.json'))
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
const isFile = (file) => {
|
|
663
|
+
try {
|
|
664
|
+
return statSync(file).isFile()
|
|
665
|
+
} catch {
|
|
666
|
+
return false
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** First existing source file for `base` (exact, with an extension, or an index file). */
|
|
671
|
+
function probe(base) {
|
|
672
|
+
if (isFile(base) && CODE_FILE.test(base) && !DECLARATION_FILE.test(base)) return base
|
|
673
|
+
// ESM-style `./Button.js` pointing at `Button.tsx`.
|
|
674
|
+
const ext = extname(base)
|
|
675
|
+
if (/^\.[cm]?jsx?$/.test(ext)) {
|
|
676
|
+
const stem = base.slice(0, -ext.length)
|
|
677
|
+
for (const candidate of [`${stem}.tsx`, `${stem}.ts`, `${stem}.mts`, `${stem}.cts`]) if (isFile(candidate)) return candidate
|
|
678
|
+
}
|
|
679
|
+
for (const suffix of [...RESOLVE_EXT, ...INDEX_FILES]) {
|
|
680
|
+
if (isFile(`${base}${suffix}`)) return `${base}${suffix}`
|
|
681
|
+
}
|
|
682
|
+
return null
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
const realOf = (file) => {
|
|
686
|
+
try {
|
|
687
|
+
return realpathSync(file)
|
|
688
|
+
} catch {
|
|
689
|
+
return file
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/** Candidate targets of a package `exports` entry, most source-like condition first. */
|
|
694
|
+
function exportTargets(entry) {
|
|
695
|
+
if (typeof entry === 'string') return [entry]
|
|
696
|
+
if (Array.isArray(entry)) return entry.flatMap(exportTargets)
|
|
697
|
+
if (!entry || typeof entry !== 'object') return []
|
|
698
|
+
const order = ['source', 'development', 'import', 'module', 'default', 'require', 'node', 'browser', 'types']
|
|
699
|
+
const keys = [...order.filter((key) => key in entry), ...Object.keys(entry).filter((key) => !order.includes(key))]
|
|
700
|
+
return keys.flatMap((key) => exportTargets(entry[key]))
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/** A built file (`dist/x.js`, `dist/x.d.ts`) mapped back to its `src/` source, if it exists. */
|
|
704
|
+
function sourceOfBuilt(pkgDir, target) {
|
|
705
|
+
const rel = toPosix(relative(pkgDir, target))
|
|
706
|
+
const match = /^(?:dist|build|lib|esm|cjs|out)\/(.+?)(?:\.d)?\.[cm]?[jt]sx?$/.exec(rel)
|
|
707
|
+
if (!match) return null
|
|
708
|
+
return probe(join(pkgDir, 'src', match[1]))
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
function resolvePackage(fromDir, spec) {
|
|
712
|
+
const parts = spec.split('/')
|
|
713
|
+
const name = spec.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0]
|
|
714
|
+
const subpath = spec.slice(name.length) // '' or '/x'
|
|
715
|
+
let dir = fromDir
|
|
716
|
+
for (;;) {
|
|
717
|
+
const candidate = join(dir, 'node_modules', name)
|
|
718
|
+
if (existsSync(join(candidate, 'package.json'))) {
|
|
719
|
+
const pkgDir = realOf(candidate)
|
|
720
|
+
const pkg = readJsonc(join(pkgDir, 'package.json')) ?? {}
|
|
721
|
+
const key = subpath ? `.${subpath}` : '.'
|
|
722
|
+
const targets = []
|
|
723
|
+
if (pkg.exports !== undefined) {
|
|
724
|
+
const map = typeof pkg.exports === 'string' || Array.isArray(pkg.exports) || !Object.keys(pkg.exports).some((k) => k.startsWith('.'))
|
|
725
|
+
? { '.': pkg.exports }
|
|
726
|
+
: pkg.exports
|
|
727
|
+
if (map[key] !== undefined) targets.push(...exportTargets(map[key]))
|
|
728
|
+
else {
|
|
729
|
+
for (const [pattern, value] of Object.entries(map)) {
|
|
730
|
+
const star = pattern.indexOf('*')
|
|
731
|
+
if (star < 0) continue
|
|
732
|
+
const prefix = pattern.slice(0, star)
|
|
733
|
+
const suffix = pattern.slice(star + 1)
|
|
734
|
+
if (key.startsWith(prefix) && key.endsWith(suffix)) {
|
|
735
|
+
const middle = key.slice(prefix.length, key.length - suffix.length)
|
|
736
|
+
targets.push(...exportTargets(value).map((target) => target.replace('*', middle)))
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
} else if (!subpath) {
|
|
741
|
+
targets.push(...[pkg.source, pkg.module, pkg.main, pkg.types].filter((value) => typeof value === 'string'))
|
|
742
|
+
targets.push('index')
|
|
743
|
+
} else {
|
|
744
|
+
targets.push(`.${subpath}`)
|
|
745
|
+
}
|
|
746
|
+
if (!subpath && typeof pkg.source === 'string') targets.unshift(pkg.source)
|
|
747
|
+
for (const target of targets) {
|
|
748
|
+
const abs = resolve(pkgDir, target)
|
|
749
|
+
const direct = probe(abs)
|
|
750
|
+
if (direct && !/[\\/](?:dist|build|lib|esm|cjs|out)[\\/]/.test(relative(pkgDir, direct))) return direct
|
|
751
|
+
const source = sourceOfBuilt(pkgDir, abs)
|
|
752
|
+
if (source) return source
|
|
753
|
+
}
|
|
754
|
+
return null
|
|
755
|
+
}
|
|
756
|
+
const parent = dirname(dir)
|
|
757
|
+
if (parent === dir) return null
|
|
758
|
+
dir = parent
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/** Resolver for module specifiers, shared by the index and the rewriter. */
|
|
763
|
+
export function createResolver(root) {
|
|
764
|
+
const paths = tsconfigPaths(root)
|
|
765
|
+
const cache = new Map()
|
|
766
|
+
return (fromAbs, spec) => {
|
|
767
|
+
if (typeof spec !== 'string' || !spec) return null
|
|
768
|
+
const key = `${dirname(fromAbs)}\0${spec}`
|
|
769
|
+
if (cache.has(key)) return cache.get(key)
|
|
770
|
+
let result = null
|
|
771
|
+
if (spec.startsWith('.')) result = probe(resolve(dirname(fromAbs), spec))
|
|
772
|
+
else if (spec.startsWith('/')) result = probe(join(root, spec))
|
|
773
|
+
else {
|
|
774
|
+
if (paths) {
|
|
775
|
+
for (const [pattern, targets] of Object.entries(paths)) {
|
|
776
|
+
const star = pattern.indexOf('*')
|
|
777
|
+
const matches = star < 0 ? spec === pattern : spec.startsWith(pattern.slice(0, star)) && spec.endsWith(pattern.slice(star + 1))
|
|
778
|
+
if (!matches) continue
|
|
779
|
+
const middle = star < 0 ? '' : spec.slice(star, spec.length - (pattern.length - star - 1))
|
|
780
|
+
for (const target of targets) {
|
|
781
|
+
result = probe(resolve(paths.baseUrl, target.replace('*', middle)))
|
|
782
|
+
if (result) break
|
|
783
|
+
}
|
|
784
|
+
if (result) break
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
if (!result && !isAbsolute(spec)) result = resolvePackage(dirname(fromAbs), spec)
|
|
788
|
+
}
|
|
789
|
+
result = result ? realOf(result) : null
|
|
790
|
+
cache.set(key, result)
|
|
791
|
+
return result
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
// ---------------------------------------------------------------------------------------
|
|
796
|
+
// Project index
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* Index every code file in `files` ([{ abs, file }], root-relative posix `file`).
|
|
800
|
+
* `designDir` (absolute) marks design files: components declared there are not part of the
|
|
801
|
+
* catalog, and uses there are "page" uses.
|
|
802
|
+
*/
|
|
803
|
+
export function buildComponentIndex({ root, files, designDir = join(root, 'design'), ctx = { parseCache: new Map() } }) {
|
|
804
|
+
const resolveSpec = createResolver(root)
|
|
805
|
+
const infos = new Map()
|
|
806
|
+
const relOf = new Map()
|
|
807
|
+
for (const entry of files) {
|
|
808
|
+
if (!CODE_FILE.test(entry.abs) || DECLARATION_FILE.test(entry.abs)) continue
|
|
809
|
+
let content
|
|
810
|
+
try {
|
|
811
|
+
content = readFileSync(entry.abs, 'utf8')
|
|
812
|
+
} catch {
|
|
813
|
+
continue
|
|
814
|
+
}
|
|
815
|
+
const abs = realOf(entry.abs)
|
|
816
|
+
const info = analyzeSource(entry.file, content, ctx)
|
|
817
|
+
info.abs = abs
|
|
818
|
+
info.design = abs.startsWith(`${realOf(designDir)}${sep}`)
|
|
819
|
+
infos.set(abs, info)
|
|
820
|
+
relOf.set(abs, entry.file)
|
|
821
|
+
}
|
|
822
|
+
const targetOf = (info, source) => {
|
|
823
|
+
if (source === null || source === undefined) return null
|
|
824
|
+
const key = `target\0${source}`
|
|
825
|
+
if (!info[key]) info[key] = { value: resolveSpec(info.abs, source) }
|
|
826
|
+
return info[key].value
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
/** The declaring file and local name an export resolves to, following re-exports. */
|
|
830
|
+
const resolveExport = (abs, name, seen = new Set()) => {
|
|
831
|
+
const info = infos.get(abs)
|
|
832
|
+
if (!info) return null
|
|
833
|
+
const visitKey = `${abs}\0${name}`
|
|
834
|
+
if (seen.has(visitKey)) return null
|
|
835
|
+
seen.add(visitKey)
|
|
836
|
+
const entry = info.exports.get(name)
|
|
837
|
+
if (entry) {
|
|
838
|
+
if (entry.kind === 'local') {
|
|
839
|
+
if (!entry.local) return name === 'default' ? { abs, local: null, exportName: 'default' } : null
|
|
840
|
+
return resolveLocalInner(abs, entry.local, seen)
|
|
841
|
+
}
|
|
842
|
+
if (entry.kind === 'reexport') {
|
|
843
|
+
const target = targetOf(info, entry.source)
|
|
844
|
+
return target ? resolveExport(target, entry.imported, seen) : null
|
|
845
|
+
}
|
|
846
|
+
return null
|
|
847
|
+
}
|
|
848
|
+
if (name === 'default') return null
|
|
849
|
+
for (const star of info.stars) {
|
|
850
|
+
const target = targetOf(info, star.source)
|
|
851
|
+
const found = target ? resolveExport(target, name, seen) : null
|
|
852
|
+
if (found) return found
|
|
853
|
+
}
|
|
854
|
+
return null
|
|
855
|
+
}
|
|
856
|
+
const resolveLocalInner = (abs, local, seen = new Set()) => {
|
|
857
|
+
const info = infos.get(abs)
|
|
858
|
+
if (!info) return null
|
|
859
|
+
if (info.declarations.has(local)) return { abs, local }
|
|
860
|
+
const imported = info.imports.get(local)
|
|
861
|
+
if (!imported || imported.imported === '*') return null
|
|
862
|
+
const target = targetOf(info, imported.source)
|
|
863
|
+
return target ? resolveExport(target, imported.imported, seen) : null
|
|
864
|
+
}
|
|
865
|
+
const keyOf = (target) => (target ? `${target.abs}#${target.local ?? 'default'}` : null)
|
|
866
|
+
|
|
867
|
+
/** What a reference in `abs` points at: { abs, local } of the declaration, or null. */
|
|
868
|
+
const resolveRef = (abs, ref) => {
|
|
869
|
+
const info = infos.get(abs)
|
|
870
|
+
const imported = info?.imports.get(ref.name)
|
|
871
|
+
if (imported?.imported === '*') {
|
|
872
|
+
if (!ref.member) return null
|
|
873
|
+
const target = targetOf(info, imported.source)
|
|
874
|
+
return target ? resolveExport(target, ref.member.name) : null
|
|
875
|
+
}
|
|
876
|
+
return resolveLocalInner(abs, ref.name)
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
// ---- definitions -------------------------------------------------------------------
|
|
880
|
+
const definitions = []
|
|
881
|
+
for (const [abs, info] of infos) {
|
|
882
|
+
if (!COMPONENT_FILE.test(abs)) continue
|
|
883
|
+
for (const item of localComponentExports(info, abs)) {
|
|
884
|
+
definitions.push({ ...item, abs, file: relOf.get(abs), design: info.design })
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
definitions.sort((a, b) => Number(a.design) - Number(b.design) || a.file.localeCompare(b.file) || a.line - b.line)
|
|
888
|
+
const byName = new Map()
|
|
889
|
+
for (const definition of definitions) if (!byName.has(definition.name)) byName.set(definition.name, definition)
|
|
890
|
+
const definitionKey = (definition) => `${definition.abs}#${definition.local ?? 'default'}`
|
|
891
|
+
|
|
892
|
+
// ---- references, by the declaration they resolve to --------------------------------
|
|
893
|
+
let refIndex = null
|
|
894
|
+
const buildRefIndex = () => {
|
|
895
|
+
refIndex = new Map()
|
|
896
|
+
const add = (key, entry) => {
|
|
897
|
+
if (!key) return
|
|
898
|
+
const list = refIndex.get(key) ?? []
|
|
899
|
+
list.push(entry)
|
|
900
|
+
refIndex.set(key, list)
|
|
901
|
+
}
|
|
902
|
+
for (const [abs, info] of infos) {
|
|
903
|
+
for (const ref of info.refs) {
|
|
904
|
+
const target = resolveRef(abs, ref)
|
|
905
|
+
add(keyOf(target), { abs, file: relOf.get(abs), info, ref, viaNamespace: info.imports.get(ref.name)?.imported === '*' })
|
|
906
|
+
}
|
|
907
|
+
for (const moduleRef of info.moduleRefs) {
|
|
908
|
+
if (moduleRef.kind !== 'dynamic') continue
|
|
909
|
+
const target = targetOf(info, moduleRef.source)
|
|
910
|
+
if (!target) continue
|
|
911
|
+
const targetInfo = infos.get(target)
|
|
912
|
+
for (const [exportName] of targetInfo?.exports ?? []) {
|
|
913
|
+
const resolved = resolveExport(target, exportName)
|
|
914
|
+
add(keyOf(resolved), {
|
|
915
|
+
abs,
|
|
916
|
+
file: relOf.get(abs),
|
|
917
|
+
info,
|
|
918
|
+
ref: { name: exportName, start: moduleRef.start, end: moduleRef.end, kind: 'dynamic-import', top: -1, topName: null, ...info.locationAt(moduleRef.start) },
|
|
919
|
+
dynamic: true,
|
|
920
|
+
})
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
const referencesOf = (definition) => {
|
|
926
|
+
if (!refIndex) buildRefIndex()
|
|
927
|
+
return refIndex.get(definitionKey(definition)) ?? []
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
/**
|
|
931
|
+
* Uses of a component: every reference outside its own declaration, minus re-export
|
|
932
|
+
* plumbing (`export { Button }`) and statements in its own file that only decorate it
|
|
933
|
+
* (`Button.displayName = …`).
|
|
934
|
+
*/
|
|
935
|
+
const usesOf = (definition) =>
|
|
936
|
+
referencesOf(definition)
|
|
937
|
+
.filter(({ abs, info, ref }) => {
|
|
938
|
+
if (ref.kind === 'export' || ref.kind === 'export-default') return false
|
|
939
|
+
if (abs !== definition.abs) return true
|
|
940
|
+
if (ref.top === definition.statementIndex) return false
|
|
941
|
+
return Boolean(info.statementNames[ref.top])
|
|
942
|
+
})
|
|
943
|
+
.map(({ abs, file, ref, dynamic }) => ({
|
|
944
|
+
abs,
|
|
945
|
+
file,
|
|
946
|
+
line: ref.line,
|
|
947
|
+
column: ref.column,
|
|
948
|
+
kind: ref.kind === 'jsx-close' ? 'jsx-close' : ref.kind,
|
|
949
|
+
owner: ref.topName ?? null,
|
|
950
|
+
top: ref.top,
|
|
951
|
+
classNames: ref.classNames ?? [],
|
|
952
|
+
...(dynamic ? { dynamic: true } : {}),
|
|
953
|
+
}))
|
|
954
|
+
.filter((use) => use.kind !== 'jsx-close')
|
|
955
|
+
|
|
956
|
+
/** Files whose module specifiers resolve to `abs`, with the specifier nodes. */
|
|
957
|
+
const importersOf = (abs) => {
|
|
958
|
+
const result = []
|
|
959
|
+
for (const [fromAbs, info] of infos) {
|
|
960
|
+
for (const moduleRef of info.moduleRefs) {
|
|
961
|
+
if (targetOf(info, moduleRef.source) === abs) result.push({ abs: fromAbs, file: relOf.get(fromAbs), info, moduleRef })
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
return result
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
/** Exported components a file declares, in statement order. */
|
|
968
|
+
const exportedComponentsOf = (abs) =>
|
|
969
|
+
definitions.filter((definition) => definition.abs === abs).sort((a, b) => a.statementIndex - b.statementIndex)
|
|
970
|
+
|
|
971
|
+
return {
|
|
972
|
+
root,
|
|
973
|
+
infos,
|
|
974
|
+
relOf,
|
|
975
|
+
definitions,
|
|
976
|
+
definitionOf: (name) => byName.get(name) ?? null,
|
|
977
|
+
definitionsNamed: (name) => definitions.filter((definition) => definition.name === name),
|
|
978
|
+
referencesOf,
|
|
979
|
+
usesOf,
|
|
980
|
+
importersOf,
|
|
981
|
+
exportedComponentsOf,
|
|
982
|
+
resolveExport,
|
|
983
|
+
resolveLocal: (abs, local) => resolveLocalInner(abs, local),
|
|
984
|
+
resolveRef,
|
|
985
|
+
targetOf: (abs, source) => {
|
|
986
|
+
const info = infos.get(abs)
|
|
987
|
+
return info ? targetOf(info, source) : resolveSpec(abs, source)
|
|
988
|
+
},
|
|
989
|
+
resolveSpec,
|
|
990
|
+
sameDefinition: (a, b) => Boolean(a && b) && a.abs === b.abs && (a.local ?? 'default') === (b.local ?? 'default'),
|
|
991
|
+
}
|
|
992
|
+
}
|