@akanjs/devkit 3.3.0-rc.7 → 3.3.0-rc.70
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 +10 -3
- package/README.md +10 -4
- package/akanApp/akanApp.host.test.ts +4 -2
- 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 +320 -21
- package/akanConfig/akanConfig.ts +113 -272
- 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 +229 -0
- package/akanConfig/index.ts +3 -0
- package/akanConfig/nativeFileSettings.ts +33 -0
- package/akanConfig/nativeKeys.ts +52 -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 +358 -5
- package/app/applicationBuildRunner.ts +221 -31
- 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 +4 -1
- 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 +8 -1
- package/codeAgent/skills/akan-code/SKILL.md +38 -3
- package/codeAgent/skills/akan-debug/SKILL.md +8 -3
- 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 +686 -13
- package/frontendBuild/clientBuildTypes.ts +26 -0
- package/frontendBuild/clientEntriesBundler.ts +49 -1
- package/frontendBuild/clientEntryDiscovery.test.ts +308 -1
- package/frontendBuild/clientEntryDiscovery.ts +136 -14
- 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/devBarrelImports.test.ts +154 -0
- package/frontendBuild/devBarrelImports.ts +83 -23
- 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/guideline/agentAssets.ts +1 -1
- package/incrementalBuilder/buildBatch.proc.ts +35 -9
- package/incrementalBuilder/buildBatchProtocol.ts +2 -1
- package/incrementalBuilder/builderBoot.ts +5 -5
- package/incrementalBuilder/builderGraphPayloads.ts +4 -4
- package/incrementalBuilder/builderLanes.ts +2 -2
- package/incrementalBuilder/builderWatch.ts +19 -9
- package/incrementalBuilder/csrRegistry.ts +4 -4
- package/incrementalBuilder/incrementalBuilder.proc.test.ts +159 -0
- package/incrementalBuilder/incrementalBuilder.proc.ts +38 -10
- package/incrementalBuilder/pendingMetadata.test.ts +27 -0
- package/incrementalBuilder/pendingMetadata.ts +19 -0
- package/incrementalBuilder/ssrRegistry.ts +6 -6
- package/index.ts +1 -1
- package/integration/devStability.integration.test.ts +46 -21
- package/integration/renderAdmissionSoak.test.ts +99 -0
- package/integration/renderAdmissionSoak.ts +609 -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 +7 -5
- package/mobile/nativeApp.ts +2 -2
- package/mobile/nativeConfig.test.ts +96 -1
- package/mobile/nativeConfig.ts +46 -12
- package/mobile/nativePublicEnv.ts +1 -1
- package/mobile/serverBundleSecrets.ts +43 -0
- package/package.json +22 -178
- package/packageExports.test.ts +39 -24
- package/preview/PreviewView.ts +25 -3
- 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 +26 -4
- 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 +208 -0
- package/transforms/secretEnvDeclaration.ts +213 -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 +148 -0
- package/transforms/secretEnvSource.ts +166 -0
- package/transforms/serverEnvEvaluationError.ts +11 -0
- package/transforms/serverEnvPlugin.test.ts +2 -1
- package/transforms/serverEnvPlugin.ts +13 -0
- package/transforms/serverEnvProfile.test.ts +129 -2
- package/transforms/serverEnvProfile.ts +126 -40
- package/transforms/serverEnvSecrets.test.ts +292 -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 +180 -3
- package/transforms/tsconfigPackageResolver.test.ts +1 -0
- package/tunnel/createTunnel.test.ts +29 -8
- package/tunnel/createTunnel.ts +22 -11
- 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/fileEditor.ts +1 -0
- package/util/jsonFormatter.test.ts +13 -9
- package/util/selectModel.ts +1 -0
- 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/getRelatedCnsts.ts +7 -0
- 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 -2718
- 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 -606
- package/codeAgent/agent/CodeAgentAsks.ts +0 -66
- package/codeAgent/agent/CodeAgentEventMapper.ts +0 -225
- package/codeAgent/agent/CodeAgentGate.ts +0 -239
- 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 -275
- package/codeAgent/agent/codeAgentContract.test.ts +0 -643
- 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,488 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { RuntimeLexicalScope } from "./runtimeLexicalScope";
|
|
3
|
+
import { TranslatorBindings } from "./translatorBindings";
|
|
4
|
+
|
|
5
|
+
export interface DictionaryKeyUnread {
|
|
6
|
+
line: number;
|
|
7
|
+
why: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface DictionaryKeyRead {
|
|
11
|
+
//? The first segment of every string the file holds, and every string that is one name: a superset of its groups.
|
|
12
|
+
names: string[];
|
|
13
|
+
//? PascalCase names, which a constant table turns into the groups of the enums, scalars and models they name.
|
|
14
|
+
classNames: string[];
|
|
15
|
+
//? One-name strings handed to a component as a prop (`modelName="memo"`): what a server module tells client UI.
|
|
16
|
+
propNames: string[];
|
|
17
|
+
api: boolean;
|
|
18
|
+
unread: DictionaryKeyUnread[];
|
|
19
|
+
//? A framework surface that shows the texts of whichever module its data names, not of the modules a page loads.
|
|
20
|
+
anyModule: string | null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type DictionaryStaticKeys = (className: string, member: string) => readonly string[] | null;
|
|
24
|
+
|
|
25
|
+
const leadName = /^([A-Za-z_$][\w$]*)(?:\.|$)/;
|
|
26
|
+
const className = /^[A-Z][A-Za-z0-9]*$/;
|
|
27
|
+
const apiKey = /\.signal\.[^.\s]*\.(?:desc|arg)\b/;
|
|
28
|
+
const anyModuleSurfaces = new Set(["Signal", "Constant"]);
|
|
29
|
+
const frameworkUi = /^akanjs\/ui(?:\/(Signal|Constant)(?:\/|$)|$)/;
|
|
30
|
+
const oneName = /^[A-Za-z_$][\w$]*$/;
|
|
31
|
+
//? `Data.Dashboard` labels its columns `summary.<column>` unless it is handed another `summaryRefName`.
|
|
32
|
+
const impliedNames = new Map([["Dashboard", "summary"]]);
|
|
33
|
+
const tableMethods = new Set(["map", "forEach", "filter", "flatMap", "find", "findLast", "some", "every"]);
|
|
34
|
+
const subsetMethods = new Set(["filter", "slice", "sort", "toSorted", "reverse", "toReversed", "concat", "at"]);
|
|
35
|
+
const objectReaders = new Set(["keys", "values", "entries"]);
|
|
36
|
+
const memberCalls = new Set(["field", "enum", "trans"]);
|
|
37
|
+
const translatorChain = new Set(["_", "rich"]);
|
|
38
|
+
const groupHead = /^[A-Za-z_$][\w$]*\./;
|
|
39
|
+
const maxDepth = 24;
|
|
40
|
+
const constantModule = /\.constant(?:\.ts)?$/;
|
|
41
|
+
const constantsName = "cnst";
|
|
42
|
+
|
|
43
|
+
const strip = (node: ts.Expression): ts.Expression =>
|
|
44
|
+
ts.isParenthesizedExpression(node) ||
|
|
45
|
+
ts.isAsExpression(node) ||
|
|
46
|
+
ts.isSatisfiesExpression(node) ||
|
|
47
|
+
ts.isNonNullExpression(node) ||
|
|
48
|
+
ts.isTypeAssertionExpression(node)
|
|
49
|
+
? strip(node.expression)
|
|
50
|
+
: node;
|
|
51
|
+
|
|
52
|
+
//* Which dictionary groups a client file can show: the keys it hands `l`, `l._`, `l.rich` or `translate` must come from
|
|
53
|
+
//* strings the file writes (a literal, a template with a literal group, a table, a typed union) or a constant's static.
|
|
54
|
+
export class DictionaryKeys {
|
|
55
|
+
readonly #sourceFile: ts.SourceFile;
|
|
56
|
+
readonly #scope = new RuntimeLexicalScope();
|
|
57
|
+
readonly #translators = new Set(["l"]);
|
|
58
|
+
readonly #bindings: TranslatorBindings;
|
|
59
|
+
readonly #staticKeys: DictionaryStaticKeys | undefined;
|
|
60
|
+
readonly #names = new Set<string>();
|
|
61
|
+
readonly #classNames = new Set<string>();
|
|
62
|
+
readonly #propNames = new Set<string>();
|
|
63
|
+
readonly #unread: DictionaryKeyUnread[] = [];
|
|
64
|
+
#api = false;
|
|
65
|
+
#anyModule: string | null = null;
|
|
66
|
+
|
|
67
|
+
private constructor(sourceFile: ts.SourceFile, staticKeys: DictionaryStaticKeys | undefined) {
|
|
68
|
+
this.#sourceFile = sourceFile;
|
|
69
|
+
this.#staticKeys = staticKeys;
|
|
70
|
+
this.#bindings = new TranslatorBindings(sourceFile, this.#scope, this.#translators);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static read(text: string, fileName: string, staticKeys?: DictionaryStaticKeys): DictionaryKeyRead {
|
|
74
|
+
const kind = fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
75
|
+
const sourceFile = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, kind);
|
|
76
|
+
return new DictionaryKeys(sourceFile, staticKeys).#read();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
#read(): DictionaryKeyRead {
|
|
80
|
+
this.#collectAliases(this.#sourceFile);
|
|
81
|
+
this.#visit(this.#sourceFile);
|
|
82
|
+
return {
|
|
83
|
+
names: [...this.#names].sort(),
|
|
84
|
+
classNames: [...this.#classNames].sort(),
|
|
85
|
+
propNames: [...this.#propNames].sort(),
|
|
86
|
+
api: this.#api,
|
|
87
|
+
unread: this.#unread,
|
|
88
|
+
anyModule: this.#anyModule,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
#collectAliases(node: ts.Node) {
|
|
93
|
+
if (ts.isBindingElement(node) && node.propertyName && ts.isIdentifier(node.name)) {
|
|
94
|
+
const property = node.propertyName;
|
|
95
|
+
if (ts.isIdentifier(property) && property.text === "l") this.#translators.add(node.name.text);
|
|
96
|
+
}
|
|
97
|
+
ts.forEachChild(node, (child) => this.#collectAliases(child));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
#visit(node: ts.Node) {
|
|
101
|
+
if (ts.isExpression(node) && this.#isTranslator(node) && this.#isReference(node)) this.#checkTranslatorUse(node);
|
|
102
|
+
if (ts.isImportDeclaration(node)) this.#readImport(node);
|
|
103
|
+
else if (ts.isStringLiteralLike(node)) this.#addText(node.text, node.text);
|
|
104
|
+
else if (ts.isTemplateExpression(node))
|
|
105
|
+
this.#addText(node.head.text, [node.head.text, ...node.templateSpans.map((span) => span.literal.text)].join("*"));
|
|
106
|
+
else if (ts.isTemplateLiteralTypeNode(node))
|
|
107
|
+
this.#addText(node.head.text, [node.head.text, ...node.templateSpans.map((span) => span.literal.text)].join("*"));
|
|
108
|
+
else if (ts.isIdentifier(node) && className.test(node.text)) this.#classNames.add(node.text);
|
|
109
|
+
else if (ts.isCallExpression(node)) this.#readCall(node);
|
|
110
|
+
else if (ts.isJsxAttribute(node) && node.initializer && ts.isStringLiteral(node.initializer)) {
|
|
111
|
+
if (oneName.test(node.initializer.text)) this.#propNames.add(node.initializer.text);
|
|
112
|
+
}
|
|
113
|
+
const implied = ts.isPropertyAccessExpression(node) ? impliedNames.get(node.name.text) : undefined;
|
|
114
|
+
if (implied) {
|
|
115
|
+
this.#names.add(implied);
|
|
116
|
+
this.#propNames.add(implied);
|
|
117
|
+
}
|
|
118
|
+
ts.forEachChild(node, (child) => this.#visit(child));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
#addText(lead: string, pattern: string) {
|
|
122
|
+
const name = lead.match(leadName)?.[1];
|
|
123
|
+
if (name) this.#names.add(name);
|
|
124
|
+
if (apiKey.test(pattern)) this.#api = true;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
#readImport(node: ts.ImportDeclaration) {
|
|
128
|
+
if (!ts.isStringLiteral(node.moduleSpecifier) || node.importClause?.isTypeOnly) return;
|
|
129
|
+
const specifier = node.moduleSpecifier.text;
|
|
130
|
+
const [matched, surface] = specifier.match(frameworkUi) ?? [];
|
|
131
|
+
if (!matched) return;
|
|
132
|
+
const bindings = node.importClause?.namedBindings;
|
|
133
|
+
const named =
|
|
134
|
+
bindings && ts.isNamedImports(bindings)
|
|
135
|
+
? bindings.elements
|
|
136
|
+
.filter((element) => !element.isTypeOnly)
|
|
137
|
+
.map((element) => (element.propertyName ?? element.name).text)
|
|
138
|
+
: [];
|
|
139
|
+
const shown = surface ?? named.find((name) => anyModuleSurfaces.has(name));
|
|
140
|
+
if (shown) this.#anyModule ??= `${shown} from ${specifier} shows the texts of any module its data names`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
#readCall(call: ts.CallExpression) {
|
|
144
|
+
const key = this.#keyOf(call);
|
|
145
|
+
if (!key || this.#follows(key, 0)) return;
|
|
146
|
+
this.#fail(key, this.#whyOf(strip(key)));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
#isReference(node: ts.Expression): boolean {
|
|
150
|
+
const { parent } = node;
|
|
151
|
+
if (ts.isShorthandPropertyAssignment(parent)) return true;
|
|
152
|
+
if ((parent as { name?: ts.Node }).name === node) return false;
|
|
153
|
+
if (ts.isBindingElement(parent) || ts.isImportSpecifier(parent) || ts.isExportSpecifier(parent))
|
|
154
|
+
return parent.propertyName !== node;
|
|
155
|
+
return !ts.isTypeQueryNode(parent) && !ts.isQualifiedName(parent);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
#checkTranslatorUse(node: ts.Expression) {
|
|
159
|
+
const outerOf = (inner: ts.Node): ts.Node => {
|
|
160
|
+
let outer = inner;
|
|
161
|
+
while (
|
|
162
|
+
ts.isParenthesizedExpression(outer.parent) ||
|
|
163
|
+
ts.isAsExpression(outer.parent) ||
|
|
164
|
+
ts.isSatisfiesExpression(outer.parent) ||
|
|
165
|
+
ts.isNonNullExpression(outer.parent) ||
|
|
166
|
+
ts.isTypeAssertionExpression(outer.parent)
|
|
167
|
+
)
|
|
168
|
+
outer = outer.parent;
|
|
169
|
+
return outer;
|
|
170
|
+
};
|
|
171
|
+
const outer = outerOf(node);
|
|
172
|
+
const { parent } = outer;
|
|
173
|
+
if (ts.isCallExpression(parent) && parent.expression === outer) return;
|
|
174
|
+
if (ts.isPropertyAccessExpression(parent) && parent.expression === outer) {
|
|
175
|
+
if (translatorChain.has(parent.name.text)) return;
|
|
176
|
+
const access = outerOf(parent);
|
|
177
|
+
if (
|
|
178
|
+
memberCalls.has(parent.name.text) &&
|
|
179
|
+
ts.isCallExpression(access.parent) &&
|
|
180
|
+
access.parent.expression === access
|
|
181
|
+
)
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
this.#fail(
|
|
185
|
+
node,
|
|
186
|
+
ts.isCallExpression(parent)
|
|
187
|
+
? "passes the translator on"
|
|
188
|
+
: "is used other than as a call, which no key can be read from",
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
#isTranslator(node: ts.Expression): boolean {
|
|
193
|
+
if (ts.isIdentifier(node)) return this.#translators.has(node.text) && !this.#bindings.isOther(node);
|
|
194
|
+
if (!ts.isPropertyAccessExpression(node)) return false;
|
|
195
|
+
const name = node.name.text;
|
|
196
|
+
if (name === "l") return true;
|
|
197
|
+
return (name === "_" || name === "rich") && this.#isTranslator(strip(node.expression));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
#keyOf(call: ts.CallExpression): ts.Expression | undefined {
|
|
201
|
+
const callee = strip(call.expression);
|
|
202
|
+
if (this.#isTranslator(callee)) return call.arguments[0];
|
|
203
|
+
const name = ts.isIdentifier(callee)
|
|
204
|
+
? callee.text
|
|
205
|
+
: ts.isPropertyAccessExpression(callee)
|
|
206
|
+
? callee.name.text
|
|
207
|
+
: null;
|
|
208
|
+
if (name === "translate" || name === "translateByLocale")
|
|
209
|
+
return call.arguments.length > 1 ? call.arguments[1] : undefined;
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
#fail(node: ts.Node, why: string) {
|
|
214
|
+
const { line } = this.#sourceFile.getLineAndCharacterOfPosition(node.getStart(this.#sourceFile));
|
|
215
|
+
const text = node.getText(this.#sourceFile).replace(/\s+/g, " ");
|
|
216
|
+
this.#unread.push({ line: line + 1, why: `${text.length > 60 ? `${text.slice(0, 57)}...` : text} ${why}` });
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
#whyOf(node: ts.Expression): string {
|
|
220
|
+
if (ts.isCallExpression(node)) return "is a call's result";
|
|
221
|
+
if (ts.isIdentifier(node)) {
|
|
222
|
+
const declared = this.#scope.resolve(node)?.parent;
|
|
223
|
+
if (!declared) return "is not declared in this file";
|
|
224
|
+
if (ts.isImportSpecifier(declared) || ts.isImportClause(declared) || ts.isNamespaceImport(declared))
|
|
225
|
+
return "is imported from another file";
|
|
226
|
+
if (ts.isParameter(this.#rootOf(declared) ?? declared)) return "is a parameter or prop whose type names no key";
|
|
227
|
+
return "is a variable this file does not hold as a written key";
|
|
228
|
+
}
|
|
229
|
+
if (ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node))
|
|
230
|
+
return "is read from data at run time";
|
|
231
|
+
return "is built from data at run time";
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
#rootOf(node: ts.Node): ts.Node | null {
|
|
235
|
+
let at: ts.Node = node;
|
|
236
|
+
while (ts.isBindingElement(at) || ts.isObjectBindingPattern(at) || ts.isArrayBindingPattern(at)) at = at.parent;
|
|
237
|
+
return at === node ? null : at;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
#follows(expression: ts.Expression, depth: number): boolean {
|
|
241
|
+
if (depth > maxDepth) return false;
|
|
242
|
+
const next = (inner: ts.Expression) => this.#follows(inner, depth + 1);
|
|
243
|
+
const node = strip(expression);
|
|
244
|
+
if (ts.isStringLiteralLike(node) || DictionaryKeys.#isNoKey(node)) return true;
|
|
245
|
+
if (ts.isTemplateExpression(node)) {
|
|
246
|
+
if (node.head.text.includes(".")) return true;
|
|
247
|
+
const [first] = node.templateSpans;
|
|
248
|
+
return !node.head.text && !!first && first.literal.text.startsWith(".") && next(first.expression);
|
|
249
|
+
}
|
|
250
|
+
if (ts.isConditionalExpression(node)) return next(node.whenTrue) && next(node.whenFalse);
|
|
251
|
+
if (ts.isBinaryExpression(node)) {
|
|
252
|
+
const operator = node.operatorToken.kind;
|
|
253
|
+
if (operator === ts.SyntaxKind.QuestionQuestionToken || operator === ts.SyntaxKind.BarBarToken)
|
|
254
|
+
return next(node.left) && next(node.right);
|
|
255
|
+
if (operator === ts.SyntaxKind.AmpersandAmpersandToken) return next(node.right);
|
|
256
|
+
if (operator !== ts.SyntaxKind.PlusToken) return false;
|
|
257
|
+
const left = strip(node.left);
|
|
258
|
+
const right = strip(node.right);
|
|
259
|
+
if (ts.isStringLiteralLike(left)) return left.text.includes(".");
|
|
260
|
+
return next(left) && ts.isStringLiteralLike(right) && right.text.startsWith(".");
|
|
261
|
+
}
|
|
262
|
+
if (ts.isIdentifier(node)) return this.#bindingFollows(node, depth + 1, false);
|
|
263
|
+
if (ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node))
|
|
264
|
+
return this.#isTable(node.expression, depth + 1);
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
static #isNoKey(node: ts.Expression): boolean {
|
|
269
|
+
const { kind } = node;
|
|
270
|
+
if (kind === ts.SyntaxKind.NullKeyword || kind === ts.SyntaxKind.FalseKeyword) return true;
|
|
271
|
+
return ts.isIdentifier(node) && node.text === "undefined";
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
//? With `names`, the property names are the keys: each one is added to the groups, and a name that is not written fails.
|
|
275
|
+
#isTable(expression: ts.Expression, depth: number, names = false): boolean {
|
|
276
|
+
if (depth > maxDepth) return false;
|
|
277
|
+
const node = strip(expression);
|
|
278
|
+
const value = (inner: ts.Expression) => this.#isValue(inner, depth + 1);
|
|
279
|
+
if (ts.isArrayLiteralExpression(node))
|
|
280
|
+
return node.elements.every((element) =>
|
|
281
|
+
ts.isSpreadElement(element) ? this.#isTable(element.expression, depth + 1, names) : value(element),
|
|
282
|
+
);
|
|
283
|
+
if (ts.isObjectLiteralExpression(node))
|
|
284
|
+
return node.properties.every((property) => {
|
|
285
|
+
if (names && !this.#addPropertyName(property)) return false;
|
|
286
|
+
if (ts.isPropertyAssignment(property)) return value(property.initializer);
|
|
287
|
+
if (ts.isShorthandPropertyAssignment(property)) return value(property.name);
|
|
288
|
+
if (ts.isSpreadAssignment(property)) return this.#isTable(property.expression, depth + 1, names);
|
|
289
|
+
return ts.isMethodDeclaration(property);
|
|
290
|
+
});
|
|
291
|
+
if (ts.isIdentifier(node)) return this.#bindingFollows(node, depth + 1, true, names);
|
|
292
|
+
if (ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node))
|
|
293
|
+
return !names && (this.#isConstantTable(node) || this.#isTable(node.expression, depth + 1));
|
|
294
|
+
if (!ts.isCallExpression(node)) return false;
|
|
295
|
+
const callee = strip(node.expression);
|
|
296
|
+
if (!ts.isPropertyAccessExpression(callee)) return false;
|
|
297
|
+
const method = callee.name.text;
|
|
298
|
+
if (ts.isIdentifier(callee.expression) && callee.expression.text === "Object" && objectReaders.has(method))
|
|
299
|
+
return !!node.arguments[0] && this.#isTable(node.arguments[0], depth + 1, method !== "values");
|
|
300
|
+
if (method === "map") return !names && this.#isMapped(node, depth);
|
|
301
|
+
if (!subsetMethods.has(method)) return false;
|
|
302
|
+
const own = this.#isTable(callee.expression, depth + 1, names);
|
|
303
|
+
return own && (method !== "concat" || node.arguments.every((argument) => this.#isTable(argument, depth + 1)));
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
#isConstantTable(node: ts.PropertyAccessExpression | ts.ElementAccessExpression): boolean {
|
|
307
|
+
if (!this.#staticKeys || !ts.isPropertyAccessExpression(node) || !ts.isIdentifier(node.name)) return false;
|
|
308
|
+
const owner = this.#constantClass(strip(node.expression));
|
|
309
|
+
const keys = owner ? this.#staticKeys(owner, node.name.text) : null;
|
|
310
|
+
for (const key of keys ?? []) this.#addText(key, key);
|
|
311
|
+
return !!keys;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
//? `cnst.Memo`, `cnst.shared.Memo` through a lib's constants, or `Memo` imported from a constant file.
|
|
315
|
+
#constantClass(node: ts.Expression): string | null {
|
|
316
|
+
if (ts.isIdentifier(node)) {
|
|
317
|
+
const imported = this.#scope.resolve(node)?.parent;
|
|
318
|
+
if (!imported || !ts.isImportSpecifier(imported)) return null;
|
|
319
|
+
const from = imported.parent.parent.parent.moduleSpecifier;
|
|
320
|
+
const constant = ts.isStringLiteral(from) && constantModule.test(from.text);
|
|
321
|
+
return constant ? (imported.propertyName ?? imported.name).text : null;
|
|
322
|
+
}
|
|
323
|
+
if (!ts.isPropertyAccessExpression(node) || !className.test(node.name.text)) return null;
|
|
324
|
+
let root = strip(node.expression);
|
|
325
|
+
while (ts.isPropertyAccessExpression(root) && !className.test(root.name.text)) root = strip(root.expression);
|
|
326
|
+
const imported = ts.isIdentifier(root) ? this.#scope.resolve(root)?.parent : undefined;
|
|
327
|
+
if (!imported) return null;
|
|
328
|
+
const source = ts.isNamespaceImport(imported)
|
|
329
|
+
? imported.name
|
|
330
|
+
: ts.isImportSpecifier(imported)
|
|
331
|
+
? (imported.propertyName ?? imported.name)
|
|
332
|
+
: null;
|
|
333
|
+
return source?.text === constantsName ? node.name.text : null;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
#isMapped(call: ts.CallExpression, depth: number): boolean {
|
|
337
|
+
const [callback] = call.arguments;
|
|
338
|
+
if (!callback || !(ts.isArrowFunction(callback) || ts.isFunctionExpression(callback))) return false;
|
|
339
|
+
const returned = ts.isBlock(callback.body) ? DictionaryKeys.#returnsOf(callback.body) : [callback.body];
|
|
340
|
+
return returned.length > 0 && returned.every((value) => !!value && this.#isValue(value, depth + 1));
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
static #returnsOf(block: ts.Block): (ts.Expression | undefined)[] {
|
|
344
|
+
const returned: (ts.Expression | undefined)[] = [];
|
|
345
|
+
const visit = (node: ts.Node) => {
|
|
346
|
+
if (ts.isFunctionLike(node)) return;
|
|
347
|
+
if (ts.isReturnStatement(node)) returned.push(node.expression);
|
|
348
|
+
else ts.forEachChild(node, visit);
|
|
349
|
+
};
|
|
350
|
+
ts.forEachChild(block, visit);
|
|
351
|
+
return returned;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
#isTranslated(node: ts.Expression): boolean {
|
|
355
|
+
if (!ts.isCallExpression(node)) return false;
|
|
356
|
+
const callee = strip(node.expression);
|
|
357
|
+
if (this.#isTranslator(callee)) return true;
|
|
358
|
+
if (!ts.isPropertyAccessExpression(callee) || !memberCalls.has(callee.name.text)) return false;
|
|
359
|
+
return this.#isTranslator(strip(callee.expression));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
#addPropertyName(property: ts.ObjectLiteralElementLike): boolean {
|
|
363
|
+
if (ts.isSpreadAssignment(property)) return true;
|
|
364
|
+
const { name } = property;
|
|
365
|
+
if (ts.isComputedPropertyName(name)) {
|
|
366
|
+
const inner = strip(name.expression);
|
|
367
|
+
if (!ts.isStringLiteralLike(inner) && !ts.isNumericLiteral(inner)) return false;
|
|
368
|
+
this.#addText(inner.text, inner.text);
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
if (ts.isPrivateIdentifier(name)) return false;
|
|
372
|
+
this.#addText(name.text, name.text);
|
|
373
|
+
return true;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
//? A value in a table is either text the file writes or something no key is made from (a number, an icon, a handler).
|
|
377
|
+
#isValue(expression: ts.Expression, depth: number): boolean {
|
|
378
|
+
const node = strip(expression);
|
|
379
|
+
if (
|
|
380
|
+
ts.isNumericLiteral(node) ||
|
|
381
|
+
node.kind === ts.SyntaxKind.TrueKeyword ||
|
|
382
|
+
node.kind === ts.SyntaxKind.FalseKeyword ||
|
|
383
|
+
node.kind === ts.SyntaxKind.NullKeyword ||
|
|
384
|
+
ts.isArrowFunction(node) ||
|
|
385
|
+
ts.isFunctionExpression(node) ||
|
|
386
|
+
ts.isJsxElement(node) ||
|
|
387
|
+
ts.isJsxSelfClosingElement(node) ||
|
|
388
|
+
ts.isJsxFragment(node) ||
|
|
389
|
+
(ts.isIdentifier(node) && (node.text === "undefined" || className.test(node.text))) ||
|
|
390
|
+
this.#isTranslated(node)
|
|
391
|
+
)
|
|
392
|
+
return true;
|
|
393
|
+
return this.#follows(node, depth) || this.#isTable(node, depth);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
#bindingFollows(identifier: ts.Identifier, depth: number, asTable: boolean, names = false): boolean {
|
|
397
|
+
const declared = this.#scope.resolve(identifier);
|
|
398
|
+
if (!declared) return false;
|
|
399
|
+
const declaration = declared.parent;
|
|
400
|
+
const root = this.#rootOf(declaration) ?? declaration;
|
|
401
|
+
if (ts.isParameter(root)) return !names && this.#parameterFollows(root, declaration, depth, asTable);
|
|
402
|
+
if (!ts.isVariableDeclaration(root)) return false;
|
|
403
|
+
const list = root.parent;
|
|
404
|
+
if (ts.isVariableDeclarationList(list) && ts.isForInStatement(list.parent))
|
|
405
|
+
return !asTable && !names && root === declaration && this.#isTable(list.parent.expression, depth + 1, true);
|
|
406
|
+
if (ts.isVariableDeclarationList(list) && ts.isForOfStatement(list.parent))
|
|
407
|
+
return !names && this.#isTable(list.parent.expression, depth + 1);
|
|
408
|
+
if (!ts.isVariableDeclarationList(list) || !(list.flags & ts.NodeFlags.Const) || !root.initializer) return false;
|
|
409
|
+
if (root !== declaration) return !names && this.#isTable(root.initializer, depth + 1);
|
|
410
|
+
return asTable ? this.#isTable(root.initializer, depth + 1, names) : this.#follows(root.initializer, depth + 1);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
#parameterFollows(parameter: ts.ParameterDeclaration, declaration: ts.Node, depth: number, asTable: boolean) {
|
|
414
|
+
if (parameter.type) {
|
|
415
|
+
const property = ts.isBindingElement(declaration) ? (declaration.propertyName ?? declaration.name) : null;
|
|
416
|
+
if (!property) return this.#typeFollows(parameter.type, depth + 1, asTable);
|
|
417
|
+
const member = ts.isIdentifier(property) ? this.#memberType(parameter.type, property.text, depth) : null;
|
|
418
|
+
return !!member && this.#typeFollows(member, depth + 1, asTable);
|
|
419
|
+
}
|
|
420
|
+
const callback = parameter.parent;
|
|
421
|
+
const call = callback.parent;
|
|
422
|
+
if (!(ts.isArrowFunction(callback) || ts.isFunctionExpression(callback)) || !ts.isCallExpression(call))
|
|
423
|
+
return false;
|
|
424
|
+
const callee = strip(call.expression);
|
|
425
|
+
return (
|
|
426
|
+
ts.isPropertyAccessExpression(callee) &&
|
|
427
|
+
tableMethods.has(callee.name.text) &&
|
|
428
|
+
callback.parameters[0] === parameter &&
|
|
429
|
+
this.#isTable(callee.expression, depth + 1)
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
#memberType(type: ts.TypeNode, name: string, depth: number): ts.TypeNode | null {
|
|
434
|
+
if (depth > maxDepth) return null;
|
|
435
|
+
if (ts.isTypeLiteralNode(type))
|
|
436
|
+
for (const member of type.members)
|
|
437
|
+
if (ts.isPropertySignature(member) && member.name.getText(this.#sourceFile) === name)
|
|
438
|
+
return member.type ?? null;
|
|
439
|
+
if (ts.isIntersectionTypeNode(type)) {
|
|
440
|
+
for (const part of type.types) {
|
|
441
|
+
const found = this.#memberType(part, name, depth + 1);
|
|
442
|
+
if (found) return found;
|
|
443
|
+
}
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
if (!ts.isTypeReferenceNode(type) || !ts.isIdentifier(type.typeName)) return null;
|
|
447
|
+
const declared = this.#typeDeclaration(type.typeName.text);
|
|
448
|
+
if (declared && ts.isTypeAliasDeclaration(declared)) return this.#memberType(declared.type, name, depth + 1);
|
|
449
|
+
if (!declared || !ts.isInterfaceDeclaration(declared)) return null;
|
|
450
|
+
for (const member of declared.members)
|
|
451
|
+
if (ts.isPropertySignature(member) && member.name.getText(this.#sourceFile) === name) return member.type ?? null;
|
|
452
|
+
return null;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
#typeFollows(type: ts.TypeNode, depth: number, asTable: boolean): boolean {
|
|
456
|
+
if (depth > maxDepth) return false;
|
|
457
|
+
const next = (inner: ts.TypeNode) => this.#typeFollows(inner, depth + 1, asTable);
|
|
458
|
+
if (ts.isParenthesizedTypeNode(type)) return next(type.type);
|
|
459
|
+
if (ts.isUnionTypeNode(type)) return type.types.every(next);
|
|
460
|
+
if (ts.isLiteralTypeNode(type)) return true;
|
|
461
|
+
if (type.kind === ts.SyntaxKind.UndefinedKeyword || type.kind === ts.SyntaxKind.NullKeyword) return true;
|
|
462
|
+
if (ts.isTemplateLiteralTypeNode(type)) return groupHead.test(type.head.text);
|
|
463
|
+
if (ts.isArrayTypeNode(type)) return asTable && next(type.elementType);
|
|
464
|
+
if (ts.isTypeOperatorNode(type) && type.operator === ts.SyntaxKind.ReadonlyKeyword) return next(type.type);
|
|
465
|
+
if (ts.isTypeOperatorNode(type) && type.operator === ts.SyntaxKind.KeyOfKeyword)
|
|
466
|
+
return ts.isTypeQueryNode(type.type) && this.#queryIsTable(type.type, depth, true);
|
|
467
|
+
if (ts.isIndexedAccessTypeNode(type)) {
|
|
468
|
+
let object = type.objectType;
|
|
469
|
+
while (ts.isParenthesizedTypeNode(object)) object = object.type;
|
|
470
|
+
return ts.isTypeQueryNode(object) && this.#queryIsTable(object, depth, false);
|
|
471
|
+
}
|
|
472
|
+
if (!ts.isTypeReferenceNode(type) || !ts.isIdentifier(type.typeName)) return false;
|
|
473
|
+
const declared = this.#typeDeclaration(type.typeName.text);
|
|
474
|
+
return !!declared && ts.isTypeAliasDeclaration(declared) && next(declared.type);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
#queryIsTable(query: ts.TypeQueryNode, depth: number, names: boolean): boolean {
|
|
478
|
+
const name = query.exprName;
|
|
479
|
+
return ts.isIdentifier(name) && this.#isTable(name, depth + 1, names);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
#typeDeclaration(name: string): ts.TypeAliasDeclaration | ts.InterfaceDeclaration | undefined {
|
|
483
|
+
return this.#sourceFile.statements.find(
|
|
484
|
+
(statement): statement is ts.TypeAliasDeclaration | ts.InterfaceDeclaration =>
|
|
485
|
+
(ts.isTypeAliasDeclaration(statement) || ts.isInterfaceDeclaration(statement)) && statement.name.text === name,
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { ObjectImportReport } from "./objectImportReport";
|
|
3
|
+
|
|
4
|
+
describe("ObjectImportReport.format", () => {
|
|
5
|
+
test("prints one line per scope with its reasons by count, and where the files are listed", () => {
|
|
6
|
+
const kept = {
|
|
7
|
+
file: "apps/demo/page/_index.tsx",
|
|
8
|
+
line: 3,
|
|
9
|
+
object: "Scroll",
|
|
10
|
+
objectFile: "libs/util/ui/Scroll/index.tsx",
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
expect(
|
|
14
|
+
ObjectImportReport.format({
|
|
15
|
+
scopes: [
|
|
16
|
+
{
|
|
17
|
+
scope: "apps/demo",
|
|
18
|
+
app: "demo",
|
|
19
|
+
narrowed: 12,
|
|
20
|
+
kept: 4,
|
|
21
|
+
reasons: { call: 1, "rendered-or-called": 2, "passed-on": 1 },
|
|
22
|
+
},
|
|
23
|
+
{ scope: "libs/util", app: "demo", narrowed: 3, kept: 0, reasons: {} },
|
|
24
|
+
],
|
|
25
|
+
kept: [{ ...kept, reason: "rendered-or-called" }],
|
|
26
|
+
}),
|
|
27
|
+
).toBe(
|
|
28
|
+
[
|
|
29
|
+
"apps/demo: 12 object imports load only the members they name, 4 load the whole object (rendered-or-called 2, call 1, passed-on 1)",
|
|
30
|
+
"libs/util: 3 object imports load only the members they name, 0 load the whole object",
|
|
31
|
+
"--format json lists each import that loads the whole object, with its file and line",
|
|
32
|
+
].join("\n"),
|
|
33
|
+
);
|
|
34
|
+
expect(ObjectImportReport.format({ scopes: [], kept: [] })).toBe("");
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { AppExecutor } from "../executors/appExecutor";
|
|
3
|
+
import type { WorkspaceExecutor } from "../executors/workspaceExecutor";
|
|
4
|
+
import { BarrelAnalyzer } from "../transforms/barrelAnalyzer";
|
|
5
|
+
import { createTsconfigPackageResolver, rewriteBarrelImports } from "../transforms/barrelImportsPlugin";
|
|
6
|
+
import { type ObjectKeepReason, ObjectMemberImports } from "../transforms/objectMemberImports";
|
|
7
|
+
import { RuntimeScopeReport } from "./runtimeScopeReport";
|
|
8
|
+
import { WorkspaceSourceFiles } from "./workspaceSourceFiles";
|
|
9
|
+
|
|
10
|
+
export interface ObjectImportScope {
|
|
11
|
+
scope: string;
|
|
12
|
+
app: string;
|
|
13
|
+
narrowed: number;
|
|
14
|
+
kept: number;
|
|
15
|
+
reasons: Partial<Record<ObjectKeepReason, number>>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ObjectImportKeptEntry {
|
|
19
|
+
file: string;
|
|
20
|
+
line: number;
|
|
21
|
+
object: string;
|
|
22
|
+
objectFile: string;
|
|
23
|
+
reason: ObjectKeepReason;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ObjectImportResult {
|
|
27
|
+
scopes: ObjectImportScope[];
|
|
28
|
+
kept: ObjectImportKeptEntry[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface ScopeReader {
|
|
32
|
+
barrels: string[];
|
|
33
|
+
analyzer: BarrelAnalyzer;
|
|
34
|
+
objects: ObjectMemberImports;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const MACRO_ATTR_RE = /with\s*\{\s*type\s*:\s*["']macro["']\s*\}/;
|
|
38
|
+
|
|
39
|
+
//* What a build does with each object import (`Memo.Zone.View`, `Field.Text`) in apps and libs: narrowed to the
|
|
40
|
+
//* members it names, or kept whole and why. A lib is read as the first app reads it.
|
|
41
|
+
export class ObjectImportReport {
|
|
42
|
+
static async scan(workspace: WorkspaceExecutor): Promise<ObjectImportResult> {
|
|
43
|
+
const apps = (await workspace.getApps()).sort();
|
|
44
|
+
const result: ObjectImportResult = { scopes: [], kept: [] };
|
|
45
|
+
const [firstApp] = apps;
|
|
46
|
+
if (firstApp === undefined) return result;
|
|
47
|
+
const readers = new Map<string, ScopeReader>();
|
|
48
|
+
const readerOf = async (app: string) => {
|
|
49
|
+
const cached = readers.get(app);
|
|
50
|
+
if (cached) return cached;
|
|
51
|
+
const executor = AppExecutor.from(workspace, app);
|
|
52
|
+
const resolvePackage = await createTsconfigPackageResolver(executor);
|
|
53
|
+
const barrels = (await executor.getConfig()).barrelImports;
|
|
54
|
+
const analyzer = new BarrelAnalyzer({ resolvePackage, packageRoot: workspace.workspaceRoot });
|
|
55
|
+
const resolve = ObjectMemberImports.resolverOf(resolvePackage);
|
|
56
|
+
const reader: ScopeReader = {
|
|
57
|
+
barrels,
|
|
58
|
+
analyzer,
|
|
59
|
+
objects: new ObjectMemberImports({ resolve, barrels: { names: barrels, analyzer } }),
|
|
60
|
+
};
|
|
61
|
+
readers.set(app, reader);
|
|
62
|
+
return reader;
|
|
63
|
+
};
|
|
64
|
+
const sources = new WorkspaceSourceFiles(workspace.workspaceRoot);
|
|
65
|
+
const scopes = new Map<string, ObjectImportScope>();
|
|
66
|
+
for (const file of await sources.collect()) {
|
|
67
|
+
const [, kind, name] = file.match(/^(apps|libs)\/([^/]+)\//) ?? [];
|
|
68
|
+
if (!kind || !name || !RuntimeScopeReport.isRuntimeSource(file)) continue;
|
|
69
|
+
const app = kind === "apps" ? name : firstApp;
|
|
70
|
+
if (!apps.includes(app)) continue;
|
|
71
|
+
const { content, absolutePath } = await sources.read(file);
|
|
72
|
+
if (MACRO_ATTR_RE.test(content)) continue;
|
|
73
|
+
const reader = await readerOf(app);
|
|
74
|
+
const rewritten = await rewriteBarrelImports(content, reader.barrels, reader.analyzer, {
|
|
75
|
+
keepLines: true,
|
|
76
|
+
reexports: true,
|
|
77
|
+
});
|
|
78
|
+
const rewrite = await reader.objects.rewrite(rewritten ?? content, absolutePath);
|
|
79
|
+
if (!rewrite || rewrite.narrowed.length + rewrite.kept.length === 0) continue;
|
|
80
|
+
const scope = `${kind}/${name}`;
|
|
81
|
+
const summary = scopes.get(scope) ?? { scope, app, narrowed: 0, kept: 0, reasons: {} };
|
|
82
|
+
scopes.set(scope, summary);
|
|
83
|
+
summary.narrowed += rewrite.narrowed.length;
|
|
84
|
+
for (const kept of rewrite.kept) {
|
|
85
|
+
summary.kept++;
|
|
86
|
+
summary.reasons[kept.reason] = (summary.reasons[kept.reason] ?? 0) + 1;
|
|
87
|
+
const objectFile = path.relative(workspace.workspaceRoot, kept.file).split(path.sep).join("/");
|
|
88
|
+
result.kept.push({ file, line: kept.line, object: kept.object, objectFile, reason: kept.reason });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
result.scopes = [...scopes.values()];
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
static format({ scopes, kept }: ObjectImportResult) {
|
|
96
|
+
const lines = scopes.map(({ scope, narrowed, kept: whole, reasons }) => {
|
|
97
|
+
const counts = Object.entries(reasons)
|
|
98
|
+
.sort(([a, x = 0], [b, y = 0]) => y - x || (a < b ? -1 : 1))
|
|
99
|
+
.map(([reason, count]) => `${reason} ${count}`);
|
|
100
|
+
return `${scope}: ${narrowed} object imports load only the members they name, ${whole} load the whole object${counts.length ? ` (${counts.join(", ")})` : ""}`;
|
|
101
|
+
});
|
|
102
|
+
if (kept.length) lines.push("--format json lists each import that loads the whole object, with its file and line");
|
|
103
|
+
return lines.join("\n");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -98,7 +98,7 @@ const SUGGESTED_RULES = [
|
|
|
98
98
|
"Warn earlier on very long Akan files: 500 lines for services, 800 lines for Template/Zone files, and 1000 lines for Util files.",
|
|
99
99
|
"Global declarations, Window augmentation, and prototype mutation should stay in explicitly approved low-level integration files.",
|
|
100
100
|
"Keep app root folders small and conventional: application code belongs under common, env, lib, page, plugin, private, public, script, srvkit, ui, or webkit.",
|
|
101
|
-
"Keep apps/*/lib and libs/*/lib root files limited to generated support facets such as cnst.ts, db.ts, dict.ts, sig.ts, srv.ts, st.ts, useClient.ts, and useServer.ts.",
|
|
101
|
+
"Keep apps/*/lib and libs/*/lib root files limited to generated support facets such as cnst.ts, db.ts, dict.ts, sig.ts, srv.ts, st.ts, useClient.ts, usePage.ts, and useServer.ts.",
|
|
102
102
|
"Use domain module folders consistently: lib/<model> for database modules, lib/_<service> for service modules, and lib/__scalar/<scalar> for scalar modules.",
|
|
103
103
|
"Keep module UI filenames predictable: database modules use <Model>.Template/Unit/Util/View/Zone.tsx, service modules use <Service>.Util/Zone.tsx, and scalar modules use <Scalar>.Template/Unit.tsx.",
|
|
104
104
|
"Hand a form control its store setter by reference so the field publishes an agent tool; put normalization in the control's transform prop and a multi-write in a _postSet<Field> hook on the store.",
|