funkscript 0.0.0-stage → 0.2.3
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,1113 @@
|
|
|
1
|
+
import { renderExportFrom, renderImport } from "../emitter/imports.js";
|
|
2
|
+
import { Lexer } from "../lexer/lexer.js";
|
|
3
|
+
import { KEYWORDS, BANNED_KEYWORDS } from "../lexer/tokens.js";
|
|
4
|
+
import { Parser } from "../parser/parser.js";
|
|
5
|
+
import { convertPipelineStyle } from "../tooling/pipeline-style.js";
|
|
6
|
+
import { ASSOCIATIVITY, PRECEDENCE } from "./operator-precedence.js";
|
|
7
|
+
import { CommentAttachments, } from "./comments.js";
|
|
8
|
+
export { extractComments } from "./comments.js";
|
|
9
|
+
const RESERVED_IDENTIFIERS = new Set([
|
|
10
|
+
...Object.keys(KEYWORDS),
|
|
11
|
+
...Object.keys(BANNED_KEYWORDS),
|
|
12
|
+
]);
|
|
13
|
+
export class Formatter {
|
|
14
|
+
source;
|
|
15
|
+
ast;
|
|
16
|
+
indentSize;
|
|
17
|
+
lineWidth;
|
|
18
|
+
leadingComments = new Map();
|
|
19
|
+
trailingComments = new Map();
|
|
20
|
+
eofComments = [];
|
|
21
|
+
constructor(source, ast, options) {
|
|
22
|
+
this.source = source;
|
|
23
|
+
this.ast = ast;
|
|
24
|
+
this.indentSize = options?.indentSize ?? 2;
|
|
25
|
+
this.lineWidth = options?.lineWidth ?? 100;
|
|
26
|
+
const comments = new CommentAttachments(ast, source);
|
|
27
|
+
this.leadingComments = comments.leadingComments;
|
|
28
|
+
this.trailingComments = comments.trailingComments;
|
|
29
|
+
this.eofComments = comments.eofComments;
|
|
30
|
+
}
|
|
31
|
+
spaces(count) {
|
|
32
|
+
return " ".repeat(Math.max(0, count));
|
|
33
|
+
}
|
|
34
|
+
escapeStringPart(val) {
|
|
35
|
+
return val
|
|
36
|
+
.replace(/\\/g, "\\\\")
|
|
37
|
+
.replace(/"/g, '\\"')
|
|
38
|
+
.replace(/\n/g, "\\n")
|
|
39
|
+
.replace(/\r/g, "\\r")
|
|
40
|
+
.replace(/\t/g, "\\t")
|
|
41
|
+
.replace(/\{/g, "\\{");
|
|
42
|
+
}
|
|
43
|
+
formatLeadingComments(node, indentStr) {
|
|
44
|
+
const comments = this.leadingComments.get(node);
|
|
45
|
+
if (!comments || comments.length === 0)
|
|
46
|
+
return "";
|
|
47
|
+
const lines = [];
|
|
48
|
+
for (let i = 0; i < comments.length; i++) {
|
|
49
|
+
const c = comments[i];
|
|
50
|
+
if (i > 0 && c.line - comments[i - 1].line > 1) {
|
|
51
|
+
lines.push("");
|
|
52
|
+
}
|
|
53
|
+
lines.push(`${indentStr}${c.text}`);
|
|
54
|
+
}
|
|
55
|
+
const lastComment = comments[comments.length - 1];
|
|
56
|
+
const blankAfter = node.span && node.span.start.line - lastComment.line > 1;
|
|
57
|
+
return lines.join("\n") + "\n" + (blankAfter ? "\n" : "");
|
|
58
|
+
}
|
|
59
|
+
getTrailingComment(line) {
|
|
60
|
+
const c = this.trailingComments.get(line);
|
|
61
|
+
if (!c)
|
|
62
|
+
return "";
|
|
63
|
+
this.trailingComments.delete(line);
|
|
64
|
+
return ` ${c.text}`;
|
|
65
|
+
}
|
|
66
|
+
format() {
|
|
67
|
+
const parts = [];
|
|
68
|
+
for (let i = 0; i < this.ast.body.length; i++) {
|
|
69
|
+
const decl = this.ast.body[i];
|
|
70
|
+
const leading = this.formatLeadingComments(decl, "");
|
|
71
|
+
const formattedDecl = this.formatTopLevelDeclaration(decl);
|
|
72
|
+
const trailing = this.getTrailingComment(decl.span.end.line);
|
|
73
|
+
const piece = `${leading}${formattedDecl}${trailing}`;
|
|
74
|
+
parts.push(piece);
|
|
75
|
+
}
|
|
76
|
+
let result = "";
|
|
77
|
+
for (let i = 0; i < parts.length; i++) {
|
|
78
|
+
result += parts[i];
|
|
79
|
+
if (i < parts.length - 1) {
|
|
80
|
+
const curr = this.ast.body[i];
|
|
81
|
+
const next = this.ast.body[i + 1];
|
|
82
|
+
const currInner = curr.kind === "ExportDeclaration" ? curr.declaration : curr;
|
|
83
|
+
const nextInner = next.kind === "ExportDeclaration" ? next.declaration : next;
|
|
84
|
+
const currIsMulti = currInner.kind === "ServiceDeclaration" ||
|
|
85
|
+
(currInner.kind === "SchemaDeclaration" &&
|
|
86
|
+
currInner.body.kind !== "TypeSignatureSchema" &&
|
|
87
|
+
currInner.body.kind !== "TsDerivedSchema" &&
|
|
88
|
+
currInner.body.kind !== "ImportedTsSchema") ||
|
|
89
|
+
(currInner.kind === "BindingDeclaration" &&
|
|
90
|
+
currInner.init.span &&
|
|
91
|
+
currInner.init.span.end.line > currInner.init.span.start.line) ||
|
|
92
|
+
(currInner.kind === "ExportDefaultDeclaration" &&
|
|
93
|
+
currInner.expression.span &&
|
|
94
|
+
currInner.expression.span.end.line >
|
|
95
|
+
currInner.expression.span.start.line);
|
|
96
|
+
const nextIsMulti = nextInner.kind === "ServiceDeclaration" ||
|
|
97
|
+
(nextInner.kind === "SchemaDeclaration" &&
|
|
98
|
+
nextInner.body.kind !== "TypeSignatureSchema" &&
|
|
99
|
+
nextInner.body.kind !== "TsDerivedSchema" &&
|
|
100
|
+
nextInner.body.kind !== "ImportedTsSchema") ||
|
|
101
|
+
(nextInner.kind === "BindingDeclaration" &&
|
|
102
|
+
nextInner.init.span &&
|
|
103
|
+
nextInner.init.span.end.line > nextInner.init.span.start.line) ||
|
|
104
|
+
(nextInner.kind === "ExportDefaultDeclaration" &&
|
|
105
|
+
nextInner.expression.span &&
|
|
106
|
+
nextInner.expression.span.end.line >
|
|
107
|
+
nextInner.expression.span.start.line);
|
|
108
|
+
if (currIsMulti || nextIsMulti) {
|
|
109
|
+
result += "\n\n";
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
const hasBlank = next.span.start.line - curr.span.end.line > 1;
|
|
113
|
+
if (hasBlank) {
|
|
114
|
+
result += "\n\n";
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
result += "\n";
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (this.eofComments.length > 0) {
|
|
123
|
+
if (result.length > 0)
|
|
124
|
+
result += "\n\n";
|
|
125
|
+
result += this.eofComments.map((c) => c.text).join("\n");
|
|
126
|
+
}
|
|
127
|
+
return result.trimEnd() + "\n";
|
|
128
|
+
}
|
|
129
|
+
formatTopLevelDeclaration(decl) {
|
|
130
|
+
switch (decl.kind) {
|
|
131
|
+
case "ExportDeclaration":
|
|
132
|
+
return `export ${this.formatDeclaration(decl.declaration)}`;
|
|
133
|
+
case "ExportDefaultDeclaration":
|
|
134
|
+
return `export default ${this.formatExpression(decl.expression, 0)}`;
|
|
135
|
+
case "ImportDeclaration":
|
|
136
|
+
return this.formatImportDeclaration(decl);
|
|
137
|
+
case "ExportFromDeclaration": {
|
|
138
|
+
const part = decl.source.parts[0];
|
|
139
|
+
const value = part && part.kind === "StringRawPart" ? part.value : "";
|
|
140
|
+
return renderExportFrom(decl, `"${this.escapeStringPart(value)}"`).slice(0, -1);
|
|
141
|
+
}
|
|
142
|
+
case "SchemaDeclaration":
|
|
143
|
+
return this.formatSchemaDeclaration(decl, 0);
|
|
144
|
+
case "ErrorDeclaration":
|
|
145
|
+
return this.formatErrorDeclaration(decl);
|
|
146
|
+
case "ServiceDeclaration":
|
|
147
|
+
return this.formatServiceDeclaration(decl, 0);
|
|
148
|
+
case "BindingDeclaration":
|
|
149
|
+
return this.formatBindingDeclaration(decl, 0);
|
|
150
|
+
case "WhenStatement":
|
|
151
|
+
return this.formatStatement(decl, 0);
|
|
152
|
+
case "TsEscapeDeclaration":
|
|
153
|
+
return this.formatTsEscape(decl.code);
|
|
154
|
+
case "AsDeclaration": {
|
|
155
|
+
const prefix = decl.serviceName ? `as ${decl.serviceName.name}` : "as";
|
|
156
|
+
return decl.code.includes("\n")
|
|
157
|
+
? `${prefix} {\n${decl.code}\n}`
|
|
158
|
+
: `${prefix} { ${decl.code} }`;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
formatDeclaration(decl) {
|
|
163
|
+
switch (decl.kind) {
|
|
164
|
+
case "SchemaDeclaration":
|
|
165
|
+
return this.formatSchemaDeclaration(decl, 0);
|
|
166
|
+
case "ErrorDeclaration":
|
|
167
|
+
return this.formatErrorDeclaration(decl);
|
|
168
|
+
case "ServiceDeclaration":
|
|
169
|
+
return this.formatServiceDeclaration(decl, 0);
|
|
170
|
+
case "BindingDeclaration":
|
|
171
|
+
return this.formatBindingDeclaration(decl, 0);
|
|
172
|
+
case "WhenStatement":
|
|
173
|
+
return this.formatStatement(decl, 0);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
formatImportDeclaration(decl) {
|
|
177
|
+
const firstPart = decl.source.parts[0];
|
|
178
|
+
const rawVal = firstPart && firstPart.kind === "StringRawPart" ? firstPart.value : "";
|
|
179
|
+
const source = `"${this.escapeStringPart(rawVal)}"`;
|
|
180
|
+
return renderImport(decl, source).slice(0, -1);
|
|
181
|
+
}
|
|
182
|
+
formatTypeParameters(typeParams) {
|
|
183
|
+
if (!typeParams || typeParams.length === 0)
|
|
184
|
+
return "";
|
|
185
|
+
return `<${typeParams
|
|
186
|
+
.map((param) => param.constraint
|
|
187
|
+
? `${param.name.name} extends ${this.formatType(param.constraint)}`
|
|
188
|
+
: param.name.name)
|
|
189
|
+
.join(", ")}>`;
|
|
190
|
+
}
|
|
191
|
+
formatSchemaDeclaration(decl, indent = 0) {
|
|
192
|
+
const prefix = decl.isTs ? "ts schema " : "schema ";
|
|
193
|
+
const typeParams = this.formatTypeParameters(decl.typeParams);
|
|
194
|
+
const body = this.formatSchemaBody(decl.body, indent);
|
|
195
|
+
const sep = body.startsWith("\n") ? " =" : " = ";
|
|
196
|
+
return `${prefix}${decl.id.name}${typeParams}${sep}${body}`;
|
|
197
|
+
}
|
|
198
|
+
formatSchemaBody(body, indent = 0) {
|
|
199
|
+
switch (body.kind) {
|
|
200
|
+
case "RecordSchema":
|
|
201
|
+
return this.formatRecordSchema(body, indent);
|
|
202
|
+
case "TaggedUnionSchema":
|
|
203
|
+
return this.formatTaggedUnionSchema(body, indent);
|
|
204
|
+
case "StructuralUnionSchema":
|
|
205
|
+
return ("\n" +
|
|
206
|
+
body.members
|
|
207
|
+
.map((member) => `${this.spaces(indent + this.indentSize)}| ${this.formatType(member, indent + this.indentSize)}`)
|
|
208
|
+
.join("\n"));
|
|
209
|
+
case "RefinementSchema":
|
|
210
|
+
return `${this.formatType(body.baseType)} |> ${this.formatExpression(body.refinement, indent)}`;
|
|
211
|
+
case "TypeSignatureSchema":
|
|
212
|
+
return this.formatType(body.signature);
|
|
213
|
+
case "TsDerivedSchema":
|
|
214
|
+
return this.formatType(body.targetType, indent);
|
|
215
|
+
case "ImportedTsSchema":
|
|
216
|
+
return `from ts<${body.typeArguments.map((t) => this.formatType(t, indent)).join(", ")}>`;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
formatRecordSchema(schema, indent = 0) {
|
|
220
|
+
if (schema.fields.length === 0)
|
|
221
|
+
return "{}";
|
|
222
|
+
const fieldIndent = this.spaces(indent + this.indentSize);
|
|
223
|
+
const closingIndent = this.spaces(indent);
|
|
224
|
+
const fields = schema.fields
|
|
225
|
+
.map((f) => {
|
|
226
|
+
const leading = this.formatLeadingComments(f, fieldIndent);
|
|
227
|
+
const trailing = this.getTrailingComment(f.span.end.line);
|
|
228
|
+
const refinement = f.refinement
|
|
229
|
+
? ` |> ${this.formatExpression(f.refinement, indent + this.indentSize)}`
|
|
230
|
+
: "";
|
|
231
|
+
const def = f.defaultValue
|
|
232
|
+
? ` = ${this.formatExpression(f.defaultValue, indent + this.indentSize)}`
|
|
233
|
+
: "";
|
|
234
|
+
return `${leading}${fieldIndent}${f.name.name}: ${this.formatType(f.typeAnnotation, indent + this.indentSize)}${refinement}${def}${trailing}`;
|
|
235
|
+
})
|
|
236
|
+
.join("\n");
|
|
237
|
+
return `{\n${fields}\n${closingIndent}}`;
|
|
238
|
+
}
|
|
239
|
+
formatTaggedUnionSchema(schema, indent = 0) {
|
|
240
|
+
const variantIndent = this.spaces(indent + this.indentSize);
|
|
241
|
+
const variants = schema.variants
|
|
242
|
+
.map((v) => {
|
|
243
|
+
const leading = this.formatLeadingComments(v, variantIndent);
|
|
244
|
+
const trailing = this.getTrailingComment(v.span.end.line);
|
|
245
|
+
const fields = v.fields.length > 0
|
|
246
|
+
? `(${v.fields.map((f) => `${f.name.name}: ${this.formatType(f.typeAnnotation)}`).join(", ")})`
|
|
247
|
+
: "";
|
|
248
|
+
return `${leading}${variantIndent}| ${v.name.name}${fields}${trailing}`;
|
|
249
|
+
})
|
|
250
|
+
.join("\n");
|
|
251
|
+
return `\n${variants}`;
|
|
252
|
+
}
|
|
253
|
+
formatErrorDeclaration(decl) {
|
|
254
|
+
const fields = decl.fields.length > 0
|
|
255
|
+
? `(${decl.fields.map((f) => `${f.name.name}: ${this.formatType(f.typeAnnotation)}`).join(", ")})`
|
|
256
|
+
: "";
|
|
257
|
+
return `error ${decl.id.name}${fields}`;
|
|
258
|
+
}
|
|
259
|
+
formatServiceDeclaration(decl, indent = 0) {
|
|
260
|
+
let tag = "";
|
|
261
|
+
if (decl.customTag) {
|
|
262
|
+
const firstPart = decl.customTag.parts[0];
|
|
263
|
+
const rawVal = firstPart && firstPart.kind === "StringRawPart" ? firstPart.value : "";
|
|
264
|
+
tag = ` "${this.escapeStringPart(rawVal)}"`;
|
|
265
|
+
}
|
|
266
|
+
const memberIndent = this.spaces(indent + this.indentSize);
|
|
267
|
+
const members = decl.members
|
|
268
|
+
.map((m) => {
|
|
269
|
+
const leading = this.formatLeadingComments(m, memberIndent);
|
|
270
|
+
const trailing = this.getTrailingComment(m.span.end.line);
|
|
271
|
+
return `${leading}${memberIndent}${m.name.name}: ${this.formatType(m.signature)}${trailing}`;
|
|
272
|
+
})
|
|
273
|
+
.join("\n");
|
|
274
|
+
return `service ${decl.id.name}${tag}\n${members}`;
|
|
275
|
+
}
|
|
276
|
+
formatBindingDeclaration(decl, indent = 0) {
|
|
277
|
+
const js = decl.isJs ? "js " : "";
|
|
278
|
+
const mut = decl.isMut ? "mut " : "";
|
|
279
|
+
const typeAnn = decl.typeAnnotation
|
|
280
|
+
? `: ${this.formatType(decl.typeAnnotation)}`
|
|
281
|
+
: "";
|
|
282
|
+
const idStr = decl.id.kind === "Identifier" || decl.id.kind === "UpperIdentifier"
|
|
283
|
+
? decl.id.name
|
|
284
|
+
: this.formatPattern(decl.id);
|
|
285
|
+
const prefix = `${js}${mut}${idStr}${typeAnn} =`;
|
|
286
|
+
const init = this.formatBindingInit(decl, indent);
|
|
287
|
+
return `${prefix}${init}`;
|
|
288
|
+
}
|
|
289
|
+
formatBindingTarget(target) {
|
|
290
|
+
return target.kind === "Identifier" || target.kind === "UpperIdentifier"
|
|
291
|
+
? target.name
|
|
292
|
+
: this.formatPattern(target);
|
|
293
|
+
}
|
|
294
|
+
formatBindingInit(decl, indent = 0) {
|
|
295
|
+
const init = decl.init;
|
|
296
|
+
if (init.kind === "FunctionExpression") {
|
|
297
|
+
const typeParams = this.formatTypeParameters(init.typeParams);
|
|
298
|
+
const params = init.params
|
|
299
|
+
.map((p) => {
|
|
300
|
+
const rest = p.isRest ? "..." : "";
|
|
301
|
+
const type = p.typeAnnotation
|
|
302
|
+
? `: ${this.formatType(p.typeAnnotation)}`
|
|
303
|
+
: "";
|
|
304
|
+
const defaultValue = p.defaultValue
|
|
305
|
+
? ` = ${this.formatExpression(p.defaultValue, indent)}`
|
|
306
|
+
: "";
|
|
307
|
+
return `${rest}${this.formatBindingTarget(p.name)}${type}${defaultValue}`;
|
|
308
|
+
})
|
|
309
|
+
.join(", ");
|
|
310
|
+
const ret = init.returnType
|
|
311
|
+
? `: ${this.formatType(init.returnType)}`
|
|
312
|
+
: "";
|
|
313
|
+
const isArrowOnly = init.params.length === 0 &&
|
|
314
|
+
!init.returnType &&
|
|
315
|
+
init.span &&
|
|
316
|
+
this.source.slice(init.span.start.offset, init.span.start.offset + 2) === "=>";
|
|
317
|
+
const sig = isArrowOnly ? ` =>` : ` ${typeParams}(${params})${ret} =>`;
|
|
318
|
+
if (init.body.kind === "BlockStatement") {
|
|
319
|
+
const body = this.formatBlockStatement(init.body, indent + this.indentSize);
|
|
320
|
+
return `${sig}\n${body}`;
|
|
321
|
+
}
|
|
322
|
+
else if (init.body.kind === "PipelineExpression" &&
|
|
323
|
+
this.shouldExpandPipeline(this.unrollPipeline(init.body), indent + this.indentSize)) {
|
|
324
|
+
const body = this.formatPipelineExpression(init.body, indent + this.indentSize);
|
|
325
|
+
return `${sig}\n${this.spaces(indent + this.indentSize)}${body}`;
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
const body = this.formatExpression(init.body, indent);
|
|
329
|
+
return `${sig} ${body}`;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
if (init.kind === "RecordExpression") {
|
|
333
|
+
if (this.shouldExpandRecord(init, indent)) {
|
|
334
|
+
return ` ${this.formatRecordExpression(init, indent)}`;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
if (init.kind === "PipelineExpression") {
|
|
338
|
+
const chain = this.unrollPipeline(init);
|
|
339
|
+
if (this.shouldExpandPipeline(chain, indent + this.indentSize)) {
|
|
340
|
+
return `\n${this.spaces(indent + this.indentSize)}${this.formatPipelineExpression(init, indent + this.indentSize)}`;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
if (init.kind === "EffectBlockExpression") {
|
|
344
|
+
return `\n${this.spaces(indent + this.indentSize)}${this.formatEffectBlock(init, indent + this.indentSize)}`;
|
|
345
|
+
}
|
|
346
|
+
if (init.kind === "MatchExpression") {
|
|
347
|
+
const isSingleLine = init.arms.length > 0 &&
|
|
348
|
+
init.arms[0].span.start.line === init.span.start.line &&
|
|
349
|
+
init.arms.every((a) => a.body.kind !== "BlockStatement");
|
|
350
|
+
if (!isSingleLine) {
|
|
351
|
+
return `\n${this.spaces(indent + this.indentSize)}${this.formatMatchExpression(init, indent + this.indentSize)}`;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if (init.span.start.line > decl.span.start.line) {
|
|
355
|
+
return `\n${this.spaces(indent + this.indentSize)}${this.formatExpression(init, indent + this.indentSize)}`;
|
|
356
|
+
}
|
|
357
|
+
return ` ${this.formatExpression(init, indent)}`;
|
|
358
|
+
}
|
|
359
|
+
unrollPipeline(expr) {
|
|
360
|
+
const chain = [];
|
|
361
|
+
let curr = expr;
|
|
362
|
+
while (curr.kind === "PipelineExpression") {
|
|
363
|
+
chain.unshift(curr.right);
|
|
364
|
+
curr = curr.left;
|
|
365
|
+
}
|
|
366
|
+
chain.unshift(curr);
|
|
367
|
+
return chain;
|
|
368
|
+
}
|
|
369
|
+
shouldExpandPipeline(chain, indent) {
|
|
370
|
+
if (chain.length > 2)
|
|
371
|
+
return true;
|
|
372
|
+
if (chain[0].span.end.line < chain[1].span.start.line ||
|
|
373
|
+
chain[0].kind === "EffectBlockExpression")
|
|
374
|
+
return true;
|
|
375
|
+
const text = `${this.formatExpression(chain[0], indent)} |> ${this.formatExpression(chain[1], indent)}`;
|
|
376
|
+
const maxInline = Math.min(80, this.lineWidth);
|
|
377
|
+
return text.includes("\n") || indent + text.length > maxInline;
|
|
378
|
+
}
|
|
379
|
+
formatPipelineExpression(expr, indent = 0) {
|
|
380
|
+
const chain = this.unrollPipeline(expr);
|
|
381
|
+
const operators = [];
|
|
382
|
+
let current = expr;
|
|
383
|
+
while (current.kind === "PipelineExpression") {
|
|
384
|
+
operators.unshift(current.deferred ? "|>>" : "|>");
|
|
385
|
+
current = current.left;
|
|
386
|
+
}
|
|
387
|
+
const isMulti = this.shouldExpandPipeline(chain, indent);
|
|
388
|
+
if (!isMulti) {
|
|
389
|
+
return `${this.formatExpression(chain[0], indent)} ${operators[0]} ${this.formatExpression(chain[1], indent)}`;
|
|
390
|
+
}
|
|
391
|
+
const head = this.formatExpression(chain[0], indent);
|
|
392
|
+
const stepIndent = this.spaces(indent);
|
|
393
|
+
const steps = chain
|
|
394
|
+
.slice(1)
|
|
395
|
+
.map((step, index) => {
|
|
396
|
+
const trailing = this.getTrailingComment(step.span.end.line);
|
|
397
|
+
return `${stepIndent}${operators[index]} ${this.formatExpression(step, indent)}${trailing}`;
|
|
398
|
+
})
|
|
399
|
+
.join("\n");
|
|
400
|
+
return `${head}\n${steps}`;
|
|
401
|
+
}
|
|
402
|
+
formatTarget(target) {
|
|
403
|
+
if (target.kind === "Identifier") {
|
|
404
|
+
return target.name;
|
|
405
|
+
}
|
|
406
|
+
if (target.kind === "IndexExpression")
|
|
407
|
+
return this.formatExpression(target, 0);
|
|
408
|
+
return target.path.map((p) => p.name).join(".");
|
|
409
|
+
}
|
|
410
|
+
formatStatement(stmt, indent) {
|
|
411
|
+
switch (stmt.kind) {
|
|
412
|
+
case "BindingDeclaration":
|
|
413
|
+
return this.formatBindingDeclaration(stmt, indent);
|
|
414
|
+
case "AssignmentStatement":
|
|
415
|
+
if (stmt.value.kind === "PipelineExpression" &&
|
|
416
|
+
this.shouldExpandPipeline(this.unrollPipeline(stmt.value), indent + this.indentSize)) {
|
|
417
|
+
return `${this.formatTarget(stmt.target)} =\n${this.spaces(indent + this.indentSize)}${this.formatPipelineExpression(stmt.value, indent + this.indentSize)}`;
|
|
418
|
+
}
|
|
419
|
+
return `${this.formatTarget(stmt.target)} = ${this.formatExpression(stmt.value, indent)}`;
|
|
420
|
+
case "ExpressionStatement":
|
|
421
|
+
if (stmt.expression.kind === "PipelineExpression" &&
|
|
422
|
+
this.shouldExpandPipeline(this.unrollPipeline(stmt.expression), indent + this.indentSize)) {
|
|
423
|
+
return this.formatPipelineExpression(stmt.expression, indent + this.indentSize);
|
|
424
|
+
}
|
|
425
|
+
return this.formatExpression(stmt.expression, indent);
|
|
426
|
+
case "ReturnStatement":
|
|
427
|
+
if (stmt.argument.kind === "PipelineExpression" &&
|
|
428
|
+
this.shouldExpandPipeline(this.unrollPipeline(stmt.argument), indent + this.indentSize)) {
|
|
429
|
+
return `=>\n${this.spaces(indent + this.indentSize)}${this.formatPipelineExpression(stmt.argument, indent + this.indentSize)}`;
|
|
430
|
+
}
|
|
431
|
+
return `=> ${this.formatExpression(stmt.argument, indent)}`;
|
|
432
|
+
case "WhenStatement":
|
|
433
|
+
if (stmt.isEarlyReturn) {
|
|
434
|
+
const retExpr = stmt.consequent;
|
|
435
|
+
if (retExpr.kind === "PipelineExpression" &&
|
|
436
|
+
this.shouldExpandPipeline(this.unrollPipeline(retExpr), indent + this.indentSize)) {
|
|
437
|
+
return `when (${this.formatExpression(stmt.condition, indent)}) =>\n${this.spaces(indent + this.indentSize)}${this.formatPipelineExpression(retExpr, indent + this.indentSize)}`;
|
|
438
|
+
}
|
|
439
|
+
return `when (${this.formatExpression(stmt.condition, indent)}) => ${this.formatExpression(retExpr, indent)}`;
|
|
440
|
+
}
|
|
441
|
+
return `when (${this.formatExpression(stmt.condition, indent)}) {\n${this.formatBlockStatement(stmt.consequent, indent + this.indentSize, true)}\n${this.spaces(indent)}}`;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
formatBlockStatement(stmt, indent, preserveSingleReturn = false) {
|
|
445
|
+
const lines = [];
|
|
446
|
+
const lineIndent = this.spaces(indent);
|
|
447
|
+
const blockLeading = this.formatLeadingComments(stmt, lineIndent);
|
|
448
|
+
if (blockLeading) {
|
|
449
|
+
lines.push(blockLeading.trimEnd());
|
|
450
|
+
}
|
|
451
|
+
const isSingleReturn = !preserveSingleReturn &&
|
|
452
|
+
stmt.statements.length === 1 &&
|
|
453
|
+
stmt.statements[0].kind === "ReturnStatement" &&
|
|
454
|
+
!stmt.finalExpression;
|
|
455
|
+
for (let i = 0; i < stmt.statements.length; i++) {
|
|
456
|
+
const s = stmt.statements[i];
|
|
457
|
+
const leading = this.formatLeadingComments(s, lineIndent);
|
|
458
|
+
const trailing = this.getTrailingComment(s.span.end.line);
|
|
459
|
+
const formattedStmt = isSingleReturn && s.kind === "ReturnStatement"
|
|
460
|
+
? this.formatExpression(s.argument, indent)
|
|
461
|
+
: this.formatStatement(s, indent);
|
|
462
|
+
lines.push(`${leading}${lineIndent}${formattedStmt}${trailing}`);
|
|
463
|
+
if (i < stmt.statements.length - 1) {
|
|
464
|
+
const next = stmt.statements[i + 1];
|
|
465
|
+
if (next.span && s.span && next.span.start.line - s.span.end.line > 1) {
|
|
466
|
+
lines.push("");
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
if (stmt.finalExpression) {
|
|
471
|
+
if (stmt.statements.length > 0) {
|
|
472
|
+
const last = stmt.statements[stmt.statements.length - 1];
|
|
473
|
+
if (stmt.finalExpression.span &&
|
|
474
|
+
last.span &&
|
|
475
|
+
stmt.finalExpression.span.start.line - last.span.end.line > 1) {
|
|
476
|
+
lines.push("");
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
const leadingFinal = this.formatLeadingComments(stmt.finalExpression, lineIndent);
|
|
480
|
+
const trailingFinal = this.getTrailingComment(stmt.finalExpression.span.end.line);
|
|
481
|
+
lines.push(`${leadingFinal}${lineIndent}${this.formatExpression(stmt.finalExpression, indent)}${trailingFinal}`);
|
|
482
|
+
}
|
|
483
|
+
return lines.join("\n");
|
|
484
|
+
}
|
|
485
|
+
formatEffectBlock(expr, indent = 0) {
|
|
486
|
+
const stmtIndent = this.spaces(indent + this.indentSize);
|
|
487
|
+
const lines = [];
|
|
488
|
+
for (let i = 0; i < expr.body.length; i++) {
|
|
489
|
+
const stmt = expr.body[i];
|
|
490
|
+
const leading = this.formatLeadingComments(stmt, stmtIndent);
|
|
491
|
+
const trailing = this.getTrailingComment(stmt.span.end.line);
|
|
492
|
+
const formatted = this.formatEffectStatement(stmt, indent + this.indentSize);
|
|
493
|
+
lines.push(`${leading}${stmtIndent}${formatted}${trailing}`);
|
|
494
|
+
if (i < expr.body.length - 1) {
|
|
495
|
+
const next = expr.body[i + 1];
|
|
496
|
+
if (next.span &&
|
|
497
|
+
stmt.span &&
|
|
498
|
+
next.span.start.line - stmt.span.end.line > 1) {
|
|
499
|
+
lines.push("");
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
if (expr.body.length > 0) {
|
|
504
|
+
const last = expr.body[expr.body.length - 1];
|
|
505
|
+
if (expr.finalExpression.span &&
|
|
506
|
+
last.span &&
|
|
507
|
+
expr.finalExpression.span.start.line - last.span.end.line > 1) {
|
|
508
|
+
lines.push("");
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
const leadingFinal = this.formatLeadingComments(expr.finalExpression, stmtIndent);
|
|
512
|
+
const trailingFinal = this.getTrailingComment(expr.finalExpression.span.end.line);
|
|
513
|
+
lines.push(`${leadingFinal}${stmtIndent}=> ${this.formatExpression(expr.finalExpression, indent + this.indentSize)}${trailingFinal}`);
|
|
514
|
+
const prefix = expr.unscoped ? "effect (unscoped)" : "effect";
|
|
515
|
+
return `${prefix} {\n${lines.join("\n")}\n${this.spaces(indent)}}`;
|
|
516
|
+
}
|
|
517
|
+
formatParBlock(expr, indent = 0) {
|
|
518
|
+
const stmtIndent = this.spaces(indent + this.indentSize);
|
|
519
|
+
const lines = [];
|
|
520
|
+
for (let i = 0; i < expr.body.length; i++) {
|
|
521
|
+
const stmt = expr.body[i];
|
|
522
|
+
const leading = this.formatLeadingComments(stmt, stmtIndent);
|
|
523
|
+
const trailing = this.getTrailingComment(stmt.span.end.line);
|
|
524
|
+
const formatted = this.formatEffectStatement(stmt, indent + this.indentSize);
|
|
525
|
+
lines.push(`${leading}${stmtIndent}${formatted}${trailing}`);
|
|
526
|
+
if (i < expr.body.length - 1) {
|
|
527
|
+
const next = expr.body[i + 1];
|
|
528
|
+
if (next.span &&
|
|
529
|
+
stmt.span &&
|
|
530
|
+
next.span.start.line - stmt.span.end.line > 1) {
|
|
531
|
+
lines.push("");
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
if (expr.hasExplicitReturn) {
|
|
536
|
+
if (expr.body.length > 0) {
|
|
537
|
+
const last = expr.body[expr.body.length - 1];
|
|
538
|
+
if (expr.finalExpression.span &&
|
|
539
|
+
last.span &&
|
|
540
|
+
expr.finalExpression.span.start.line - last.span.end.line > 1) {
|
|
541
|
+
lines.push("");
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
const leadingFinal = this.formatLeadingComments(expr.finalExpression, stmtIndent);
|
|
545
|
+
const trailingFinal = this.getTrailingComment(expr.finalExpression.span.end.line);
|
|
546
|
+
lines.push(`${leadingFinal}${stmtIndent}=> ${this.formatExpression(expr.finalExpression, indent + this.indentSize)}${trailingFinal}`);
|
|
547
|
+
}
|
|
548
|
+
const opt = expr.concurrency
|
|
549
|
+
? ` (${this.formatExpression(expr.concurrency)})`
|
|
550
|
+
: "";
|
|
551
|
+
return `par${opt} {\n${lines.join("\n")}\n${this.spaces(indent)}}`;
|
|
552
|
+
}
|
|
553
|
+
formatEffectStatement(stmt, indent) {
|
|
554
|
+
switch (stmt.kind) {
|
|
555
|
+
case "EffectBindStatement": {
|
|
556
|
+
const idStr = stmt.id.kind === "Identifier"
|
|
557
|
+
? stmt.id.name
|
|
558
|
+
: this.formatPattern(stmt.id);
|
|
559
|
+
return `${idStr} <- ${this.formatExpression(stmt.effect, indent)}`;
|
|
560
|
+
}
|
|
561
|
+
case "EffectDiscardStatement":
|
|
562
|
+
return `_ <- ${this.formatExpression(stmt.effect, indent)}`;
|
|
563
|
+
case "BindingDeclaration":
|
|
564
|
+
return this.formatBindingDeclaration(stmt, indent);
|
|
565
|
+
case "AssignmentStatement":
|
|
566
|
+
return `${this.formatTarget(stmt.target)} = ${this.formatExpression(stmt.value, indent)}`;
|
|
567
|
+
case "EffectControlFlowStatement":
|
|
568
|
+
return this.formatExpression(stmt.statement, indent);
|
|
569
|
+
case "WhenStatement":
|
|
570
|
+
return this.formatStatement(stmt, indent);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
formatMatchExpression(expr, indent = 0) {
|
|
574
|
+
const keyword = expr.isCatch ? "catch" : "match";
|
|
575
|
+
const isSingleLine = expr.arms.length > 0 &&
|
|
576
|
+
expr.arms[0].span.start.line === expr.span.start.line &&
|
|
577
|
+
expr.arms.every((a) => a.body.kind !== "BlockStatement");
|
|
578
|
+
if (isSingleLine) {
|
|
579
|
+
const scrutinee = expr.scrutinee
|
|
580
|
+
? ` ${this.formatExpression(expr.scrutinee, indent)}`
|
|
581
|
+
: "";
|
|
582
|
+
const arms = expr.arms.map((a) => this.formatMatchArmInline(a)).join(" ");
|
|
583
|
+
return `${keyword}${scrutinee} ${arms}`;
|
|
584
|
+
}
|
|
585
|
+
const scrutinee = expr.scrutinee
|
|
586
|
+
? ` ${this.formatExpression(expr.scrutinee, indent)}`
|
|
587
|
+
: "";
|
|
588
|
+
const arms = expr.arms
|
|
589
|
+
.map((a) => this.formatMatchArm(a, indent + this.indentSize))
|
|
590
|
+
.join("\n");
|
|
591
|
+
return `${keyword}${scrutinee}\n${arms}`;
|
|
592
|
+
}
|
|
593
|
+
formatMatchArm(arm, indent) {
|
|
594
|
+
const armIndent = this.spaces(indent);
|
|
595
|
+
const leading = this.formatLeadingComments(arm, armIndent);
|
|
596
|
+
const trailing = this.getTrailingComment(arm.span.end.line);
|
|
597
|
+
const pat = this.formatPattern(arm.pattern);
|
|
598
|
+
const guard = arm.guard
|
|
599
|
+
? ` when ${this.formatExpression(arm.guard, indent)}`
|
|
600
|
+
: "";
|
|
601
|
+
if (arm.body.kind === "BlockStatement") {
|
|
602
|
+
const bodyIndent = indent + this.indentSize * 2;
|
|
603
|
+
const body = this.formatBlockStatement(arm.body, bodyIndent);
|
|
604
|
+
return `${leading}${armIndent}| ${pat}${guard} =>\n${body}${trailing}`;
|
|
605
|
+
}
|
|
606
|
+
else {
|
|
607
|
+
if (arm.body.kind === "PipelineExpression") {
|
|
608
|
+
const bodyIndent = indent + this.indentSize;
|
|
609
|
+
const body = this.formatExpression(arm.body, bodyIndent);
|
|
610
|
+
if (body.includes("\n")) {
|
|
611
|
+
return `${leading}${armIndent}| ${pat}${guard} =>\n${this.spaces(bodyIndent)}${body}${trailing}`;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
const body = this.formatExpression(arm.body, indent);
|
|
615
|
+
return `${leading}${armIndent}| ${pat}${guard} => ${body}${trailing}`;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
formatMatchArmInline(arm) {
|
|
619
|
+
const pat = this.formatPattern(arm.pattern);
|
|
620
|
+
const guard = arm.guard
|
|
621
|
+
? ` when ${this.formatExpression(arm.guard, 0)}`
|
|
622
|
+
: "";
|
|
623
|
+
const body = arm.body.kind === "BlockStatement"
|
|
624
|
+
? this.formatBlockStatement(arm.body, 0)
|
|
625
|
+
: this.formatExpression(arm.body, 0);
|
|
626
|
+
return `| ${pat}${guard} => ${body}`;
|
|
627
|
+
}
|
|
628
|
+
formatPattern(pat) {
|
|
629
|
+
switch (pat.kind) {
|
|
630
|
+
case "WildcardPattern":
|
|
631
|
+
return "_";
|
|
632
|
+
case "LiteralPattern": {
|
|
633
|
+
if (pat.literal.kind === "BooleanLiteral" && pat.literal.rawExpr) {
|
|
634
|
+
return this.formatExpression(pat.literal.rawExpr, 0);
|
|
635
|
+
}
|
|
636
|
+
return this.formatLiteral(pat.literal);
|
|
637
|
+
}
|
|
638
|
+
case "VariablePattern":
|
|
639
|
+
return pat.name.name;
|
|
640
|
+
case "ConstructorPattern": {
|
|
641
|
+
const args = pat.args.length > 0
|
|
642
|
+
? `(${pat.args.map((a) => this.formatPattern(a)).join(", ")})`
|
|
643
|
+
: "";
|
|
644
|
+
return `${pat.constructorName.name}${args}`;
|
|
645
|
+
}
|
|
646
|
+
case "RecordPattern":
|
|
647
|
+
return `{ ${pat.fields.map((f) => (f.isRest ? `...${f.key.name}` : f.pattern ? `${f.key.name}: ${this.formatPattern(f.pattern)}` : f.key.name)).join(", ")} }`;
|
|
648
|
+
case "ArrayPattern":
|
|
649
|
+
return `[${pat.elements.map((e) => this.formatPattern(e)).join(", ")}]`;
|
|
650
|
+
case "RestPattern":
|
|
651
|
+
return `...${this.formatPattern(pat.argument)}`;
|
|
652
|
+
case "OrPattern":
|
|
653
|
+
return pat.patterns.map((p) => this.formatPattern(p)).join(" | ");
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
shouldExpandRecord(expr, indent = 0) {
|
|
657
|
+
if (expr.entries.length === 0)
|
|
658
|
+
return false;
|
|
659
|
+
if (expr.entries.length >= 3)
|
|
660
|
+
return true;
|
|
661
|
+
if (expr.entries[0].span.start.line > expr.span.start.line ||
|
|
662
|
+
expr.entries.some((e) => e.span.end.line > e.span.start.line)) {
|
|
663
|
+
return true;
|
|
664
|
+
}
|
|
665
|
+
const hasSpread = expr.entries.some((e) => e.kind === "SpreadEntry");
|
|
666
|
+
const entriesStr = expr.entries
|
|
667
|
+
.map((e) => this.formatRecordEntry(e, indent))
|
|
668
|
+
.join(", ");
|
|
669
|
+
if (entriesStr.includes("\n"))
|
|
670
|
+
return true;
|
|
671
|
+
if (hasSpread &&
|
|
672
|
+
expr.entries.length >= 2 &&
|
|
673
|
+
indent + entriesStr.length + 4 > 50) {
|
|
674
|
+
return true;
|
|
675
|
+
}
|
|
676
|
+
const maxInline = Math.min(60, this.lineWidth);
|
|
677
|
+
return indent + entriesStr.length + 4 > maxInline;
|
|
678
|
+
}
|
|
679
|
+
formatRecordExpression(expr, indent = 0) {
|
|
680
|
+
if (expr.entries.length === 0)
|
|
681
|
+
return "{}";
|
|
682
|
+
if (!this.shouldExpandRecord(expr, indent)) {
|
|
683
|
+
const entriesStr = expr.entries
|
|
684
|
+
.map((e) => this.formatRecordEntry(e, indent))
|
|
685
|
+
.join(", ");
|
|
686
|
+
return `{ ${entriesStr} }`;
|
|
687
|
+
}
|
|
688
|
+
const entryIndent = this.spaces(indent + this.indentSize);
|
|
689
|
+
const closingIndent = this.spaces(indent);
|
|
690
|
+
const lines = [];
|
|
691
|
+
for (let i = 0; i < expr.entries.length; i++) {
|
|
692
|
+
const e = expr.entries[i];
|
|
693
|
+
const leading = this.formatLeadingComments(e, entryIndent);
|
|
694
|
+
const trailing = this.getTrailingComment(e.span.end.line);
|
|
695
|
+
lines.push(`${leading}${entryIndent}${this.formatRecordEntry(e, indent + this.indentSize)}${trailing}`);
|
|
696
|
+
if (i < expr.entries.length - 1) {
|
|
697
|
+
const next = expr.entries[i + 1];
|
|
698
|
+
if (next.span && e.span && next.span.start.line - e.span.end.line > 1) {
|
|
699
|
+
lines.push("");
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
return `{\n${lines.join("\n")}\n${closingIndent}}`;
|
|
704
|
+
}
|
|
705
|
+
formatRecordEntry(entry, indent = 0) {
|
|
706
|
+
switch (entry.kind) {
|
|
707
|
+
case "SpreadEntry":
|
|
708
|
+
return `...${this.formatExpression(entry.argument, indent)}`;
|
|
709
|
+
case "FieldEntry": {
|
|
710
|
+
const key = RESERVED_IDENTIFIERS.has(entry.key.name)
|
|
711
|
+
? `"${entry.key.name}"`
|
|
712
|
+
: entry.key.name;
|
|
713
|
+
return `${key}: ${this.formatExpression(entry.value, indent)}`;
|
|
714
|
+
}
|
|
715
|
+
case "DotPathEntry":
|
|
716
|
+
return `${entry.path.map((p) => p.name).join(".")}: ${this.formatExpression(entry.value, indent)}`;
|
|
717
|
+
case "ShorthandEntry":
|
|
718
|
+
return entry.key.name;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
formatArrayExpression(expr, indent = 0) {
|
|
722
|
+
if (expr.elements.length === 0)
|
|
723
|
+
return "[]";
|
|
724
|
+
const isMultiline = expr.elements.some((e) => e.span.end.line > e.span.start.line) ||
|
|
725
|
+
(expr.elements.length > 0 &&
|
|
726
|
+
expr.elements[0].span.start.line > expr.span.start.line);
|
|
727
|
+
const elements = expr.elements
|
|
728
|
+
.map((e) => this.formatExpression(e, indent))
|
|
729
|
+
.join(", ");
|
|
730
|
+
if (!isMultiline &&
|
|
731
|
+
indent + elements.length + 2 <= this.lineWidth &&
|
|
732
|
+
!elements.includes("\n")) {
|
|
733
|
+
return `[${elements}]`;
|
|
734
|
+
}
|
|
735
|
+
const elemIndent = this.spaces(indent + this.indentSize);
|
|
736
|
+
const closingIndent = this.spaces(indent);
|
|
737
|
+
const lines = expr.elements.map((e) => `${elemIndent}${this.formatExpression(e, indent + this.indentSize)}`);
|
|
738
|
+
return `[\n${lines.join(",\n")}\n${closingIndent}]`;
|
|
739
|
+
}
|
|
740
|
+
formatBinaryExpression(expr, indent = 0, parentOp, isRightSide) {
|
|
741
|
+
const currentPrec = PRECEDENCE[expr.operator] ?? 0;
|
|
742
|
+
const parentPrec = parentOp ? (PRECEDENCE[parentOp] ?? 0) : 0;
|
|
743
|
+
let needsParens = false;
|
|
744
|
+
if (parentOp) {
|
|
745
|
+
if (currentPrec < parentPrec) {
|
|
746
|
+
needsParens = true;
|
|
747
|
+
}
|
|
748
|
+
else if (currentPrec === parentPrec) {
|
|
749
|
+
const associativity = ASSOCIATIVITY[parentOp] ?? "left";
|
|
750
|
+
if ((associativity === "left" && isRightSide) ||
|
|
751
|
+
(associativity === "right" && !isRightSide)) {
|
|
752
|
+
needsParens = true;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
const left = this.formatBinaryOperand(expr.left, indent, expr.operator, false);
|
|
757
|
+
const right = this.formatBinaryOperand(expr.right, indent, expr.operator, true);
|
|
758
|
+
const res = `${left} ${expr.operator} ${right}`;
|
|
759
|
+
return needsParens ? `(${res})` : res;
|
|
760
|
+
}
|
|
761
|
+
formatBinaryOperand(expr, indent, parentOp, isRightSide) {
|
|
762
|
+
if (expr.kind === "BinaryExpression") {
|
|
763
|
+
return this.formatBinaryExpression(expr, indent, parentOp, isRightSide);
|
|
764
|
+
}
|
|
765
|
+
if (expr.kind === "PipelineExpression") {
|
|
766
|
+
return `(${this.formatPipelineExpression(expr, indent)})`;
|
|
767
|
+
}
|
|
768
|
+
return this.formatExpression(expr, indent);
|
|
769
|
+
}
|
|
770
|
+
formatUnaryExpression(expr, indent = 0) {
|
|
771
|
+
const op = expr.operator === "not" ? "not " : "-";
|
|
772
|
+
let arg;
|
|
773
|
+
if (expr.argument.kind === "BinaryExpression" ||
|
|
774
|
+
expr.argument.kind === "PipelineExpression") {
|
|
775
|
+
arg = `(${this.formatExpression(expr.argument, indent)})`;
|
|
776
|
+
}
|
|
777
|
+
else {
|
|
778
|
+
arg = this.formatExpression(expr.argument, indent);
|
|
779
|
+
}
|
|
780
|
+
return `${op}${arg}`;
|
|
781
|
+
}
|
|
782
|
+
formatCallExpression(expr, indent = 0) {
|
|
783
|
+
let callee;
|
|
784
|
+
if (expr.callee.kind === "FunctionExpression") {
|
|
785
|
+
callee = `(${this.formatExpression(expr.callee, indent)})`;
|
|
786
|
+
}
|
|
787
|
+
else {
|
|
788
|
+
callee = this.formatExpression(expr.callee, indent);
|
|
789
|
+
}
|
|
790
|
+
if (expr.args.length === 0)
|
|
791
|
+
return `${callee}()`;
|
|
792
|
+
const lastArg = expr.args[expr.args.length - 1];
|
|
793
|
+
const isMultilineBlock = lastArg.kind === "FunctionExpression" &&
|
|
794
|
+
(lastArg.body.kind === "BlockStatement" ||
|
|
795
|
+
lastArg.body.kind === "MatchExpression" ||
|
|
796
|
+
lastArg.body.kind === "EffectBlockExpression" ||
|
|
797
|
+
lastArg.body.kind === "ParBlockExpression" ||
|
|
798
|
+
(lastArg.body.kind === "PipelineExpression" &&
|
|
799
|
+
this.shouldExpandPipeline(this.unrollPipeline(lastArg.body), indent + this.indentSize)));
|
|
800
|
+
if (isMultilineBlock) {
|
|
801
|
+
const formattedLast = this.formatExpression(lastArg, indent);
|
|
802
|
+
const firstLine = formattedLast.split("\n")[0] ?? "";
|
|
803
|
+
const close = formattedLast.endsWith("}") && firstLine.includes("{")
|
|
804
|
+
? ")"
|
|
805
|
+
: `\n${this.spaces(indent)})`;
|
|
806
|
+
if (expr.args.length === 1) {
|
|
807
|
+
return `${callee}(${formattedLast}${close}`;
|
|
808
|
+
}
|
|
809
|
+
const otherArgs = expr.args
|
|
810
|
+
.slice(0, -1)
|
|
811
|
+
.map((a) => this.formatExpression(a, indent))
|
|
812
|
+
.join(", ");
|
|
813
|
+
return `${callee}(${otherArgs}, ${formattedLast}${close}`;
|
|
814
|
+
}
|
|
815
|
+
const args = expr.args
|
|
816
|
+
.map((a) => this.formatExpression(a, indent))
|
|
817
|
+
.join(", ");
|
|
818
|
+
return `${callee}(${args})`;
|
|
819
|
+
}
|
|
820
|
+
formatExpression(expr, indent = 0) {
|
|
821
|
+
switch (expr.kind) {
|
|
822
|
+
case "PipelineExpression":
|
|
823
|
+
return this.formatPipelineExpression(expr, indent);
|
|
824
|
+
case "BinaryExpression":
|
|
825
|
+
return this.formatBinaryExpression(expr, indent);
|
|
826
|
+
case "UnaryExpression":
|
|
827
|
+
return this.formatUnaryExpression(expr, indent);
|
|
828
|
+
case "CallExpression":
|
|
829
|
+
return this.formatCallExpression(expr, indent);
|
|
830
|
+
case "MemberExpression":
|
|
831
|
+
return `${this.formatExpression(expr.object, indent)}.${expr.property.name}`;
|
|
832
|
+
case "IndexExpression":
|
|
833
|
+
return `${this.formatExpression(expr.object, indent)}[${this.formatExpression(expr.index, indent)}]`;
|
|
834
|
+
case "RecordExpression":
|
|
835
|
+
return this.formatRecordExpression(expr, indent);
|
|
836
|
+
case "ArrayExpression":
|
|
837
|
+
return this.formatArrayExpression(expr, indent);
|
|
838
|
+
case "FunctionExpression":
|
|
839
|
+
return this.formatFunctionExpression(expr, indent);
|
|
840
|
+
case "MutBlockExpression":
|
|
841
|
+
return `mut (${expr.draftParam.name}) =>\n${this.formatFunctionBody(expr.body, indent + this.indentSize)}`;
|
|
842
|
+
case "MatchExpression":
|
|
843
|
+
return this.formatMatchExpression(expr, indent);
|
|
844
|
+
case "EffectBlockExpression":
|
|
845
|
+
return this.formatEffectBlock(expr, indent);
|
|
846
|
+
case "ParBlockExpression":
|
|
847
|
+
return this.formatParBlock(expr, indent);
|
|
848
|
+
case "TypedHole":
|
|
849
|
+
return expr.label
|
|
850
|
+
? `??? "${this.escapeStringPart(expr.label)}"`
|
|
851
|
+
: "???";
|
|
852
|
+
case "Placeholder":
|
|
853
|
+
return "_";
|
|
854
|
+
case "NumberLiteral":
|
|
855
|
+
return expr.raw ?? expr.value.toString();
|
|
856
|
+
case "BooleanLiteral":
|
|
857
|
+
return expr.value ? "true" : "false";
|
|
858
|
+
case "StringLiteral":
|
|
859
|
+
return this.formatStringLiteral(expr);
|
|
860
|
+
case "TemplateLiteral":
|
|
861
|
+
return this.formatTemplateLiteral(expr);
|
|
862
|
+
case "TaggedTemplateExpression":
|
|
863
|
+
return this.formatTaggedTemplateExpression(expr, indent);
|
|
864
|
+
case "Identifier":
|
|
865
|
+
case "UpperIdentifier":
|
|
866
|
+
return expr.name;
|
|
867
|
+
case "JSXElement":
|
|
868
|
+
case "JSXSelfClosingElement":
|
|
869
|
+
return this.source.slice(expr.span.start.offset, expr.span.end.offset);
|
|
870
|
+
case "JsExpression":
|
|
871
|
+
return `js ${this.formatExpression(expr.expression, indent)}`;
|
|
872
|
+
case "TsEscapeExpression":
|
|
873
|
+
return this.formatTsEscape(expr.code);
|
|
874
|
+
case "SpreadElement":
|
|
875
|
+
return `...${this.formatExpression(expr.argument, indent)}`;
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
formatFunctionBody(body, indent) {
|
|
879
|
+
if (body.kind === "BlockStatement") {
|
|
880
|
+
return this.formatBlockStatement(body, indent);
|
|
881
|
+
}
|
|
882
|
+
return `${this.spaces(indent)}${this.formatExpression(body, indent)}`;
|
|
883
|
+
}
|
|
884
|
+
formatStringLiteral(str) {
|
|
885
|
+
let result = '"';
|
|
886
|
+
for (const part of str.parts) {
|
|
887
|
+
if (part.kind === "StringRawPart") {
|
|
888
|
+
result += this.escapeStringPart(part.value);
|
|
889
|
+
}
|
|
890
|
+
else {
|
|
891
|
+
result += `{${this.formatExpression(part.expression, 0)}}`;
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
result += '"';
|
|
895
|
+
return result;
|
|
896
|
+
}
|
|
897
|
+
formatTemplateLiteral(tpl) {
|
|
898
|
+
let result = "`";
|
|
899
|
+
for (const part of tpl.parts) {
|
|
900
|
+
if (part.kind === "StringRawPart") {
|
|
901
|
+
result += part.value;
|
|
902
|
+
}
|
|
903
|
+
else {
|
|
904
|
+
result += `\${${this.formatExpression(part.expression, 0)}}`;
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
result += "`";
|
|
908
|
+
return result;
|
|
909
|
+
}
|
|
910
|
+
formatTaggedTemplateExpression(expr, indent) {
|
|
911
|
+
const tag = this.formatExpression(expr.tag, indent);
|
|
912
|
+
let typeArgs = "";
|
|
913
|
+
if (expr.typeArguments && expr.typeArguments.length > 0) {
|
|
914
|
+
typeArgs =
|
|
915
|
+
"<" +
|
|
916
|
+
expr.typeArguments.map((t) => this.formatType(t)).join(", ") +
|
|
917
|
+
">";
|
|
918
|
+
}
|
|
919
|
+
return `${tag}${typeArgs}${this.formatTemplateLiteral(expr.template)}`;
|
|
920
|
+
}
|
|
921
|
+
formatLiteral(lit) {
|
|
922
|
+
switch (lit.kind) {
|
|
923
|
+
case "NumberLiteral":
|
|
924
|
+
return lit.raw ?? lit.value.toString();
|
|
925
|
+
case "BooleanLiteral":
|
|
926
|
+
return lit.value ? "true" : "false";
|
|
927
|
+
case "StringLiteral":
|
|
928
|
+
return this.formatStringLiteral(lit);
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
formatFunctionExpression(expr, indent = 0) {
|
|
932
|
+
const typeParams = this.formatTypeParameters(expr.typeParams);
|
|
933
|
+
const params = expr.params
|
|
934
|
+
.map((p) => {
|
|
935
|
+
const rest = p.isRest ? "..." : "";
|
|
936
|
+
const type = p.typeAnnotation
|
|
937
|
+
? `: ${this.formatType(p.typeAnnotation)}`
|
|
938
|
+
: "";
|
|
939
|
+
const defaultValue = p.defaultValue
|
|
940
|
+
? ` = ${this.formatExpression(p.defaultValue, indent)}`
|
|
941
|
+
: "";
|
|
942
|
+
return `${rest}${this.formatBindingTarget(p.name)}${type}${defaultValue}`;
|
|
943
|
+
})
|
|
944
|
+
.join(", ");
|
|
945
|
+
const ret = expr.returnType ? `: ${this.formatType(expr.returnType)}` : "";
|
|
946
|
+
const isArrowOnly = expr.params.length === 0 &&
|
|
947
|
+
!expr.returnType &&
|
|
948
|
+
expr.span &&
|
|
949
|
+
this.source.slice(expr.span.start.offset, expr.span.start.offset + 2) ===
|
|
950
|
+
"=>";
|
|
951
|
+
const sig = isArrowOnly ? `=>` : `${typeParams}(${params})${ret} =>`;
|
|
952
|
+
if (expr.body.kind === "BlockStatement") {
|
|
953
|
+
if (expr.body.statements.length === 1 && !expr.body.finalExpression) {
|
|
954
|
+
const single = expr.body.statements[0];
|
|
955
|
+
const singleExpr = single?.kind === "ReturnStatement"
|
|
956
|
+
? single.argument
|
|
957
|
+
: single?.kind === "ExpressionStatement"
|
|
958
|
+
? single.expression
|
|
959
|
+
: null;
|
|
960
|
+
if (singleExpr &&
|
|
961
|
+
singleExpr.kind === "PipelineExpression" &&
|
|
962
|
+
this.shouldExpandPipeline(this.unrollPipeline(singleExpr), indent + this.indentSize)) {
|
|
963
|
+
const body = this.formatPipelineExpression(singleExpr, indent + this.indentSize);
|
|
964
|
+
return `${sig}\n${this.spaces(indent + this.indentSize)}${body}`;
|
|
965
|
+
}
|
|
966
|
+
else if (singleExpr) {
|
|
967
|
+
return `${sig} ${this.formatExpression(singleExpr, indent)}`;
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
else if (expr.body.statements.length === 0 &&
|
|
971
|
+
expr.body.finalExpression) {
|
|
972
|
+
if (expr.body.finalExpression.kind === "PipelineExpression" &&
|
|
973
|
+
this.shouldExpandPipeline(this.unrollPipeline(expr.body.finalExpression), indent + this.indentSize)) {
|
|
974
|
+
const body = this.formatPipelineExpression(expr.body.finalExpression, indent + this.indentSize);
|
|
975
|
+
return `${sig}\n${this.spaces(indent + this.indentSize)}${body}`;
|
|
976
|
+
}
|
|
977
|
+
const bodyStartsWithBrace = this.source.length === 0 ||
|
|
978
|
+
this.source[expr.body.span?.start.offset ?? -1] === "{";
|
|
979
|
+
if (!bodyStartsWithBrace) {
|
|
980
|
+
const body = this.formatBlockStatement(expr.body, indent + this.indentSize);
|
|
981
|
+
return `${sig}\n${body}`;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
const body = this.formatBlockStatement(expr.body, indent + this.indentSize);
|
|
985
|
+
return body ? `${sig} {\n${body}\n${this.spaces(indent)}}` : `${sig} {}`;
|
|
986
|
+
}
|
|
987
|
+
else if (expr.body.kind === "PipelineExpression" &&
|
|
988
|
+
this.shouldExpandPipeline(this.unrollPipeline(expr.body), indent + this.indentSize)) {
|
|
989
|
+
const body = this.formatPipelineExpression(expr.body, indent + this.indentSize);
|
|
990
|
+
return `${sig}\n${this.spaces(indent + this.indentSize)}${body}`;
|
|
991
|
+
}
|
|
992
|
+
else {
|
|
993
|
+
const body = this.formatExpression(expr.body, indent);
|
|
994
|
+
return `${sig} ${body}`;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
formatType(node, indent = 0) {
|
|
998
|
+
switch (node.kind) {
|
|
999
|
+
case "QualifiedTypeReferenceNode": {
|
|
1000
|
+
const args = node.typeArgs?.length
|
|
1001
|
+
? `<${node.typeArgs.map((arg) => this.formatType(arg, indent)).join(", ")}>`
|
|
1002
|
+
: "";
|
|
1003
|
+
return `${node.parts.map((part) => part.name).join(".")}${args}`;
|
|
1004
|
+
}
|
|
1005
|
+
case "TypeReferenceNode": {
|
|
1006
|
+
const typeArgs = node.typeArgs && node.typeArgs.length > 0
|
|
1007
|
+
? `<${node.typeArgs.map((t) => this.formatType(t, indent)).join(", ")}>`
|
|
1008
|
+
: "";
|
|
1009
|
+
return `${node.name.name[0].toUpperCase()}${node.name.name.slice(1)}${typeArgs}`;
|
|
1010
|
+
}
|
|
1011
|
+
case "FunctionTypeNode": {
|
|
1012
|
+
const params = node.params
|
|
1013
|
+
.map((p) => {
|
|
1014
|
+
const rest = p.isRest ? "..." : "";
|
|
1015
|
+
const typeStr = p.typeAnnotation
|
|
1016
|
+
? this.formatType(p.typeAnnotation, indent)
|
|
1017
|
+
: "Unknown";
|
|
1018
|
+
return p.name
|
|
1019
|
+
? `${rest}${this.formatBindingTarget(p.name)}: ${typeStr}`
|
|
1020
|
+
: `${rest}${typeStr}`;
|
|
1021
|
+
})
|
|
1022
|
+
.join(", ");
|
|
1023
|
+
return `${this.formatTypeParameters(node.typeParams)}(${params}) => ${this.formatType(node.returnType, indent)}`;
|
|
1024
|
+
}
|
|
1025
|
+
case "EffectTypeNode": {
|
|
1026
|
+
const args = [this.formatType(node.success, indent)];
|
|
1027
|
+
if (node.error) {
|
|
1028
|
+
args.push(this.formatType(node.error, indent));
|
|
1029
|
+
if (node.requirements) {
|
|
1030
|
+
args.push(this.formatType(node.requirements, indent));
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
return `Effect<${args.join(", ")}>`;
|
|
1034
|
+
}
|
|
1035
|
+
case "RecordTypeNode": {
|
|
1036
|
+
if (node.fields.length === 0)
|
|
1037
|
+
return "{}";
|
|
1038
|
+
const isMultiline = node.fields.length >= 3 ||
|
|
1039
|
+
node.span.start.line !== node.span.end.line;
|
|
1040
|
+
const inlineFields = node.fields
|
|
1041
|
+
.map((f) => {
|
|
1042
|
+
const key = RESERVED_IDENTIFIERS.has(f.name.name)
|
|
1043
|
+
? `"${f.name.name}"`
|
|
1044
|
+
: f.name.name;
|
|
1045
|
+
return `${key}: ${this.formatType(f.typeAnnotation, indent)}${f.defaultValue ? ` = ${this.formatExpression(f.defaultValue, indent)}` : ""}`;
|
|
1046
|
+
})
|
|
1047
|
+
.join(", ");
|
|
1048
|
+
const maxInline = Math.min(60, this.lineWidth);
|
|
1049
|
+
if (!isMultiline &&
|
|
1050
|
+
indent + inlineFields.length + 4 <= maxInline &&
|
|
1051
|
+
!inlineFields.includes("\n")) {
|
|
1052
|
+
return `{ ${inlineFields} }`;
|
|
1053
|
+
}
|
|
1054
|
+
const fieldIndent = this.spaces(indent + this.indentSize);
|
|
1055
|
+
const closingIndent = this.spaces(indent);
|
|
1056
|
+
const fields = node.fields
|
|
1057
|
+
.map((f) => {
|
|
1058
|
+
const key = RESERVED_IDENTIFIERS.has(f.name.name)
|
|
1059
|
+
? `"${f.name.name}"`
|
|
1060
|
+
: f.name.name;
|
|
1061
|
+
return `${fieldIndent}${key}: ${this.formatType(f.typeAnnotation, indent + this.indentSize)}${f.defaultValue ? ` = ${this.formatExpression(f.defaultValue, indent + this.indentSize)}` : ""}`;
|
|
1062
|
+
})
|
|
1063
|
+
.join("\n");
|
|
1064
|
+
return `{\n${fields}\n${closingIndent}}`;
|
|
1065
|
+
}
|
|
1066
|
+
case "ArrayTypeNode":
|
|
1067
|
+
return `Array<${this.formatType(node.elementType, indent)}>`;
|
|
1068
|
+
case "UnionTypeNode":
|
|
1069
|
+
return node.types.map((t) => this.formatType(t, indent)).join(" | ");
|
|
1070
|
+
case "IntersectionTypeNode":
|
|
1071
|
+
return node.types.map((t) => this.formatType(t, indent)).join(" & ");
|
|
1072
|
+
case "LiteralTypeNode":
|
|
1073
|
+
return JSON.stringify(node.value);
|
|
1074
|
+
case "TypeQueryNode":
|
|
1075
|
+
return `typeof ${this.formatExpression(node.expr, indent)}`;
|
|
1076
|
+
case "IndexedAccessTypeNode":
|
|
1077
|
+
return `${this.formatType(node.objectType, indent)}[${this.formatType(node.indexType, indent)}]`;
|
|
1078
|
+
case "ParenthesizedTypeNode":
|
|
1079
|
+
return `(${this.formatType(node.typeNode, indent)})`;
|
|
1080
|
+
case "TsEscapeTypeNode":
|
|
1081
|
+
return this.formatTsEscape(node.code);
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
formatTsEscape(code) {
|
|
1085
|
+
return code.includes("\n") ? `ts {\n${code}\n}` : `ts { ${code} }`;
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
export function format(source, options) {
|
|
1089
|
+
const filename = options?.filename ?? "<anonymous>";
|
|
1090
|
+
// Type-aware shortening is optional for incomplete or unresolvable source;
|
|
1091
|
+
// formatting itself must remain available while editing.
|
|
1092
|
+
let input = source;
|
|
1093
|
+
try {
|
|
1094
|
+
input = convertPipelineStyle(source, filename, "bare").text;
|
|
1095
|
+
}
|
|
1096
|
+
catch {
|
|
1097
|
+
// Preserve the original stage whenever its resolution cannot be proven.
|
|
1098
|
+
}
|
|
1099
|
+
const lexer = new Lexer(input, filename);
|
|
1100
|
+
const tokens = lexer.tokenize();
|
|
1101
|
+
const parser = new Parser(tokens, filename);
|
|
1102
|
+
const ast = parser.parse();
|
|
1103
|
+
const formatter = new Formatter(input, ast, options);
|
|
1104
|
+
return formatter.format();
|
|
1105
|
+
}
|
|
1106
|
+
export function checkFormat(source, options) {
|
|
1107
|
+
const formatted = format(source, options);
|
|
1108
|
+
return {
|
|
1109
|
+
isFormatted: formatted === source,
|
|
1110
|
+
formatted,
|
|
1111
|
+
};
|
|
1112
|
+
}
|
|
1113
|
+
//# sourceMappingURL=index.js.map
|