@soundscript/soundscript 0.1.13 → 0.1.15
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/package.json +15 -6
- package/project-transform/index.js +2 -0
- package/project-transform/index.ts +8 -0
- package/project-transform/src/annotation_syntax.js +948 -0
- package/project-transform/src/annotation_syntax.ts +1217 -0
- package/project-transform/src/build_package.js +475 -0
- package/project-transform/src/build_package.ts +683 -0
- package/project-transform/src/bundled/portable-web-globals.d.ts +153 -0
- package/project-transform/src/bundled/runtime_externs.js +220 -0
- package/project-transform/src/bundled/runtime_externs.ts +237 -0
- package/project-transform/src/bundled/sound-libs/lib.decorators.d.ts +385 -0
- package/project-transform/src/bundled/sound-libs/lib.decorators.legacy.d.ts +22 -0
- package/project-transform/src/bundled/sound-libs/lib.dom.asynciterable.d.ts +42 -0
- package/project-transform/src/bundled/sound-libs/lib.dom.d.ts +39440 -0
- package/project-transform/src/bundled/sound-libs/lib.es2015.collection.d.ts +149 -0
- package/project-transform/src/bundled/sound-libs/lib.es2015.core.d.ts +657 -0
- package/project-transform/src/bundled/sound-libs/lib.es2015.d.ts +28 -0
- package/project-transform/src/bundled/sound-libs/lib.es2015.generator.d.ts +77 -0
- package/project-transform/src/bundled/sound-libs/lib.es2015.iterable.d.ts +616 -0
- package/project-transform/src/bundled/sound-libs/lib.es2015.promise.d.ts +80 -0
- package/project-transform/src/bundled/sound-libs/lib.es2015.proxy.d.ts +128 -0
- package/project-transform/src/bundled/sound-libs/lib.es2015.reflect.d.ts +144 -0
- package/project-transform/src/bundled/sound-libs/lib.es2015.symbol.d.ts +46 -0
- package/project-transform/src/bundled/sound-libs/lib.es2015.symbol.wellknown.d.ts +170 -0
- package/project-transform/src/bundled/sound-libs/lib.es2016.array.include.d.ts +116 -0
- package/project-transform/src/bundled/sound-libs/lib.es2016.d.ts +21 -0
- package/project-transform/src/bundled/sound-libs/lib.es2017.arraybuffer.d.ts +21 -0
- package/project-transform/src/bundled/sound-libs/lib.es2017.d.ts +26 -0
- package/project-transform/src/bundled/sound-libs/lib.es2017.date.d.ts +31 -0
- package/project-transform/src/bundled/sound-libs/lib.es2017.object.d.ts +49 -0
- package/project-transform/src/bundled/sound-libs/lib.es2017.string.d.ts +45 -0
- package/project-transform/src/bundled/sound-libs/lib.es2017.typedarrays.d.ts +53 -0
- package/project-transform/src/bundled/sound-libs/lib.es2018.asyncgenerator.d.ts +77 -0
- package/project-transform/src/bundled/sound-libs/lib.es2018.asynciterable.d.ts +57 -0
- package/project-transform/src/bundled/sound-libs/lib.es2018.d.ts +24 -0
- package/project-transform/src/bundled/sound-libs/lib.es2018.promise.d.ts +30 -0
- package/project-transform/src/bundled/sound-libs/lib.es2018.regexp.d.ts +37 -0
- package/project-transform/src/bundled/sound-libs/lib.es2019.array.d.ts +79 -0
- package/project-transform/src/bundled/sound-libs/lib.es2019.d.ts +24 -0
- package/project-transform/src/bundled/sound-libs/lib.es2019.object.d.ts +47 -0
- package/project-transform/src/bundled/sound-libs/lib.es2019.string.d.ts +37 -0
- package/project-transform/src/bundled/sound-libs/lib.es2019.symbol.d.ts +24 -0
- package/project-transform/src/bundled/sound-libs/lib.es2020.bigint.d.ts +765 -0
- package/project-transform/src/bundled/sound-libs/lib.es2020.d.ts +27 -0
- package/project-transform/src/bundled/sound-libs/lib.es2020.date.d.ts +42 -0
- package/project-transform/src/bundled/sound-libs/lib.es2020.number.d.ts +28 -0
- package/project-transform/src/bundled/sound-libs/lib.es2020.promise.d.ts +49 -0
- package/project-transform/src/bundled/sound-libs/lib.es2020.string.d.ts +44 -0
- package/project-transform/src/bundled/sound-libs/lib.es2020.symbol.wellknown.d.ts +41 -0
- package/project-transform/src/bundled/sound-libs/lib.es2021.d.ts +23 -0
- package/project-transform/src/bundled/sound-libs/lib.es2021.promise.d.ts +48 -0
- package/project-transform/src/bundled/sound-libs/lib.es2021.string.d.ts +33 -0
- package/project-transform/src/bundled/sound-libs/lib.es2021.weakref.d.ts +78 -0
- package/project-transform/src/bundled/sound-libs/lib.es2022.array.d.ts +121 -0
- package/project-transform/src/bundled/sound-libs/lib.es2022.d.ts +25 -0
- package/project-transform/src/bundled/sound-libs/lib.es2022.error.d.ts +75 -0
- package/project-transform/src/bundled/sound-libs/lib.es2022.object.d.ts +26 -0
- package/project-transform/src/bundled/sound-libs/lib.es2022.regexp.d.ts +39 -0
- package/project-transform/src/bundled/sound-libs/lib.es2022.string.d.ts +25 -0
- package/project-transform/src/bundled/sound-libs/lib.es2023.array.d.ts +924 -0
- package/project-transform/src/bundled/sound-libs/lib.es2023.collection.d.ts +21 -0
- package/project-transform/src/bundled/sound-libs/lib.es2023.d.ts +22 -0
- package/project-transform/src/bundled/sound-libs/lib.es2024.arraybuffer.d.ts +65 -0
- package/project-transform/src/bundled/sound-libs/lib.es2024.collection.d.ts +29 -0
- package/project-transform/src/bundled/sound-libs/lib.es2024.d.ts +26 -0
- package/project-transform/src/bundled/sound-libs/lib.es2024.object.d.ts +33 -0
- package/project-transform/src/bundled/sound-libs/lib.es2024.promise.d.ts +35 -0
- package/project-transform/src/bundled/sound-libs/lib.es2024.regexp.d.ts +25 -0
- package/project-transform/src/bundled/sound-libs/lib.es2024.string.d.ts +29 -0
- package/project-transform/src/bundled/sound-libs/lib.es5.d.ts +4924 -0
- package/project-transform/src/bundled/sound_stdlib.js +142 -0
- package/project-transform/src/bundled/sound_stdlib.ts +180 -0
- package/project-transform/src/checker/analyze_project.js +1361 -0
- package/project-transform/src/checker/analyze_project.ts +2246 -0
- package/project-transform/src/checker/diagnostics.js +112 -0
- package/project-transform/src/checker/diagnostics.ts +222 -0
- package/project-transform/src/checker/engine/context.js +235 -0
- package/project-transform/src/checker/engine/context.ts +340 -0
- package/project-transform/src/checker/engine/diagnostic_codes.js +72 -0
- package/project-transform/src/checker/engine/diagnostic_codes.ts +95 -0
- package/project-transform/src/checker/engine/facts.js +35 -0
- package/project-transform/src/checker/engine/facts.ts +48 -0
- package/project-transform/src/checker/engine/types.js +1 -0
- package/project-transform/src/checker/engine/types.ts +485 -0
- package/project-transform/src/checker/proof_escape_hatch_diagnostics.js +104 -0
- package/project-transform/src/checker/proof_escape_hatch_diagnostics.ts +173 -0
- package/project-transform/src/checker/rules/async_surface.js +231 -0
- package/project-transform/src/checker/rules/async_surface.ts +335 -0
- package/project-transform/src/checker/rules/class_lifecycle.js +798 -0
- package/project-transform/src/checker/rules/class_lifecycle.ts +1276 -0
- package/project-transform/src/checker/rules/directive_validation.js +571 -0
- package/project-transform/src/checker/rules/directive_validation.ts +938 -0
- package/project-transform/src/checker/rules/directives.js +23 -0
- package/project-transform/src/checker/rules/directives.ts +25 -0
- package/project-transform/src/checker/rules/flow.js +202 -0
- package/project-transform/src/checker/rules/flow.ts +333 -0
- package/project-transform/src/checker/rules/flow_facts.js +601 -0
- package/project-transform/src/checker/rules/flow_facts.ts +978 -0
- package/project-transform/src/checker/rules/flow_invalidation.js +1119 -0
- package/project-transform/src/checker/rules/flow_invalidation.ts +2150 -0
- package/project-transform/src/checker/rules/flow_shared.js +2822 -0
- package/project-transform/src/checker/rules/flow_shared.ts +4383 -0
- package/project-transform/src/checker/rules/foreign_boundary.js +120 -0
- package/project-transform/src/checker/rules/foreign_boundary.ts +196 -0
- package/project-transform/src/checker/rules/foreign_projection.js +279 -0
- package/project-transform/src/checker/rules/foreign_projection.ts +425 -0
- package/project-transform/src/checker/rules/generated_helpers.js +13 -0
- package/project-transform/src/checker/rules/generated_helpers.ts +18 -0
- package/project-transform/src/checker/rules/index.js +35 -0
- package/project-transform/src/checker/rules/index.ts +49 -0
- package/project-transform/src/checker/rules/namespace_object.js +845 -0
- package/project-transform/src/checker/rules/namespace_object.ts +1224 -0
- package/project-transform/src/checker/rules/non_ordinary_recovery.js +1328 -0
- package/project-transform/src/checker/rules/non_ordinary_recovery.ts +2391 -0
- package/project-transform/src/checker/rules/null_prototype.js +3 -0
- package/project-transform/src/checker/rules/null_prototype.ts +6 -0
- package/project-transform/src/checker/rules/overloads.js +181 -0
- package/project-transform/src/checker/rules/overloads.ts +317 -0
- package/project-transform/src/checker/rules/predicate_verification.js +691 -0
- package/project-transform/src/checker/rules/predicate_verification.ts +1088 -0
- package/project-transform/src/checker/rules/prototype_hardening.js +237 -0
- package/project-transform/src/checker/rules/prototype_hardening.ts +343 -0
- package/project-transform/src/checker/rules/receiver_discipline.js +263 -0
- package/project-transform/src/checker/rules/receiver_discipline.ts +356 -0
- package/project-transform/src/checker/rules/relations.js +6861 -0
- package/project-transform/src/checker/rules/relations.ts +12158 -0
- package/project-transform/src/checker/rules/resolved_builtins.js +274 -0
- package/project-transform/src/checker/rules/resolved_builtins.ts +438 -0
- package/project-transform/src/checker/rules/trust.js +217 -0
- package/project-transform/src/checker/rules/trust.ts +301 -0
- package/project-transform/src/checker/rules/type_guards.js +173 -0
- package/project-transform/src/checker/rules/type_guards.ts +257 -0
- package/project-transform/src/checker/rules/universal.js +17 -0
- package/project-transform/src/checker/rules/universal.ts +22 -0
- package/project-transform/src/checker/rules/unsafe_value_origin.js +80 -0
- package/project-transform/src/checker/rules/unsafe_value_origin.ts +125 -0
- package/project-transform/src/checker/rules/unsound_imports.js +218 -0
- package/project-transform/src/checker/rules/unsound_imports.ts +301 -0
- package/project-transform/src/checker/rules/unsound_syntax.js +1695 -0
- package/project-transform/src/checker/rules/unsound_syntax.ts +2540 -0
- package/project-transform/src/checker/rules/value_types.js +206 -0
- package/project-transform/src/checker/rules/value_types.ts +407 -0
- package/project-transform/src/checker/timing.js +43 -0
- package/project-transform/src/checker/timing.ts +78 -0
- package/project-transform/src/checker/unsupported_feature_messages.js +337 -0
- package/project-transform/src/checker/unsupported_feature_messages.ts +531 -0
- package/project-transform/src/cli.js +892 -0
- package/project-transform/src/cli.ts +1476 -0
- package/project-transform/src/compiler/compile_project.js +319 -0
- package/project-transform/src/compiler/compile_project.ts +508 -0
- package/project-transform/src/compiler/errors.js +10 -0
- package/project-transform/src/compiler/errors.ts +29 -0
- package/project-transform/src/compiler/ir.js +1 -0
- package/project-transform/src/compiler/ir.ts +1526 -0
- package/project-transform/src/compiler/lower.js +30550 -0
- package/project-transform/src/compiler/lower.ts +43645 -0
- package/project-transform/src/compiler/lower_arrays.js +140 -0
- package/project-transform/src/compiler/lower_arrays.ts +190 -0
- package/project-transform/src/compiler/lower_strings.js +121 -0
- package/project-transform/src/compiler/lower_strings.ts +198 -0
- package/project-transform/src/compiler/lower_tagged.js +329 -0
- package/project-transform/src/compiler/lower_tagged.ts +427 -0
- package/project-transform/src/compiler/lower_views.js +171 -0
- package/project-transform/src/compiler/lower_views.ts +251 -0
- package/project-transform/src/compiler/object_keys.js +25 -0
- package/project-transform/src/compiler/object_keys.ts +35 -0
- package/project-transform/src/compiler/runtime_ir.js +30 -0
- package/project-transform/src/compiler/runtime_ir.ts +727 -0
- package/project-transform/src/compiler/tagged_boundary.js +18 -0
- package/project-transform/src/compiler/tagged_boundary.ts +37 -0
- package/project-transform/src/compiler/toolchain.js +170 -0
- package/project-transform/src/compiler/toolchain.ts +229 -0
- package/project-transform/src/compiler/unicode_case_data.js +2102 -0
- package/project-transform/src/compiler/unicode_case_data.ts +2112 -0
- package/project-transform/src/compiler/wasm_js_host_runtime.js +656 -0
- package/project-transform/src/compiler/wasm_js_host_runtime.ts +762 -0
- package/project-transform/src/compiler/wat_arrays.js +3132 -0
- package/project-transform/src/compiler/wat_arrays.ts +3768 -0
- package/project-transform/src/compiler/wat_emitter.js +17952 -0
- package/project-transform/src/compiler/wat_emitter.ts +22812 -0
- package/project-transform/src/compiler/wat_strings.js +129 -0
- package/project-transform/src/compiler/wat_strings.ts +187 -0
- package/project-transform/src/compiler/wat_tagged.js +548 -0
- package/project-transform/src/compiler/wat_tagged.ts +674 -0
- package/project-transform/src/compiler_generator_runner.js +153 -0
- package/project-transform/src/compiler_generator_runner.ts +171 -0
- package/project-transform/src/compiler_object_test_helpers.js +69 -0
- package/project-transform/src/compiler_object_test_helpers.ts +96 -0
- package/project-transform/src/compiler_promise_runner.js +2116 -0
- package/project-transform/src/compiler_promise_runner.ts +2184 -0
- package/project-transform/src/compiler_test_helpers.js +854 -0
- package/project-transform/src/compiler_test_helpers.ts +1087 -0
- package/project-transform/src/config.js +568 -0
- package/project-transform/src/config.ts +892 -0
- package/project-transform/src/diagnostic_metadata.js +67 -0
- package/project-transform/src/diagnostic_metadata.ts +99 -0
- package/project-transform/src/diagnostic_reference.js +1368 -0
- package/project-transform/src/diagnostic_reference.ts +1523 -0
- package/project-transform/src/editor_diagnostics_worker.js +176 -0
- package/project-transform/src/editor_diagnostics_worker.ts +250 -0
- package/project-transform/src/editor_projection.js +224 -0
- package/project-transform/src/editor_projection.ts +421 -0
- package/project-transform/src/frontend/builtin_expanded_program_test_cleanup.js +47 -0
- package/project-transform/src/frontend/builtin_expanded_program_test_cleanup.ts +72 -0
- package/project-transform/src/frontend/builtin_macro_support.js +842 -0
- package/project-transform/src/frontend/builtin_macro_support.ts +1386 -0
- package/project-transform/src/frontend/builtin_macros.js +409 -0
- package/project-transform/src/frontend/builtin_macros.ts +542 -0
- package/project-transform/src/frontend/component_poc_runtime.js +279 -0
- package/project-transform/src/frontend/component_poc_runtime.ts +372 -0
- package/project-transform/src/frontend/css_macro.js +148 -0
- package/project-transform/src/frontend/css_macro.ts +222 -0
- package/project-transform/src/frontend/derive_macros.js +2072 -0
- package/project-transform/src/frontend/derive_macros.ts +3188 -0
- package/project-transform/src/frontend/embedded_fragment_support.js +106 -0
- package/project-transform/src/frontend/embedded_fragment_support.ts +172 -0
- package/project-transform/src/frontend/error_normalization.js +403 -0
- package/project-transform/src/frontend/error_normalization.ts +832 -0
- package/project-transform/src/frontend/error_stdlib_support.js +1 -0
- package/project-transform/src/frontend/error_stdlib_support.ts +6 -0
- package/project-transform/src/frontend/expand_project.js +169 -0
- package/project-transform/src/frontend/expand_project.ts +248 -0
- package/project-transform/src/frontend/format_soundscript.js +297 -0
- package/project-transform/src/frontend/format_soundscript.ts +582 -0
- package/project-transform/src/frontend/graphql_macro.js +174 -0
- package/project-transform/src/frontend/graphql_macro.ts +253 -0
- package/project-transform/src/frontend/hash_context.js +83 -0
- package/project-transform/src/frontend/hash_context.ts +113 -0
- package/project-transform/src/frontend/hkt_macro.js +448 -0
- package/project-transform/src/frontend/hkt_macro.ts +897 -0
- package/project-transform/src/frontend/import_binding_usage.js +190 -0
- package/project-transform/src/frontend/import_binding_usage.ts +277 -0
- package/project-transform/src/frontend/macro_advanced_backend_adapter.js +58 -0
- package/project-transform/src/frontend/macro_advanced_backend_adapter.ts +123 -0
- package/project-transform/src/frontend/macro_advanced_context.js +826 -0
- package/project-transform/src/frontend/macro_advanced_context.ts +1102 -0
- package/project-transform/src/frontend/macro_advanced_output.js +21 -0
- package/project-transform/src/frontend/macro_advanced_output.ts +41 -0
- package/project-transform/src/frontend/macro_api.js +353 -0
- package/project-transform/src/frontend/macro_api.ts +1722 -0
- package/project-transform/src/frontend/macro_api_internal.js +35 -0
- package/project-transform/src/frontend/macro_api_internal.ts +80 -0
- package/project-transform/src/frontend/macro_api_module_support.js +39 -0
- package/project-transform/src/frontend/macro_api_module_support.ts +65 -0
- package/project-transform/src/frontend/macro_backend_adapter.js +272 -0
- package/project-transform/src/frontend/macro_backend_adapter.ts +420 -0
- package/project-transform/src/frontend/macro_context.js +816 -0
- package/project-transform/src/frontend/macro_context.ts +1105 -0
- package/project-transform/src/frontend/macro_debug.js +99 -0
- package/project-transform/src/frontend/macro_debug.ts +157 -0
- package/project-transform/src/frontend/macro_definition_support.js +28 -0
- package/project-transform/src/frontend/macro_definition_support.ts +73 -0
- package/project-transform/src/frontend/macro_errors.js +40 -0
- package/project-transform/src/frontend/macro_errors.ts +70 -0
- package/project-transform/src/frontend/macro_expander.js +919 -0
- package/project-transform/src/frontend/macro_expander.ts +1611 -0
- package/project-transform/src/frontend/macro_factory_support.js +176 -0
- package/project-transform/src/frontend/macro_factory_support.ts +263 -0
- package/project-transform/src/frontend/macro_host_ast_internal.js +64 -0
- package/project-transform/src/frontend/macro_host_ast_internal.ts +109 -0
- package/project-transform/src/frontend/macro_index.js +27 -0
- package/project-transform/src/frontend/macro_index.ts +50 -0
- package/project-transform/src/frontend/macro_loader.js +281 -0
- package/project-transform/src/frontend/macro_loader.ts +506 -0
- package/project-transform/src/frontend/macro_operand_semantics.js +838 -0
- package/project-transform/src/frontend/macro_operand_semantics.ts +1489 -0
- package/project-transform/src/frontend/macro_output.js +54 -0
- package/project-transform/src/frontend/macro_output.ts +123 -0
- package/project-transform/src/frontend/macro_parser.js +611 -0
- package/project-transform/src/frontend/macro_parser.ts +832 -0
- package/project-transform/src/frontend/macro_resolver.js +69 -0
- package/project-transform/src/frontend/macro_resolver.ts +125 -0
- package/project-transform/src/frontend/macro_rewrite.js +285 -0
- package/project-transform/src/frontend/macro_rewrite.ts +442 -0
- package/project-transform/src/frontend/macro_runtime_support.js +232 -0
- package/project-transform/src/frontend/macro_runtime_support.ts +324 -0
- package/project-transform/src/frontend/macro_scanner.js +393 -0
- package/project-transform/src/frontend/macro_scanner.ts +455 -0
- package/project-transform/src/frontend/macro_semantic_backend_adapter.js +87 -0
- package/project-transform/src/frontend/macro_semantic_backend_adapter.ts +166 -0
- package/project-transform/src/frontend/macro_semantic_context.js +5 -0
- package/project-transform/src/frontend/macro_semantic_context.ts +12 -0
- package/project-transform/src/frontend/macro_semantic_output.js +24 -0
- package/project-transform/src/frontend/macro_semantic_output.ts +47 -0
- package/project-transform/src/frontend/macro_semantic_types.js +1 -0
- package/project-transform/src/frontend/macro_semantic_types.ts +98 -0
- package/project-transform/src/frontend/macro_semantics.js +1172 -0
- package/project-transform/src/frontend/macro_semantics.ts +1502 -0
- package/project-transform/src/frontend/macro_site_kind_support.js +164 -0
- package/project-transform/src/frontend/macro_site_kind_support.ts +255 -0
- package/project-transform/src/frontend/macro_syntax_internal.js +1950 -0
- package/project-transform/src/frontend/macro_syntax_internal.ts +3338 -0
- package/project-transform/src/frontend/macro_templates.js +57 -0
- package/project-transform/src/frontend/macro_templates.ts +143 -0
- package/project-transform/src/frontend/macro_test_helpers.js +82 -0
- package/project-transform/src/frontend/macro_test_helpers.ts +136 -0
- package/project-transform/src/frontend/macro_types.js +1 -0
- package/project-transform/src/frontend/macro_types.ts +103 -0
- package/project-transform/src/frontend/macro_vm.js +39 -0
- package/project-transform/src/frontend/macro_vm.ts +113 -0
- package/project-transform/src/frontend/match_macro.js +885 -0
- package/project-transform/src/frontend/match_macro.ts +1220 -0
- package/project-transform/src/frontend/numeric_normalization.js +824 -0
- package/project-transform/src/frontend/numeric_normalization.ts +1380 -0
- package/project-transform/src/frontend/numeric_prelude.js +278 -0
- package/project-transform/src/frontend/numeric_prelude.ts +370 -0
- package/project-transform/src/frontend/project_frontend.js +2396 -0
- package/project-transform/src/frontend/project_frontend.ts +3776 -0
- package/project-transform/src/frontend/project_macro_support.js +1401 -0
- package/project-transform/src/frontend/project_macro_support.ts +2137 -0
- package/project-transform/src/frontend/sql_macro.js +175 -0
- package/project-transform/src/frontend/sql_macro.ts +254 -0
- package/project-transform/src/frontend/sql_stdlib_support.js +1 -0
- package/project-transform/src/frontend/sql_stdlib_support.ts +6 -0
- package/project-transform/src/frontend/std_package_support.js +228 -0
- package/project-transform/src/frontend/std_package_support.ts +400 -0
- package/project-transform/src/frontend/value_normalization.js +306 -0
- package/project-transform/src/frontend/value_normalization.ts +599 -0
- package/project-transform/src/lsp/project_service.js +4771 -0
- package/project-transform/src/lsp/project_service.ts +7580 -0
- package/project-transform/src/lsp/protocol.js +9 -0
- package/project-transform/src/lsp/protocol.ts +38 -0
- package/project-transform/src/lsp/server.js +355 -0
- package/project-transform/src/lsp/server.ts +671 -0
- package/project-transform/src/lsp/session.js +49 -0
- package/project-transform/src/lsp/session.ts +48 -0
- package/project-transform/src/lsp/timing.js +43 -0
- package/project-transform/src/lsp/timing.ts +76 -0
- package/project-transform/src/lsp/transport.js +205 -0
- package/project-transform/src/lsp/transport.ts +253 -0
- package/project-transform/src/lsp_main.js +5 -0
- package/project-transform/src/lsp_main.ts +7 -0
- package/project-transform/src/macros.d.ts +1 -0
- package/project-transform/src/macros.js +1 -0
- package/project-transform/src/macros.ts +1 -0
- package/project-transform/src/main.js +24 -0
- package/project-transform/src/main.ts +28 -0
- package/project-transform/src/platform/host.js +264 -0
- package/project-transform/src/platform/host.ts +343 -0
- package/project-transform/src/platform/path.js +8 -0
- package/project-transform/src/platform/path.ts +20 -0
- package/project-transform/src/public_macro_api/macro_api.d.ts +1054 -0
- package/project-transform/src/public_macro_api/macro_semantic_types.d.ts +66 -0
- package/project-transform/src/public_macro_api/macro_types.d.ts +70 -0
- package/project-transform/src/run_program.js +14 -0
- package/project-transform/src/run_program.ts +33 -0
- package/project-transform/src/runtime/materialize.js +371 -0
- package/project-transform/src/runtime/materialize.ts +502 -0
- package/project-transform/src/runtime/on_demand.js +203 -0
- package/project-transform/src/runtime/on_demand.ts +305 -0
- package/project-transform/src/runtime/source_maps.js +205 -0
- package/project-transform/src/runtime/source_maps.ts +297 -0
- package/project-transform/src/runtime/transform.js +148 -0
- package/project-transform/src/runtime/transform.ts +295 -0
- package/project-transform/src/service/types.js +1 -0
- package/project-transform/src/service/types.ts +22 -0
- package/project-transform/src/soundscript_packages.js +477 -0
- package/project-transform/src/soundscript_packages.ts +754 -0
- package/project-transform/src/soundscript_runtime_specifiers.js +88 -0
- package/project-transform/src/soundscript_runtime_specifiers.ts +96 -0
- package/project-transform/src/stdlib/async.d.ts +81 -0
- package/project-transform/src/stdlib/async.js +213 -0
- package/project-transform/src/stdlib/async.ts +315 -0
- package/project-transform/src/stdlib/codec.d.ts +32 -0
- package/project-transform/src/stdlib/codec.js +30 -0
- package/project-transform/src/stdlib/codec.ts +76 -0
- package/project-transform/src/stdlib/compare.d.ts +28 -0
- package/project-transform/src/stdlib/compare.js +115 -0
- package/project-transform/src/stdlib/compare.ts +151 -0
- package/project-transform/src/stdlib/css.d.ts +16 -0
- package/project-transform/src/stdlib/css.js +9 -0
- package/project-transform/src/stdlib/css.ts +28 -0
- package/project-transform/src/stdlib/debug.d.ts +2 -0
- package/project-transform/src/stdlib/debug.js +9 -0
- package/project-transform/src/stdlib/debug.ts +10 -0
- package/project-transform/src/stdlib/decode.d.ts +86 -0
- package/project-transform/src/stdlib/decode.js +254 -0
- package/project-transform/src/stdlib/decode.ts +390 -0
- package/project-transform/src/stdlib/derive.d.ts +6 -0
- package/project-transform/src/stdlib/derive.js +7 -0
- package/project-transform/src/stdlib/derive.ts +7 -0
- package/project-transform/src/stdlib/encode.d.ts +100 -0
- package/project-transform/src/stdlib/encode.js +130 -0
- package/project-transform/src/stdlib/encode.ts +259 -0
- package/project-transform/src/stdlib/failures.d.ts +23 -0
- package/project-transform/src/stdlib/failures.js +41 -0
- package/project-transform/src/stdlib/failures.ts +64 -0
- package/project-transform/src/stdlib/fetch.d.ts +67 -0
- package/project-transform/src/stdlib/fetch.js +5 -0
- package/project-transform/src/stdlib/fetch.ts +11 -0
- package/project-transform/src/stdlib/graphql.d.ts +16 -0
- package/project-transform/src/stdlib/graphql.js +9 -0
- package/project-transform/src/stdlib/graphql.ts +28 -0
- package/project-transform/src/stdlib/hash.d.ts +34 -0
- package/project-transform/src/stdlib/hash.js +110 -0
- package/project-transform/src/stdlib/hash.ts +188 -0
- package/project-transform/src/stdlib/hkt.d.ts +40 -0
- package/project-transform/src/stdlib/hkt.js +3 -0
- package/project-transform/src/stdlib/hkt.ts +41 -0
- package/project-transform/src/stdlib/index.d.ts +9 -0
- package/project-transform/src/stdlib/index.js +15 -0
- package/project-transform/src/stdlib/index.ts +23 -0
- package/project-transform/src/stdlib/json.d.ts +125 -0
- package/project-transform/src/stdlib/json.js +764 -0
- package/project-transform/src/stdlib/json.ts +1034 -0
- package/project-transform/src/stdlib/match.d.ts +11 -0
- package/project-transform/src/stdlib/match.js +13 -0
- package/project-transform/src/stdlib/match.ts +26 -0
- package/project-transform/src/stdlib/numerics.d.ts +523 -0
- package/project-transform/src/stdlib/numerics.js +1356 -0
- package/project-transform/src/stdlib/numerics.ts +1937 -0
- package/project-transform/src/stdlib/random.d.ts +19 -0
- package/project-transform/src/stdlib/random.js +3 -0
- package/project-transform/src/stdlib/random.ts +5 -0
- package/project-transform/src/stdlib/result.d.ts +68 -0
- package/project-transform/src/stdlib/result.js +139 -0
- package/project-transform/src/stdlib/result.ts +248 -0
- package/project-transform/src/stdlib/sql.d.ts +22 -0
- package/project-transform/src/stdlib/sql.js +23 -0
- package/project-transform/src/stdlib/sql.ts +53 -0
- package/project-transform/src/stdlib/text.d.ts +24 -0
- package/project-transform/src/stdlib/text.js +3 -0
- package/project-transform/src/stdlib/text.ts +4 -0
- package/project-transform/src/stdlib/thunk.d.ts +2 -0
- package/project-transform/src/stdlib/thunk.js +9 -0
- package/project-transform/src/stdlib/thunk.ts +15 -0
- package/project-transform/src/stdlib/typeclasses.d.ts +57 -0
- package/project-transform/src/stdlib/typeclasses.js +78 -0
- package/project-transform/src/stdlib/typeclasses.ts +173 -0
- package/project-transform/src/stdlib/url.d.ts +37 -0
- package/project-transform/src/stdlib/url.js +3 -0
- package/project-transform/src/stdlib/url.ts +4 -0
- package/project-transform/src/stdlib/value.d.ts +9 -0
- package/project-transform/src/stdlib/value.js +104 -0
- package/project-transform/src/stdlib/value.ts +133 -0
- package/project-transform/src/test_installed_stdlib.js +147 -0
- package/project-transform/src/test_installed_stdlib.ts +245 -0
- package/project-transform/src/test_macro_package_fixture.js +50 -0
- package/project-transform/src/test_macro_package_fixture.ts +68 -0
- package/project-transform/src/value_deep_safe.js +191 -0
- package/project-transform/src/value_deep_safe.ts +273 -0
|
@@ -0,0 +1,1088 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
AnalysisContext,
|
|
5
|
+
PredicateSupportedTarget,
|
|
6
|
+
PredicateVerificationTargetFact,
|
|
7
|
+
} from '../engine/types.ts';
|
|
8
|
+
|
|
9
|
+
type SupportedPrimitive = 'bigint' | 'boolean' | 'number' | 'string' | 'symbol';
|
|
10
|
+
type SupportedTypeof = SupportedPrimitive | 'object';
|
|
11
|
+
|
|
12
|
+
export interface PredicateCheck {
|
|
13
|
+
body: ts.ConciseBody;
|
|
14
|
+
declaration: SignatureDeclarationWithBody;
|
|
15
|
+
parameterName: string;
|
|
16
|
+
parameterType: ts.Type;
|
|
17
|
+
predicateKind: ts.TypePredicateKind;
|
|
18
|
+
predicateType: ts.Type;
|
|
19
|
+
target: PredicateSupportedTarget;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type PredicateSignatureDeclaration =
|
|
23
|
+
| ts.FunctionDeclaration
|
|
24
|
+
| ts.FunctionExpression
|
|
25
|
+
| ts.ArrowFunction
|
|
26
|
+
| ts.MethodDeclaration;
|
|
27
|
+
|
|
28
|
+
export type SignatureDeclarationWithBody =
|
|
29
|
+
PredicateSignatureDeclaration;
|
|
30
|
+
|
|
31
|
+
interface ResolvedPredicateContract {
|
|
32
|
+
forbiddenPredicateTypeNode?: ts.TypeNode;
|
|
33
|
+
predicate: ts.TypePredicate;
|
|
34
|
+
signature: ts.Signature;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function isSignatureDeclarationWithBody(
|
|
38
|
+
node: ts.Node,
|
|
39
|
+
): node is SignatureDeclarationWithBody {
|
|
40
|
+
return (ts.isArrowFunction(node) && !!node.body) ||
|
|
41
|
+
((ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) ||
|
|
42
|
+
ts.isMethodDeclaration(node)) &&
|
|
43
|
+
!!node.body &&
|
|
44
|
+
ts.isBlock(node.body));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function getDeclaredPredicateContract(
|
|
48
|
+
context: AnalysisContext,
|
|
49
|
+
declaration: PredicateSignatureDeclaration,
|
|
50
|
+
): ResolvedPredicateContract | undefined {
|
|
51
|
+
if (!declaration.type || !ts.isTypePredicateNode(declaration.type)) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const signature = context.checker.getSignatureFromDeclaration(declaration);
|
|
56
|
+
if (!signature) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const predicate = context.checker.getTypePredicateOfSignature(signature);
|
|
61
|
+
if (!predicate) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
forbiddenPredicateTypeNode:
|
|
67
|
+
declaration.type && ts.isTypePredicateNode(declaration.type) ? declaration.type.type : undefined,
|
|
68
|
+
predicate,
|
|
69
|
+
signature,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function getContextualFunctionTypeNode(
|
|
74
|
+
declaration: PredicateSignatureDeclaration,
|
|
75
|
+
): ts.FunctionTypeNode | undefined {
|
|
76
|
+
if (!ts.isArrowFunction(declaration) && !ts.isFunctionExpression(declaration)) {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const parent = declaration.parent;
|
|
81
|
+
if (
|
|
82
|
+
ts.isVariableDeclaration(parent) &&
|
|
83
|
+
parent.initializer === declaration &&
|
|
84
|
+
parent.type &&
|
|
85
|
+
ts.isFunctionTypeNode(parent.type)
|
|
86
|
+
) {
|
|
87
|
+
return parent.type;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function getPropertyNameText(
|
|
94
|
+
context: AnalysisContext,
|
|
95
|
+
name: ts.PropertyName,
|
|
96
|
+
): string | undefined {
|
|
97
|
+
if (ts.isIdentifier(name) || ts.isStringLiteral(name) || ts.isNumericLiteral(name)) {
|
|
98
|
+
return name.text;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (ts.isComputedPropertyName(name)) {
|
|
102
|
+
const expression = name.expression;
|
|
103
|
+
if (
|
|
104
|
+
ts.isStringLiteral(expression) ||
|
|
105
|
+
ts.isNumericLiteral(expression) ||
|
|
106
|
+
ts.isNoSubstitutionTemplateLiteral(expression)
|
|
107
|
+
) {
|
|
108
|
+
return expression.text;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const constantType = context.checker.getTypeAtLocation(expression);
|
|
112
|
+
if ((constantType.flags & ts.TypeFlags.StringLiteral) !== 0) {
|
|
113
|
+
return (constantType as ts.StringLiteralType).value;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if ((constantType.flags & ts.TypeFlags.NumberLiteral) !== 0) {
|
|
117
|
+
return String((constantType as ts.NumberLiteralType).value);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function getContextualMethodType(
|
|
125
|
+
context: AnalysisContext,
|
|
126
|
+
declaration: ts.MethodDeclaration,
|
|
127
|
+
): ts.Type | undefined {
|
|
128
|
+
const parent = declaration.parent;
|
|
129
|
+
if (!ts.isObjectLiteralExpression(parent)) {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const propertyName = getPropertyNameText(context, declaration.name);
|
|
134
|
+
if (!propertyName) {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const contextualObjectType = context.checker.getContextualType(parent);
|
|
139
|
+
if (!contextualObjectType) {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const property = context.checker.getPropertyOfType(contextualObjectType, propertyName);
|
|
144
|
+
if (!property) {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return context.checker.getTypeOfSymbolAtLocation(property, declaration.name);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function getPredicateTypeNodeFromSignatureDeclaration(
|
|
152
|
+
declaration: ts.Declaration | undefined,
|
|
153
|
+
): ts.TypeNode | undefined {
|
|
154
|
+
if (!declaration) {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (
|
|
159
|
+
(ts.isFunctionTypeNode(declaration) ||
|
|
160
|
+
ts.isMethodSignature(declaration) ||
|
|
161
|
+
ts.isFunctionDeclaration(declaration) ||
|
|
162
|
+
ts.isMethodDeclaration(declaration)) &&
|
|
163
|
+
declaration.type &&
|
|
164
|
+
ts.isTypePredicateNode(declaration.type)
|
|
165
|
+
) {
|
|
166
|
+
return declaration.type.type;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function hasExplicitPredicateSurface(
|
|
173
|
+
declaration: ts.Declaration | undefined,
|
|
174
|
+
): boolean {
|
|
175
|
+
if (!declaration) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
(ts.isFunctionTypeNode(declaration) ||
|
|
181
|
+
ts.isMethodSignature(declaration) ||
|
|
182
|
+
ts.isFunctionDeclaration(declaration) ||
|
|
183
|
+
ts.isMethodDeclaration(declaration) ||
|
|
184
|
+
ts.isFunctionExpression(declaration) ||
|
|
185
|
+
ts.isArrowFunction(declaration)) &&
|
|
186
|
+
!!declaration.type &&
|
|
187
|
+
ts.isTypePredicateNode(declaration.type)
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function getContextualPredicateContract(
|
|
192
|
+
context: AnalysisContext,
|
|
193
|
+
declaration: PredicateSignatureDeclaration,
|
|
194
|
+
): ResolvedPredicateContract | undefined {
|
|
195
|
+
const contextualType = ts.isMethodDeclaration(declaration)
|
|
196
|
+
? getContextualMethodType(context, declaration)
|
|
197
|
+
: ts.isArrowFunction(declaration) || ts.isFunctionExpression(declaration)
|
|
198
|
+
? context.checker.getContextualType(declaration)
|
|
199
|
+
: undefined;
|
|
200
|
+
if (!contextualType) {
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const signature = context.checker.getSignaturesOfType(contextualType, ts.SignatureKind.Call)
|
|
205
|
+
.find((candidate) => context.checker.getTypePredicateOfSignature(candidate));
|
|
206
|
+
if (!signature) {
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const predicate = context.checker.getTypePredicateOfSignature(signature);
|
|
211
|
+
if (!predicate) {
|
|
212
|
+
return undefined;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const contextualTypeNode = getContextualFunctionTypeNode(declaration);
|
|
216
|
+
const signatureDeclaration = signature.getDeclaration();
|
|
217
|
+
if (
|
|
218
|
+
!(contextualTypeNode &&
|
|
219
|
+
ts.isTypePredicateNode(contextualTypeNode.type)) &&
|
|
220
|
+
!hasExplicitPredicateSurface(signatureDeclaration)
|
|
221
|
+
) {
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const forbiddenPredicateTypeNode = contextualTypeNode &&
|
|
226
|
+
ts.isTypePredicateNode(contextualTypeNode.type)
|
|
227
|
+
? contextualTypeNode.type.type
|
|
228
|
+
: getPredicateTypeNodeFromSignatureDeclaration(signatureDeclaration);
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
forbiddenPredicateTypeNode,
|
|
232
|
+
predicate,
|
|
233
|
+
signature,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function getResolvedPredicateContract(
|
|
238
|
+
context: AnalysisContext,
|
|
239
|
+
declaration: PredicateSignatureDeclaration,
|
|
240
|
+
): ResolvedPredicateContract | undefined {
|
|
241
|
+
return getDeclaredPredicateContract(context, declaration) ??
|
|
242
|
+
getContextualPredicateContract(context, declaration);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function getSupportedPrimitive(type: ts.Type): SupportedPrimitive | undefined {
|
|
246
|
+
if ((type.flags & ts.TypeFlags.Union) !== 0) {
|
|
247
|
+
const primitiveOptions = new Set(
|
|
248
|
+
(type as ts.UnionType).types
|
|
249
|
+
.map((part) => getSupportedPrimitive(part))
|
|
250
|
+
.filter((primitive): primitive is SupportedPrimitive => primitive !== undefined),
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
return primitiveOptions.size === 1 ? primitiveOptions.values().next().value : undefined;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if ((type.flags & ts.TypeFlags.BigIntLike) !== 0) {
|
|
257
|
+
return 'bigint';
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if ((type.flags & ts.TypeFlags.BooleanLike) !== 0) {
|
|
261
|
+
return 'boolean';
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if ((type.flags & ts.TypeFlags.NumberLike) !== 0) {
|
|
265
|
+
return 'number';
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if ((type.flags & ts.TypeFlags.StringLike) !== 0) {
|
|
269
|
+
return 'string';
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (
|
|
273
|
+
(type.flags & ts.TypeFlags.ESSymbolLike) !== 0 ||
|
|
274
|
+
(type.flags & ts.TypeFlags.UniqueESSymbol) !== 0
|
|
275
|
+
) {
|
|
276
|
+
return 'symbol';
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return undefined;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function isPrimitiveLiteralLike(type: ts.Type): boolean {
|
|
283
|
+
return (type.flags & ts.TypeFlags.StringLiteral) !== 0 ||
|
|
284
|
+
(type.flags & ts.TypeFlags.NumberLiteral) !== 0 ||
|
|
285
|
+
(type.flags & ts.TypeFlags.BooleanLiteral) !== 0 ||
|
|
286
|
+
(type.flags & ts.TypeFlags.BigIntLiteral) !== 0 ||
|
|
287
|
+
(type.flags & ts.TypeFlags.UniqueESSymbol) !== 0;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function isCompleteBooleanDomain(type: ts.Type): boolean {
|
|
291
|
+
const constituents = (type.flags & ts.TypeFlags.Union) !== 0
|
|
292
|
+
? (type as ts.UnionType).types
|
|
293
|
+
: [type];
|
|
294
|
+
let hasTrue = false;
|
|
295
|
+
let hasFalse = false;
|
|
296
|
+
|
|
297
|
+
for (const part of constituents) {
|
|
298
|
+
if ((part.flags & ts.TypeFlags.BooleanLiteral) === 0) {
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const intrinsicName = (part as ts.Type & { intrinsicName?: string }).intrinsicName;
|
|
303
|
+
if (intrinsicName === 'true') {
|
|
304
|
+
hasTrue = true;
|
|
305
|
+
} else if (intrinsicName === 'false') {
|
|
306
|
+
hasFalse = true;
|
|
307
|
+
} else {
|
|
308
|
+
return false;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return hasTrue && hasFalse;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function hasBroadSupportedPrimitiveConstituent(
|
|
316
|
+
type: ts.Type,
|
|
317
|
+
primitive: SupportedPrimitive,
|
|
318
|
+
): boolean {
|
|
319
|
+
const constituents = (type.flags & ts.TypeFlags.Union) !== 0
|
|
320
|
+
? (type as ts.UnionType).types
|
|
321
|
+
: [type];
|
|
322
|
+
|
|
323
|
+
return constituents.some((part) =>
|
|
324
|
+
getSupportedPrimitive(part) === primitive && !isPrimitiveLiteralLike(part)
|
|
325
|
+
) || (primitive === 'boolean' && isCompleteBooleanDomain(type));
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function isExactObjectPredicateTarget(type: ts.Type): boolean {
|
|
329
|
+
return (type.flags & ts.TypeFlags.NonPrimitive) !== 0;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function getReferenceTypeArguments(
|
|
333
|
+
context: AnalysisContext,
|
|
334
|
+
type: ts.Type,
|
|
335
|
+
): readonly ts.Type[] {
|
|
336
|
+
if ((type.flags & ts.TypeFlags.Object) === 0) {
|
|
337
|
+
return [];
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const objectType = type as ts.ObjectType;
|
|
341
|
+
if ((objectType.objectFlags & ts.ObjectFlags.Reference) === 0) {
|
|
342
|
+
return [];
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return context.checker.getTypeArguments(objectType as ts.TypeReference);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function resolveAliasedSymbol(checker: ts.TypeChecker, symbol: ts.Symbol): ts.Symbol {
|
|
349
|
+
let current = symbol;
|
|
350
|
+
|
|
351
|
+
while ((current.flags & ts.SymbolFlags.Alias) !== 0) {
|
|
352
|
+
const aliased = checker.getAliasedSymbol(current);
|
|
353
|
+
if (aliased === current) {
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
current = aliased;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return current;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function classifySupportedPredicateTarget(
|
|
364
|
+
context: AnalysisContext,
|
|
365
|
+
type: ts.Type,
|
|
366
|
+
): PredicateSupportedTarget | undefined {
|
|
367
|
+
const primitive = getSupportedPrimitive(type);
|
|
368
|
+
if (primitive) {
|
|
369
|
+
if (!hasBroadSupportedPrimitiveConstituent(type, primitive)) {
|
|
370
|
+
return undefined;
|
|
371
|
+
}
|
|
372
|
+
return { kind: 'primitive', primitive };
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if ((type.flags & ts.TypeFlags.Union) !== 0) {
|
|
376
|
+
const options = (type as ts.UnionType).types
|
|
377
|
+
.map((part) => classifySupportedPredicateTarget(context, part))
|
|
378
|
+
.filter((option): option is PredicateSupportedTarget => option !== undefined);
|
|
379
|
+
|
|
380
|
+
if (options.length > 1 && options.length === (type as ts.UnionType).types.length) {
|
|
381
|
+
return { kind: 'unionOfSupported', options };
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return undefined;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const normalizedType = context.checker.getNonNullableType(type);
|
|
388
|
+
if (isExactObjectPredicateTarget(type)) {
|
|
389
|
+
return context.checker.isArrayType(normalizedType) || context.checker.isTupleType(normalizedType)
|
|
390
|
+
? undefined
|
|
391
|
+
: { kind: 'nonNullObject' };
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if ((normalizedType.flags & ts.TypeFlags.Object) === 0) {
|
|
395
|
+
return undefined;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (context.checker.isArrayType(normalizedType) || context.checker.isTupleType(normalizedType)) {
|
|
399
|
+
return undefined;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if ((normalizedType.flags & ts.TypeFlags.TypeParameter) !== 0) {
|
|
403
|
+
return undefined;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (getReferenceTypeArguments(context, normalizedType).length > 0) {
|
|
407
|
+
return undefined;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const symbol = normalizedType.getSymbol();
|
|
411
|
+
if (!symbol) {
|
|
412
|
+
return undefined;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
return {
|
|
416
|
+
kind: 'instanceof',
|
|
417
|
+
constructorSymbol: resolveAliasedSymbol(context.checker, symbol),
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
export function classifyPredicateVerificationTarget(
|
|
422
|
+
context: AnalysisContext,
|
|
423
|
+
declaration: PredicateSignatureDeclaration,
|
|
424
|
+
): PredicateVerificationTargetFact | undefined {
|
|
425
|
+
return context.facts.getPredicateVerificationTarget(declaration, () => {
|
|
426
|
+
const contract = getResolvedPredicateContract(context, declaration);
|
|
427
|
+
if (!contract) {
|
|
428
|
+
return undefined;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const predicate = contract.predicate;
|
|
432
|
+
|
|
433
|
+
if (
|
|
434
|
+
predicate.kind === ts.TypePredicateKind.AssertsThis ||
|
|
435
|
+
predicate.kind === ts.TypePredicateKind.This
|
|
436
|
+
) {
|
|
437
|
+
return {
|
|
438
|
+
kind: 'unsupported',
|
|
439
|
+
reason: 'receiverPredicate',
|
|
440
|
+
subject: 'receiver',
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
if (predicate.parameterIndex === undefined || !predicate.type) {
|
|
445
|
+
return {
|
|
446
|
+
kind: 'unsupported',
|
|
447
|
+
reason: 'assertsCondition',
|
|
448
|
+
subject: 'parameter',
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const parameter = declaration.parameters[predicate.parameterIndex];
|
|
453
|
+
if (!parameter || !ts.isIdentifier(parameter.name)) {
|
|
454
|
+
return {
|
|
455
|
+
kind: 'unsupported',
|
|
456
|
+
reason: 'unsupportedParameterName',
|
|
457
|
+
subject: 'parameter',
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const target = classifySupportedPredicateTarget(context, predicate.type);
|
|
462
|
+
if (!target) {
|
|
463
|
+
return {
|
|
464
|
+
kind: 'unsupported',
|
|
465
|
+
reason: 'unsupportedTarget',
|
|
466
|
+
subject: 'parameter',
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return {
|
|
471
|
+
kind: 'supported',
|
|
472
|
+
subject: 'parameter',
|
|
473
|
+
target,
|
|
474
|
+
};
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
export function getPredicateCheck(
|
|
479
|
+
context: AnalysisContext,
|
|
480
|
+
declaration: SignatureDeclarationWithBody,
|
|
481
|
+
): PredicateCheck | undefined {
|
|
482
|
+
return getPredicateCheckForSignature(context, declaration, declaration);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
export function getPredicateCheckForSignature(
|
|
486
|
+
context: AnalysisContext,
|
|
487
|
+
signatureDeclaration: PredicateSignatureDeclaration,
|
|
488
|
+
bodyDeclaration: SignatureDeclarationWithBody,
|
|
489
|
+
): PredicateCheck | undefined {
|
|
490
|
+
const body = bodyDeclaration.body;
|
|
491
|
+
if (!body) {
|
|
492
|
+
return undefined;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
const classification = classifyPredicateVerificationTarget(context, signatureDeclaration);
|
|
496
|
+
if (!classification || classification.kind !== 'supported') {
|
|
497
|
+
return undefined;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const contract = getResolvedPredicateContract(context, signatureDeclaration);
|
|
501
|
+
if (!contract) {
|
|
502
|
+
return undefined;
|
|
503
|
+
}
|
|
504
|
+
const predicate = contract.predicate;
|
|
505
|
+
if (predicate.parameterIndex === undefined || !predicate.type) {
|
|
506
|
+
return undefined;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
const parameter = bodyDeclaration.parameters[predicate.parameterIndex];
|
|
510
|
+
if (!parameter || !ts.isIdentifier(parameter.name)) {
|
|
511
|
+
return undefined;
|
|
512
|
+
}
|
|
513
|
+
const parameterType = context.checker.getTypeAtLocation(parameter);
|
|
514
|
+
|
|
515
|
+
return {
|
|
516
|
+
body,
|
|
517
|
+
declaration: bodyDeclaration,
|
|
518
|
+
parameterName: parameter.name.text,
|
|
519
|
+
parameterType,
|
|
520
|
+
predicateKind: predicate.kind,
|
|
521
|
+
predicateType: predicate.type,
|
|
522
|
+
target: classification.target,
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
export function getForbiddenPredicateTypeNode(
|
|
527
|
+
context: AnalysisContext,
|
|
528
|
+
node: PredicateSignatureDeclaration,
|
|
529
|
+
): ts.TypeNode | undefined {
|
|
530
|
+
const predicateTypeNode = getResolvedPredicateContract(context, node)?.forbiddenPredicateTypeNode;
|
|
531
|
+
return predicateTypeNode?.kind === ts.SyntaxKind.AnyKeyword ? predicateTypeNode : undefined;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
export function requiresPredicateVerification(
|
|
535
|
+
node: PredicateSignatureDeclaration,
|
|
536
|
+
): boolean {
|
|
537
|
+
return !!node.type && ts.isTypePredicateNode(node.type);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
export function hasPredicateVerificationContract(
|
|
541
|
+
context: AnalysisContext,
|
|
542
|
+
node: PredicateSignatureDeclaration,
|
|
543
|
+
): boolean {
|
|
544
|
+
return requiresPredicateVerification(node) || getContextualPredicateContract(context, node) !== undefined;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function isIdentifierReference(expression: ts.Expression, identifierName: string): boolean {
|
|
548
|
+
return ts.isIdentifier(expression) && expression.text === identifierName;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
function isStringLiteralValue(expression: ts.Expression, value: string): boolean {
|
|
552
|
+
return ts.isStringLiteral(expression) && expression.text === value;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
function isTypeofCheck(
|
|
556
|
+
expression: ts.Expression,
|
|
557
|
+
parameterName: string,
|
|
558
|
+
primitive: SupportedTypeof,
|
|
559
|
+
equality: 'equal' | 'not-equal',
|
|
560
|
+
): boolean {
|
|
561
|
+
if (!ts.isBinaryExpression(expression)) {
|
|
562
|
+
return false;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
const operatorMatches = equality === 'equal'
|
|
566
|
+
? expression.operatorToken.kind === ts.SyntaxKind.EqualsEqualsEqualsToken
|
|
567
|
+
: expression.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsEqualsToken;
|
|
568
|
+
if (!operatorMatches) {
|
|
569
|
+
return false;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
const leftMatches = expression.left.kind === ts.SyntaxKind.TypeOfExpression &&
|
|
573
|
+
isIdentifierReference((expression.left as ts.TypeOfExpression).expression, parameterName) &&
|
|
574
|
+
isStringLiteralValue(expression.right, primitive);
|
|
575
|
+
const rightMatches = expression.right.kind === ts.SyntaxKind.TypeOfExpression &&
|
|
576
|
+
isStringLiteralValue(expression.left, primitive) &&
|
|
577
|
+
isIdentifierReference((expression.right as ts.TypeOfExpression).expression, parameterName);
|
|
578
|
+
|
|
579
|
+
return leftMatches || rightMatches;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
function isNullCheck(
|
|
583
|
+
expression: ts.Expression,
|
|
584
|
+
parameterName: string,
|
|
585
|
+
equality: 'equal' | 'not-equal',
|
|
586
|
+
): boolean {
|
|
587
|
+
if (!ts.isBinaryExpression(expression)) {
|
|
588
|
+
return false;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
const operatorMatches = equality === 'equal'
|
|
592
|
+
? expression.operatorToken.kind === ts.SyntaxKind.EqualsEqualsEqualsToken
|
|
593
|
+
: expression.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsEqualsToken;
|
|
594
|
+
if (!operatorMatches) {
|
|
595
|
+
return false;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
const leftMatches = isIdentifierReference(expression.left, parameterName) &&
|
|
599
|
+
expression.right.kind === ts.SyntaxKind.NullKeyword;
|
|
600
|
+
const rightMatches = expression.left.kind === ts.SyntaxKind.NullKeyword &&
|
|
601
|
+
isIdentifierReference(expression.right, parameterName);
|
|
602
|
+
|
|
603
|
+
return leftMatches || rightMatches;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
function getRuntimeConstructorSymbol(
|
|
607
|
+
context: AnalysisContext,
|
|
608
|
+
expression: ts.Expression,
|
|
609
|
+
): ts.Symbol | undefined {
|
|
610
|
+
if (ts.isIdentifier(expression)) {
|
|
611
|
+
const symbol = context.checker.getSymbolAtLocation(expression);
|
|
612
|
+
return symbol ? resolveAliasedSymbol(context.checker, symbol) : undefined;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
if (ts.isPropertyAccessExpression(expression)) {
|
|
616
|
+
const symbol = context.checker.getSymbolAtLocation(expression.name);
|
|
617
|
+
return symbol ? resolveAliasedSymbol(context.checker, symbol) : undefined;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
return undefined;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
function isInstanceofCheck(
|
|
624
|
+
context: AnalysisContext,
|
|
625
|
+
expression: ts.Expression,
|
|
626
|
+
parameterName: string,
|
|
627
|
+
constructorSymbol: ts.Symbol | undefined,
|
|
628
|
+
): boolean {
|
|
629
|
+
if (!constructorSymbol) {
|
|
630
|
+
return false;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
return ts.isBinaryExpression(expression) &&
|
|
634
|
+
expression.operatorToken.kind === ts.SyntaxKind.InstanceOfKeyword &&
|
|
635
|
+
isIdentifierReference(expression.left, parameterName) &&
|
|
636
|
+
getRuntimeConstructorSymbol(context, expression.right) === constructorSymbol;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
function flattenConjunction(expression: ts.Expression): readonly ts.Expression[] {
|
|
640
|
+
if (
|
|
641
|
+
ts.isBinaryExpression(expression) &&
|
|
642
|
+
expression.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken
|
|
643
|
+
) {
|
|
644
|
+
return [...flattenConjunction(expression.left), ...flattenConjunction(expression.right)];
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
return [expression];
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
function getCompositeTypeConstituents(type: ts.Type): readonly ts.Type[] {
|
|
651
|
+
if ((type.flags & ts.TypeFlags.Union) !== 0) {
|
|
652
|
+
return (type as ts.UnionType).types;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
if ((type.flags & ts.TypeFlags.Intersection) !== 0) {
|
|
656
|
+
return (type as ts.IntersectionType).types;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
return [type];
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
function hasUniqueLiteralPropertyValue(
|
|
663
|
+
context: AnalysisContext,
|
|
664
|
+
parameterType: ts.Type,
|
|
665
|
+
predicateType: ts.Type,
|
|
666
|
+
propertyName: string,
|
|
667
|
+
literalValue: string,
|
|
668
|
+
location: ts.Node,
|
|
669
|
+
): boolean {
|
|
670
|
+
let matchCount = 0;
|
|
671
|
+
|
|
672
|
+
for (
|
|
673
|
+
const constituentType of getCompositeTypeConstituents(
|
|
674
|
+
context.checker.getNonNullableType(parameterType),
|
|
675
|
+
)
|
|
676
|
+
) {
|
|
677
|
+
const propertySymbol = context.checker.getPropertyOfType(constituentType, propertyName);
|
|
678
|
+
if (!propertySymbol) {
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
const propertyType = context.checker.getTypeOfSymbolAtLocation(propertySymbol, location);
|
|
683
|
+
if ((propertyType.flags & ts.TypeFlags.StringLiteral) === 0) {
|
|
684
|
+
continue;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
if ((propertyType as ts.StringLiteralType).value !== literalValue) {
|
|
688
|
+
continue;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
if (!context.checker.isTypeAssignableTo(constituentType, predicateType)) {
|
|
692
|
+
return false;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
matchCount++;
|
|
696
|
+
if (matchCount > 1) {
|
|
697
|
+
return false;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
return matchCount === 1;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
function matchesPositiveCondition(
|
|
705
|
+
context: AnalysisContext,
|
|
706
|
+
expression: ts.Expression,
|
|
707
|
+
parameterName: string,
|
|
708
|
+
parameterType: ts.Type,
|
|
709
|
+
predicateType: ts.Type,
|
|
710
|
+
target: PredicateSupportedTarget,
|
|
711
|
+
): boolean {
|
|
712
|
+
switch (target.kind) {
|
|
713
|
+
case 'primitive':
|
|
714
|
+
return isTypeofCheck(expression, parameterName, target.primitive, 'equal');
|
|
715
|
+
case 'nonNullObject': {
|
|
716
|
+
const parts = flattenConjunction(expression);
|
|
717
|
+
const hasObjectCheck = parts.some((part) =>
|
|
718
|
+
isTypeofCheck(part, parameterName, 'object', 'equal')
|
|
719
|
+
);
|
|
720
|
+
const hasNonNullCheck = parts.some((part) => isNullCheck(part, parameterName, 'not-equal'));
|
|
721
|
+
return parts.length === 2 && hasObjectCheck && hasNonNullCheck;
|
|
722
|
+
}
|
|
723
|
+
case 'instanceof': {
|
|
724
|
+
if (isInstanceofCheck(context, expression, parameterName, target.constructorSymbol)) {
|
|
725
|
+
return true;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
if (!ts.isBinaryExpression(expression)) {
|
|
729
|
+
return false;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
if (expression.operatorToken.kind !== ts.SyntaxKind.EqualsEqualsEqualsToken) {
|
|
733
|
+
return false;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
const propertyAccess = ts.isPropertyAccessExpression(expression.left) &&
|
|
737
|
+
isIdentifierReference(expression.left.expression, parameterName) &&
|
|
738
|
+
ts.isStringLiteral(expression.right)
|
|
739
|
+
? expression.left
|
|
740
|
+
: ts.isPropertyAccessExpression(expression.right) &&
|
|
741
|
+
isIdentifierReference(expression.right.expression, parameterName) &&
|
|
742
|
+
ts.isStringLiteral(expression.left)
|
|
743
|
+
? expression.right
|
|
744
|
+
: undefined;
|
|
745
|
+
const literal = ts.isStringLiteral(expression.right)
|
|
746
|
+
? expression.right
|
|
747
|
+
: ts.isStringLiteral(expression.left)
|
|
748
|
+
? expression.left
|
|
749
|
+
: undefined;
|
|
750
|
+
if (!propertyAccess || !literal) {
|
|
751
|
+
return false;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
const propertySymbol = context.checker.getPropertyOfType(
|
|
755
|
+
predicateType,
|
|
756
|
+
propertyAccess.name.text,
|
|
757
|
+
);
|
|
758
|
+
if (!propertySymbol) {
|
|
759
|
+
return false;
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
const propertyType = context.checker.getTypeOfSymbolAtLocation(
|
|
763
|
+
propertySymbol,
|
|
764
|
+
propertyAccess.name,
|
|
765
|
+
);
|
|
766
|
+
return (propertyType.flags & ts.TypeFlags.StringLiteral) !== 0 &&
|
|
767
|
+
(propertyType as ts.StringLiteralType).value === literal.text &&
|
|
768
|
+
hasUniqueLiteralPropertyValue(
|
|
769
|
+
context,
|
|
770
|
+
parameterType,
|
|
771
|
+
predicateType,
|
|
772
|
+
propertyAccess.name.text,
|
|
773
|
+
literal.text,
|
|
774
|
+
propertyAccess.name,
|
|
775
|
+
);
|
|
776
|
+
}
|
|
777
|
+
case 'unionOfSupported': {
|
|
778
|
+
const remainingTargets = [...target.options];
|
|
779
|
+
const queue = [expression];
|
|
780
|
+
|
|
781
|
+
while (queue.length > 0) {
|
|
782
|
+
const current = queue.pop();
|
|
783
|
+
if (!current) {
|
|
784
|
+
break;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
if (
|
|
788
|
+
ts.isBinaryExpression(current) && current.operatorToken.kind === ts.SyntaxKind.BarBarToken
|
|
789
|
+
) {
|
|
790
|
+
queue.push(current.left, current.right);
|
|
791
|
+
continue;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
const matchIndex = remainingTargets.findIndex((option) =>
|
|
795
|
+
matchesPositiveCondition(
|
|
796
|
+
context,
|
|
797
|
+
current,
|
|
798
|
+
parameterName,
|
|
799
|
+
parameterType,
|
|
800
|
+
predicateType,
|
|
801
|
+
option,
|
|
802
|
+
)
|
|
803
|
+
);
|
|
804
|
+
if (matchIndex === -1) {
|
|
805
|
+
return false;
|
|
806
|
+
}
|
|
807
|
+
remainingTargets.splice(matchIndex, 1);
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
return remainingTargets.length === 0;
|
|
811
|
+
}
|
|
812
|
+
default: {
|
|
813
|
+
const exhaustiveCheck: never = target;
|
|
814
|
+
return exhaustiveCheck;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
function matchesNegativeCondition(
|
|
820
|
+
context: AnalysisContext,
|
|
821
|
+
expression: ts.Expression,
|
|
822
|
+
parameterName: string,
|
|
823
|
+
target: PredicateSupportedTarget,
|
|
824
|
+
): boolean {
|
|
825
|
+
switch (target.kind) {
|
|
826
|
+
case 'primitive':
|
|
827
|
+
return isTypeofCheck(expression, parameterName, target.primitive, 'not-equal');
|
|
828
|
+
case 'nonNullObject':
|
|
829
|
+
return isTypeofCheck(expression, parameterName, 'object', 'not-equal') ||
|
|
830
|
+
isNullCheck(expression, parameterName, 'equal');
|
|
831
|
+
case 'instanceof':
|
|
832
|
+
return ts.isPrefixUnaryExpression(expression) &&
|
|
833
|
+
expression.operator === ts.SyntaxKind.ExclamationToken &&
|
|
834
|
+
isInstanceofCheck(context, expression.operand, parameterName, target.constructorSymbol);
|
|
835
|
+
case 'unionOfSupported': {
|
|
836
|
+
const remainingTargets = [...target.options];
|
|
837
|
+
|
|
838
|
+
for (const part of flattenConjunction(expression)) {
|
|
839
|
+
const matchIndex = remainingTargets.findIndex((option) =>
|
|
840
|
+
matchesNegativeCondition(context, part, parameterName, option)
|
|
841
|
+
);
|
|
842
|
+
if (matchIndex === -1) {
|
|
843
|
+
return false;
|
|
844
|
+
}
|
|
845
|
+
remainingTargets.splice(matchIndex, 1);
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
return remainingTargets.length === 0;
|
|
849
|
+
}
|
|
850
|
+
default: {
|
|
851
|
+
const exhaustiveCheck: never = target;
|
|
852
|
+
return exhaustiveCheck;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
function unwrapSingleStatement(statement: ts.Statement): ts.Statement {
|
|
858
|
+
if (ts.isBlock(statement) && statement.statements.length === 1) {
|
|
859
|
+
return unwrapSingleStatement(statement.statements[0]);
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
return statement;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
function isBooleanReturn(statement: ts.Statement, expected: boolean): boolean {
|
|
866
|
+
const unwrappedStatement = unwrapSingleStatement(statement);
|
|
867
|
+
return ts.isReturnStatement(unwrappedStatement) &&
|
|
868
|
+
!!unwrappedStatement.expression &&
|
|
869
|
+
(expected
|
|
870
|
+
? unwrappedStatement.expression.kind === ts.SyntaxKind.TrueKeyword
|
|
871
|
+
: unwrappedStatement.expression.kind === ts.SyntaxKind.FalseKeyword);
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
function isThrowStatement(statement: ts.Statement): boolean {
|
|
875
|
+
return ts.isThrowStatement(unwrapSingleStatement(statement));
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
function getObjectNegativeCoverage(
|
|
879
|
+
expression: ts.Expression,
|
|
880
|
+
parameterName: string,
|
|
881
|
+
): { readonly typeofNotObject: boolean; readonly nullCheck: boolean } | undefined {
|
|
882
|
+
if (
|
|
883
|
+
ts.isBinaryExpression(expression) &&
|
|
884
|
+
expression.operatorToken.kind === ts.SyntaxKind.BarBarToken
|
|
885
|
+
) {
|
|
886
|
+
const left = getObjectNegativeCoverage(expression.left, parameterName);
|
|
887
|
+
const right = getObjectNegativeCoverage(expression.right, parameterName);
|
|
888
|
+
if (!left || !right) {
|
|
889
|
+
return undefined;
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
return {
|
|
893
|
+
typeofNotObject: left.typeofNotObject || right.typeofNotObject,
|
|
894
|
+
nullCheck: left.nullCheck || right.nullCheck,
|
|
895
|
+
};
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
if (isTypeofCheck(expression, parameterName, 'object', 'not-equal')) {
|
|
899
|
+
return { typeofNotObject: true, nullCheck: false };
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
if (isNullCheck(expression, parameterName, 'equal')) {
|
|
903
|
+
return { typeofNotObject: false, nullCheck: true };
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
return undefined;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
function verifyNegativeGuardBody(
|
|
910
|
+
context: AnalysisContext,
|
|
911
|
+
statements: readonly ts.Statement[],
|
|
912
|
+
parameterName: string,
|
|
913
|
+
target: PredicateSupportedTarget,
|
|
914
|
+
guardKind: 'return-false' | 'throw',
|
|
915
|
+
): boolean {
|
|
916
|
+
if (statements.length === 0) {
|
|
917
|
+
return false;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
const finalStatement = statements.at(-1);
|
|
921
|
+
const guardStatements = guardKind === 'return-false' ? statements.slice(0, -1) : statements;
|
|
922
|
+
|
|
923
|
+
if (guardKind === 'return-false' && (!finalStatement || !isBooleanReturn(finalStatement, true))) {
|
|
924
|
+
return false;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
if (target.kind === 'nonNullObject') {
|
|
928
|
+
let sawTypeofNotObject = false;
|
|
929
|
+
let sawNullCheck = false;
|
|
930
|
+
|
|
931
|
+
for (const statement of guardStatements) {
|
|
932
|
+
if (!ts.isIfStatement(statement) || statement.elseStatement) {
|
|
933
|
+
return false;
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
if (guardKind === 'return-false' && !isBooleanReturn(statement.thenStatement, false)) {
|
|
937
|
+
return false;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
if (guardKind === 'throw' && !isThrowStatement(statement.thenStatement)) {
|
|
941
|
+
return false;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
const coverage = getObjectNegativeCoverage(statement.expression, parameterName);
|
|
945
|
+
if (!coverage) {
|
|
946
|
+
return false;
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
sawTypeofNotObject ||= coverage.typeofNotObject;
|
|
950
|
+
sawNullCheck ||= coverage.nullCheck;
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
return sawTypeofNotObject && sawNullCheck;
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
return guardStatements.every((statement) => {
|
|
957
|
+
if (!ts.isIfStatement(statement) || statement.elseStatement) {
|
|
958
|
+
return false;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
if (guardKind === 'return-false' && !isBooleanReturn(statement.thenStatement, false)) {
|
|
962
|
+
return false;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
if (guardKind === 'throw' && !isThrowStatement(statement.thenStatement)) {
|
|
966
|
+
return false;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
return matchesNegativeCondition(context, statement.expression, parameterName, target);
|
|
970
|
+
});
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
function verifyPositiveBranchBody(
|
|
974
|
+
context: AnalysisContext,
|
|
975
|
+
statements: readonly ts.Statement[],
|
|
976
|
+
parameterName: string,
|
|
977
|
+
parameterType: ts.Type,
|
|
978
|
+
predicateType: ts.Type,
|
|
979
|
+
target: PredicateSupportedTarget,
|
|
980
|
+
): boolean {
|
|
981
|
+
if (statements.length < 2) {
|
|
982
|
+
return false;
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
const finalStatement = statements.at(-1);
|
|
986
|
+
if (!finalStatement || !isBooleanReturn(finalStatement, false)) {
|
|
987
|
+
return false;
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
const branchConditions: ts.Expression[] = [];
|
|
991
|
+
|
|
992
|
+
for (const statement of statements.slice(0, -1)) {
|
|
993
|
+
if (
|
|
994
|
+
!ts.isIfStatement(statement) || statement.elseStatement ||
|
|
995
|
+
!isBooleanReturn(statement.thenStatement, true)
|
|
996
|
+
) {
|
|
997
|
+
return false;
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
branchConditions.push(statement.expression);
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
if (target.kind === 'unionOfSupported') {
|
|
1004
|
+
const remainingTargets = [...target.options];
|
|
1005
|
+
|
|
1006
|
+
for (const condition of branchConditions) {
|
|
1007
|
+
const matchIndex = remainingTargets.findIndex((option) =>
|
|
1008
|
+
matchesPositiveCondition(
|
|
1009
|
+
context,
|
|
1010
|
+
condition,
|
|
1011
|
+
parameterName,
|
|
1012
|
+
parameterType,
|
|
1013
|
+
predicateType,
|
|
1014
|
+
option,
|
|
1015
|
+
)
|
|
1016
|
+
);
|
|
1017
|
+
if (matchIndex === -1) {
|
|
1018
|
+
return false;
|
|
1019
|
+
}
|
|
1020
|
+
remainingTargets.splice(matchIndex, 1);
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
return remainingTargets.length === 0;
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
return branchConditions.length === 1 &&
|
|
1027
|
+
matchesPositiveCondition(
|
|
1028
|
+
context,
|
|
1029
|
+
branchConditions[0],
|
|
1030
|
+
parameterName,
|
|
1031
|
+
parameterType,
|
|
1032
|
+
predicateType,
|
|
1033
|
+
target,
|
|
1034
|
+
);
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
export function verifyPredicateBody(context: AnalysisContext, check: PredicateCheck): boolean {
|
|
1038
|
+
if (!ts.isBlock(check.body)) {
|
|
1039
|
+
if (check.predicateKind !== ts.TypePredicateKind.Identifier) {
|
|
1040
|
+
return false;
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
return matchesPositiveCondition(
|
|
1044
|
+
context,
|
|
1045
|
+
check.body,
|
|
1046
|
+
check.parameterName,
|
|
1047
|
+
check.parameterType,
|
|
1048
|
+
check.predicateType,
|
|
1049
|
+
check.target,
|
|
1050
|
+
);
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
const statements = check.body.statements;
|
|
1054
|
+
|
|
1055
|
+
if (statements.length === 1 && ts.isReturnStatement(statements[0]) && statements[0].expression) {
|
|
1056
|
+
return matchesPositiveCondition(
|
|
1057
|
+
context,
|
|
1058
|
+
statements[0].expression,
|
|
1059
|
+
check.parameterName,
|
|
1060
|
+
check.parameterType,
|
|
1061
|
+
check.predicateType,
|
|
1062
|
+
check.target,
|
|
1063
|
+
);
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
return verifyNegativeGuardBody(
|
|
1067
|
+
context,
|
|
1068
|
+
statements,
|
|
1069
|
+
check.parameterName,
|
|
1070
|
+
check.target,
|
|
1071
|
+
'return-false',
|
|
1072
|
+
) ||
|
|
1073
|
+
verifyNegativeGuardBody(
|
|
1074
|
+
context,
|
|
1075
|
+
statements,
|
|
1076
|
+
check.parameterName,
|
|
1077
|
+
check.target,
|
|
1078
|
+
'throw',
|
|
1079
|
+
) ||
|
|
1080
|
+
verifyPositiveBranchBody(
|
|
1081
|
+
context,
|
|
1082
|
+
statements,
|
|
1083
|
+
check.parameterName,
|
|
1084
|
+
check.parameterType,
|
|
1085
|
+
check.predicateType,
|
|
1086
|
+
check.target,
|
|
1087
|
+
);
|
|
1088
|
+
}
|