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,1287 @@
|
|
|
1
|
+
import { FunScriptError } from "../errors.js";
|
|
2
|
+
import { indentContinuation } from "./indent.js";
|
|
3
|
+
import { emitUnpackedRecordPattern, patternHasOptionalField, } from "./declarations.js";
|
|
4
|
+
const JS_ERROR_NAMES = new Set([
|
|
5
|
+
"Error",
|
|
6
|
+
"TypeError",
|
|
7
|
+
"RangeError",
|
|
8
|
+
"SyntaxError",
|
|
9
|
+
"ReferenceError",
|
|
10
|
+
"URIError",
|
|
11
|
+
"EvalError",
|
|
12
|
+
]);
|
|
13
|
+
/** Renders match expressions and their patterns using the enclosing emitter's scope. */
|
|
14
|
+
export class MatchEmitter {
|
|
15
|
+
context;
|
|
16
|
+
constructor(context) {
|
|
17
|
+
this.context = context;
|
|
18
|
+
}
|
|
19
|
+
emitMatchExpression(expr, pipelineScrutineeType, pipelineSource) {
|
|
20
|
+
if (expr.isCatch) {
|
|
21
|
+
return this.emitCatchExpression(expr);
|
|
22
|
+
}
|
|
23
|
+
if (!expr.scrutinee && !expr.isPipeline) {
|
|
24
|
+
return this.emitIfElseMatch(expr.arms, (arm) => this.emitPatternAsCondition(arm.pattern));
|
|
25
|
+
}
|
|
26
|
+
const scrutineeId = expr.scrutinee?.kind === "Identifier" ? expr.scrutinee.name : undefined;
|
|
27
|
+
const isGuardOnlyNarrowingMatch = Boolean(expr.scrutinee) &&
|
|
28
|
+
!expr.isPipeline &&
|
|
29
|
+
expr.arms.some((a) => Boolean(a.guard)) &&
|
|
30
|
+
expr.arms.every((a) => a.pattern.kind === "WildcardPattern" ||
|
|
31
|
+
(scrutineeId !== undefined &&
|
|
32
|
+
a.pattern.kind === "VariablePattern" &&
|
|
33
|
+
a.pattern.name.name === scrutineeId));
|
|
34
|
+
if (isGuardOnlyNarrowingMatch) {
|
|
35
|
+
return this.emitIfElseMatch(expr.arms, (arm) => arm.guard ? this.context.emitExpression(arm.guard) : "true");
|
|
36
|
+
}
|
|
37
|
+
const scrutineeType = expr.scrutinee && !expr.isPipeline
|
|
38
|
+
? this.context.getTypeNodeForExpression?.(expr.scrutinee)
|
|
39
|
+
: pipelineScrutineeType;
|
|
40
|
+
if (scrutineeType?.kind === "TypeReferenceNode" &&
|
|
41
|
+
scrutineeType.name.name === "JSNullish") {
|
|
42
|
+
return this.emitJSNullishMatch(expr.scrutinee, expr.arms);
|
|
43
|
+
}
|
|
44
|
+
const hasNullishFailurePattern = expr.arms.some((arm) => {
|
|
45
|
+
const p = arm.pattern;
|
|
46
|
+
return (p.kind === "ConstructorPattern" &&
|
|
47
|
+
p.constructorName.name === "Failure" &&
|
|
48
|
+
p.args.some((arg) => arg.kind === "ConstructorPattern" &&
|
|
49
|
+
(arg.constructorName.name === "Null" ||
|
|
50
|
+
arg.constructorName.name === "Undefined")));
|
|
51
|
+
});
|
|
52
|
+
const isKnownTaggedFailure = scrutineeType?.kind === "TypeReferenceNode" &&
|
|
53
|
+
(scrutineeType.name.name === "Result" ||
|
|
54
|
+
(this.context.isTaggedUnionVariant?.(scrutineeType.name.name, "Failure") ??
|
|
55
|
+
false));
|
|
56
|
+
if (hasNullishFailurePattern && !isKnownTaggedFailure) {
|
|
57
|
+
throw new FunScriptError("Failure(Null) and Failure(Undefined) require a statically known JSNullish<T> scrutinee; add a JSNullish annotation before matching", expr.span);
|
|
58
|
+
}
|
|
59
|
+
if (scrutineeType?.kind === "TypeReferenceNode" &&
|
|
60
|
+
scrutineeType.name.name === "JSOptional") {
|
|
61
|
+
return this.emitJSOptionalMatch(expr.scrutinee, expr.arms);
|
|
62
|
+
}
|
|
63
|
+
// Scrutinee match or pipeline match => ts-pattern
|
|
64
|
+
this.context.requireTsPattern();
|
|
65
|
+
const isPipe = Boolean(expr.isPipeline);
|
|
66
|
+
let code = isPipe
|
|
67
|
+
? `(($v) => ${this.context.emitTaggedMatch?.(undefined, expr.arms, pipelineScrutineeType, pipelineSource) ?? "match($v)"}`
|
|
68
|
+
: (this.context.emitTaggedMatch?.(expr.scrutinee, expr.arms) ??
|
|
69
|
+
`match(${this.context.emitExpression(expr.scrutinee)})`);
|
|
70
|
+
const optionalRecord = expr.scrutinee
|
|
71
|
+
? this.context.recordField(expr.scrutinee)
|
|
72
|
+
: undefined;
|
|
73
|
+
const payloadFields = optionalRecord?.optional
|
|
74
|
+
? optionalRecord.fields
|
|
75
|
+
: undefined;
|
|
76
|
+
const isOptional = expr.scrutinee
|
|
77
|
+
? this.context.isOptionalValue(expr.scrutinee)
|
|
78
|
+
: false;
|
|
79
|
+
const hasWildcard = expr.arms.some((a) => a.pattern.kind === "WildcardPattern");
|
|
80
|
+
const lastArm = expr.arms[expr.arms.length - 1];
|
|
81
|
+
const isBooleanLiteralPattern = (p) => p.kind === "LiteralPattern" && p.literal.kind === "BooleanLiteral";
|
|
82
|
+
const lastArmIsUnguardedBool = Boolean(lastArm && !lastArm.guard && isBooleanLiteralPattern(lastArm.pattern));
|
|
83
|
+
const oppositeBoolValue = lastArmIsUnguardedBool &&
|
|
84
|
+
lastArm &&
|
|
85
|
+
isBooleanLiteralPattern(lastArm.pattern)
|
|
86
|
+
? !lastArm.pattern.literal.value
|
|
87
|
+
: undefined;
|
|
88
|
+
const hasOppositeUnguarded = oppositeBoolValue !== undefined &&
|
|
89
|
+
expr.arms
|
|
90
|
+
.slice(0, -1)
|
|
91
|
+
.some((a) => !a.guard &&
|
|
92
|
+
isBooleanLiteralPattern(a.pattern) &&
|
|
93
|
+
a.pattern.literal.value === oppositeBoolValue);
|
|
94
|
+
const isExhaustiveBooleanMatch = !hasWildcard && lastArmIsUnguardedBool && hasOppositeUnguarded;
|
|
95
|
+
for (let i = 0; i < expr.arms.length; i++) {
|
|
96
|
+
const arm = expr.arms[i];
|
|
97
|
+
const isLastArm = i === expr.arms.length - 1;
|
|
98
|
+
let payloadName;
|
|
99
|
+
let recordPattern;
|
|
100
|
+
if (payloadFields) {
|
|
101
|
+
const pat = arm.pattern;
|
|
102
|
+
if (pat.kind === "ConstructorPattern" &&
|
|
103
|
+
pat.constructorName.name === "Some" &&
|
|
104
|
+
pat.args.length === 1 &&
|
|
105
|
+
pat.args[0].kind === "VariablePattern") {
|
|
106
|
+
payloadName = pat.args[0].name.name;
|
|
107
|
+
}
|
|
108
|
+
else if (pat.kind === "ConstructorPattern" &&
|
|
109
|
+
pat.constructorName.name === "Some" &&
|
|
110
|
+
pat.args.length === 1 &&
|
|
111
|
+
pat.args[0].kind === "RecordPattern") {
|
|
112
|
+
recordPattern = pat.args[0];
|
|
113
|
+
}
|
|
114
|
+
else if (pat.kind === "ConstructorPattern" &&
|
|
115
|
+
pat.constructorName.name === "Some" &&
|
|
116
|
+
pat.args.length === 1) {
|
|
117
|
+
// Allowed: Some(...) with any pattern
|
|
118
|
+
}
|
|
119
|
+
else if (!(pat.kind === "WildcardPattern" ||
|
|
120
|
+
(pat.kind === "ConstructorPattern" &&
|
|
121
|
+
pat.constructorName.name === "None" &&
|
|
122
|
+
pat.args.length === 0))) {
|
|
123
|
+
throw new FunScriptError("Match an optional record with Some(name), Some({...}), None, or _", pat.span);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
let payloadType;
|
|
127
|
+
if (expr.scrutinee && this.context.getTypeNodeForExpression) {
|
|
128
|
+
const scrutineeType = this.context.getTypeNodeForExpression(expr.scrutinee);
|
|
129
|
+
if (scrutineeType?.kind === "TypeReferenceNode" &&
|
|
130
|
+
scrutineeType.name.name === "Option" &&
|
|
131
|
+
scrutineeType.typeArgs?.length === 1) {
|
|
132
|
+
payloadType = scrutineeType.typeArgs[0];
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
payloadType = scrutineeType;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (payloadName && payloadFields) {
|
|
139
|
+
this.context.enterOptionalPayload(payloadName, payloadFields, arm, payloadType);
|
|
140
|
+
}
|
|
141
|
+
else if (recordPattern && payloadFields) {
|
|
142
|
+
this.context.enterOptionalRecordPattern?.(recordPattern, payloadFields, arm, payloadType);
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
if ((arm.pattern.kind === "WildcardPattern" && !arm.guard) ||
|
|
146
|
+
(isExhaustiveBooleanMatch && isLastArm)) {
|
|
147
|
+
const body = this.emitArmHandler(arm.body, arm.pattern, expr.scrutinee, payloadFields);
|
|
148
|
+
code += `\n .otherwise(${body})`;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
const pat = this.emitPattern(arm.pattern);
|
|
152
|
+
const body = this.emitArmHandler(arm.body, arm.pattern, expr.scrutinee, payloadFields);
|
|
153
|
+
if (arm.guard) {
|
|
154
|
+
let guardFn;
|
|
155
|
+
if (arm.pattern.kind === "RecordPattern") {
|
|
156
|
+
const scrutineeName = expr.scrutinee?.kind === "Identifier"
|
|
157
|
+
? expr.scrutinee.name
|
|
158
|
+
: undefined;
|
|
159
|
+
const unpackLines = this.emitRecordPatternUnpack(arm.pattern, "$matched");
|
|
160
|
+
if (unpackLines.length > 0) {
|
|
161
|
+
const lines = [
|
|
162
|
+
...(scrutineeName
|
|
163
|
+
? [`const ${scrutineeName} = $matched;`]
|
|
164
|
+
: []),
|
|
165
|
+
...unpackLines,
|
|
166
|
+
`return ${indentContinuation(this.context.emitExpression(arm.guard), 4)};`,
|
|
167
|
+
];
|
|
168
|
+
guardFn = `($matched) => {\n ${lines.join("\n ")}\n }`;
|
|
169
|
+
}
|
|
170
|
+
else if (scrutineeName) {
|
|
171
|
+
guardFn = `(${scrutineeName}) => ${this.context.emitExpression(arm.guard)}`;
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
guardFn = `($matched) => ${this.context.emitExpression(arm.guard)}`;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else if (arm.pattern.kind === "OrPattern" &&
|
|
178
|
+
arm.pattern.patterns.every((p) => p.kind === "RecordPattern")) {
|
|
179
|
+
const scrutineeName = expr.scrutinee?.kind === "Identifier"
|
|
180
|
+
? expr.scrutinee.name
|
|
181
|
+
: undefined;
|
|
182
|
+
const unpackLines = this.emitPatternUnpackLines(arm.pattern, "$matched");
|
|
183
|
+
if (unpackLines.length > 0) {
|
|
184
|
+
const lines = [
|
|
185
|
+
...(scrutineeName
|
|
186
|
+
? [`const ${scrutineeName} = $matched;`]
|
|
187
|
+
: []),
|
|
188
|
+
...unpackLines,
|
|
189
|
+
`return ${indentContinuation(this.context.emitExpression(arm.guard), 4)};`,
|
|
190
|
+
];
|
|
191
|
+
guardFn = `($matched) => {\n ${lines.join("\n ")}\n }`;
|
|
192
|
+
}
|
|
193
|
+
else if (scrutineeName) {
|
|
194
|
+
guardFn = `(${scrutineeName}) => ${this.context.emitExpression(arm.guard)}`;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
guardFn = `($matched) => ${this.context.emitExpression(arm.guard)}`;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
const vars = this.collectPatternVariables(arm.pattern);
|
|
202
|
+
if (vars.length > 0) {
|
|
203
|
+
const some = arm.pattern.kind === "ConstructorPattern" &&
|
|
204
|
+
arm.pattern.constructorName.name === "Some" &&
|
|
205
|
+
arm.pattern.args.length === 1 &&
|
|
206
|
+
(arm.pattern.args[0].kind === "VariablePattern" ||
|
|
207
|
+
(arm.pattern.args[0].kind === "ArrayPattern" &&
|
|
208
|
+
this.isSimpleBindingPattern(arm.pattern.args[0])));
|
|
209
|
+
const someRecord = arm.pattern.kind === "ConstructorPattern" &&
|
|
210
|
+
arm.pattern.constructorName.name === "Some" &&
|
|
211
|
+
arm.pattern.args.length === 1 &&
|
|
212
|
+
arm.pattern.args[0].kind === "RecordPattern" &&
|
|
213
|
+
(this.isSimpleBindingPattern(arm.pattern.args[0]) ||
|
|
214
|
+
patternHasOptionalField(arm.pattern.args[0], payloadFields));
|
|
215
|
+
const directOptional = expr.scrutinee?.kind === "Identifier" &&
|
|
216
|
+
this.context.isOptionalParameter(expr.scrutinee.name);
|
|
217
|
+
let unpack;
|
|
218
|
+
if (someRecord) {
|
|
219
|
+
unpack = this.emitSomeRecordUnpack(arm.pattern
|
|
220
|
+
.args[0], payloadFields, directOptional || isOptional);
|
|
221
|
+
}
|
|
222
|
+
else if (arm.pattern.kind === "VariablePattern") {
|
|
223
|
+
unpack = `const ${vars[0]} = $matched;`;
|
|
224
|
+
}
|
|
225
|
+
else if (some) {
|
|
226
|
+
const target = this.context.emitBindingTarget(arm.pattern.args[0]);
|
|
227
|
+
unpack = `const { value: ${target} } = $matched;`;
|
|
228
|
+
}
|
|
229
|
+
else if (arm.pattern.kind === "ConstructorPattern" &&
|
|
230
|
+
JS_ERROR_NAMES.has(arm.pattern.constructorName.name)) {
|
|
231
|
+
unpack = `const { ${vars.join(", ")} } = $matched;`;
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
unpack = this.emitPatternUnpackLines(arm.pattern, "$matched", {
|
|
235
|
+
isOptionalScrutinee: directOptional || isOptional,
|
|
236
|
+
payloadFields,
|
|
237
|
+
}).join("\n ");
|
|
238
|
+
}
|
|
239
|
+
guardFn = `($matched) => {\n ${unpack}\n return ${indentContinuation(this.context.emitExpression(arm.guard), 4)};\n }`;
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
guardFn = `() => ${this.context.emitExpression(arm.guard)}`;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
code += `\n .with(${pat}, ${guardFn}, ${body})`;
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
code += `\n .with(${pat}, ${body})`;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
finally {
|
|
253
|
+
if (payloadName && payloadFields) {
|
|
254
|
+
this.context.leaveOptionalPayload();
|
|
255
|
+
}
|
|
256
|
+
else if (recordPattern && payloadFields) {
|
|
257
|
+
this.context.leaveOptionalRecordPattern?.();
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (!hasWildcard && !isExhaustiveBooleanMatch) {
|
|
262
|
+
code += `\n .exhaustive()`;
|
|
263
|
+
}
|
|
264
|
+
if (isPipe) {
|
|
265
|
+
code += `)`;
|
|
266
|
+
}
|
|
267
|
+
return code;
|
|
268
|
+
}
|
|
269
|
+
emitPatternAsCondition(pattern) {
|
|
270
|
+
if (pattern.kind === "LiteralPattern") {
|
|
271
|
+
if (pattern.literal.kind === "BooleanLiteral" &&
|
|
272
|
+
pattern.literal.rawExpr) {
|
|
273
|
+
return this.context.emitExpression(pattern.literal.rawExpr);
|
|
274
|
+
}
|
|
275
|
+
if (pattern.literal.kind === "BooleanLiteral") {
|
|
276
|
+
return pattern.literal.value ? "true" : "false";
|
|
277
|
+
}
|
|
278
|
+
if (pattern.literal.kind === "NumberLiteral") {
|
|
279
|
+
return `${pattern.literal.value}`;
|
|
280
|
+
}
|
|
281
|
+
return this.context.emitStringLiteral(pattern.literal);
|
|
282
|
+
}
|
|
283
|
+
return "true";
|
|
284
|
+
}
|
|
285
|
+
isFilteringPattern(pat) {
|
|
286
|
+
if (!pat)
|
|
287
|
+
return false;
|
|
288
|
+
if (pat.kind === "VariablePattern" || pat.kind === "WildcardPattern") {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
emitPattern(pat) {
|
|
294
|
+
if (pat.kind === "WildcardPattern")
|
|
295
|
+
return "P._";
|
|
296
|
+
if (pat.kind === "LiteralPattern") {
|
|
297
|
+
if (pat.literal.kind === "BooleanLiteral")
|
|
298
|
+
return pat.literal.value ? "true" : "false";
|
|
299
|
+
if (pat.literal.kind === "NumberLiteral")
|
|
300
|
+
return `${pat.literal.value}`;
|
|
301
|
+
return this.context.emitStringLiteral(pat.literal);
|
|
302
|
+
}
|
|
303
|
+
if (pat.kind === "VariablePattern")
|
|
304
|
+
return `P.select("${pat.name.name}")`;
|
|
305
|
+
if (pat.kind === "ConstructorPattern") {
|
|
306
|
+
if (JS_ERROR_NAMES.has(pat.constructorName.name)) {
|
|
307
|
+
if (pat.args.length === 0) {
|
|
308
|
+
return `P.instanceOf(${pat.constructorName.name})`;
|
|
309
|
+
}
|
|
310
|
+
if (pat.args.length === 1) {
|
|
311
|
+
return `P.intersection(P.instanceOf(${pat.constructorName.name}), { message: ${this.emitPattern(pat.args[0])} })`;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (pat.args.length === 0) {
|
|
315
|
+
return `{ _tag: "${pat.constructorName.name}" }`;
|
|
316
|
+
}
|
|
317
|
+
if (pat.constructorName.name === "Some" && pat.args.length === 1) {
|
|
318
|
+
const arg = pat.args[0];
|
|
319
|
+
if (arg.kind === "ArrayPattern" ||
|
|
320
|
+
arg.kind === "LiteralPattern" ||
|
|
321
|
+
arg.kind === "OrPattern" ||
|
|
322
|
+
arg.kind === "ConstructorPattern") {
|
|
323
|
+
return `{ _tag: "Some", value: ${this.emitPattern(arg)} }`;
|
|
324
|
+
}
|
|
325
|
+
if (arg.kind === "RecordPattern") {
|
|
326
|
+
const filteringFields = arg.fields.filter((f) => !f.isRest && this.isFilteringPattern(f.pattern));
|
|
327
|
+
if (filteringFields.length > 0) {
|
|
328
|
+
const fieldsStr = filteringFields
|
|
329
|
+
.map((f) => `${f.key.name}: ${this.emitPattern(f.pattern)}`)
|
|
330
|
+
.join(", ");
|
|
331
|
+
return `{ _tag: "Some", value: { ${fieldsStr} } }`;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
const fieldNames = this.context.getVariantFields?.(pat.constructorName.name);
|
|
336
|
+
if (fieldNames && fieldNames.length === pat.args.length) {
|
|
337
|
+
const filteringFields = [];
|
|
338
|
+
for (let i = 0; i < fieldNames.length; i++) {
|
|
339
|
+
const arg = pat.args[i];
|
|
340
|
+
if (this.isFilteringPattern(arg)) {
|
|
341
|
+
filteringFields.push(`${fieldNames[i]}: ${this.emitPattern(arg)}`);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (filteringFields.length > 0) {
|
|
345
|
+
return `{ _tag: "${pat.constructorName.name}", ${filteringFields.join(", ")} }`;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
// Tagged union positional pattern matches fields
|
|
349
|
+
return `{ _tag: "${pat.constructorName.name}" }`;
|
|
350
|
+
}
|
|
351
|
+
if (pat.kind === "OrPattern") {
|
|
352
|
+
const sub = pat.patterns.map((p) => this.emitPattern(p)).join(", ");
|
|
353
|
+
return `P.union(${sub})`;
|
|
354
|
+
}
|
|
355
|
+
if (pat.kind === "RecordPattern") {
|
|
356
|
+
const fields = pat.fields
|
|
357
|
+
.filter((f) => !f.isRest)
|
|
358
|
+
.map((f) => {
|
|
359
|
+
if (!f.pattern || f.pattern.kind === "VariablePattern") {
|
|
360
|
+
return `${f.key.name}: P._`;
|
|
361
|
+
}
|
|
362
|
+
return `${f.key.name}: ${this.emitPattern(f.pattern)}`;
|
|
363
|
+
})
|
|
364
|
+
.join(", ");
|
|
365
|
+
return `{ ${fields} }`;
|
|
366
|
+
}
|
|
367
|
+
if (pat.kind === "ArrayPattern") {
|
|
368
|
+
const elements = [];
|
|
369
|
+
for (const el of pat.elements) {
|
|
370
|
+
if (el.kind === "RestPattern") {
|
|
371
|
+
elements.push("...P.array()");
|
|
372
|
+
}
|
|
373
|
+
else if (el.kind === "VariablePattern" ||
|
|
374
|
+
el.kind === "WildcardPattern") {
|
|
375
|
+
elements.push("P._");
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
elements.push(this.emitPattern(el));
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return `[${elements.join(", ")}]`;
|
|
382
|
+
}
|
|
383
|
+
return "P._";
|
|
384
|
+
}
|
|
385
|
+
collectRecordDestructuring(pat) {
|
|
386
|
+
const parts = [];
|
|
387
|
+
const hasRest = pat.fields.some((f) => f.isRest);
|
|
388
|
+
for (const f of pat.fields) {
|
|
389
|
+
if (f.isRest)
|
|
390
|
+
continue;
|
|
391
|
+
if (!f.pattern) {
|
|
392
|
+
parts.push(f.key.name);
|
|
393
|
+
}
|
|
394
|
+
else if (f.pattern.kind === "VariablePattern") {
|
|
395
|
+
parts.push(`${f.key.name}: ${f.pattern.name.name}`);
|
|
396
|
+
}
|
|
397
|
+
else if (f.pattern.kind === "RecordPattern") {
|
|
398
|
+
const sub = this.collectRecordDestructuring(f.pattern);
|
|
399
|
+
if (sub.length > 0) {
|
|
400
|
+
parts.push(`${f.key.name}: { ${sub.join(", ")} }`);
|
|
401
|
+
}
|
|
402
|
+
else if (hasRest) {
|
|
403
|
+
parts.push(`${f.key.name}: _${f.key.name}`);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
else if (f.pattern.kind === "ArrayPattern") {
|
|
407
|
+
parts.push(`${f.key.name}: ${this.context.emitBindingTarget(f.pattern)}`);
|
|
408
|
+
}
|
|
409
|
+
else if (hasRest) {
|
|
410
|
+
parts.push(`${f.key.name}: _${f.key.name}`);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (hasRest) {
|
|
414
|
+
const restField = pat.fields.find((f) => f.isRest);
|
|
415
|
+
parts.push(`...${restField.key.name}`);
|
|
416
|
+
}
|
|
417
|
+
return parts;
|
|
418
|
+
}
|
|
419
|
+
emitSomeRecordUnpack(pat, payloadFields, _isOptional) {
|
|
420
|
+
if (patternHasOptionalField(pat, payloadFields)) {
|
|
421
|
+
this.context.addEffectImport?.("Option");
|
|
422
|
+
const cast = "";
|
|
423
|
+
const lines = [
|
|
424
|
+
`const { value: $funscriptSomeInner } = $matched${cast};`,
|
|
425
|
+
...emitUnpackedRecordPattern({
|
|
426
|
+
pat,
|
|
427
|
+
basePath: "$funscriptSomeInner",
|
|
428
|
+
fieldsMap: payloadFields,
|
|
429
|
+
emitBindingTarget: (target) => this.context.emitBindingTarget(target),
|
|
430
|
+
addEffectImport: (name) => this.context.addEffectImport?.(name),
|
|
431
|
+
}),
|
|
432
|
+
];
|
|
433
|
+
return lines.join("\n ");
|
|
434
|
+
}
|
|
435
|
+
const destructParts = this.collectRecordDestructuring(pat);
|
|
436
|
+
if (destructParts.length === 0)
|
|
437
|
+
return "";
|
|
438
|
+
const cast = "";
|
|
439
|
+
return `const { value: { ${destructParts.join(", ")} } } = $matched${cast};`;
|
|
440
|
+
}
|
|
441
|
+
emitArmHandler(body, pat, scrutinee, payloadFields) {
|
|
442
|
+
if (pat?.kind === "ConstructorPattern" &&
|
|
443
|
+
pat.args.some((arg) => arg.kind === "VariablePattern") &&
|
|
444
|
+
pat.args.every((arg) => ["VariablePattern", "WildcardPattern", "LiteralPattern"].includes(arg.kind)) &&
|
|
445
|
+
!JS_ERROR_NAMES.has(pat.constructorName.name)) {
|
|
446
|
+
const fields = pat.constructorName.name === "Some" && pat.args.length === 1
|
|
447
|
+
? ["value"]
|
|
448
|
+
: this.context.getVariantFields?.(pat.constructorName.name);
|
|
449
|
+
if (fields?.length === pat.args.length) {
|
|
450
|
+
const bindings = pat.args.flatMap((arg, index) => arg.kind === "VariablePattern"
|
|
451
|
+
? [`${fields[index]}: ${arg.name.name}`]
|
|
452
|
+
: []);
|
|
453
|
+
const parameter = `({ ${bindings.join(", ")} })`;
|
|
454
|
+
return body.kind === "BlockStatement"
|
|
455
|
+
? `${parameter} => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`
|
|
456
|
+
: `${parameter} => ${this.context.emitArrowExpression(body)}`;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
if (pat?.kind === "RecordPattern") {
|
|
460
|
+
const scrutineeName = scrutinee?.kind === "Identifier" ? scrutinee.name : undefined;
|
|
461
|
+
const unpackLines = this.emitRecordPatternUnpack(pat, "$matched");
|
|
462
|
+
if (unpackLines.length > 0) {
|
|
463
|
+
const lines = [];
|
|
464
|
+
if (scrutineeName) {
|
|
465
|
+
lines.push(`const ${scrutineeName} = $matched;`);
|
|
466
|
+
}
|
|
467
|
+
lines.push(...unpackLines);
|
|
468
|
+
if (body.kind === "BlockStatement") {
|
|
469
|
+
return `($matched) => {\n ${lines.join("\n ")}\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
470
|
+
}
|
|
471
|
+
return `($matched) => {\n ${lines.join("\n ")}\n return ${indentContinuation(this.context.emitExpression(body), 4)};\n }`;
|
|
472
|
+
}
|
|
473
|
+
if (scrutineeName) {
|
|
474
|
+
if (body.kind === "BlockStatement") {
|
|
475
|
+
return `(${scrutineeName}) => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
476
|
+
}
|
|
477
|
+
return `(${scrutineeName}) => ${this.context.emitArrowExpression(body)}`;
|
|
478
|
+
}
|
|
479
|
+
if (body.kind === "BlockStatement") {
|
|
480
|
+
return `($matched) => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
481
|
+
}
|
|
482
|
+
return `($matched) => ${this.context.emitArrowExpression(body)}`;
|
|
483
|
+
}
|
|
484
|
+
if (pat?.kind === "ArrayPattern") {
|
|
485
|
+
const scrutineeName = scrutinee?.kind === "Identifier" ? scrutinee.name : undefined;
|
|
486
|
+
const names = this.collectPatternVariables(pat);
|
|
487
|
+
if (names.length > 0 &&
|
|
488
|
+
new Set(names).size === names.length &&
|
|
489
|
+
(!scrutineeName || !names.includes(scrutineeName)) &&
|
|
490
|
+
pat.elements.every((element) => element.kind === "VariablePattern" ||
|
|
491
|
+
element.kind === "WildcardPattern")) {
|
|
492
|
+
const bindings = pat.elements.flatMap((element, index) => element.kind === "VariablePattern"
|
|
493
|
+
? [`${index}: ${element.name.name}`]
|
|
494
|
+
: []);
|
|
495
|
+
const parameter = `({ ${bindings.join(", ")} }${scrutineeName ? `, ${scrutineeName}` : ""})`;
|
|
496
|
+
return body.kind === "BlockStatement"
|
|
497
|
+
? `${parameter} => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`
|
|
498
|
+
: `${parameter} => ${this.context.emitArrowExpression(body)}`;
|
|
499
|
+
}
|
|
500
|
+
const lines = [];
|
|
501
|
+
if (scrutineeName) {
|
|
502
|
+
lines.push(`const ${scrutineeName} = $matched;`);
|
|
503
|
+
}
|
|
504
|
+
lines.push(...this.emitPatternUnpackLines(pat, "$matched"));
|
|
505
|
+
if (lines.length > 0) {
|
|
506
|
+
if (body.kind === "BlockStatement") {
|
|
507
|
+
return `($matched) => {\n ${lines.join("\n ")}\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
508
|
+
}
|
|
509
|
+
return `($matched) => {\n ${lines.join("\n ")}\n return ${indentContinuation(this.context.emitExpression(body), 4)};\n }`;
|
|
510
|
+
}
|
|
511
|
+
if (body.kind === "BlockStatement") {
|
|
512
|
+
return `() => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
513
|
+
}
|
|
514
|
+
return `() => ${this.context.emitArrowExpression(body)}`;
|
|
515
|
+
}
|
|
516
|
+
if (pat?.kind === "OrPattern" &&
|
|
517
|
+
pat.patterns.every((p) => p.kind === "RecordPattern")) {
|
|
518
|
+
const scrutineeName = scrutinee?.kind === "Identifier" ? scrutinee.name : undefined;
|
|
519
|
+
const first = pat.patterns[0];
|
|
520
|
+
if (first?.kind === "RecordPattern") {
|
|
521
|
+
const signature = this.patternBindingSignature(first);
|
|
522
|
+
const names = this.collectPatternVariables(first);
|
|
523
|
+
const safe = names.length > 0 &&
|
|
524
|
+
new Set(names).size === names.length &&
|
|
525
|
+
(!scrutineeName || !names.includes(scrutineeName)) &&
|
|
526
|
+
pat.patterns.every((alternative) => alternative.kind === "RecordPattern" &&
|
|
527
|
+
this.patternBindingSignature(alternative) === signature &&
|
|
528
|
+
alternative.fields.every((field) => !field.isRest &&
|
|
529
|
+
(!field.pattern ||
|
|
530
|
+
[
|
|
531
|
+
"VariablePattern",
|
|
532
|
+
"WildcardPattern",
|
|
533
|
+
"LiteralPattern",
|
|
534
|
+
].includes(field.pattern.kind))));
|
|
535
|
+
if (safe) {
|
|
536
|
+
const bindings = this.collectRecordDestructuring(first);
|
|
537
|
+
const parameter = `({ ${bindings.join(", ")} }${scrutineeName ? `, ${scrutineeName}` : ""})`;
|
|
538
|
+
return body.kind === "BlockStatement"
|
|
539
|
+
? `${parameter} => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`
|
|
540
|
+
: `${parameter} => ${this.context.emitArrowExpression(body)}`;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
const unpackLines = this.emitPatternUnpackLines(pat, "$matched");
|
|
544
|
+
if (unpackLines.length > 0) {
|
|
545
|
+
const lines = [];
|
|
546
|
+
if (scrutineeName) {
|
|
547
|
+
lines.push(`const ${scrutineeName} = $matched;`);
|
|
548
|
+
}
|
|
549
|
+
lines.push(...unpackLines);
|
|
550
|
+
if (body.kind === "BlockStatement") {
|
|
551
|
+
return `($matched) => {\n ${lines.join("\n ")}\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
552
|
+
}
|
|
553
|
+
return `($matched) => {\n ${lines.join("\n ")}\n return ${indentContinuation(this.context.emitExpression(body), 4)};\n }`;
|
|
554
|
+
}
|
|
555
|
+
if (scrutineeName) {
|
|
556
|
+
if (body.kind === "BlockStatement") {
|
|
557
|
+
return `(${scrutineeName}) => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
558
|
+
}
|
|
559
|
+
return `(${scrutineeName}) => ${this.context.emitArrowExpression(body)}`;
|
|
560
|
+
}
|
|
561
|
+
if (body.kind === "BlockStatement") {
|
|
562
|
+
return `($matched) => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
563
|
+
}
|
|
564
|
+
return `($matched) => ${this.context.emitArrowExpression(body)}`;
|
|
565
|
+
}
|
|
566
|
+
const vars = pat ? this.collectPatternVariables(pat) : [];
|
|
567
|
+
const isOptional = scrutinee
|
|
568
|
+
? this.context.isOptionalValue(scrutinee)
|
|
569
|
+
: false;
|
|
570
|
+
if (pat?.kind === "ConstructorPattern" &&
|
|
571
|
+
JS_ERROR_NAMES.has(pat.constructorName.name)) {
|
|
572
|
+
if (vars.length > 0) {
|
|
573
|
+
const unpack = `const { ${vars.join(", ")} } = $matched;`;
|
|
574
|
+
if (body.kind === "BlockStatement") {
|
|
575
|
+
return `($matched) => {\n ${unpack}\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
576
|
+
}
|
|
577
|
+
return `($matched) => {\n ${unpack}\n return ${indentContinuation(this.context.emitExpression(body), 4)};\n }`;
|
|
578
|
+
}
|
|
579
|
+
if (body.kind === "BlockStatement") {
|
|
580
|
+
return `() => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
581
|
+
}
|
|
582
|
+
return `() => ${this.context.emitArrowExpression(body)}`;
|
|
583
|
+
}
|
|
584
|
+
if (pat?.kind === "ConstructorPattern" &&
|
|
585
|
+
pat.constructorName.name === "Some" &&
|
|
586
|
+
pat.args.length === 1) {
|
|
587
|
+
const arg = pat.args[0];
|
|
588
|
+
if (arg.kind === "RecordPattern" &&
|
|
589
|
+
(this.isSimpleBindingPattern(arg) ||
|
|
590
|
+
patternHasOptionalField(arg, payloadFields))) {
|
|
591
|
+
if (!patternHasOptionalField(arg, payloadFields)) {
|
|
592
|
+
const fields = this.collectRecordDestructuring(arg);
|
|
593
|
+
if (fields.length) {
|
|
594
|
+
const parameter = `({ value: { ${fields.join(", ")} } })`;
|
|
595
|
+
return body.kind === "BlockStatement"
|
|
596
|
+
? `${parameter} => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`
|
|
597
|
+
: `${parameter} => ${this.context.emitArrowExpression(body)}`;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
const unpack = this.emitSomeRecordUnpack(arg, payloadFields, isOptional);
|
|
601
|
+
if (unpack) {
|
|
602
|
+
if (body.kind === "BlockStatement")
|
|
603
|
+
return `($matched) => {\n ${unpack}\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
604
|
+
return `($matched) => {\n ${unpack}\n return ${indentContinuation(this.context.emitExpression(body), 4)};\n }`;
|
|
605
|
+
}
|
|
606
|
+
if (body.kind === "BlockStatement")
|
|
607
|
+
return `() => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
608
|
+
return `() => ${this.context.emitArrowExpression(body)}`;
|
|
609
|
+
}
|
|
610
|
+
if (arg.kind === "LiteralPattern") {
|
|
611
|
+
if (body.kind === "BlockStatement")
|
|
612
|
+
return `() => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
613
|
+
return `() => ${this.context.emitArrowExpression(body)}`;
|
|
614
|
+
}
|
|
615
|
+
if (arg.kind === "ArrayPattern" && this.isSimpleBindingPattern(arg)) {
|
|
616
|
+
const bindingTarget = this.context.emitBindingTarget(arg);
|
|
617
|
+
if (bindingTarget && bindingTarget !== "[]" && bindingTarget !== "{}") {
|
|
618
|
+
const parameter = `({ value: ${bindingTarget} })`;
|
|
619
|
+
if (body.kind === "BlockStatement")
|
|
620
|
+
return `${parameter} => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
621
|
+
return `${parameter} => ${this.context.emitArrowExpression(body)}`;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
else if (vars.length > 0) {
|
|
625
|
+
const unpack = this.emitPatternUnpackLines(pat, "$matched", {
|
|
626
|
+
isOptionalScrutinee: isOptional,
|
|
627
|
+
payloadFields,
|
|
628
|
+
}).join("\n ");
|
|
629
|
+
if (body.kind === "BlockStatement")
|
|
630
|
+
return `($matched) => {\n ${unpack}\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
631
|
+
return `($matched) => {\n ${unpack}\n return ${indentContinuation(this.context.emitExpression(body), 4)};\n }`;
|
|
632
|
+
}
|
|
633
|
+
if (body.kind === "BlockStatement")
|
|
634
|
+
return `() => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
635
|
+
return `() => ${this.context.emitArrowExpression(body)}`;
|
|
636
|
+
}
|
|
637
|
+
if (vars.length > 0 && pat) {
|
|
638
|
+
const unpack = pat.kind === "VariablePattern"
|
|
639
|
+
? `const { ${vars[0]} } = $matched;`
|
|
640
|
+
: this.emitPatternUnpackLines(pat, "$matched", {
|
|
641
|
+
isOptionalScrutinee: isOptional,
|
|
642
|
+
payloadFields,
|
|
643
|
+
}).join("\n ");
|
|
644
|
+
if (body.kind === "BlockStatement") {
|
|
645
|
+
return `($matched) => {\n ${unpack}\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
646
|
+
}
|
|
647
|
+
return `($matched) => {\n ${unpack}\n return ${indentContinuation(this.context.emitExpression(body), 4)};\n }`;
|
|
648
|
+
}
|
|
649
|
+
if (body.kind === "BlockStatement") {
|
|
650
|
+
return `() => {\n ${indentContinuation(this.context.emitFunctionBody(body), 2)}\n }`;
|
|
651
|
+
}
|
|
652
|
+
return `() => ${this.context.emitArrowExpression(body)}`;
|
|
653
|
+
}
|
|
654
|
+
collectPatternVariables(pat) {
|
|
655
|
+
if (pat.kind === "VariablePattern")
|
|
656
|
+
return [pat.name.name];
|
|
657
|
+
if (pat.kind === "ConstructorPattern") {
|
|
658
|
+
const vars = [];
|
|
659
|
+
for (const a of pat.args) {
|
|
660
|
+
vars.push(...this.collectPatternVariables(a));
|
|
661
|
+
}
|
|
662
|
+
return vars;
|
|
663
|
+
}
|
|
664
|
+
if (pat.kind === "OrPattern") {
|
|
665
|
+
return pat.patterns.length > 0
|
|
666
|
+
? this.collectPatternVariables(pat.patterns[0])
|
|
667
|
+
: [];
|
|
668
|
+
}
|
|
669
|
+
if (pat.kind === "RecordPattern") {
|
|
670
|
+
const vars = [];
|
|
671
|
+
for (const f of pat.fields) {
|
|
672
|
+
if (!f.pattern) {
|
|
673
|
+
vars.push(f.key.name);
|
|
674
|
+
}
|
|
675
|
+
else {
|
|
676
|
+
vars.push(...this.collectPatternVariables(f.pattern));
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
return vars;
|
|
680
|
+
}
|
|
681
|
+
if (pat.kind === "ArrayPattern") {
|
|
682
|
+
const vars = [];
|
|
683
|
+
for (const el of pat.elements) {
|
|
684
|
+
vars.push(...this.collectPatternVariables(el));
|
|
685
|
+
}
|
|
686
|
+
return vars;
|
|
687
|
+
}
|
|
688
|
+
if (pat.kind === "RestPattern") {
|
|
689
|
+
return this.collectPatternVariables(pat.argument);
|
|
690
|
+
}
|
|
691
|
+
return [];
|
|
692
|
+
}
|
|
693
|
+
/** Returns true when a pattern can be unpacked with plain JS destructuring. */
|
|
694
|
+
isSimpleBindingPattern(pat) {
|
|
695
|
+
if (pat.kind === "WildcardPattern" ||
|
|
696
|
+
pat.kind === "LiteralPattern" ||
|
|
697
|
+
pat.kind === "VariablePattern") {
|
|
698
|
+
return true;
|
|
699
|
+
}
|
|
700
|
+
if (pat.kind === "RestPattern") {
|
|
701
|
+
return this.isSimpleBindingPattern(pat.argument);
|
|
702
|
+
}
|
|
703
|
+
if (pat.kind === "ArrayPattern") {
|
|
704
|
+
return pat.elements.every((el) => this.isSimpleBindingPattern(el));
|
|
705
|
+
}
|
|
706
|
+
if (pat.kind === "RecordPattern") {
|
|
707
|
+
return pat.fields.every((f) => f.isRest || !f.pattern || this.isSimpleBindingPattern(f.pattern));
|
|
708
|
+
}
|
|
709
|
+
return false;
|
|
710
|
+
}
|
|
711
|
+
/** Produces a structural signature of where variables are bound in a pattern. */
|
|
712
|
+
patternBindingSignature(pat) {
|
|
713
|
+
if (pat.kind === "VariablePattern")
|
|
714
|
+
return `VAR:${pat.name.name}`;
|
|
715
|
+
if (pat.kind === "WildcardPattern" || pat.kind === "LiteralPattern") {
|
|
716
|
+
return "_";
|
|
717
|
+
}
|
|
718
|
+
if (pat.kind === "RestPattern") {
|
|
719
|
+
return `...${this.patternBindingSignature(pat.argument)}`;
|
|
720
|
+
}
|
|
721
|
+
if (pat.kind === "ArrayPattern") {
|
|
722
|
+
return `[${pat.elements
|
|
723
|
+
.map((el, i) => this.collectPatternVariables(el).length > 0
|
|
724
|
+
? `${i}:${this.patternBindingSignature(el)}`
|
|
725
|
+
: "_")
|
|
726
|
+
.join(",")}]`;
|
|
727
|
+
}
|
|
728
|
+
if (pat.kind === "RecordPattern") {
|
|
729
|
+
const entries = pat.fields
|
|
730
|
+
.filter((f) => f.isRest ||
|
|
731
|
+
!f.pattern ||
|
|
732
|
+
this.collectPatternVariables(f.pattern).length > 0)
|
|
733
|
+
.map((f) => f.isRest
|
|
734
|
+
? `...${f.key.name}`
|
|
735
|
+
: `${f.key.name}:${f.pattern ? this.patternBindingSignature(f.pattern) : `VAR:${f.key.name}`}`)
|
|
736
|
+
.sort();
|
|
737
|
+
return `{${entries.join(",")}}`;
|
|
738
|
+
}
|
|
739
|
+
if (pat.kind === "ConstructorPattern") {
|
|
740
|
+
return `${pat.constructorName.name}(${pat.args.map((a) => this.patternBindingSignature(a)).join(",")})`;
|
|
741
|
+
}
|
|
742
|
+
if (pat.kind === "OrPattern") {
|
|
743
|
+
return `OR(${pat.patterns.map((p) => this.patternBindingSignature(p)).join("|")})`;
|
|
744
|
+
}
|
|
745
|
+
return "_";
|
|
746
|
+
}
|
|
747
|
+
/** Emits destructuring and nested unpack lines for a record pattern. */
|
|
748
|
+
emitRecordPatternUnpack(pat, targetExpr, nextTemp = (() => {
|
|
749
|
+
let idx = 0;
|
|
750
|
+
return () => `__match${idx++}`;
|
|
751
|
+
})()) {
|
|
752
|
+
if (this.isSimpleBindingPattern(pat)) {
|
|
753
|
+
const destructParts = this.collectRecordDestructuring(pat);
|
|
754
|
+
return destructParts.length > 0
|
|
755
|
+
? [`const { ${destructParts.join(", ")} } = ${targetExpr};`]
|
|
756
|
+
: [];
|
|
757
|
+
}
|
|
758
|
+
const parts = [];
|
|
759
|
+
const nestedLines = [];
|
|
760
|
+
const hasRest = pat.fields.some((f) => f.isRest);
|
|
761
|
+
for (const f of pat.fields) {
|
|
762
|
+
if (f.isRest) {
|
|
763
|
+
parts.push(`...${f.key.name}`);
|
|
764
|
+
}
|
|
765
|
+
else if (!f.pattern) {
|
|
766
|
+
parts.push(f.key.name);
|
|
767
|
+
}
|
|
768
|
+
else if (f.pattern.kind === "VariablePattern") {
|
|
769
|
+
parts.push(`${f.key.name}: ${f.pattern.name.name}`);
|
|
770
|
+
}
|
|
771
|
+
else if (this.isSimpleBindingPattern(f.pattern)) {
|
|
772
|
+
if (f.pattern.kind === "RecordPattern") {
|
|
773
|
+
const sub = this.collectRecordDestructuring(f.pattern);
|
|
774
|
+
if (sub.length > 0) {
|
|
775
|
+
parts.push(`${f.key.name}: { ${sub.join(", ")} }`);
|
|
776
|
+
}
|
|
777
|
+
else if (hasRest) {
|
|
778
|
+
parts.push(`${f.key.name}: _${f.key.name}`);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
else if (f.pattern.kind === "ArrayPattern") {
|
|
782
|
+
const target = this.context.emitBindingTarget(f.pattern);
|
|
783
|
+
if (target && target !== "[]") {
|
|
784
|
+
parts.push(`${f.key.name}: ${target}`);
|
|
785
|
+
}
|
|
786
|
+
else if (hasRest) {
|
|
787
|
+
parts.push(`${f.key.name}: _${f.key.name}`);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
else if (hasRest) {
|
|
791
|
+
parts.push(`${f.key.name}: _${f.key.name}`);
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
else if (this.collectPatternVariables(f.pattern).length > 0) {
|
|
795
|
+
const tmp = nextTemp();
|
|
796
|
+
parts.push(`${f.key.name}: ${tmp}`);
|
|
797
|
+
nestedLines.push(...this.emitPatternUnpackLines(f.pattern, tmp, { nextTemp }));
|
|
798
|
+
}
|
|
799
|
+
else if (hasRest) {
|
|
800
|
+
parts.push(`${f.key.name}: _${f.key.name}`);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
if (parts.length > 0) {
|
|
804
|
+
return [`const { ${parts.join(", ")} } = ${targetExpr};`, ...nestedLines];
|
|
805
|
+
}
|
|
806
|
+
return nestedLines;
|
|
807
|
+
}
|
|
808
|
+
/** Recursively unpacks bindings from any match pattern against `targetExpr`. */
|
|
809
|
+
emitPatternUnpackLines(pat, targetExpr, options) {
|
|
810
|
+
let tempCounter = 0;
|
|
811
|
+
const nextTemp = options?.nextTemp ?? (() => `__match${tempCounter++}`);
|
|
812
|
+
const vars = this.collectPatternVariables(pat);
|
|
813
|
+
if (vars.length === 0) {
|
|
814
|
+
return [];
|
|
815
|
+
}
|
|
816
|
+
if (pat.kind === "VariablePattern") {
|
|
817
|
+
return [`const ${pat.name.name} = ${targetExpr};`];
|
|
818
|
+
}
|
|
819
|
+
if (pat.kind === "RestPattern") {
|
|
820
|
+
return this.emitPatternUnpackLines(pat.argument, targetExpr, {
|
|
821
|
+
nextTemp,
|
|
822
|
+
});
|
|
823
|
+
}
|
|
824
|
+
if (pat.kind === "RecordPattern") {
|
|
825
|
+
return this.emitRecordPatternUnpack(pat, targetExpr, nextTemp);
|
|
826
|
+
}
|
|
827
|
+
if (pat.kind === "ArrayPattern") {
|
|
828
|
+
const lines = [];
|
|
829
|
+
for (let i = 0; i < pat.elements.length; i++) {
|
|
830
|
+
const el = pat.elements[i];
|
|
831
|
+
const elemExpr = `${targetExpr}[${i}]`;
|
|
832
|
+
if (el.kind === "RestPattern") {
|
|
833
|
+
if (el.argument.kind === "VariablePattern") {
|
|
834
|
+
lines.push(`const ${el.argument.name.name} = ${targetExpr}.slice(${i});`);
|
|
835
|
+
}
|
|
836
|
+
else if (this.collectPatternVariables(el.argument).length > 0) {
|
|
837
|
+
const tmp = nextTemp();
|
|
838
|
+
lines.push(`const ${tmp} = ${targetExpr}.slice(${i});`);
|
|
839
|
+
lines.push(...this.emitPatternUnpackLines(el.argument, tmp, { nextTemp }));
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
else if (this.collectPatternVariables(el).length > 0) {
|
|
843
|
+
lines.push(...this.emitPatternUnpackLines(el, elemExpr, { nextTemp }));
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
return lines;
|
|
847
|
+
}
|
|
848
|
+
if (pat.kind === "OrPattern") {
|
|
849
|
+
const first = pat.patterns[0];
|
|
850
|
+
if (!first)
|
|
851
|
+
return [];
|
|
852
|
+
const firstSig = this.patternBindingSignature(first);
|
|
853
|
+
const allSameShape = pat.patterns.every((p) => this.patternBindingSignature(p) === firstSig);
|
|
854
|
+
if (allSameShape) {
|
|
855
|
+
return this.emitPatternUnpackLines(first, targetExpr, {
|
|
856
|
+
...options,
|
|
857
|
+
nextTemp,
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
const uniqueVars = Array.from(new Set(vars));
|
|
861
|
+
const branches = pat.patterns
|
|
862
|
+
.map((alt) => {
|
|
863
|
+
const orVar = nextTemp();
|
|
864
|
+
const altLines = this.emitPatternUnpackLines(alt, orVar, {
|
|
865
|
+
nextTemp,
|
|
866
|
+
});
|
|
867
|
+
const bodyLines = [
|
|
868
|
+
...altLines,
|
|
869
|
+
`return { ${uniqueVars.join(", ")} };`,
|
|
870
|
+
];
|
|
871
|
+
return `.with(${this.emitPattern(alt)}, (${orVar}) => {\n ${bodyLines.join("\n ")}\n })`;
|
|
872
|
+
})
|
|
873
|
+
.join("\n ");
|
|
874
|
+
return [
|
|
875
|
+
`const { ${uniqueVars.join(", ")} } = match(${targetExpr})\n ${branches}\n .otherwise(() => ({}));`,
|
|
876
|
+
];
|
|
877
|
+
}
|
|
878
|
+
if (pat.kind === "ConstructorPattern") {
|
|
879
|
+
if (JS_ERROR_NAMES.has(pat.constructorName.name)) {
|
|
880
|
+
return [`const { ${vars.join(", ")} } = ${targetExpr};`];
|
|
881
|
+
}
|
|
882
|
+
if (pat.constructorName.name === "Some" && pat.args.length === 1) {
|
|
883
|
+
const arg = pat.args[0];
|
|
884
|
+
const cast = "";
|
|
885
|
+
if (arg.kind === "RecordPattern" &&
|
|
886
|
+
targetExpr === "$matched" &&
|
|
887
|
+
patternHasOptionalField(arg, options?.payloadFields)) {
|
|
888
|
+
return [
|
|
889
|
+
this.emitSomeRecordUnpack(arg, options?.payloadFields, options?.isOptionalScrutinee),
|
|
890
|
+
].filter(Boolean);
|
|
891
|
+
}
|
|
892
|
+
if (arg.kind === "VariablePattern") {
|
|
893
|
+
return [`const { value: ${arg.name.name} } = ${targetExpr}${cast};`];
|
|
894
|
+
}
|
|
895
|
+
if ((arg.kind === "RecordPattern" || arg.kind === "ArrayPattern") &&
|
|
896
|
+
this.isSimpleBindingPattern(arg)) {
|
|
897
|
+
const bindingTarget = this.context.emitBindingTarget(arg);
|
|
898
|
+
if (bindingTarget &&
|
|
899
|
+
bindingTarget !== "[]" &&
|
|
900
|
+
bindingTarget !== "{}" &&
|
|
901
|
+
bindingTarget !== "_") {
|
|
902
|
+
return [
|
|
903
|
+
`const { value: ${bindingTarget} } = ${targetExpr}${cast};`,
|
|
904
|
+
];
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
const tmp = nextTemp();
|
|
908
|
+
return [
|
|
909
|
+
`const { value: ${tmp} } = ${targetExpr}${cast};`,
|
|
910
|
+
...this.emitPatternUnpackLines(arg, tmp, { nextTemp }),
|
|
911
|
+
];
|
|
912
|
+
}
|
|
913
|
+
const fieldNames = this.context.getVariantFields?.(pat.constructorName.name);
|
|
914
|
+
if (fieldNames && fieldNames.length === pat.args.length) {
|
|
915
|
+
const parts = [];
|
|
916
|
+
const nestedLines = [];
|
|
917
|
+
for (let i = 0; i < fieldNames.length; i++) {
|
|
918
|
+
const arg = pat.args[i];
|
|
919
|
+
if (this.collectPatternVariables(arg).length === 0)
|
|
920
|
+
continue;
|
|
921
|
+
if (arg.kind === "VariablePattern") {
|
|
922
|
+
parts.push(`${fieldNames[i]}: ${arg.name.name}`);
|
|
923
|
+
}
|
|
924
|
+
else if (arg.kind === "RecordPattern" &&
|
|
925
|
+
this.isSimpleBindingPattern(arg)) {
|
|
926
|
+
const sub = this.collectRecordDestructuring(arg);
|
|
927
|
+
if (sub.length > 0) {
|
|
928
|
+
parts.push(`${fieldNames[i]}: { ${sub.join(", ")} }`);
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
else if (arg.kind === "ArrayPattern" &&
|
|
932
|
+
this.isSimpleBindingPattern(arg)) {
|
|
933
|
+
const target = this.context.emitBindingTarget(arg);
|
|
934
|
+
if (target && target !== "[]") {
|
|
935
|
+
parts.push(`${fieldNames[i]}: ${target}`);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
else {
|
|
939
|
+
const tmp = nextTemp();
|
|
940
|
+
parts.push(`${fieldNames[i]}: ${tmp}`);
|
|
941
|
+
nestedLines.push(...this.emitPatternUnpackLines(arg, tmp, { nextTemp }));
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
if (parts.length > 0) {
|
|
945
|
+
return [
|
|
946
|
+
`const { ${parts.join(", ")} } = ${targetExpr};`,
|
|
947
|
+
...nestedLines,
|
|
948
|
+
];
|
|
949
|
+
}
|
|
950
|
+
return nestedLines;
|
|
951
|
+
}
|
|
952
|
+
const fieldsVar = nextTemp();
|
|
953
|
+
const argTargets = [];
|
|
954
|
+
const nestedLines = [];
|
|
955
|
+
for (const arg of pat.args) {
|
|
956
|
+
if (this.collectPatternVariables(arg).length === 0) {
|
|
957
|
+
argTargets.push("");
|
|
958
|
+
}
|
|
959
|
+
else if (arg.kind === "VariablePattern") {
|
|
960
|
+
argTargets.push(arg.name.name);
|
|
961
|
+
}
|
|
962
|
+
else {
|
|
963
|
+
const tmp = nextTemp();
|
|
964
|
+
argTargets.push(tmp);
|
|
965
|
+
nestedLines.push(...this.emitPatternUnpackLines(arg, tmp, { nextTemp }));
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
return [
|
|
969
|
+
`const { _tag: _, ...${fieldsVar} } = ${targetExpr};`,
|
|
970
|
+
`const [${argTargets.join(", ")}] = Object.values(${fieldsVar});`,
|
|
971
|
+
...nestedLines,
|
|
972
|
+
];
|
|
973
|
+
}
|
|
974
|
+
return [];
|
|
975
|
+
}
|
|
976
|
+
emitIfElseMatch(arms, getCondition) {
|
|
977
|
+
const hasBlockArm = arms.some((a) => a.body.kind === "BlockStatement");
|
|
978
|
+
if (!hasBlockArm) {
|
|
979
|
+
let code = "";
|
|
980
|
+
for (let i = 0; i < arms.length; i++) {
|
|
981
|
+
const arm = arms[i];
|
|
982
|
+
const isDefault = (arm.pattern.kind === "WildcardPattern" ||
|
|
983
|
+
arm.pattern.kind === "VariablePattern") &&
|
|
984
|
+
!arm.guard;
|
|
985
|
+
const body = this.context.emitExpression(arm.body);
|
|
986
|
+
if (isDefault) {
|
|
987
|
+
code += body;
|
|
988
|
+
break;
|
|
989
|
+
}
|
|
990
|
+
if (i === arms.length - 1) {
|
|
991
|
+
code += `${getCondition(arm)} ? ${body} : (() => { throw new Error("Non-exhaustive match"); })()`;
|
|
992
|
+
break;
|
|
993
|
+
}
|
|
994
|
+
code += `${getCondition(arm)} ? ${body} : `;
|
|
995
|
+
}
|
|
996
|
+
return `(${code})`;
|
|
997
|
+
}
|
|
998
|
+
const branches = [];
|
|
999
|
+
let hasDefault = false;
|
|
1000
|
+
for (let i = 0; i < arms.length; i++) {
|
|
1001
|
+
const arm = arms[i];
|
|
1002
|
+
const isDefault = (arm.pattern.kind === "WildcardPattern" ||
|
|
1003
|
+
arm.pattern.kind === "VariablePattern") &&
|
|
1004
|
+
!arm.guard;
|
|
1005
|
+
const inner = arm.body.kind === "BlockStatement"
|
|
1006
|
+
? this.context.emitFunctionBody(arm.body)
|
|
1007
|
+
: ` return ${this.context.emitExpression(arm.body)};`;
|
|
1008
|
+
if (i === 0) {
|
|
1009
|
+
if (isDefault) {
|
|
1010
|
+
hasDefault = true;
|
|
1011
|
+
branches.push(inner);
|
|
1012
|
+
break;
|
|
1013
|
+
}
|
|
1014
|
+
branches.push(`if (${getCondition(arm)}) {\n${inner}\n}`);
|
|
1015
|
+
}
|
|
1016
|
+
else if (isDefault) {
|
|
1017
|
+
hasDefault = true;
|
|
1018
|
+
branches.push(`else {\n${inner}\n}`);
|
|
1019
|
+
break;
|
|
1020
|
+
}
|
|
1021
|
+
else {
|
|
1022
|
+
branches.push(`else if (${getCondition(arm)}) {\n${inner}\n}`);
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
if (!hasDefault) {
|
|
1026
|
+
branches.push(`else {\n throw new Error("Non-exhaustive match");\n}`);
|
|
1027
|
+
}
|
|
1028
|
+
return `(() => {\n ${branches.join(" ")}\n})()`;
|
|
1029
|
+
}
|
|
1030
|
+
emitCatchExpression(expr) {
|
|
1031
|
+
this.context.addEffectImport?.("Effect");
|
|
1032
|
+
const collectCases = (pattern, body) => {
|
|
1033
|
+
if (pattern.kind === "OrPattern") {
|
|
1034
|
+
return pattern.patterns.flatMap((part) => collectCases(part, body));
|
|
1035
|
+
}
|
|
1036
|
+
if (pattern.kind !== "ConstructorPattern") {
|
|
1037
|
+
throw new Error("Invalid catch arm");
|
|
1038
|
+
}
|
|
1039
|
+
const arg = pattern.args[0];
|
|
1040
|
+
const binding = arg?.kind === "VariablePattern" ? arg.name.name : "";
|
|
1041
|
+
return [
|
|
1042
|
+
`${JSON.stringify(pattern.constructorName.name)}: (${binding}) => {\n${body}\n}`,
|
|
1043
|
+
];
|
|
1044
|
+
};
|
|
1045
|
+
const cases = expr.arms.flatMap((arm) => collectCases(arm.pattern, this.context.emitFunctionBody(arm.body)));
|
|
1046
|
+
const handlers = `{\n${cases.join(",\n")}\n}`;
|
|
1047
|
+
if (expr.isPipeline || !expr.scrutinee) {
|
|
1048
|
+
return `(__catchInput) => Effect.catchTags(__catchInput, ${handlers})`;
|
|
1049
|
+
}
|
|
1050
|
+
return `Effect.catchTags(${this.context.emitExpression(expr.scrutinee)}, ${handlers})`;
|
|
1051
|
+
}
|
|
1052
|
+
emitJSNullishMatch(scrutinee, arms) {
|
|
1053
|
+
if (!arms.length)
|
|
1054
|
+
throw new FunScriptError("JSNullish match requires at least one arm", scrutinee?.span ?? {
|
|
1055
|
+
start: { line: 1, column: 1, offset: 0 },
|
|
1056
|
+
end: { line: 1, column: 1, offset: 0 },
|
|
1057
|
+
});
|
|
1058
|
+
// Always capture once: a property getter or foreign expression may be effectful.
|
|
1059
|
+
const value = "$jsNullish";
|
|
1060
|
+
const preamble = scrutinee
|
|
1061
|
+
? `const ${value} = ${this.context.emitExpression(scrutinee)};`
|
|
1062
|
+
: "";
|
|
1063
|
+
const branches = [];
|
|
1064
|
+
let coversSuccess = false;
|
|
1065
|
+
let coversNull = false;
|
|
1066
|
+
let coversUndefined = false;
|
|
1067
|
+
let coversAll = false;
|
|
1068
|
+
let terminated = false;
|
|
1069
|
+
for (const arm of arms) {
|
|
1070
|
+
const pat = arm.pattern;
|
|
1071
|
+
let condition;
|
|
1072
|
+
let binding;
|
|
1073
|
+
let caseKind;
|
|
1074
|
+
if (pat.kind === "WildcardPattern" || pat.kind === "VariablePattern") {
|
|
1075
|
+
condition = "true";
|
|
1076
|
+
caseKind = "all";
|
|
1077
|
+
if (pat.kind === "VariablePattern")
|
|
1078
|
+
binding = pat.name.name;
|
|
1079
|
+
}
|
|
1080
|
+
else if (pat.kind === "ConstructorPattern" &&
|
|
1081
|
+
pat.constructorName.name === "Success") {
|
|
1082
|
+
const arg = pat.args[0];
|
|
1083
|
+
if (pat.args.length !== 1 ||
|
|
1084
|
+
(arg.kind !== "VariablePattern" && arg.kind !== "WildcardPattern")) {
|
|
1085
|
+
throw new FunScriptError("JSNullish Success expects one variable or '_' pattern", pat.span);
|
|
1086
|
+
}
|
|
1087
|
+
condition = `(${value} !== null && ${value} !== undefined)`;
|
|
1088
|
+
caseKind = "success";
|
|
1089
|
+
if (arg.kind === "VariablePattern")
|
|
1090
|
+
binding = arg.name.name;
|
|
1091
|
+
}
|
|
1092
|
+
else if (pat.kind === "ConstructorPattern" &&
|
|
1093
|
+
pat.constructorName.name === "Failure") {
|
|
1094
|
+
const arg = pat.args[0];
|
|
1095
|
+
if (pat.args.length !== 1) {
|
|
1096
|
+
throw new FunScriptError("JSNullish Failure expects Null, Undefined, a variable, or '_'", pat.span);
|
|
1097
|
+
}
|
|
1098
|
+
if (arg.kind === "ConstructorPattern" &&
|
|
1099
|
+
arg.args.length === 0 &&
|
|
1100
|
+
arg.constructorName.name === "Null") {
|
|
1101
|
+
condition = `(${value} === null)`;
|
|
1102
|
+
caseKind = "null";
|
|
1103
|
+
}
|
|
1104
|
+
else if (arg.kind === "ConstructorPattern" &&
|
|
1105
|
+
arg.args.length === 0 &&
|
|
1106
|
+
arg.constructorName.name === "Undefined") {
|
|
1107
|
+
condition = `(${value} === undefined)`;
|
|
1108
|
+
caseKind = "undefined";
|
|
1109
|
+
}
|
|
1110
|
+
else if (arg.kind === "VariablePattern" ||
|
|
1111
|
+
arg.kind === "WildcardPattern") {
|
|
1112
|
+
condition = `(${value} === null || ${value} === undefined)`;
|
|
1113
|
+
caseKind = "failure";
|
|
1114
|
+
if (arg.kind === "VariablePattern")
|
|
1115
|
+
binding = arg.name.name;
|
|
1116
|
+
}
|
|
1117
|
+
else {
|
|
1118
|
+
throw new FunScriptError("JSNullish Failure expects Null, Undefined, a variable, or '_'", pat.span);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
else {
|
|
1122
|
+
throw new FunScriptError("Match JSNullish using Success(value), Failure(Null), Failure(Undefined), or '_'", pat.span);
|
|
1123
|
+
}
|
|
1124
|
+
if (arm.guard) {
|
|
1125
|
+
const guard = this.context.emitExpression(arm.guard);
|
|
1126
|
+
// Bind the narrowed value inside an IIFE so TypeScript preserves its
|
|
1127
|
+
// narrowed type without triggering TS7006 on an unannotated arrow param.
|
|
1128
|
+
const guardExpr = binding
|
|
1129
|
+
? `(() => {\n const ${binding} = ${value};\n return (${guard});\n})()`
|
|
1130
|
+
: `(${guard})`;
|
|
1131
|
+
condition =
|
|
1132
|
+
condition === "true" ? guardExpr : `${condition} && ${guardExpr}`;
|
|
1133
|
+
}
|
|
1134
|
+
else {
|
|
1135
|
+
coversSuccess ||= caseKind === "success" || caseKind === "all";
|
|
1136
|
+
coversNull ||=
|
|
1137
|
+
caseKind === "null" || caseKind === "failure" || caseKind === "all";
|
|
1138
|
+
coversUndefined ||=
|
|
1139
|
+
caseKind === "undefined" ||
|
|
1140
|
+
caseKind === "failure" ||
|
|
1141
|
+
caseKind === "all";
|
|
1142
|
+
coversAll ||= caseKind === "all";
|
|
1143
|
+
}
|
|
1144
|
+
const body = this.context.emitFunctionBody(arm.body);
|
|
1145
|
+
const complete = !arm.guard && coversSuccess && coversNull && coversUndefined;
|
|
1146
|
+
const bindingValue = complete && caseKind === "failure"
|
|
1147
|
+
? `${value} as null | undefined`
|
|
1148
|
+
: value;
|
|
1149
|
+
const bindingLine = binding
|
|
1150
|
+
? ` const ${binding} = ${bindingValue};\n`
|
|
1151
|
+
: "";
|
|
1152
|
+
if ((caseKind === "all" || complete) && !arm.guard) {
|
|
1153
|
+
branches.push(`${branches.length ? "else " : ""}{\n${bindingLine}${body}\n}`);
|
|
1154
|
+
terminated = true;
|
|
1155
|
+
break;
|
|
1156
|
+
}
|
|
1157
|
+
branches.push(`${branches.length ? "else " : ""}if (${condition}) {\n${bindingLine}${body}\n}`);
|
|
1158
|
+
}
|
|
1159
|
+
if (terminated || coversAll) {
|
|
1160
|
+
// A wildcard arm emitted an unconditional final branch.
|
|
1161
|
+
}
|
|
1162
|
+
else if (!(coversSuccess && coversNull && coversUndefined)) {
|
|
1163
|
+
branches.push(`else {\n const __nonExhaustive: never = ${value};\n void __nonExhaustive;\n throw new Error("Non-exhaustive JSNullish match");\n}`);
|
|
1164
|
+
}
|
|
1165
|
+
else {
|
|
1166
|
+
// Even with all cases covered, retain a final assertion so TypeScript can
|
|
1167
|
+
// infer a non-optional return type for separately tested conditions.
|
|
1168
|
+
branches.push(`else {\n const __exhaustive: never = ${value};\n return __exhaustive;\n}`);
|
|
1169
|
+
}
|
|
1170
|
+
const bodyLines = preamble
|
|
1171
|
+
? ` ${preamble}\n ${branches.join(" ")}`
|
|
1172
|
+
: ` ${branches.join(" ")}`;
|
|
1173
|
+
return scrutinee
|
|
1174
|
+
? `(() => {\n${bodyLines}\n})()`
|
|
1175
|
+
: `(${value}) => {\n${bodyLines}\n}`;
|
|
1176
|
+
}
|
|
1177
|
+
emitJSOptionalMatch(scrutinee, arms) {
|
|
1178
|
+
const scrutineeId = scrutinee.kind === "Identifier" ? scrutinee.name : "$jsOpt";
|
|
1179
|
+
const preamble = scrutinee.kind === "Identifier"
|
|
1180
|
+
? ""
|
|
1181
|
+
: `const $jsOpt = ${this.context.emitExpression(scrutinee)};\n `;
|
|
1182
|
+
const hasWildcard = arms.some((a) => (a.pattern.kind === "WildcardPattern" ||
|
|
1183
|
+
a.pattern.kind === "VariablePattern") &&
|
|
1184
|
+
!a.guard);
|
|
1185
|
+
const hasSome = arms.some((a) => a.pattern.kind === "ConstructorPattern" &&
|
|
1186
|
+
a.pattern.constructorName.name === "Some" &&
|
|
1187
|
+
!a.guard);
|
|
1188
|
+
const hasFullNone = arms.some((a) => a.pattern.kind === "ConstructorPattern" &&
|
|
1189
|
+
a.pattern.constructorName.name === "None" &&
|
|
1190
|
+
!a.guard &&
|
|
1191
|
+
((a.pattern.args ?? []).length === 0 ||
|
|
1192
|
+
a.pattern.args[0]?.kind === "WildcardPattern"));
|
|
1193
|
+
const hasNull = arms.some((a) => a.pattern.kind === "ConstructorPattern" &&
|
|
1194
|
+
a.pattern.constructorName.name === "None" &&
|
|
1195
|
+
!a.guard &&
|
|
1196
|
+
((a.pattern.args?.[0]?.kind === "VariablePattern" &&
|
|
1197
|
+
a.pattern.args[0].name.name === "null") ||
|
|
1198
|
+
(a.pattern.args?.[0]?.kind === "ConstructorPattern" &&
|
|
1199
|
+
a.pattern.args[0].constructorName.name === "Null")));
|
|
1200
|
+
const hasUndefined = arms.some((a) => a.pattern.kind === "ConstructorPattern" &&
|
|
1201
|
+
a.pattern.constructorName.name === "None" &&
|
|
1202
|
+
!a.guard &&
|
|
1203
|
+
((a.pattern.args?.[0]?.kind === "VariablePattern" &&
|
|
1204
|
+
a.pattern.args[0].name.name === "undefined") ||
|
|
1205
|
+
(a.pattern.args?.[0]?.kind === "ConstructorPattern" &&
|
|
1206
|
+
a.pattern.args[0].constructorName.name === "Undefined")));
|
|
1207
|
+
const isExhaustive = hasWildcard || (hasSome && (hasFullNone || (hasNull && hasUndefined)));
|
|
1208
|
+
const branches = [];
|
|
1209
|
+
for (let i = 0; i < arms.length; i++) {
|
|
1210
|
+
const arm = arms[i];
|
|
1211
|
+
const pat = arm.pattern;
|
|
1212
|
+
const isWildcard = (pat.kind === "WildcardPattern" || pat.kind === "VariablePattern") &&
|
|
1213
|
+
!arm.guard;
|
|
1214
|
+
const bindings = [];
|
|
1215
|
+
let cond = "true";
|
|
1216
|
+
if (pat.kind === "ConstructorPattern") {
|
|
1217
|
+
if (pat.constructorName.name === "Some") {
|
|
1218
|
+
cond = `(${scrutineeId} !== null && ${scrutineeId} !== undefined)`;
|
|
1219
|
+
if (pat.args.length === 1 && pat.args[0].kind === "VariablePattern") {
|
|
1220
|
+
bindings.push(`const ${pat.args[0].name.name} = ${scrutineeId};`);
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
else if (pat.constructorName.name === "None") {
|
|
1224
|
+
const arg = pat.args[0];
|
|
1225
|
+
if ((arg?.kind === "VariablePattern" &&
|
|
1226
|
+
arg.name.name === "undefined") ||
|
|
1227
|
+
(arg?.kind === "ConstructorPattern" &&
|
|
1228
|
+
arg.constructorName.name === "Undefined")) {
|
|
1229
|
+
cond = `(${scrutineeId} === undefined)`;
|
|
1230
|
+
}
|
|
1231
|
+
else if ((arg?.kind === "VariablePattern" && arg.name.name === "null") ||
|
|
1232
|
+
(arg?.kind === "ConstructorPattern" &&
|
|
1233
|
+
arg.constructorName.name === "Null")) {
|
|
1234
|
+
cond = `(${scrutineeId} === null)`;
|
|
1235
|
+
}
|
|
1236
|
+
else {
|
|
1237
|
+
cond = `(${scrutineeId} === null || ${scrutineeId} === undefined)`;
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
else if (pat.kind === "VariablePattern") {
|
|
1242
|
+
bindings.push(`const ${pat.name.name} = ${scrutineeId};`);
|
|
1243
|
+
}
|
|
1244
|
+
if (arm.guard) {
|
|
1245
|
+
const g = this.context.emitExpression(arm.guard);
|
|
1246
|
+
const guard = bindings.length
|
|
1247
|
+
? `(() => { ${bindings.join(" ")} return (${g}); })()`
|
|
1248
|
+
: `(${g})`;
|
|
1249
|
+
cond = cond === "true" ? guard : `(${cond} && ${guard})`;
|
|
1250
|
+
}
|
|
1251
|
+
const bodyCode = arm.body.kind === "BlockStatement"
|
|
1252
|
+
? this.context.emitFunctionBody(arm.body)
|
|
1253
|
+
: ` return ${this.context.emitExpression(arm.body)};`;
|
|
1254
|
+
const inner = bindings.length > 0
|
|
1255
|
+
? ` ${bindings.join("\n ")}\n${bodyCode}`
|
|
1256
|
+
: bodyCode;
|
|
1257
|
+
const isLast = i === arms.length - 1;
|
|
1258
|
+
if (i === 0) {
|
|
1259
|
+
if (isWildcard) {
|
|
1260
|
+
branches.push(inner);
|
|
1261
|
+
break;
|
|
1262
|
+
}
|
|
1263
|
+
branches.push(`if (${cond}) {\n${inner}\n}`);
|
|
1264
|
+
}
|
|
1265
|
+
else if (isWildcard ||
|
|
1266
|
+
(isLast && !arm.guard && pat.kind !== "ConstructorPattern")) {
|
|
1267
|
+
branches.push(`else {\n${inner}\n}`);
|
|
1268
|
+
break;
|
|
1269
|
+
}
|
|
1270
|
+
else if (isExhaustive &&
|
|
1271
|
+
isLast &&
|
|
1272
|
+
!arm.guard &&
|
|
1273
|
+
pat.kind === "ConstructorPattern") {
|
|
1274
|
+
branches.push(`else {\n${inner}\n}`);
|
|
1275
|
+
break;
|
|
1276
|
+
}
|
|
1277
|
+
else {
|
|
1278
|
+
branches.push(`else if (${cond}) {\n${inner}\n}`);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
if (!isExhaustive) {
|
|
1282
|
+
branches.push(`else {\n const __nonExhaustive: never = ${scrutineeId};\n return __nonExhaustive;\n}`);
|
|
1283
|
+
}
|
|
1284
|
+
return `(() => {\n ${preamble}${branches.join(" ")}\n})()`;
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
//# sourceMappingURL=match.js.map
|