funkscript 0.2.4 → 0.2.6
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/README.md +23 -5
- package/dist/ast/types.d.ts.map +1 -1
- package/dist/ast/types.js.map +1 -1
- package/dist/ast/walk.d.ts.map +1 -1
- package/dist/ast/walk.js.map +1 -1
- package/dist/cli/build-bun.d.ts.map +1 -1
- package/dist/cli/build-bun.js.map +1 -1
- package/dist/cli/build-watch.d.ts.map +1 -1
- package/dist/cli/build-watch.js +5 -3
- package/dist/cli/build-watch.js.map +1 -1
- package/dist/cli/build.d.ts +1 -1
- package/dist/cli/build.d.ts.map +1 -1
- package/dist/cli/build.js.map +1 -1
- package/dist/cli/check.d.ts.map +1 -1
- package/dist/cli/check.js.map +1 -1
- package/dist/cli/commands.d.ts.map +1 -1
- package/dist/cli/commands.js.map +1 -1
- package/dist/cli/doc.d.ts.map +1 -1
- package/dist/cli/doc.js.map +1 -1
- package/dist/cli/doctor.d.ts.map +1 -1
- package/dist/cli/doctor.js +4 -2
- package/dist/cli/doctor.js.map +1 -1
- package/dist/cli/emit.d.ts +1 -1
- package/dist/cli/emit.d.ts.map +1 -1
- package/dist/cli/emit.js.map +1 -1
- package/dist/cli/explain.d.ts.map +1 -1
- package/dist/cli/explain.js.map +1 -1
- package/dist/cli/fmt.d.ts.map +1 -1
- package/dist/cli/fmt.js.map +1 -1
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +0 -0
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/init.d.ts +1 -1
- package/dist/cli/init.d.ts.map +1 -1
- package/dist/cli/init.js +66 -21
- package/dist/cli/init.js.map +1 -1
- package/dist/cli/io.d.ts.map +1 -1
- package/dist/cli/io.js.map +1 -1
- package/dist/cli/pipeline-style.d.ts.map +1 -1
- package/dist/cli/pipeline-style.js.map +1 -1
- package/dist/cli/repl.d.ts.map +1 -1
- package/dist/cli/repl.js.map +1 -1
- package/dist/cli/run.d.ts.map +1 -1
- package/dist/cli/run.js.map +1 -1
- package/dist/cli/test.d.ts +1 -1
- package/dist/cli/test.d.ts.map +1 -1
- package/dist/cli/test.js.map +1 -1
- package/dist/cli/tsc.d.ts.map +1 -1
- package/dist/cli/tsc.js +5 -3
- package/dist/cli/tsc.js.map +1 -1
- package/dist/cli/typecheck.d.ts.map +1 -1
- package/dist/cli/typecheck.js.map +1 -1
- package/dist/compiler/ast-inspection.d.ts.map +1 -1
- package/dist/compiler/ast-inspection.js.map +1 -1
- package/dist/compiler/boolean-operators.d.ts.map +1 -1
- package/dist/compiler/boolean-operators.js +12 -2
- package/dist/compiler/boolean-operators.js.map +1 -1
- package/dist/compiler/check.d.ts.map +1 -1
- package/dist/compiler/check.js +95 -103
- package/dist/compiler/check.js.map +1 -1
- package/dist/compiler/diagnostics.d.ts.map +1 -1
- package/dist/compiler/diagnostics.js.map +1 -1
- package/dist/compiler/effect-mutation-rule.d.ts.map +1 -1
- package/dist/compiler/effect-mutation-rule.js.map +1 -1
- package/dist/compiler/entry-rules.d.ts.map +1 -1
- package/dist/compiler/entry-rules.js.map +1 -1
- package/dist/compiler/incremental-build.d.ts.map +1 -1
- package/dist/compiler/incremental-build.js.map +1 -1
- package/dist/compiler/incremental-dependencies.d.ts.map +1 -1
- package/dist/compiler/incremental-dependencies.js.map +1 -1
- package/dist/compiler/js-interop-nullability.d.ts.map +1 -1
- package/dist/compiler/js-interop-nullability.js.map +1 -1
- package/dist/compiler/modules/output-collisions.d.ts.map +1 -1
- package/dist/compiler/modules/output-collisions.js.map +1 -1
- package/dist/compiler/modules/output-plan.d.ts.map +1 -1
- package/dist/compiler/modules/output-plan.js.map +1 -1
- package/dist/compiler/modules/resolution.d.ts.map +1 -1
- package/dist/compiler/modules/resolution.js +2 -1
- package/dist/compiler/modules/resolution.js.map +1 -1
- package/dist/compiler/modules/source-files.d.ts +3 -0
- package/dist/compiler/modules/source-files.d.ts.map +1 -0
- package/dist/compiler/modules/source-files.js +5 -0
- package/dist/compiler/modules/source-files.js.map +1 -0
- package/dist/compiler/numeric-comparisons.d.ts.map +1 -1
- package/dist/compiler/numeric-comparisons.js.map +1 -1
- package/dist/compiler/optional-arguments.d.ts.map +1 -1
- package/dist/compiler/optional-arguments.js.map +1 -1
- package/dist/compiler/package-fingerprint.d.ts.map +1 -1
- package/dist/compiler/package-fingerprint.js.map +1 -1
- package/dist/compiler/package-record-contracts.d.ts.map +1 -1
- package/dist/compiler/package-record-contracts.js.map +1 -1
- package/dist/compiler/pipelines/catalog.d.ts.map +1 -1
- package/dist/compiler/pipelines/catalog.js.map +1 -1
- package/dist/compiler/pipelines/companions.d.ts.map +1 -1
- package/dist/compiler/pipelines/companions.js.map +1 -1
- package/dist/compiler/pipelines/resolution.d.ts.map +1 -1
- package/dist/compiler/pipelines/resolution.js.map +1 -1
- package/dist/compiler/project-config.d.ts.map +1 -1
- package/dist/compiler/project-config.js +29 -19
- package/dist/compiler/project-config.js.map +1 -1
- package/dist/compiler/record-contracts.d.ts.map +1 -1
- package/dist/compiler/record-contracts.js.map +1 -1
- package/dist/compiler/rules/control-flow.d.ts.map +1 -1
- package/dist/compiler/rules/control-flow.js.map +1 -1
- package/dist/compiler/rules/diagnostic.d.ts.map +1 -1
- package/dist/compiler/rules/diagnostic.js.map +1 -1
- package/dist/compiler/rules/module-scope-refs.d.ts.map +1 -1
- package/dist/compiler/rules/module-scope-refs.js.map +1 -1
- package/dist/compiler/rules/resource-escape.d.ts +7 -0
- package/dist/compiler/rules/resource-escape.d.ts.map +1 -0
- package/dist/compiler/rules/resource-escape.js +146 -0
- package/dist/compiler/rules/resource-escape.js.map +1 -0
- package/dist/compiler/rules/resource-providers.d.ts +19 -0
- package/dist/compiler/rules/resource-providers.d.ts.map +1 -0
- package/dist/compiler/rules/resource-providers.js +222 -0
- package/dist/compiler/rules/resource-providers.js.map +1 -0
- package/dist/compiler/schemas/analysis.d.ts.map +1 -1
- package/dist/compiler/schemas/analysis.js.map +1 -1
- package/dist/compiler/schemas/imported-record-functions.d.ts.map +1 -1
- package/dist/compiler/schemas/imported-record-functions.js.map +1 -1
- package/dist/compiler/schemas/imported-schemas.d.ts.map +1 -1
- package/dist/compiler/schemas/imported-schemas.js.map +1 -1
- package/dist/compiler/source-map.d.ts.map +1 -1
- package/dist/compiler/source-map.js.map +1 -1
- package/dist/compiler/type-view-paths.d.ts +4 -0
- package/dist/compiler/type-view-paths.d.ts.map +1 -0
- package/dist/compiler/type-view-paths.js +4 -0
- package/dist/compiler/type-view-paths.js.map +1 -0
- package/dist/compiler/typescript-context.d.ts.map +1 -1
- package/dist/compiler/typescript-context.js.map +1 -1
- package/dist/compiler.d.ts.map +1 -1
- package/dist/compiler.js.map +1 -1
- package/dist/emitter/assemblyscript.d.ts.map +1 -1
- package/dist/emitter/assemblyscript.js.map +1 -1
- package/dist/emitter/declarations.d.ts.map +1 -1
- package/dist/emitter/declarations.js.map +1 -1
- package/dist/emitter/emitter.d.ts +3 -1
- package/dist/emitter/emitter.d.ts.map +1 -1
- package/dist/emitter/emitter.js +10 -1
- package/dist/emitter/emitter.js.map +1 -1
- package/dist/emitter/imports.d.ts.map +1 -1
- package/dist/emitter/imports.js.map +1 -1
- package/dist/emitter/indent.d.ts.map +1 -1
- package/dist/emitter/indent.js.map +1 -1
- package/dist/emitter/jsx.d.ts.map +1 -1
- package/dist/emitter/jsx.js.map +1 -1
- package/dist/emitter/local-bindings.d.ts.map +1 -1
- package/dist/emitter/local-bindings.js.map +1 -1
- package/dist/emitter/match.d.ts.map +1 -1
- package/dist/emitter/match.js.map +1 -1
- package/dist/emitter/runtime-helpers.d.ts.map +1 -1
- package/dist/emitter/runtime-helpers.js.map +1 -1
- package/dist/emitter/schemas.d.ts.map +1 -1
- package/dist/emitter/schemas.js.map +1 -1
- package/dist/emitter/statements.d.ts +1 -0
- package/dist/emitter/statements.d.ts.map +1 -1
- package/dist/emitter/statements.js +1 -0
- package/dist/emitter/statements.js.map +1 -1
- package/dist/emitter/tagged-match.d.ts.map +1 -1
- package/dist/emitter/tagged-match.js.map +1 -1
- package/dist/emitter/types.d.ts.map +1 -1
- package/dist/emitter/types.js.map +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/formatter/comments.d.ts.map +1 -1
- package/dist/formatter/comments.js +13 -0
- package/dist/formatter/comments.js.map +1 -1
- package/dist/formatter/index.d.ts.map +1 -1
- package/dist/formatter/index.js +57 -3
- package/dist/formatter/index.js.map +1 -1
- package/dist/formatter/operator-precedence.d.ts.map +1 -1
- package/dist/formatter/operator-precedence.js.map +1 -1
- package/dist/formatter/prettier.d.ts.map +1 -1
- package/dist/formatter/prettier.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/interop/callback.d.ts.map +1 -1
- package/dist/interop/callback.js.map +1 -1
- package/dist/interop/js.d.ts.map +1 -1
- package/dist/interop/js.js.map +1 -1
- package/dist/interop/record.d.ts.map +1 -1
- package/dist/interop/record.js.map +1 -1
- package/dist/interop/valibot.d.ts.map +1 -1
- package/dist/interop/valibot.js.map +1 -1
- package/dist/interop/zod.d.ts.map +1 -1
- package/dist/interop/zod.js.map +1 -1
- package/dist/lexer/lexer.d.ts +1 -0
- package/dist/lexer/lexer.d.ts.map +1 -1
- package/dist/lexer/lexer.js +26 -10
- package/dist/lexer/lexer.js.map +1 -1
- package/dist/lexer/tokens.d.ts.map +1 -1
- package/dist/lexer/tokens.js.map +1 -1
- package/dist/lsp/code-action-worker.d.ts.map +1 -1
- package/dist/lsp/code-action-worker.js.map +1 -1
- package/dist/lsp/completion.d.ts.map +1 -1
- package/dist/lsp/completion.js.map +1 -1
- package/dist/lsp/definition.d.ts.map +1 -1
- package/dist/lsp/definition.js.map +1 -1
- package/dist/lsp/diagnostics-worker.d.ts.map +1 -1
- package/dist/lsp/diagnostics-worker.js +1 -1
- package/dist/lsp/diagnostics-worker.js.map +1 -1
- package/dist/lsp/docs.d.ts +1 -1
- package/dist/lsp/docs.d.ts.map +1 -1
- package/dist/lsp/docs.js +4 -3
- package/dist/lsp/docs.js.map +1 -1
- package/dist/lsp/error-channel.d.ts.map +1 -1
- package/dist/lsp/error-channel.js.map +1 -1
- package/dist/lsp/hover.d.ts.map +1 -1
- package/dist/lsp/hover.js +16 -5
- package/dist/lsp/hover.js.map +1 -1
- package/dist/lsp/imports.d.ts.map +1 -1
- package/dist/lsp/imports.js +10 -1
- package/dist/lsp/imports.js.map +1 -1
- package/dist/lsp/index.d.ts.map +1 -1
- package/dist/lsp/index.js.map +1 -1
- package/dist/lsp/inlay-hints.d.ts.map +1 -1
- package/dist/lsp/inlay-hints.js.map +1 -1
- package/dist/lsp/interop-rename.d.ts.map +1 -1
- package/dist/lsp/interop-rename.js.map +1 -1
- package/dist/lsp/match-arms.d.ts.map +1 -1
- package/dist/lsp/match-arms.js.map +1 -1
- package/dist/lsp/navigation.d.ts.map +1 -1
- package/dist/lsp/navigation.js.map +1 -1
- package/dist/lsp/parsed-documents.d.ts.map +1 -1
- package/dist/lsp/parsed-documents.js.map +1 -1
- package/dist/lsp/rename-worker.d.ts.map +1 -1
- package/dist/lsp/rename-worker.js.map +1 -1
- package/dist/lsp/server.d.ts +2 -1
- package/dist/lsp/server.d.ts.map +1 -1
- package/dist/lsp/server.js +20 -0
- package/dist/lsp/server.js.map +1 -1
- package/dist/lsp/signature-help.d.ts +6 -0
- package/dist/lsp/signature-help.d.ts.map +1 -0
- package/dist/lsp/signature-help.js +183 -0
- package/dist/lsp/signature-help.js.map +1 -0
- package/dist/lsp/thunk-pipeline.d.ts.map +1 -1
- package/dist/lsp/thunk-pipeline.js.map +1 -1
- package/dist/lsp/validation.d.ts.map +1 -1
- package/dist/lsp/validation.js +3 -2
- package/dist/lsp/validation.js.map +1 -1
- package/dist/lsp/workspace.d.ts.map +1 -1
- package/dist/lsp/workspace.js.map +1 -1
- package/dist/parser/declarations.d.ts.map +1 -1
- package/dist/parser/declarations.js +22 -1
- package/dist/parser/declarations.js.map +1 -1
- package/dist/parser/errors.d.ts.map +1 -1
- package/dist/parser/errors.js.map +1 -1
- package/dist/parser/jsx.d.ts.map +1 -1
- package/dist/parser/jsx.js.map +1 -1
- package/dist/parser/operator-expressions.d.ts.map +1 -1
- package/dist/parser/operator-expressions.js.map +1 -1
- package/dist/parser/parser.d.ts.map +1 -1
- package/dist/parser/parser.js.map +1 -1
- package/dist/parser/patterns.d.ts.map +1 -1
- package/dist/parser/patterns.js.map +1 -1
- package/dist/parser/pipeline-expressions.d.ts.map +1 -1
- package/dist/parser/pipeline-expressions.js.map +1 -1
- package/dist/parser/scope.d.ts.map +1 -1
- package/dist/parser/scope.js.map +1 -1
- package/dist/parser/statements.d.ts.map +1 -1
- package/dist/parser/statements.js.map +1 -1
- package/dist/parser/types.d.ts.map +1 -1
- package/dist/parser/types.js.map +1 -1
- package/dist/plugins/bun-preload.d.ts.map +1 -1
- package/dist/plugins/bun-preload.js.map +1 -1
- package/dist/plugins/bun.d.ts.map +1 -1
- package/dist/plugins/bun.js.map +1 -1
- package/dist/plugins/esbuild.d.ts.map +1 -1
- package/dist/plugins/esbuild.js.map +1 -1
- package/dist/plugins/type-view.d.ts.map +1 -1
- package/dist/plugins/type-view.js +5 -3
- package/dist/plugins/type-view.js.map +1 -1
- package/dist/plugins/vite.d.ts.map +1 -1
- package/dist/plugins/vite.js.map +1 -1
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js.map +1 -1
- package/dist/tooling/doc.d.ts.map +1 -1
- package/dist/tooling/doc.js.map +1 -1
- package/dist/tooling/idiomatic.d.ts.map +1 -1
- package/dist/tooling/idiomatic.js.map +1 -1
- package/dist/tooling/pipeline-style.d.ts.map +1 -1
- package/dist/tooling/pipeline-style.js.map +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +13 -6
- package/src/cli/build-watch.ts +6 -3
- package/src/cli/doctor.ts +8 -2
- package/src/cli/init.ts +66 -21
- package/src/cli/tsc.ts +8 -3
- package/src/compiler/boolean-operators.ts +11 -2
- package/src/compiler/check.ts +124 -132
- package/src/compiler/modules/resolution.ts +2 -1
- package/src/compiler/modules/source-files.ts +4 -0
- package/src/compiler/project-config.ts +30 -19
- package/src/compiler/rules/resource-escape.ts +223 -0
- package/src/compiler/rules/resource-providers.ts +293 -0
- package/src/compiler/type-view-paths.ts +3 -0
- package/src/emitter/emitter.ts +13 -1
- package/src/emitter/statements.ts +2 -0
- package/src/formatter/comments.ts +14 -0
- package/src/formatter/index.ts +70 -2
- package/src/lexer/lexer.ts +39 -20
- package/src/lsp/diagnostics-worker.ts +1 -4
- package/src/lsp/docs.ts +4 -3
- package/src/lsp/hover.ts +23 -4
- package/src/lsp/imports.ts +20 -1
- package/src/lsp/server.ts +30 -0
- package/src/lsp/signature-help.ts +226 -0
- package/src/lsp/validation.ts +3 -2
- package/src/parser/declarations.ts +27 -1
- package/src/plugins/type-view.ts +11 -3
- package/dist/Widget.tsx +0 -9
- package/dist/cli/output-collisions.d.ts +0 -2
- package/dist/cli/output-collisions.d.ts.map +0 -1
- package/dist/cli/output-collisions.js +0 -39
- package/dist/cli/output-collisions.js.map +0 -1
- package/dist/compiler/imported-record-functions.d.ts +0 -4
- package/dist/compiler/imported-record-functions.d.ts.map +0 -1
- package/dist/compiler/imported-record-functions.js +0 -706
- package/dist/compiler/imported-record-functions.js.map +0 -1
- package/dist/compiler/imported-schemas.d.ts +0 -21
- package/dist/compiler/imported-schemas.d.ts.map +0 -1
- package/dist/compiler/imported-schemas.js +0 -461
- package/dist/compiler/imported-schemas.js.map +0 -1
- package/dist/compiler/module-resolution.d.ts +0 -6
- package/dist/compiler/module-resolution.d.ts.map +0 -1
- package/dist/compiler/module-resolution.js +0 -44
- package/dist/compiler/module-resolution.js.map +0 -1
- package/dist/compiler/pipeline-companions.d.ts +0 -18
- package/dist/compiler/pipeline-companions.d.ts.map +0 -1
- package/dist/compiler/pipeline-companions.js +0 -1042
- package/dist/compiler/pipeline-companions.js.map +0 -1
- package/dist/compiler/providers.d.ts +0 -4
- package/dist/compiler/providers.d.ts.map +0 -1
- package/dist/compiler/providers.js +0 -101
- package/dist/compiler/providers.js.map +0 -1
- package/dist/emitter/pipeline-catalog.d.ts +0 -15
- package/dist/emitter/pipeline-catalog.d.ts.map +0 -1
- package/dist/emitter/pipeline-catalog.js +0 -55
- package/dist/emitter/pipeline-catalog.js.map +0 -1
- package/dist/emitter/pipeline-resolution.d.ts +0 -38
- package/dist/emitter/pipeline-resolution.d.ts.map +0 -1
- package/dist/emitter/pipeline-resolution.js +0 -571
- package/dist/emitter/pipeline-resolution.js.map +0 -1
- package/dist/interop/index.d.ts +0 -6
- package/dist/interop/index.d.ts.map +0 -1
- package/dist/interop/index.js +0 -6
- package/dist/interop/index.js.map +0 -1
- package/dist/math.ts +0 -1
- package/dist/parser/index.d.ts +0 -9
- package/dist/parser/index.d.ts.map +0 -1
- package/dist/parser/index.js +0 -9
- package/dist/parser/index.js.map +0 -1
- package/dist/service.ts +0 -3
- package/dist/service.ts.map +0 -1
- package/dist/tooling/migrate.d.ts +0 -62
- package/dist/tooling/migrate.d.ts.map +0 -1
- package/dist/tooling/migrate.js +0 -608
- package/dist/tooling/migrate.js.map +0 -1
package/src/compiler/check.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { checkControlFlowRules } from "./rules/control-flow.js";
|
|
2
2
|
import { checkModuleScopeRefs } from "./rules/module-scope-refs.js";
|
|
3
|
+
import { findScopedResourceEscape } from "./rules/resource-escape.js";
|
|
4
|
+
import {
|
|
5
|
+
createResourceProviderResolver,
|
|
6
|
+
effectImportNames,
|
|
7
|
+
} from "./rules/resource-providers.js";
|
|
3
8
|
import { isEntryModule, getEffectRunnerName } from "./entry-rules.js";
|
|
4
9
|
export { isEntryModule } from "./entry-rules.js";
|
|
5
10
|
import { checkCapturedMutInEffect } from "./effect-mutation-rule.js";
|
|
@@ -2100,8 +2105,85 @@ export const typeCheckVirtualFiles = (
|
|
|
2100
2105
|
(finalExpr?.kind === "Identifier" ||
|
|
2101
2106
|
finalExpr?.kind === "UpperIdentifier") &&
|
|
2102
2107
|
astName(finalExpr) === "Void";
|
|
2108
|
+
const isIndependentLiteral = (
|
|
2109
|
+
expr: AstInspectNode | null,
|
|
2110
|
+
): boolean => {
|
|
2111
|
+
if (!expr) return false;
|
|
2112
|
+
if (
|
|
2113
|
+
[
|
|
2114
|
+
"NumberLiteral",
|
|
2115
|
+
"StringLiteral",
|
|
2116
|
+
"BooleanLiteral",
|
|
2117
|
+
"TemplateLiteral",
|
|
2118
|
+
].includes(expr.kind)
|
|
2119
|
+
)
|
|
2120
|
+
return true;
|
|
2121
|
+
if (expr.kind === "ArrayExpression")
|
|
2122
|
+
return astChildren(expr, "elements").every(
|
|
2123
|
+
isIndependentLiteral,
|
|
2124
|
+
);
|
|
2125
|
+
if (expr.kind === "RecordExpression")
|
|
2126
|
+
return astChildren(expr, "entries").every(
|
|
2127
|
+
(entry) =>
|
|
2128
|
+
(entry.kind === "FieldEntry" ||
|
|
2129
|
+
entry.kind === "DotPathEntry") &&
|
|
2130
|
+
isIndependentLiteral(astChild(entry, "value")),
|
|
2131
|
+
);
|
|
2132
|
+
return false;
|
|
2133
|
+
};
|
|
2103
2134
|
|
|
2104
|
-
if (!isUnit) {
|
|
2135
|
+
if (!isUnit && !isIndependentLiteral(finalExpr)) {
|
|
2136
|
+
// Function parameters shadow module bindings, including
|
|
2137
|
+
// inside nested functions. Never follow a shadowed name.
|
|
2138
|
+
const shadowed = new Set<string>();
|
|
2139
|
+
const collectEnclosingParams = (
|
|
2140
|
+
candidate: AstInspectNode,
|
|
2141
|
+
): void => {
|
|
2142
|
+
if (
|
|
2143
|
+
!candidate.span ||
|
|
2144
|
+
!node.span ||
|
|
2145
|
+
candidate.span.start.offset > node.span.start.offset ||
|
|
2146
|
+
candidate.span.end.offset < node.span.end.offset
|
|
2147
|
+
)
|
|
2148
|
+
return;
|
|
2149
|
+
if (candidate.kind === "FunctionExpression") {
|
|
2150
|
+
for (const param of astChildren(candidate, "params")) {
|
|
2151
|
+
for (const name of collectPatternVars(
|
|
2152
|
+
astChild(param, "name"),
|
|
2153
|
+
))
|
|
2154
|
+
shadowed.add(name);
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
for (const [key, value] of Object.entries(
|
|
2158
|
+
asRecord(candidate),
|
|
2159
|
+
)) {
|
|
2160
|
+
if (key === "span") continue;
|
|
2161
|
+
if (Array.isArray(value)) {
|
|
2162
|
+
for (const item of value)
|
|
2163
|
+
if (isAstNode(item)) collectEnclosingParams(item);
|
|
2164
|
+
} else if (isAstNode(value))
|
|
2165
|
+
collectEnclosingParams(value);
|
|
2166
|
+
}
|
|
2167
|
+
};
|
|
2168
|
+
if (meta.ast) collectEnclosingParams(meta.ast);
|
|
2169
|
+
for (const statement of astChildren(node, "body")) {
|
|
2170
|
+
if (
|
|
2171
|
+
statement.kind === "BindingDeclaration" ||
|
|
2172
|
+
statement.kind === "EffectBindStatement"
|
|
2173
|
+
) {
|
|
2174
|
+
for (const name of collectPatternVars(
|
|
2175
|
+
astChild(statement, "id"),
|
|
2176
|
+
))
|
|
2177
|
+
shadowed.add(name);
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
const imports = effectImportNames(meta.ast);
|
|
2181
|
+
for (const name of shadowed) {
|
|
2182
|
+
imports.effect.delete(name);
|
|
2183
|
+
imports.scope.delete(name);
|
|
2184
|
+
imports.acquire.delete(name);
|
|
2185
|
+
imports.finalizer.delete(name);
|
|
2186
|
+
}
|
|
2105
2187
|
const findFinalizer = (
|
|
2106
2188
|
n: unknown,
|
|
2107
2189
|
allowAcquire: boolean,
|
|
@@ -2121,7 +2203,8 @@ export const typeCheckVirtualFiles = (
|
|
|
2121
2203
|
callee.property as { name?: string } | undefined
|
|
2122
2204
|
)?.name;
|
|
2123
2205
|
if (
|
|
2124
|
-
|
|
2206
|
+
!!obj &&
|
|
2207
|
+
(imports.effect.has(obj) || imports.scope.has(obj)) &&
|
|
2125
2208
|
(prop === "addFinalizer" ||
|
|
2126
2209
|
(allowAcquire && prop === "acquireRelease"))
|
|
2127
2210
|
) {
|
|
@@ -2133,8 +2216,10 @@ export const typeCheckVirtualFiles = (
|
|
|
2133
2216
|
} else if (callee?.kind === "Identifier") {
|
|
2134
2217
|
const calleeName = (callee as { name?: string }).name;
|
|
2135
2218
|
if (
|
|
2136
|
-
calleeName
|
|
2137
|
-
(allowAcquire &&
|
|
2219
|
+
(!!calleeName && imports.finalizer.has(calleeName)) ||
|
|
2220
|
+
(allowAcquire &&
|
|
2221
|
+
!!calleeName &&
|
|
2222
|
+
imports.acquire.has(calleeName))
|
|
2138
2223
|
) {
|
|
2139
2224
|
return {
|
|
2140
2225
|
name: calleeName,
|
|
@@ -2175,10 +2260,11 @@ export const typeCheckVirtualFiles = (
|
|
|
2175
2260
|
detectedCall = findFinalizer(finalExpr, false);
|
|
2176
2261
|
}
|
|
2177
2262
|
|
|
2178
|
-
// Follow
|
|
2179
|
-
//
|
|
2180
|
-
//
|
|
2181
|
-
|
|
2263
|
+
// Follow resources through aliases, returned containers and
|
|
2264
|
+
// captured closures. An eagerly read property can be an
|
|
2265
|
+
// independent value, but a closure reading it captures the
|
|
2266
|
+
// resource itself for later use.
|
|
2267
|
+
if (!detectedCall && finalExpr) {
|
|
2182
2268
|
const bindings = new Map<string, AstInspectNode>();
|
|
2183
2269
|
for (const item of meta.ast?.body ?? []) {
|
|
2184
2270
|
const decl =
|
|
@@ -2192,39 +2278,6 @@ export const typeCheckVirtualFiles = (
|
|
|
2192
2278
|
bindings.set(decl.id.name, decl.init);
|
|
2193
2279
|
}
|
|
2194
2280
|
}
|
|
2195
|
-
// Function parameters shadow module bindings, including
|
|
2196
|
-
// inside nested functions. Never follow a shadowed name.
|
|
2197
|
-
const shadowed = new Set<string>();
|
|
2198
|
-
const collectEnclosingParams = (
|
|
2199
|
-
candidate: AstInspectNode,
|
|
2200
|
-
): void => {
|
|
2201
|
-
if (
|
|
2202
|
-
!candidate.span ||
|
|
2203
|
-
!node.span ||
|
|
2204
|
-
candidate.span.start.offset > node.span.start.offset ||
|
|
2205
|
-
candidate.span.end.offset < node.span.end.offset
|
|
2206
|
-
)
|
|
2207
|
-
return;
|
|
2208
|
-
if (candidate.kind === "FunctionExpression") {
|
|
2209
|
-
for (const param of astChildren(candidate, "params")) {
|
|
2210
|
-
for (const name of collectPatternVars(
|
|
2211
|
-
astChild(param, "name"),
|
|
2212
|
-
))
|
|
2213
|
-
shadowed.add(name);
|
|
2214
|
-
}
|
|
2215
|
-
}
|
|
2216
|
-
for (const [key, value] of Object.entries(
|
|
2217
|
-
asRecord(candidate),
|
|
2218
|
-
)) {
|
|
2219
|
-
if (key === "span") continue;
|
|
2220
|
-
if (Array.isArray(value)) {
|
|
2221
|
-
for (const item of value)
|
|
2222
|
-
if (isAstNode(item)) collectEnclosingParams(item);
|
|
2223
|
-
} else if (isAstNode(value))
|
|
2224
|
-
collectEnclosingParams(value);
|
|
2225
|
-
}
|
|
2226
|
-
};
|
|
2227
|
-
if (meta.ast) collectEnclosingParams(meta.ast);
|
|
2228
2281
|
for (const name of shadowed) bindings.delete(name);
|
|
2229
2282
|
for (const stmt of astChildren(node, "body")) {
|
|
2230
2283
|
if (stmt.kind === "BindingDeclaration") {
|
|
@@ -2233,96 +2286,33 @@ export const typeCheckVirtualFiles = (
|
|
|
2233
2286
|
if (name && init) bindings.set(name, init);
|
|
2234
2287
|
}
|
|
2235
2288
|
}
|
|
2236
|
-
const
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
const bind = stmts.find(
|
|
2243
|
-
(stmt) =>
|
|
2244
|
-
stmt.kind === "EffectBindStatement" &&
|
|
2245
|
-
astName(astChild(stmt, "id")) === name,
|
|
2246
|
-
);
|
|
2247
|
-
if (bind) return bind;
|
|
2248
|
-
const alias = stmts.find(
|
|
2249
|
-
(stmt) =>
|
|
2250
|
-
stmt.kind === "BindingDeclaration" &&
|
|
2251
|
-
astName(astChild(stmt, "id")) === name,
|
|
2252
|
-
);
|
|
2253
|
-
const init = astChild(alias, "init");
|
|
2254
|
-
return init?.kind === "Identifier"
|
|
2255
|
-
? resolveReturned(
|
|
2256
|
-
astName(init),
|
|
2257
|
-
new Set([...seen, name]),
|
|
2258
|
-
)
|
|
2259
|
-
: undefined;
|
|
2260
|
-
};
|
|
2261
|
-
const bind = resolveReturned(astName(finalExpr), new Set());
|
|
2262
|
-
const source = astChild(bind, "effect");
|
|
2263
|
-
const followsAcquire = (
|
|
2264
|
-
expr: AstInspectNode | null,
|
|
2265
|
-
seen: Set<string>,
|
|
2266
|
-
): boolean => {
|
|
2267
|
-
if (!expr) return false;
|
|
2268
|
-
if (expr.kind === "Identifier") {
|
|
2269
|
-
const name = astName(expr);
|
|
2270
|
-
if (!name || seen.has(name)) return false;
|
|
2271
|
-
const next = bindings.get(name);
|
|
2272
|
-
if (!next) return false;
|
|
2273
|
-
return followsAcquire(next, new Set([...seen, name]));
|
|
2274
|
-
}
|
|
2275
|
-
if (expr.kind === "FunctionExpression") {
|
|
2276
|
-
const nestedSeen = new Set(seen);
|
|
2277
|
-
for (const param of astChildren(expr, "params")) {
|
|
2278
|
-
for (const name of collectPatternVars(
|
|
2279
|
-
astChild(param, "name"),
|
|
2280
|
-
))
|
|
2281
|
-
nestedSeen.add(name);
|
|
2282
|
-
}
|
|
2283
|
-
return followsAcquire(
|
|
2284
|
-
astChild(expr, "body"),
|
|
2285
|
-
nestedSeen,
|
|
2289
|
+
const programs = new Map<string, Program>();
|
|
2290
|
+
for (const entry of virtualSourceMap.values()) {
|
|
2291
|
+
if (entry.ast)
|
|
2292
|
+
programs.set(
|
|
2293
|
+
path.resolve(cwd, entry.funPath),
|
|
2294
|
+
entry.ast,
|
|
2286
2295
|
);
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
return followsAcquire(astChild(bind, "effect"), seen);
|
|
2306
|
-
}
|
|
2307
|
-
if (expr.kind === "CallExpression") {
|
|
2308
|
-
const callee = astChild(expr, "callee");
|
|
2309
|
-
const member = getMemberNames(callee);
|
|
2310
|
-
if (
|
|
2311
|
-
(member?.object === "Effect" &&
|
|
2312
|
-
member.property === "acquireRelease") ||
|
|
2313
|
-
(callee?.kind === "Identifier" &&
|
|
2314
|
-
astName(callee) === "acquireRelease")
|
|
2315
|
-
)
|
|
2316
|
-
return true;
|
|
2317
|
-
if (callee?.kind === "Identifier") {
|
|
2318
|
-
return followsAcquire(callee, seen);
|
|
2319
|
-
}
|
|
2320
|
-
}
|
|
2321
|
-
return false;
|
|
2322
|
-
};
|
|
2323
|
-
if (source && followsAcquire(source, new Set())) {
|
|
2296
|
+
}
|
|
2297
|
+
const followsAcquire = createResourceProviderResolver({
|
|
2298
|
+
rootFile: path.resolve(cwd, meta.funPath),
|
|
2299
|
+
rootAst: meta.ast!,
|
|
2300
|
+
rootBindings: bindings,
|
|
2301
|
+
rootImports: imports,
|
|
2302
|
+
shadowed,
|
|
2303
|
+
sources: virtualSources,
|
|
2304
|
+
programs,
|
|
2305
|
+
});
|
|
2306
|
+
const source = findScopedResourceEscape(
|
|
2307
|
+
node,
|
|
2308
|
+
finalExpr,
|
|
2309
|
+
sf,
|
|
2310
|
+
checker,
|
|
2311
|
+
followsAcquire,
|
|
2312
|
+
);
|
|
2313
|
+
if (source) {
|
|
2324
2314
|
detectedCall = {
|
|
2325
|
-
name: "
|
|
2315
|
+
name: "a scope-bound provider",
|
|
2326
2316
|
span: source.span ?? node.span!,
|
|
2327
2317
|
};
|
|
2328
2318
|
}
|
|
@@ -2333,10 +2323,12 @@ export const typeCheckVirtualFiles = (
|
|
|
2333
2323
|
addAstDiag(
|
|
2334
2324
|
callInfo.span,
|
|
2335
2325
|
"PREMATURE_FINALIZER_CLOSE",
|
|
2336
|
-
|
|
2326
|
+
callInfo.name === "a scope-bound provider"
|
|
2327
|
+
? "A scope-bound resource escapes this effect through the returned value. Its scope closes before the caller can use it."
|
|
2328
|
+
: `Finalizer registered with '${callInfo.name}' in a scoped effect block will execute immediately when this function returns, closing the returned resource or a resource it captures.`,
|
|
2337
2329
|
"error",
|
|
2338
|
-
"By default, 'effect' blocks create a scoped boundary ('Effect.scoped').
|
|
2339
|
-
"Change 'effect' to 'effect (unscoped)' so the resource's lifetime is bound to the caller's scope, or
|
|
2330
|
+
"By default, 'effect' blocks create a scoped boundary ('Effect.scoped'). Scope-bound resources are finalized as soon as the block returns, before the caller can use them.",
|
|
2331
|
+
"Change 'effect' to 'effect (unscoped)' so the resource's lifetime is bound to the caller's scope, or finish using the resource inside this block and return an independent value.",
|
|
2340
2332
|
);
|
|
2341
2333
|
}
|
|
2342
2334
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as path from "node:path";
|
|
2
2
|
import * as fs from "node:fs";
|
|
3
3
|
import { isFileNotFoundError } from "../../errors.js";
|
|
4
|
+
import { isFunSourceFile } from "./source-files.js";
|
|
4
5
|
|
|
5
6
|
/** Source module specifiers become JavaScript paths in emitted TypeScript. */
|
|
6
7
|
export function emittedModuleSource(source: string): string {
|
|
@@ -28,7 +29,7 @@ export function resolveFunModule(
|
|
|
28
29
|
): string | undefined {
|
|
29
30
|
if (!specifier.startsWith(".")) return undefined;
|
|
30
31
|
const requested = path.resolve(path.dirname(importer), specifier);
|
|
31
|
-
if (
|
|
32
|
+
if (isFunSourceFile(requested))
|
|
32
33
|
return exists(requested) ? requested : undefined;
|
|
33
34
|
// Explicit TypeScript paths identify TypeScript files, even if absent.
|
|
34
35
|
if (/\.(?:ts|tsx)$/.test(requested)) return undefined;
|
|
@@ -17,6 +17,25 @@ export interface FunScriptProjectConfig {
|
|
|
17
17
|
allowEarlyReturns?: boolean | string[];
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
const CONFIG_FIELDS = [
|
|
21
|
+
"noTs",
|
|
22
|
+
"noGlobalThis",
|
|
23
|
+
"barePipelines",
|
|
24
|
+
"effectOnly",
|
|
25
|
+
"format",
|
|
26
|
+
"modularEffectImports",
|
|
27
|
+
"noDeepDestructure",
|
|
28
|
+
"maxDestructureDepth",
|
|
29
|
+
"allowHoles",
|
|
30
|
+
"allowEarlyReturns",
|
|
31
|
+
];
|
|
32
|
+
const MIN_DESTRUCTURE_DEPTH = 1;
|
|
33
|
+
const MAX_DESTRUCTURE_DEPTH = 10;
|
|
34
|
+
const FORMAT_BOUNDS = {
|
|
35
|
+
indentSize: { min: 1, max: 8 },
|
|
36
|
+
lineWidth: { min: 40, max: 240 },
|
|
37
|
+
} as const;
|
|
38
|
+
|
|
20
39
|
/** Read a literal FunkScript record without compiling or executing project code. */
|
|
21
40
|
export function loadProjectConfig(cwd: string): FunScriptProjectConfig {
|
|
22
41
|
const file = path.resolve(cwd, "funscript.config.fun");
|
|
@@ -49,21 +68,12 @@ export function loadProjectConfig(cwd: string): FunScriptProjectConfig {
|
|
|
49
68
|
for (const entry of binding.init.entries) {
|
|
50
69
|
if (
|
|
51
70
|
entry.kind !== "FieldEntry" ||
|
|
52
|
-
!
|
|
53
|
-
"noTs",
|
|
54
|
-
"noGlobalThis",
|
|
55
|
-
"barePipelines",
|
|
56
|
-
"effectOnly",
|
|
57
|
-
"format",
|
|
58
|
-
"modularEffectImports",
|
|
59
|
-
"noDeepDestructure",
|
|
60
|
-
"maxDestructureDepth",
|
|
61
|
-
"allowHoles",
|
|
62
|
-
"allowEarlyReturns",
|
|
63
|
-
].includes(entry.key.name)
|
|
71
|
+
!CONFIG_FIELDS.includes(entry.key.name)
|
|
64
72
|
) {
|
|
65
73
|
throw new Error(
|
|
66
|
-
|
|
74
|
+
`config accepts only ${CONFIG_FIELDS.slice(0, -1)
|
|
75
|
+
.map((field) => `'${field}'`)
|
|
76
|
+
.join(", ")}, and '${CONFIG_FIELDS.at(-1)}'`,
|
|
67
77
|
);
|
|
68
78
|
}
|
|
69
79
|
const name = entry.key.name;
|
|
@@ -73,11 +83,11 @@ export function loadProjectConfig(cwd: string): FunScriptProjectConfig {
|
|
|
73
83
|
if (
|
|
74
84
|
entry.value.kind !== "NumberLiteral" ||
|
|
75
85
|
!Number.isInteger(entry.value.value) ||
|
|
76
|
-
entry.value.value <
|
|
77
|
-
entry.value.value >
|
|
86
|
+
entry.value.value < MIN_DESTRUCTURE_DEPTH ||
|
|
87
|
+
entry.value.value > MAX_DESTRUCTURE_DEPTH
|
|
78
88
|
) {
|
|
79
89
|
throw new Error(
|
|
80
|
-
|
|
90
|
+
`maxDestructureDepth must be an integer between ${MIN_DESTRUCTURE_DEPTH} and ${MAX_DESTRUCTURE_DEPTH}`,
|
|
81
91
|
);
|
|
82
92
|
}
|
|
83
93
|
config.maxDestructureDepth = entry.value.value;
|
|
@@ -94,14 +104,15 @@ export function loadProjectConfig(cwd: string): FunScriptProjectConfig {
|
|
|
94
104
|
const key = field.key.name as "indentSize" | "lineWidth";
|
|
95
105
|
if (format[key] !== undefined)
|
|
96
106
|
throw new Error(`duplicate format field '${key}'`);
|
|
107
|
+
const { min, max } = FORMAT_BOUNDS[key];
|
|
97
108
|
if (
|
|
98
109
|
field.value.kind !== "NumberLiteral" ||
|
|
99
110
|
!Number.isInteger(field.value.value) ||
|
|
100
|
-
field.value.value <
|
|
101
|
-
field.value.value >
|
|
111
|
+
field.value.value < min ||
|
|
112
|
+
field.value.value > max
|
|
102
113
|
)
|
|
103
114
|
throw new Error(
|
|
104
|
-
`${key} must be an integer between ${
|
|
115
|
+
`${key} must be an integer between ${min} and ${max}`,
|
|
105
116
|
);
|
|
106
117
|
format[key] = field.value.value;
|
|
107
118
|
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import {
|
|
3
|
+
asRecord,
|
|
4
|
+
astChild,
|
|
5
|
+
astChildren,
|
|
6
|
+
astName,
|
|
7
|
+
collectPatternVars,
|
|
8
|
+
isAstNode,
|
|
9
|
+
type AstInspectNode,
|
|
10
|
+
} from "../ast-inspection.js";
|
|
11
|
+
|
|
12
|
+
/** Find resources flowing into a block's result, including through aliases and closures. */
|
|
13
|
+
export function findResourceEscape(
|
|
14
|
+
effectBlock: AstInspectNode,
|
|
15
|
+
finalExpr: AstInspectNode,
|
|
16
|
+
followsAcquire: (expr: AstInspectNode, seen: Set<string>) => boolean,
|
|
17
|
+
isIndependentProperty: (expr: AstInspectNode) => boolean,
|
|
18
|
+
): AstInspectNode | undefined {
|
|
19
|
+
const resolveReturned = (
|
|
20
|
+
name: string | undefined,
|
|
21
|
+
seen: Set<string>,
|
|
22
|
+
): AstInspectNode | undefined => {
|
|
23
|
+
if (!name || seen.has(name)) return undefined;
|
|
24
|
+
const stmts = astChildren(effectBlock, "body");
|
|
25
|
+
const bind = stmts.find(
|
|
26
|
+
(stmt) =>
|
|
27
|
+
stmt.kind === "EffectBindStatement" &&
|
|
28
|
+
astName(astChild(stmt, "id")) === name,
|
|
29
|
+
);
|
|
30
|
+
if (bind) return bind;
|
|
31
|
+
const alias = stmts.find(
|
|
32
|
+
(stmt) =>
|
|
33
|
+
stmt.kind === "BindingDeclaration" &&
|
|
34
|
+
astName(astChild(stmt, "id")) === name,
|
|
35
|
+
);
|
|
36
|
+
const init = astChild(alias, "init");
|
|
37
|
+
return init?.kind === "Identifier"
|
|
38
|
+
? resolveReturned(astName(init), new Set([...seen, name]))
|
|
39
|
+
: undefined;
|
|
40
|
+
};
|
|
41
|
+
const findEscapedResource = (
|
|
42
|
+
expr: AstInspectNode | null,
|
|
43
|
+
hidden: Set<string>,
|
|
44
|
+
seen: Set<string>,
|
|
45
|
+
inClosure: boolean,
|
|
46
|
+
matched: ReadonlyMap<string, AstInspectNode> = new Map(),
|
|
47
|
+
): AstInspectNode | undefined => {
|
|
48
|
+
if (!expr) return undefined;
|
|
49
|
+
if (
|
|
50
|
+
expr.kind === "StringLiteral" ||
|
|
51
|
+
expr.kind === "TemplateLiteral" ||
|
|
52
|
+
expr.kind === "NumberLiteral" ||
|
|
53
|
+
expr.kind === "BooleanLiteral"
|
|
54
|
+
)
|
|
55
|
+
return undefined;
|
|
56
|
+
if (expr.kind === "Identifier") {
|
|
57
|
+
const name = astName(expr);
|
|
58
|
+
if (!name || seen.has(name)) return undefined;
|
|
59
|
+
if (matched.has(name)) return matched.get(name);
|
|
60
|
+
if (hidden.has(name)) return undefined;
|
|
61
|
+
const bind = resolveReturned(name, new Set());
|
|
62
|
+
const source = astChild(bind, "effect");
|
|
63
|
+
if (source && followsAcquire(source, new Set())) return source;
|
|
64
|
+
const local = astChildren(effectBlock, "body").find(
|
|
65
|
+
(stmt) =>
|
|
66
|
+
stmt.kind === "BindingDeclaration" &&
|
|
67
|
+
astName(astChild(stmt, "id")) === name,
|
|
68
|
+
);
|
|
69
|
+
return findEscapedResource(
|
|
70
|
+
astChild(local, "init"),
|
|
71
|
+
hidden,
|
|
72
|
+
new Set([...seen, name]),
|
|
73
|
+
inClosure,
|
|
74
|
+
matched,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
if (expr.kind === "MemberExpression" || expr.kind === "IndexExpression") {
|
|
78
|
+
return inClosure || !isIndependentProperty(expr)
|
|
79
|
+
? findEscapedResource(
|
|
80
|
+
astChild(expr, "object"),
|
|
81
|
+
hidden,
|
|
82
|
+
seen,
|
|
83
|
+
true,
|
|
84
|
+
matched,
|
|
85
|
+
)
|
|
86
|
+
: undefined;
|
|
87
|
+
}
|
|
88
|
+
if (expr.kind === "MatchExpression") {
|
|
89
|
+
const subject = findEscapedResource(
|
|
90
|
+
astChild(expr, "scrutinee"),
|
|
91
|
+
hidden,
|
|
92
|
+
seen,
|
|
93
|
+
false,
|
|
94
|
+
matched,
|
|
95
|
+
);
|
|
96
|
+
for (const arm of astChildren(expr, "arms")) {
|
|
97
|
+
const nestedHidden = new Set(hidden);
|
|
98
|
+
const nestedMatched = new Map(matched);
|
|
99
|
+
for (const name of collectPatternVars(astChild(arm, "pattern"))) {
|
|
100
|
+
nestedHidden.add(name);
|
|
101
|
+
if (subject) nestedMatched.set(name, subject);
|
|
102
|
+
else nestedMatched.delete(name);
|
|
103
|
+
}
|
|
104
|
+
const found = findEscapedResource(
|
|
105
|
+
astChild(arm, "body"),
|
|
106
|
+
nestedHidden,
|
|
107
|
+
seen,
|
|
108
|
+
inClosure,
|
|
109
|
+
nestedMatched,
|
|
110
|
+
);
|
|
111
|
+
if (found) return found;
|
|
112
|
+
}
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
if (expr.kind === "FunctionExpression") {
|
|
116
|
+
const nestedHidden = new Set(hidden);
|
|
117
|
+
const nestedMatched = new Map(matched);
|
|
118
|
+
for (const param of astChildren(expr, "params")) {
|
|
119
|
+
for (const name of collectPatternVars(astChild(param, "name"))) {
|
|
120
|
+
nestedHidden.add(name);
|
|
121
|
+
nestedMatched.delete(name);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const body = astChild(expr, "body");
|
|
125
|
+
if (body?.kind === "BlockStatement") {
|
|
126
|
+
for (const stmt of astChildren(body, "statements")) {
|
|
127
|
+
if (stmt.kind === "BindingDeclaration") {
|
|
128
|
+
for (const name of collectPatternVars(astChild(stmt, "id"))) {
|
|
129
|
+
nestedHidden.add(name);
|
|
130
|
+
nestedMatched.delete(name);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return findEscapedResource(body, nestedHidden, seen, true, nestedMatched);
|
|
136
|
+
}
|
|
137
|
+
if (expr.kind === "FieldEntry" || expr.kind === "DotPathEntry")
|
|
138
|
+
return findEscapedResource(
|
|
139
|
+
astChild(expr, "value"),
|
|
140
|
+
hidden,
|
|
141
|
+
seen,
|
|
142
|
+
inClosure,
|
|
143
|
+
matched,
|
|
144
|
+
);
|
|
145
|
+
if (expr.kind === "ShorthandEntry")
|
|
146
|
+
return findEscapedResource(
|
|
147
|
+
astChild(expr, "key"),
|
|
148
|
+
hidden,
|
|
149
|
+
seen,
|
|
150
|
+
inClosure,
|
|
151
|
+
matched,
|
|
152
|
+
);
|
|
153
|
+
for (const [key, value] of Object.entries(asRecord(expr))) {
|
|
154
|
+
if (key === "span" || key === "typeAnnotation") continue;
|
|
155
|
+
const children = Array.isArray(value) ? value : [value];
|
|
156
|
+
for (const child of children) {
|
|
157
|
+
if (!isAstNode(child)) continue;
|
|
158
|
+
const found = findEscapedResource(
|
|
159
|
+
child,
|
|
160
|
+
hidden,
|
|
161
|
+
seen,
|
|
162
|
+
inClosure,
|
|
163
|
+
matched,
|
|
164
|
+
);
|
|
165
|
+
if (found) return found;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return undefined;
|
|
169
|
+
};
|
|
170
|
+
return findEscapedResource(finalExpr, new Set(), new Set(), false);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Check a scoped result using TypeScript types to recognize independent values. */
|
|
174
|
+
export function findScopedResourceEscape(
|
|
175
|
+
effectBlock: AstInspectNode,
|
|
176
|
+
finalExpr: AstInspectNode,
|
|
177
|
+
sourceFile: ts.SourceFile,
|
|
178
|
+
checker: ts.TypeChecker,
|
|
179
|
+
followsAcquire: (expr: AstInspectNode, seen: Set<string>) => boolean,
|
|
180
|
+
): AstInspectNode | undefined {
|
|
181
|
+
const isIndependentProperty = (expr: AstInspectNode): boolean => {
|
|
182
|
+
if (expr.kind !== "MemberExpression") return false;
|
|
183
|
+
const object = astName(astChild(expr, "object"));
|
|
184
|
+
const property = astName(astChild(expr, "property"));
|
|
185
|
+
if (!object || !property) return false;
|
|
186
|
+
const isPrimitive = (type: ts.Type): boolean =>
|
|
187
|
+
type.isUnion()
|
|
188
|
+
? type.types.every(isPrimitive)
|
|
189
|
+
: !!(
|
|
190
|
+
type.flags &
|
|
191
|
+
(ts.TypeFlags.StringLike |
|
|
192
|
+
ts.TypeFlags.NumberLike |
|
|
193
|
+
ts.TypeFlags.BooleanLike |
|
|
194
|
+
ts.TypeFlags.BigIntLike |
|
|
195
|
+
ts.TypeFlags.ESSymbolLike |
|
|
196
|
+
ts.TypeFlags.Null |
|
|
197
|
+
ts.TypeFlags.Undefined)
|
|
198
|
+
);
|
|
199
|
+
let found = false;
|
|
200
|
+
let independent = true;
|
|
201
|
+
const visit = (candidate: ts.Node): void => {
|
|
202
|
+
if (
|
|
203
|
+
ts.isPropertyAccessExpression(candidate) &&
|
|
204
|
+
ts.isIdentifier(candidate.expression) &&
|
|
205
|
+
candidate.expression.text === object &&
|
|
206
|
+
candidate.name.text === property
|
|
207
|
+
) {
|
|
208
|
+
found = true;
|
|
209
|
+
if (!isPrimitive(checker.getTypeAtLocation(candidate)))
|
|
210
|
+
independent = false;
|
|
211
|
+
}
|
|
212
|
+
ts.forEachChild(candidate, visit);
|
|
213
|
+
};
|
|
214
|
+
visit(sourceFile);
|
|
215
|
+
return found && independent;
|
|
216
|
+
};
|
|
217
|
+
return findResourceEscape(
|
|
218
|
+
effectBlock,
|
|
219
|
+
finalExpr,
|
|
220
|
+
followsAcquire,
|
|
221
|
+
isIndependentProperty,
|
|
222
|
+
);
|
|
223
|
+
}
|