@akanjs/devkit 3.3.0-rc.8 → 3.3.0-rc.81
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/DEV_RUNTIME_KNOBS.md +1 -1
- package/README.ko.md +7 -1
- package/README.md +7 -1
- package/akanApp/akanApp.host.test.ts +3 -1
- package/akanApp/akanApp.host.ts +26 -2
- package/akanApp/akanApp.host.wiring.test.ts +111 -0
- package/akanApp/devHostPolicy.ts +5 -0
- package/akanApp/devMetadataRestart.ts +93 -0
- package/akanApp/devRecycler.test.ts +2 -0
- package/akanApp/devRecycler.ts +9 -0
- package/akanConfig/akanBin.ts +4 -0
- package/akanConfig/akanConfig.test.ts +307 -18
- package/akanConfig/akanConfig.ts +104 -210
- package/akanConfig/akanPreview.test.ts +28 -0
- package/akanConfig/akanPreview.ts +24 -0
- package/akanConfig/akanWorkers.test.ts +73 -0
- package/akanConfig/akanWorkers.ts +61 -0
- package/akanConfig/desktopServerConfig.ts +96 -0
- package/akanConfig/dockerImage.ts +233 -0
- package/akanConfig/index.ts +3 -0
- package/akanConfig/nativeFileSettings.ts +33 -0
- package/akanConfig/types.ts +4 -0
- package/app/appBin.test.ts +63 -0
- package/app/appBin.ts +50 -0
- package/app/applicationBuildReporter.test.ts +51 -1
- package/app/applicationBuildReporter.ts +6 -1
- package/app/applicationBuildRunner.test.ts +436 -5
- package/app/applicationBuildRunner.ts +237 -37
- package/app/clientServerBoundary.test.ts +115 -0
- package/app/clientServerBoundary.ts +89 -0
- package/app/dictionarySizes.test.ts +40 -0
- package/app/dictionarySizes.ts +95 -0
- package/app/routeFilesReport.test.ts +59 -0
- package/app/routeFilesReport.ts +46 -0
- package/app/routeJsBudget.ts +18 -4
- package/artifact/implicitRootLayout.test.ts +19 -0
- package/artifact/implicitRootLayout.ts +16 -2
- package/artifact/implicitRootLayoutRender.test.tsx +7 -2
- package/artifact/storeBootstrap.test.ts +28 -0
- package/artifact/storeBootstrap.ts +64 -8
- package/biome.base.json +7 -5
- package/codeAgent/index.ts +1 -34
- package/codeAgent/skills/akan-agent/SKILL.md +2 -2
- package/codeAgent/skills/akan-debug/SKILL.md +11 -5
- package/codeAgent/skills/akan-deps/SKILL.md +10 -3
- package/codeAgent/skills/akan-err/SKILL.md +13 -8
- package/codeAgent/skills/akan-mcp/SKILL.md +1 -1
- package/codeAgent/skills/akan-module/SKILL.md +2 -2
- package/codeAgent/skills/akan-page/SKILL.md +65 -30
- package/codeAgent/skills/akan-query/SKILL.md +13 -20
- package/codeAgent/skills/akan-runtime/SKILL.md +21 -1
- package/codeAgent/skills/akan-service/SKILL.md +21 -20
- package/codeAgent/skills/akan-transport/SKILL.md +5 -14
- package/codeAgent/skills/akan-ui/SKILL.md +16 -21
- package/codeAgent/skills/akan-validate/SKILL.md +8 -2
- package/commandDecorators/command.ts +28 -12
- package/commandDecorators/commandBuilder.ts +8 -1
- package/commandDecorators/commandDecorators.test.ts +70 -3
- package/commandDecorators/helpFormatter.ts +14 -12
- package/context/akanContext.ts +2 -0
- package/csrE2e/devHmrLatency.e2e.test.ts +1 -1
- package/csrE2e/ssrRegistry.e2e.test.ts +108 -22
- package/docs/apiReference.test.ts +25 -0
- package/docs/apiReference.ts +38 -5
- package/docs/declarationReader.ts +30 -0
- package/executors/appExecutor.ts +26 -12
- package/executors/clientRuntimeKeyOrder.fixture.json +682 -0
- package/executors/clientRuntimeKeys.fixture.json +393 -0
- package/executors/clientRuntimeKeys.test.ts +1690 -0
- package/executors/executor.ts +14 -6
- package/executors/executors.test.ts +155 -1
- package/executors/pkgExecutor.ts +2 -1
- package/executors/sysExecutor.ts +112 -8
- package/executors/workspaceExecutor.ts +7 -4
- package/frontendBuild/allRoutesBuilder.ts +99 -7
- package/frontendBuild/autoImportSync.ts +3 -2
- package/frontendBuild/buildRouteClient.test.ts +684 -6
- package/frontendBuild/clientBuildTypes.ts +26 -0
- package/frontendBuild/clientEntriesBundler.test.ts +79 -0
- package/frontendBuild/clientEntriesBundler.ts +70 -1
- package/frontendBuild/clientEntryDiscovery.test.ts +308 -1
- package/frontendBuild/clientEntryDiscovery.ts +134 -12
- package/frontendBuild/clientEntryGroups.test.ts +77 -0
- package/frontendBuild/clientEntryGroups.ts +61 -0
- package/frontendBuild/csrDevArtifactWriter.ts +2 -2
- package/frontendBuild/csrDevModuleCompiler.test.ts +86 -0
- package/frontendBuild/csrDevModuleCompiler.ts +44 -8
- package/frontendBuild/csrDevPatcher.test.ts +4 -5
- package/frontendBuild/csrDevPatcher.ts +37 -11
- package/frontendBuild/csrDevTypes.ts +9 -0
- package/frontendBuild/cssCandidateCache.test.ts +12 -0
- package/frontendBuild/cssCandidateCache.ts +5 -3
- package/frontendBuild/cssCompiler.test.ts +14 -0
- package/frontendBuild/devBarrelImports.test.ts +124 -0
- package/frontendBuild/devBarrelImports.ts +81 -21
- package/frontendBuild/devChangePlanner.ts +0 -1
- package/frontendBuild/devRegistryBundler.ts +4 -2
- package/frontendBuild/devRouteRuntime.test.ts +492 -0
- package/frontendBuild/devRouteRuntime.ts +167 -0
- package/frontendBuild/dictionaryAssetBuilder.test.ts +408 -0
- package/frontendBuild/dictionaryAssetBuilder.ts +185 -17
- package/frontendBuild/dictionaryConstants.ts +136 -0
- package/frontendBuild/dictionaryOwnParts.ts +88 -0
- package/frontendBuild/dictionaryPackState.ts +131 -0
- package/frontendBuild/dictionaryPacking.test.ts +207 -0
- package/frontendBuild/dictionaryPacking.ts +160 -0
- package/frontendBuild/dictionaryRoutes.test.ts +251 -0
- package/frontendBuild/dictionaryRoutes.ts +227 -0
- package/frontendBuild/frontendBuild.test.ts +5 -3
- package/frontendBuild/pagesBundleBuilder.test.ts +84 -0
- package/frontendBuild/pagesBundleBuilder.ts +78 -2
- package/frontendBuild/precompressArtifacts.test.ts +22 -0
- package/frontendBuild/precompressArtifacts.ts +34 -8
- package/frontendBuild/registryMetadataFingerprint.ts +3 -3
- package/frontendBuild/renderedEntries.ts +144 -0
- package/frontendBuild/routeClientBuilder.ts +407 -43
- package/frontendBuild/routeHandoffs.test.ts +155 -0
- package/frontendBuild/routeHandoffs.ts +117 -0
- package/frontendBuild/routeModulePreloads.test.ts +32 -0
- package/frontendBuild/routeModulePreloads.ts +20 -11
- package/frontendBuild/routeRuntimeCheck.test.ts +280 -0
- package/frontendBuild/routeRuntimeCheck.ts +248 -0
- package/frontendBuild/routeRuntimePays.test.ts +143 -0
- package/frontendBuild/routeRuntimePays.ts +117 -0
- package/frontendBuild/ssrBaseArtifactBuilder.ts +30 -35
- package/frontendBuild/ssrDevBundler.test.ts +150 -1
- package/frontendBuild/ssrDevBundler.ts +66 -11
- package/frontendBuild/vendorFileImports.test.ts +254 -0
- package/frontendBuild/vendorFileImports.ts +163 -0
- package/frontendBuild/vendorNarrowing.test.ts +47 -3
- package/frontendBuild/vendorNarrowing.ts +38 -2
- package/frontendBuild/vendorSpecifiers.ts +2 -0
- package/frontendBuild/workerChunks.ts +61 -0
- package/guideline/agentAssets.ts +1 -1
- package/incrementalBuilder/buildBatch.proc.ts +27 -1
- package/incrementalBuilder/buildBatchProtocol.ts +1 -0
- package/incrementalBuilder/builderWatch.ts +11 -1
- package/incrementalBuilder/incrementalBuilder.proc.test.ts +159 -0
- package/incrementalBuilder/incrementalBuilder.proc.ts +31 -3
- package/incrementalBuilder/pendingMetadata.test.ts +27 -0
- package/incrementalBuilder/pendingMetadata.ts +19 -0
- package/integration/devResourceProbe.ts +14 -0
- package/integration/devStability.integration.test.ts +46 -21
- package/integration/renderAdmissionSoak.ts +6 -0
- package/integration/ssrMemoryProbe.ts +5 -0
- package/lint/__fixtures__/no-arbitrary-color/every-color-function/bad.tsx +11 -0
- package/lint/__fixtures__/no-arbitrary-color/every-color-function/good.tsx +2 -0
- package/lint/__fixtures__/no-daisyui-legacy-class/every-class/bad.tsx +34 -0
- package/lint/__fixtures__/no-daisyui-legacy-class/every-class/good.tsx +2 -0
- package/lint/__fixtures__/no-inline-color/every-color-function/bad.tsx +10 -0
- package/lint/__fixtures__/no-inline-color/every-color-function/good.tsx +2 -0
- package/lint/__fixtures__/no-raw-palette-class/every-palette/bad.tsx +22 -0
- package/lint/__fixtures__/no-raw-palette-class/every-palette/good.tsx +2 -0
- package/lint/biomeBase.test.ts +50 -3
- package/lint/biomeBase.ts +1 -1
- package/lint/biomeCheck.test.ts +162 -0
- package/lint/biomeCheck.ts +131 -0
- package/lint/linter.test.ts +16 -11
- package/lint/no-arbitrary-color.grit +5 -0
- package/lint/no-conditional-st-hook.grit +5 -1
- package/lint/no-daisyui-legacy-class.grit +8 -0
- package/lint/no-document-cookie.grit +3 -0
- package/lint/no-inline-color.grit +10 -2
- package/lint/no-oversized-list-limit.grit +2 -0
- package/lint/no-raw-palette-class.grit +7 -0
- package/lint/no-unpublished-form-setter.grit +2 -0
- package/lint/no-web-only-api-outside-webkit.grit +5 -0
- package/lint/no-web-storage.grit +3 -0
- package/lint/withheldEnvFile.ts +17 -0
- package/mobile/desktopBin.ts +2 -2
- package/mobile/desktopServerStage.test.ts +18 -3
- package/mobile/desktopServerStage.ts +7 -2
- package/mobile/nativeApp.test.ts +13 -5
- package/mobile/nativeApp.ts +9 -4
- package/mobile/nativeConfig.test.ts +17 -1
- package/mobile/nativeConfig.ts +11 -6
- package/mobile/nativePublicEnv.ts +1 -1
- package/mobile/serverBundleSecrets.ts +43 -0
- package/package.json +11 -12
- package/packageExports.test.ts +3 -2
- package/preview/PreviewView.ts +43 -6
- package/preview/previewView.test.ts +31 -0
- package/quality/dictionaryKeys.test.ts +356 -0
- package/quality/dictionaryKeys.ts +488 -0
- package/quality/objectImportReport.test.ts +36 -0
- package/quality/objectImportReport.ts +105 -0
- package/quality/qualityScanner.ts +1 -1
- package/quality/routeRuntimeReport.test.ts +91 -0
- package/quality/routeRuntimeReport.ts +121 -0
- package/quality/runtimeConstantFacts.ts +71 -0
- package/quality/runtimeConstantGraph.ts +123 -0
- package/quality/runtimeConstantReads.ts +116 -0
- package/quality/runtimeImports.test.ts +190 -0
- package/quality/runtimeImports.ts +233 -0
- package/quality/runtimeKeyNames.ts +46 -0
- package/quality/runtimeLexicalScope.ts +132 -0
- package/quality/runtimeMarkerReach.test.ts +84 -0
- package/quality/runtimeMarkerReach.ts +255 -0
- package/quality/runtimeModuleFacts.ts +290 -0
- package/quality/runtimeNameTable.test.ts +172 -0
- package/quality/runtimeNameTable.ts +477 -0
- package/quality/runtimeScopeReport.test.ts +123 -0
- package/quality/runtimeScopeReport.ts +167 -0
- package/quality/runtimeSliceKey.ts +269 -0
- package/quality/runtimeSyntax.ts +133 -0
- package/quality/runtimeTable.fixture.ts +189 -0
- package/quality/runtimeUse.test.ts +737 -0
- package/quality/runtimeUse.ts +696 -0
- package/quality/runtimeUseReadable.test.ts +393 -0
- package/quality/ssrScanner.test.ts +13 -0
- package/quality/ssrScanner.ts +1 -1
- package/quality/translatorBindings.ts +187 -0
- package/test/fakeTerminal.ts +45 -0
- package/test/secretEnvFixture.ts +134 -0
- package/test/testHelpers.ts +11 -0
- package/transforms/barrelAnalyzer.ts +24 -2
- package/transforms/barrelImportsPlugin.ts +121 -32
- package/transforms/exportReach.test.ts +162 -0
- package/transforms/exportReach.ts +308 -0
- package/transforms/frameworkObjects.fixture.tsx +58 -0
- package/transforms/frameworkObjects.test.ts +54 -0
- package/transforms/modelReference.test.ts +196 -0
- package/transforms/modelReference.ts +39 -0
- package/transforms/moduleParts.test.ts +225 -0
- package/transforms/moduleParts.ts +418 -0
- package/transforms/objectImportEdits.ts +150 -0
- package/transforms/objectMemberImports.test.ts +633 -0
- package/transforms/objectMemberImports.ts +571 -0
- package/transforms/objectMemberLookup.ts +150 -0
- package/transforms/objectModule.test.ts +307 -0
- package/transforms/objectModule.ts +482 -0
- package/transforms/objectModuleAnalyzer.ts +104 -0
- package/transforms/partImports.test.ts +76 -0
- package/transforms/partImports.ts +110 -0
- package/transforms/partModules.test.ts +187 -0
- package/transforms/partModules.ts +90 -0
- package/transforms/partPurity.ts +124 -0
- package/transforms/reactIconImports.test.ts +231 -0
- package/transforms/reactIconImports.ts +325 -0
- package/transforms/routeConstants.ts +122 -0
- package/transforms/routeRuntime.test.ts +835 -0
- package/transforms/routeRuntime.ts +703 -0
- package/transforms/routeRuntimeEdits.ts +92 -0
- package/transforms/routeRuntimeGraph.ts +66 -0
- package/transforms/routeRuntimePlan.ts +244 -0
- package/transforms/secretEnvDeclaration.test.ts +211 -0
- package/transforms/secretEnvDeclaration.ts +219 -0
- package/transforms/secretEnvFile.test.ts +117 -0
- package/transforms/secretEnvFile.ts +113 -0
- package/transforms/secretEnvName.test.ts +54 -0
- package/transforms/secretEnvName.ts +29 -0
- package/transforms/secretEnvSource.test.ts +152 -0
- package/transforms/secretEnvSource.ts +197 -0
- package/transforms/serverEnvEvaluationError.ts +11 -0
- package/transforms/serverEnvPlugin.test.ts +155 -1
- package/transforms/serverEnvPlugin.ts +123 -19
- package/transforms/serverEnvProfile.test.ts +129 -2
- package/transforms/serverEnvProfile.ts +133 -43
- package/transforms/serverEnvSecrets.test.ts +268 -0
- package/transforms/signalDescriptors.test.ts +280 -0
- package/transforms/signalDescriptors.ts +227 -0
- package/transforms/thisFreeExports.ts +87 -0
- package/transforms/transforms.test.ts +177 -0
- package/transforms/tsconfigPackageResolver.test.ts +1 -0
- package/tunnel/createTunnel.ts +5 -1
- package/tunnel/ssh2Module.test.ts +37 -0
- package/tunnel/ssh2Module.ts +28 -0
- package/tunnel/sshTunnel.ts +4 -2
- package/typecheck/projectTypecheck.test.ts +32 -1
- package/typecheck/projectTypecheck.ts +3 -3
- package/typecheck/typeChecker.test.ts +49 -0
- package/typecheck/typeChecker.ts +9 -7
- package/typecheck/withheldDiagnostics.ts +41 -0
- package/util/devInstallCommand.ts +7 -0
- package/util/jsonFormatter.test.ts +13 -9
- package/workflow/executor.ts +6 -2
- package/workflow/index.ts +1 -0
- package/workflow/plan.ts +36 -7
- package/workflow/types.ts +1 -1
- package/workflow/uiTarget.ts +50 -0
- package/workspace/envSeed.test.ts +30 -1
- package/workspace/envSeed.ts +30 -1
- package/workspace/mainCheckout.test.ts +55 -0
- package/workspace/mainCheckout.ts +38 -0
- package/workspace/scanInfo.test.ts +39 -0
- package/workspace/scanInfo.ts +55 -6
- package/workspace/slicePlanner.ts +2 -1
- package/workspace/workspaceLayout.test.ts +22 -1
- package/workspace/workspaceLayout.ts +3 -3
- package/CHANGELOG.md +0 -2903
- package/codeAgent/agent/AkanCodeCompaction.ts +0 -49
- package/codeAgent/agent/AkanCodeServices.ts +0 -122
- package/codeAgent/agent/AkanContextFiles.ts +0 -94
- package/codeAgent/agent/CodeAgent.ts +0 -626
- package/codeAgent/agent/CodeAgentAllowed.ts +0 -35
- package/codeAgent/agent/CodeAgentAsks.ts +0 -66
- package/codeAgent/agent/CodeAgentCommandLine.ts +0 -372
- package/codeAgent/agent/CodeAgentEventMapper.ts +0 -225
- package/codeAgent/agent/CodeAgentGate.ts +0 -258
- package/codeAgent/agent/CodeAgentProxy.ts +0 -83
- package/codeAgent/agent/CodeAgentRpcHost.ts +0 -109
- package/codeAgent/agent/CodeAgentRpcListener.ts +0 -195
- package/codeAgent/agent/CodeAgentStreamPrinter.ts +0 -86
- package/codeAgent/agent/CodeAgentSuspended.ts +0 -85
- package/codeAgent/agent/CodeAgentUi.ts +0 -47
- package/codeAgent/agent/CodeMailbox.ts +0 -148
- package/codeAgent/agent/CodeSessionFork.ts +0 -56
- package/codeAgent/agent/CodeSessionIndex.ts +0 -78
- package/codeAgent/agent/SubagentPool.ts +0 -219
- package/codeAgent/agent/WorkspaceTrust.ts +0 -200
- package/codeAgent/agent/akanCodeCompaction.test.ts +0 -36
- package/codeAgent/agent/akanCodeModel.ts +0 -47
- package/codeAgent/agent/akanCodeServices.test.ts +0 -54
- package/codeAgent/agent/akanContextFiles.test.ts +0 -116
- package/codeAgent/agent/akanSystemPrompt.ts +0 -50
- package/codeAgent/agent/codeAgent.test.ts +0 -337
- package/codeAgent/agent/codeAgentCommandLine.test.ts +0 -212
- package/codeAgent/agent/codeAgentContract.test.ts +0 -755
- package/codeAgent/agent/codeAgentHosts.ts +0 -30
- package/codeAgent/agent/codeAgentProxy.test.ts +0 -126
- package/codeAgent/agent/codeAgentRpcListener.test.ts +0 -215
- package/codeAgent/agent/codeAgentSuspended.test.ts +0 -47
- package/codeAgent/agent/codeMailbox.test.ts +0 -76
- package/codeAgent/agent/codeSessionFork.test.ts +0 -58
- package/codeAgent/agent/codeSessionIndex.test.ts +0 -77
- package/codeAgent/agent/consoleToStderr.ts +0 -8
- package/codeAgent/agent/jsonLines.ts +0 -29
- package/codeAgent/agent/stringArg.ts +0 -5
- package/codeAgent/feedback/DevLogFeedback.ts +0 -82
- package/codeAgent/feedback/PreviewFeedback.ts +0 -46
- package/codeAgent/feedback/TurnFeedback.ts +0 -98
- package/codeAgent/feedback/VerifyFeedback.ts +0 -39
- package/codeAgent/tools/AkanCli.ts +0 -51
- package/codeAgent/tools/AkanCodePlugins.ts +0 -142
- package/codeAgent/tools/AkanEditScope.ts +0 -53
- package/codeAgent/tools/AkanEnvKeys.ts +0 -50
- package/codeAgent/tools/AkanToolPack.ts +0 -86
- package/codeAgent/tools/AkanVerifier.ts +0 -30
- package/codeAgent/tools/AskToolPack.ts +0 -99
- package/codeAgent/tools/DeepSeekWeb.ts +0 -127
- package/codeAgent/tools/MailToolPack.ts +0 -76
- package/codeAgent/tools/McpClient.ts +0 -209
- package/codeAgent/tools/McpOAuth.ts +0 -233
- package/codeAgent/tools/McpOAuthLoopback.ts +0 -64
- package/codeAgent/tools/McpServerConfig.ts +0 -131
- package/codeAgent/tools/McpSignIn.ts +0 -158
- package/codeAgent/tools/McpTokenStore.ts +0 -128
- package/codeAgent/tools/McpToolPack.ts +0 -206
- package/codeAgent/tools/SessionToolPack.ts +0 -91
- package/codeAgent/tools/WebToolPack.ts +0 -315
- package/codeAgent/tools/akanCodePlugins.test.ts +0 -954
- package/codeAgent/tools/askToolPack.test.ts +0 -116
- package/codeAgent/tools/deepSeekWeb.test.ts +0 -113
- package/codeAgent/tools/mcpOAuth.test.ts +0 -648
- package/codeAgent/tools/mcpServerConfig.test.ts +0 -341
- package/piDependencyPins.test.ts +0 -45
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
import { RuntimeImports } from "./runtimeImports";
|
|
5
|
+
|
|
6
|
+
const files = [
|
|
7
|
+
"apps/demo/ui/Card.tsx",
|
|
8
|
+
"apps/demo/lib/memo/Memo.Zone.tsx",
|
|
9
|
+
"apps/demo/lib/st/inner.ts",
|
|
10
|
+
"libs/kit/ui/deep/inner/Panel.tsx",
|
|
11
|
+
];
|
|
12
|
+
const targets = [
|
|
13
|
+
"apps/demo/client",
|
|
14
|
+
"apps/demo/lib/useClient",
|
|
15
|
+
"apps/demo/lib/usePage",
|
|
16
|
+
"apps/demo/lib/st",
|
|
17
|
+
"apps/demo/lib/sig",
|
|
18
|
+
"apps/demo/lib/__client/memo.st",
|
|
19
|
+
"libs/kit/client",
|
|
20
|
+
"libs/kit/lib/useClient",
|
|
21
|
+
];
|
|
22
|
+
const tails = ["", ".ts", ".tsx", ".js", ".jsx", "/index", "/index.ts", "/index.js"];
|
|
23
|
+
const frameworks = ["akanjs/store", "akanjs/client", "akanjs/./store", "akanjs/client/index", "akanjs/store/baseSt"];
|
|
24
|
+
|
|
25
|
+
const spellingsOf = (file: string, target: string) => {
|
|
26
|
+
const [root = "", scope = "", ...rest] = target.split("/");
|
|
27
|
+
const relative = path.posix.relative(path.posix.dirname(file), target);
|
|
28
|
+
const dotted = relative.startsWith(".") ? relative : `./${relative}`;
|
|
29
|
+
return [
|
|
30
|
+
...tails.flatMap((tail) => [
|
|
31
|
+
`@${target}${tail}`,
|
|
32
|
+
`@${root}/${scope}//${rest.join("/")}${tail}`,
|
|
33
|
+
`@${root}/${scope}/./${rest.join("/")}${tail}`,
|
|
34
|
+
`${dotted}${tail}`,
|
|
35
|
+
`${dotted.replace(/\//g, "//")}${tail}`,
|
|
36
|
+
`${dotted}/../${path.posix.basename(target)}${tail}`,
|
|
37
|
+
`./ /..${dotted.slice(1)}${tail}`,
|
|
38
|
+
]),
|
|
39
|
+
".",
|
|
40
|
+
"..",
|
|
41
|
+
"./",
|
|
42
|
+
"../.",
|
|
43
|
+
];
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const escapedOf = (spelling: string) => {
|
|
47
|
+
const at = spelling.search(/[a-z](?=[^/]*$)/);
|
|
48
|
+
if (at < 0) return [spelling];
|
|
49
|
+
const code = spelling.charCodeAt(at);
|
|
50
|
+
const swap = (escape: string) => `${spelling.slice(0, at)}${escape}${spelling.slice(at + 1)}`;
|
|
51
|
+
return [
|
|
52
|
+
spelling,
|
|
53
|
+
swap(`\\u${code.toString(16).padStart(4, "0")}`),
|
|
54
|
+
swap(`\\u{${code.toString(16)}}`),
|
|
55
|
+
swap(`\\x${code.toString(16)}`),
|
|
56
|
+
swap(`\\\n${spelling[at]}`),
|
|
57
|
+
swap(`\\\r\n${spelling[at]}`),
|
|
58
|
+
];
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const statementsOf = (literal: string) => [
|
|
62
|
+
`import { st } from ${literal};\n`,
|
|
63
|
+
`export * from ${literal};\n`,
|
|
64
|
+
`import client = require(${literal});\n`,
|
|
65
|
+
`const client = require(${literal});\n`,
|
|
66
|
+
`export const load = () => import(${literal});\n`,
|
|
67
|
+
`import ${literal};\n`,
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
const specifierOf = (file: string, text: string) => {
|
|
71
|
+
let found: string | null = null;
|
|
72
|
+
const visit = (node: ts.Node) => {
|
|
73
|
+
if (found === null && ts.isStringLiteralLike(node)) found = node.text;
|
|
74
|
+
ts.forEachChild(node, visit);
|
|
75
|
+
};
|
|
76
|
+
visit(ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX));
|
|
77
|
+
return found as string | null;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
describe("RuntimeImports", () => {
|
|
81
|
+
test("never skips a file whose specifier, in any spelling, the specifier reader resolves", () => {
|
|
82
|
+
let resolved = 0;
|
|
83
|
+
const skipped: string[] = [];
|
|
84
|
+
for (const file of files)
|
|
85
|
+
for (const spelling of [...targets.flatMap((target) => spellingsOf(file, target)), ...frameworks])
|
|
86
|
+
for (const escaped of escapedOf(spelling))
|
|
87
|
+
for (const quote of ['"', "'", "`"])
|
|
88
|
+
for (const text of statementsOf(`${quote}${escaped}${quote}`)) {
|
|
89
|
+
const specifier = specifierOf(file, `// a \`note\`, it's\n${text}`);
|
|
90
|
+
if (specifier === null || !RuntimeImports.targetOf(specifier, file)) continue;
|
|
91
|
+
resolved++;
|
|
92
|
+
if (!RuntimeImports.mayUseRuntime(`// a \`note\`, it's\n${text}`, file)) skipped.push(`${file}: ${text}`);
|
|
93
|
+
}
|
|
94
|
+
expect(skipped).toEqual([]);
|
|
95
|
+
expect(resolved).toBeGreaterThan(20000);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("reads a specifier through its extension, index, doubled slashes and dot segments", () => {
|
|
99
|
+
const file = "apps/demo/lib/memo/Memo.Zone.tsx";
|
|
100
|
+
expect(
|
|
101
|
+
[
|
|
102
|
+
"@apps/demo/client.js",
|
|
103
|
+
"@apps/demo//client",
|
|
104
|
+
"@apps/demo/./lib/../client/index.tsx",
|
|
105
|
+
"../useClient.js",
|
|
106
|
+
"../__client/memo.st",
|
|
107
|
+
"../__client/_base.fetch",
|
|
108
|
+
"../__client/_st",
|
|
109
|
+
"../__client/_useClient.ts",
|
|
110
|
+
"akanjs/./store",
|
|
111
|
+
"akanjs/store/baseSt",
|
|
112
|
+
"akanjs/client/native/haptics",
|
|
113
|
+
"akanjs/fetch",
|
|
114
|
+
"@apps/demo/lib/cnst",
|
|
115
|
+
].map((specifier) => RuntimeImports.targetOf(specifier, file)?.kind ?? null),
|
|
116
|
+
).toEqual([
|
|
117
|
+
"client",
|
|
118
|
+
"client",
|
|
119
|
+
"client",
|
|
120
|
+
"useClient",
|
|
121
|
+
"piece",
|
|
122
|
+
"piece",
|
|
123
|
+
"_st",
|
|
124
|
+
"_useClient",
|
|
125
|
+
"framework-store",
|
|
126
|
+
"framework-deep",
|
|
127
|
+
null,
|
|
128
|
+
"framework-fetch",
|
|
129
|
+
null,
|
|
130
|
+
]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("names the agent entry apart, and lists as its screen exports the files that import no store or fetch", async () => {
|
|
134
|
+
const kindOf = (specifier: string) => RuntimeImports.targetOf(specifier, "apps/demo/ui/Card.tsx")?.kind ?? null;
|
|
135
|
+
expect(
|
|
136
|
+
[
|
|
137
|
+
"akanjs/store/agent",
|
|
138
|
+
"akanjs/store/agent/index.ts",
|
|
139
|
+
"akanjs/store/agent/ScreenSettle",
|
|
140
|
+
"akanjs/client/agent",
|
|
141
|
+
].map(kindOf),
|
|
142
|
+
).toEqual(["framework-agent", "framework-agent", "framework-deep", "framework-deep"]);
|
|
143
|
+
const use = (name: string) => RuntimeImports.useOf({ scope: "apps/demo", kind: "framework-agent" }, name, false);
|
|
144
|
+
expect([use("ScreenSettle"), use("AgentBridge"), use("somethingNew")]).toEqual([
|
|
145
|
+
null,
|
|
146
|
+
{ reason: "whole-runtime-import", why: "a framework internal may hand out the whole runtime" },
|
|
147
|
+
{ reason: "whole-runtime-import", why: "a framework internal may hand out the whole runtime" },
|
|
148
|
+
]);
|
|
149
|
+
|
|
150
|
+
const dir = path.join(import.meta.dir, "../../../akanjs/store/agent");
|
|
151
|
+
const index = await Bun.file(path.join(dir, "index.ts")).text();
|
|
152
|
+
const files = new Map<string, { imports: string[]; exports: string[]; text: string }>();
|
|
153
|
+
for (const [, name = ""] of index.matchAll(/export \* from "\.\/(\w+)";/g)) {
|
|
154
|
+
const text = await Bun.file(path.join(dir, `${name}.ts`)).text();
|
|
155
|
+
const source = ts.createSourceFile(name, text, ts.ScriptTarget.Latest);
|
|
156
|
+
const imports = source.statements.flatMap((statement) => {
|
|
157
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier)) return [];
|
|
158
|
+
const clause = statement.importClause;
|
|
159
|
+
const named =
|
|
160
|
+
clause?.namedBindings && ts.isNamedImports(clause.namedBindings) ? clause.namedBindings.elements : [];
|
|
161
|
+
const values = named.filter((element) => !element.isTypeOnly).map((element) => element.name.text);
|
|
162
|
+
if (clause?.isTypeOnly || (clause && !clause.name && named.length && !values.length)) return [];
|
|
163
|
+
const specifier = statement.moduleSpecifier.text;
|
|
164
|
+
return specifier === "akanjs/client" && values.join() === "router" ? [] : [specifier];
|
|
165
|
+
});
|
|
166
|
+
const exports = source.statements.flatMap((statement) => {
|
|
167
|
+
const exported =
|
|
168
|
+
ts.canHaveModifiers(statement) &&
|
|
169
|
+
ts.getModifiers(statement)?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword);
|
|
170
|
+
if (!exported) return [];
|
|
171
|
+
if (ts.isVariableStatement(statement))
|
|
172
|
+
return statement.declarationList.declarations.map((declaration) => declaration.name.getText(source));
|
|
173
|
+
return [(statement as ts.DeclarationStatement).name?.text ?? "default"];
|
|
174
|
+
});
|
|
175
|
+
files.set(name, { imports, exports, text });
|
|
176
|
+
}
|
|
177
|
+
expect(files.size).toBe(12);
|
|
178
|
+
const outside = new Set(["akanjs/base", "use-agentic"]);
|
|
179
|
+
for (let size = -1; size !== files.size; ) {
|
|
180
|
+
size = files.size;
|
|
181
|
+
for (const [name, { imports }] of files)
|
|
182
|
+
if (!imports.every((specifier) => outside.has(specifier) || files.has(specifier.replace(/^\.\//, ""))))
|
|
183
|
+
files.delete(name);
|
|
184
|
+
}
|
|
185
|
+
expect([...files.values()].filter(({ text }) => /globalThis|Symbol\.for\(|import\(/.test(text))).toEqual([]);
|
|
186
|
+
expect([...files.values()].flatMap(({ exports }) => exports).sort()).toEqual(
|
|
187
|
+
[...RuntimeImports.agentScreenNames].sort(),
|
|
188
|
+
);
|
|
189
|
+
});
|
|
190
|
+
});
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { RuntimeConstantFacts } from "./runtimeConstantFacts";
|
|
3
|
+
import type { RuntimeFamily } from "./runtimeNameTable";
|
|
4
|
+
|
|
5
|
+
export type RuntimeImportKind =
|
|
6
|
+
| "client"
|
|
7
|
+
| "useClient"
|
|
8
|
+
| "usePage"
|
|
9
|
+
| "st"
|
|
10
|
+
| "sig"
|
|
11
|
+
| "piece"
|
|
12
|
+
| "_st"
|
|
13
|
+
| "_useClient"
|
|
14
|
+
| "framework-store"
|
|
15
|
+
| "framework-client"
|
|
16
|
+
| "framework-fetch"
|
|
17
|
+
| "framework-agent"
|
|
18
|
+
| "framework-deep";
|
|
19
|
+
|
|
20
|
+
export interface RuntimeImportTarget {
|
|
21
|
+
scope: string | null;
|
|
22
|
+
kind: RuntimeImportKind;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type RuntimeHandleKind =
|
|
26
|
+
| "st"
|
|
27
|
+
| "root"
|
|
28
|
+
| "self"
|
|
29
|
+
| "state"
|
|
30
|
+
| "getter"
|
|
31
|
+
| "fetch"
|
|
32
|
+
| "signal"
|
|
33
|
+
| "akanjs/store"
|
|
34
|
+
| "akanjs/client"
|
|
35
|
+
| Exclude<RuntimeFamily, "fetch">;
|
|
36
|
+
|
|
37
|
+
export interface RuntimeHandle {
|
|
38
|
+
kind: RuntimeHandleKind;
|
|
39
|
+
members?: ReadonlySet<string>;
|
|
40
|
+
cast?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type RuntimeBinding =
|
|
44
|
+
| { type: "handle"; handle: RuntimeHandle }
|
|
45
|
+
| { type: "rootStore" }
|
|
46
|
+
| { type: "storeFactory" }
|
|
47
|
+
| { type: "whole"; name: string }
|
|
48
|
+
| { type: "cnst"; scope: string };
|
|
49
|
+
|
|
50
|
+
export interface RuntimeConstantTarget {
|
|
51
|
+
scope: string;
|
|
52
|
+
kind: "cnst" | "barrel";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type RuntimeImportUse =
|
|
56
|
+
| { binding: RuntimeBinding }
|
|
57
|
+
| { reason: "unknown-name" | "whole-runtime-import"; why: string }
|
|
58
|
+
| null;
|
|
59
|
+
|
|
60
|
+
const runtimePathPattern =
|
|
61
|
+
/^((?:apps|libs)\/[^/]+)\/(client|lib\/useClient|lib\/usePage|lib\/st|lib\/sig|lib\/__client\/.+)$/;
|
|
62
|
+
const frameworkPathPattern = /^akanjs\/(store|client|fetch)(\/.+)?$/;
|
|
63
|
+
const constantPathPattern = /^((?:apps|libs)\/[^/]+)\/lib\/cnst$/;
|
|
64
|
+
const quotedPattern = /(?=(["'])((?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\n\r])*)\1)|(?=`((?:\\[\s\S]|[^\\`])*)`)/g;
|
|
65
|
+
const loaderPattern = /\brequire\b|\bimport\s*(?:\(|\/[/*])/;
|
|
66
|
+
const escapePattern = /\\(u\{[0-9a-fA-F]+\}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|\r\n|[\s\S])/g;
|
|
67
|
+
const simpleEscapes: Record<string, string> = {
|
|
68
|
+
n: "\n",
|
|
69
|
+
r: "\r",
|
|
70
|
+
t: "\t",
|
|
71
|
+
b: "\b",
|
|
72
|
+
f: "\f",
|
|
73
|
+
v: "\v",
|
|
74
|
+
"0": "\0",
|
|
75
|
+
"\r\n": "",
|
|
76
|
+
"\n": "",
|
|
77
|
+
"\r": "",
|
|
78
|
+
"
": "",
|
|
79
|
+
"
": "",
|
|
80
|
+
};
|
|
81
|
+
const quietNames = new Set(["usePage", "msg", "Err"]);
|
|
82
|
+
const wholePieces = new Set(["_st", "_useClient"]);
|
|
83
|
+
//? Each of these hands out the tab's one root store or fetch client, so a name read through it is not visible here.
|
|
84
|
+
const frameworkWholeNames = new Set([
|
|
85
|
+
"RootStore",
|
|
86
|
+
"StoreRegistry",
|
|
87
|
+
"StoreInstance",
|
|
88
|
+
"base",
|
|
89
|
+
"BaseStore",
|
|
90
|
+
"StoreCatalogue",
|
|
91
|
+
"StoreSurfaceSource",
|
|
92
|
+
"ensureStoreSurface",
|
|
93
|
+
"AgentBridge",
|
|
94
|
+
"AgentContext",
|
|
95
|
+
"FetchClient",
|
|
96
|
+
]);
|
|
97
|
+
//? What `akanjs/store/agent` exports that reads the screen or the router and never the root store or the fetch client.
|
|
98
|
+
const agentScreenNames = new Set([
|
|
99
|
+
"AgentCursor",
|
|
100
|
+
"AgentPrompts",
|
|
101
|
+
"AgentVisual",
|
|
102
|
+
"AgentVisualOption",
|
|
103
|
+
"ScreenFlash",
|
|
104
|
+
"ScreenImage",
|
|
105
|
+
"ScreenImageView",
|
|
106
|
+
"ScreenReadOptions",
|
|
107
|
+
"ScreenReader",
|
|
108
|
+
"ScreenSettle",
|
|
109
|
+
"ScreenSettleOptions",
|
|
110
|
+
"ScreenTarget",
|
|
111
|
+
]);
|
|
112
|
+
|
|
113
|
+
export class RuntimeImports {
|
|
114
|
+
static readonly frameworkWholeNames: ReadonlySet<string> = frameworkWholeNames;
|
|
115
|
+
static readonly agentScreenNames: ReadonlySet<string> = agentScreenNames;
|
|
116
|
+
|
|
117
|
+
static mayUseRuntime(text: string, fileName: string) {
|
|
118
|
+
return (
|
|
119
|
+
loaderPattern.test(text) ||
|
|
120
|
+
(text.includes("sliceName") && text.includes("refName")) ||
|
|
121
|
+
RuntimeImports.#quotes(text, (specifier) => !!RuntimeImports.targetOf(specifier, fileName))
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static mayUseConstants(text: string, fileName: string) {
|
|
126
|
+
return RuntimeImports.#quotes(
|
|
127
|
+
text,
|
|
128
|
+
(specifier) =>
|
|
129
|
+
!!RuntimeImports.constantTargetOf(specifier, fileName) || !!RuntimeConstantFacts.ownerOf(specifier, fileName),
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
static #quotes(text: string, found: (specifier: string) => boolean) {
|
|
134
|
+
for (const [, , quoted, template] of text.matchAll(quotedPattern)) {
|
|
135
|
+
const raw = quoted ?? template?.replace(/\r\n?/g, "\n") ?? "";
|
|
136
|
+
if (found(RuntimeImports.#decode(raw))) return true;
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
static targetOf(specifier: string, fileName: string): RuntimeImportTarget | null {
|
|
142
|
+
const fileScope = fileName.match(/^((?:apps|libs)\/[^/]+)\//)?.[1] ?? null;
|
|
143
|
+
if (specifier.startsWith("akanjs/")) {
|
|
144
|
+
const framework = RuntimeImports.#stripped(path.posix.normalize(specifier)).match(frameworkPathPattern);
|
|
145
|
+
const [, facet, deep] = framework ?? [];
|
|
146
|
+
if (!facet || (facet === "client" && deep && /^\/native(?:\/|$)/.test(deep))) return null;
|
|
147
|
+
const kind = !deep ? `framework-${facet}` : facet === "store" && deep === "/agent" ? "framework-agent" : null;
|
|
148
|
+
return { scope: fileScope, kind: (kind ?? "framework-deep") as RuntimeImportKind };
|
|
149
|
+
}
|
|
150
|
+
const aliased = specifier.match(/^@(apps|libs)\/([\s\S]+)$/);
|
|
151
|
+
const joined = aliased
|
|
152
|
+
? `${aliased[1]}/${aliased[2]}`
|
|
153
|
+
: specifier.startsWith(".")
|
|
154
|
+
? path.posix.join(path.posix.dirname(fileName), specifier)
|
|
155
|
+
: null;
|
|
156
|
+
const match =
|
|
157
|
+
joined === null ? null : RuntimeImports.#stripped(path.posix.normalize(joined)).match(runtimePathPattern);
|
|
158
|
+
if (!match?.[1] || !match[2]) return null;
|
|
159
|
+
const piece = match[2].match(/^lib\/__client\/(.+)$/)?.[1];
|
|
160
|
+
const kind = piece === undefined ? match[2].replace(/^lib\//, "") : wholePieces.has(piece) ? piece : "piece";
|
|
161
|
+
return { scope: match[1], kind: kind as RuntimeImportKind };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
static constantTargetOf(specifier: string, fileName: string): RuntimeConstantTarget | null {
|
|
165
|
+
const barrel = specifier.match(/^@(libs\/[^/]+)$/)?.[1];
|
|
166
|
+
if (barrel) return { scope: barrel, kind: "barrel" };
|
|
167
|
+
const aliased = specifier.match(/^@((?:apps|libs)\/[\s\S]+)$/)?.[1];
|
|
168
|
+
const joined =
|
|
169
|
+
aliased ?? (specifier.startsWith(".") ? path.posix.join(path.posix.dirname(fileName), specifier) : null);
|
|
170
|
+
const scope = joined && RuntimeImports.#stripped(path.posix.normalize(joined)).match(constantPathPattern)?.[1];
|
|
171
|
+
return scope ? { scope, kind: "cnst" } : null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
static useOf(target: RuntimeImportTarget, imported: string, typeOnly: boolean): RuntimeImportUse {
|
|
175
|
+
const { kind } = target;
|
|
176
|
+
if (kind.startsWith("framework-")) return typeOnly ? null : RuntimeImports.#frameworkUse(kind, imported);
|
|
177
|
+
const whole = (why: string): RuntimeImportUse => ({ reason: "whole-runtime-import", why });
|
|
178
|
+
if (imported === "RootStore" && (kind === "client" || kind === "st" || kind === "_st"))
|
|
179
|
+
return typeOnly
|
|
180
|
+
? { binding: { type: "rootStore" } }
|
|
181
|
+
: whole("a value import of RootStore loads the store runtime whole");
|
|
182
|
+
if (typeOnly) return null;
|
|
183
|
+
if (kind === "sig" || kind === "_st" || kind === "_useClient")
|
|
184
|
+
return whole(`the ${kind} file holds the runtime of every module`);
|
|
185
|
+
if (kind === "piece") return whole("a runtime piece imported by hand is not read name by name");
|
|
186
|
+
const handle = (handleKind: RuntimeHandleKind): RuntimeImportUse => ({
|
|
187
|
+
binding: { type: "handle", handle: { kind: handleKind } },
|
|
188
|
+
});
|
|
189
|
+
if ((imported === "fetch" || imported === "sig") && kind !== "st") return handle(imported);
|
|
190
|
+
if (imported === "st" && kind !== "useClient") return handle("st");
|
|
191
|
+
if (kind === "st" || (kind === "client" && imported === "store"))
|
|
192
|
+
return whole(`${imported} holds every module's store`);
|
|
193
|
+
if (kind !== "client" ? quietNames.has(imported) : quietNames.has(imported) || imported === "cnst") return null;
|
|
194
|
+
if (kind === "client" && /^[A-Z]/.test(imported)) return null;
|
|
195
|
+
return { reason: "unknown-name", why: `${imported} reaches past the names a module answers` };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
static #frameworkUse(kind: RuntimeImportKind, imported: string): RuntimeImportUse {
|
|
199
|
+
if (kind === "framework-agent" && agentScreenNames.has(imported)) return null;
|
|
200
|
+
if (kind === "framework-deep" || kind === "framework-agent")
|
|
201
|
+
return { reason: "whole-runtime-import", why: "a framework internal may hand out the whole runtime" };
|
|
202
|
+
if (frameworkWholeNames.has(imported)) return { binding: { type: "whole", name: imported } };
|
|
203
|
+
if (kind === "framework-store" && imported === "st") return { binding: { type: "handle", handle: { kind: "st" } } };
|
|
204
|
+
if (kind === "framework-store" && imported === "store") return { binding: { type: "storeFactory" } };
|
|
205
|
+
if (kind === "framework-client" && (imported === "fetch" || imported === "sig"))
|
|
206
|
+
return { binding: { type: "handle", handle: { kind: imported } } };
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
static namespaceUse(kind: RuntimeImportKind, name: string): RuntimeImportUse {
|
|
211
|
+
if (kind === "framework-store" || kind === "framework-client")
|
|
212
|
+
return {
|
|
213
|
+
binding: { type: "handle", handle: { kind: kind === "framework-store" ? "akanjs/store" : "akanjs/client" } },
|
|
214
|
+
};
|
|
215
|
+
if (kind === "framework-fetch") return { binding: { type: "whole", name } };
|
|
216
|
+
return { reason: "whole-runtime-import", why: "a namespace or default import holds the whole runtime" };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
static #stripped(resolved: string) {
|
|
220
|
+
return resolved.replace(/\.(?:tsx?|jsx?)$/, "").replace(/\/index$/, "");
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
static #decode(raw: string) {
|
|
224
|
+
return raw.replace(escapePattern, (_, escape: string) => {
|
|
225
|
+
if (escape.startsWith("u{")) {
|
|
226
|
+
const code = Number.parseInt(escape.slice(2, -1), 16);
|
|
227
|
+
return code <= 0x10ffff ? String.fromCodePoint(code) : "";
|
|
228
|
+
}
|
|
229
|
+
if (escape.length > 1 && /^[ux]/.test(escape)) return String.fromCharCode(Number.parseInt(escape.slice(1), 16));
|
|
230
|
+
return simpleEscapes[escape] ?? escape;
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import type { RuntimeLexicalScope } from "./runtimeLexicalScope";
|
|
3
|
+
import { staticName } from "./runtimeSyntax";
|
|
4
|
+
|
|
5
|
+
const maxDepth = 8;
|
|
6
|
+
|
|
7
|
+
export class RuntimeKeyNames {
|
|
8
|
+
readonly #lexical: RuntimeLexicalScope;
|
|
9
|
+
|
|
10
|
+
constructor(lexical: RuntimeLexicalScope) {
|
|
11
|
+
this.#lexical = lexical;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//? An assertion to literal names wins over the value under it: the author states the key behaves as that name.
|
|
15
|
+
namesOf(expression: ts.Expression, depth = 0): string[] | null {
|
|
16
|
+
let value = expression;
|
|
17
|
+
while (ts.isParenthesizedExpression(value) || ts.isNonNullExpression(value)) value = value.expression;
|
|
18
|
+
if (ts.isStringLiteralLike(value) || ts.isNumericLiteral(value)) return [value.text];
|
|
19
|
+
if (ts.isAsExpression(value) || ts.isTypeAssertionExpression(value) || ts.isSatisfiesExpression(value))
|
|
20
|
+
return RuntimeKeyNames.#literalsOf(value.type) ?? this.namesOf(value.expression, depth);
|
|
21
|
+
const initializer = ts.isIdentifier(value) && depth < maxDepth ? this.constantOf(value) : null;
|
|
22
|
+
return initializer ? this.namesOf(initializer, depth + 1) : null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
ofProperty(name: ts.PropertyName | ts.BindingName): string[] | null {
|
|
26
|
+
if (ts.isComputedPropertyName(name)) return this.namesOf(name.expression);
|
|
27
|
+
const text = staticName(name);
|
|
28
|
+
return text === null ? null : [text];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
constantOf(identifier: ts.Identifier): ts.Expression | null {
|
|
32
|
+
const declaration = this.#lexical.resolve(identifier)?.parent;
|
|
33
|
+
if (!declaration || !ts.isVariableDeclaration(declaration) || !ts.isIdentifier(declaration.name)) return null;
|
|
34
|
+
const list = declaration.parent;
|
|
35
|
+
const constant = ts.isVariableDeclarationList(list) && !!(list.flags & ts.NodeFlags.Const);
|
|
36
|
+
return constant ? (declaration.initializer ?? null) : null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static #literalsOf(type: ts.TypeNode): string[] | null {
|
|
40
|
+
if (ts.isParenthesizedTypeNode(type)) return RuntimeKeyNames.#literalsOf(type.type);
|
|
41
|
+
if (ts.isLiteralTypeNode(type)) return ts.isStringLiteralLike(type.literal) ? [type.literal.text] : null;
|
|
42
|
+
if (!ts.isUnionTypeNode(type)) return null;
|
|
43
|
+
const names = type.types.map((member) => RuntimeKeyNames.#literalsOf(member));
|
|
44
|
+
return names.every((member): member is string[] => member !== null) ? [...new Set(names.flat())] : null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
export type RuntimeThisOwner =
|
|
4
|
+
| { kind: "class"; node: ts.ClassLikeDeclaration; isStatic: boolean }
|
|
5
|
+
| { kind: "function"; node: ts.SignatureDeclaration };
|
|
6
|
+
|
|
7
|
+
export class RuntimeLexicalScope {
|
|
8
|
+
readonly #declared = new WeakMap<ts.Node, Map<string, ts.Identifier>>();
|
|
9
|
+
|
|
10
|
+
resolve(identifier: ts.Identifier): ts.Identifier | null {
|
|
11
|
+
for (let node: ts.Node | undefined = identifier.parent; node; node = node.parent) {
|
|
12
|
+
if (ts.isFunctionLike(node) && RuntimeLexicalScope.evaluatedOutside(node, identifier)) continue;
|
|
13
|
+
const names = this.#namesOf(node);
|
|
14
|
+
const found = names?.get(identifier.text);
|
|
15
|
+
if (found) return found;
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static thisOwnerOf(node: ts.Node): RuntimeThisOwner | null {
|
|
21
|
+
for (let at: ts.Node | undefined = node.parent; at; at = at.parent) {
|
|
22
|
+
if (ts.isArrowFunction(at) || ts.isClassLike(at)) continue;
|
|
23
|
+
if (ts.isClassElement(at) && ts.isClassLike(at.parent)) {
|
|
24
|
+
if (RuntimeLexicalScope.evaluatedOutside(at, node)) continue;
|
|
25
|
+
const isStatic =
|
|
26
|
+
ts.isClassStaticBlockDeclaration(at) || !!(ts.getCombinedModifierFlags(at) & ts.ModifierFlags.Static);
|
|
27
|
+
return { kind: "class", node: at.parent, isStatic };
|
|
28
|
+
}
|
|
29
|
+
if (ts.isFunctionLike(at)) return { kind: "function", node: at };
|
|
30
|
+
if (ts.isSourceFile(at) || ts.isModuleBlock(at)) return null;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//? A computed member name and a decorator run where the member is defined, outside its own scope and `this`.
|
|
36
|
+
static evaluatedOutside(member: ts.Node, node: ts.Node) {
|
|
37
|
+
const within = (outer: ts.Node) => node.pos >= outer.pos && node.end <= outer.end;
|
|
38
|
+
const name = (member as ts.NamedDeclaration).name;
|
|
39
|
+
if (name && ts.isComputedPropertyName(name) && within(name)) return true;
|
|
40
|
+
const parameters = ts.isFunctionLike(member) ? member.parameters : [];
|
|
41
|
+
return [member, ...parameters].some(
|
|
42
|
+
(decorated) => ts.canHaveDecorators(decorated) && (ts.getDecorators(decorated) ?? []).some(within),
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static isReference(identifier: ts.Identifier) {
|
|
47
|
+
const parent = identifier.parent;
|
|
48
|
+
if (ts.isPropertyAccessExpression(parent)) return parent.expression === identifier;
|
|
49
|
+
if (ts.isQualifiedName(parent)) return parent.left === identifier;
|
|
50
|
+
if (ts.isBindingElement(parent)) return parent.initializer === identifier;
|
|
51
|
+
if (ts.isPropertyAssignment(parent) || ts.isPropertyDeclaration(parent) || ts.isParameter(parent))
|
|
52
|
+
return parent.initializer === identifier;
|
|
53
|
+
if (ts.isVariableDeclaration(parent) || ts.isEnumMember(parent)) return parent.initializer === identifier;
|
|
54
|
+
if (
|
|
55
|
+
ts.isImportSpecifier(parent) ||
|
|
56
|
+
ts.isImportClause(parent) ||
|
|
57
|
+
ts.isNamespaceImport(parent) ||
|
|
58
|
+
ts.isExportSpecifier(parent) ||
|
|
59
|
+
ts.isJsxAttribute(parent) ||
|
|
60
|
+
ts.isLabeledStatement(parent) ||
|
|
61
|
+
ts.isBreakOrContinueStatement(parent) ||
|
|
62
|
+
ts.isTypeParameterDeclaration(parent)
|
|
63
|
+
)
|
|
64
|
+
return false;
|
|
65
|
+
if (
|
|
66
|
+
(ts.isFunctionLike(parent) ||
|
|
67
|
+
ts.isClassLike(parent) ||
|
|
68
|
+
ts.isInterfaceDeclaration(parent) ||
|
|
69
|
+
ts.isTypeAliasDeclaration(parent) ||
|
|
70
|
+
ts.isEnumDeclaration(parent) ||
|
|
71
|
+
ts.isModuleDeclaration(parent) ||
|
|
72
|
+
ts.isPropertySignature(parent) ||
|
|
73
|
+
ts.isGetAccessor(parent) ||
|
|
74
|
+
ts.isSetAccessor(parent)) &&
|
|
75
|
+
(parent as ts.NamedDeclaration).name === identifier
|
|
76
|
+
)
|
|
77
|
+
return false;
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
#namesOf(node: ts.Node) {
|
|
82
|
+
const cached = this.#declared.get(node);
|
|
83
|
+
if (cached) return cached;
|
|
84
|
+
const names = RuntimeLexicalScope.#collect(node);
|
|
85
|
+
if (names) this.#declared.set(node, names);
|
|
86
|
+
return names;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
static #collect(node: ts.Node): Map<string, ts.Identifier> | null {
|
|
90
|
+
const names = new Map<string, ts.Identifier>();
|
|
91
|
+
const addBinding = (name: ts.BindingName) => {
|
|
92
|
+
if (ts.isIdentifier(name)) names.set(name.text, name);
|
|
93
|
+
else for (const element of name.elements) if (!ts.isOmittedExpression(element)) addBinding(element.name);
|
|
94
|
+
};
|
|
95
|
+
const addStatements = (statements: readonly ts.Statement[]) => {
|
|
96
|
+
for (const statement of statements) {
|
|
97
|
+
if (ts.isVariableStatement(statement))
|
|
98
|
+
for (const declaration of statement.declarationList.declarations) addBinding(declaration.name);
|
|
99
|
+
else if (
|
|
100
|
+
(ts.isFunctionDeclaration(statement) ||
|
|
101
|
+
ts.isClassDeclaration(statement) ||
|
|
102
|
+
ts.isEnumDeclaration(statement)) &&
|
|
103
|
+
statement.name
|
|
104
|
+
)
|
|
105
|
+
names.set(statement.name.text, statement.name);
|
|
106
|
+
else if (ts.isImportDeclaration(statement)) RuntimeLexicalScope.#addImport(statement, names);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
if (ts.isSourceFile(node) || ts.isBlock(node) || ts.isModuleBlock(node)) addStatements(node.statements);
|
|
110
|
+
else if (ts.isCaseBlock(node)) for (const clause of node.clauses) addStatements(clause.statements);
|
|
111
|
+
else if (ts.isFunctionLike(node)) {
|
|
112
|
+
for (const parameter of node.parameters) addBinding(parameter.name);
|
|
113
|
+
if (ts.isFunctionExpression(node) && node.name) names.set(node.name.text, node.name);
|
|
114
|
+
} else if (ts.isClassExpression(node) && node.name) names.set(node.name.text, node.name);
|
|
115
|
+
else if (ts.isForStatement(node) || ts.isForInStatement(node) || ts.isForOfStatement(node)) {
|
|
116
|
+
const initializer = node.initializer;
|
|
117
|
+
if (initializer && ts.isVariableDeclarationList(initializer))
|
|
118
|
+
for (const declaration of initializer.declarations) addBinding(declaration.name);
|
|
119
|
+
} else if (ts.isCatchClause(node) && node.variableDeclaration) addBinding(node.variableDeclaration.name);
|
|
120
|
+
else return null;
|
|
121
|
+
return names;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
static #addImport(statement: ts.ImportDeclaration, names: Map<string, ts.Identifier>) {
|
|
125
|
+
const clause = statement.importClause;
|
|
126
|
+
if (!clause) return;
|
|
127
|
+
if (clause.name) names.set(clause.name.text, clause.name);
|
|
128
|
+
const bindings = clause.namedBindings;
|
|
129
|
+
if (bindings && ts.isNamespaceImport(bindings)) names.set(bindings.name.text, bindings.name);
|
|
130
|
+
else if (bindings) for (const element of bindings.elements) names.set(element.name.text, element.name);
|
|
131
|
+
}
|
|
132
|
+
}
|