funkscript 0.0.0-stage → 0.2.4
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/LICENSE +21 -0
- package/README.md +204 -2
- package/dist/Widget.tsx +9 -0
- package/dist/ast/types.d.ts +513 -0
- package/dist/ast/types.d.ts.map +1 -0
- package/dist/ast/types.js +5 -0
- package/dist/ast/types.js.map +1 -0
- package/dist/ast/walk.d.ts +8 -0
- package/dist/ast/walk.d.ts.map +1 -0
- package/dist/ast/walk.js +23 -0
- package/dist/ast/walk.js.map +1 -0
- package/dist/cli/build-bun.d.ts +24 -0
- package/dist/cli/build-bun.d.ts.map +1 -0
- package/dist/cli/build-bun.js +190 -0
- package/dist/cli/build-bun.js.map +1 -0
- package/dist/cli/build-watch.d.ts +21 -0
- package/dist/cli/build-watch.d.ts.map +1 -0
- package/dist/cli/build-watch.js +244 -0
- package/dist/cli/build-watch.js.map +1 -0
- package/dist/cli/build.d.ts +4 -0
- package/dist/cli/build.d.ts.map +1 -0
- package/dist/cli/build.js +338 -0
- package/dist/cli/build.js.map +1 -0
- package/dist/cli/check.d.ts +4 -0
- package/dist/cli/check.d.ts.map +1 -0
- package/dist/cli/check.js +278 -0
- package/dist/cli/check.js.map +1 -0
- package/dist/cli/commands.d.ts +13 -0
- package/dist/cli/commands.d.ts.map +1 -0
- package/dist/cli/commands.js +262 -0
- package/dist/cli/commands.js.map +1 -0
- package/dist/cli/doc.d.ts +3 -0
- package/dist/cli/doc.d.ts.map +1 -0
- package/dist/cli/doc.js +71 -0
- package/dist/cli/doc.js.map +1 -0
- package/dist/cli/doctor.d.ts +3 -0
- package/dist/cli/doctor.d.ts.map +1 -0
- package/dist/cli/doctor.js +121 -0
- package/dist/cli/doctor.js.map +1 -0
- package/dist/cli/emit.d.ts +4 -0
- package/dist/cli/emit.d.ts.map +1 -0
- package/dist/cli/emit.js +87 -0
- package/dist/cli/emit.js.map +1 -0
- package/dist/cli/explain.d.ts +3 -0
- package/dist/cli/explain.d.ts.map +1 -0
- package/dist/cli/explain.js +103 -0
- package/dist/cli/explain.js.map +1 -0
- package/dist/cli/fmt.d.ts +3 -0
- package/dist/cli/fmt.d.ts.map +1 -0
- package/dist/cli/fmt.js +146 -0
- package/dist/cli/fmt.js.map +1 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +186 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/init.d.ts +10 -0
- package/dist/cli/init.d.ts.map +1 -0
- package/dist/cli/init.js +412 -0
- package/dist/cli/init.js.map +1 -0
- package/dist/cli/io.d.ts +9 -0
- package/dist/cli/io.d.ts.map +1 -0
- package/dist/cli/io.js +113 -0
- package/dist/cli/io.js.map +1 -0
- package/dist/cli/output-collisions.d.ts +2 -0
- package/dist/cli/output-collisions.d.ts.map +1 -0
- package/dist/cli/output-collisions.js +39 -0
- package/dist/cli/output-collisions.js.map +1 -0
- package/dist/cli/pipeline-style.d.ts +3 -0
- package/dist/cli/pipeline-style.d.ts.map +1 -0
- package/dist/cli/pipeline-style.js +45 -0
- package/dist/cli/pipeline-style.js.map +1 -0
- package/dist/cli/repl.d.ts +37 -0
- package/dist/cli/repl.d.ts.map +1 -0
- package/dist/cli/repl.js +334 -0
- package/dist/cli/repl.js.map +1 -0
- package/dist/cli/run.d.ts +4 -0
- package/dist/cli/run.d.ts.map +1 -0
- package/dist/cli/run.js +185 -0
- package/dist/cli/run.js.map +1 -0
- package/dist/cli/test.d.ts +3 -0
- package/dist/cli/test.d.ts.map +1 -0
- package/dist/cli/test.js +124 -0
- package/dist/cli/test.js.map +1 -0
- package/dist/cli/tsc.d.ts +3 -0
- package/dist/cli/tsc.d.ts.map +1 -0
- package/dist/cli/tsc.js +143 -0
- package/dist/cli/tsc.js.map +1 -0
- package/dist/cli/typecheck.d.ts +23 -0
- package/dist/cli/typecheck.d.ts.map +1 -0
- package/dist/cli/typecheck.js +65 -0
- package/dist/cli/typecheck.js.map +1 -0
- package/dist/compiler/ast-inspection.d.ts +18 -0
- package/dist/compiler/ast-inspection.d.ts.map +1 -0
- package/dist/compiler/ast-inspection.js +96 -0
- package/dist/compiler/ast-inspection.js.map +1 -0
- package/dist/compiler/boolean-operators.d.ts +8 -0
- package/dist/compiler/boolean-operators.d.ts.map +1 -0
- package/dist/compiler/boolean-operators.js +84 -0
- package/dist/compiler/boolean-operators.js.map +1 -0
- package/dist/compiler/check.d.ts +87 -0
- package/dist/compiler/check.d.ts.map +1 -0
- package/dist/compiler/check.js +1830 -0
- package/dist/compiler/check.js.map +1 -0
- package/dist/compiler/diagnostics.d.ts +6 -0
- package/dist/compiler/diagnostics.d.ts.map +1 -0
- package/dist/compiler/diagnostics.js +136 -0
- package/dist/compiler/diagnostics.js.map +1 -0
- package/dist/compiler/effect-mutation-rule.d.ts +4 -0
- package/dist/compiler/effect-mutation-rule.d.ts.map +1 -0
- package/dist/compiler/effect-mutation-rule.js +348 -0
- package/dist/compiler/effect-mutation-rule.js.map +1 -0
- package/dist/compiler/entry-rules.d.ts +5 -0
- package/dist/compiler/entry-rules.d.ts.map +1 -0
- package/dist/compiler/entry-rules.js +89 -0
- package/dist/compiler/entry-rules.js.map +1 -0
- package/dist/compiler/imported-record-functions.d.ts +4 -0
- package/dist/compiler/imported-record-functions.d.ts.map +1 -0
- package/dist/compiler/imported-record-functions.js +706 -0
- package/dist/compiler/imported-record-functions.js.map +1 -0
- package/dist/compiler/imported-schemas.d.ts +21 -0
- package/dist/compiler/imported-schemas.d.ts.map +1 -0
- package/dist/compiler/imported-schemas.js +461 -0
- package/dist/compiler/imported-schemas.js.map +1 -0
- package/dist/compiler/incremental-build.d.ts +23 -0
- package/dist/compiler/incremental-build.d.ts.map +1 -0
- package/dist/compiler/incremental-build.js +90 -0
- package/dist/compiler/incremental-build.js.map +1 -0
- package/dist/compiler/incremental-dependencies.d.ts +7 -0
- package/dist/compiler/incremental-dependencies.d.ts.map +1 -0
- package/dist/compiler/incremental-dependencies.js +190 -0
- package/dist/compiler/incremental-dependencies.js.map +1 -0
- package/dist/compiler/js-interop-nullability.d.ts +9 -0
- package/dist/compiler/js-interop-nullability.d.ts.map +1 -0
- package/dist/compiler/js-interop-nullability.js +57 -0
- package/dist/compiler/js-interop-nullability.js.map +1 -0
- package/dist/compiler/module-resolution.d.ts +6 -0
- package/dist/compiler/module-resolution.d.ts.map +1 -0
- package/dist/compiler/module-resolution.js +44 -0
- package/dist/compiler/module-resolution.js.map +1 -0
- package/dist/compiler/modules/output-collisions.d.ts +2 -0
- package/dist/compiler/modules/output-collisions.d.ts.map +1 -0
- package/dist/compiler/modules/output-collisions.js +39 -0
- package/dist/compiler/modules/output-collisions.js.map +1 -0
- package/dist/compiler/modules/output-plan.d.ts +7 -0
- package/dist/compiler/modules/output-plan.d.ts.map +1 -0
- package/dist/compiler/modules/output-plan.js +37 -0
- package/dist/compiler/modules/output-plan.js.map +1 -0
- package/dist/compiler/modules/resolution.d.ts +8 -0
- package/dist/compiler/modules/resolution.d.ts.map +1 -0
- package/dist/compiler/modules/resolution.js +48 -0
- package/dist/compiler/modules/resolution.js.map +1 -0
- package/dist/compiler/numeric-comparisons.d.ts +9 -0
- package/dist/compiler/numeric-comparisons.d.ts.map +1 -0
- package/dist/compiler/numeric-comparisons.js +153 -0
- package/dist/compiler/numeric-comparisons.js.map +1 -0
- package/dist/compiler/optional-arguments.d.ts +11 -0
- package/dist/compiler/optional-arguments.d.ts.map +1 -0
- package/dist/compiler/optional-arguments.js +246 -0
- package/dist/compiler/optional-arguments.js.map +1 -0
- package/dist/compiler/package-fingerprint.d.ts +7 -0
- package/dist/compiler/package-fingerprint.d.ts.map +1 -0
- package/dist/compiler/package-fingerprint.js +178 -0
- package/dist/compiler/package-fingerprint.js.map +1 -0
- package/dist/compiler/package-record-contracts.d.ts +5 -0
- package/dist/compiler/package-record-contracts.d.ts.map +1 -0
- package/dist/compiler/package-record-contracts.js +126 -0
- package/dist/compiler/package-record-contracts.js.map +1 -0
- package/dist/compiler/pipeline-companions.d.ts +18 -0
- package/dist/compiler/pipeline-companions.d.ts.map +1 -0
- package/dist/compiler/pipeline-companions.js +1042 -0
- package/dist/compiler/pipeline-companions.js.map +1 -0
- package/dist/compiler/pipelines/catalog.d.ts +15 -0
- package/dist/compiler/pipelines/catalog.d.ts.map +1 -0
- package/dist/compiler/pipelines/catalog.js +55 -0
- package/dist/compiler/pipelines/catalog.js.map +1 -0
- package/dist/compiler/pipelines/companions.d.ts +18 -0
- package/dist/compiler/pipelines/companions.d.ts.map +1 -0
- package/dist/compiler/pipelines/companions.js +1042 -0
- package/dist/compiler/pipelines/companions.js.map +1 -0
- package/dist/compiler/pipelines/resolution.d.ts +38 -0
- package/dist/compiler/pipelines/resolution.d.ts.map +1 -0
- package/dist/compiler/pipelines/resolution.js +571 -0
- package/dist/compiler/pipelines/resolution.js.map +1 -0
- package/dist/compiler/project-config.d.ts +28 -0
- package/dist/compiler/project-config.d.ts.map +1 -0
- package/dist/compiler/project-config.js +155 -0
- package/dist/compiler/project-config.js.map +1 -0
- package/dist/compiler/providers.d.ts +4 -0
- package/dist/compiler/providers.d.ts.map +1 -0
- package/dist/compiler/providers.js +101 -0
- package/dist/compiler/providers.js.map +1 -0
- package/dist/compiler/record-contracts.d.ts +6 -0
- package/dist/compiler/record-contracts.d.ts.map +1 -0
- package/dist/compiler/record-contracts.js +41 -0
- package/dist/compiler/record-contracts.js.map +1 -0
- package/dist/compiler/rules/control-flow.d.ts +10 -0
- package/dist/compiler/rules/control-flow.d.ts.map +1 -0
- package/dist/compiler/rules/control-flow.js +19 -0
- package/dist/compiler/rules/control-flow.js.map +1 -0
- package/dist/compiler/rules/diagnostic.d.ts +4 -0
- package/dist/compiler/rules/diagnostic.d.ts.map +1 -0
- package/dist/compiler/rules/diagnostic.js +2 -0
- package/dist/compiler/rules/diagnostic.js.map +1 -0
- package/dist/compiler/rules/module-scope-refs.d.ts +5 -0
- package/dist/compiler/rules/module-scope-refs.d.ts.map +1 -0
- package/dist/compiler/rules/module-scope-refs.js +47 -0
- package/dist/compiler/rules/module-scope-refs.js.map +1 -0
- package/dist/compiler/schemas/analysis.d.ts +13 -0
- package/dist/compiler/schemas/analysis.d.ts.map +1 -0
- package/dist/compiler/schemas/analysis.js +24 -0
- package/dist/compiler/schemas/analysis.js.map +1 -0
- package/dist/compiler/schemas/imported-record-functions.d.ts +4 -0
- package/dist/compiler/schemas/imported-record-functions.d.ts.map +1 -0
- package/dist/compiler/schemas/imported-record-functions.js +706 -0
- package/dist/compiler/schemas/imported-record-functions.js.map +1 -0
- package/dist/compiler/schemas/imported-schemas.d.ts +21 -0
- package/dist/compiler/schemas/imported-schemas.d.ts.map +1 -0
- package/dist/compiler/schemas/imported-schemas.js +461 -0
- package/dist/compiler/schemas/imported-schemas.js.map +1 -0
- package/dist/compiler/source-map.d.ts +6 -0
- package/dist/compiler/source-map.d.ts.map +1 -0
- package/dist/compiler/source-map.js +127 -0
- package/dist/compiler/source-map.js.map +1 -0
- package/dist/compiler/typescript-context.d.ts +33 -0
- package/dist/compiler/typescript-context.d.ts.map +1 -0
- package/dist/compiler/typescript-context.js +200 -0
- package/dist/compiler/typescript-context.js.map +1 -0
- package/dist/compiler.d.ts +77 -0
- package/dist/compiler.d.ts.map +1 -0
- package/dist/compiler.js +497 -0
- package/dist/compiler.js.map +1 -0
- package/dist/emitter/assemblyscript.d.ts +5 -0
- package/dist/emitter/assemblyscript.d.ts.map +1 -0
- package/dist/emitter/assemblyscript.js +979 -0
- package/dist/emitter/assemblyscript.js.map +1 -0
- package/dist/emitter/declarations.d.ts +46 -0
- package/dist/emitter/declarations.d.ts.map +1 -0
- package/dist/emitter/declarations.js +739 -0
- package/dist/emitter/declarations.js.map +1 -0
- package/dist/emitter/emitter.d.ts +165 -0
- package/dist/emitter/emitter.d.ts.map +1 -0
- package/dist/emitter/emitter.js +2448 -0
- package/dist/emitter/emitter.js.map +1 -0
- package/dist/emitter/imports.d.ts +6 -0
- package/dist/emitter/imports.d.ts.map +1 -0
- package/dist/emitter/imports.js +73 -0
- package/dist/emitter/imports.js.map +1 -0
- package/dist/emitter/indent.d.ts +3 -0
- package/dist/emitter/indent.d.ts.map +1 -0
- package/dist/emitter/indent.js +31 -0
- package/dist/emitter/indent.js.map +1 -0
- package/dist/emitter/jsx.d.ts +20 -0
- package/dist/emitter/jsx.d.ts.map +1 -0
- package/dist/emitter/jsx.js +82 -0
- package/dist/emitter/jsx.js.map +1 -0
- package/dist/emitter/local-bindings.d.ts +6 -0
- package/dist/emitter/local-bindings.d.ts.map +1 -0
- package/dist/emitter/local-bindings.js +32 -0
- package/dist/emitter/local-bindings.js.map +1 -0
- package/dist/emitter/match.d.ts +56 -0
- package/dist/emitter/match.d.ts.map +1 -0
- package/dist/emitter/match.js +1287 -0
- package/dist/emitter/match.js.map +1 -0
- package/dist/emitter/pipeline-catalog.d.ts +15 -0
- package/dist/emitter/pipeline-catalog.d.ts.map +1 -0
- package/dist/emitter/pipeline-catalog.js +55 -0
- package/dist/emitter/pipeline-catalog.js.map +1 -0
- package/dist/emitter/pipeline-resolution.d.ts +38 -0
- package/dist/emitter/pipeline-resolution.d.ts.map +1 -0
- package/dist/emitter/pipeline-resolution.js +571 -0
- package/dist/emitter/pipeline-resolution.js.map +1 -0
- package/dist/emitter/runtime-helpers.d.ts +10 -0
- package/dist/emitter/runtime-helpers.d.ts.map +1 -0
- package/dist/emitter/runtime-helpers.js +22 -0
- package/dist/emitter/runtime-helpers.js.map +1 -0
- package/dist/emitter/schemas.d.ts +36 -0
- package/dist/emitter/schemas.d.ts.map +1 -0
- package/dist/emitter/schemas.js +766 -0
- package/dist/emitter/schemas.js.map +1 -0
- package/dist/emitter/statements.d.ts +23 -0
- package/dist/emitter/statements.d.ts.map +1 -0
- package/dist/emitter/statements.js +216 -0
- package/dist/emitter/statements.js.map +1 -0
- package/dist/emitter/tagged-match.d.ts +23 -0
- package/dist/emitter/tagged-match.d.ts.map +1 -0
- package/dist/emitter/tagged-match.js +109 -0
- package/dist/emitter/tagged-match.js.map +1 -0
- package/dist/emitter/types.d.ts +22 -0
- package/dist/emitter/types.d.ts.map +1 -0
- package/dist/emitter/types.js +231 -0
- package/dist/emitter/types.js.map +1 -0
- package/dist/errors.d.ts +39 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +98 -0
- package/dist/errors.js.map +1 -0
- package/dist/formatter/comments.d.ts +25 -0
- package/dist/formatter/comments.d.ts.map +1 -0
- package/dist/formatter/comments.js +283 -0
- package/dist/formatter/comments.js.map +1 -0
- package/dist/formatter/index.d.ts +71 -0
- package/dist/formatter/index.d.ts.map +1 -0
- package/dist/formatter/index.js +1113 -0
- package/dist/formatter/index.js.map +1 -0
- package/dist/formatter/operator-precedence.d.ts +3 -0
- package/dist/formatter/operator-precedence.d.ts.map +1 -0
- package/dist/formatter/operator-precedence.js +27 -0
- package/dist/formatter/operator-precedence.js.map +1 -0
- package/dist/formatter/prettier.d.ts +7 -0
- package/dist/formatter/prettier.d.ts.map +1 -0
- package/dist/formatter/prettier.js +51 -0
- package/dist/formatter/prettier.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/interop/callback.d.ts +38 -0
- package/dist/interop/callback.d.ts.map +1 -0
- package/dist/interop/callback.js +130 -0
- package/dist/interop/callback.js.map +1 -0
- package/dist/interop/index.d.ts +6 -0
- package/dist/interop/index.d.ts.map +1 -0
- package/dist/interop/index.js +6 -0
- package/dist/interop/index.js.map +1 -0
- package/dist/interop/js.d.ts +16 -0
- package/dist/interop/js.d.ts.map +1 -0
- package/dist/interop/js.js +35 -0
- package/dist/interop/js.js.map +1 -0
- package/dist/interop/record.d.ts +9 -0
- package/dist/interop/record.d.ts.map +1 -0
- package/dist/interop/record.js +18 -0
- package/dist/interop/record.js.map +1 -0
- package/dist/interop/valibot.d.ts +9 -0
- package/dist/interop/valibot.d.ts.map +1 -0
- package/dist/interop/valibot.js +94 -0
- package/dist/interop/valibot.js.map +1 -0
- package/dist/interop/zod.d.ts +45 -0
- package/dist/interop/zod.d.ts.map +1 -0
- package/dist/interop/zod.js +143 -0
- package/dist/interop/zod.js.map +1 -0
- package/dist/lexer/lexer.d.ts +65 -0
- package/dist/lexer/lexer.d.ts.map +1 -0
- package/dist/lexer/lexer.js +1335 -0
- package/dist/lexer/lexer.js.map +1 -0
- package/dist/lexer/tokens.d.ts +96 -0
- package/dist/lexer/tokens.d.ts.map +1 -0
- package/dist/lexer/tokens.js +150 -0
- package/dist/lexer/tokens.js.map +1 -0
- package/dist/lsp/code-action-worker.d.ts +2 -0
- package/dist/lsp/code-action-worker.d.ts.map +1 -0
- package/dist/lsp/code-action-worker.js +17 -0
- package/dist/lsp/code-action-worker.js.map +1 -0
- package/dist/lsp/completion.d.ts +11 -0
- package/dist/lsp/completion.d.ts.map +1 -0
- package/dist/lsp/completion.js +260 -0
- package/dist/lsp/completion.js.map +1 -0
- package/dist/lsp/definition.d.ts +42 -0
- package/dist/lsp/definition.d.ts.map +1 -0
- package/dist/lsp/definition.js +1074 -0
- package/dist/lsp/definition.js.map +1 -0
- package/dist/lsp/diagnostics-worker.d.ts +2 -0
- package/dist/lsp/diagnostics-worker.d.ts.map +1 -0
- package/dist/lsp/diagnostics-worker.js +169 -0
- package/dist/lsp/diagnostics-worker.js.map +1 -0
- package/dist/lsp/docs.d.ts +37 -0
- package/dist/lsp/docs.d.ts.map +1 -0
- package/dist/lsp/docs.js +830 -0
- package/dist/lsp/docs.js.map +1 -0
- package/dist/lsp/error-channel.d.ts +3 -0
- package/dist/lsp/error-channel.d.ts.map +1 -0
- package/dist/lsp/error-channel.js +48 -0
- package/dist/lsp/error-channel.js.map +1 -0
- package/dist/lsp/hover.d.ts +5 -0
- package/dist/lsp/hover.d.ts.map +1 -0
- package/dist/lsp/hover.js +651 -0
- package/dist/lsp/hover.js.map +1 -0
- package/dist/lsp/imports.d.ts +5 -0
- package/dist/lsp/imports.d.ts.map +1 -0
- package/dist/lsp/imports.js +317 -0
- package/dist/lsp/imports.js.map +1 -0
- package/dist/lsp/index.d.ts +16 -0
- package/dist/lsp/index.d.ts.map +1 -0
- package/dist/lsp/index.js +17 -0
- package/dist/lsp/index.js.map +1 -0
- package/dist/lsp/inlay-hints.d.ts +3 -0
- package/dist/lsp/inlay-hints.d.ts.map +1 -0
- package/dist/lsp/inlay-hints.js +236 -0
- package/dist/lsp/inlay-hints.js.map +1 -0
- package/dist/lsp/interop-rename.d.ts +26 -0
- package/dist/lsp/interop-rename.d.ts.map +1 -0
- package/dist/lsp/interop-rename.js +346 -0
- package/dist/lsp/interop-rename.js.map +1 -0
- package/dist/lsp/match-arms.d.ts +3 -0
- package/dist/lsp/match-arms.d.ts.map +1 -0
- package/dist/lsp/match-arms.js +341 -0
- package/dist/lsp/match-arms.js.map +1 -0
- package/dist/lsp/navigation.d.ts +23 -0
- package/dist/lsp/navigation.d.ts.map +1 -0
- package/dist/lsp/navigation.js +430 -0
- package/dist/lsp/navigation.js.map +1 -0
- package/dist/lsp/parsed-documents.d.ts +25 -0
- package/dist/lsp/parsed-documents.d.ts.map +1 -0
- package/dist/lsp/parsed-documents.js +47 -0
- package/dist/lsp/parsed-documents.js.map +1 -0
- package/dist/lsp/rename-worker.d.ts +2 -0
- package/dist/lsp/rename-worker.d.ts.map +1 -0
- package/dist/lsp/rename-worker.js +27 -0
- package/dist/lsp/rename-worker.js.map +1 -0
- package/dist/lsp/server.d.ts +78 -0
- package/dist/lsp/server.d.ts.map +1 -0
- package/dist/lsp/server.js +1098 -0
- package/dist/lsp/server.js.map +1 -0
- package/dist/lsp/thunk-pipeline.d.ts +4 -0
- package/dist/lsp/thunk-pipeline.d.ts.map +1 -0
- package/dist/lsp/thunk-pipeline.js +95 -0
- package/dist/lsp/thunk-pipeline.js.map +1 -0
- package/dist/lsp/validation.d.ts +8 -0
- package/dist/lsp/validation.d.ts.map +1 -0
- package/dist/lsp/validation.js +124 -0
- package/dist/lsp/validation.js.map +1 -0
- package/dist/lsp/workspace.d.ts +8 -0
- package/dist/lsp/workspace.d.ts.map +1 -0
- package/dist/lsp/workspace.js +70 -0
- package/dist/lsp/workspace.js.map +1 -0
- package/dist/math.ts +1 -0
- package/dist/parser/declarations.d.ts +21 -0
- package/dist/parser/declarations.d.ts.map +1 -0
- package/dist/parser/declarations.js +626 -0
- package/dist/parser/declarations.js.map +1 -0
- package/dist/parser/errors.d.ts +9 -0
- package/dist/parser/errors.d.ts.map +1 -0
- package/dist/parser/errors.js +11 -0
- package/dist/parser/errors.js.map +1 -0
- package/dist/parser/index.d.ts +9 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/index.js +9 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/jsx.d.ts +5 -0
- package/dist/parser/jsx.d.ts.map +1 -0
- package/dist/parser/jsx.js +188 -0
- package/dist/parser/jsx.js.map +1 -0
- package/dist/parser/operator-expressions.d.ts +16 -0
- package/dist/parser/operator-expressions.d.ts.map +1 -0
- package/dist/parser/operator-expressions.js +146 -0
- package/dist/parser/operator-expressions.js.map +1 -0
- package/dist/parser/parser.d.ts +104 -0
- package/dist/parser/parser.d.ts.map +1 -0
- package/dist/parser/parser.js +1216 -0
- package/dist/parser/parser.js.map +1 -0
- package/dist/parser/patterns.d.ts +9 -0
- package/dist/parser/patterns.d.ts.map +1 -0
- package/dist/parser/patterns.js +292 -0
- package/dist/parser/patterns.js.map +1 -0
- package/dist/parser/pipeline-expressions.d.ts +10 -0
- package/dist/parser/pipeline-expressions.d.ts.map +1 -0
- package/dist/parser/pipeline-expressions.js +57 -0
- package/dist/parser/pipeline-expressions.js.map +1 -0
- package/dist/parser/scope.d.ts +17 -0
- package/dist/parser/scope.d.ts.map +1 -0
- package/dist/parser/scope.js +56 -0
- package/dist/parser/scope.js.map +1 -0
- package/dist/parser/statements.d.ts +13 -0
- package/dist/parser/statements.d.ts.map +1 -0
- package/dist/parser/statements.js +737 -0
- package/dist/parser/statements.js.map +1 -0
- package/dist/parser/types.d.ts +13 -0
- package/dist/parser/types.d.ts.map +1 -0
- package/dist/parser/types.js +371 -0
- package/dist/parser/types.js.map +1 -0
- package/dist/plugins/bun-preload.d.ts +4 -0
- package/dist/plugins/bun-preload.d.ts.map +1 -0
- package/dist/plugins/bun-preload.js +18 -0
- package/dist/plugins/bun-preload.js.map +1 -0
- package/dist/plugins/bun.d.ts +115 -0
- package/dist/plugins/bun.d.ts.map +1 -0
- package/dist/plugins/bun.js +240 -0
- package/dist/plugins/bun.js.map +1 -0
- package/dist/plugins/esbuild.d.ts +45 -0
- package/dist/plugins/esbuild.d.ts.map +1 -0
- package/dist/plugins/esbuild.js +184 -0
- package/dist/plugins/esbuild.js.map +1 -0
- package/dist/plugins/type-view.d.ts +30 -0
- package/dist/plugins/type-view.d.ts.map +1 -0
- package/dist/plugins/type-view.js +245 -0
- package/dist/plugins/type-view.js.map +1 -0
- package/dist/plugins/vite.d.ts +56 -0
- package/dist/plugins/vite.d.ts.map +1 -0
- package/dist/plugins/vite.js +182 -0
- package/dist/plugins/vite.js.map +1 -0
- package/dist/service.ts +3 -0
- package/dist/service.ts.map +1 -0
- package/dist/testing/index.d.ts +24 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +56 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/tooling/doc.d.ts +82 -0
- package/dist/tooling/doc.d.ts.map +1 -0
- package/dist/tooling/doc.js +624 -0
- package/dist/tooling/doc.js.map +1 -0
- package/dist/tooling/idiomatic.d.ts +20 -0
- package/dist/tooling/idiomatic.d.ts.map +1 -0
- package/dist/tooling/idiomatic.js +154 -0
- package/dist/tooling/idiomatic.js.map +1 -0
- package/dist/tooling/migrate.d.ts +62 -0
- package/dist/tooling/migrate.d.ts.map +1 -0
- package/dist/tooling/migrate.js +608 -0
- package/dist/tooling/migrate.js.map +1 -0
- package/dist/tooling/pipeline-style.d.ts +12 -0
- package/dist/tooling/pipeline-style.d.ts.map +1 -0
- package/dist/tooling/pipeline-style.js +208 -0
- package/dist/tooling/pipeline-style.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +25 -0
- package/dist/version.js.map +1 -0
- package/package.json +128 -4
- package/src/ast/types.ts +753 -0
- package/src/ast/walk.ts +35 -0
- package/src/cli/build-bun.ts +249 -0
- package/src/cli/build-watch.ts +299 -0
- package/src/cli/build.ts +450 -0
- package/src/cli/check.ts +342 -0
- package/src/cli/commands.ts +301 -0
- package/src/cli/doc.ts +81 -0
- package/src/cli/doctor.ts +173 -0
- package/src/cli/emit.ts +138 -0
- package/src/cli/explain.ts +127 -0
- package/src/cli/fmt.ts +170 -0
- package/src/cli/index.ts +227 -0
- package/src/cli/init.ts +491 -0
- package/src/cli/io.ts +150 -0
- package/src/cli/pipeline-style.ts +55 -0
- package/src/cli/repl.ts +443 -0
- package/src/cli/run.ts +238 -0
- package/src/cli/test.ts +148 -0
- package/src/cli/tsc.ts +207 -0
- package/src/cli/typecheck.ts +100 -0
- package/src/compiler/ast-inspection.ts +121 -0
- package/src/compiler/boolean-operators.ts +108 -0
- package/src/compiler/check.ts +2763 -0
- package/src/compiler/diagnostics.ts +179 -0
- package/src/compiler/effect-mutation-rule.ts +407 -0
- package/src/compiler/entry-rules.ts +116 -0
- package/src/compiler/incremental-build.ts +131 -0
- package/src/compiler/incremental-dependencies.ts +200 -0
- package/src/compiler/js-interop-nullability.ts +67 -0
- package/src/compiler/modules/output-collisions.ts +48 -0
- package/src/compiler/modules/output-plan.ts +46 -0
- package/src/compiler/modules/resolution.ts +56 -0
- package/src/compiler/numeric-comparisons.ts +213 -0
- package/src/compiler/optional-arguments.ts +335 -0
- package/src/compiler/package-fingerprint.ts +216 -0
- package/src/compiler/package-record-contracts.ts +156 -0
- package/src/compiler/pipelines/catalog.ts +1128 -0
- package/src/compiler/pipelines/companions.ts +1279 -0
- package/src/compiler/pipelines/resolution.ts +684 -0
- package/src/compiler/project-config.ts +221 -0
- package/src/compiler/record-contracts.ts +46 -0
- package/src/compiler/rules/control-flow.ts +57 -0
- package/src/compiler/rules/diagnostic.ts +11 -0
- package/src/compiler/rules/module-scope-refs.ts +63 -0
- package/src/compiler/schemas/analysis.ts +45 -0
- package/src/compiler/schemas/imported-record-functions.ts +984 -0
- package/src/compiler/schemas/imported-schemas.ts +761 -0
- package/src/compiler/source-map.ts +165 -0
- package/src/compiler/typescript-context.ts +250 -0
- package/src/compiler.ts +865 -0
- package/src/emitter/assemblyscript.ts +1220 -0
- package/src/emitter/declarations.ts +916 -0
- package/src/emitter/emitter.ts +3128 -0
- package/src/emitter/imports.ts +89 -0
- package/src/emitter/indent.ts +39 -0
- package/src/emitter/jsx.ts +122 -0
- package/src/emitter/local-bindings.ts +40 -0
- package/src/emitter/match.ts +1631 -0
- package/src/emitter/runtime-helpers.ts +31 -0
- package/src/emitter/schemas.ts +969 -0
- package/src/emitter/statements.ts +310 -0
- package/src/emitter/tagged-match.ts +171 -0
- package/src/emitter/types.ts +289 -0
- package/src/errors.ts +115 -0
- package/src/formatter/comments.ts +350 -0
- package/src/formatter/index.ts +1457 -0
- package/src/formatter/operator-precedence.ts +36 -0
- package/src/formatter/prettier.ts +61 -0
- package/src/index.ts +16 -0
- package/src/interop/callback.ts +188 -0
- package/src/interop/js.ts +66 -0
- package/src/interop/record.ts +36 -0
- package/src/interop/valibot.ts +141 -0
- package/src/interop/zod.ts +219 -0
- package/src/lexer/lexer.ts +1521 -0
- package/src/lexer/tokens.ts +174 -0
- package/src/lsp/code-action-worker.ts +31 -0
- package/src/lsp/completion.ts +343 -0
- package/src/lsp/definition.ts +1165 -0
- package/src/lsp/diagnostics-worker.ts +198 -0
- package/src/lsp/docs.ts +864 -0
- package/src/lsp/error-channel.ts +63 -0
- package/src/lsp/hover.ts +803 -0
- package/src/lsp/imports.ts +380 -0
- package/src/lsp/index.ts +55 -0
- package/src/lsp/inlay-hints.ts +308 -0
- package/src/lsp/interop-rename.ts +454 -0
- package/src/lsp/match-arms.ts +400 -0
- package/src/lsp/navigation.ts +553 -0
- package/src/lsp/parsed-documents.ts +64 -0
- package/src/lsp/rename-worker.ts +40 -0
- package/src/lsp/server.ts +1437 -0
- package/src/lsp/thunk-pipeline.ts +117 -0
- package/src/lsp/validation.ts +147 -0
- package/src/lsp/workspace.ts +74 -0
- package/src/parser/declarations.ts +808 -0
- package/src/parser/errors.ts +18 -0
- package/src/parser/jsx.ts +253 -0
- package/src/parser/operator-expressions.ts +170 -0
- package/src/parser/parser.ts +1565 -0
- package/src/parser/patterns.ts +342 -0
- package/src/parser/pipeline-expressions.ts +77 -0
- package/src/parser/scope.ts +69 -0
- package/src/parser/statements.ts +831 -0
- package/src/parser/types.ts +453 -0
- package/src/plugins/bun-preload.ts +25 -0
- package/src/plugins/bun.ts +448 -0
- package/src/plugins/esbuild.ts +308 -0
- package/src/plugins/type-view.ts +326 -0
- package/src/plugins/vite.ts +294 -0
- package/src/testing/index.ts +72 -0
- package/src/tooling/doc.ts +786 -0
- package/src/tooling/idiomatic.ts +202 -0
- package/src/tooling/pipeline-style.ts +248 -0
- package/src/version.ts +27 -0
|
@@ -0,0 +1,3128 @@
|
|
|
1
|
+
import { emitTaggedMatch } from "./tagged-match.js";
|
|
2
|
+
import { collectLocalBindings } from "./local-bindings.js";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
import { indentContinuation } from "./indent.js";
|
|
5
|
+
import {
|
|
6
|
+
type BinaryExpression,
|
|
7
|
+
type BindingDeclaration,
|
|
8
|
+
type BindingTarget,
|
|
9
|
+
type CallExpression,
|
|
10
|
+
type EffectBlockExpression,
|
|
11
|
+
type ErrorDeclaration,
|
|
12
|
+
type Expression,
|
|
13
|
+
type FunctionBody,
|
|
14
|
+
type FunctionExpression,
|
|
15
|
+
type Identifier,
|
|
16
|
+
type IndexExpression,
|
|
17
|
+
type JsExpression,
|
|
18
|
+
type JSXAttribute,
|
|
19
|
+
type JSXChild,
|
|
20
|
+
type JSXElement,
|
|
21
|
+
type JSXExpressionContainer,
|
|
22
|
+
type JSXSelfClosingElement,
|
|
23
|
+
type JSXTagName,
|
|
24
|
+
type JSXText,
|
|
25
|
+
type MatchExpression,
|
|
26
|
+
type MemberExpression,
|
|
27
|
+
type MutBlockExpression,
|
|
28
|
+
type ParBlockExpression,
|
|
29
|
+
type Pattern,
|
|
30
|
+
type PipelineExpression,
|
|
31
|
+
type Program,
|
|
32
|
+
type RecordExpression,
|
|
33
|
+
type RecordPattern,
|
|
34
|
+
type RecordTypeField,
|
|
35
|
+
type SchemaDeclaration,
|
|
36
|
+
type ServiceDeclaration,
|
|
37
|
+
type Statement,
|
|
38
|
+
type StringLiteral,
|
|
39
|
+
type TaggedTemplateExpression,
|
|
40
|
+
type TaggedUnionSchema,
|
|
41
|
+
type TemplateLiteral,
|
|
42
|
+
type TopLevelDeclaration,
|
|
43
|
+
type TypeNode,
|
|
44
|
+
type TypeParameter,
|
|
45
|
+
type UnaryExpression,
|
|
46
|
+
type UpperIdentifier,
|
|
47
|
+
type WhenStatement,
|
|
48
|
+
type Span,
|
|
49
|
+
} from "../ast/types.js";
|
|
50
|
+
import {
|
|
51
|
+
isRecordContract,
|
|
52
|
+
recordContractType,
|
|
53
|
+
} from "../compiler/record-contracts.js";
|
|
54
|
+
import { FunScriptError } from "../errors.js";
|
|
55
|
+
import {
|
|
56
|
+
collectBindingNames,
|
|
57
|
+
emitBindingDeclaration,
|
|
58
|
+
emitErrorDeclaration,
|
|
59
|
+
emitServiceDeclaration,
|
|
60
|
+
emitTopLevelDeclaration,
|
|
61
|
+
emitUnpackedRecordPattern,
|
|
62
|
+
extractRecordFields,
|
|
63
|
+
isPrimitiveType,
|
|
64
|
+
patternHasOptionalField,
|
|
65
|
+
} from "./declarations.js";
|
|
66
|
+
import {
|
|
67
|
+
emitJsxAttribute,
|
|
68
|
+
emitJsxChild,
|
|
69
|
+
emitJsxElement,
|
|
70
|
+
emitJsxExpressionContainer,
|
|
71
|
+
emitJsxSelfClosingElement,
|
|
72
|
+
emitJsxTagName,
|
|
73
|
+
emitJsxText,
|
|
74
|
+
} from "./jsx.js";
|
|
75
|
+
import { MatchEmitter, type RecordFieldInfo } from "./match.js";
|
|
76
|
+
import {
|
|
77
|
+
COMPANION_IMPORT_SOURCE,
|
|
78
|
+
PIPELINE_COMPANION_CATALOG,
|
|
79
|
+
type PipelineCompanionModule,
|
|
80
|
+
} from "../compiler/pipelines/catalog.js";
|
|
81
|
+
import {
|
|
82
|
+
COMPARISON_METHOD_MAP,
|
|
83
|
+
ES_TOOLKIT_METHODS,
|
|
84
|
+
PipelineResolver,
|
|
85
|
+
} from "../compiler/pipelines/resolution.js";
|
|
86
|
+
import { emitRuntimeHelpers } from "./runtime-helpers.js";
|
|
87
|
+
import { emitSchemaDeclaration, emitSchemaTypeField } from "./schemas.js";
|
|
88
|
+
import {
|
|
89
|
+
emitArrowExpression,
|
|
90
|
+
emitEffectBlockExpression,
|
|
91
|
+
emitEffectCall,
|
|
92
|
+
emitIndexTarget,
|
|
93
|
+
emitMutativeRecipe,
|
|
94
|
+
emitParBlockExpression,
|
|
95
|
+
emitStatement,
|
|
96
|
+
emitWhenStatement,
|
|
97
|
+
} from "./statements.js";
|
|
98
|
+
import {
|
|
99
|
+
EFFECT_BUILTIN_MODULES,
|
|
100
|
+
emitPropertyKey,
|
|
101
|
+
emitTypeNode,
|
|
102
|
+
emitTypeParameters,
|
|
103
|
+
} from "./types.js";
|
|
104
|
+
export {
|
|
105
|
+
COMPARISON_METHOD_MAP,
|
|
106
|
+
ES_TOOLKIT_DATA_LAST_CALLS,
|
|
107
|
+
ES_TOOLKIT_MATH_COLLECTION,
|
|
108
|
+
ES_TOOLKIT_MATH_DATA_LAST,
|
|
109
|
+
ES_TOOLKIT_METHODS,
|
|
110
|
+
ES_TOOLKIT_STRING_METHODS,
|
|
111
|
+
ES_TOOLKIT_TIMING_DATA_LAST,
|
|
112
|
+
PipelineResolutionError,
|
|
113
|
+
} from "../compiler/pipelines/resolution.js";
|
|
114
|
+
|
|
115
|
+
export interface StageResolution {
|
|
116
|
+
module: string;
|
|
117
|
+
method?: string;
|
|
118
|
+
isComparison?: boolean;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface EmitterOptions {
|
|
122
|
+
/** TypeScript-directed companion facts keyed by the pipeline source offset. */
|
|
123
|
+
pipelineCompanions?: ReadonlyMap<number, PipelineCompanionModule>;
|
|
124
|
+
/** Stage resolutions keyed by pipeline stage offset. */
|
|
125
|
+
stageResolutions?: ReadonlyMap<number, StageResolution>;
|
|
126
|
+
/** Direct call resolutions keyed by call expression offset. */
|
|
127
|
+
directCallResolutions?: ReadonlyMap<number, StageResolution>;
|
|
128
|
+
/** Used only for the checker pre-pass, whose unresolved stage names are intentional. */
|
|
129
|
+
allowAmbiguousPipeline?: boolean;
|
|
130
|
+
/** Wrap pipeline sources so the checker can locate their exact inferred type. */
|
|
131
|
+
markPipelineSources?: boolean;
|
|
132
|
+
/** Wrap native comparison operands so the checker can enforce number-only use. */
|
|
133
|
+
markNumericComparisonOperands?: boolean;
|
|
134
|
+
/** Wrap logical `and`/`or` operands so the checker can enforce boolean-only use. */
|
|
135
|
+
markBooleanOperands?: boolean;
|
|
136
|
+
/** Wrap call callees so the checker can detect construct-only JS/TS classes. */
|
|
137
|
+
markCallCallees?: boolean;
|
|
138
|
+
/** Call offsets resolved as `new Callee(...)` via TypeScript construct signatures. */
|
|
139
|
+
constructorCalls?: ReadonlySet<number>;
|
|
140
|
+
/** Preserve source locations of ordinary call arguments during checker analysis. */
|
|
141
|
+
markCallArguments?: boolean;
|
|
142
|
+
forwardOptionalArguments?: ReadonlySet<number>;
|
|
143
|
+
forwardNullArguments?: ReadonlySet<number>;
|
|
144
|
+
forwardOptionalProperties?: ReadonlySet<number>;
|
|
145
|
+
forwardNullProperties?: ReadonlySet<number>;
|
|
146
|
+
/** Pre-resolved Effect schema expressions for imported data types keyed by schema name. */
|
|
147
|
+
importedSchemas?: ReadonlyMap<string, string>;
|
|
148
|
+
/** Direct imported FunkScript functions with verified optional-record parameters. */
|
|
149
|
+
importedRecordFunctions?: ReadonlyMap<string, ReadonlySet<number>>;
|
|
150
|
+
importedRecordContracts?: ReadonlyMap<
|
|
151
|
+
string,
|
|
152
|
+
import("../ast/types.js").RecordTypeNode
|
|
153
|
+
>;
|
|
154
|
+
/** Direct imported data schemas verified to have an identity encoding. */
|
|
155
|
+
importedIdentitySchemas?: ReadonlySet<string>;
|
|
156
|
+
importedGenericDataSchemas?: ReadonlyMap<string, number>;
|
|
157
|
+
importedDataSchemas?: ReadonlySet<string>;
|
|
158
|
+
importedNullishSchemas?: ReadonlyMap<string, ReadonlyMap<string, TypeNode>>;
|
|
159
|
+
importedTaggedUnionVariants?: ReadonlyMap<string, ReadonlySet<string>>;
|
|
160
|
+
/** Whether the output is `.tsx`; generic arrows need a comma there. */
|
|
161
|
+
isJsx?: boolean;
|
|
162
|
+
context?: import("../compiler/typescript-context.js").TypeScriptCompilationContext;
|
|
163
|
+
modularEffectImports?: boolean;
|
|
164
|
+
preserveFunImports?: boolean;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export const EFFECT_MODULE_NAMES = new Set([
|
|
168
|
+
...Object.keys(PIPELINE_COMPANION_CATALOG),
|
|
169
|
+
"Context",
|
|
170
|
+
"Data",
|
|
171
|
+
"Duration",
|
|
172
|
+
"Equal",
|
|
173
|
+
"Number",
|
|
174
|
+
"pipe",
|
|
175
|
+
]);
|
|
176
|
+
|
|
177
|
+
const BANNED_INSTANCE_METHODS = new Map<
|
|
178
|
+
string,
|
|
179
|
+
{ category: string; replacement: string; pipeline: string }
|
|
180
|
+
>([
|
|
181
|
+
// Array methods
|
|
182
|
+
[
|
|
183
|
+
"map",
|
|
184
|
+
{
|
|
185
|
+
category: "array",
|
|
186
|
+
replacement: "Array.map",
|
|
187
|
+
pipeline: "|> Array.map(...)",
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
[
|
|
191
|
+
"filter",
|
|
192
|
+
{
|
|
193
|
+
category: "array",
|
|
194
|
+
replacement: "Array.filter",
|
|
195
|
+
pipeline: "|> Array.filter(...)",
|
|
196
|
+
},
|
|
197
|
+
],
|
|
198
|
+
[
|
|
199
|
+
"reduce",
|
|
200
|
+
{
|
|
201
|
+
category: "array",
|
|
202
|
+
replacement: "Array.reduce",
|
|
203
|
+
pipeline: "|> Array.reduce(...)",
|
|
204
|
+
},
|
|
205
|
+
],
|
|
206
|
+
[
|
|
207
|
+
"reduceRight",
|
|
208
|
+
{
|
|
209
|
+
category: "array",
|
|
210
|
+
replacement: "Array.reduceRight",
|
|
211
|
+
pipeline: "|> Array.reduceRight(...)",
|
|
212
|
+
},
|
|
213
|
+
],
|
|
214
|
+
[
|
|
215
|
+
"flatMap",
|
|
216
|
+
{
|
|
217
|
+
category: "array",
|
|
218
|
+
replacement: "Array.flatMap",
|
|
219
|
+
pipeline: "|> Array.flatMap(...)",
|
|
220
|
+
},
|
|
221
|
+
],
|
|
222
|
+
[
|
|
223
|
+
"flat",
|
|
224
|
+
{
|
|
225
|
+
category: "array",
|
|
226
|
+
replacement: "Array.flatten",
|
|
227
|
+
pipeline: "|> Array.flatten",
|
|
228
|
+
},
|
|
229
|
+
],
|
|
230
|
+
[
|
|
231
|
+
"find",
|
|
232
|
+
{
|
|
233
|
+
category: "array",
|
|
234
|
+
replacement: "Array.findFirst",
|
|
235
|
+
pipeline: "|> Array.findFirst(...)",
|
|
236
|
+
},
|
|
237
|
+
],
|
|
238
|
+
[
|
|
239
|
+
"findIndex",
|
|
240
|
+
{
|
|
241
|
+
category: "array",
|
|
242
|
+
replacement: "Array.findFirstIndex",
|
|
243
|
+
pipeline: "|> Array.findFirstIndex(...)",
|
|
244
|
+
},
|
|
245
|
+
],
|
|
246
|
+
[
|
|
247
|
+
"findLast",
|
|
248
|
+
{
|
|
249
|
+
category: "array",
|
|
250
|
+
replacement: "Array.findLast",
|
|
251
|
+
pipeline: "|> Array.findLast(...)",
|
|
252
|
+
},
|
|
253
|
+
],
|
|
254
|
+
[
|
|
255
|
+
"findLastIndex",
|
|
256
|
+
{
|
|
257
|
+
category: "array",
|
|
258
|
+
replacement: "Array.findLastIndex",
|
|
259
|
+
pipeline: "|> Array.findLastIndex(...)",
|
|
260
|
+
},
|
|
261
|
+
],
|
|
262
|
+
[
|
|
263
|
+
"some",
|
|
264
|
+
{
|
|
265
|
+
category: "array",
|
|
266
|
+
replacement: "Array.some",
|
|
267
|
+
pipeline: "|> Array.some(...)",
|
|
268
|
+
},
|
|
269
|
+
],
|
|
270
|
+
[
|
|
271
|
+
"every",
|
|
272
|
+
{
|
|
273
|
+
category: "array",
|
|
274
|
+
replacement: "Array.every",
|
|
275
|
+
pipeline: "|> Array.every(...)",
|
|
276
|
+
},
|
|
277
|
+
],
|
|
278
|
+
[
|
|
279
|
+
"sort",
|
|
280
|
+
{
|
|
281
|
+
category: "array",
|
|
282
|
+
replacement: "Array.sort",
|
|
283
|
+
pipeline: "|> Array.sort(...)",
|
|
284
|
+
},
|
|
285
|
+
],
|
|
286
|
+
[
|
|
287
|
+
"reverse",
|
|
288
|
+
{
|
|
289
|
+
category: "array",
|
|
290
|
+
replacement: "Array.reverse",
|
|
291
|
+
pipeline: "|> Array.reverse",
|
|
292
|
+
},
|
|
293
|
+
],
|
|
294
|
+
[
|
|
295
|
+
"push",
|
|
296
|
+
{
|
|
297
|
+
category: "array",
|
|
298
|
+
replacement: "Array.append",
|
|
299
|
+
pipeline: "|> Array.append(...)",
|
|
300
|
+
},
|
|
301
|
+
],
|
|
302
|
+
[
|
|
303
|
+
"pop",
|
|
304
|
+
{
|
|
305
|
+
category: "array",
|
|
306
|
+
replacement: "Array.unappend",
|
|
307
|
+
pipeline: "|> Array.unappend",
|
|
308
|
+
},
|
|
309
|
+
],
|
|
310
|
+
[
|
|
311
|
+
"shift",
|
|
312
|
+
{
|
|
313
|
+
category: "array",
|
|
314
|
+
replacement: "Array.unprepend",
|
|
315
|
+
pipeline: "|> Array.unprepend",
|
|
316
|
+
},
|
|
317
|
+
],
|
|
318
|
+
[
|
|
319
|
+
"unshift",
|
|
320
|
+
{
|
|
321
|
+
category: "array",
|
|
322
|
+
replacement: "Array.prepend",
|
|
323
|
+
pipeline: "|> Array.prepend(...)",
|
|
324
|
+
},
|
|
325
|
+
],
|
|
326
|
+
[
|
|
327
|
+
"splice",
|
|
328
|
+
{
|
|
329
|
+
category: "array",
|
|
330
|
+
replacement: "Array.remove / Array.modify",
|
|
331
|
+
pipeline: "Array.remove(...)",
|
|
332
|
+
},
|
|
333
|
+
],
|
|
334
|
+
|
|
335
|
+
// String methods
|
|
336
|
+
[
|
|
337
|
+
"trim",
|
|
338
|
+
{
|
|
339
|
+
category: "string",
|
|
340
|
+
replacement: "String.trim",
|
|
341
|
+
pipeline: "|> String.trim",
|
|
342
|
+
},
|
|
343
|
+
],
|
|
344
|
+
[
|
|
345
|
+
"trimStart",
|
|
346
|
+
{
|
|
347
|
+
category: "string",
|
|
348
|
+
replacement: "String.trimStart",
|
|
349
|
+
pipeline: "|> String.trimStart",
|
|
350
|
+
},
|
|
351
|
+
],
|
|
352
|
+
[
|
|
353
|
+
"trimEnd",
|
|
354
|
+
{
|
|
355
|
+
category: "string",
|
|
356
|
+
replacement: "String.trimEnd",
|
|
357
|
+
pipeline: "|> String.trimEnd",
|
|
358
|
+
},
|
|
359
|
+
],
|
|
360
|
+
[
|
|
361
|
+
"toLowerCase",
|
|
362
|
+
{
|
|
363
|
+
category: "string",
|
|
364
|
+
replacement: "String.toLowerCase",
|
|
365
|
+
pipeline: "|> String.toLowerCase",
|
|
366
|
+
},
|
|
367
|
+
],
|
|
368
|
+
[
|
|
369
|
+
"toUpperCase",
|
|
370
|
+
{
|
|
371
|
+
category: "string",
|
|
372
|
+
replacement: "String.toUpperCase",
|
|
373
|
+
pipeline: "|> String.toUpperCase",
|
|
374
|
+
},
|
|
375
|
+
],
|
|
376
|
+
[
|
|
377
|
+
"toLocaleLowerCase",
|
|
378
|
+
{
|
|
379
|
+
category: "string",
|
|
380
|
+
replacement: "String.toLocaleLowerCase",
|
|
381
|
+
pipeline: "|> String.toLocaleLowerCase",
|
|
382
|
+
},
|
|
383
|
+
],
|
|
384
|
+
[
|
|
385
|
+
"toLocaleUpperCase",
|
|
386
|
+
{
|
|
387
|
+
category: "string",
|
|
388
|
+
replacement: "String.toLocaleUpperCase",
|
|
389
|
+
pipeline: "|> String.toLocaleUpperCase",
|
|
390
|
+
},
|
|
391
|
+
],
|
|
392
|
+
[
|
|
393
|
+
"startsWith",
|
|
394
|
+
{
|
|
395
|
+
category: "string",
|
|
396
|
+
replacement: "String.startsWith",
|
|
397
|
+
pipeline: "|> String.startsWith(...)",
|
|
398
|
+
},
|
|
399
|
+
],
|
|
400
|
+
[
|
|
401
|
+
"endsWith",
|
|
402
|
+
{
|
|
403
|
+
category: "string",
|
|
404
|
+
replacement: "String.endsWith",
|
|
405
|
+
pipeline: "|> String.endsWith(...)",
|
|
406
|
+
},
|
|
407
|
+
],
|
|
408
|
+
[
|
|
409
|
+
"split",
|
|
410
|
+
{
|
|
411
|
+
category: "string",
|
|
412
|
+
replacement: "String.split",
|
|
413
|
+
pipeline: "|> String.split(...)",
|
|
414
|
+
},
|
|
415
|
+
],
|
|
416
|
+
[
|
|
417
|
+
"replace",
|
|
418
|
+
{
|
|
419
|
+
category: "string",
|
|
420
|
+
replacement: "String.replace",
|
|
421
|
+
pipeline: "|> String.replace(...)",
|
|
422
|
+
},
|
|
423
|
+
],
|
|
424
|
+
[
|
|
425
|
+
"replaceAll",
|
|
426
|
+
{
|
|
427
|
+
category: "string",
|
|
428
|
+
replacement: "String.replaceAll",
|
|
429
|
+
pipeline: "|> String.replaceAll(...)",
|
|
430
|
+
},
|
|
431
|
+
],
|
|
432
|
+
[
|
|
433
|
+
"padStart",
|
|
434
|
+
{
|
|
435
|
+
category: "string",
|
|
436
|
+
replacement: "String.padStart",
|
|
437
|
+
pipeline: "|> String.padStart(...)",
|
|
438
|
+
},
|
|
439
|
+
],
|
|
440
|
+
[
|
|
441
|
+
"padEnd",
|
|
442
|
+
{
|
|
443
|
+
category: "string",
|
|
444
|
+
replacement: "String.padEnd",
|
|
445
|
+
pipeline: "|> String.padEnd(...)",
|
|
446
|
+
},
|
|
447
|
+
],
|
|
448
|
+
[
|
|
449
|
+
"repeat",
|
|
450
|
+
{
|
|
451
|
+
category: "string",
|
|
452
|
+
replacement: "String.repeat",
|
|
453
|
+
pipeline: "|> String.repeat(...)",
|
|
454
|
+
},
|
|
455
|
+
],
|
|
456
|
+
|
|
457
|
+
// Collection mutation methods
|
|
458
|
+
[
|
|
459
|
+
"add",
|
|
460
|
+
{
|
|
461
|
+
category: "collection",
|
|
462
|
+
replacement: "HashSet.add",
|
|
463
|
+
pipeline: "|> HashSet.add(...)",
|
|
464
|
+
},
|
|
465
|
+
],
|
|
466
|
+
[
|
|
467
|
+
"clear",
|
|
468
|
+
{
|
|
469
|
+
category: "collection",
|
|
470
|
+
replacement: "HashMap.empty() / HashSet.empty()",
|
|
471
|
+
pipeline: "HashMap.empty()",
|
|
472
|
+
},
|
|
473
|
+
],
|
|
474
|
+
[
|
|
475
|
+
"delete",
|
|
476
|
+
{
|
|
477
|
+
category: "collection",
|
|
478
|
+
replacement: "HashMap.remove / Record.remove",
|
|
479
|
+
pipeline: "|> HashMap.remove(...)",
|
|
480
|
+
},
|
|
481
|
+
],
|
|
482
|
+
[
|
|
483
|
+
"set",
|
|
484
|
+
{
|
|
485
|
+
category: "collection",
|
|
486
|
+
replacement: "HashMap.set / Record.set",
|
|
487
|
+
pipeline: "|> HashMap.set(...)",
|
|
488
|
+
},
|
|
489
|
+
],
|
|
490
|
+
|
|
491
|
+
// Date mutation methods
|
|
492
|
+
[
|
|
493
|
+
"setFullYear",
|
|
494
|
+
{ category: "date", replacement: "DateTime.*", pipeline: "|> DateTime.*" },
|
|
495
|
+
],
|
|
496
|
+
[
|
|
497
|
+
"setMonth",
|
|
498
|
+
{ category: "date", replacement: "DateTime.*", pipeline: "|> DateTime.*" },
|
|
499
|
+
],
|
|
500
|
+
[
|
|
501
|
+
"setDate",
|
|
502
|
+
{ category: "date", replacement: "DateTime.*", pipeline: "|> DateTime.*" },
|
|
503
|
+
],
|
|
504
|
+
[
|
|
505
|
+
"setHours",
|
|
506
|
+
{ category: "date", replacement: "DateTime.*", pipeline: "|> DateTime.*" },
|
|
507
|
+
],
|
|
508
|
+
[
|
|
509
|
+
"setMinutes",
|
|
510
|
+
{ category: "date", replacement: "DateTime.*", pipeline: "|> DateTime.*" },
|
|
511
|
+
],
|
|
512
|
+
[
|
|
513
|
+
"setSeconds",
|
|
514
|
+
{ category: "date", replacement: "DateTime.*", pipeline: "|> DateTime.*" },
|
|
515
|
+
],
|
|
516
|
+
[
|
|
517
|
+
"setTime",
|
|
518
|
+
{ category: "date", replacement: "DateTime.*", pipeline: "|> DateTime.*" },
|
|
519
|
+
],
|
|
520
|
+
[
|
|
521
|
+
"setMilliseconds",
|
|
522
|
+
{ category: "date", replacement: "DateTime.*", pipeline: "|> DateTime.*" },
|
|
523
|
+
],
|
|
524
|
+
]);
|
|
525
|
+
|
|
526
|
+
const BANNED_OBJECT_STATIC_METHODS = new Map<string, string>([
|
|
527
|
+
["keys", "Record.keys(...)"],
|
|
528
|
+
["values", "Record.values(...)"],
|
|
529
|
+
["entries", "Record.toEntries(...)"],
|
|
530
|
+
["fromEntries", "Record.fromEntries(...)"],
|
|
531
|
+
["assign", "Record.union(...) or object spread '{ ...a, ...b }'"],
|
|
532
|
+
[
|
|
533
|
+
"freeze",
|
|
534
|
+
"plain record literals '{ ... }' (FunkScript records are already immutable)",
|
|
535
|
+
],
|
|
536
|
+
["hasOwn", "Record.has(...)"],
|
|
537
|
+
]);
|
|
538
|
+
|
|
539
|
+
function getRootCalleeName(node: Expression): string | null {
|
|
540
|
+
if (node.kind === "Identifier" || node.kind === "UpperIdentifier") {
|
|
541
|
+
return node.name;
|
|
542
|
+
}
|
|
543
|
+
if (node.kind === "MemberExpression") {
|
|
544
|
+
return getRootCalleeName(node.object);
|
|
545
|
+
}
|
|
546
|
+
if (node.kind === "CallExpression") {
|
|
547
|
+
return getRootCalleeName(node.callee);
|
|
548
|
+
}
|
|
549
|
+
return null;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
export function isEffectModule(moduleName: string): boolean {
|
|
553
|
+
return EFFECT_MODULE_NAMES.has(moduleName);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// `Date` has both a call signature and a construct signature in TypeScript, so
|
|
557
|
+
// type-directed `constructorCalls` skips it; `Map`/`WeakMap` keep the 1-arg cast.
|
|
558
|
+
const AUTO_NEW_CONSTRUCTORS = new Set(["Date"]);
|
|
559
|
+
|
|
560
|
+
const NATIVE_CONSTRUCTOR_NAMES = new Set([
|
|
561
|
+
"Number",
|
|
562
|
+
"String",
|
|
563
|
+
"Boolean",
|
|
564
|
+
"Array",
|
|
565
|
+
]);
|
|
566
|
+
|
|
567
|
+
interface EmitContext {
|
|
568
|
+
readonly inPipelineStage: boolean;
|
|
569
|
+
readonly currentPlaceholderParam: string | null;
|
|
570
|
+
readonly inTryContext: boolean;
|
|
571
|
+
readonly pendingTryThunk: boolean;
|
|
572
|
+
readonly pendingTryRecord: boolean;
|
|
573
|
+
readonly activeSchemaParameters?: ReadonlySet<string>;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
export class Emitter {
|
|
577
|
+
private effectImports = new Set<string>();
|
|
578
|
+
public parameterScopes: Array<Map<string, boolean>> = [];
|
|
579
|
+
public variableTypeScopes: Array<Map<string, TypeNode | undefined>> = [];
|
|
580
|
+
private localBindingScopes: Array<
|
|
581
|
+
Map<string, { init: Expression; isMut: boolean }>
|
|
582
|
+
> = [];
|
|
583
|
+
public recordScopes: Array<
|
|
584
|
+
Map<string, Map<string, RecordFieldInfo> | undefined>
|
|
585
|
+
> = [];
|
|
586
|
+
private userEffectBindings = new Set<string>();
|
|
587
|
+
public effectModuleBindings = new Set<string>();
|
|
588
|
+
private userEsToolkitBindings = new Set<string>();
|
|
589
|
+
public userImportBindings = new Set<string>();
|
|
590
|
+
private esToolkitImports = new Set<string>();
|
|
591
|
+
private needsTsPattern = false;
|
|
592
|
+
private needsMutative = false;
|
|
593
|
+
private needsReferenceEquality = false;
|
|
594
|
+
private needsJsInterop = false;
|
|
595
|
+
private needsDecodeUnknown = false;
|
|
596
|
+
private needsFromNullishOrElse = false;
|
|
597
|
+
private needsFromNullishOr = false;
|
|
598
|
+
private needsOptionTry = false;
|
|
599
|
+
public primitiveIdentifiers = new Set<string>();
|
|
600
|
+
private placeholderCounter = 0;
|
|
601
|
+
public bindings = new Map<string, BindingDeclaration>();
|
|
602
|
+
private emitCtx: EmitContext = {
|
|
603
|
+
inPipelineStage: false,
|
|
604
|
+
currentPlaceholderParam: null,
|
|
605
|
+
inTryContext: false,
|
|
606
|
+
pendingTryThunk: false,
|
|
607
|
+
pendingTryRecord: false,
|
|
608
|
+
};
|
|
609
|
+
|
|
610
|
+
private withEmitContext<T>(patch: Partial<EmitContext>, fn: () => T): T {
|
|
611
|
+
const prev = this.emitCtx;
|
|
612
|
+
this.emitCtx = { ...prev, ...patch };
|
|
613
|
+
try {
|
|
614
|
+
return fn();
|
|
615
|
+
} finally {
|
|
616
|
+
this.emitCtx = prev;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
public activeDraftParams = new Set<string>();
|
|
620
|
+
|
|
621
|
+
public declaredTypeNames = new Set<string>();
|
|
622
|
+
public runtimeSchemaNames = new Set<string>();
|
|
623
|
+
public genericDataSchemas = new Map<string, number>();
|
|
624
|
+
public genericSchemaDeclarations = new Map<string, SchemaDeclaration>();
|
|
625
|
+
public get activeSchemaParameters(): ReadonlySet<string> | undefined {
|
|
626
|
+
return this.emitCtx.activeSchemaParameters;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
public withSchemaParameters<T>(names: ReadonlySet<string>, fn: () => T): T {
|
|
630
|
+
return this.withEmitContext({ activeSchemaParameters: names }, fn);
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
public readonly unsafeSchemaBinding: boolean;
|
|
634
|
+
public recordContracts = new Map<
|
|
635
|
+
string,
|
|
636
|
+
import("../ast/types.js").RecordTypeNode
|
|
637
|
+
>();
|
|
638
|
+
private readonly nullishSchemaFields = new Map<
|
|
639
|
+
string,
|
|
640
|
+
Map<string, TypeNode>
|
|
641
|
+
>();
|
|
642
|
+
private readonly typeSignatureSchemas = new Map<string, TypeNode>();
|
|
643
|
+
public needsCheckedRecordDecoder = false;
|
|
644
|
+
public checkedCallbackKinds = new Set<"sync" | "void" | "effect">();
|
|
645
|
+
public identityRecordSchemas = new Set<string>();
|
|
646
|
+
public checkedTaggedSchemas = new Map<string, TaggedUnionSchema>();
|
|
647
|
+
public variantFields = new Map<string, string[]>();
|
|
648
|
+
private taggedUnionVariants = new Map<string, Set<string>>();
|
|
649
|
+
private taggedUnionParameters = new Map<string, TypeParameter[]>();
|
|
650
|
+
private importedTaggedUnionNames = new Set<string>();
|
|
651
|
+
private importedTaggedFactories = new Map<string, string>();
|
|
652
|
+
public modularEffectImports: boolean;
|
|
653
|
+
public preserveFunImports: boolean;
|
|
654
|
+
|
|
655
|
+
public getRecordTypeNode(
|
|
656
|
+
name: string,
|
|
657
|
+
): import("../ast/types.js").RecordTypeNode | undefined {
|
|
658
|
+
return this.recordContracts.get(name);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
public addEffectImport(name: string): void {
|
|
662
|
+
this.effectImports.add(name);
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
constructor(
|
|
666
|
+
private program?: Program,
|
|
667
|
+
contextProgram?: Program,
|
|
668
|
+
public options: EmitterOptions = {},
|
|
669
|
+
) {
|
|
670
|
+
this.modularEffectImports = options.modularEffectImports ?? false;
|
|
671
|
+
this.preserveFunImports = options.preserveFunImports ?? false;
|
|
672
|
+
for (const [name, contract] of options.importedRecordContracts ?? [])
|
|
673
|
+
this.recordContracts.set(name, contract);
|
|
674
|
+
for (const [name, fields] of options.importedNullishSchemas ?? [])
|
|
675
|
+
this.nullishSchemaFields.set(name, new Map(fields));
|
|
676
|
+
for (const [name, variants] of options.importedTaggedUnionVariants ?? []) {
|
|
677
|
+
const returnedVariant = (
|
|
678
|
+
variants as ReadonlySet<string> & { returnedVariant?: string }
|
|
679
|
+
).returnedVariant;
|
|
680
|
+
if (returnedVariant) {
|
|
681
|
+
this.importedTaggedFactories.set(name, returnedVariant);
|
|
682
|
+
continue;
|
|
683
|
+
}
|
|
684
|
+
this.taggedUnionVariants.set(name, new Set(variants));
|
|
685
|
+
this.importedTaggedUnionNames.add(name);
|
|
686
|
+
const parameters = (
|
|
687
|
+
variants as ReadonlySet<string> & { typeParameters?: TypeParameter[] }
|
|
688
|
+
).typeParameters;
|
|
689
|
+
if (parameters) this.taggedUnionParameters.set(name, parameters);
|
|
690
|
+
}
|
|
691
|
+
// Source-map emission renders declarations individually, but resolution
|
|
692
|
+
// still needs the bindings from the complete source program.
|
|
693
|
+
let unsafeSchemaBinding = false;
|
|
694
|
+
const allItems = (contextProgram ?? program)?.body ?? [];
|
|
695
|
+
for (const item of allItems) {
|
|
696
|
+
const decl = item.kind === "ExportDeclaration" ? item.declaration : item;
|
|
697
|
+
if (item.kind === "ImportDeclaration") {
|
|
698
|
+
for (const spec of item.specifiers) {
|
|
699
|
+
if (
|
|
700
|
+
spec.local.name === "Schema" &&
|
|
701
|
+
(item.isTypeOnly ||
|
|
702
|
+
spec.isTypeOnly ||
|
|
703
|
+
item.source.raw !== '"effect"' ||
|
|
704
|
+
spec.imported.name !== "Schema")
|
|
705
|
+
)
|
|
706
|
+
unsafeSchemaBinding = true;
|
|
707
|
+
}
|
|
708
|
+
if (
|
|
709
|
+
item.namespaceBinding?.name === "Schema" &&
|
|
710
|
+
(item.isTypeOnly || item.source.raw !== '"effect/Schema"')
|
|
711
|
+
)
|
|
712
|
+
unsafeSchemaBinding = true;
|
|
713
|
+
if (!item.isTypeOnly) {
|
|
714
|
+
for (const spec of item.specifiers) {
|
|
715
|
+
if (!spec.isTypeOnly) this.userImportBindings.add(spec.local.name);
|
|
716
|
+
}
|
|
717
|
+
if (item.namespaceBinding)
|
|
718
|
+
this.userImportBindings.add(item.namespaceBinding.name);
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
if (item.kind === "ImportDeclaration" && item.source.raw === '"effect"') {
|
|
722
|
+
for (const spec of item.specifiers)
|
|
723
|
+
this.userEffectBindings.add(spec.local.name);
|
|
724
|
+
}
|
|
725
|
+
if (
|
|
726
|
+
item.kind === "ImportDeclaration" &&
|
|
727
|
+
item.source.raw === '"es-toolkit"'
|
|
728
|
+
) {
|
|
729
|
+
for (const spec of item.specifiers)
|
|
730
|
+
this.userEsToolkitBindings.add(spec.local.name);
|
|
731
|
+
}
|
|
732
|
+
if (
|
|
733
|
+
item.kind === "ImportDeclaration" &&
|
|
734
|
+
/^"effect(?:\/|")/.test(item.source.raw)
|
|
735
|
+
) {
|
|
736
|
+
for (const spec of item.specifiers)
|
|
737
|
+
this.effectModuleBindings.add(spec.local.name);
|
|
738
|
+
if (item.namespaceBinding) {
|
|
739
|
+
this.effectModuleBindings.add(item.namespaceBinding.name);
|
|
740
|
+
if (item.source.raw === `"effect/${item.namespaceBinding.name}"`) {
|
|
741
|
+
this.userEffectBindings.add(item.namespaceBinding.name);
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
if (
|
|
746
|
+
decl.kind === "SchemaDeclaration" ||
|
|
747
|
+
decl.kind === "ErrorDeclaration" ||
|
|
748
|
+
decl.kind === "ServiceDeclaration"
|
|
749
|
+
) {
|
|
750
|
+
this.declaredTypeNames.add(decl.id.name);
|
|
751
|
+
}
|
|
752
|
+
if (decl.kind === "BindingDeclaration" && "name" in decl.id)
|
|
753
|
+
this.bindings.set(decl.id.name, decl);
|
|
754
|
+
}
|
|
755
|
+
this.unsafeSchemaBinding = unsafeSchemaBinding;
|
|
756
|
+
|
|
757
|
+
const staticTypeNames = new Set<string>();
|
|
758
|
+
for (const [name] of this.recordContracts) staticTypeNames.add(name);
|
|
759
|
+
for (const item of allItems) {
|
|
760
|
+
const decl = item.kind === "ExportDeclaration" ? item.declaration : item;
|
|
761
|
+
if (
|
|
762
|
+
decl.kind === "SchemaDeclaration" &&
|
|
763
|
+
decl.body.kind === "TypeSignatureSchema"
|
|
764
|
+
) {
|
|
765
|
+
staticTypeNames.add(decl.id.name);
|
|
766
|
+
if (!decl.typeParams?.length) {
|
|
767
|
+
this.typeSignatureSchemas.set(decl.id.name, decl.body.signature);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
for (const item of allItems) {
|
|
772
|
+
const decl = item.kind === "ExportDeclaration" ? item.declaration : item;
|
|
773
|
+
if (
|
|
774
|
+
decl.kind === "SchemaDeclaration" &&
|
|
775
|
+
decl.body.kind === "RecordSchema"
|
|
776
|
+
) {
|
|
777
|
+
const fields = new Map<string, TypeNode>();
|
|
778
|
+
for (const field of decl.body.fields) {
|
|
779
|
+
const resolvedFieldType = this.unwrapTypeAlias(field.typeAnnotation);
|
|
780
|
+
if (
|
|
781
|
+
resolvedFieldType?.kind === "TypeReferenceNode" &&
|
|
782
|
+
resolvedFieldType.name.name === "JSNullish"
|
|
783
|
+
) {
|
|
784
|
+
fields.set(field.name.name, resolvedFieldType);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
if (fields.size) this.nullishSchemaFields.set(decl.id.name, fields);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
// Conservative whitelist: only local, non-transforming record codecs are used
|
|
791
|
+
// for generated Effect callback adapters. Optional/defaulted fields can change
|
|
792
|
+
// their encoded representation and are deliberately excluded.
|
|
793
|
+
const identityType = (type: TypeNode): boolean => {
|
|
794
|
+
if (type.kind === "LiteralTypeNode") return true;
|
|
795
|
+
if (type.kind === "UnionTypeNode") return type.types.every(identityType);
|
|
796
|
+
if (type.kind === "ParenthesizedTypeNode")
|
|
797
|
+
return identityType(type.typeNode);
|
|
798
|
+
return (
|
|
799
|
+
type.kind === "TypeReferenceNode" &&
|
|
800
|
+
!type.typeArgs?.length &&
|
|
801
|
+
((["String", "Number", "Boolean"].includes(type.name.name) &&
|
|
802
|
+
!this.declaredTypeNames.has(type.name.name)) ||
|
|
803
|
+
this.identityRecordSchemas.has(type.name.name))
|
|
804
|
+
);
|
|
805
|
+
};
|
|
806
|
+
let addedIdentity = true;
|
|
807
|
+
while (addedIdentity) {
|
|
808
|
+
addedIdentity = false;
|
|
809
|
+
for (const item of allItems) {
|
|
810
|
+
const decl =
|
|
811
|
+
item.kind === "ExportDeclaration" ? item.declaration : item;
|
|
812
|
+
if (
|
|
813
|
+
decl.kind !== "SchemaDeclaration" ||
|
|
814
|
+
decl.body.kind !== "RecordSchema" ||
|
|
815
|
+
decl.typeParams?.length ||
|
|
816
|
+
this.identityRecordSchemas.has(decl.id.name)
|
|
817
|
+
)
|
|
818
|
+
continue;
|
|
819
|
+
if (
|
|
820
|
+
decl.body.fields.every(
|
|
821
|
+
(field) =>
|
|
822
|
+
!field.defaultValue && identityType(field.typeAnnotation),
|
|
823
|
+
)
|
|
824
|
+
) {
|
|
825
|
+
this.identityRecordSchemas.add(decl.id.name);
|
|
826
|
+
addedIdentity = true;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
for (const item of allItems) {
|
|
831
|
+
const decl = item.kind === "ExportDeclaration" ? item.declaration : item;
|
|
832
|
+
if (
|
|
833
|
+
decl.kind === "SchemaDeclaration" &&
|
|
834
|
+
decl.body.kind === "TaggedUnionSchema"
|
|
835
|
+
) {
|
|
836
|
+
this.taggedUnionParameters.set(decl.id.name, decl.typeParams ?? []);
|
|
837
|
+
this.importedTaggedUnionNames.delete(decl.id.name);
|
|
838
|
+
this.taggedUnionVariants.set(
|
|
839
|
+
decl.id.name,
|
|
840
|
+
new Set(decl.body.variants.map((variant) => variant.name.name)),
|
|
841
|
+
);
|
|
842
|
+
for (const variant of decl.body.variants) {
|
|
843
|
+
this.variantFields.set(
|
|
844
|
+
variant.name.name,
|
|
845
|
+
variant.fields.map((f) => f.name.name),
|
|
846
|
+
);
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
// These codecs can be checked in place without transforming tagged payloads.
|
|
851
|
+
// Optional/defaulted, recursive, generic, and unverified imports stay excluded.
|
|
852
|
+
for (const item of allItems) {
|
|
853
|
+
const decl = item.kind === "ExportDeclaration" ? item.declaration : item;
|
|
854
|
+
if (
|
|
855
|
+
decl.kind === "SchemaDeclaration" &&
|
|
856
|
+
decl.body.kind === "TaggedUnionSchema" &&
|
|
857
|
+
!decl.typeParams?.length &&
|
|
858
|
+
decl.body.variants.length > 0 &&
|
|
859
|
+
decl.body.variants.every((variant) =>
|
|
860
|
+
variant.fields.every(
|
|
861
|
+
(field) =>
|
|
862
|
+
field.name.name !== "_tag" &&
|
|
863
|
+
field.typeAnnotation.kind === "TypeReferenceNode" &&
|
|
864
|
+
!field.typeAnnotation.typeArgs?.length &&
|
|
865
|
+
((["String", "Number", "Boolean"].includes(
|
|
866
|
+
field.typeAnnotation.name.name,
|
|
867
|
+
) &&
|
|
868
|
+
!this.declaredTypeNames.has(field.typeAnnotation.name.name)) ||
|
|
869
|
+
this.identityRecordSchemas.has(
|
|
870
|
+
field.typeAnnotation.name.name,
|
|
871
|
+
) ||
|
|
872
|
+
this.options.importedIdentitySchemas?.has(
|
|
873
|
+
field.typeAnnotation.name.name,
|
|
874
|
+
) === true),
|
|
875
|
+
),
|
|
876
|
+
)
|
|
877
|
+
) {
|
|
878
|
+
this.checkedTaggedSchemas.set(decl.id.name, decl.body);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
let changed = true;
|
|
882
|
+
while (changed) {
|
|
883
|
+
changed = false;
|
|
884
|
+
for (const item of allItems) {
|
|
885
|
+
const decl =
|
|
886
|
+
item.kind === "ExportDeclaration" ? item.declaration : item;
|
|
887
|
+
if (
|
|
888
|
+
decl.kind === "SchemaDeclaration" &&
|
|
889
|
+
decl.body.kind === "RecordSchema" &&
|
|
890
|
+
!staticTypeNames.has(decl.id.name)
|
|
891
|
+
) {
|
|
892
|
+
const visibleStatics = new Set(staticTypeNames);
|
|
893
|
+
for (const param of decl.typeParams ?? [])
|
|
894
|
+
visibleStatics.delete(param.name.name);
|
|
895
|
+
if (isRecordContract(decl.body, visibleStatics)) {
|
|
896
|
+
staticTypeNames.add(decl.id.name);
|
|
897
|
+
changed = true;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
for (const item of allItems) {
|
|
904
|
+
const decl = item.kind === "ExportDeclaration" ? item.declaration : item;
|
|
905
|
+
if (decl.kind === "SchemaDeclaration") {
|
|
906
|
+
if (
|
|
907
|
+
decl.body.kind === "RecordSchema" &&
|
|
908
|
+
staticTypeNames.has(decl.id.name)
|
|
909
|
+
) {
|
|
910
|
+
this.recordContracts.set(decl.id.name, recordContractType(decl.body));
|
|
911
|
+
} else if (
|
|
912
|
+
(decl.body.kind === "RecordSchema" &&
|
|
913
|
+
!staticTypeNames.has(decl.id.name)) ||
|
|
914
|
+
decl.body.kind === "StructuralUnionSchema" ||
|
|
915
|
+
decl.body.kind === "RefinementSchema" ||
|
|
916
|
+
decl.body.kind === "TsDerivedSchema" ||
|
|
917
|
+
decl.body.kind === "ImportedTsSchema"
|
|
918
|
+
) {
|
|
919
|
+
this.runtimeSchemaNames.add(decl.id.name);
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
for (const item of allItems) {
|
|
924
|
+
const decl = item.kind === "ExportDeclaration" ? item.declaration : item;
|
|
925
|
+
if (
|
|
926
|
+
decl.kind === "SchemaDeclaration" &&
|
|
927
|
+
(decl.body.kind === "RecordSchema" ||
|
|
928
|
+
decl.body.kind === "StructuralUnionSchema") &&
|
|
929
|
+
decl.typeParams?.length &&
|
|
930
|
+
!this.recordContracts.has(decl.id.name)
|
|
931
|
+
) {
|
|
932
|
+
this.genericDataSchemas.set(decl.id.name, decl.typeParams.length);
|
|
933
|
+
this.genericSchemaDeclarations.set(decl.id.name, decl);
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
public emit(): string {
|
|
939
|
+
const bodyParts =
|
|
940
|
+
this.program?.body.map((decl) => this.emitTopLevelDeclaration(decl)) ??
|
|
941
|
+
[];
|
|
942
|
+
|
|
943
|
+
// Generate header imports
|
|
944
|
+
let header = "";
|
|
945
|
+
const generatedEffectImports = Array.from(this.effectImports).filter(
|
|
946
|
+
(name) => {
|
|
947
|
+
const asMatch = /^(.+?)\s+as\s+(.+)$/.exec(name);
|
|
948
|
+
const localName = asMatch ? asMatch[2] : name;
|
|
949
|
+
const source = COMPANION_IMPORT_SOURCE[asMatch ? asMatch[1] : name];
|
|
950
|
+
if (source) return !this.userImportBindings.has(localName);
|
|
951
|
+
return !this.userEffectBindings.has(localName);
|
|
952
|
+
},
|
|
953
|
+
);
|
|
954
|
+
// Generated imports stay in the header, preserving their module evaluation
|
|
955
|
+
// order. Compatible user bindings can share that existing import.
|
|
956
|
+
const mergeNamedImport = (source: string, names: string[]): string[] => {
|
|
957
|
+
if (this.modularEffectImports) return names;
|
|
958
|
+
const decl = this.program?.body.find(
|
|
959
|
+
(item) =>
|
|
960
|
+
item.kind === "ImportDeclaration" &&
|
|
961
|
+
(item.source.raw === JSON.stringify(source) ||
|
|
962
|
+
item.source.raw === `'${source}'`) &&
|
|
963
|
+
!item.isSideEffect &&
|
|
964
|
+
!item.isTypeOnly &&
|
|
965
|
+
!item.defaultBinding &&
|
|
966
|
+
!item.namespaceBinding &&
|
|
967
|
+
item.specifiers.length > 0,
|
|
968
|
+
);
|
|
969
|
+
if (!decl || decl.kind !== "ImportDeclaration") return names;
|
|
970
|
+
const index = this.program!.body.indexOf(decl);
|
|
971
|
+
bodyParts[index] = "";
|
|
972
|
+
const userNames = decl.specifiers.map(
|
|
973
|
+
(spec) =>
|
|
974
|
+
`${spec.isTypeOnly ? "type " : ""}${spec.imported.name}${spec.imported.name === spec.local.name ? "" : ` as ${spec.local.name}`}`,
|
|
975
|
+
);
|
|
976
|
+
return [...userNames, ...names];
|
|
977
|
+
};
|
|
978
|
+
if (generatedEffectImports.length > 0) {
|
|
979
|
+
if (this.modularEffectImports) {
|
|
980
|
+
for (const name of generatedEffectImports.sort()) {
|
|
981
|
+
const asMatch = /^(.+?)\s+as\s+(.+)$/.exec(name);
|
|
982
|
+
const moduleName = asMatch ? asMatch[1] : name;
|
|
983
|
+
const localName = asMatch ? asMatch[2] : name;
|
|
984
|
+
const source = COMPANION_IMPORT_SOURCE[moduleName];
|
|
985
|
+
if (moduleName === "pipe") {
|
|
986
|
+
const spec =
|
|
987
|
+
localName === moduleName ? "pipe" : `pipe as ${localName}`;
|
|
988
|
+
header += `import { ${spec} } from "effect/Function";\n`;
|
|
989
|
+
} else if (source) {
|
|
990
|
+
const spec =
|
|
991
|
+
localName === moduleName
|
|
992
|
+
? moduleName
|
|
993
|
+
: `${moduleName} as ${localName}`;
|
|
994
|
+
header += `import { ${spec} } from ${JSON.stringify(source)};\n`;
|
|
995
|
+
} else {
|
|
996
|
+
header += `import * as ${localName} from "effect/${moduleName}";\n`;
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
} else {
|
|
1000
|
+
const bySource = new Map<string, string[]>();
|
|
1001
|
+
for (const name of generatedEffectImports.sort()) {
|
|
1002
|
+
const asMatch = /^(.+?)\s+as\s+(.+)$/.exec(name);
|
|
1003
|
+
const moduleName = asMatch ? asMatch[1] : name;
|
|
1004
|
+
const source = COMPANION_IMPORT_SOURCE[moduleName] ?? "effect";
|
|
1005
|
+
const list = bySource.get(source) ?? [];
|
|
1006
|
+
list.push(name);
|
|
1007
|
+
bySource.set(source, list);
|
|
1008
|
+
}
|
|
1009
|
+
for (const source of [...bySource.keys()].sort()) {
|
|
1010
|
+
const names = bySource.get(source)!;
|
|
1011
|
+
const combined = mergeNamedImport(source, names);
|
|
1012
|
+
header += `import { ${combined.join(", ")} } from ${JSON.stringify(source)};\n`;
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
const generatedEsToolkitImports = Array.from(this.esToolkitImports)
|
|
1017
|
+
.filter((name) => !this.userEsToolkitBindings.has(name))
|
|
1018
|
+
.map((name) => (name === "slug" ? "kebabCase as slug" : name))
|
|
1019
|
+
.sort((a, b) => a.localeCompare(b));
|
|
1020
|
+
if (generatedEsToolkitImports.length > 0) {
|
|
1021
|
+
const combined = mergeNamedImport(
|
|
1022
|
+
"es-toolkit",
|
|
1023
|
+
generatedEsToolkitImports,
|
|
1024
|
+
);
|
|
1025
|
+
header += `import { ${combined.join(", ")} } from "es-toolkit";\n`;
|
|
1026
|
+
}
|
|
1027
|
+
if (this.needsCheckedRecordDecoder) {
|
|
1028
|
+
const helpers = ["decodeCheckedRecord as $funscriptDecodeCheckedRecord"];
|
|
1029
|
+
if (this.checkedCallbackKinds.has("sync"))
|
|
1030
|
+
helpers.push("checkedSync as $funscriptCheckedSync");
|
|
1031
|
+
if (this.checkedCallbackKinds.has("void"))
|
|
1032
|
+
helpers.push("checkedSyncVoid as $funscriptCheckedSyncVoid");
|
|
1033
|
+
if (this.checkedCallbackKinds.has("effect"))
|
|
1034
|
+
helpers.push(
|
|
1035
|
+
"checkedEffect as $funscriptCheckedEffect",
|
|
1036
|
+
"type CallbackBoundaryError as $funscriptCallbackBoundaryError",
|
|
1037
|
+
);
|
|
1038
|
+
header += `import { ${helpers.join(", ")} } from "funkscript/callback";\n`;
|
|
1039
|
+
}
|
|
1040
|
+
if (this.needsTsPattern) {
|
|
1041
|
+
header += `import { match, P } from "ts-pattern";\n`;
|
|
1042
|
+
}
|
|
1043
|
+
if (this.needsMutative) {
|
|
1044
|
+
header += `import { create } from "mutative";\n`;
|
|
1045
|
+
}
|
|
1046
|
+
if (this.needsJsInterop) {
|
|
1047
|
+
header += `import { jsInterop as __funscriptJs } from "funkscript/js";\n`;
|
|
1048
|
+
}
|
|
1049
|
+
header += emitRuntimeHelpers({
|
|
1050
|
+
referenceEquality: this.needsReferenceEquality,
|
|
1051
|
+
decodeUnknown: this.needsDecodeUnknown,
|
|
1052
|
+
fromNullishOrElse: this.needsFromNullishOrElse,
|
|
1053
|
+
fromNullishOr: this.needsFromNullishOr,
|
|
1054
|
+
optionTry: this.needsOptionTry,
|
|
1055
|
+
});
|
|
1056
|
+
|
|
1057
|
+
if (header.length > 0) {
|
|
1058
|
+
header += "\n";
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
return header + bodyParts.join("\n\n").trim() + "\n";
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
private emitTopLevelDeclaration(
|
|
1065
|
+
decl: TopLevelDeclaration,
|
|
1066
|
+
isExport = false,
|
|
1067
|
+
): string {
|
|
1068
|
+
return emitTopLevelDeclaration(this, decl, isExport);
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
public emitSchemaDeclaration(
|
|
1072
|
+
decl: SchemaDeclaration,
|
|
1073
|
+
isExport: boolean,
|
|
1074
|
+
): string {
|
|
1075
|
+
return emitSchemaDeclaration(this, decl, isExport);
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
public emitSchemaTypeField(typeNode: TypeNode): string {
|
|
1079
|
+
return emitSchemaTypeField(this, typeNode);
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
public emitErrorDeclaration(
|
|
1083
|
+
decl: ErrorDeclaration,
|
|
1084
|
+
isExport: boolean,
|
|
1085
|
+
): string {
|
|
1086
|
+
return emitErrorDeclaration(this, decl, isExport);
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
public emitServiceDeclaration(
|
|
1090
|
+
decl: ServiceDeclaration,
|
|
1091
|
+
isExport: boolean,
|
|
1092
|
+
): string {
|
|
1093
|
+
return emitServiceDeclaration(this, decl, isExport);
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
public isPrimitiveType(type?: TypeNode): boolean {
|
|
1097
|
+
return isPrimitiveType(type);
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
public isPrimitive(expr: Expression): boolean {
|
|
1101
|
+
if (
|
|
1102
|
+
expr.kind === "NumberLiteral" ||
|
|
1103
|
+
expr.kind === "StringLiteral" ||
|
|
1104
|
+
expr.kind === "BooleanLiteral"
|
|
1105
|
+
) {
|
|
1106
|
+
return true;
|
|
1107
|
+
}
|
|
1108
|
+
if (expr.kind === "UnaryExpression") {
|
|
1109
|
+
return true;
|
|
1110
|
+
}
|
|
1111
|
+
if (expr.kind === "BinaryExpression") {
|
|
1112
|
+
return true;
|
|
1113
|
+
}
|
|
1114
|
+
if (
|
|
1115
|
+
expr.kind === "Identifier" &&
|
|
1116
|
+
this.primitiveIdentifiers.has(expr.name)
|
|
1117
|
+
) {
|
|
1118
|
+
return true;
|
|
1119
|
+
}
|
|
1120
|
+
return false;
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
private isOptionalParameter(name: string): boolean {
|
|
1124
|
+
return (
|
|
1125
|
+
[...this.parameterScopes]
|
|
1126
|
+
.reverse()
|
|
1127
|
+
.find((scope) => scope.has(name))
|
|
1128
|
+
?.get(name) ?? false
|
|
1129
|
+
);
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
public recordFields(name: string): Map<string, RecordFieldInfo> | undefined {
|
|
1133
|
+
for (let i = this.recordScopes.length - 1; i >= 0; i--) {
|
|
1134
|
+
if (this.recordScopes[i].has(name)) return this.recordScopes[i].get(name);
|
|
1135
|
+
}
|
|
1136
|
+
return undefined;
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
public recordField(expr: Expression): RecordFieldInfo | undefined {
|
|
1140
|
+
if (
|
|
1141
|
+
expr.kind === "IndexExpression" &&
|
|
1142
|
+
expr.index.kind === "StringLiteral"
|
|
1143
|
+
) {
|
|
1144
|
+
const propName = expr.index.parts
|
|
1145
|
+
.map((p) => ("value" in p ? String(p.value) : ""))
|
|
1146
|
+
.join("");
|
|
1147
|
+
const fields =
|
|
1148
|
+
expr.object.kind === "Identifier"
|
|
1149
|
+
? this.recordFields(expr.object.name)
|
|
1150
|
+
: this.recordField(expr.object)?.fields;
|
|
1151
|
+
return fields?.get(propName);
|
|
1152
|
+
}
|
|
1153
|
+
if (expr.kind !== "MemberExpression") return undefined;
|
|
1154
|
+
const fields =
|
|
1155
|
+
expr.object.kind === "Identifier"
|
|
1156
|
+
? this.recordFields(expr.object.name)
|
|
1157
|
+
: this.recordField(expr.object)?.fields;
|
|
1158
|
+
return fields?.get(expr.property.name);
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
public getTypeNodeForIdentifier(name: string): TypeNode | undefined {
|
|
1162
|
+
for (let i = this.variableTypeScopes.length - 1; i >= 0; i--) {
|
|
1163
|
+
if (this.variableTypeScopes[i].has(name)) {
|
|
1164
|
+
return this.variableTypeScopes[i].get(name);
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
return this.bindings.get(name)?.typeAnnotation;
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
private unwrapTypeAlias(
|
|
1171
|
+
type: TypeNode | undefined,
|
|
1172
|
+
visited: Set<string> = new Set(),
|
|
1173
|
+
): TypeNode | undefined {
|
|
1174
|
+
let curr = type;
|
|
1175
|
+
while (
|
|
1176
|
+
curr?.kind === "TypeReferenceNode" &&
|
|
1177
|
+
!curr.typeArgs?.length &&
|
|
1178
|
+
this.typeSignatureSchemas.has(curr.name.name) &&
|
|
1179
|
+
!visited.has(curr.name.name)
|
|
1180
|
+
) {
|
|
1181
|
+
visited.add(curr.name.name);
|
|
1182
|
+
curr = this.typeSignatureSchemas.get(curr.name.name);
|
|
1183
|
+
}
|
|
1184
|
+
return curr;
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
public getTypeNodeForExpression(expr: Expression): TypeNode | undefined {
|
|
1188
|
+
return this.unwrapTypeAlias(
|
|
1189
|
+
this.resolveExpressionTypeNode(expr, new Set()),
|
|
1190
|
+
);
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
private resolveExpressionTypeNode(
|
|
1194
|
+
expr: Expression,
|
|
1195
|
+
visited: Set<string>,
|
|
1196
|
+
): TypeNode | undefined {
|
|
1197
|
+
if (expr.kind === "Identifier") {
|
|
1198
|
+
for (let i = this.variableTypeScopes.length - 1; i >= 0; i--) {
|
|
1199
|
+
if (this.variableTypeScopes[i].has(expr.name))
|
|
1200
|
+
return this.variableTypeScopes[i].get(expr.name);
|
|
1201
|
+
}
|
|
1202
|
+
if (visited.has(expr.name)) return undefined;
|
|
1203
|
+
const binding = this.bindings.get(expr.name);
|
|
1204
|
+
if (!binding) return undefined;
|
|
1205
|
+
if (binding.typeAnnotation) return binding.typeAnnotation;
|
|
1206
|
+
visited.add(expr.name);
|
|
1207
|
+
return this.resolveExpressionTypeNode(binding.init, visited);
|
|
1208
|
+
}
|
|
1209
|
+
if (expr.kind === "FunctionExpression" && expr.returnType) {
|
|
1210
|
+
return {
|
|
1211
|
+
kind: "FunctionTypeNode",
|
|
1212
|
+
params: expr.params,
|
|
1213
|
+
typeParams: expr.typeParams,
|
|
1214
|
+
returnType: expr.returnType,
|
|
1215
|
+
span: expr.span,
|
|
1216
|
+
};
|
|
1217
|
+
}
|
|
1218
|
+
if (expr.kind === "CallExpression") {
|
|
1219
|
+
const callee = this.resolveExpressionTypeNode(expr.callee, visited);
|
|
1220
|
+
if (callee?.kind === "FunctionTypeNode") return callee.returnType;
|
|
1221
|
+
}
|
|
1222
|
+
if (expr.kind === "MemberExpression") {
|
|
1223
|
+
const objType = this.resolveExpressionTypeNode(expr.object, visited);
|
|
1224
|
+
if (!objType) return undefined;
|
|
1225
|
+
let recordType: TypeNode | undefined = objType;
|
|
1226
|
+
if (recordType.kind === "TypeReferenceNode") {
|
|
1227
|
+
const nullishField = this.nullishSchemaFields
|
|
1228
|
+
.get(recordType.name.name)
|
|
1229
|
+
?.get(expr.property.name);
|
|
1230
|
+
if (nullishField) return nullishField;
|
|
1231
|
+
if (
|
|
1232
|
+
recordType.name.name === "Option" &&
|
|
1233
|
+
recordType.typeArgs?.length === 1
|
|
1234
|
+
) {
|
|
1235
|
+
recordType = recordType.typeArgs[0];
|
|
1236
|
+
} else {
|
|
1237
|
+
recordType = this.recordContracts.get(recordType.name.name);
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
if (recordType?.kind === "RecordTypeNode") {
|
|
1241
|
+
const field = recordType.fields.find(
|
|
1242
|
+
(f) => f.name.name === expr.property.name,
|
|
1243
|
+
);
|
|
1244
|
+
return field?.typeAnnotation;
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
return undefined;
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
private isTargetNativeCategory(
|
|
1251
|
+
expr: Expression,
|
|
1252
|
+
category: string,
|
|
1253
|
+
prop: string,
|
|
1254
|
+
visitedBindings: Set<string> = new Set(),
|
|
1255
|
+
): boolean {
|
|
1256
|
+
if (category === "array" && expr.kind === "ArrayExpression") return true;
|
|
1257
|
+
if (
|
|
1258
|
+
category === "string" &&
|
|
1259
|
+
(expr.kind === "StringLiteral" || expr.kind === "TemplateLiteral")
|
|
1260
|
+
)
|
|
1261
|
+
return true;
|
|
1262
|
+
if (expr.kind === "CallExpression") {
|
|
1263
|
+
const callee = expr.callee;
|
|
1264
|
+
if (callee.kind === "Identifier" || callee.kind === "UpperIdentifier") {
|
|
1265
|
+
if (category === "array" && callee.name === "Array") return true;
|
|
1266
|
+
if (
|
|
1267
|
+
category === "collection" &&
|
|
1268
|
+
(callee.name === "Map" ||
|
|
1269
|
+
callee.name === "Set" ||
|
|
1270
|
+
callee.name === "WeakMap" ||
|
|
1271
|
+
callee.name === "WeakSet")
|
|
1272
|
+
)
|
|
1273
|
+
return true;
|
|
1274
|
+
if (category === "date" && callee.name === "Date") return true;
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
const typeNode = this.getTypeNodeForExpression(expr);
|
|
1279
|
+
if (typeNode) {
|
|
1280
|
+
if (typeNode.kind === "ArrayTypeNode") {
|
|
1281
|
+
return category === "array";
|
|
1282
|
+
}
|
|
1283
|
+
if (typeNode.kind === "TypeReferenceNode") {
|
|
1284
|
+
const typeName = typeNode.name.name;
|
|
1285
|
+
if (typeName === "Array" || typeName === "ReadonlyArray") {
|
|
1286
|
+
return category === "array";
|
|
1287
|
+
}
|
|
1288
|
+
if (typeName === "String" || typeName === "string") {
|
|
1289
|
+
return category === "string";
|
|
1290
|
+
}
|
|
1291
|
+
if (
|
|
1292
|
+
typeName === "Map" ||
|
|
1293
|
+
typeName === "Set" ||
|
|
1294
|
+
typeName === "WeakMap" ||
|
|
1295
|
+
typeName === "WeakSet"
|
|
1296
|
+
) {
|
|
1297
|
+
return category === "collection";
|
|
1298
|
+
}
|
|
1299
|
+
if (typeName === "Date") {
|
|
1300
|
+
return category === "date";
|
|
1301
|
+
}
|
|
1302
|
+
// Any other type reference (URLSearchParams, Headers, FormData, Request, Response, URL, Observable, QueryBuilder, Parser, Store, etc.)
|
|
1303
|
+
// is NOT a native JavaScript type replaced by Effect.
|
|
1304
|
+
return false;
|
|
1305
|
+
}
|
|
1306
|
+
if (
|
|
1307
|
+
typeNode.kind === "RecordTypeNode" ||
|
|
1308
|
+
typeNode.kind === "FunctionTypeNode"
|
|
1309
|
+
) {
|
|
1310
|
+
return false;
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
if (expr.kind === "Identifier") {
|
|
1315
|
+
const name = expr.name;
|
|
1316
|
+
// Check if initialized from a binding
|
|
1317
|
+
const binding = this.bindings.get(name);
|
|
1318
|
+
if (binding) {
|
|
1319
|
+
if (!visitedBindings.has(name)) {
|
|
1320
|
+
visitedBindings.add(name);
|
|
1321
|
+
if (
|
|
1322
|
+
this.isTargetNativeCategory(
|
|
1323
|
+
binding.init,
|
|
1324
|
+
category,
|
|
1325
|
+
prop,
|
|
1326
|
+
visitedBindings,
|
|
1327
|
+
)
|
|
1328
|
+
) {
|
|
1329
|
+
return true;
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
// TypeScript TypeChecker check (for unannotated identifiers, ts escape blocks, external imports)
|
|
1336
|
+
if (this.options.context) {
|
|
1337
|
+
const checker = this.options.context.getTypeChecker();
|
|
1338
|
+
const program = this.options.context.getProgram();
|
|
1339
|
+
if (checker && program && expr.kind === "Identifier") {
|
|
1340
|
+
const sourceFile = program.getSourceFile(
|
|
1341
|
+
this.options.context.getNormalizedFilename(),
|
|
1342
|
+
);
|
|
1343
|
+
if (sourceFile) {
|
|
1344
|
+
const tsType = this.findTsTypeForIdentifier(
|
|
1345
|
+
sourceFile,
|
|
1346
|
+
checker,
|
|
1347
|
+
expr.name,
|
|
1348
|
+
);
|
|
1349
|
+
if (tsType) {
|
|
1350
|
+
const symName = tsType.symbol?.name ?? tsType.aliasSymbol?.name;
|
|
1351
|
+
if (category === "array") {
|
|
1352
|
+
return (
|
|
1353
|
+
checker.isArrayType(tsType) ||
|
|
1354
|
+
symName === "Array" ||
|
|
1355
|
+
symName === "ReadonlyArray"
|
|
1356
|
+
);
|
|
1357
|
+
}
|
|
1358
|
+
if (category === "string") {
|
|
1359
|
+
return Boolean(tsType.flags & ts.TypeFlags.StringLike);
|
|
1360
|
+
}
|
|
1361
|
+
if (category === "collection") {
|
|
1362
|
+
return (
|
|
1363
|
+
symName === "Map" ||
|
|
1364
|
+
symName === "Set" ||
|
|
1365
|
+
symName === "WeakMap" ||
|
|
1366
|
+
symName === "WeakSet"
|
|
1367
|
+
);
|
|
1368
|
+
}
|
|
1369
|
+
if (category === "date") {
|
|
1370
|
+
return symName === "Date";
|
|
1371
|
+
}
|
|
1372
|
+
return false;
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
return false;
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
private findTsTypeForIdentifier(
|
|
1382
|
+
sourceFile: ts.SourceFile,
|
|
1383
|
+
checker: ts.TypeChecker,
|
|
1384
|
+
name: string,
|
|
1385
|
+
): ts.Type | undefined {
|
|
1386
|
+
let foundType: ts.Type | undefined;
|
|
1387
|
+
const visit = (node: ts.Node): void => {
|
|
1388
|
+
if (ts.isIdentifier(node) && node.text === name) {
|
|
1389
|
+
foundType = checker.getTypeAtLocation(node);
|
|
1390
|
+
}
|
|
1391
|
+
if (!foundType) {
|
|
1392
|
+
ts.forEachChild(node, visit);
|
|
1393
|
+
}
|
|
1394
|
+
};
|
|
1395
|
+
visit(sourceFile);
|
|
1396
|
+
return foundType;
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
private isHtmlPropsType(type?: TypeNode): boolean {
|
|
1400
|
+
if (!type) return false;
|
|
1401
|
+
if (type.kind === "TypeReferenceNode" && type.name.name === "HtmlProps")
|
|
1402
|
+
return true;
|
|
1403
|
+
if (type.kind === "IntersectionTypeNode") {
|
|
1404
|
+
return type.types.some((t) => this.isHtmlPropsType(t));
|
|
1405
|
+
}
|
|
1406
|
+
return false;
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
public isOptionalValue(expr: Expression): boolean {
|
|
1410
|
+
if (expr.kind === "Identifier") return this.isOptionalParameter(expr.name);
|
|
1411
|
+
if (expr.kind === "MemberExpression")
|
|
1412
|
+
return this.recordField(expr)?.optional ?? false;
|
|
1413
|
+
if (expr.kind === "IndexExpression") return true;
|
|
1414
|
+
return false;
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
private validateRecordDefault(
|
|
1418
|
+
value: Expression,
|
|
1419
|
+
currentParamNames: Set<string>,
|
|
1420
|
+
subsequentParamNames: Set<string>,
|
|
1421
|
+
): void {
|
|
1422
|
+
const visit = (
|
|
1423
|
+
item: unknown,
|
|
1424
|
+
parent?: Record<string, unknown>,
|
|
1425
|
+
key?: string,
|
|
1426
|
+
): void => {
|
|
1427
|
+
if (!item || typeof item !== "object") return;
|
|
1428
|
+
if (Array.isArray(item)) {
|
|
1429
|
+
for (const child of item) visit(child, parent, key);
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
const node = item as Record<string, unknown>;
|
|
1433
|
+
if (node.kind === "Identifier" && typeof node.name === "string") {
|
|
1434
|
+
if (
|
|
1435
|
+
(parent?.kind === "MemberExpression" && key === "property") ||
|
|
1436
|
+
(parent?.kind === "FieldEntry" && key === "key")
|
|
1437
|
+
)
|
|
1438
|
+
return;
|
|
1439
|
+
if (currentParamNames.has(node.name)) {
|
|
1440
|
+
throw new FunScriptError(
|
|
1441
|
+
`Defaulted record field cannot reference its own record parameter '${node.name}'`,
|
|
1442
|
+
node.span as Span | undefined,
|
|
1443
|
+
);
|
|
1444
|
+
}
|
|
1445
|
+
if (subsequentParamNames.has(node.name)) {
|
|
1446
|
+
throw new FunScriptError(
|
|
1447
|
+
`Parameter '${node.name}' cannot be referenced in a default value before its declaration`,
|
|
1448
|
+
node.span as Span | undefined,
|
|
1449
|
+
);
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
for (const [childKey, child] of Object.entries(node)) {
|
|
1453
|
+
if (
|
|
1454
|
+
childKey !== "span" &&
|
|
1455
|
+
childKey !== "typeAnnotation" &&
|
|
1456
|
+
childKey !== "returnType"
|
|
1457
|
+
)
|
|
1458
|
+
visit(child, node, childKey);
|
|
1459
|
+
}
|
|
1460
|
+
};
|
|
1461
|
+
visit(value);
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
public emitBindingDeclaration(
|
|
1465
|
+
decl: BindingDeclaration,
|
|
1466
|
+
isExport = false,
|
|
1467
|
+
): string {
|
|
1468
|
+
return emitBindingDeclaration(this, decl, isExport);
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
public emitBindingTarget(target: BindingTarget | Pattern): string {
|
|
1472
|
+
if (target.kind === "Identifier" || target.kind === "UpperIdentifier") {
|
|
1473
|
+
return target.name;
|
|
1474
|
+
}
|
|
1475
|
+
if (target.kind === "VariablePattern") {
|
|
1476
|
+
return target.name.name;
|
|
1477
|
+
}
|
|
1478
|
+
if (target.kind === "WildcardPattern" || target.kind === "LiteralPattern") {
|
|
1479
|
+
return "";
|
|
1480
|
+
}
|
|
1481
|
+
if (target.kind === "RestPattern") {
|
|
1482
|
+
const arg = this.emitBindingTarget(target.argument);
|
|
1483
|
+
return arg ? `...${arg}` : "";
|
|
1484
|
+
}
|
|
1485
|
+
if (target.kind === "RecordPattern") {
|
|
1486
|
+
const fields = target.fields.map((f) => {
|
|
1487
|
+
if (f.isRest) {
|
|
1488
|
+
return `...${f.key.name}`;
|
|
1489
|
+
}
|
|
1490
|
+
if (
|
|
1491
|
+
!f.pattern ||
|
|
1492
|
+
(f.pattern.kind === "VariablePattern" &&
|
|
1493
|
+
f.pattern.name.name === f.key.name)
|
|
1494
|
+
) {
|
|
1495
|
+
return f.key.name;
|
|
1496
|
+
}
|
|
1497
|
+
return `${f.key.name}: ${this.emitBindingTarget(f.pattern)}`;
|
|
1498
|
+
});
|
|
1499
|
+
return `{ ${fields.join(", ")} }`;
|
|
1500
|
+
}
|
|
1501
|
+
if (target.kind === "ArrayPattern") {
|
|
1502
|
+
const elements = target.elements.map((el) => this.emitBindingTarget(el));
|
|
1503
|
+
while (elements.length > 0 && elements[elements.length - 1] === "") {
|
|
1504
|
+
elements.pop();
|
|
1505
|
+
}
|
|
1506
|
+
return `[${elements.join(", ")}]`;
|
|
1507
|
+
}
|
|
1508
|
+
return "_";
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
public collectBindingNames(target: BindingTarget | Pattern): string[] {
|
|
1512
|
+
return collectBindingNames(target);
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
// --- Expressions ---
|
|
1516
|
+
|
|
1517
|
+
public emitExpression(expr: Expression): string {
|
|
1518
|
+
switch (expr.kind) {
|
|
1519
|
+
case "NumberLiteral":
|
|
1520
|
+
return expr.raw;
|
|
1521
|
+
case "StringLiteral":
|
|
1522
|
+
return this.emitStringLiteral(expr);
|
|
1523
|
+
case "TemplateLiteral":
|
|
1524
|
+
return this.emitTemplateLiteral(expr);
|
|
1525
|
+
case "TaggedTemplateExpression":
|
|
1526
|
+
return this.emitTaggedTemplateExpression(expr);
|
|
1527
|
+
case "BooleanLiteral":
|
|
1528
|
+
return expr.value ? "true" : "false";
|
|
1529
|
+
case "Identifier":
|
|
1530
|
+
case "UpperIdentifier":
|
|
1531
|
+
if (this.recordContracts.has(expr.name))
|
|
1532
|
+
throw new FunScriptError(
|
|
1533
|
+
`${expr.name} is a compile-time record contract, not a runtime schema. Validate data separately and use an explicit checked callback boundary for foreign functions.`,
|
|
1534
|
+
expr.span,
|
|
1535
|
+
);
|
|
1536
|
+
if (expr.name === "Void") return "undefined";
|
|
1537
|
+
if (expr.name === "dbg" && !this.isUserBinding("dbg")) {
|
|
1538
|
+
return `((__v: any) => { console.log(__v); return __v; })`;
|
|
1539
|
+
}
|
|
1540
|
+
if (
|
|
1541
|
+
EFFECT_BUILTIN_MODULES.has(expr.name) &&
|
|
1542
|
+
!NATIVE_CONSTRUCTOR_NAMES.has(expr.name) &&
|
|
1543
|
+
!this.isUserBinding(expr.name)
|
|
1544
|
+
) {
|
|
1545
|
+
this.effectImports.add(expr.name);
|
|
1546
|
+
}
|
|
1547
|
+
const fields = this.recordFields(expr.name);
|
|
1548
|
+
if (fields) {
|
|
1549
|
+
const defaults = [...fields.entries()].filter(
|
|
1550
|
+
([_, info]) => info.defaultBinding,
|
|
1551
|
+
);
|
|
1552
|
+
if (defaults.length > 0) {
|
|
1553
|
+
return `({ ...${expr.name}, ${defaults.map(([key, info]) => `${key}: ${info.defaultBinding}`).join(", ")} })`;
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
return expr.name;
|
|
1557
|
+
case "Placeholder":
|
|
1558
|
+
return this.emitCtx.currentPlaceholderParam ?? "__placeholder";
|
|
1559
|
+
case "TypedHole":
|
|
1560
|
+
return `(() => { throw new Error("Typed hole reached: ${expr.label || "???"}"); })()`;
|
|
1561
|
+
case "PipelineExpression":
|
|
1562
|
+
return this.emitPipelineExpression(expr);
|
|
1563
|
+
case "BinaryExpression":
|
|
1564
|
+
return this.emitBinaryExpression(expr);
|
|
1565
|
+
case "UnaryExpression":
|
|
1566
|
+
return this.emitUnaryExpression(expr);
|
|
1567
|
+
case "CallExpression":
|
|
1568
|
+
return this.emitCallExpression(expr);
|
|
1569
|
+
case "MemberExpression": {
|
|
1570
|
+
const prop = expr.property.name;
|
|
1571
|
+
if (
|
|
1572
|
+
prop === "decodeJson" ||
|
|
1573
|
+
prop === "decodeJsonSync" ||
|
|
1574
|
+
prop === "encodeJson" ||
|
|
1575
|
+
prop === "encodeJsonSync"
|
|
1576
|
+
) {
|
|
1577
|
+
this.effectImports.add("Schema");
|
|
1578
|
+
const schema = this.emitExpression(expr.object);
|
|
1579
|
+
let combinator = "Schema.decodeUnknownEffect";
|
|
1580
|
+
if (prop === "decodeJsonSync")
|
|
1581
|
+
combinator = "Schema.decodeUnknownSync";
|
|
1582
|
+
else if (prop === "encodeJson") combinator = "Schema.encodeEffect";
|
|
1583
|
+
else if (prop === "encodeJsonSync") combinator = "Schema.encodeSync";
|
|
1584
|
+
return `${combinator}(Schema.fromJsonString(${schema}))`;
|
|
1585
|
+
}
|
|
1586
|
+
const raw = `${this.emitExpression(expr.object)}.${this.emitMemberProperty(expr)}`;
|
|
1587
|
+
const field = this.recordField(expr);
|
|
1588
|
+
if (field?.defaultBinding) return field.defaultBinding;
|
|
1589
|
+
if (field?.optional) {
|
|
1590
|
+
this.effectImports.add("Option");
|
|
1591
|
+
return `Option.fromNullishOr(${raw})`;
|
|
1592
|
+
}
|
|
1593
|
+
return raw;
|
|
1594
|
+
}
|
|
1595
|
+
case "IndexExpression": {
|
|
1596
|
+
const raw = `${this.emitExpression(expr.object)}[${this.emitExpression(expr.index)}]`;
|
|
1597
|
+
const field = this.recordField(expr);
|
|
1598
|
+
if (field?.defaultBinding) return field.defaultBinding;
|
|
1599
|
+
if (field?.optional) {
|
|
1600
|
+
this.effectImports.add("Option");
|
|
1601
|
+
return `Option.fromNullishOr(${raw})`;
|
|
1602
|
+
}
|
|
1603
|
+
if (expr.index.kind === "StringLiteral") {
|
|
1604
|
+
return raw;
|
|
1605
|
+
}
|
|
1606
|
+
this.needsFromNullishOr = true;
|
|
1607
|
+
this.effectImports.add("Option");
|
|
1608
|
+
return `fromNullishOr(${raw})`;
|
|
1609
|
+
}
|
|
1610
|
+
case "RecordExpression":
|
|
1611
|
+
return this.emitRecordExpression(expr);
|
|
1612
|
+
case "ArrayExpression":
|
|
1613
|
+
return `[${expr.elements.map((e) => this.emitExpression(e)).join(", ")}]`;
|
|
1614
|
+
case "FunctionExpression":
|
|
1615
|
+
return this.emitFunctionExpression(expr);
|
|
1616
|
+
case "MutBlockExpression":
|
|
1617
|
+
this.needsMutative = true;
|
|
1618
|
+
return `create(${this.emitMutativeRecipe(expr)})`;
|
|
1619
|
+
case "MatchExpression":
|
|
1620
|
+
return this.emitMatchExpression(expr);
|
|
1621
|
+
case "EffectBlockExpression":
|
|
1622
|
+
return this.emitEffectBlockExpression(expr);
|
|
1623
|
+
case "ParBlockExpression":
|
|
1624
|
+
return this.emitParBlockExpression(expr);
|
|
1625
|
+
case "JSXElement":
|
|
1626
|
+
return this.emitJsxElement(expr);
|
|
1627
|
+
case "JSXSelfClosingElement":
|
|
1628
|
+
return this.emitJsxSelfClosingElement(expr);
|
|
1629
|
+
case "JsExpression":
|
|
1630
|
+
return this.emitJsExpression(expr);
|
|
1631
|
+
case "TsEscapeExpression":
|
|
1632
|
+
return expr.code;
|
|
1633
|
+
case "SpreadElement":
|
|
1634
|
+
return `...${this.emitExpression(expr.argument)}`;
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
public emitJsExpression(expr: JsExpression): string {
|
|
1639
|
+
this.needsJsInterop = true;
|
|
1640
|
+
// compiler.ts replaces this provisional flag with the nullability inferred
|
|
1641
|
+
// from the native TypeScript return type.
|
|
1642
|
+
const inner = this.emitInTryContext(expr.expression);
|
|
1643
|
+
if (
|
|
1644
|
+
expr.expression.kind === "FunctionExpression" &&
|
|
1645
|
+
expr.expression.params.length === 0
|
|
1646
|
+
) {
|
|
1647
|
+
return `__funscriptJs(${inner}, false)`;
|
|
1648
|
+
}
|
|
1649
|
+
const wrapped =
|
|
1650
|
+
expr.expression.kind === "RecordExpression" ? `(${inner})` : inner;
|
|
1651
|
+
return `__funscriptJs(() => ${wrapped}, false)`;
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1654
|
+
public emitMemberProperty(expr: MemberExpression): string {
|
|
1655
|
+
if (
|
|
1656
|
+
expr.object.kind === "UpperIdentifier" &&
|
|
1657
|
+
expr.object.name === "Effect"
|
|
1658
|
+
) {
|
|
1659
|
+
const replacement =
|
|
1660
|
+
expr.property.name === "catchAll"
|
|
1661
|
+
? "catch"
|
|
1662
|
+
: expr.property.name === "fork"
|
|
1663
|
+
? "forkChild"
|
|
1664
|
+
: undefined;
|
|
1665
|
+
if (replacement) {
|
|
1666
|
+
throw new FunScriptError(
|
|
1667
|
+
`Effect.${expr.property.name} was removed; use Effect.${replacement} instead.`,
|
|
1668
|
+
expr.span,
|
|
1669
|
+
);
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
return expr.property.name;
|
|
1673
|
+
}
|
|
1674
|
+
|
|
1675
|
+
public emitMutativeRecipe(expr: MutBlockExpression): string {
|
|
1676
|
+
return emitMutativeRecipe(this, expr);
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
public emitStringLiteral(lit: StringLiteral): string {
|
|
1680
|
+
if (lit.parts.length === 1 && lit.parts[0].kind === "StringRawPart") {
|
|
1681
|
+
return JSON.stringify(lit.parts[0].value);
|
|
1682
|
+
}
|
|
1683
|
+
const template = lit.parts
|
|
1684
|
+
.map((p) => {
|
|
1685
|
+
if (p.kind === "StringRawPart")
|
|
1686
|
+
return p.value.replace(/`/g, "\\`").replace(/\${/g, "\\${");
|
|
1687
|
+
return `\${${this.emitExpression(p.expression)}}`;
|
|
1688
|
+
})
|
|
1689
|
+
.join("");
|
|
1690
|
+
return `\`${template}\``;
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
private emitTemplateLiteral(lit: TemplateLiteral): string {
|
|
1694
|
+
const template = lit.parts
|
|
1695
|
+
.map((p) => {
|
|
1696
|
+
if (p.kind === "StringRawPart")
|
|
1697
|
+
return p.value.replace(/`/g, "\\`").replace(/\${/g, "\\${");
|
|
1698
|
+
return `\${${this.emitExpression(p.expression)}}`;
|
|
1699
|
+
})
|
|
1700
|
+
.join("");
|
|
1701
|
+
return `\`${template}\``;
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
private emitTaggedTemplateExpression(expr: TaggedTemplateExpression): string {
|
|
1705
|
+
const tag = this.emitExpression(expr.tag);
|
|
1706
|
+
let typeArgs = "";
|
|
1707
|
+
if (expr.typeArguments && expr.typeArguments.length > 0) {
|
|
1708
|
+
typeArgs =
|
|
1709
|
+
"<" +
|
|
1710
|
+
expr.typeArguments.map((t) => this.emitTypeNode(t)).join(", ") +
|
|
1711
|
+
">";
|
|
1712
|
+
}
|
|
1713
|
+
return `${tag}${typeArgs}${this.emitTemplateLiteral(expr.template)}`;
|
|
1714
|
+
}
|
|
1715
|
+
|
|
1716
|
+
private emitCallExpression(expr: CallExpression): string {
|
|
1717
|
+
if (
|
|
1718
|
+
!this.isUserBinding("dbg") &&
|
|
1719
|
+
expr.callee.kind === "Identifier" &&
|
|
1720
|
+
expr.callee.name === "dbg"
|
|
1721
|
+
) {
|
|
1722
|
+
if (expr.args.length === 1) {
|
|
1723
|
+
const val = this.emitExpression(expr.args[0]);
|
|
1724
|
+
return `((__v) => { console.log(__v); return __v; })(${val})`;
|
|
1725
|
+
}
|
|
1726
|
+
if (expr.args.length >= 2) {
|
|
1727
|
+
const label = this.emitExpression(expr.args[0]);
|
|
1728
|
+
const val = this.emitExpression(expr.args[1]);
|
|
1729
|
+
return `((__v) => { console.log(${label}, __v); return __v; })(${val})`;
|
|
1730
|
+
}
|
|
1731
|
+
return `((__v: any) => { console.log(__v); return __v; })`;
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1734
|
+
if (
|
|
1735
|
+
expr.callee.kind === "Identifier" &&
|
|
1736
|
+
!this.isUserBinding(expr.callee.name)
|
|
1737
|
+
) {
|
|
1738
|
+
const name = expr.callee.name;
|
|
1739
|
+
if (
|
|
1740
|
+
name === "decodeJson" ||
|
|
1741
|
+
name === "decodeJsonSync" ||
|
|
1742
|
+
name === "encodeJson" ||
|
|
1743
|
+
name === "encodeJsonSync"
|
|
1744
|
+
) {
|
|
1745
|
+
this.effectImports.add("Schema");
|
|
1746
|
+
const schema =
|
|
1747
|
+
expr.args.length > 0
|
|
1748
|
+
? this.emitExpression(expr.args[0])
|
|
1749
|
+
: "Schema.Unknown";
|
|
1750
|
+
let combinator = "Schema.decodeUnknownEffect";
|
|
1751
|
+
if (name === "decodeJsonSync") combinator = "Schema.decodeUnknownSync";
|
|
1752
|
+
else if (name === "encodeJson") combinator = "Schema.encodeEffect";
|
|
1753
|
+
else if (name === "encodeJsonSync") combinator = "Schema.encodeSync";
|
|
1754
|
+
|
|
1755
|
+
const codec = `${combinator}(Schema.fromJsonString(${schema}))`;
|
|
1756
|
+
if (expr.args.length >= 2) {
|
|
1757
|
+
const target = this.emitExpression(expr.args[1]);
|
|
1758
|
+
return `${codec}(${target})`;
|
|
1759
|
+
}
|
|
1760
|
+
return codec;
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1763
|
+
|
|
1764
|
+
if (
|
|
1765
|
+
expr.callee.kind === "MemberExpression" &&
|
|
1766
|
+
(expr.callee.property.name === "decodeJson" ||
|
|
1767
|
+
expr.callee.property.name === "decodeJsonSync" ||
|
|
1768
|
+
expr.callee.property.name === "encodeJson" ||
|
|
1769
|
+
expr.callee.property.name === "encodeJsonSync")
|
|
1770
|
+
) {
|
|
1771
|
+
this.effectImports.add("Schema");
|
|
1772
|
+
const schema = this.emitExpression(expr.callee.object);
|
|
1773
|
+
const prop = expr.callee.property.name;
|
|
1774
|
+
let combinator = "Schema.decodeUnknownEffect";
|
|
1775
|
+
if (prop === "decodeJsonSync") combinator = "Schema.decodeUnknownSync";
|
|
1776
|
+
else if (prop === "encodeJson") combinator = "Schema.encodeEffect";
|
|
1777
|
+
else if (prop === "encodeJsonSync") combinator = "Schema.encodeSync";
|
|
1778
|
+
|
|
1779
|
+
const codec = `${combinator}(Schema.fromJsonString(${schema}))`;
|
|
1780
|
+
if (expr.args.length >= 1) {
|
|
1781
|
+
const target = this.emitExpression(expr.args[0]);
|
|
1782
|
+
return `${codec}(${target})`;
|
|
1783
|
+
}
|
|
1784
|
+
return codec;
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
if (
|
|
1788
|
+
expr.callee.kind === "MemberExpression" &&
|
|
1789
|
+
(expr.callee.object.kind === "Identifier" ||
|
|
1790
|
+
expr.callee.object.kind === "UpperIdentifier") &&
|
|
1791
|
+
expr.callee.object.name === "Option" &&
|
|
1792
|
+
expr.callee.property.name === "fromNullishOrElse"
|
|
1793
|
+
) {
|
|
1794
|
+
this.needsFromNullishOrElse = true;
|
|
1795
|
+
this.effectImports.add("Option");
|
|
1796
|
+
return `fromNullishOrElse(${expr.args.map((a) => this.emitExpression(a)).join(", ")})`;
|
|
1797
|
+
}
|
|
1798
|
+
|
|
1799
|
+
if (
|
|
1800
|
+
expr.callee.kind === "MemberExpression" &&
|
|
1801
|
+
(expr.callee.object.kind === "Identifier" ||
|
|
1802
|
+
expr.callee.object.kind === "UpperIdentifier") &&
|
|
1803
|
+
expr.callee.object.name === "Option" &&
|
|
1804
|
+
expr.callee.property.name === "fromNullishOr"
|
|
1805
|
+
) {
|
|
1806
|
+
this.needsFromNullishOr = true;
|
|
1807
|
+
this.effectImports.add("Option");
|
|
1808
|
+
return `fromNullishOr(${expr.args.map((a) => this.emitExpression(a)).join(", ")})`;
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
if (
|
|
1812
|
+
expr.callee.kind === "Identifier" &&
|
|
1813
|
+
expr.callee.name === "fromNullishOr" &&
|
|
1814
|
+
!this.isUserBinding("fromNullishOr")
|
|
1815
|
+
) {
|
|
1816
|
+
this.needsFromNullishOr = true;
|
|
1817
|
+
this.effectImports.add("Option");
|
|
1818
|
+
return `fromNullishOr(${expr.args.map((a) => this.emitExpression(a)).join(", ")})`;
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
if (
|
|
1822
|
+
expr.callee.kind === "Identifier" &&
|
|
1823
|
+
expr.callee.name === "fromNullishOrElse" &&
|
|
1824
|
+
!this.isUserBinding("fromNullishOrElse")
|
|
1825
|
+
) {
|
|
1826
|
+
this.needsFromNullishOrElse = true;
|
|
1827
|
+
this.effectImports.add("Option");
|
|
1828
|
+
return `fromNullishOrElse(${expr.args.map((a) => this.emitExpression(a)).join(", ")})`;
|
|
1829
|
+
}
|
|
1830
|
+
|
|
1831
|
+
if (
|
|
1832
|
+
expr.callee.kind === "Identifier" &&
|
|
1833
|
+
(expr.callee.name === "parseInt" || expr.callee.name === "parseFloat")
|
|
1834
|
+
) {
|
|
1835
|
+
throw new FunScriptError(
|
|
1836
|
+
`'${expr.callee.name}()' is banned by the Effect style guide because it produces NaN on invalid input. Use 'Number.parse(s)' instead for total parsing returning Option<number>.`,
|
|
1837
|
+
expr.callee.span,
|
|
1838
|
+
);
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
if (
|
|
1842
|
+
(expr.callee.kind === "UpperIdentifier" ||
|
|
1843
|
+
expr.callee.kind === "Identifier") &&
|
|
1844
|
+
expr.callee.name === "URL" &&
|
|
1845
|
+
!this.emitCtx.inTryContext
|
|
1846
|
+
) {
|
|
1847
|
+
throw new FunScriptError(
|
|
1848
|
+
`'URL(...)' is unsafe because URL constructors throw on malformed input. Guard it with 'js URL(...)', 'Result.try(() => URL(...))', or 'Option.try(() => URL(...))'.`,
|
|
1849
|
+
expr.callee.span,
|
|
1850
|
+
);
|
|
1851
|
+
}
|
|
1852
|
+
|
|
1853
|
+
if (
|
|
1854
|
+
expr.callee.kind === "MemberExpression" &&
|
|
1855
|
+
(expr.callee.object.kind === "Identifier" ||
|
|
1856
|
+
expr.callee.object.kind === "UpperIdentifier") &&
|
|
1857
|
+
expr.callee.object.name === "Option" &&
|
|
1858
|
+
expr.callee.property.name === "try"
|
|
1859
|
+
) {
|
|
1860
|
+
this.needsOptionTry = true;
|
|
1861
|
+
this.effectImports.add("Option");
|
|
1862
|
+
const emittedArgs = expr.args.map((a) => this.emitTryArgument(a));
|
|
1863
|
+
return `optionTry(${emittedArgs.join(", ")})`;
|
|
1864
|
+
}
|
|
1865
|
+
|
|
1866
|
+
if (
|
|
1867
|
+
expr.callee.kind === "MemberExpression" &&
|
|
1868
|
+
(expr.callee.object.kind === "Identifier" ||
|
|
1869
|
+
expr.callee.object.kind === "UpperIdentifier") &&
|
|
1870
|
+
expr.callee.object.name === "Result" &&
|
|
1871
|
+
expr.callee.property.name === "try"
|
|
1872
|
+
) {
|
|
1873
|
+
this.effectImports.add("Result");
|
|
1874
|
+
const emittedArgs = expr.args.map((a) => this.emitTryArgument(a));
|
|
1875
|
+
return `Result.try(${emittedArgs.join(", ")})`;
|
|
1876
|
+
}
|
|
1877
|
+
|
|
1878
|
+
if (expr.callee.kind === "MemberExpression") {
|
|
1879
|
+
const prop = expr.callee.property.name;
|
|
1880
|
+
const root = getRootCalleeName(expr.callee.object);
|
|
1881
|
+
const objName =
|
|
1882
|
+
expr.callee.object.kind === "Identifier" ||
|
|
1883
|
+
expr.callee.object.kind === "UpperIdentifier"
|
|
1884
|
+
? expr.callee.object.name
|
|
1885
|
+
: null;
|
|
1886
|
+
|
|
1887
|
+
if (
|
|
1888
|
+
(objName === "Number" || objName === "globalThis") &&
|
|
1889
|
+
(prop === "parseInt" || prop === "parseFloat")
|
|
1890
|
+
) {
|
|
1891
|
+
throw new FunScriptError(
|
|
1892
|
+
`'${objName}.${prop}()' is banned by the Effect style guide because it produces NaN on invalid input. Use 'Number.parse(s)' instead for total parsing returning Option<number>.`,
|
|
1893
|
+
expr.callee.property.span,
|
|
1894
|
+
);
|
|
1895
|
+
}
|
|
1896
|
+
|
|
1897
|
+
if (
|
|
1898
|
+
(objName === "JSON" || objName === "globalThis") &&
|
|
1899
|
+
prop === "parse" &&
|
|
1900
|
+
!this.emitCtx.inTryContext
|
|
1901
|
+
) {
|
|
1902
|
+
throw new FunScriptError(
|
|
1903
|
+
`'JSON.parse()' is unsafe because it throws on malformed input. Guard it with 'js JSON.parse(...)', 'Result.try(() => JSON.parse(...))', or use 'Schema.decodeUnknown'.`,
|
|
1904
|
+
expr.callee.property.span,
|
|
1905
|
+
);
|
|
1906
|
+
}
|
|
1907
|
+
|
|
1908
|
+
if (
|
|
1909
|
+
(objName === "Math" || objName === "globalThis") &&
|
|
1910
|
+
prop === "random"
|
|
1911
|
+
) {
|
|
1912
|
+
throw new FunScriptError(
|
|
1913
|
+
`'Math.random()' is banned by the Effect style guide because it is unmanaged and non-deterministic. Use 'Random.next' or 'Random.nextIntBetween' instead.`,
|
|
1914
|
+
expr.callee.property.span,
|
|
1915
|
+
);
|
|
1916
|
+
}
|
|
1917
|
+
|
|
1918
|
+
if (
|
|
1919
|
+
(expr.callee.object.kind === "Identifier" ||
|
|
1920
|
+
expr.callee.object.kind === "UpperIdentifier") &&
|
|
1921
|
+
expr.callee.object.name === "Object" &&
|
|
1922
|
+
BANNED_OBJECT_STATIC_METHODS.has(prop)
|
|
1923
|
+
) {
|
|
1924
|
+
const replacement = BANNED_OBJECT_STATIC_METHODS.get(prop)!;
|
|
1925
|
+
throw new FunScriptError(
|
|
1926
|
+
`'Object.${prop}()' is banned by the Effect style guide. Use ${replacement} instead.`,
|
|
1927
|
+
expr.callee.property.span,
|
|
1928
|
+
);
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
const isAllowed =
|
|
1932
|
+
root !== null &&
|
|
1933
|
+
(root[0] === root[0].toUpperCase() ||
|
|
1934
|
+
EFFECT_MODULE_NAMES.has(root) ||
|
|
1935
|
+
EFFECT_BUILTIN_MODULES.has(root));
|
|
1936
|
+
|
|
1937
|
+
if (
|
|
1938
|
+
!isAllowed &&
|
|
1939
|
+
!this.activeDraftParams.has(root ?? "") &&
|
|
1940
|
+
BANNED_INSTANCE_METHODS.has(prop)
|
|
1941
|
+
) {
|
|
1942
|
+
const info = BANNED_INSTANCE_METHODS.get(prop)!;
|
|
1943
|
+
if (
|
|
1944
|
+
this.isTargetNativeCategory(expr.callee.object, info.category, prop)
|
|
1945
|
+
) {
|
|
1946
|
+
if (["push", "pop", "shift", "unshift", "splice"].includes(prop)) {
|
|
1947
|
+
throw new FunScriptError(
|
|
1948
|
+
`In-place mutation '.${prop}()' is quarantined in FunkScript. Use '${info.replacement}(...)' or pipeline '${info.pipeline}' instead, or isolate mutable edits inside 'mut (draft) => { ... }'.`,
|
|
1949
|
+
expr.callee.property.span,
|
|
1950
|
+
);
|
|
1951
|
+
} else {
|
|
1952
|
+
throw new FunScriptError(
|
|
1953
|
+
`Native ${info.category} method '.${prop}()' is banned by the Effect style guide. Use '${info.replacement}(...)' or pipeline '${info.pipeline}' instead.`,
|
|
1954
|
+
expr.callee.property.span,
|
|
1955
|
+
);
|
|
1956
|
+
}
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
if (
|
|
1961
|
+
(expr.callee.object.kind === "Identifier" ||
|
|
1962
|
+
expr.callee.object.kind === "UpperIdentifier") &&
|
|
1963
|
+
EFFECT_BUILTIN_MODULES.has(expr.callee.object.name)
|
|
1964
|
+
) {
|
|
1965
|
+
this.effectImports.add(expr.callee.object.name);
|
|
1966
|
+
}
|
|
1967
|
+
}
|
|
1968
|
+
|
|
1969
|
+
if (
|
|
1970
|
+
(expr.callee.kind === "UpperIdentifier" ||
|
|
1971
|
+
expr.callee.kind === "Identifier") &&
|
|
1972
|
+
(expr.callee.name === "Map" || expr.callee.name === "WeakMap") &&
|
|
1973
|
+
expr.args.length === 1 &&
|
|
1974
|
+
!this.bindings.has(expr.callee.name) &&
|
|
1975
|
+
!this.declaredTypeNames.has(expr.callee.name)
|
|
1976
|
+
) {
|
|
1977
|
+
return `new ${expr.callee.name}(${this.emitExpression(expr.args[0])} as any)`;
|
|
1978
|
+
}
|
|
1979
|
+
|
|
1980
|
+
if (this.options.constructorCalls?.has(expr.callee.span.end.offset)) {
|
|
1981
|
+
return `new ${this.emitExpression(expr.callee)}(${expr.args.map((a) => this.emitExpression(a)).join(", ")})`;
|
|
1982
|
+
}
|
|
1983
|
+
|
|
1984
|
+
if (
|
|
1985
|
+
(expr.callee.kind === "UpperIdentifier" ||
|
|
1986
|
+
expr.callee.kind === "Identifier") &&
|
|
1987
|
+
AUTO_NEW_CONSTRUCTORS.has(expr.callee.name) &&
|
|
1988
|
+
!this.bindings.has(expr.callee.name) &&
|
|
1989
|
+
!this.declaredTypeNames.has(expr.callee.name)
|
|
1990
|
+
) {
|
|
1991
|
+
return `new ${expr.callee.name}(${expr.args.map((a) => this.emitExpression(a)).join(", ")})`;
|
|
1992
|
+
}
|
|
1993
|
+
|
|
1994
|
+
if (
|
|
1995
|
+
expr.callee.kind === "MemberExpression" &&
|
|
1996
|
+
AUTO_NEW_CONSTRUCTORS.has(expr.callee.property.name) &&
|
|
1997
|
+
expr.callee.object.kind === "Identifier" &&
|
|
1998
|
+
(expr.callee.object.name === "globalThis" ||
|
|
1999
|
+
expr.callee.object.name === "window")
|
|
2000
|
+
) {
|
|
2001
|
+
return `new ${this.emitExpression(expr.callee)}(${expr.args.map((a) => this.emitExpression(a)).join(", ")})`;
|
|
2002
|
+
}
|
|
2003
|
+
|
|
2004
|
+
if (
|
|
2005
|
+
(expr.callee.kind === "UpperIdentifier" ||
|
|
2006
|
+
expr.callee.kind === "Identifier") &&
|
|
2007
|
+
expr.callee.name === "Brand"
|
|
2008
|
+
) {
|
|
2009
|
+
this.effectImports.add("Schema");
|
|
2010
|
+
return `Schema.brand(${expr.args.map((a) => this.emitExpression(a)).join(", ")})`;
|
|
2011
|
+
}
|
|
2012
|
+
|
|
2013
|
+
const isPipelineStageCall = this.emitCtx.inPipelineStage;
|
|
2014
|
+
return this.withEmitContext({ inPipelineStage: false }, () => {
|
|
2015
|
+
if (
|
|
2016
|
+
this.options.markPipelineSources &&
|
|
2017
|
+
!isPipelineStageCall &&
|
|
2018
|
+
expr.callee.kind === "Identifier" &&
|
|
2019
|
+
expr.args.length >= 1
|
|
2020
|
+
) {
|
|
2021
|
+
const fnName = expr.callee.name;
|
|
2022
|
+
if (
|
|
2023
|
+
fnName in COMPARISON_METHOD_MAP ||
|
|
2024
|
+
(!this.bindings.has(fnName) && !this.userEffectBindings.has(fnName))
|
|
2025
|
+
) {
|
|
2026
|
+
const firstArg = this.emitExpression(expr.args[0]);
|
|
2027
|
+
const restArgs = expr.args
|
|
2028
|
+
.slice(1)
|
|
2029
|
+
.map((a) => this.emitExpression(a));
|
|
2030
|
+
const markedFirstArg = `__funscriptDirectCall(${expr.span.start.offset}, ${firstArg})`;
|
|
2031
|
+
return `${fnName}(${[markedFirstArg, ...restArgs].join(", ")})`;
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
|
|
2035
|
+
if (this.options.directCallResolutions?.has(expr.span.start.offset)) {
|
|
2036
|
+
const res = this.options.directCallResolutions.get(
|
|
2037
|
+
expr.span.start.offset,
|
|
2038
|
+
)!;
|
|
2039
|
+
if (isEffectModule(res.module)) {
|
|
2040
|
+
this.effectImports.add(res.module);
|
|
2041
|
+
}
|
|
2042
|
+
const callee = `${res.module}.${res.method ?? (expr.callee as Identifier).name}`;
|
|
2043
|
+
return `${callee}(${expr.args.map((a) => this.emitExpression(a)).join(", ")})`;
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2046
|
+
if (
|
|
2047
|
+
expr.callee.kind === "Identifier" &&
|
|
2048
|
+
!this.bindings.has(expr.callee.name) &&
|
|
2049
|
+
ES_TOOLKIT_METHODS.has(expr.callee.name)
|
|
2050
|
+
) {
|
|
2051
|
+
this.esToolkitImports.add(expr.callee.name);
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
const emittedCallee = this.emitExpression(expr.callee);
|
|
2055
|
+
const calleeCode =
|
|
2056
|
+
this.options.markCallCallees && !isPipelineStageCall
|
|
2057
|
+
? `__funscriptCallCallee(${expr.callee.span.end.offset}, ${emittedCallee})`
|
|
2058
|
+
: emittedCallee;
|
|
2059
|
+
return `${calleeCode}(${expr.args
|
|
2060
|
+
.map((a) => {
|
|
2061
|
+
const emitted = this.emitExpression(a);
|
|
2062
|
+
if (this.options.forwardNullArguments?.has(a.span.start.offset)) {
|
|
2063
|
+
this.effectImports.add("Option");
|
|
2064
|
+
return `Option.getOrNull(${emitted})`;
|
|
2065
|
+
}
|
|
2066
|
+
if (this.options.forwardOptionalArguments?.has(a.span.start.offset)) {
|
|
2067
|
+
this.effectImports.add("Option");
|
|
2068
|
+
return `Option.getOrUndefined(${emitted})`;
|
|
2069
|
+
}
|
|
2070
|
+
return this.options.markCallArguments
|
|
2071
|
+
? `__funscriptCallArgument(${a.span.start.offset}, ${emitted})`
|
|
2072
|
+
: emitted;
|
|
2073
|
+
})
|
|
2074
|
+
.join(", ")})`;
|
|
2075
|
+
});
|
|
2076
|
+
}
|
|
2077
|
+
|
|
2078
|
+
private emitPipelineExpression(expr: PipelineExpression): string {
|
|
2079
|
+
this.effectImports.add("pipe");
|
|
2080
|
+
|
|
2081
|
+
const resolver = new PipelineResolver({
|
|
2082
|
+
bindings: this.bindings,
|
|
2083
|
+
pipelineCompanions: this.options.pipelineCompanions,
|
|
2084
|
+
allowAmbiguousPipeline: this.options.allowAmbiguousPipeline,
|
|
2085
|
+
isUserBinding: (name) => this.isUserBinding(name),
|
|
2086
|
+
effectImports: this.effectImports,
|
|
2087
|
+
esToolkitImports: this.esToolkitImports,
|
|
2088
|
+
requireDecodeUnknown: () => {
|
|
2089
|
+
this.needsDecodeUnknown = true;
|
|
2090
|
+
},
|
|
2091
|
+
});
|
|
2092
|
+
|
|
2093
|
+
// Flatten left-nested pipelines: a |> b |> c
|
|
2094
|
+
const stages: Expression[] = [];
|
|
2095
|
+
let curr: Expression = expr;
|
|
2096
|
+
let deferredSource = false;
|
|
2097
|
+
|
|
2098
|
+
while (curr.kind === "PipelineExpression") {
|
|
2099
|
+
stages.unshift(curr.right);
|
|
2100
|
+
if (curr.deferred) {
|
|
2101
|
+
deferredSource = true;
|
|
2102
|
+
curr = curr.left;
|
|
2103
|
+
break;
|
|
2104
|
+
}
|
|
2105
|
+
curr = curr.left;
|
|
2106
|
+
}
|
|
2107
|
+
stages.unshift(curr);
|
|
2108
|
+
|
|
2109
|
+
let preferredCompanion = resolver.inferPipelineCompanion(stages[0]);
|
|
2110
|
+
const emittedStages = stages.map((s, idx) => {
|
|
2111
|
+
if (idx === 0) {
|
|
2112
|
+
return this.withEmitContext({ inPipelineStage: false }, () => {
|
|
2113
|
+
const value = this.emitExpression(s);
|
|
2114
|
+
const emitted = deferredSource ? `() => (${value})` : value;
|
|
2115
|
+
return this.options.markPipelineSources
|
|
2116
|
+
? `__funscriptPipelineSource(${s.span.start.offset}, ${emitted})`
|
|
2117
|
+
: emitted;
|
|
2118
|
+
});
|
|
2119
|
+
}
|
|
2120
|
+
return this.withEmitContext({ inPipelineStage: true }, () => {
|
|
2121
|
+
if (
|
|
2122
|
+
idx === 1 &&
|
|
2123
|
+
deferredSource &&
|
|
2124
|
+
s.kind === "Identifier" &&
|
|
2125
|
+
[
|
|
2126
|
+
"sync",
|
|
2127
|
+
"suspend",
|
|
2128
|
+
"promise",
|
|
2129
|
+
"try",
|
|
2130
|
+
"tryPromise",
|
|
2131
|
+
"failSync",
|
|
2132
|
+
].includes(s.name) &&
|
|
2133
|
+
!this.isUserBinding(s.name)
|
|
2134
|
+
) {
|
|
2135
|
+
this.effectImports.add("Effect");
|
|
2136
|
+
return s.name === "try" || s.name === "tryPromise"
|
|
2137
|
+
? `(__thunk) => Effect.${s.name}(__thunk)`
|
|
2138
|
+
: `Effect.${s.name}`;
|
|
2139
|
+
}
|
|
2140
|
+
if (
|
|
2141
|
+
idx === 1 &&
|
|
2142
|
+
deferredSource &&
|
|
2143
|
+
s.kind === "MemberExpression" &&
|
|
2144
|
+
s.object.kind === "UpperIdentifier" &&
|
|
2145
|
+
s.object.name === "Effect" &&
|
|
2146
|
+
(s.property.name === "try" || s.property.name === "tryPromise")
|
|
2147
|
+
) {
|
|
2148
|
+
this.effectImports.add("Effect");
|
|
2149
|
+
return `(__thunk) => Effect.${s.property.name}(__thunk)`;
|
|
2150
|
+
}
|
|
2151
|
+
if (s.kind === "MatchExpression" && s.isCatch) {
|
|
2152
|
+
return this.emitMatchExpression(s);
|
|
2153
|
+
}
|
|
2154
|
+
if (s.kind === "MutBlockExpression") {
|
|
2155
|
+
this.needsMutative = true;
|
|
2156
|
+
return `(__mutInput) => create(__mutInput, ${this.emitMutativeRecipe(s)})`;
|
|
2157
|
+
}
|
|
2158
|
+
|
|
2159
|
+
if (!this.isUserBinding("fromNullishOr")) {
|
|
2160
|
+
if (s.kind === "Identifier" && s.name === "fromNullishOr") {
|
|
2161
|
+
this.needsFromNullishOr = true;
|
|
2162
|
+
this.effectImports.add("Option");
|
|
2163
|
+
return `fromNullishOr`;
|
|
2164
|
+
}
|
|
2165
|
+
}
|
|
2166
|
+
|
|
2167
|
+
if (!this.isUserBinding("fromNullishOrElse")) {
|
|
2168
|
+
if (
|
|
2169
|
+
s.kind === "CallExpression" &&
|
|
2170
|
+
s.callee.kind === "Identifier" &&
|
|
2171
|
+
s.callee.name === "fromNullishOrElse"
|
|
2172
|
+
) {
|
|
2173
|
+
this.needsFromNullishOrElse = true;
|
|
2174
|
+
this.effectImports.add("Option");
|
|
2175
|
+
return `fromNullishOrElse(${s.args.map((a) => this.emitExpression(a)).join(", ")})`;
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
|
|
2179
|
+
if (!this.isUserBinding("dbg")) {
|
|
2180
|
+
if (s.kind === "Identifier" && s.name === "dbg") {
|
|
2181
|
+
return `((__v) => { console.log(__v); return __v; })`;
|
|
2182
|
+
}
|
|
2183
|
+
if (
|
|
2184
|
+
s.kind === "CallExpression" &&
|
|
2185
|
+
s.callee.kind === "Identifier" &&
|
|
2186
|
+
s.callee.name === "dbg"
|
|
2187
|
+
) {
|
|
2188
|
+
const labelArg =
|
|
2189
|
+
s.args.length > 0 ? this.emitExpression(s.args[0]) : undefined;
|
|
2190
|
+
const logCall = labelArg
|
|
2191
|
+
? `console.log(${labelArg}, __v)`
|
|
2192
|
+
: `console.log(__v)`;
|
|
2193
|
+
return `((__v) => { ${logCall}; return __v; })`;
|
|
2194
|
+
}
|
|
2195
|
+
}
|
|
2196
|
+
|
|
2197
|
+
// Check if stage was resolved by type-directed resolution
|
|
2198
|
+
if (this.options.stageResolutions?.has(s.span.start.offset)) {
|
|
2199
|
+
const res = this.options.stageResolutions.get(s.span.start.offset)!;
|
|
2200
|
+
if (isEffectModule(res.module)) {
|
|
2201
|
+
this.effectImports.add(res.module);
|
|
2202
|
+
}
|
|
2203
|
+
if (
|
|
2204
|
+
res.module === "Effect" &&
|
|
2205
|
+
(res.method === "retry" ||
|
|
2206
|
+
(s.kind === "CallExpression" &&
|
|
2207
|
+
(s.callee as Identifier).name === "retry"))
|
|
2208
|
+
) {
|
|
2209
|
+
const resolved = resolver.resolvePipelineStage(s, "Effect");
|
|
2210
|
+
return this.emitExpression(resolved);
|
|
2211
|
+
}
|
|
2212
|
+
if (s.kind === "Identifier" || s.kind === "UpperIdentifier") {
|
|
2213
|
+
return `${res.module}.${res.method ?? s.name}`;
|
|
2214
|
+
}
|
|
2215
|
+
if (s.kind === "CallExpression") {
|
|
2216
|
+
const callee = s.callee as Identifier | UpperIdentifier;
|
|
2217
|
+
if (
|
|
2218
|
+
res.module === "Schema" &&
|
|
2219
|
+
(res.method === "decode" ||
|
|
2220
|
+
(!res.method && callee.name === "decode"))
|
|
2221
|
+
) {
|
|
2222
|
+
this.needsDecodeUnknown = true;
|
|
2223
|
+
}
|
|
2224
|
+
const method =
|
|
2225
|
+
res.module === "Schema" &&
|
|
2226
|
+
(res.method === "decode" ||
|
|
2227
|
+
(!res.method && callee.name === "decode"))
|
|
2228
|
+
? "decodeUnknown"
|
|
2229
|
+
: (res.method ?? callee.name);
|
|
2230
|
+
const calleeName = `${res.module}.${method}`;
|
|
2231
|
+
if (resolver.hasPlaceholder(s)) {
|
|
2232
|
+
const placeholderParam = this.emitCtx.currentPlaceholderParam
|
|
2233
|
+
? `__p${++this.placeholderCounter}`
|
|
2234
|
+
: "__p";
|
|
2235
|
+
return this.withEmitContext(
|
|
2236
|
+
{ currentPlaceholderParam: placeholderParam },
|
|
2237
|
+
() => {
|
|
2238
|
+
const args = s.args
|
|
2239
|
+
.map((a) => this.emitExpression(a))
|
|
2240
|
+
.join(", ");
|
|
2241
|
+
return `(${placeholderParam}) => ${calleeName}(${args})`;
|
|
2242
|
+
},
|
|
2243
|
+
);
|
|
2244
|
+
}
|
|
2245
|
+
const args = s.args.map((a) => this.emitExpression(a)).join(", ");
|
|
2246
|
+
return `${calleeName}(${args})`;
|
|
2247
|
+
}
|
|
2248
|
+
}
|
|
2249
|
+
|
|
2250
|
+
// Auto-namespace resolution for pipeline stage callee
|
|
2251
|
+
resolver.assertPipelineStageIsNotAmbiguous(s, preferredCompanion);
|
|
2252
|
+
const resolvedStage = resolver.resolvePipelineStage(
|
|
2253
|
+
s,
|
|
2254
|
+
preferredCompanion,
|
|
2255
|
+
);
|
|
2256
|
+
|
|
2257
|
+
// `js`, `runPromise`, and `runSync` run an Effect and leave the Effect pipeline domain.
|
|
2258
|
+
const isEffectRunner =
|
|
2259
|
+
(s.kind === "Identifier" &&
|
|
2260
|
+
(s.name === "js" ||
|
|
2261
|
+
s.name === "runPromise" ||
|
|
2262
|
+
s.name === "runSync")) ||
|
|
2263
|
+
(s.kind === "CallExpression" &&
|
|
2264
|
+
s.callee.kind === "Identifier" &&
|
|
2265
|
+
(s.callee.name === "runPromise" || s.callee.name === "runSync") &&
|
|
2266
|
+
s.args.length === 0);
|
|
2267
|
+
if (isEffectRunner) preferredCompanion = undefined;
|
|
2268
|
+
|
|
2269
|
+
// Check if stage has placeholder '_'
|
|
2270
|
+
if (resolver.hasPlaceholder(resolvedStage)) {
|
|
2271
|
+
const placeholderParam = this.emitCtx.currentPlaceholderParam
|
|
2272
|
+
? `__p${++this.placeholderCounter}`
|
|
2273
|
+
: "__p";
|
|
2274
|
+
return this.withEmitContext(
|
|
2275
|
+
{ currentPlaceholderParam: placeholderParam },
|
|
2276
|
+
() => {
|
|
2277
|
+
const body = this.emitExpression(resolvedStage);
|
|
2278
|
+
const formattedBody =
|
|
2279
|
+
resolvedStage.kind === "RecordExpression" ? `(${body})` : body;
|
|
2280
|
+
return `(${placeholderParam}) => ${formattedBody}`;
|
|
2281
|
+
},
|
|
2282
|
+
);
|
|
2283
|
+
}
|
|
2284
|
+
|
|
2285
|
+
if (idx === 1 && s.kind === "MatchExpression" && !s.isCatch) {
|
|
2286
|
+
return this.emitMatchExpression(
|
|
2287
|
+
s,
|
|
2288
|
+
this.getTypeNodeForExpression(stages[0]),
|
|
2289
|
+
stages[0],
|
|
2290
|
+
);
|
|
2291
|
+
}
|
|
2292
|
+
|
|
2293
|
+
return this.emitExpression(resolvedStage);
|
|
2294
|
+
});
|
|
2295
|
+
});
|
|
2296
|
+
|
|
2297
|
+
const checkedStages = this.options.markPipelineSources
|
|
2298
|
+
? emittedStages.map((stage, index) =>
|
|
2299
|
+
index === 0
|
|
2300
|
+
? stage
|
|
2301
|
+
: `(__funscriptPipelineValue) => (${stage})(__funscriptPipelineStageInput(${stages[index].span.start.offset}, __funscriptPipelineValue))`,
|
|
2302
|
+
)
|
|
2303
|
+
: emittedStages;
|
|
2304
|
+
return `pipe(\n ${checkedStages.join(",\n ")}\n)`;
|
|
2305
|
+
}
|
|
2306
|
+
|
|
2307
|
+
public isUserBinding(name: string): boolean {
|
|
2308
|
+
return (
|
|
2309
|
+
this.bindings.has(name) ||
|
|
2310
|
+
this.userImportBindings.has(name) ||
|
|
2311
|
+
this.userEffectBindings.has(name) ||
|
|
2312
|
+
this.userEsToolkitBindings.has(name) ||
|
|
2313
|
+
this.declaredTypeNames.has(name) ||
|
|
2314
|
+
this.parameterScopes.some((scope) => scope.has(name))
|
|
2315
|
+
);
|
|
2316
|
+
}
|
|
2317
|
+
|
|
2318
|
+
private emitBinaryExpression(expr: BinaryExpression): string {
|
|
2319
|
+
const emitOperand = (operand: Expression) => {
|
|
2320
|
+
const emitted = this.emitExpression(operand);
|
|
2321
|
+
return operand.kind === "FunctionExpression" ? `(${emitted})` : emitted;
|
|
2322
|
+
};
|
|
2323
|
+
let left = emitOperand(expr.left);
|
|
2324
|
+
const right = emitOperand(expr.right);
|
|
2325
|
+
// A left-nested addition already has JavaScript's default grouping. Keep
|
|
2326
|
+
// right-nested additions parenthesized: strings and floating point values
|
|
2327
|
+
// make reassociation observable.
|
|
2328
|
+
if (
|
|
2329
|
+
expr.operator === "+" &&
|
|
2330
|
+
expr.left.kind === "BinaryExpression" &&
|
|
2331
|
+
expr.left.operator === "+" &&
|
|
2332
|
+
left.startsWith("(") &&
|
|
2333
|
+
left.endsWith(")")
|
|
2334
|
+
) {
|
|
2335
|
+
left = left.slice(1, -1);
|
|
2336
|
+
}
|
|
2337
|
+
const markBoolean = (operand: Expression, emitted: string) =>
|
|
2338
|
+
this.options.markBooleanOperands
|
|
2339
|
+
? `__funscriptBooleanOperand(${operand.span.start.offset}, ${JSON.stringify(expr.operator)}, ${emitted})`
|
|
2340
|
+
: emitted;
|
|
2341
|
+
switch (expr.operator) {
|
|
2342
|
+
case "and":
|
|
2343
|
+
return `(${markBoolean(expr.left, left)} && ${markBoolean(expr.right, right)})`;
|
|
2344
|
+
case "or":
|
|
2345
|
+
return `(${markBoolean(expr.left, left)} || ${markBoolean(expr.right, right)})`;
|
|
2346
|
+
case "@=":
|
|
2347
|
+
if (this.isPrimitive(expr.left) || this.isPrimitive(expr.right)) {
|
|
2348
|
+
throw new Error(
|
|
2349
|
+
`Reference identity operator '@=' cannot be used with primitive values; use '#=' or '#!' for value equality`,
|
|
2350
|
+
);
|
|
2351
|
+
}
|
|
2352
|
+
this.needsReferenceEquality = true;
|
|
2353
|
+
return `referenceEquals(${left}, ${right})`;
|
|
2354
|
+
case "@!":
|
|
2355
|
+
if (this.isPrimitive(expr.left) || this.isPrimitive(expr.right)) {
|
|
2356
|
+
throw new Error(
|
|
2357
|
+
`Reference identity operator '@!' cannot be used with primitive values; use '#=' or '#!' for value equality`,
|
|
2358
|
+
);
|
|
2359
|
+
}
|
|
2360
|
+
this.needsReferenceEquality = true;
|
|
2361
|
+
return `(!referenceEquals(${left}, ${right}))`;
|
|
2362
|
+
case "#=":
|
|
2363
|
+
if (this.isPrimitive(expr.left) || this.isPrimitive(expr.right)) {
|
|
2364
|
+
return `(${left} === ${right})`;
|
|
2365
|
+
}
|
|
2366
|
+
this.effectImports.add("Equal");
|
|
2367
|
+
return `Equal.equals(${left}, ${right})`;
|
|
2368
|
+
case "#!":
|
|
2369
|
+
if (this.isPrimitive(expr.left) || this.isPrimitive(expr.right)) {
|
|
2370
|
+
return `(${left} !== ${right})`;
|
|
2371
|
+
}
|
|
2372
|
+
this.effectImports.add("Equal");
|
|
2373
|
+
return `(!Equal.equals(${left}, ${right}))`;
|
|
2374
|
+
case "<":
|
|
2375
|
+
case "<=":
|
|
2376
|
+
case ">":
|
|
2377
|
+
case ">=": {
|
|
2378
|
+
if (!this.options.markNumericComparisonOperands) {
|
|
2379
|
+
return `(${left} ${expr.operator} ${right})`;
|
|
2380
|
+
}
|
|
2381
|
+
const mark = (operand: string, offset: number) =>
|
|
2382
|
+
`__funscriptNumericOperand(${offset}, ${JSON.stringify(expr.operator)}, ${operand})`;
|
|
2383
|
+
return `(${mark(left, expr.left.span.start.offset)} ${expr.operator} ${mark(right, expr.right.span.start.offset)})`;
|
|
2384
|
+
}
|
|
2385
|
+
default:
|
|
2386
|
+
return `(${left} ${expr.operator} ${right})`;
|
|
2387
|
+
}
|
|
2388
|
+
}
|
|
2389
|
+
|
|
2390
|
+
private emitUnaryExpression(expr: UnaryExpression): string {
|
|
2391
|
+
const arg = this.emitExpression(expr.argument);
|
|
2392
|
+
if (expr.operator === "not") return `(!${arg})`;
|
|
2393
|
+
return `(-${arg})`;
|
|
2394
|
+
}
|
|
2395
|
+
|
|
2396
|
+
private emitInTryContext(arg: Expression): string {
|
|
2397
|
+
if (arg.kind === "FunctionExpression") {
|
|
2398
|
+
return this.withEmitContext({ pendingTryThunk: true }, () =>
|
|
2399
|
+
this.emitExpression(arg),
|
|
2400
|
+
);
|
|
2401
|
+
}
|
|
2402
|
+
return this.withEmitContext({ inTryContext: true }, () =>
|
|
2403
|
+
this.emitExpression(arg),
|
|
2404
|
+
);
|
|
2405
|
+
}
|
|
2406
|
+
|
|
2407
|
+
private emitTryArgument(arg: Expression): string {
|
|
2408
|
+
if (arg.kind === "RecordExpression") {
|
|
2409
|
+
return this.withEmitContext({ pendingTryRecord: true }, () =>
|
|
2410
|
+
this.emitExpression(arg),
|
|
2411
|
+
);
|
|
2412
|
+
}
|
|
2413
|
+
return this.emitInTryContext(arg);
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2416
|
+
private emitRecordExpression(expr: RecordExpression): string {
|
|
2417
|
+
const isTryRecord = this.emitCtx.pendingTryRecord;
|
|
2418
|
+
return this.withEmitContext({ pendingTryRecord: false }, () => {
|
|
2419
|
+
const hasDotPaths = expr.entries.some((e) => e.kind === "DotPathEntry");
|
|
2420
|
+
if (hasDotPaths) {
|
|
2421
|
+
// Lower to Mutative create
|
|
2422
|
+
this.needsMutative = true;
|
|
2423
|
+
const baseSpread = expr.entries.find((e) => e.kind === "SpreadEntry");
|
|
2424
|
+
const base =
|
|
2425
|
+
baseSpread && "argument" in baseSpread
|
|
2426
|
+
? this.emitExpression(baseSpread.argument)
|
|
2427
|
+
: "{}";
|
|
2428
|
+
const mutations = expr.entries
|
|
2429
|
+
.filter((e) => e.kind !== "SpreadEntry")
|
|
2430
|
+
.map((e) => {
|
|
2431
|
+
if (e.kind === "DotPathEntry") {
|
|
2432
|
+
return ` draft.${e.path.map((p) => p.name).join(".")} = ${this.emitExpression(e.value)};`;
|
|
2433
|
+
}
|
|
2434
|
+
if (e.kind === "FieldEntry") {
|
|
2435
|
+
const keyAccess = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(e.key.name)
|
|
2436
|
+
? `.${e.key.name}`
|
|
2437
|
+
: `[${JSON.stringify(e.key.name)}]`;
|
|
2438
|
+
return ` draft${keyAccess} = ${this.emitExpression(e.value)};`;
|
|
2439
|
+
}
|
|
2440
|
+
return ` draft.${e.key.name} = ${e.key.name};`;
|
|
2441
|
+
})
|
|
2442
|
+
.join("\n");
|
|
2443
|
+
return `create(${base}, (draft) => {\n${mutations}\n})`;
|
|
2444
|
+
}
|
|
2445
|
+
|
|
2446
|
+
const DISCRIMINANT_RECORD_KEYS = new Set([
|
|
2447
|
+
"_tag",
|
|
2448
|
+
"type",
|
|
2449
|
+
"kind",
|
|
2450
|
+
"status",
|
|
2451
|
+
"code",
|
|
2452
|
+
"tag",
|
|
2453
|
+
"ok",
|
|
2454
|
+
"state",
|
|
2455
|
+
]);
|
|
2456
|
+
const entries = expr.entries
|
|
2457
|
+
.map((e) => {
|
|
2458
|
+
if (e.kind === "SpreadEntry")
|
|
2459
|
+
return `...${this.emitExpression(e.argument)}`;
|
|
2460
|
+
if (e.kind === "FieldEntry" || e.kind === "ShorthandEntry") {
|
|
2461
|
+
const keyName = e.key.name;
|
|
2462
|
+
const valueExpr = e.kind === "FieldEntry" ? e.value : e.key;
|
|
2463
|
+
let val: string;
|
|
2464
|
+
if (isTryRecord && keyName === "try") {
|
|
2465
|
+
val = this.emitInTryContext(valueExpr);
|
|
2466
|
+
} else {
|
|
2467
|
+
val = this.emitExpression(valueExpr);
|
|
2468
|
+
}
|
|
2469
|
+
if (
|
|
2470
|
+
this.options.forwardNullProperties?.has(
|
|
2471
|
+
valueExpr.span.start.offset,
|
|
2472
|
+
)
|
|
2473
|
+
) {
|
|
2474
|
+
this.effectImports.add("Option");
|
|
2475
|
+
return `[${JSON.stringify(keyName)}]: Option.getOrNull(${val})`;
|
|
2476
|
+
}
|
|
2477
|
+
if (
|
|
2478
|
+
this.options.forwardOptionalProperties?.has(
|
|
2479
|
+
valueExpr.span.start.offset,
|
|
2480
|
+
)
|
|
2481
|
+
) {
|
|
2482
|
+
this.effectImports.add("Option");
|
|
2483
|
+
return `...((__value: import("effect").Option.Option<any>) => Option.isSome(__value) ? { [${JSON.stringify(keyName)}]: __value.value } : {})(${val})`;
|
|
2484
|
+
}
|
|
2485
|
+
if (this.options.markCallArguments)
|
|
2486
|
+
val = `__funscriptCallArgument(${valueExpr.span.start.offset}, ${val})`;
|
|
2487
|
+
if (valueExpr.kind === "BooleanLiteral") {
|
|
2488
|
+
val = `${val} as const`;
|
|
2489
|
+
} else if (
|
|
2490
|
+
(valueExpr.kind === "StringLiteral" ||
|
|
2491
|
+
valueExpr.kind === "NumberLiteral") &&
|
|
2492
|
+
DISCRIMINANT_RECORD_KEYS.has(keyName)
|
|
2493
|
+
) {
|
|
2494
|
+
val = `${val} as const`;
|
|
2495
|
+
}
|
|
2496
|
+
if (
|
|
2497
|
+
e.kind === "ShorthandEntry" &&
|
|
2498
|
+
!this.options.markCallArguments
|
|
2499
|
+
) {
|
|
2500
|
+
return keyName;
|
|
2501
|
+
}
|
|
2502
|
+
return `${emitPropertyKey(keyName)}: ${val}`;
|
|
2503
|
+
}
|
|
2504
|
+
return "";
|
|
2505
|
+
})
|
|
2506
|
+
.join(", ");
|
|
2507
|
+
return `{ ${entries} }`;
|
|
2508
|
+
});
|
|
2509
|
+
}
|
|
2510
|
+
|
|
2511
|
+
public emitNamedEffectFunction(
|
|
2512
|
+
expr: FunctionExpression,
|
|
2513
|
+
name: string,
|
|
2514
|
+
): string {
|
|
2515
|
+
return this.emitFunctionExpression(expr, name);
|
|
2516
|
+
}
|
|
2517
|
+
|
|
2518
|
+
private emitFunctionExpression(
|
|
2519
|
+
expr: FunctionExpression,
|
|
2520
|
+
traceName?: string,
|
|
2521
|
+
): string {
|
|
2522
|
+
const isTryThunk = this.emitCtx.pendingTryThunk;
|
|
2523
|
+
const prevPrimitiveIdentifiers = new Set(this.primitiveIdentifiers);
|
|
2524
|
+
try {
|
|
2525
|
+
return this.withEmitContext(
|
|
2526
|
+
{
|
|
2527
|
+
pendingTryThunk: false,
|
|
2528
|
+
inTryContext: isTryThunk,
|
|
2529
|
+
inPipelineStage: false,
|
|
2530
|
+
currentPlaceholderParam: null,
|
|
2531
|
+
},
|
|
2532
|
+
() => {
|
|
2533
|
+
for (const p of expr.params) {
|
|
2534
|
+
const isPrim = this.isPrimitiveType(p.typeAnnotation);
|
|
2535
|
+
for (const n of this.collectBindingNames(p.name)) {
|
|
2536
|
+
if (isPrim) {
|
|
2537
|
+
this.primitiveIdentifiers.add(n);
|
|
2538
|
+
} else {
|
|
2539
|
+
this.primitiveIdentifiers.delete(n);
|
|
2540
|
+
}
|
|
2541
|
+
}
|
|
2542
|
+
}
|
|
2543
|
+
const params = expr.params
|
|
2544
|
+
.map((p, index) => {
|
|
2545
|
+
const rest = p.isRest ? "..." : "";
|
|
2546
|
+
let name = this.emitBindingTarget(p.name);
|
|
2547
|
+
if (p.name.kind === "RecordPattern") {
|
|
2548
|
+
let recType: TypeNode | undefined = p.typeAnnotation;
|
|
2549
|
+
if (recType?.kind === "TypeReferenceNode") {
|
|
2550
|
+
recType = this.getRecordTypeNode(recType.name.name);
|
|
2551
|
+
}
|
|
2552
|
+
const recFields =
|
|
2553
|
+
recType?.kind === "RecordTypeNode"
|
|
2554
|
+
? extractRecordFields(recType, (name) =>
|
|
2555
|
+
this.getRecordTypeNode(name),
|
|
2556
|
+
)
|
|
2557
|
+
: undefined;
|
|
2558
|
+
if (patternHasOptionalField(p.name, recFields)) {
|
|
2559
|
+
name = `$funscriptParam${index}`;
|
|
2560
|
+
}
|
|
2561
|
+
}
|
|
2562
|
+
let typeAnn = "";
|
|
2563
|
+
if (p.typeAnnotation) {
|
|
2564
|
+
const base = this.emitTypeNode(p.typeAnnotation);
|
|
2565
|
+
typeAnn = `: ${base}`;
|
|
2566
|
+
}
|
|
2567
|
+
const defaultValue = p.defaultValue
|
|
2568
|
+
? ` = ${this.emitExpression(p.defaultValue)}`
|
|
2569
|
+
: "";
|
|
2570
|
+
return `${rest}${name}${typeAnn}${defaultValue}`;
|
|
2571
|
+
})
|
|
2572
|
+
.join(", ");
|
|
2573
|
+
const typeParams = this.emitTypeParameters(expr.typeParams, true);
|
|
2574
|
+
const retType = expr.returnType
|
|
2575
|
+
? `: ${this.emitTypeNode(expr.returnType)}`
|
|
2576
|
+
: "";
|
|
2577
|
+
|
|
2578
|
+
const records = new Map<
|
|
2579
|
+
string,
|
|
2580
|
+
Map<string, RecordFieldInfo> | undefined
|
|
2581
|
+
>();
|
|
2582
|
+
const recordDefaults: string[] = [];
|
|
2583
|
+
expr.params.forEach((param, paramIndex) => {
|
|
2584
|
+
if (param.name.kind === "RecordPattern") {
|
|
2585
|
+
let recType: TypeNode | undefined = param.typeAnnotation;
|
|
2586
|
+
if (recType?.kind === "TypeReferenceNode") {
|
|
2587
|
+
recType = this.getRecordTypeNode(recType.name.name);
|
|
2588
|
+
}
|
|
2589
|
+
const recFields =
|
|
2590
|
+
recType?.kind === "RecordTypeNode"
|
|
2591
|
+
? extractRecordFields(recType, (name) =>
|
|
2592
|
+
this.getRecordTypeNode(name),
|
|
2593
|
+
)
|
|
2594
|
+
: undefined;
|
|
2595
|
+
if (patternHasOptionalField(param.name, recFields)) {
|
|
2596
|
+
recordDefaults.push(
|
|
2597
|
+
...emitUnpackedRecordPattern({
|
|
2598
|
+
pat: param.name,
|
|
2599
|
+
basePath: `$funscriptParam${paramIndex}`,
|
|
2600
|
+
fieldsMap: recFields,
|
|
2601
|
+
emitBindingTarget: (target) =>
|
|
2602
|
+
this.emitBindingTarget(target),
|
|
2603
|
+
addEffectImport: (name) => this.effectImports.add(name),
|
|
2604
|
+
indent: " ",
|
|
2605
|
+
onRecordBinding: (varName, fields) => {
|
|
2606
|
+
records.set(varName, fields);
|
|
2607
|
+
},
|
|
2608
|
+
}),
|
|
2609
|
+
);
|
|
2610
|
+
}
|
|
2611
|
+
return;
|
|
2612
|
+
}
|
|
2613
|
+
if (param.name.kind !== "Identifier") return;
|
|
2614
|
+
const paramName = param.name.name;
|
|
2615
|
+
const declared = param.typeAnnotation;
|
|
2616
|
+
if (this.isHtmlPropsType(declared)) {
|
|
2617
|
+
records.set(paramName, undefined);
|
|
2618
|
+
return;
|
|
2619
|
+
}
|
|
2620
|
+
const recordType =
|
|
2621
|
+
declared?.kind === "TypeReferenceNode"
|
|
2622
|
+
? this.getRecordTypeNode(declared.name.name)
|
|
2623
|
+
: declared;
|
|
2624
|
+
if (recordType?.kind !== "RecordTypeNode") {
|
|
2625
|
+
records.set(paramName, undefined);
|
|
2626
|
+
return;
|
|
2627
|
+
}
|
|
2628
|
+
const fields = recordType.fields;
|
|
2629
|
+
const nestedFields = (
|
|
2630
|
+
type: TypeNode,
|
|
2631
|
+
): Map<string, RecordFieldInfo> | undefined => {
|
|
2632
|
+
if (type.kind !== "RecordTypeNode") {
|
|
2633
|
+
return undefined;
|
|
2634
|
+
}
|
|
2635
|
+
const result = new Map<string, RecordFieldInfo>();
|
|
2636
|
+
for (const field of type.fields) {
|
|
2637
|
+
const children = nestedFields(field.typeAnnotation);
|
|
2638
|
+
result.set(field.name.name, {
|
|
2639
|
+
optional: Boolean(field.optional),
|
|
2640
|
+
fields: children,
|
|
2641
|
+
});
|
|
2642
|
+
}
|
|
2643
|
+
return [...result.values()].some(
|
|
2644
|
+
(field) => field.optional || field.fields,
|
|
2645
|
+
)
|
|
2646
|
+
? result
|
|
2647
|
+
: undefined;
|
|
2648
|
+
};
|
|
2649
|
+
if (
|
|
2650
|
+
!fields.some(
|
|
2651
|
+
(field) =>
|
|
2652
|
+
field.optional ||
|
|
2653
|
+
field.defaultValue ||
|
|
2654
|
+
nestedFields(field.typeAnnotation),
|
|
2655
|
+
)
|
|
2656
|
+
) {
|
|
2657
|
+
records.set(paramName, undefined);
|
|
2658
|
+
return;
|
|
2659
|
+
}
|
|
2660
|
+
const mapped = new Map<string, RecordFieldInfo>();
|
|
2661
|
+
fields.forEach((field, fieldIndex) => {
|
|
2662
|
+
const binding = field.defaultValue
|
|
2663
|
+
? `$funscriptField${paramIndex}_${fieldIndex}`
|
|
2664
|
+
: undefined;
|
|
2665
|
+
if (field.defaultValue) {
|
|
2666
|
+
const currentParamNames = new Set(
|
|
2667
|
+
this.collectBindingNames(expr.params[paramIndex].name),
|
|
2668
|
+
);
|
|
2669
|
+
const subsequentParamNames = new Set(
|
|
2670
|
+
expr.params
|
|
2671
|
+
.slice(paramIndex + 1)
|
|
2672
|
+
.flatMap((p) => this.collectBindingNames(p.name)),
|
|
2673
|
+
);
|
|
2674
|
+
this.validateRecordDefault(
|
|
2675
|
+
field.defaultValue,
|
|
2676
|
+
currentParamNames,
|
|
2677
|
+
subsequentParamNames,
|
|
2678
|
+
);
|
|
2679
|
+
const raw = `$funscriptFieldRaw${paramIndex}_${fieldIndex}`;
|
|
2680
|
+
recordDefaults.push(
|
|
2681
|
+
` const ${raw} = ${paramName}.${field.name.name};`,
|
|
2682
|
+
);
|
|
2683
|
+
recordDefaults.push(
|
|
2684
|
+
` const ${binding} = ${raw} === undefined ? ${this.emitExpression(field.defaultValue)} : ${raw};`,
|
|
2685
|
+
);
|
|
2686
|
+
}
|
|
2687
|
+
const children = nestedFields(field.typeAnnotation);
|
|
2688
|
+
mapped.set(field.name.name, {
|
|
2689
|
+
optional: Boolean(field.optional),
|
|
2690
|
+
defaultBinding: binding,
|
|
2691
|
+
fields: children,
|
|
2692
|
+
});
|
|
2693
|
+
});
|
|
2694
|
+
records.set(paramName, mapped);
|
|
2695
|
+
});
|
|
2696
|
+
if (expr.body.kind === "BlockStatement") {
|
|
2697
|
+
for (const stmt of expr.body.statements) {
|
|
2698
|
+
if (
|
|
2699
|
+
stmt.kind !== "BindingDeclaration" ||
|
|
2700
|
+
stmt.isMut ||
|
|
2701
|
+
stmt.isJs ||
|
|
2702
|
+
!("name" in stmt.id) ||
|
|
2703
|
+
records.has(stmt.id.name)
|
|
2704
|
+
)
|
|
2705
|
+
continue;
|
|
2706
|
+
if (stmt.init.kind === "Identifier") {
|
|
2707
|
+
const fields = records.get(stmt.init.name);
|
|
2708
|
+
if (
|
|
2709
|
+
!fields ||
|
|
2710
|
+
[...fields.values()].some((field) => field.defaultBinding)
|
|
2711
|
+
)
|
|
2712
|
+
continue;
|
|
2713
|
+
records.set(stmt.id.name, fields);
|
|
2714
|
+
} else if (stmt.init.kind === "RecordExpression") {
|
|
2715
|
+
const fields = new Map<string, RecordFieldInfo>();
|
|
2716
|
+
let hasTracked = false;
|
|
2717
|
+
for (const prop of stmt.init.entries) {
|
|
2718
|
+
if (prop.kind === "SpreadEntry") {
|
|
2719
|
+
if (prop.argument.kind === "Identifier") {
|
|
2720
|
+
const spreadFields = records.get(prop.argument.name);
|
|
2721
|
+
if (spreadFields) {
|
|
2722
|
+
for (const [key, info] of spreadFields) {
|
|
2723
|
+
if (info.optional) hasTracked = true;
|
|
2724
|
+
fields.set(key, {
|
|
2725
|
+
optional: info.optional,
|
|
2726
|
+
defaultBinding: undefined,
|
|
2727
|
+
fields: info.fields,
|
|
2728
|
+
});
|
|
2729
|
+
}
|
|
2730
|
+
}
|
|
2731
|
+
}
|
|
2732
|
+
} else if (
|
|
2733
|
+
prop.kind === "FieldEntry" ||
|
|
2734
|
+
prop.kind === "ShorthandEntry"
|
|
2735
|
+
) {
|
|
2736
|
+
const keyName = prop.key.name;
|
|
2737
|
+
if (keyName) {
|
|
2738
|
+
fields.set(keyName, {
|
|
2739
|
+
optional: false,
|
|
2740
|
+
defaultBinding: undefined,
|
|
2741
|
+
fields: undefined,
|
|
2742
|
+
});
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2745
|
+
}
|
|
2746
|
+
if (hasTracked) {
|
|
2747
|
+
records.set(stmt.id.name, fields);
|
|
2748
|
+
}
|
|
2749
|
+
}
|
|
2750
|
+
}
|
|
2751
|
+
}
|
|
2752
|
+
const paramScopeEntries: [string, boolean][] = [];
|
|
2753
|
+
const varTypeEntries: [string, TypeNode | undefined][] = [];
|
|
2754
|
+
for (const p of expr.params) {
|
|
2755
|
+
if (p.name.kind === "RecordPattern") {
|
|
2756
|
+
let recType: TypeNode | undefined = p.typeAnnotation;
|
|
2757
|
+
if (recType?.kind === "TypeReferenceNode") {
|
|
2758
|
+
recType = this.getRecordTypeNode(recType.name.name);
|
|
2759
|
+
}
|
|
2760
|
+
const recFields =
|
|
2761
|
+
recType?.kind === "RecordTypeNode"
|
|
2762
|
+
? extractRecordFields(recType, (name) =>
|
|
2763
|
+
this.getRecordTypeNode(name),
|
|
2764
|
+
)
|
|
2765
|
+
: undefined;
|
|
2766
|
+
if (patternHasOptionalField(p.name, recFields)) {
|
|
2767
|
+
const collectParamsFromPattern = (
|
|
2768
|
+
pat: RecordPattern,
|
|
2769
|
+
fieldsMap?: Map<string, RecordFieldInfo>,
|
|
2770
|
+
typeNode?: TypeNode,
|
|
2771
|
+
parentIsOpt = false,
|
|
2772
|
+
) => {
|
|
2773
|
+
for (const f of pat.fields) {
|
|
2774
|
+
if (f.isRest) {
|
|
2775
|
+
paramScopeEntries.push([f.key.name, parentIsOpt]);
|
|
2776
|
+
varTypeEntries.push([f.key.name, undefined]);
|
|
2777
|
+
continue;
|
|
2778
|
+
}
|
|
2779
|
+
const info = fieldsMap?.get(f.key.name);
|
|
2780
|
+
const isOpt = parentIsOpt || info?.optional === true;
|
|
2781
|
+
const matchingFieldType =
|
|
2782
|
+
typeNode?.kind === "RecordTypeNode"
|
|
2783
|
+
? typeNode.fields.find(
|
|
2784
|
+
(tf) => tf.name.name === f.key.name,
|
|
2785
|
+
)?.typeAnnotation
|
|
2786
|
+
: undefined;
|
|
2787
|
+
if (f.pattern?.kind === "RecordPattern") {
|
|
2788
|
+
let subType: TypeNode | undefined = matchingFieldType;
|
|
2789
|
+
if (subType?.kind === "TypeReferenceNode") {
|
|
2790
|
+
subType = this.getRecordTypeNode(subType.name.name);
|
|
2791
|
+
}
|
|
2792
|
+
collectParamsFromPattern(
|
|
2793
|
+
f.pattern,
|
|
2794
|
+
info?.fields,
|
|
2795
|
+
subType,
|
|
2796
|
+
isOpt,
|
|
2797
|
+
);
|
|
2798
|
+
} else if (f.pattern?.kind === "ArrayPattern") {
|
|
2799
|
+
const elemType =
|
|
2800
|
+
matchingFieldType?.kind === "ArrayTypeNode"
|
|
2801
|
+
? matchingFieldType.elementType
|
|
2802
|
+
: undefined;
|
|
2803
|
+
for (const el of f.pattern.elements) {
|
|
2804
|
+
for (const n of this.collectBindingNames(el)) {
|
|
2805
|
+
paramScopeEntries.push([n, isOpt]);
|
|
2806
|
+
varTypeEntries.push([n, elemType]);
|
|
2807
|
+
}
|
|
2808
|
+
}
|
|
2809
|
+
} else {
|
|
2810
|
+
const varName =
|
|
2811
|
+
f.pattern?.kind === "VariablePattern"
|
|
2812
|
+
? f.pattern.name.name
|
|
2813
|
+
: f.key.name;
|
|
2814
|
+
paramScopeEntries.push([varName, isOpt]);
|
|
2815
|
+
varTypeEntries.push([varName, matchingFieldType]);
|
|
2816
|
+
}
|
|
2817
|
+
}
|
|
2818
|
+
};
|
|
2819
|
+
collectParamsFromPattern(p.name, recFields, recType);
|
|
2820
|
+
continue;
|
|
2821
|
+
}
|
|
2822
|
+
}
|
|
2823
|
+
for (const n of this.collectBindingNames(p.name)) {
|
|
2824
|
+
paramScopeEntries.push([n, false]);
|
|
2825
|
+
varTypeEntries.push([n, p.typeAnnotation]);
|
|
2826
|
+
}
|
|
2827
|
+
}
|
|
2828
|
+
this.parameterScopes.push(new Map(paramScopeEntries));
|
|
2829
|
+
this.variableTypeScopes.push(new Map(varTypeEntries));
|
|
2830
|
+
this.recordScopes.push(records);
|
|
2831
|
+
this.localBindingScopes.push(
|
|
2832
|
+
collectLocalBindings(
|
|
2833
|
+
expr.body.kind === "BlockStatement" ? expr.body.statements : [],
|
|
2834
|
+
),
|
|
2835
|
+
);
|
|
2836
|
+
try {
|
|
2837
|
+
if (
|
|
2838
|
+
traceName &&
|
|
2839
|
+
(expr.params.some((p) => p.defaultValue) ||
|
|
2840
|
+
recordDefaults.length > 0)
|
|
2841
|
+
) {
|
|
2842
|
+
const body = this.emitFunctionBody(expr.body);
|
|
2843
|
+
const defaults = recordDefaults.length
|
|
2844
|
+
? `${recordDefaults.join("\n")}\n`
|
|
2845
|
+
: "";
|
|
2846
|
+
return `${typeParams}(${params})${retType} => {\n${defaults} return Effect.fn(${JSON.stringify(traceName)})(() => {\n ${indentContinuation(body, 2)}\n })();\n}`;
|
|
2847
|
+
}
|
|
2848
|
+
let result: string;
|
|
2849
|
+
if (expr.body.kind === "BlockStatement" || recordDefaults.length) {
|
|
2850
|
+
const body = this.emitFunctionBody(expr.body);
|
|
2851
|
+
result = `${typeParams}(${params})${retType} => {\n${[...recordDefaults, body].filter(Boolean).join("\n")}\n}`;
|
|
2852
|
+
} else {
|
|
2853
|
+
result = `${typeParams}(${params})${retType} => ${this.emitArrowExpression(expr.body)}`;
|
|
2854
|
+
}
|
|
2855
|
+
return traceName
|
|
2856
|
+
? `Effect.fn(${JSON.stringify(traceName)})(${result})`
|
|
2857
|
+
: result;
|
|
2858
|
+
} finally {
|
|
2859
|
+
this.localBindingScopes.pop();
|
|
2860
|
+
this.recordScopes.pop();
|
|
2861
|
+
this.parameterScopes.pop();
|
|
2862
|
+
this.variableTypeScopes.pop();
|
|
2863
|
+
}
|
|
2864
|
+
},
|
|
2865
|
+
);
|
|
2866
|
+
} finally {
|
|
2867
|
+
this.primitiveIdentifiers = prevPrimitiveIdentifiers;
|
|
2868
|
+
}
|
|
2869
|
+
}
|
|
2870
|
+
|
|
2871
|
+
public emitFunctionBody(body: FunctionBody): string {
|
|
2872
|
+
if (body.kind === "BlockStatement") {
|
|
2873
|
+
const stmts = body.statements
|
|
2874
|
+
.map((s) => " " + this.emitStatement(s))
|
|
2875
|
+
.join("\n");
|
|
2876
|
+
const isUnit =
|
|
2877
|
+
body.finalExpression &&
|
|
2878
|
+
(body.finalExpression.kind === "Identifier" ||
|
|
2879
|
+
body.finalExpression.kind === "UpperIdentifier") &&
|
|
2880
|
+
body.finalExpression.name === "Void";
|
|
2881
|
+
const ret = isUnit
|
|
2882
|
+
? " return;"
|
|
2883
|
+
: body.finalExpression
|
|
2884
|
+
? ` return ${this.indentReturnedExpression(body.finalExpression)};`
|
|
2885
|
+
: "";
|
|
2886
|
+
if (stmts && ret) return `${stmts}\n${ret}`;
|
|
2887
|
+
if (stmts) return stmts;
|
|
2888
|
+
if (ret) return ret;
|
|
2889
|
+
return "";
|
|
2890
|
+
}
|
|
2891
|
+
const isUnit =
|
|
2892
|
+
(body.kind === "Identifier" || body.kind === "UpperIdentifier") &&
|
|
2893
|
+
body.name === "Void";
|
|
2894
|
+
if (isUnit) return " return;";
|
|
2895
|
+
return ` return ${this.indentReturnedExpression(body)};`;
|
|
2896
|
+
}
|
|
2897
|
+
|
|
2898
|
+
private indentReturnedExpression(expr: Expression): string {
|
|
2899
|
+
const emitted = this.emitExpression(expr);
|
|
2900
|
+
return expr.kind === "PipelineExpression" || expr.kind === "MatchExpression"
|
|
2901
|
+
? indentContinuation(emitted, 2)
|
|
2902
|
+
: emitted;
|
|
2903
|
+
}
|
|
2904
|
+
|
|
2905
|
+
public emitArrowExpression(expr: Expression): string {
|
|
2906
|
+
return emitArrowExpression(this, expr);
|
|
2907
|
+
}
|
|
2908
|
+
|
|
2909
|
+
public emitIndexTarget(expr: IndexExpression): string {
|
|
2910
|
+
return emitIndexTarget(this, expr);
|
|
2911
|
+
}
|
|
2912
|
+
|
|
2913
|
+
public emitStatement(stmt: Statement): string {
|
|
2914
|
+
return emitStatement(this, stmt);
|
|
2915
|
+
}
|
|
2916
|
+
|
|
2917
|
+
public emitWhenStatement(stmt: WhenStatement, insideEffect = false): string {
|
|
2918
|
+
return emitWhenStatement(this, stmt, insideEffect);
|
|
2919
|
+
}
|
|
2920
|
+
|
|
2921
|
+
private emitMatchExpression(
|
|
2922
|
+
expr: MatchExpression,
|
|
2923
|
+
pipelineScrutineeType?: TypeNode,
|
|
2924
|
+
pipelineSource?: Expression,
|
|
2925
|
+
): string {
|
|
2926
|
+
return new MatchEmitter({
|
|
2927
|
+
emitExpression: (expression) => this.emitExpression(expression),
|
|
2928
|
+
emitFunctionBody: (body) => this.emitFunctionBody(body),
|
|
2929
|
+
emitArrowExpression: (expression) => this.emitArrowExpression(expression),
|
|
2930
|
+
emitBindingTarget: (target) => this.emitBindingTarget(target),
|
|
2931
|
+
emitStringLiteral: (literal) => this.emitStringLiteral(literal),
|
|
2932
|
+
recordField: (expression) => this.recordField(expression),
|
|
2933
|
+
recordFields: (name) => this.recordFields(name),
|
|
2934
|
+
getRecordTypeNode: (name) => this.getRecordTypeNode(name),
|
|
2935
|
+
getVariantFields: (name) => this.variantFields.get(name),
|
|
2936
|
+
isTaggedUnionVariant: (type, variant) =>
|
|
2937
|
+
this.taggedUnionVariants.get(type)?.has(variant) ?? false,
|
|
2938
|
+
recordContracts: this.recordContracts,
|
|
2939
|
+
isOptionalParameter: (name) => this.isOptionalParameter(name),
|
|
2940
|
+
isOptionalValue: (expression) => this.isOptionalValue(expression),
|
|
2941
|
+
addEffectImport: (name) => this.addEffectImport(name),
|
|
2942
|
+
getTypeNodeForExpression: (expression) =>
|
|
2943
|
+
this.getTypeNodeForExpression(expression),
|
|
2944
|
+
emitTaggedMatch: (expression, arms, inputType, source) =>
|
|
2945
|
+
emitTaggedMatch(
|
|
2946
|
+
{
|
|
2947
|
+
taggedUnionVariants: this.taggedUnionVariants,
|
|
2948
|
+
taggedUnionParameters: this.taggedUnionParameters,
|
|
2949
|
+
localBindingScopes: this.localBindingScopes,
|
|
2950
|
+
importedTaggedUnionNames: this.importedTaggedUnionNames,
|
|
2951
|
+
getTypeNodeForExpression: (value) =>
|
|
2952
|
+
this.getTypeNodeForExpression(value),
|
|
2953
|
+
parameterScopes: this.parameterScopes,
|
|
2954
|
+
bindings: this.bindings,
|
|
2955
|
+
importedTaggedFactories: this.importedTaggedFactories,
|
|
2956
|
+
collectBindingNames: (target) => this.collectBindingNames(target),
|
|
2957
|
+
emitExpression: (value) => this.emitExpression(value),
|
|
2958
|
+
emitTypeParameters: (params, arrow) =>
|
|
2959
|
+
this.emitTypeParameters(params, arrow),
|
|
2960
|
+
},
|
|
2961
|
+
expression,
|
|
2962
|
+
arms,
|
|
2963
|
+
inputType,
|
|
2964
|
+
source,
|
|
2965
|
+
),
|
|
2966
|
+
requireTsPattern: () => {
|
|
2967
|
+
this.needsTsPattern = true;
|
|
2968
|
+
},
|
|
2969
|
+
enterOptionalPayload: (name, fields, _arm, payloadType) => {
|
|
2970
|
+
this.recordScopes.push(new Map([[name, fields]]));
|
|
2971
|
+
this.parameterScopes.push(new Map([[name, false]]));
|
|
2972
|
+
this.variableTypeScopes.push(new Map([[name, payloadType]]));
|
|
2973
|
+
},
|
|
2974
|
+
leaveOptionalPayload: () => {
|
|
2975
|
+
this.variableTypeScopes.pop();
|
|
2976
|
+
this.parameterScopes.pop();
|
|
2977
|
+
this.recordScopes.pop();
|
|
2978
|
+
},
|
|
2979
|
+
enterOptionalRecordPattern: (pattern, fields, _arm, payloadType) => {
|
|
2980
|
+
const recordScope = new Map<string, Map<string, RecordFieldInfo>>();
|
|
2981
|
+
const paramScope = new Map<string, boolean>();
|
|
2982
|
+
const varTypeScope = new Map<string, TypeNode | undefined>();
|
|
2983
|
+
|
|
2984
|
+
let typeFields: RecordTypeField[] | undefined;
|
|
2985
|
+
if (payloadType?.kind === "RecordTypeNode") {
|
|
2986
|
+
typeFields = payloadType.fields;
|
|
2987
|
+
} else if (payloadType?.kind === "TypeReferenceNode") {
|
|
2988
|
+
const contract =
|
|
2989
|
+
this.getRecordTypeNode(payloadType.name.name) ??
|
|
2990
|
+
this.recordContracts.get(payloadType.name.name);
|
|
2991
|
+
if (contract?.kind === "RecordTypeNode") {
|
|
2992
|
+
typeFields = contract.fields;
|
|
2993
|
+
}
|
|
2994
|
+
}
|
|
2995
|
+
|
|
2996
|
+
const registerRecordPatternFields = (
|
|
2997
|
+
pat: RecordPattern,
|
|
2998
|
+
fieldsMap?: Map<string, RecordFieldInfo>,
|
|
2999
|
+
recordTypeFields?: RecordTypeField[],
|
|
3000
|
+
) => {
|
|
3001
|
+
for (const f of pat.fields) {
|
|
3002
|
+
if (f.isRest) {
|
|
3003
|
+
paramScope.set(f.key.name, false);
|
|
3004
|
+
continue;
|
|
3005
|
+
}
|
|
3006
|
+
if (f.pattern?.kind === "WildcardPattern") continue;
|
|
3007
|
+
const info = fieldsMap?.get(f.key.name);
|
|
3008
|
+
const isOpt = info?.optional === true;
|
|
3009
|
+
const fieldTypeNode = recordTypeFields?.find(
|
|
3010
|
+
(tf) => tf.name.name === f.key.name,
|
|
3011
|
+
)?.typeAnnotation;
|
|
3012
|
+
|
|
3013
|
+
if (f.pattern?.kind === "RecordPattern") {
|
|
3014
|
+
let subTypeFields: RecordTypeField[] | undefined;
|
|
3015
|
+
if (fieldTypeNode?.kind === "RecordTypeNode") {
|
|
3016
|
+
subTypeFields = fieldTypeNode.fields;
|
|
3017
|
+
} else if (fieldTypeNode?.kind === "TypeReferenceNode") {
|
|
3018
|
+
const contract =
|
|
3019
|
+
this.getRecordTypeNode(fieldTypeNode.name.name) ??
|
|
3020
|
+
this.recordContracts.get(fieldTypeNode.name.name);
|
|
3021
|
+
if (contract?.kind === "RecordTypeNode") {
|
|
3022
|
+
subTypeFields = contract.fields;
|
|
3023
|
+
}
|
|
3024
|
+
}
|
|
3025
|
+
registerRecordPatternFields(
|
|
3026
|
+
f.pattern,
|
|
3027
|
+
info?.fields,
|
|
3028
|
+
subTypeFields,
|
|
3029
|
+
);
|
|
3030
|
+
} else {
|
|
3031
|
+
const varName =
|
|
3032
|
+
f.pattern?.kind === "VariablePattern"
|
|
3033
|
+
? f.pattern.name.name
|
|
3034
|
+
: f.key.name;
|
|
3035
|
+
paramScope.set(varName, isOpt);
|
|
3036
|
+
if (info?.fields) {
|
|
3037
|
+
recordScope.set(varName, info.fields);
|
|
3038
|
+
}
|
|
3039
|
+
if (fieldTypeNode) {
|
|
3040
|
+
if (isOpt) {
|
|
3041
|
+
varTypeScope.set(varName, {
|
|
3042
|
+
kind: "TypeReferenceNode",
|
|
3043
|
+
name: { kind: "Identifier", name: "Option", span: f.span },
|
|
3044
|
+
typeArgs: [fieldTypeNode],
|
|
3045
|
+
span: f.span,
|
|
3046
|
+
});
|
|
3047
|
+
} else {
|
|
3048
|
+
varTypeScope.set(varName, fieldTypeNode);
|
|
3049
|
+
}
|
|
3050
|
+
}
|
|
3051
|
+
}
|
|
3052
|
+
}
|
|
3053
|
+
};
|
|
3054
|
+
|
|
3055
|
+
registerRecordPatternFields(pattern, fields, typeFields);
|
|
3056
|
+
|
|
3057
|
+
this.recordScopes.push(recordScope);
|
|
3058
|
+
this.parameterScopes.push(paramScope);
|
|
3059
|
+
this.variableTypeScopes.push(varTypeScope);
|
|
3060
|
+
},
|
|
3061
|
+
leaveOptionalRecordPattern: () => {
|
|
3062
|
+
this.variableTypeScopes.pop();
|
|
3063
|
+
this.parameterScopes.pop();
|
|
3064
|
+
this.recordScopes.pop();
|
|
3065
|
+
},
|
|
3066
|
+
}).emitMatchExpression(expr, pipelineScrutineeType, pipelineSource);
|
|
3067
|
+
}
|
|
3068
|
+
|
|
3069
|
+
public emitEffectBlockExpression(expr: EffectBlockExpression): string {
|
|
3070
|
+
this.localBindingScopes.push(collectLocalBindings(expr.body));
|
|
3071
|
+
try {
|
|
3072
|
+
return emitEffectBlockExpression(this, expr);
|
|
3073
|
+
} finally {
|
|
3074
|
+
this.localBindingScopes.pop();
|
|
3075
|
+
}
|
|
3076
|
+
}
|
|
3077
|
+
|
|
3078
|
+
public emitParBlockExpression(expr: ParBlockExpression): string {
|
|
3079
|
+
return emitParBlockExpression(this, expr);
|
|
3080
|
+
}
|
|
3081
|
+
|
|
3082
|
+
public emitEffectCall(effectExpr: Expression): string {
|
|
3083
|
+
return emitEffectCall(this, effectExpr);
|
|
3084
|
+
}
|
|
3085
|
+
|
|
3086
|
+
// --- Types ---
|
|
3087
|
+
|
|
3088
|
+
public emitTypeParameters(
|
|
3089
|
+
typeParams: TypeParameter[] | undefined,
|
|
3090
|
+
forArrowExpression = false,
|
|
3091
|
+
): string {
|
|
3092
|
+
return emitTypeParameters(this, typeParams, forArrowExpression);
|
|
3093
|
+
}
|
|
3094
|
+
|
|
3095
|
+
public emitTypeNode(node: TypeNode): string {
|
|
3096
|
+
return emitTypeNode(this, node);
|
|
3097
|
+
}
|
|
3098
|
+
|
|
3099
|
+
// --- JSX ---
|
|
3100
|
+
|
|
3101
|
+
public emitJsxElement(el: JSXElement): string {
|
|
3102
|
+
return emitJsxElement(this, el);
|
|
3103
|
+
}
|
|
3104
|
+
|
|
3105
|
+
public emitJsxSelfClosingElement(el: JSXSelfClosingElement): string {
|
|
3106
|
+
return emitJsxSelfClosingElement(this, el);
|
|
3107
|
+
}
|
|
3108
|
+
|
|
3109
|
+
public emitJsxTagName(name: JSXTagName): string {
|
|
3110
|
+
return emitJsxTagName(name);
|
|
3111
|
+
}
|
|
3112
|
+
|
|
3113
|
+
public emitJsxAttribute(attr: JSXAttribute): string {
|
|
3114
|
+
return emitJsxAttribute(this, attr);
|
|
3115
|
+
}
|
|
3116
|
+
|
|
3117
|
+
public emitJsxExpressionContainer(container: JSXExpressionContainer): string {
|
|
3118
|
+
return emitJsxExpressionContainer(this, container);
|
|
3119
|
+
}
|
|
3120
|
+
|
|
3121
|
+
public emitJsxText(text: JSXText): string {
|
|
3122
|
+
return emitJsxText(text);
|
|
3123
|
+
}
|
|
3124
|
+
|
|
3125
|
+
public emitJsxChild(child: JSXChild): string {
|
|
3126
|
+
return emitJsxChild(this, child);
|
|
3127
|
+
}
|
|
3128
|
+
}
|