hinmil 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2306 -2
- package/dist/cli.d.ts +15 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +338 -0
- package/dist/cli.js.map +1 -0
- package/dist/commentParser.d.ts +384 -0
- package/dist/commentParser.d.ts.map +1 -0
- package/dist/commentParser.js +2487 -0
- package/dist/commentParser.js.map +1 -0
- package/dist/config.d.ts +203 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +375 -0
- package/dist/config.js.map +1 -0
- package/dist/debugLog.d.ts +13 -0
- package/dist/debugLog.d.ts.map +1 -0
- package/dist/debugLog.js +44 -0
- package/dist/debugLog.js.map +1 -0
- package/dist/estree.d.ts +100 -0
- package/dist/estree.d.ts.map +1 -0
- package/dist/estree.js +34 -0
- package/dist/estree.js.map +1 -0
- package/dist/hm/env.d.ts +134 -0
- package/dist/hm/env.d.ts.map +1 -0
- package/dist/hm/env.js +939 -0
- package/dist/hm/env.js.map +1 -0
- package/dist/hm/index.d.ts +7 -0
- package/dist/hm/index.d.ts.map +1 -0
- package/dist/hm/index.js +26 -0
- package/dist/hm/index.js.map +1 -0
- package/dist/hm/substitution.d.ts +57 -0
- package/dist/hm/substitution.d.ts.map +1 -0
- package/dist/hm/substitution.js +546 -0
- package/dist/hm/substitution.js.map +1 -0
- package/dist/hm/typeParser.d.ts +39 -0
- package/dist/hm/typeParser.d.ts.map +1 -0
- package/dist/hm/typeParser.js +1285 -0
- package/dist/hm/typeParser.js.map +1 -0
- package/dist/hm/typeUtils.d.ts +248 -0
- package/dist/hm/typeUtils.d.ts.map +1 -0
- package/dist/hm/typeUtils.js +905 -0
- package/dist/hm/typeUtils.js.map +1 -0
- package/dist/hm/types.d.ts +738 -0
- package/dist/hm/types.d.ts.map +1 -0
- package/dist/hm/types.js +1853 -0
- package/dist/hm/types.js.map +1 -0
- package/dist/hm/unify.d.ts +59 -0
- package/dist/hm/unify.d.ts.map +1 -0
- package/dist/hm/unify.js +1700 -0
- package/dist/hm/unify.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +196 -0
- package/dist/index.js.map +1 -0
- package/dist/infer/annotation-rigid.d.ts +76 -0
- package/dist/infer/annotation-rigid.d.ts.map +1 -0
- package/dist/infer/annotation-rigid.js +542 -0
- package/dist/infer/annotation-rigid.js.map +1 -0
- package/dist/infer/annotation-validation.d.ts +109 -0
- package/dist/infer/annotation-validation.d.ts.map +1 -0
- package/dist/infer/annotation-validation.js +1078 -0
- package/dist/infer/annotation-validation.js.map +1 -0
- package/dist/infer/annotations.d.ts +277 -0
- package/dist/infer/annotations.d.ts.map +1 -0
- package/dist/infer/annotations.js +1343 -0
- package/dist/infer/annotations.js.map +1 -0
- package/dist/infer/call-overloads.d.ts +24 -0
- package/dist/infer/call-overloads.d.ts.map +1 -0
- package/dist/infer/call-overloads.js +900 -0
- package/dist/infer/call-overloads.js.map +1 -0
- package/dist/infer/call-rows.d.ts +154 -0
- package/dist/infer/call-rows.d.ts.map +1 -0
- package/dist/infer/call-rows.js +2162 -0
- package/dist/infer/call-rows.js.map +1 -0
- package/dist/infer/calls.d.ts +20 -0
- package/dist/infer/calls.d.ts.map +1 -0
- package/dist/infer/calls.js +1468 -0
- package/dist/infer/calls.js.map +1 -0
- package/dist/infer/constraints.d.ts +99 -0
- package/dist/infer/constraints.d.ts.map +1 -0
- package/dist/infer/constraints.js +1208 -0
- package/dist/infer/constraints.js.map +1 -0
- package/dist/infer/expr-assignment.d.ts +20 -0
- package/dist/infer/expr-assignment.d.ts.map +1 -0
- package/dist/infer/expr-assignment.js +620 -0
- package/dist/infer/expr-assignment.js.map +1 -0
- package/dist/infer/expr-constraint-lookup.d.ts +44 -0
- package/dist/infer/expr-constraint-lookup.d.ts.map +1 -0
- package/dist/infer/expr-constraint-lookup.js +207 -0
- package/dist/infer/expr-constraint-lookup.js.map +1 -0
- package/dist/infer/expr-functions.d.ts +20 -0
- package/dist/infer/expr-functions.d.ts.map +1 -0
- package/dist/infer/expr-functions.js +1359 -0
- package/dist/infer/expr-functions.js.map +1 -0
- package/dist/infer/expr-jsx.d.ts +19 -0
- package/dist/infer/expr-jsx.d.ts.map +1 -0
- package/dist/infer/expr-jsx.js +310 -0
- package/dist/infer/expr-jsx.js.map +1 -0
- package/dist/infer/expr-member.d.ts +40 -0
- package/dist/infer/expr-member.d.ts.map +1 -0
- package/dist/infer/expr-member.js +1154 -0
- package/dist/infer/expr-member.js.map +1 -0
- package/dist/infer/expr-prototype.d.ts +11 -0
- package/dist/infer/expr-prototype.d.ts.map +1 -0
- package/dist/infer/expr-prototype.js +202 -0
- package/dist/infer/expr-prototype.js.map +1 -0
- package/dist/infer/expressions.d.ts +40 -0
- package/dist/infer/expressions.d.ts.map +1 -0
- package/dist/infer/expressions.js +1497 -0
- package/dist/infer/expressions.js.map +1 -0
- package/dist/infer/functionPatterns.d.ts +229 -0
- package/dist/infer/functionPatterns.d.ts.map +1 -0
- package/dist/infer/functionPatterns.js +1035 -0
- package/dist/infer/functionPatterns.js.map +1 -0
- package/dist/infer/helpers.d.ts +73 -0
- package/dist/infer/helpers.d.ts.map +1 -0
- package/dist/infer/helpers.js +187 -0
- package/dist/infer/helpers.js.map +1 -0
- package/dist/infer/index.d.ts +551 -0
- package/dist/infer/index.d.ts.map +1 -0
- package/dist/infer/index.js +1127 -0
- package/dist/infer/index.js.map +1 -0
- package/dist/infer/jsx-dom.d.ts +26 -0
- package/dist/infer/jsx-dom.d.ts.map +1 -0
- package/dist/infer/jsx-dom.js +303 -0
- package/dist/infer/jsx-dom.js.map +1 -0
- package/dist/infer/moduleReps.d.ts +7 -0
- package/dist/infer/moduleReps.d.ts.map +1 -0
- package/dist/infer/moduleReps.js +48 -0
- package/dist/infer/moduleReps.js.map +1 -0
- package/dist/infer/modules.d.ts +10 -0
- package/dist/infer/modules.d.ts.map +1 -0
- package/dist/infer/modules.js +775 -0
- package/dist/infer/modules.js.map +1 -0
- package/dist/infer/narrowing.d.ts +199 -0
- package/dist/infer/narrowing.d.ts.map +1 -0
- package/dist/infer/narrowing.js +1428 -0
- package/dist/infer/narrowing.js.map +1 -0
- package/dist/infer/results.d.ts +24 -0
- package/dist/infer/results.d.ts.map +1 -0
- package/dist/infer/results.js +611 -0
- package/dist/infer/results.js.map +1 -0
- package/dist/infer/setupPass.d.ts +11 -0
- package/dist/infer/setupPass.d.ts.map +1 -0
- package/dist/infer/setupPass.js +524 -0
- package/dist/infer/setupPass.js.map +1 -0
- package/dist/infer/statements.d.ts +26 -0
- package/dist/infer/statements.d.ts.map +1 -0
- package/dist/infer/statements.js +750 -0
- package/dist/infer/statements.js.map +1 -0
- package/dist/infer/stmt-bindings.d.ts +22 -0
- package/dist/infer/stmt-bindings.d.ts.map +1 -0
- package/dist/infer/stmt-bindings.js +1482 -0
- package/dist/infer/stmt-bindings.js.map +1 -0
- package/dist/infer/stmt-destructure.d.ts +29 -0
- package/dist/infer/stmt-destructure.d.ts.map +1 -0
- package/dist/infer/stmt-destructure.js +490 -0
- package/dist/infer/stmt-destructure.js.map +1 -0
- package/dist/infer/types.d.ts +220 -0
- package/dist/infer/types.d.ts.map +1 -0
- package/dist/infer/types.js +46 -0
- package/dist/infer/types.js.map +1 -0
- package/dist/infer/unifyDiag.d.ts +46 -0
- package/dist/infer/unifyDiag.d.ts.map +1 -0
- package/dist/infer/unifyDiag.js +642 -0
- package/dist/infer/unifyDiag.js.map +1 -0
- package/dist/parsing-babel.d.ts +3 -0
- package/dist/parsing-babel.d.ts.map +1 -0
- package/dist/parsing-babel.js +353 -0
- package/dist/parsing-babel.js.map +1 -0
- package/dist/parsing.d.ts +69 -0
- package/dist/parsing.d.ts.map +1 -0
- package/dist/parsing.js +288 -0
- package/dist/parsing.js.map +1 -0
- package/dist/registries/constraintRegistry.d.ts +233 -0
- package/dist/registries/constraintRegistry.d.ts.map +1 -0
- package/dist/registries/constraintRegistry.js +803 -0
- package/dist/registries/constraintRegistry.js.map +1 -0
- package/dist/registries/dataTypeRegistry.d.ts +63 -0
- package/dist/registries/dataTypeRegistry.d.ts.map +1 -0
- package/dist/registries/dataTypeRegistry.js +180 -0
- package/dist/registries/dataTypeRegistry.js.map +1 -0
- package/dist/registries/domGlobals.d.ts +33 -0
- package/dist/registries/domGlobals.d.ts.map +1 -0
- package/dist/registries/domGlobals.js +309 -0
- package/dist/registries/domGlobals.js.map +1 -0
- package/dist/registries/methodRegistry.d.ts +277 -0
- package/dist/registries/methodRegistry.d.ts.map +1 -0
- package/dist/registries/methodRegistry.js +2074 -0
- package/dist/registries/methodRegistry.js.map +1 -0
- package/dist/registries/session.d.ts +38 -0
- package/dist/registries/session.d.ts.map +1 -0
- package/dist/registries/session.js +75 -0
- package/dist/registries/session.js.map +1 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +1896 -0
- package/dist/server.js.map +1 -0
- package/dist/services/codeActions.d.ts +22 -0
- package/dist/services/codeActions.d.ts.map +1 -0
- package/dist/services/codeActions.js +465 -0
- package/dist/services/codeActions.js.map +1 -0
- package/dist/services/completion.d.ts +259 -0
- package/dist/services/completion.d.ts.map +1 -0
- package/dist/services/completion.js +2794 -0
- package/dist/services/completion.js.map +1 -0
- package/dist/services/definitions.d.ts +37 -0
- package/dist/services/definitions.d.ts.map +1 -0
- package/dist/services/definitions.js +605 -0
- package/dist/services/definitions.js.map +1 -0
- package/dist/services/diagnosticCodes.d.ts +70 -0
- package/dist/services/diagnosticCodes.d.ts.map +1 -0
- package/dist/services/diagnosticCodes.js +239 -0
- package/dist/services/diagnosticCodes.js.map +1 -0
- package/dist/services/diagnostics.d.ts +86 -0
- package/dist/services/diagnostics.d.ts.map +1 -0
- package/dist/services/diagnostics.js +665 -0
- package/dist/services/diagnostics.js.map +1 -0
- package/dist/services/documentCache.d.ts +88 -0
- package/dist/services/documentCache.d.ts.map +1 -0
- package/dist/services/documentCache.js +108 -0
- package/dist/services/documentCache.js.map +1 -0
- package/dist/services/documentSymbols.d.ts +20 -0
- package/dist/services/documentSymbols.d.ts.map +1 -0
- package/dist/services/documentSymbols.js +381 -0
- package/dist/services/documentSymbols.js.map +1 -0
- package/dist/services/fileSelection.d.ts +24 -0
- package/dist/services/fileSelection.d.ts.map +1 -0
- package/dist/services/fileSelection.js +154 -0
- package/dist/services/fileSelection.js.map +1 -0
- package/dist/services/fileSelectionRefresh.d.ts +26 -0
- package/dist/services/fileSelectionRefresh.d.ts.map +1 -0
- package/dist/services/fileSelectionRefresh.js +77 -0
- package/dist/services/fileSelectionRefresh.js.map +1 -0
- package/dist/services/files.d.ts +342 -0
- package/dist/services/files.d.ts.map +1 -0
- package/dist/services/files.js +1712 -0
- package/dist/services/files.js.map +1 -0
- package/dist/services/fsProbe.d.ts +14 -0
- package/dist/services/fsProbe.d.ts.map +1 -0
- package/dist/services/fsProbe.js +112 -0
- package/dist/services/fsProbe.js.map +1 -0
- package/dist/services/globalsLoader.d.ts +20 -0
- package/dist/services/globalsLoader.d.ts.map +1 -0
- package/dist/services/globalsLoader.js +122 -0
- package/dist/services/globalsLoader.js.map +1 -0
- package/dist/services/hmFileServices.d.ts +113 -0
- package/dist/services/hmFileServices.d.ts.map +1 -0
- package/dist/services/hmFileServices.js +686 -0
- package/dist/services/hmFileServices.js.map +1 -0
- package/dist/services/hover.d.ts +280 -0
- package/dist/services/hover.d.ts.map +1 -0
- package/dist/services/hover.js +1687 -0
- package/dist/services/hover.js.map +1 -0
- package/dist/services/importContext.d.ts +396 -0
- package/dist/services/importContext.d.ts.map +1 -0
- package/dist/services/importContext.js +1999 -0
- package/dist/services/importContext.js.map +1 -0
- package/dist/services/projectChecker.d.ts +50 -0
- package/dist/services/projectChecker.d.ts.map +1 -0
- package/dist/services/projectChecker.js +228 -0
- package/dist/services/projectChecker.js.map +1 -0
- package/dist/services/references.d.ts +41 -0
- package/dist/services/references.d.ts.map +1 -0
- package/dist/services/references.js +757 -0
- package/dist/services/references.js.map +1 -0
- package/dist/services/rename.d.ts +22 -0
- package/dist/services/rename.d.ts.map +1 -0
- package/dist/services/rename.js +101 -0
- package/dist/services/rename.js.map +1 -0
- package/dist/services/shortCircuit.d.ts +27 -0
- package/dist/services/shortCircuit.d.ts.map +1 -0
- package/dist/services/shortCircuit.js +28 -0
- package/dist/services/shortCircuit.js.map +1 -0
- package/dist/services/sidecarPerf.d.ts +19 -0
- package/dist/services/sidecarPerf.d.ts.map +1 -0
- package/dist/services/sidecarPerf.js +59 -0
- package/dist/services/sidecarPerf.js.map +1 -0
- package/dist/services/signatureHelp.d.ts +27 -0
- package/dist/services/signatureHelp.d.ts.map +1 -0
- package/dist/services/signatureHelp.js +984 -0
- package/dist/services/signatureHelp.js.map +1 -0
- package/dist/walking.d.ts +25 -0
- package/dist/walking.d.ts.map +1 -0
- package/dist/walking.js +107 -0
- package/dist/walking.js.map +1 -0
- package/hm-types-builtin/assert.hm +20 -0
- package/hm-types-builtin/buffer.hm +8 -0
- package/hm-types-builtin/child_process.hm +16 -0
- package/hm-types-builtin/crypto.hm +17 -0
- package/hm-types-builtin/fs.hm +15 -0
- package/hm-types-builtin/http.hm +14 -0
- package/hm-types-builtin/os.hm +19 -0
- package/hm-types-builtin/path.hm +18 -0
- package/hm-types-builtin/react-dom.hm +27 -0
- package/hm-types-builtin/react.hm +113 -0
- package/hm-types-builtin/url.hm +7 -0
- package/hm-types-builtin/zlib.hm +12 -0
- package/package.json +53 -4
- package/schemas/hinmil.config.schema.json +261 -0
- package/schemas/package-hinmil.schema.json +59 -0
|
@@ -0,0 +1,750 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ============================================================================
|
|
3
|
+
// Statement-level and declaration inference
|
|
4
|
+
// Extracted from InferenceEngine – each method takes `engine` as first arg.
|
|
5
|
+
//
|
|
6
|
+
// Plain language: *statements* are the sentences of a program — `const x =
|
|
7
|
+
// …`, `if (…) { … }`, `return …`, `for (…)`, class declarations. This file
|
|
8
|
+
// walks them one by one: declarations add names to the environment (the
|
|
9
|
+
// "phone book"), `if`/`else` branches are checked with narrowed types, and
|
|
10
|
+
// `return` statements collect the function's result type.
|
|
11
|
+
// ============================================================================
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.inferDestructuringPattern = exports.inferObjectDestructuring = exports.inferFunctionDeclaration = exports.inferNamedBinding = void 0;
|
|
14
|
+
exports.hoistFunctionDeclarations = hoistFunctionDeclarations;
|
|
15
|
+
exports.inferProgramWithEnv = inferProgramWithEnv;
|
|
16
|
+
exports.inferStatement = inferStatement;
|
|
17
|
+
exports.inferVariableDeclaration = inferVariableDeclaration;
|
|
18
|
+
exports.inferWhileStatement = inferWhileStatement;
|
|
19
|
+
exports.inferDoWhileStatement = inferDoWhileStatement;
|
|
20
|
+
exports.inferForStatement = inferForStatement;
|
|
21
|
+
exports.inferForInStatement = inferForInStatement;
|
|
22
|
+
exports.inferForOfStatement = inferForOfStatement;
|
|
23
|
+
exports.inferSwitchStatement = inferSwitchStatement;
|
|
24
|
+
exports.inferTryStatement = inferTryStatement;
|
|
25
|
+
const types_1 = require("../hm/types");
|
|
26
|
+
const env_1 = require("../hm/env");
|
|
27
|
+
const unify_1 = require("../hm/unify");
|
|
28
|
+
const debugLog_1 = require("../debugLog");
|
|
29
|
+
const narrowing_1 = require("./narrowing");
|
|
30
|
+
const helpers_1 = require("./helpers");
|
|
31
|
+
const dataTypeRegistry_1 = require("../registries/dataTypeRegistry");
|
|
32
|
+
const types_2 = require("./types");
|
|
33
|
+
const commentParser_1 = require("../commentParser");
|
|
34
|
+
// Re-exports: these families moved to sibling modules; existing importers
|
|
35
|
+
// (src/infer/index.ts) keep importing them from './statements'.
|
|
36
|
+
var stmt_bindings_1 = require("./stmt-bindings");
|
|
37
|
+
Object.defineProperty(exports, "inferNamedBinding", { enumerable: true, get: function () { return stmt_bindings_1.inferNamedBinding; } });
|
|
38
|
+
Object.defineProperty(exports, "inferFunctionDeclaration", { enumerable: true, get: function () { return stmt_bindings_1.inferFunctionDeclaration; } });
|
|
39
|
+
var stmt_destructure_1 = require("./stmt-destructure");
|
|
40
|
+
Object.defineProperty(exports, "inferObjectDestructuring", { enumerable: true, get: function () { return stmt_destructure_1.inferObjectDestructuring; } });
|
|
41
|
+
Object.defineProperty(exports, "inferDestructuringPattern", { enumerable: true, get: function () { return stmt_destructure_1.inferDestructuringPattern; } });
|
|
42
|
+
// -----------------------------------------------------------------------
|
|
43
|
+
// Helpers
|
|
44
|
+
// -----------------------------------------------------------------------
|
|
45
|
+
/**
|
|
46
|
+
* Detect the constructor-flavour of a `const Name = <init>` binding so that
|
|
47
|
+
* `.constructor` access and `new` strictness diagnostics can be applied.
|
|
48
|
+
*
|
|
49
|
+
* Records:
|
|
50
|
+
* - `'arrow-factory'` for `(...) => ({...})` or `(...) => { return {...}; }`
|
|
51
|
+
* — at runtime these return `Object`, NOT a `Name` instance.
|
|
52
|
+
* - `'function-this'` for `function(){ this.x = ... }` style ctors.
|
|
53
|
+
* - `'class'` for class expressions.
|
|
54
|
+
* - `'sumtype-variant'` overrides the above when the binding is a registered
|
|
55
|
+
* sum-type variant (e.g. `const Just = value => Object.create(...)`).
|
|
56
|
+
*/
|
|
57
|
+
function detectConstructorProvenance(engine, name, init) {
|
|
58
|
+
if ((0, dataTypeRegistry_1.isVariant)(name)) {
|
|
59
|
+
engine.constructorProvenance.set(name, 'sumtype-variant');
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (init.type === 'ClassExpression') {
|
|
63
|
+
engine.constructorProvenance.set(name, 'class');
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (init.type === 'ArrowFunctionExpression') {
|
|
67
|
+
const body = init.body;
|
|
68
|
+
// `(...) => ({...})` — expression body wrapped in parens.
|
|
69
|
+
if (body.type === 'ObjectExpression') {
|
|
70
|
+
if (!objectExpressionHasOwnConstructor(body)) {
|
|
71
|
+
engine.constructorProvenance.set(name, 'arrow-factory');
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
// `(...) => { return {...}; }` — single-statement block returning object.
|
|
76
|
+
if (body.type === 'BlockStatement') {
|
|
77
|
+
const stmts = body.body;
|
|
78
|
+
if (stmts.length === 1
|
|
79
|
+
&& stmts[0].type === 'ReturnStatement'
|
|
80
|
+
&& stmts[0].argument?.type === 'ObjectExpression') {
|
|
81
|
+
const obj = stmts[0].argument;
|
|
82
|
+
if (!objectExpressionHasOwnConstructor(obj)) {
|
|
83
|
+
engine.constructorProvenance.set(name, 'arrow-factory');
|
|
84
|
+
}
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (init.type === 'FunctionExpression') {
|
|
91
|
+
const fn = init;
|
|
92
|
+
if (fn.body?.type === 'BlockStatement' && (0, narrowing_1.blockHasThisAssignment)(fn.body)
|
|
93
|
+
&& !(0, narrowing_1.hasSelfNewGuard)(fn.body, name)) {
|
|
94
|
+
engine.constructorProvenance.set(name, 'function-this');
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function objectExpressionHasOwnConstructor(obj) {
|
|
99
|
+
return obj.properties.some(p => p.type === 'Property'
|
|
100
|
+
&& p.key?.type === 'Identifier'
|
|
101
|
+
&& p.key.name === 'constructor');
|
|
102
|
+
}
|
|
103
|
+
// -----------------------------------------------------------------------
|
|
104
|
+
// Program & Statement level
|
|
105
|
+
// -----------------------------------------------------------------------
|
|
106
|
+
/**
|
|
107
|
+
* Pre-bind `function` declarations in a statement list: JavaScript hoists
|
|
108
|
+
* them to the top of their scope, so closures defined textually earlier may
|
|
109
|
+
* reference them (e.g. trampoline code where `const onResolve = s => go(s)`
|
|
110
|
+
* precedes `function go(acc) { … onResolve … }`). Annotated declarations use
|
|
111
|
+
* their annotation; unannotated ones get a fresh type variable that the
|
|
112
|
+
* later declaration unifies with.
|
|
113
|
+
*/
|
|
114
|
+
function hoistFunctionDeclarations(engine, body, env) {
|
|
115
|
+
let currentEnv = env;
|
|
116
|
+
for (const stmt of body) {
|
|
117
|
+
if (stmt.type === 'FunctionDeclaration' && stmt.id?.name) {
|
|
118
|
+
const name = stmt.id.name;
|
|
119
|
+
if (currentEnv.lookup(name))
|
|
120
|
+
continue;
|
|
121
|
+
const annotation = engine.lookupAnnotation(name, stmt.start);
|
|
122
|
+
currentEnv = currentEnv.extend(name, annotation?.scheme ?? (0, types_1.mono)((0, types_1.tVar)()));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return currentEnv;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Reconcile forward-referenced top-level bindings declared by `stmt`: the
|
|
129
|
+
* placeholder that earlier closures used is unified with a FRESH
|
|
130
|
+
* instantiation of the declared scheme, so the uses flow into the real type
|
|
131
|
+
* while later uses keep the binding's full polymorphism.
|
|
132
|
+
*
|
|
133
|
+
* Lenient on conflict, like self-recursion (`selfRecTv` in stmt-bindings.ts):
|
|
134
|
+
* the use-site type of an UNANNOTATED forward reference is a guess (computed
|
|
135
|
+
* access `fallbacks[method]` on an unknown receiver infers an Array), so a
|
|
136
|
+
* mismatch here is not evidence of a bug. Annotated bindings are hoisted
|
|
137
|
+
* with their annotation and checked normally.
|
|
138
|
+
*/
|
|
139
|
+
function reconcileForwardDecls(engine, stmt, env, forwardDecls) {
|
|
140
|
+
const decl = stmt.type === 'ExportNamedDeclaration' && stmt.declaration ? stmt.declaration : stmt;
|
|
141
|
+
if (decl.type !== 'VariableDeclaration')
|
|
142
|
+
return;
|
|
143
|
+
for (const d of decl.declarations) {
|
|
144
|
+
if (d.id?.type !== 'Identifier')
|
|
145
|
+
continue;
|
|
146
|
+
const name = d.id.name;
|
|
147
|
+
const placeholder = forwardDecls.get(name);
|
|
148
|
+
if (!placeholder)
|
|
149
|
+
continue;
|
|
150
|
+
forwardDecls.delete(name);
|
|
151
|
+
const declared = env.lookup(name);
|
|
152
|
+
if (!declared)
|
|
153
|
+
continue;
|
|
154
|
+
const used = engine.subst.apply(placeholder);
|
|
155
|
+
// Never referenced before its declaration: nothing to reconcile.
|
|
156
|
+
if (used.tag === 'TVar' && placeholder.tag === 'TVar' && used.id === placeholder.id)
|
|
157
|
+
continue;
|
|
158
|
+
try {
|
|
159
|
+
engine.subst = engine.subst.compose((0, unify_1.unify)(used, (0, env_1.instantiate)(declared)));
|
|
160
|
+
}
|
|
161
|
+
catch (e) {
|
|
162
|
+
if (!(e instanceof unify_1.UnificationError))
|
|
163
|
+
throw e;
|
|
164
|
+
(0, debugLog_1.debugLog)('statements.forwardDeclReconcile', e);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function inferProgramWithEnv(engine, program, env) {
|
|
169
|
+
let currentEnv = env;
|
|
170
|
+
let lastType = types_1.tVoid;
|
|
171
|
+
const forwardDecls = new Map();
|
|
172
|
+
// Hoist function declarations: JavaScript hoists `function` declarations
|
|
173
|
+
// to the top of their scope, so they should be available before their
|
|
174
|
+
// textual position. Pre-extend the environment with annotated types or
|
|
175
|
+
// fresh type variables.
|
|
176
|
+
for (const stmt of program.body) {
|
|
177
|
+
const decl = stmt.type === 'ExportNamedDeclaration' && stmt.declaration ? stmt.declaration :
|
|
178
|
+
stmt.type === 'ExportDefaultDeclaration' && stmt.declaration ? stmt.declaration :
|
|
179
|
+
stmt;
|
|
180
|
+
if (decl.type === 'FunctionDeclaration' && decl.id?.name) {
|
|
181
|
+
const name = decl.id.name;
|
|
182
|
+
const annotation = engine.lookupAnnotation(name, decl.start);
|
|
183
|
+
if (annotation?.scheme) {
|
|
184
|
+
currentEnv = currentEnv.extend(name, annotation.scheme);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
const tv = (0, types_1.tVar)();
|
|
188
|
+
currentEnv = currentEnv.extend(name, (0, types_1.mono)(tv));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// Also hoist top-level `const`/`let`/`var` bindings so that closures
|
|
192
|
+
// defined earlier in the file (e.g. `Param.of = function() { return
|
|
193
|
+
// Path(...) }`) can reference names declared textually later. Annotated
|
|
194
|
+
// bindings are hoisted with their annotation (checked normally);
|
|
195
|
+
// unannotated `const`/`let` get a placeholder reconciled after the
|
|
196
|
+
// declaration (see reconcileForwardDecls).
|
|
197
|
+
if (decl.type === 'VariableDeclaration') {
|
|
198
|
+
for (const d of decl.declarations) {
|
|
199
|
+
if (d.id?.type === 'Identifier') {
|
|
200
|
+
const name = d.id.name;
|
|
201
|
+
if (currentEnv.lookup(name))
|
|
202
|
+
continue;
|
|
203
|
+
const annotation = engine.lookupAnnotation(name, d.start);
|
|
204
|
+
if (annotation?.scheme) {
|
|
205
|
+
currentEnv = currentEnv.extend(name, annotation.scheme);
|
|
206
|
+
}
|
|
207
|
+
else if ((decl.kind === 'const' || decl.kind === 'let') && d.init) {
|
|
208
|
+
// Unannotated top-level `const`/`let`: a closure defined
|
|
209
|
+
// textually EARLIER may reference it (FP-library Free: `const fallback
|
|
210
|
+
// = … => fallbacks[method] …` above `const fallbacks = { … }`) —
|
|
211
|
+
// valid JS, the closure runs later. Bind a placeholder that is
|
|
212
|
+
// reconciled with the declared type right after the declaration.
|
|
213
|
+
const tv = (0, types_1.tVar)();
|
|
214
|
+
forwardDecls.set(name, tv);
|
|
215
|
+
currentEnv = currentEnv.extend(name, (0, types_1.mono)(tv));
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
for (const stmt of program.body) {
|
|
222
|
+
const [newEnv, type] = engine.inferStatement(stmt, currentEnv);
|
|
223
|
+
currentEnv = newEnv;
|
|
224
|
+
lastType = type;
|
|
225
|
+
if (forwardDecls.size > 0)
|
|
226
|
+
reconcileForwardDecls(engine, stmt, currentEnv, forwardDecls);
|
|
227
|
+
}
|
|
228
|
+
return [currentEnv, lastType];
|
|
229
|
+
}
|
|
230
|
+
/** True when a branch statement's value IS a returned value: the statement
|
|
231
|
+
* (or the last statement of its block) is a `return` with an argument. */
|
|
232
|
+
function endsInReturn(node) {
|
|
233
|
+
if (node.type === 'ReturnStatement')
|
|
234
|
+
return !!node.argument;
|
|
235
|
+
if (node.type === 'BlockStatement') {
|
|
236
|
+
const body = node.body ?? [];
|
|
237
|
+
const last = body[body.length - 1];
|
|
238
|
+
return !!last && endsInReturn(last);
|
|
239
|
+
}
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
function inferStatement(engine, node, env) {
|
|
243
|
+
switch (node.type) {
|
|
244
|
+
case 'VariableDeclaration':
|
|
245
|
+
return engine.inferVariableDeclaration(node, env);
|
|
246
|
+
case 'ExpressionStatement':
|
|
247
|
+
return [env, engine.inferExpression(node.expression, env)];
|
|
248
|
+
case 'ReturnStatement':
|
|
249
|
+
if (node.argument) {
|
|
250
|
+
const t = engine.inferExpression(node.argument, env);
|
|
251
|
+
// Record the type of THIS return (re-expressed under a local kind
|
|
252
|
+
// equality when one is in force); the block's return collection
|
|
253
|
+
// reads it back instead of guessing from source offsets.
|
|
254
|
+
const recorded = engine.localKindEqs.length > 0 ? liftUnderLocalKindEqs(engine, t) : t;
|
|
255
|
+
engine.returnTypeOverrides.set(node, recorded);
|
|
256
|
+
return [env, recorded];
|
|
257
|
+
}
|
|
258
|
+
return [env, types_1.tVoid];
|
|
259
|
+
case 'FunctionDeclaration':
|
|
260
|
+
return engine.inferFunctionDeclaration(node, env);
|
|
261
|
+
case 'BlockStatement': {
|
|
262
|
+
let currentEnv = hoistFunctionDeclarations(engine, node.body, env);
|
|
263
|
+
let lastType = types_1.tVoid;
|
|
264
|
+
for (const stmt of node.body) {
|
|
265
|
+
const [newEnv, type] = engine.inferStatement(stmt, currentEnv);
|
|
266
|
+
currentEnv = newEnv;
|
|
267
|
+
lastType = type;
|
|
268
|
+
// `if (!m.has (k)) m.set (k, …);` / `… return;`: `k` is in `m` now.
|
|
269
|
+
const present = (0, narrowing_1.keyPresentAfter)(stmt);
|
|
270
|
+
if (present)
|
|
271
|
+
currentEnv = currentEnv.extend(present, (0, types_1.mono)(types_1.tBool));
|
|
272
|
+
// A guard clause (`if (a == null || b == null) continue;`, `… return`,
|
|
273
|
+
// `break`, `throw`): the rest of the block runs only when the test was
|
|
274
|
+
// false — the else-side narrowing, as after a guard in a function body.
|
|
275
|
+
else if (stmt.type === 'IfStatement' && !stmt.alternate && (0, narrowing_1.leavesBlock)(stmt.consequent)) {
|
|
276
|
+
currentEnv = (0, narrowing_1.computeNarrowedEnvs)(engine.subst, stmt.test, currentEnv, engine.annotationMap).altEnv;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return [env, lastType]; // block doesn't leak bindings in let/const
|
|
280
|
+
}
|
|
281
|
+
case 'IfStatement': {
|
|
282
|
+
engine.inferExpression(node.test, env);
|
|
283
|
+
// Compute narrowed environments for consequent and alternate branches
|
|
284
|
+
const narrowed = (0, narrowing_1.computeNarrowedEnvs)(engine.subst, node.test, env, engine.annotationMap);
|
|
285
|
+
const { altEnv } = narrowed;
|
|
286
|
+
// A `::narrow C` guard on a value of abstract type `f a` proves `f ≡ C`
|
|
287
|
+
// inside the true branch only (see `localKindEqs`).
|
|
288
|
+
const local = localKindEqsForGuard(engine, node.test, env, narrowed.consEnv);
|
|
289
|
+
const consEnv = local.env;
|
|
290
|
+
// Save substitution state before branches so that side-effects
|
|
291
|
+
// from one branch (e.g. res("foemp") constraining a TVar to String)
|
|
292
|
+
// don't leak into the other branch (e.g. making e.leader = String).
|
|
293
|
+
const substSnapshot = engine.subst.snapshot();
|
|
294
|
+
engine.localKindEqs.push(...local.eqs);
|
|
295
|
+
let consType;
|
|
296
|
+
try {
|
|
297
|
+
[, consType] = engine.inferStatement(node.consequent, consEnv);
|
|
298
|
+
}
|
|
299
|
+
finally {
|
|
300
|
+
engine.localKindEqs.length -= local.eqs.length;
|
|
301
|
+
}
|
|
302
|
+
if (node.alternate) {
|
|
303
|
+
// Capture bindings added by the consequent branch
|
|
304
|
+
const consBindings = engine.subst.diff(substSnapshot);
|
|
305
|
+
// Restore pre-branch state for the alternate
|
|
306
|
+
engine.subst.restore(substSnapshot);
|
|
307
|
+
const [, altType] = engine.inferStatement(node.alternate, altEnv);
|
|
308
|
+
const altBindings = engine.subst.diff(substSnapshot);
|
|
309
|
+
// Merge: re-apply bindings from both branches.
|
|
310
|
+
// For TVars bound in both branches to different concrete types,
|
|
311
|
+
// create a union. Otherwise just restore the binding.
|
|
312
|
+
engine.subst.mergeBranches(consBindings, altBindings);
|
|
313
|
+
// Only unify the branch "values" when both branches actually END in
|
|
314
|
+
// a `return` — then the types feed the function's return type and a
|
|
315
|
+
// mismatch is meaningful. For ordinary statement branches (e.g.
|
|
316
|
+
// `if (flag) { x = s; } else { notify (ok); }`) the trailing
|
|
317
|
+
// expression value is discarded in JS, so unifying them produces
|
|
318
|
+
// spurious errors (FP-library chainRec: 'Step b a' vs '()').
|
|
319
|
+
if (endsInReturn(node.consequent) && endsInReturn(node.alternate)) {
|
|
320
|
+
engine.unifyWithDiagnostic(consType, altType, node);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return [env, consType];
|
|
324
|
+
}
|
|
325
|
+
case 'ImportDeclaration':
|
|
326
|
+
return [engine.inferImportDeclaration(node, env), types_1.tVoid];
|
|
327
|
+
case 'ExportNamedDeclaration':
|
|
328
|
+
return engine.inferExportNamedDeclaration(node, env);
|
|
329
|
+
case 'ExportDefaultDeclaration':
|
|
330
|
+
return engine.inferExportDefaultDeclaration(node, env);
|
|
331
|
+
case 'ExportAllDeclaration':
|
|
332
|
+
return engine.inferExportAllDeclaration(node, env);
|
|
333
|
+
case 'ClassDeclaration':
|
|
334
|
+
return engine.inferClassDeclaration(node, env);
|
|
335
|
+
case 'WhileStatement':
|
|
336
|
+
return engine.inferWhileStatement(node, env);
|
|
337
|
+
case 'DoWhileStatement':
|
|
338
|
+
return engine.inferDoWhileStatement(node, env);
|
|
339
|
+
case 'ForStatement':
|
|
340
|
+
return engine.inferForStatement(node, env);
|
|
341
|
+
case 'ForInStatement':
|
|
342
|
+
return engine.inferForInStatement(node, env);
|
|
343
|
+
case 'ForOfStatement':
|
|
344
|
+
return engine.inferForOfStatement(node, env);
|
|
345
|
+
case 'SwitchStatement':
|
|
346
|
+
return engine.inferSwitchStatement(node, env);
|
|
347
|
+
case 'TryStatement':
|
|
348
|
+
return engine.inferTryStatement(node, env);
|
|
349
|
+
case 'ThrowStatement': {
|
|
350
|
+
if (node.argument) {
|
|
351
|
+
engine.inferExpression(node.argument, env);
|
|
352
|
+
}
|
|
353
|
+
return [env, types_1.tVoid];
|
|
354
|
+
}
|
|
355
|
+
case 'LabeledStatement':
|
|
356
|
+
return engine.inferStatement(node.body, env);
|
|
357
|
+
case 'BreakStatement':
|
|
358
|
+
case 'ContinueStatement':
|
|
359
|
+
case 'DebuggerStatement':
|
|
360
|
+
case 'EmptyStatement':
|
|
361
|
+
return [env, types_1.tVoid];
|
|
362
|
+
default:
|
|
363
|
+
return [env, types_1.tVoid];
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
// -----------------------------------------------------------------------
|
|
367
|
+
// Declarations
|
|
368
|
+
// -----------------------------------------------------------------------
|
|
369
|
+
function inferVariableDeclaration(engine, node, env) {
|
|
370
|
+
let currentEnv = env;
|
|
371
|
+
let lastType = types_1.tVoid;
|
|
372
|
+
for (const decl of node.declarations) {
|
|
373
|
+
// Handle destructured require: const { Box, Pair } = require('./demo')
|
|
374
|
+
if (decl.id?.type === 'ObjectPattern' && (0, narrowing_1.isRequireCall)(decl.init)) {
|
|
375
|
+
const resolved = (0, narrowing_1.resolveRequire)(engine.importResolver, decl.init);
|
|
376
|
+
if ((0, types_2.isUntypedModule)(resolved)) {
|
|
377
|
+
// Untyped npm module — bind every local name to opaque `Any`
|
|
378
|
+
// (does not propagate via unification, unlike a fresh tvar).
|
|
379
|
+
for (const prop of decl.id.properties ?? []) {
|
|
380
|
+
if (prop.type === 'Property' && prop.value?.type === 'Identifier') {
|
|
381
|
+
const localName = prop.value.name;
|
|
382
|
+
const scheme = (0, types_1.mono)(types_1.tAnyType);
|
|
383
|
+
currentEnv = currentEnv.extend(localName, scheme);
|
|
384
|
+
engine.recordNodeType(prop.value, (0, env_1.instantiate)(scheme));
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
if (resolved) {
|
|
390
|
+
const specifier = decl.init?.arguments?.[0]?.type === 'Literal'
|
|
391
|
+
? decl.init.arguments[0].value
|
|
392
|
+
: undefined;
|
|
393
|
+
for (const prop of decl.id.properties ?? []) {
|
|
394
|
+
if (prop.type === 'Property' && prop.key?.type === 'Identifier' && prop.value?.type === 'Identifier') {
|
|
395
|
+
const importedName = prop.key.name;
|
|
396
|
+
const localName = prop.value.name;
|
|
397
|
+
const scheme = resolved.get(importedName);
|
|
398
|
+
if (scheme) {
|
|
399
|
+
currentEnv = currentEnv.extend(localName, scheme);
|
|
400
|
+
engine.recordNodeType(prop.value, (0, env_1.instantiate)(scheme));
|
|
401
|
+
engine.propagateCurryFlags(scheme, localName, prop.value.start);
|
|
402
|
+
}
|
|
403
|
+
else if (specifier) {
|
|
404
|
+
engine.addDiag(`'${importedName}' is not exported from '${specifier}'`, prop.key);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
// Handle plain require: const demo = require('./demo')
|
|
412
|
+
if (decl.id?.type === 'Identifier' && (0, narrowing_1.isRequireCall)(decl.init)) {
|
|
413
|
+
const resolved = (0, narrowing_1.resolveRequire)(engine.importResolver, decl.init);
|
|
414
|
+
if ((0, types_2.isUntypedModule)(resolved)) {
|
|
415
|
+
const name = decl.id.name;
|
|
416
|
+
const scheme = (0, types_1.mono)(types_1.tAnyType);
|
|
417
|
+
currentEnv = currentEnv.extend(name, scheme);
|
|
418
|
+
engine.recordNodeType(decl, (0, env_1.instantiate)(scheme));
|
|
419
|
+
lastType = (0, env_1.instantiate)(scheme);
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
if (resolved) {
|
|
423
|
+
const name = decl.id.name;
|
|
424
|
+
// CommonJS `module.exports = X` pattern: the resolved exports map
|
|
425
|
+
// contains a `default` entry whose type IS the module's runtime
|
|
426
|
+
// value. A plain require should bind to that value directly
|
|
427
|
+
// (e.g. `const nil = require('./nil')` where nil.js does
|
|
428
|
+
// `module.exports = nil` makes `nil` callable), not to a
|
|
429
|
+
// synthetic `{ default, nil }` record.
|
|
430
|
+
const defaultScheme = resolved.get('default');
|
|
431
|
+
if (defaultScheme) {
|
|
432
|
+
currentEnv = currentEnv.extend(name, defaultScheme);
|
|
433
|
+
const t = (0, env_1.instantiate)(defaultScheme);
|
|
434
|
+
engine.recordNodeType(decl, t);
|
|
435
|
+
engine.propagateCurryFlags(defaultScheme, name, decl.id.start);
|
|
436
|
+
// Mirror the ES `import X from '...'` default-import path: remember
|
|
437
|
+
// the source specifier so a later `const { A, B } = X` destructure
|
|
438
|
+
// can resolve the module's static-member exports (e.g. `List.Cons`)
|
|
439
|
+
// instead of falling back to a fresh type variable.
|
|
440
|
+
const specifier = decl.init?.arguments?.[0]?.type === 'Literal'
|
|
441
|
+
? decl.init.arguments[0].value
|
|
442
|
+
: undefined;
|
|
443
|
+
if (specifier)
|
|
444
|
+
engine.defaultImportSources.set(name, specifier);
|
|
445
|
+
lastType = t;
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
const recType = (0, helpers_1.exportsToRecord)(resolved);
|
|
449
|
+
currentEnv = currentEnv.extend(name, (0, types_1.mono)(recType));
|
|
450
|
+
engine.recordNodeType(decl, recType);
|
|
451
|
+
lastType = recType;
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
// Handle general object destructuring: const { Right } = Either
|
|
456
|
+
if (decl.id?.type === 'ObjectPattern' && decl.init) {
|
|
457
|
+
const result = engine.inferObjectDestructuring(decl, currentEnv, node.kind);
|
|
458
|
+
currentEnv = result.env;
|
|
459
|
+
lastType = result.lastType;
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
// Handle array destructuring: const [first, ...rest] = arr;
|
|
463
|
+
if (decl.id?.type === 'ArrayPattern' && decl.init) {
|
|
464
|
+
const initType = engine.inferExpression(decl.init, currentEnv);
|
|
465
|
+
const resolved = engine.subst.apply(initType);
|
|
466
|
+
const { env: newEnv } = engine.inferDestructuringPattern(decl.id, initType, currentEnv, node.kind);
|
|
467
|
+
currentEnv = newEnv;
|
|
468
|
+
lastType = resolved.tag === 'TCon' && resolved.name === types_1.T_ARRAY && resolved.args.length === 1
|
|
469
|
+
? resolved.args[0]
|
|
470
|
+
: initType;
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
const name = decl.id.type === 'Identifier' ? decl.id.name : undefined;
|
|
474
|
+
if (!name)
|
|
475
|
+
continue;
|
|
476
|
+
if (decl.init) {
|
|
477
|
+
// Provenance detection for `.constructor` typing & `new` strictness.
|
|
478
|
+
detectConstructorProvenance(engine, name, decl.init);
|
|
479
|
+
const named = engine.inferNamedBinding(name, decl, currentEnv, node.kind, node.start);
|
|
480
|
+
const { displayType } = named;
|
|
481
|
+
let { scheme } = named;
|
|
482
|
+
// `const k = "id"` holds exactly that text: keep it on its `String`.
|
|
483
|
+
if (node.kind === 'const' && decl.init.type === 'Literal' && typeof decl.init.value === 'string'
|
|
484
|
+
&& scheme.vars.length === 0) {
|
|
485
|
+
const bound = engine.subst.apply(scheme.type);
|
|
486
|
+
if (bound.tag === 'TCon' && bound.name === 'String' && bound.args.length === 0) {
|
|
487
|
+
scheme = { ...scheme, type: { ...bound, stringLiteral: String(decl.init.value) } };
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
currentEnv = currentEnv.extend(name, scheme);
|
|
491
|
+
engine.recordNodeType(decl, displayType);
|
|
492
|
+
// Remember the generalized scheme of a polymorphic binding so hover can
|
|
493
|
+
// show its generic type at the declaration site rather than a type
|
|
494
|
+
// polluted by a downstream call site's specialization.
|
|
495
|
+
if (scheme.vars.length > 0) {
|
|
496
|
+
engine.declaredSchemes.set(decl.start, scheme);
|
|
497
|
+
}
|
|
498
|
+
lastType = displayType;
|
|
499
|
+
// Propagate built-in `.constructor` marker by variable name so
|
|
500
|
+
// `const ac = [].constructor; ac(1, 2)` can specialise at the
|
|
501
|
+
// call site (e.g. widen Array(args) to [union]).
|
|
502
|
+
if (decl.init.type === 'MemberExpression' && !decl.init.computed
|
|
503
|
+
&& decl.init.property?.name === 'constructor') {
|
|
504
|
+
const kind = engine.builtinCtorKind.get(decl.init.property.start);
|
|
505
|
+
if (kind)
|
|
506
|
+
engine.builtinCtorByVarName.set(name, kind);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
else {
|
|
510
|
+
// No initializer: check for annotation (scope-checked via nodeStart)
|
|
511
|
+
let annotation = engine.annotationMap.get(name);
|
|
512
|
+
// Drop non-local annotations: a sidecar / globals / `::types` entry
|
|
513
|
+
// for `name` must not silently type a user's bare `let name;`
|
|
514
|
+
// declaration as if it were the file's own annotated binding.
|
|
515
|
+
if (annotation && !(0, commentParser_1.isLocalAnnotation)(annotation))
|
|
516
|
+
annotation = undefined;
|
|
517
|
+
if (annotation && annotation.nodeStart !== undefined
|
|
518
|
+
&& (annotation.nodeStart < node.start || annotation.nodeStart > decl.end)) {
|
|
519
|
+
annotation = undefined;
|
|
520
|
+
}
|
|
521
|
+
if (annotation?.scheme) {
|
|
522
|
+
const annotatedType = (0, env_1.instantiate)(annotation.scheme);
|
|
523
|
+
currentEnv = currentEnv.extend(name, annotation.scheme);
|
|
524
|
+
engine.recordNodeType(decl, annotatedType);
|
|
525
|
+
lastType = annotatedType;
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
const tv = (0, types_1.tVar)();
|
|
529
|
+
currentEnv = currentEnv.extend(name, (0, types_1.mono)(tv));
|
|
530
|
+
engine.recordNodeType(decl, tv);
|
|
531
|
+
lastType = tv;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
return [currentEnv, lastType];
|
|
536
|
+
}
|
|
537
|
+
// -----------------------------------------------------------------------
|
|
538
|
+
// Loop statements
|
|
539
|
+
// -----------------------------------------------------------------------
|
|
540
|
+
function inferWhileStatement(engine, node, env) {
|
|
541
|
+
engine.inferExpression(node.test, env);
|
|
542
|
+
engine.inferStatement(node.body, env);
|
|
543
|
+
return [env, types_1.tVoid];
|
|
544
|
+
}
|
|
545
|
+
function inferDoWhileStatement(engine, node, env) {
|
|
546
|
+
engine.inferStatement(node.body, env);
|
|
547
|
+
engine.inferExpression(node.test, env);
|
|
548
|
+
return [env, types_1.tVoid];
|
|
549
|
+
}
|
|
550
|
+
function inferForStatement(engine, node, env) {
|
|
551
|
+
let loopEnv = env;
|
|
552
|
+
// init: may be a VariableDeclaration or ExpressionStatement
|
|
553
|
+
if (node.init) {
|
|
554
|
+
if (node.init.type === 'VariableDeclaration') {
|
|
555
|
+
const [newEnv] = engine.inferVariableDeclaration(node.init, env);
|
|
556
|
+
loopEnv = newEnv;
|
|
557
|
+
}
|
|
558
|
+
else {
|
|
559
|
+
engine.inferExpression(node.init, env);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
if (node.test) {
|
|
563
|
+
engine.inferExpression(node.test, loopEnv);
|
|
564
|
+
}
|
|
565
|
+
if (node.update) {
|
|
566
|
+
engine.inferExpression(node.update, loopEnv);
|
|
567
|
+
}
|
|
568
|
+
engine.inferStatement(node.body, loopEnv);
|
|
569
|
+
return [env, types_1.tVoid];
|
|
570
|
+
}
|
|
571
|
+
function inferForInStatement(engine, node, env) {
|
|
572
|
+
let loopEnv = env;
|
|
573
|
+
if (node.left.type === 'VariableDeclaration') {
|
|
574
|
+
// for (const key in obj) — key is always String
|
|
575
|
+
const decl = node.left.declarations?.[0];
|
|
576
|
+
if (decl?.id?.type === 'Identifier') {
|
|
577
|
+
loopEnv = env.extend(decl.id.name, (0, types_1.mono)(types_1.tString));
|
|
578
|
+
engine.recordNodeType(decl.id, types_1.tString);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
if (node.right) {
|
|
582
|
+
engine.inferExpression(node.right, loopEnv);
|
|
583
|
+
}
|
|
584
|
+
engine.inferStatement(node.body, loopEnv);
|
|
585
|
+
return [env, types_1.tVoid];
|
|
586
|
+
}
|
|
587
|
+
function inferForOfStatement(engine, node, env) {
|
|
588
|
+
let loopEnv = env;
|
|
589
|
+
if (node.right) {
|
|
590
|
+
const iterableType = engine.inferExpression(node.right, env);
|
|
591
|
+
let resolved = engine.subst.apply(iterableType);
|
|
592
|
+
// For `for await (... of ...)`, unwrap Promise e (Array T) or Promise e T
|
|
593
|
+
if (node.await && resolved.tag === 'TCon' && resolved.name === 'Promise' && resolved.args.length === 2) {
|
|
594
|
+
resolved = resolved.args[1];
|
|
595
|
+
}
|
|
596
|
+
// If iterating over an Array, the loop variable gets the element type
|
|
597
|
+
let elemType = (0, types_1.tVar)();
|
|
598
|
+
if (resolved.tag === 'TCon' && (resolved.name === types_1.T_ARRAY || resolved.name === 'Set') && resolved.args.length > 0) {
|
|
599
|
+
elemType = resolved.args[0];
|
|
600
|
+
}
|
|
601
|
+
else if (resolved.tag === 'TCon' && resolved.name === 'Map' && resolved.args.length > 0) {
|
|
602
|
+
// for-of on Map<k, v> → [k, v] (tuple)
|
|
603
|
+
const k = resolved.args[0] ?? (0, types_1.tVar)();
|
|
604
|
+
const v = resolved.args[1] ?? (0, types_1.tVar)();
|
|
605
|
+
elemType = (0, types_1.tTuple)([k, v]);
|
|
606
|
+
}
|
|
607
|
+
else if (resolved.tag === 'TCon' && resolved.name === types_1.T_STRING) {
|
|
608
|
+
elemType = types_1.tString;
|
|
609
|
+
}
|
|
610
|
+
else if ((0, types_1.isTuple)(resolved) && resolved.args.length > 0) {
|
|
611
|
+
elemType = resolved.args.length === 1 ? resolved.args[0] : (0, types_1.tUnion)(resolved.args);
|
|
612
|
+
}
|
|
613
|
+
// For `for await ... of`, unwrap any per-element Promise so the loop
|
|
614
|
+
// variable receives the resolved value type (matches JS semantics: each
|
|
615
|
+
// iteration awaits the yielded value).
|
|
616
|
+
if (node.await && elemType.tag === 'TCon' && elemType.name === 'Promise' && elemType.args.length === 2) {
|
|
617
|
+
elemType = elemType.args[1];
|
|
618
|
+
}
|
|
619
|
+
if (node.left.type === 'VariableDeclaration') {
|
|
620
|
+
const decl = node.left.declarations?.[0];
|
|
621
|
+
if (decl?.id?.type === 'Identifier') {
|
|
622
|
+
loopEnv = env.extend(decl.id.name, (0, types_1.mono)(elemType));
|
|
623
|
+
engine.recordNodeType(decl.id, elemType);
|
|
624
|
+
}
|
|
625
|
+
else if (decl?.id?.type === 'ObjectPattern' || decl?.id?.type === 'ArrayPattern') {
|
|
626
|
+
const { env: newEnv } = engine.inferDestructuringPattern(decl.id, elemType, env, 'const');
|
|
627
|
+
loopEnv = newEnv;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
engine.inferStatement(node.body, loopEnv);
|
|
632
|
+
return [env, types_1.tVoid];
|
|
633
|
+
}
|
|
634
|
+
// -----------------------------------------------------------------------
|
|
635
|
+
// Switch statement
|
|
636
|
+
// -----------------------------------------------------------------------
|
|
637
|
+
function inferSwitchStatement(engine, node, env) {
|
|
638
|
+
const discriminant = engine.inferExpression(node.discriminant, env);
|
|
639
|
+
let resultType = types_1.tVoid;
|
|
640
|
+
for (const switchCase of (node.cases ?? [])) {
|
|
641
|
+
if (switchCase.test) {
|
|
642
|
+
const testType = engine.inferExpression(switchCase.test, env);
|
|
643
|
+
engine.unifyWithDiagnostic(testType, discriminant, switchCase.test);
|
|
644
|
+
}
|
|
645
|
+
// Infer the statements in each case
|
|
646
|
+
let caseEnv = env;
|
|
647
|
+
for (const stmt of (switchCase.consequent ?? [])) {
|
|
648
|
+
const [newEnv, stmtType] = engine.inferStatement(stmt, caseEnv);
|
|
649
|
+
caseEnv = newEnv;
|
|
650
|
+
resultType = stmtType;
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
return [env, resultType];
|
|
654
|
+
}
|
|
655
|
+
// -----------------------------------------------------------------------
|
|
656
|
+
// Try/catch/finally statement
|
|
657
|
+
// -----------------------------------------------------------------------
|
|
658
|
+
function inferTryStatement(engine, node, env) {
|
|
659
|
+
let resultType = types_1.tVoid;
|
|
660
|
+
// Infer the try block
|
|
661
|
+
if (node.block) {
|
|
662
|
+
const [, tryType] = engine.inferStatement(node.block, env);
|
|
663
|
+
resultType = tryType;
|
|
664
|
+
}
|
|
665
|
+
// Infer the catch clause
|
|
666
|
+
if (node.handler) {
|
|
667
|
+
let catchEnv = env;
|
|
668
|
+
const param = node.handler.param;
|
|
669
|
+
if (param) {
|
|
670
|
+
if (param.type === 'Identifier') {
|
|
671
|
+
// Catch parameter: type as the same `Error` constructor the builtin
|
|
672
|
+
// error constructors produce (`new Error(...) :: Error`), so the
|
|
673
|
+
// binding unifies with annotation slots declared as `Error` (e.g.
|
|
674
|
+
// an FP library's `try :: (() -> a) -> RTask r Error a` calling `rej (e)`).
|
|
675
|
+
// Member access `e.message` / `e.name` / `e.stack` resolves through
|
|
676
|
+
// the builtin Error property support, same as constructed errors.
|
|
677
|
+
const errorType = (0, types_1.tCon)('Error');
|
|
678
|
+
catchEnv = catchEnv.extend(param.name, (0, types_1.mono)(errorType));
|
|
679
|
+
engine.recordNodeType(param, errorType);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
// Optional catch binding: catch { } — no param
|
|
683
|
+
if (node.handler.body) {
|
|
684
|
+
engine.inferStatement(node.handler.body, catchEnv);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
// Infer the finally block
|
|
688
|
+
if (node.finalizer) {
|
|
689
|
+
engine.inferStatement(node.finalizer, env);
|
|
690
|
+
}
|
|
691
|
+
return [env, resultType];
|
|
692
|
+
}
|
|
693
|
+
// ---------------------------------------------------------------------------
|
|
694
|
+
// Local kind equalities under a `::narrow` guard
|
|
695
|
+
// ---------------------------------------------------------------------------
|
|
696
|
+
/**
|
|
697
|
+
* For `if (isArray (xs))` with `isArray` carrying `::narrow Array` and
|
|
698
|
+
* `xs :: f a` (an abstract, constraint-bound constructor), the true branch
|
|
699
|
+
* may treat `xs` as `[a]` — its OWN element type, not a fresh one — and
|
|
700
|
+
* knows `f ≡ Array` there. Returns the branch env (with `xs :: Array a`)
|
|
701
|
+
* and the equalities to push while inferring the branch. A no-op unless the
|
|
702
|
+
* guard narrows an argument from a kind-variable application to a concrete
|
|
703
|
+
* constructor of the same arity.
|
|
704
|
+
*/
|
|
705
|
+
function localKindEqsForGuard(engine, test, env, consEnv) {
|
|
706
|
+
const eqs = [];
|
|
707
|
+
if (consEnv === env || test.type !== 'CallExpression')
|
|
708
|
+
return { env: consEnv, eqs };
|
|
709
|
+
let outEnv = consEnv;
|
|
710
|
+
for (const arg of (test.arguments ?? [])) {
|
|
711
|
+
if (arg.type !== 'Identifier')
|
|
712
|
+
continue;
|
|
713
|
+
const name = arg.name;
|
|
714
|
+
const before = env.lookup(name);
|
|
715
|
+
const after = consEnv.lookup(name);
|
|
716
|
+
if (!before || !after || before === after)
|
|
717
|
+
continue;
|
|
718
|
+
const orig = engine.subst.apply(before.type);
|
|
719
|
+
const narrowedT = engine.subst.apply(after.type);
|
|
720
|
+
if (orig.tag !== 'TCon' || orig.args.length === 0)
|
|
721
|
+
continue;
|
|
722
|
+
const kv = (0, types_1.parseKvName)(orig.name);
|
|
723
|
+
if (!kv)
|
|
724
|
+
continue;
|
|
725
|
+
if (narrowedT.tag !== 'TCon' || (0, types_1.parseKvName)(narrowedT.name) || narrowedT.args.length !== orig.args.length)
|
|
726
|
+
continue;
|
|
727
|
+
eqs.push({ varId: kv.varId, varName: kv.varName, con: narrowedT.name, arity: orig.args.length });
|
|
728
|
+
outEnv = outEnv.extend(name, { vars: [], type: (0, types_1.tCon)(narrowedT.name, orig.args) });
|
|
729
|
+
}
|
|
730
|
+
return { env: outEnv, eqs };
|
|
731
|
+
}
|
|
732
|
+
/** Re-express a branch result `C x…` as `f x…` under a local `f ≡ C`. */
|
|
733
|
+
function liftUnderLocalKindEqs(engine, t) {
|
|
734
|
+
const r = engine.subst.apply(t);
|
|
735
|
+
if (r.tag !== 'TCon' || (0, types_1.parseKvName)(r.name))
|
|
736
|
+
return t;
|
|
737
|
+
for (let i = engine.localKindEqs.length - 1; i >= 0; i--) {
|
|
738
|
+
const eq = engine.localKindEqs[i];
|
|
739
|
+
if (eq.con === r.name && eq.arity === r.args.length) {
|
|
740
|
+
// Resolve the kv through the substitution: if `f` has meanwhile been
|
|
741
|
+
// bound to a concrete constructor, there is nothing abstract to lift.
|
|
742
|
+
const head = engine.subst.apply({ tag: 'TVar', id: eq.varId, name: eq.varName });
|
|
743
|
+
if (head.tag !== 'TVar')
|
|
744
|
+
return t;
|
|
745
|
+
return (0, types_1.tCon)((0, types_1.kvName)(eq.varName, head.id), r.args);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
return t;
|
|
749
|
+
}
|
|
750
|
+
//# sourceMappingURL=statements.js.map
|