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,775 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ============================================================================
|
|
3
|
+
// Module-related inference functions extracted from InferenceEngine
|
|
4
|
+
//
|
|
5
|
+
// Plain language: the cross-file glue. Handles `import`/`export` (and
|
|
6
|
+
// `require`) statements: figuring out what another file provides, bringing
|
|
7
|
+
// those types into the current file's scope, and recording what the current
|
|
8
|
+
// file offers to others. Class declarations and their methods are also
|
|
9
|
+
// registered here so they can be used across files.
|
|
10
|
+
// ============================================================================
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.inferImportDeclaration = inferImportDeclaration;
|
|
13
|
+
exports.inferExportNamedDeclaration = inferExportNamedDeclaration;
|
|
14
|
+
exports.inferExportAllDeclaration = inferExportAllDeclaration;
|
|
15
|
+
exports.inferExportDefaultDeclaration = inferExportDefaultDeclaration;
|
|
16
|
+
exports.inferClassDeclaration = inferClassDeclaration;
|
|
17
|
+
const helpers_1 = require("./helpers");
|
|
18
|
+
const types_1 = require("../hm/types");
|
|
19
|
+
const env_1 = require("../hm/env");
|
|
20
|
+
const substitution_1 = require("../hm/substitution");
|
|
21
|
+
const dataTypeRegistry_1 = require("../registries/dataTypeRegistry");
|
|
22
|
+
const methodRegistry_1 = require("../registries/methodRegistry");
|
|
23
|
+
const helpers_2 = require("./helpers");
|
|
24
|
+
const annotations_1 = require("./annotations");
|
|
25
|
+
const debugLog_1 = require("../debugLog");
|
|
26
|
+
const types_2 = require("./types");
|
|
27
|
+
// -----------------------------------------------------------------------
|
|
28
|
+
// Import / Export declarations
|
|
29
|
+
// -----------------------------------------------------------------------
|
|
30
|
+
/**
|
|
31
|
+
* Recursively collect every local binding name introduced by a destructuring
|
|
32
|
+
* pattern (Identifier, ObjectPattern, ArrayPattern, RestElement, AssignmentPattern).
|
|
33
|
+
*/
|
|
34
|
+
function collectPatternNames(pattern, out) {
|
|
35
|
+
if (!pattern)
|
|
36
|
+
return;
|
|
37
|
+
switch (pattern.type) {
|
|
38
|
+
case 'Identifier':
|
|
39
|
+
out.add(pattern.name);
|
|
40
|
+
return;
|
|
41
|
+
case 'ObjectPattern':
|
|
42
|
+
for (const prop of pattern.properties) {
|
|
43
|
+
if (prop.type === 'RestElement') {
|
|
44
|
+
collectPatternNames(prop.argument, out);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
// Property — `value` holds the local binding (may itself be a pattern)
|
|
48
|
+
collectPatternNames(prop.value, out);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return;
|
|
52
|
+
case 'ArrayPattern':
|
|
53
|
+
for (const elem of pattern.elements) {
|
|
54
|
+
if (elem)
|
|
55
|
+
collectPatternNames(elem, out);
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
case 'RestElement':
|
|
59
|
+
collectPatternNames(pattern.argument, out);
|
|
60
|
+
return;
|
|
61
|
+
case 'AssignmentPattern':
|
|
62
|
+
collectPatternNames(pattern.left, out);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** The closed aliases of this file (no free type variables, no
|
|
67
|
+
* parameters), such as a props record `Toast :: { message :: String, … }`. */
|
|
68
|
+
const closedAliasCache = new WeakMap();
|
|
69
|
+
function closedAliases(engine) {
|
|
70
|
+
// Per alias table (set once per `infer`): every import declaration of a
|
|
71
|
+
// file shares it.
|
|
72
|
+
const cached = closedAliasCache.get(engine.typeAliases);
|
|
73
|
+
if (cached)
|
|
74
|
+
return cached;
|
|
75
|
+
const closed = new Map();
|
|
76
|
+
for (const [name, body] of engine.typeAliases) {
|
|
77
|
+
if ((engine.typeAliasVars.get(name) ?? []).length > 0)
|
|
78
|
+
continue;
|
|
79
|
+
const vars = new Map();
|
|
80
|
+
(0, types_1.collectVarNames)(body, vars);
|
|
81
|
+
if (vars.size === 0)
|
|
82
|
+
closed.set(name, body);
|
|
83
|
+
}
|
|
84
|
+
closedAliasCache.set(engine.typeAliases, closed);
|
|
85
|
+
return closed;
|
|
86
|
+
}
|
|
87
|
+
const expandedSchemeCache = new WeakMap();
|
|
88
|
+
/** An imported scheme with its closed aliases expanded, as local
|
|
89
|
+
* annotations are. A name deep inside an imported type
|
|
90
|
+
* (`makeToaster :: … -> Promise Error (Toast -> ())`) otherwise stayed a
|
|
91
|
+
* bare constructor, and met the expanded record of a local annotation as
|
|
92
|
+
* "Cannot unify 'Toast' with 'Toast'". Aliases with free variables are
|
|
93
|
+
* left alone: they get fresh variables per use site. */
|
|
94
|
+
function expandImportedScheme(scheme, closed) {
|
|
95
|
+
if (closed.size === 0)
|
|
96
|
+
return scheme;
|
|
97
|
+
let perTable = expandedSchemeCache.get(closed);
|
|
98
|
+
if (!perTable) {
|
|
99
|
+
perTable = new WeakMap();
|
|
100
|
+
expandedSchemeCache.set(closed, perTable);
|
|
101
|
+
}
|
|
102
|
+
const hit = perTable.get(scheme);
|
|
103
|
+
if (hit)
|
|
104
|
+
return hit;
|
|
105
|
+
const type = (0, annotations_1.expandAliases)(closed, scheme.type);
|
|
106
|
+
const out = type === scheme.type ? scheme : { ...scheme, type };
|
|
107
|
+
perTable.set(scheme, out);
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
function inferImportDeclaration(engine, node, env) {
|
|
111
|
+
if (!engine.importResolver)
|
|
112
|
+
return env;
|
|
113
|
+
const specifier = node.source?.value;
|
|
114
|
+
if (!specifier)
|
|
115
|
+
return env;
|
|
116
|
+
const resolverResult = engine.importResolver(specifier);
|
|
117
|
+
if (!resolverResult) {
|
|
118
|
+
// Module could not be resolved — emit a diagnostic on the specifier string
|
|
119
|
+
engine.addDiag(`Cannot resolve module '${specifier}'`, node.source ?? node);
|
|
120
|
+
return env;
|
|
121
|
+
}
|
|
122
|
+
// Untyped module (e.g. an npm package without a `.hm` sidecar): bind every
|
|
123
|
+
// imported name to the opaque `Any` type. Unlike a fresh tvar, `Any` does
|
|
124
|
+
// not get unified into specific types by downstream usage — it propagates
|
|
125
|
+
// as `Any`, preventing usage-driven hallucinated signatures.
|
|
126
|
+
if ((0, types_2.isUntypedModule)(resolverResult)) {
|
|
127
|
+
let currentEnv = env;
|
|
128
|
+
for (const spec of node.specifiers ?? []) {
|
|
129
|
+
const localName = spec.local?.name;
|
|
130
|
+
if (!localName)
|
|
131
|
+
continue;
|
|
132
|
+
const scheme = (0, types_1.mono)(types_1.tAnyType);
|
|
133
|
+
currentEnv = currentEnv.extend(localName, scheme);
|
|
134
|
+
engine.recordNodeType(spec.local, (0, env_1.instantiate)(scheme));
|
|
135
|
+
if (spec.type === 'ImportDefaultSpecifier') {
|
|
136
|
+
engine.defaultImportSources.set(localName, specifier);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return currentEnv;
|
|
140
|
+
}
|
|
141
|
+
const exportedBindings = resolverResult;
|
|
142
|
+
let currentEnv = env;
|
|
143
|
+
let closed;
|
|
144
|
+
const expanded = (scheme) => {
|
|
145
|
+
if (!closed)
|
|
146
|
+
closed = closedAliases(engine);
|
|
147
|
+
return expandImportedScheme(scheme, closed);
|
|
148
|
+
};
|
|
149
|
+
for (const spec of node.specifiers ?? []) {
|
|
150
|
+
if (spec.type === 'ImportSpecifier') {
|
|
151
|
+
// import { Box } from './demo' or import { Box as B } from './demo'
|
|
152
|
+
const importedName = spec.imported?.name;
|
|
153
|
+
const localName = spec.local?.name;
|
|
154
|
+
const exportedScheme = exportedBindings.get(importedName);
|
|
155
|
+
const scheme = exportedScheme && expanded(exportedScheme);
|
|
156
|
+
if (scheme && localName) {
|
|
157
|
+
currentEnv = currentEnv.extend(localName, scheme);
|
|
158
|
+
engine.recordNodeType(spec.local, (0, env_1.instantiate)(scheme));
|
|
159
|
+
engine.propagateCurryFlags(scheme, localName, spec.local.start);
|
|
160
|
+
}
|
|
161
|
+
else if (!scheme && importedName && (0, types_2.isOpenWorldExports)(exportedBindings)) {
|
|
162
|
+
// Bundled (builtin) typings may lag behind the installed package
|
|
163
|
+
// version — bind unknown names to `Any` instead of erroring.
|
|
164
|
+
if (localName) {
|
|
165
|
+
const anyScheme = (0, types_1.mono)(types_1.tAnyType);
|
|
166
|
+
currentEnv = currentEnv.extend(localName, anyScheme);
|
|
167
|
+
engine.recordNodeType(spec.local, (0, env_1.instantiate)(anyScheme));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
else if (!scheme && importedName) {
|
|
171
|
+
// The name does not exist in the module's exports — emit a diagnostic
|
|
172
|
+
engine.addDiag(`'${importedName}' is not exported from '${specifier}'`, spec.imported ?? spec);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
else if (spec.type === 'ImportDefaultSpecifier') {
|
|
176
|
+
// import Box from './demo'
|
|
177
|
+
const localName = spec.local?.name;
|
|
178
|
+
const defaultScheme = exportedBindings.get('default');
|
|
179
|
+
const scheme = (defaultScheme && expanded(defaultScheme))
|
|
180
|
+
?? ((0, types_2.isOpenWorldExports)(exportedBindings) ? (0, types_1.mono)(types_1.tAnyType) : undefined);
|
|
181
|
+
if (scheme && localName) {
|
|
182
|
+
currentEnv = currentEnv.extend(localName, scheme);
|
|
183
|
+
engine.recordNodeType(spec.local, (0, env_1.instantiate)(scheme));
|
|
184
|
+
engine.defaultImportSources.set(localName, specifier);
|
|
185
|
+
engine.propagateCurryFlags(scheme, localName, spec.local.start);
|
|
186
|
+
}
|
|
187
|
+
else if (localName) {
|
|
188
|
+
// No default export: report it at the import instead of leaving the
|
|
189
|
+
// name unbound (which surfaced later as a misleading
|
|
190
|
+
// "Unbound variable" at every use site).
|
|
191
|
+
engine.addDiag(`Module '${specifier}' has no default export`, spec.local ?? spec);
|
|
192
|
+
const anyScheme = (0, types_1.mono)(types_1.tAnyType);
|
|
193
|
+
currentEnv = currentEnv.extend(localName, anyScheme);
|
|
194
|
+
engine.recordNodeType(spec.local, (0, env_1.instantiate)(anyScheme));
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
else if (spec.type === 'ImportNamespaceSpecifier') {
|
|
198
|
+
// import * as Demo from './demo' — create a record of all exports
|
|
199
|
+
const localName = spec.local?.name;
|
|
200
|
+
if (localName) {
|
|
201
|
+
currentEnv = currentEnv.extend(localName, (0, types_1.mono)((0, helpers_1.exportsToRecord)(exportedBindings)));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return currentEnv;
|
|
206
|
+
}
|
|
207
|
+
function inferExportNamedDeclaration(engine, node, env) {
|
|
208
|
+
if (node.declaration) {
|
|
209
|
+
// export const x = ...; export function f() {}
|
|
210
|
+
const [newEnv, type] = engine.inferStatement(node.declaration, env);
|
|
211
|
+
// Mark declared names as exported
|
|
212
|
+
if (node.declaration.type === 'VariableDeclaration') {
|
|
213
|
+
for (const decl of node.declaration.declarations) {
|
|
214
|
+
collectPatternNames(decl.id, engine.exportedNames);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
else if ((node.declaration.type === 'FunctionDeclaration' || node.declaration.type === 'ClassDeclaration')
|
|
218
|
+
&& node.declaration.id?.name) {
|
|
219
|
+
engine.exportedNames.add(node.declaration.id.name);
|
|
220
|
+
}
|
|
221
|
+
return [newEnv, type];
|
|
222
|
+
}
|
|
223
|
+
if (node.specifiers) {
|
|
224
|
+
// Re-export from another module: `export { x, y as z } from './other'`
|
|
225
|
+
const sourceSpec = node.source?.value;
|
|
226
|
+
if (sourceSpec && engine.importResolver) {
|
|
227
|
+
const resolved = engine.importResolver(sourceSpec);
|
|
228
|
+
if ((0, types_2.isUntypedModule)(resolved)) {
|
|
229
|
+
// Re-exporting from an untyped npm module — surface the names as
|
|
230
|
+
// exports bound to fresh TVars without inspecting the source.
|
|
231
|
+
for (const spec of node.specifiers) {
|
|
232
|
+
const localName = spec.local?.name;
|
|
233
|
+
const exportedName = spec.exported?.name;
|
|
234
|
+
const targetName = exportedName || localName;
|
|
235
|
+
if (!targetName)
|
|
236
|
+
continue;
|
|
237
|
+
engine.exportedNames.add(targetName);
|
|
238
|
+
engine.cjsExportTypes.set(targetName, types_1.tAnyType);
|
|
239
|
+
}
|
|
240
|
+
return [env, types_1.tVoid];
|
|
241
|
+
}
|
|
242
|
+
if (resolved) {
|
|
243
|
+
for (const spec of node.specifiers) {
|
|
244
|
+
const localName = spec.local?.name;
|
|
245
|
+
const exportedName = spec.exported?.name;
|
|
246
|
+
const targetName = exportedName || localName;
|
|
247
|
+
if (!localName || !targetName)
|
|
248
|
+
continue;
|
|
249
|
+
const scheme = resolved.get(localName);
|
|
250
|
+
if (scheme) {
|
|
251
|
+
engine.exportedNames.add(targetName);
|
|
252
|
+
engine.cjsExportTypes.set(targetName, (0, env_1.instantiate)(scheme));
|
|
253
|
+
}
|
|
254
|
+
else if ((0, types_2.isOpenWorldExports)(resolved)) {
|
|
255
|
+
// Bundled typings — unknown re-exported names stay usable as Any.
|
|
256
|
+
engine.exportedNames.add(targetName);
|
|
257
|
+
engine.cjsExportTypes.set(targetName, types_1.tAnyType);
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
engine.addDiag(`'${localName}' is not exported from '${sourceSpec}'`, spec.local ?? spec);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
engine.addDiag(`Cannot resolve module '${sourceSpec}'`, node.source ?? node);
|
|
266
|
+
}
|
|
267
|
+
return [env, types_1.tVoid];
|
|
268
|
+
}
|
|
269
|
+
// Plain named export specifier: `export { x, y as z }`
|
|
270
|
+
for (const spec of node.specifiers) {
|
|
271
|
+
const localName = spec.local?.name;
|
|
272
|
+
const exportedName = spec.exported?.name;
|
|
273
|
+
if (localName) {
|
|
274
|
+
engine.exportedNames.add(exportedName || localName);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return [env, types_1.tVoid];
|
|
279
|
+
}
|
|
280
|
+
function inferExportAllDeclaration(engine, node, env) {
|
|
281
|
+
// export * from './other' — re-export all named (excluding 'default')
|
|
282
|
+
// export * as ns from './other' — re-export all under namespace
|
|
283
|
+
const specifier = node.source?.value;
|
|
284
|
+
if (!specifier || !engine.importResolver)
|
|
285
|
+
return [env, types_1.tVoid];
|
|
286
|
+
const resolved = engine.importResolver(specifier);
|
|
287
|
+
if (!resolved) {
|
|
288
|
+
engine.addDiag(`Cannot resolve module '${specifier}'`, node.source ?? node);
|
|
289
|
+
return [env, types_1.tVoid];
|
|
290
|
+
}
|
|
291
|
+
if ((0, types_2.isUntypedModule)(resolved)) {
|
|
292
|
+
// Untyped npm module: nothing to re-export with concrete types.
|
|
293
|
+
// For `export * as ns from ...` bind ns to a fresh TVar.
|
|
294
|
+
const exportedAs = node.exported?.name;
|
|
295
|
+
if (exportedAs) {
|
|
296
|
+
engine.exportedNames.add(exportedAs);
|
|
297
|
+
engine.cjsExportTypes.set(exportedAs, types_1.tAnyType);
|
|
298
|
+
}
|
|
299
|
+
return [env, types_1.tVoid];
|
|
300
|
+
}
|
|
301
|
+
const exportedAs = node.exported?.name;
|
|
302
|
+
if (exportedAs) {
|
|
303
|
+
// export * as ns from './other' — bundle into a single namespace export
|
|
304
|
+
const recType = (0, helpers_1.exportsToRecord)(resolved);
|
|
305
|
+
engine.exportedNames.add(exportedAs);
|
|
306
|
+
engine.cjsExportTypes.set(exportedAs, recType);
|
|
307
|
+
return [env, types_1.tVoid];
|
|
308
|
+
}
|
|
309
|
+
for (const [name, scheme] of resolved) {
|
|
310
|
+
if (name === 'default')
|
|
311
|
+
continue;
|
|
312
|
+
engine.exportedNames.add(name);
|
|
313
|
+
engine.cjsExportTypes.set(name, (0, env_1.instantiate)(scheme));
|
|
314
|
+
}
|
|
315
|
+
return [env, types_1.tVoid];
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Copy the metadata that shapes calls of a binding (optional trailing
|
|
319
|
+
* parameters from JS defaults, type predicates, conditional-return
|
|
320
|
+
* narrowing) onto the rebuilt `default` scheme. Without it a default import
|
|
321
|
+
* asked for every argument of `(key, url, pollMs = 0) => …`.
|
|
322
|
+
*/
|
|
323
|
+
function copyCallSiteMetadata(from, to) {
|
|
324
|
+
if (!from)
|
|
325
|
+
return;
|
|
326
|
+
if (from.optionalParamSlots && from.optionalParamSlots.length > 0)
|
|
327
|
+
to.optionalParamSlots = from.optionalParamSlots.slice();
|
|
328
|
+
if (from.typePredicate && !to.typePredicate)
|
|
329
|
+
to.typePredicate = from.typePredicate;
|
|
330
|
+
if (from.conditionalReturn && !to.conditionalReturn)
|
|
331
|
+
to.conditionalReturn = from.conditionalReturn;
|
|
332
|
+
}
|
|
333
|
+
function inferExportDefaultDeclaration(engine, node, env) {
|
|
334
|
+
if (node.declaration) {
|
|
335
|
+
if (node.declaration.type === 'FunctionDeclaration' || node.declaration.type === 'ClassDeclaration') {
|
|
336
|
+
const [newEnv, type] = engine.inferStatement(node.declaration, env);
|
|
337
|
+
const name = node.declaration.id?.name;
|
|
338
|
+
if (name) {
|
|
339
|
+
engine.exportedNames.add(name);
|
|
340
|
+
}
|
|
341
|
+
// Bind 'default' in the env so export collection — and default imports in
|
|
342
|
+
// other files (`import name from './f'`) — can find it. This also covers
|
|
343
|
+
// anonymous `export default function () {}` / `export default class {}`,
|
|
344
|
+
// which have no name of their own: the importer supplies the name.
|
|
345
|
+
engine.exportedNames.add('default');
|
|
346
|
+
const resolved = engine.subst.apply(type);
|
|
347
|
+
const scheme = (0, env_1.generalize)(newEnv, resolved);
|
|
348
|
+
// The rebuilt scheme must carry the call-site metadata of the
|
|
349
|
+
// declaration (JS default parameters, narrowing), like a named export.
|
|
350
|
+
if (name)
|
|
351
|
+
copyCallSiteMetadata(newEnv.lookup(name), scheme);
|
|
352
|
+
const envWithDefault = newEnv.extend('default', scheme);
|
|
353
|
+
return [envWithDefault, type];
|
|
354
|
+
}
|
|
355
|
+
// export default <expression>
|
|
356
|
+
if (node.declaration.type === 'Identifier') {
|
|
357
|
+
engine.defaultExportName = node.declaration.name;
|
|
358
|
+
}
|
|
359
|
+
const type = engine.inferExpression(node.declaration, env);
|
|
360
|
+
// Consume pendingIsCurriedResult set by curry wrapper calls (e.g. export default curry(fn))
|
|
361
|
+
const pendingCurried = engine.pendingIsCurriedResult;
|
|
362
|
+
engine.pendingIsCurriedResult = false;
|
|
363
|
+
// Store as 'default' export
|
|
364
|
+
const resolved = engine.subst.apply(type);
|
|
365
|
+
const scheme = (0, env_1.generalize)(env, resolved);
|
|
366
|
+
// Propagate isCurriedResult / isCurryWrapper flags
|
|
367
|
+
if (pendingCurried)
|
|
368
|
+
scheme.isCurriedResult = true;
|
|
369
|
+
if (node.declaration.type === 'Identifier') {
|
|
370
|
+
const identScheme = env.lookup(node.declaration.name);
|
|
371
|
+
if (identScheme?.isCurriedResult)
|
|
372
|
+
scheme.isCurriedResult = true;
|
|
373
|
+
if (identScheme?.isCurryWrapper)
|
|
374
|
+
scheme.isCurryWrapper = true;
|
|
375
|
+
if (identScheme?.curryLeadingArgs)
|
|
376
|
+
scheme.curryLeadingArgs = identScheme.curryLeadingArgs;
|
|
377
|
+
copyCallSiteMetadata(identScheme, scheme);
|
|
378
|
+
}
|
|
379
|
+
const newEnv = env.extend('default', scheme);
|
|
380
|
+
engine.exportedNames.add('default');
|
|
381
|
+
return [newEnv, type];
|
|
382
|
+
}
|
|
383
|
+
return [env, types_1.tVoid];
|
|
384
|
+
}
|
|
385
|
+
// -----------------------------------------------------------------------
|
|
386
|
+
// Class declarations
|
|
387
|
+
// -----------------------------------------------------------------------
|
|
388
|
+
function inferClassDeclaration(engine, node, env) {
|
|
389
|
+
const name = node.id?.name;
|
|
390
|
+
if (!name)
|
|
391
|
+
return [env, (0, types_1.tVar)()];
|
|
392
|
+
// Track that `name` is a class-style constructor (used for `new`
|
|
393
|
+
// strictness diagnostics and `.constructor` provenance). Variants of
|
|
394
|
+
// a registered sum type get a more specific marker.
|
|
395
|
+
engine.constructorProvenance.set(name, (0, dataTypeRegistry_1.isVariant)(name) ? 'sumtype-variant' : 'class');
|
|
396
|
+
const annotation = engine.annotationMap.get(name);
|
|
397
|
+
const classBody = node.body?.body ?? [];
|
|
398
|
+
// Find the constructor MethodDefinition
|
|
399
|
+
const constructorMethod = (0, helpers_2.findConstructor)(classBody);
|
|
400
|
+
// A class is a constructor, so its annotation must be a function type
|
|
401
|
+
// (`() -> Foo` or `(A, B) -> Foo`). A bare `Foo :: Foo` is almost
|
|
402
|
+
// certainly a mistake — flag it and fall through to the unannotated
|
|
403
|
+
// inference path so downstream type-checking can still proceed.
|
|
404
|
+
//
|
|
405
|
+
// Exception: nullary variant classes of a registered sum type are
|
|
406
|
+
// legitimately annotated with the parent type directly
|
|
407
|
+
// (e.g. `Nothing :: Maybe a` for `Maybe :: Just a | Nothing`).
|
|
408
|
+
if (annotation?.scheme && annotation.scheme.type.tag !== 'TFun' && !(0, dataTypeRegistry_1.isVariant)(name)) {
|
|
409
|
+
engine.addDiag(`Invalid type annotation for class '${name}': a class is a constructor, ` +
|
|
410
|
+
`so its annotation must be a function type like '() -> ${name}' ` +
|
|
411
|
+
`(or '(A, B) -> ${name}' when the constructor takes parameters), ` +
|
|
412
|
+
`got '${(0, types_1.prettyTypeToString)(annotation.scheme.type)}'`, node.id ?? node);
|
|
413
|
+
// Treat as unannotated from here on.
|
|
414
|
+
}
|
|
415
|
+
else if (annotation?.scheme) {
|
|
416
|
+
const expectedType = engine.instantiateWithConstraints(annotation.scheme, node, annotation);
|
|
417
|
+
// The return type of the constructor annotation is the instance type
|
|
418
|
+
// (e.g. for `Just :: a -> Maybe a` the instance type is `Maybe a`).
|
|
419
|
+
const instanceType = (0, types_1.getReturnType)(expectedType);
|
|
420
|
+
// --- Infer constructor body ---
|
|
421
|
+
if (constructorMethod) {
|
|
422
|
+
const ctorEnv = env
|
|
423
|
+
.extend('this', (0, types_1.mono)(instanceType))
|
|
424
|
+
.extend(name, annotation.scheme);
|
|
425
|
+
const ctorFuncType = engine.inferFunctionExpression(constructorMethod.value, ctorEnv, expectedType);
|
|
426
|
+
const diagsBefore = engine.diagnostics.length;
|
|
427
|
+
engine.checkRigid(ctorFuncType, annotation.scheme, node);
|
|
428
|
+
if (engine.diagnostics.length === diagsBefore) {
|
|
429
|
+
engine.unifyWithDiagnostic(ctorFuncType, expectedType, constructorMethod);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
// Bind the class in env so method bodies can reference it (e.g. `new Just(...)`)
|
|
433
|
+
const classEnv = env.extend(name, annotation.scheme);
|
|
434
|
+
// --- Infer instance methods and getters/setters ---
|
|
435
|
+
for (const member of classBody) {
|
|
436
|
+
// --- Class fields: `x = 1;`, `#y = 2;`, `static z = 3;` ---
|
|
437
|
+
if (member.type === 'PropertyDefinition') {
|
|
438
|
+
inferClassPropertyDefinition(engine, member, classEnv, instanceType, name);
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
// --- Static initialisation block: `static { ... }` ---
|
|
442
|
+
if (member.type === 'StaticBlock') {
|
|
443
|
+
const staticEnv = classEnv;
|
|
444
|
+
for (const stmt of (member.body ?? [])) {
|
|
445
|
+
engine.inferStatement(stmt, staticEnv);
|
|
446
|
+
}
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
if (member.type !== 'MethodDefinition')
|
|
450
|
+
continue;
|
|
451
|
+
// PrivateIdentifier method names (e.g. `#priv() { ... }`) are accepted;
|
|
452
|
+
// we treat them like regular methods but skip registry lookups (they
|
|
453
|
+
// are not callable from outside the class).
|
|
454
|
+
if (member.key?.type !== 'Identifier' && member.key?.type !== 'PrivateIdentifier')
|
|
455
|
+
continue;
|
|
456
|
+
if (member.key.type === 'PrivateIdentifier') {
|
|
457
|
+
// Private method: infer the body so completion / hover work inside
|
|
458
|
+
// it; don't register publicly. Honour `// Class##name :: T`
|
|
459
|
+
// annotations and stash the resolved type for `this.#name` lookups.
|
|
460
|
+
const fieldName = member.key.name;
|
|
461
|
+
const annKey = `${name}##${fieldName}`;
|
|
462
|
+
const annotation = engine.annotationMap.get(annKey);
|
|
463
|
+
const methodEnv = classEnv.extend('this', (0, types_1.mono)(instanceType));
|
|
464
|
+
const inferredType = engine.inferFunctionExpression(member.value, methodEnv);
|
|
465
|
+
if (annotation?.scheme) {
|
|
466
|
+
const expectedType = (0, env_1.instantiate)(annotation.scheme);
|
|
467
|
+
engine.unifyWithDiagnostic(inferredType, expectedType, member.key);
|
|
468
|
+
}
|
|
469
|
+
const resolved = engine.subst.apply(inferredType);
|
|
470
|
+
engine.privateClassMembers.set(`${name}##${fieldName}`, resolved);
|
|
471
|
+
engine.recordNodeType(member.key, resolved);
|
|
472
|
+
engine.methodSignatures.set(member.key.start, `${name}##${fieldName} :: ${(0, types_1.prettyTypeToString)(resolved)}`);
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
if (member.kind === 'constructor')
|
|
476
|
+
continue; // already handled above
|
|
477
|
+
const methodName = member.key.name;
|
|
478
|
+
if (member.kind === 'get') {
|
|
479
|
+
inferClassGetter(engine, member, classEnv, instanceType, name, methodName);
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
if (member.kind === 'set') {
|
|
483
|
+
// Setter: treat as a single-arg function returning void
|
|
484
|
+
const setterEnv = classEnv.extend('this', (0, types_1.mono)(instanceType));
|
|
485
|
+
const setterType = engine.inferFunctionExpression(member.value, setterEnv);
|
|
486
|
+
engine.recordNodeType(member.key, setterType);
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
// Regular method — resolve annotation
|
|
490
|
+
const methodAnn = resolveMethodAnnotation(engine, member, name, methodName);
|
|
491
|
+
const isStatic = !!member.static;
|
|
492
|
+
if (methodAnn?.receiver && methodAnn?.methodType && methodAnn?.scheme) {
|
|
493
|
+
const { thisType: methodThisType, expectedType: expectedMethodType } = engine.coordinateInstantiate(methodAnn);
|
|
494
|
+
let methodEnv = classEnv.extend('this', (0, helpers_2.variantThisScheme)(name, methodThisType) ?? (0, types_1.mono)(methodThisType));
|
|
495
|
+
methodEnv = methodEnv.extend('__signatureThis', (0, types_1.mono)(types_1.tVoid));
|
|
496
|
+
if ((0, dataTypeRegistry_1.isVariant)(name)) {
|
|
497
|
+
methodEnv = methodEnv.extend('__sumtypeVariant', (0, types_1.mono)((0, types_1.tCon)(name, [])));
|
|
498
|
+
}
|
|
499
|
+
engine.annotationMethodNode = member;
|
|
500
|
+
const methodType = engine.inferFunctionExpression(member.value, methodEnv, expectedMethodType);
|
|
501
|
+
engine.annotationMethodNode = null;
|
|
502
|
+
const diagsBefore = engine.diagnostics.length;
|
|
503
|
+
engine.checkRigid(methodType, methodAnn.scheme, member);
|
|
504
|
+
// Save and restore the substitution around unification so that
|
|
505
|
+
// parameter types recorded during inferFunctionExpression are not
|
|
506
|
+
// corrupted. e.g. Nothing.map returns `this` (Maybe a) but the
|
|
507
|
+
// annotation expects Maybe b — unification would bind a=b, turning
|
|
508
|
+
// the parameter type `a -> b` into `b -> b`.
|
|
509
|
+
const savedSubst = new substitution_1.Substitution(engine.subst.entries());
|
|
510
|
+
if (engine.diagnostics.length === diagsBefore) {
|
|
511
|
+
engine.unifyWithDiagnostic(methodType, expectedMethodType, member);
|
|
512
|
+
}
|
|
513
|
+
engine.subst = savedSubst;
|
|
514
|
+
engine.recordNodeType(member.key, methodType);
|
|
515
|
+
// Re-record parameter nodeTypes using uncorrupted annotation types.
|
|
516
|
+
// Body inference may have corrupted the TVars created by coordinateInstantiate
|
|
517
|
+
// (e.g. `of` with type `f` gets unified to `Either e a -> f (Either e b)`
|
|
518
|
+
// when the body calls `of(this)`). The annotation is the source of truth.
|
|
519
|
+
// We call coordinateInstantiate again to get a second set of fresh TVars
|
|
520
|
+
// that are not in the current substitution.
|
|
521
|
+
const fnNode = member.value;
|
|
522
|
+
if (fnNode && fnNode.params) {
|
|
523
|
+
const { expectedType: displayMethodType } = engine.coordinateInstantiate(methodAnn);
|
|
524
|
+
const decomposed = (0, types_1.decomposeFunParams)(displayMethodType, fnNode.params.length);
|
|
525
|
+
if (decomposed) {
|
|
526
|
+
for (let i = 0; i < fnNode.params.length && i < decomposed.paramTypes.length; i++) {
|
|
527
|
+
const param = fnNode.params[i];
|
|
528
|
+
if (param.type === 'Identifier') {
|
|
529
|
+
engine.recordNodeType(param, decomposed.paramTypes[i]);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
// Register annotation at method key offset so hover can find it
|
|
535
|
+
engine.annotationByOffset.set(member.key.start, methodAnn);
|
|
536
|
+
}
|
|
537
|
+
else if (isStatic && methodAnn?.scheme) {
|
|
538
|
+
// Static method with annotation (no ~> receiver).
|
|
539
|
+
// Infer the body normally but DON'T re-register the method — the
|
|
540
|
+
// annotation-based registration from registerStaticMethod is used
|
|
541
|
+
// at call sites (which creates fresh TVars, preventing bleeding).
|
|
542
|
+
const methodEnv = classEnv;
|
|
543
|
+
const inferredType = engine.inferFunctionExpression(member.value, methodEnv);
|
|
544
|
+
const methodType = inferredType.tag === 'TFun' ? inferredType : (0, types_1.tFun)(types_1.tVoid, inferredType);
|
|
545
|
+
engine.recordNodeType(member.key, methodType);
|
|
546
|
+
engine.annotationByOffset.set(member.key.start, methodAnn);
|
|
547
|
+
engine.methodSignatures.set(member.key.start, `${name}.${methodName} :: ${methodAnn.raw}`);
|
|
548
|
+
// Register constraint info for body parameter TVars so that hover
|
|
549
|
+
// and autocomplete work on constraint-typed parameters.
|
|
550
|
+
if (methodAnn.scheme.constraints) {
|
|
551
|
+
engine.registerStaticMethodConstraints(methodAnn.scheme, methodType);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
else {
|
|
555
|
+
inferUnannotatedClassMethod(engine, member, classEnv, instanceType, name, methodName, isStatic);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
const newEnv = env.extend(name, annotation.scheme);
|
|
559
|
+
const displayType = (0, env_1.instantiate)(annotation.scheme);
|
|
560
|
+
engine.recordNodeType(node.id, displayType);
|
|
561
|
+
return [newEnv, engine.subst.apply(expectedType)];
|
|
562
|
+
}
|
|
563
|
+
// --- No annotation: infer constructor from body ---
|
|
564
|
+
let constructorType;
|
|
565
|
+
if (constructorMethod) {
|
|
566
|
+
constructorType = engine.inferFunctionExpression(constructorMethod.value, env);
|
|
567
|
+
}
|
|
568
|
+
else {
|
|
569
|
+
constructorType = (0, types_1.tVar)();
|
|
570
|
+
}
|
|
571
|
+
// Walk class fields and static blocks even without an annotation so that
|
|
572
|
+
// their initialisers are type-checked and recorded.
|
|
573
|
+
for (const member of classBody) {
|
|
574
|
+
if (member.type === 'PropertyDefinition' && member.value) {
|
|
575
|
+
try {
|
|
576
|
+
engine.inferExpression(member.value, env);
|
|
577
|
+
}
|
|
578
|
+
catch (e) {
|
|
579
|
+
(0, debugLog_1.debugLog)('modules.classFieldInit', e);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
else if (member.type === 'StaticBlock') {
|
|
583
|
+
for (const stmt of (member.body ?? [])) {
|
|
584
|
+
try {
|
|
585
|
+
engine.inferStatement(stmt, env);
|
|
586
|
+
}
|
|
587
|
+
catch (e) {
|
|
588
|
+
(0, debugLog_1.debugLog)('modules.staticBlock', e);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
const resolved = engine.subst.apply(constructorType);
|
|
594
|
+
const scheme = (0, env_1.generalize)(env, resolved);
|
|
595
|
+
const newEnv = env.extend(name, scheme);
|
|
596
|
+
engine.recordNodeType(node.id, resolved);
|
|
597
|
+
return [newEnv, constructorType];
|
|
598
|
+
}
|
|
599
|
+
// -----------------------------------------------------------------------
|
|
600
|
+
// inferClassDeclaration helpers
|
|
601
|
+
// -----------------------------------------------------------------------
|
|
602
|
+
/**
|
|
603
|
+
* Infer a class field declaration: `x = 1;`, `#priv = 2;`, `static z = 3;`.
|
|
604
|
+
* The initialiser is type-checked in the class environment. Instance fields
|
|
605
|
+
* are recorded as constructor properties so hover and dotted access work.
|
|
606
|
+
* Static fields are registered on the class registry.
|
|
607
|
+
*/
|
|
608
|
+
function inferClassPropertyDefinition(engine, member, classEnv, instanceType, className) {
|
|
609
|
+
const isStatic = !!member.static;
|
|
610
|
+
const isPrivate = member.key?.type === 'PrivateIdentifier';
|
|
611
|
+
const fieldName = member.key?.name;
|
|
612
|
+
// Infer the initialiser. Instance fields run with `this` bound to the
|
|
613
|
+
// instance type so `this.other` references work; static fields run in the
|
|
614
|
+
// class env directly.
|
|
615
|
+
let valueType = (0, types_1.tVar)();
|
|
616
|
+
if (member.value) {
|
|
617
|
+
const initEnv = isStatic ? classEnv : classEnv.extend('this', (0, types_1.mono)(instanceType));
|
|
618
|
+
valueType = engine.inferExpression(member.value, initEnv);
|
|
619
|
+
}
|
|
620
|
+
if (!fieldName)
|
|
621
|
+
return;
|
|
622
|
+
engine.recordNodeType(member.key, valueType);
|
|
623
|
+
// Look for an explicit annotation: `// ClassName#field :: T` for instance
|
|
624
|
+
// fields, `// ClassName.field :: T` for static fields, and
|
|
625
|
+
// `// ClassName##field :: T` for private fields (only visible inside the
|
|
626
|
+
// class body — JS only allows `obj.#field` from within the class).
|
|
627
|
+
const annKey = isPrivate
|
|
628
|
+
? `${className}##${fieldName}`
|
|
629
|
+
: isStatic ? `${className}.${fieldName}` : `${className}#${fieldName}`;
|
|
630
|
+
const annotation = engine.annotationMap.get(annKey);
|
|
631
|
+
if (annotation?.scheme) {
|
|
632
|
+
const expected = (0, env_1.instantiate)(annotation.scheme);
|
|
633
|
+
if (member.value)
|
|
634
|
+
engine.unifyWithDiagnostic(valueType, expected, member);
|
|
635
|
+
valueType = expected;
|
|
636
|
+
}
|
|
637
|
+
if (isPrivate) {
|
|
638
|
+
// Record the private member on the engine so `this.#field` lookups in
|
|
639
|
+
// the member's own class can return the inferred / annotated type.
|
|
640
|
+
const resolved = engine.subst.apply(valueType);
|
|
641
|
+
engine.privateClassMembers.set(`${className}##${fieldName}`, resolved);
|
|
642
|
+
engine.methodSignatures.set(member.key.start, `${className}##${fieldName} :: ${(0, types_1.prettyTypeToString)(resolved)}`);
|
|
643
|
+
}
|
|
644
|
+
else if (isStatic) {
|
|
645
|
+
// Register as a static property on the class so `ClassName.field` works.
|
|
646
|
+
const resolved = engine.subst.apply(valueType);
|
|
647
|
+
(0, methodRegistry_1.registerMethod)(className, fieldName, {
|
|
648
|
+
signature: `${className}.${fieldName} :: ${(0, types_1.prettyTypeToString)(resolved)}`,
|
|
649
|
+
factory: (_typeArgs) => resolved,
|
|
650
|
+
isStatic: true,
|
|
651
|
+
});
|
|
652
|
+
engine.methodSignatures.set(member.key.start, `${className}.${fieldName} :: ${(0, types_1.prettyTypeToString)(resolved)}`);
|
|
653
|
+
}
|
|
654
|
+
else {
|
|
655
|
+
// Mark as an instance property assigned in the (implicit) constructor.
|
|
656
|
+
engine.constructorProperties.add(`${className}#${fieldName}`);
|
|
657
|
+
const resolved = engine.subst.apply(valueType);
|
|
658
|
+
engine.methodSignatures.set(member.key.start, `${className}#${fieldName} :: ${(0, types_1.prettyTypeToString)(instanceType)} ~> ${(0, types_1.prettyTypeToString)(resolved)}`);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
/** Infer a getter as a property (not a method). */
|
|
662
|
+
function inferClassGetter(engine, member, classEnv, instanceType, className, methodName) {
|
|
663
|
+
// Remove annotation at offset so inferFunctionExpression doesn't try to
|
|
664
|
+
// unify the getter body (a function) with the annotation's property type.
|
|
665
|
+
const savedAnn = engine.annotationByOffset.get(member.value?.start ?? member.start);
|
|
666
|
+
if (savedAnn) {
|
|
667
|
+
engine.annotationByOffset.delete(member.value?.start ?? member.start);
|
|
668
|
+
}
|
|
669
|
+
const getterEnv = classEnv.extend('this', (0, types_1.mono)(instanceType));
|
|
670
|
+
const getterType = engine.inferFunctionExpression(member.value, getterEnv);
|
|
671
|
+
const returnType = (0, types_1.getReturnType)(getterType);
|
|
672
|
+
// Validate return type against annotation
|
|
673
|
+
let getterAnn = savedAnn;
|
|
674
|
+
if (!getterAnn) {
|
|
675
|
+
getterAnn = engine.annotationMap.get(`${className}#${methodName}`);
|
|
676
|
+
if (!getterAnn) {
|
|
677
|
+
const parentName = (0, dataTypeRegistry_1.getParentTypeName)(className);
|
|
678
|
+
if (parentName) {
|
|
679
|
+
getterAnn = engine.annotationMap.get(`${parentName}#${methodName}`);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
if (getterAnn?.methodNameMismatch && getterAnn.name) {
|
|
684
|
+
engine.addDiag((0, annotations_1.methodNameMismatchMessage)(methodName, getterAnn.name), member.key, [{ message: `Annotation '${getterAnn.name}' defined here`, start: getterAnn.commentStart, end: getterAnn.commentEnd }]);
|
|
685
|
+
}
|
|
686
|
+
else if (getterAnn?.receiver && getterAnn?.methodType && getterAnn?.scheme) {
|
|
687
|
+
const { expectedType: expectedMethodType } = engine.coordinateInstantiate(getterAnn);
|
|
688
|
+
engine.unifyWithDiagnostic(returnType, expectedMethodType, member.key);
|
|
689
|
+
}
|
|
690
|
+
const resolvedReturn = engine.subst.apply(returnType);
|
|
691
|
+
engine.recordNodeType(member.key, resolvedReturn);
|
|
692
|
+
// Register getter as a property in the method registry
|
|
693
|
+
const resolvedInstance = engine.subst.apply(instanceType);
|
|
694
|
+
(0, methodRegistry_1.registerMethod)(className, methodName, {
|
|
695
|
+
signature: `${className}#${methodName} :: ${(0, types_1.prettyTypeToString)(resolvedInstance)} ~> ${(0, types_1.prettyTypeToString)(resolvedReturn)}`,
|
|
696
|
+
factory: (_typeArgs) => resolvedReturn,
|
|
697
|
+
});
|
|
698
|
+
const parentName = (0, dataTypeRegistry_1.getParentTypeName)(className);
|
|
699
|
+
if (parentName) {
|
|
700
|
+
(0, methodRegistry_1.registerMethod)(parentName, methodName, {
|
|
701
|
+
signature: `${parentName}#${methodName} :: ${(0, types_1.prettyTypeToString)(resolvedInstance)} ~> ${(0, types_1.prettyTypeToString)(resolvedReturn)}`,
|
|
702
|
+
factory: (_typeArgs) => resolvedReturn,
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
engine.methodSignatures.set(member.key.start, `${className}#${methodName} :: ${(0, types_1.prettyTypeToString)(resolvedInstance)} ~> ${(0, types_1.prettyTypeToString)(resolvedReturn)}`);
|
|
706
|
+
if (savedAnn) {
|
|
707
|
+
engine.annotationByOffset.set(member.key.start, savedAnn);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
/** Look up the method annotation for a class member, checking ClassName#method,
|
|
711
|
+
* offset-based lookup, sumtype parent type, and static annotations. */
|
|
712
|
+
function resolveMethodAnnotation(engine, member, className, methodName) {
|
|
713
|
+
let methodAnn = engine.annotationMap.get(`${className}#${methodName}`);
|
|
714
|
+
if (!methodAnn) {
|
|
715
|
+
const offsetAnn = engine.annotationByOffset.get(member.value?.start ?? member.start);
|
|
716
|
+
if (offsetAnn?.receiver) {
|
|
717
|
+
if (offsetAnn.methodNameMismatch) {
|
|
718
|
+
engine.addDiag((0, annotations_1.methodNameMismatchMessage)(methodName, offsetAnn.name ?? ''), member.key, [{ message: `Annotation '${offsetAnn.name}' defined here`, start: offsetAnn.commentStart, end: offsetAnn.commentEnd }]);
|
|
719
|
+
}
|
|
720
|
+
else {
|
|
721
|
+
const annName = offsetAnn.name;
|
|
722
|
+
if (annName && annName.includes('#')) {
|
|
723
|
+
if (annName.split('#')[1] === methodName)
|
|
724
|
+
methodAnn = offsetAnn;
|
|
725
|
+
}
|
|
726
|
+
else {
|
|
727
|
+
methodAnn = offsetAnn;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
if (!methodAnn) {
|
|
733
|
+
const parentName = (0, dataTypeRegistry_1.getParentTypeName)(className);
|
|
734
|
+
if (parentName) {
|
|
735
|
+
const parentAnn = engine.annotationMap.get(`${parentName}#${methodName}`);
|
|
736
|
+
if (parentAnn?.receiver && parentAnn?.methodType && parentAnn?.scheme && !parentAnn.methodNameMismatch) {
|
|
737
|
+
methodAnn = parentAnn;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
const isStatic = !!member.static;
|
|
742
|
+
if (!methodAnn && isStatic) {
|
|
743
|
+
const staticAnn = engine.annotationMap.get(`${className}.${methodName}`);
|
|
744
|
+
if (staticAnn?.scheme && !staticAnn.methodNameMismatch) {
|
|
745
|
+
methodAnn = staticAnn;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
return methodAnn;
|
|
749
|
+
}
|
|
750
|
+
/** Infer an unannotated class method (no type annotation). */
|
|
751
|
+
function inferUnannotatedClassMethod(engine, member, classEnv, instanceType, className, methodName, isStatic) {
|
|
752
|
+
let methodEnv = isStatic
|
|
753
|
+
? classEnv
|
|
754
|
+
: classEnv.extend('this', (0, types_1.mono)(instanceType));
|
|
755
|
+
if (!isStatic && (0, dataTypeRegistry_1.isVariant)(className)) {
|
|
756
|
+
methodEnv = methodEnv.extend('__sumtypeVariant', (0, types_1.mono)((0, types_1.tCon)(className, [])));
|
|
757
|
+
}
|
|
758
|
+
const inferredType = engine.inferFunctionExpression(member.value, methodEnv);
|
|
759
|
+
const methodType = inferredType.tag === 'TFun' ? inferredType : (0, types_1.tFun)(types_1.tVoid, inferredType);
|
|
760
|
+
engine.recordNodeType(member.key, methodType);
|
|
761
|
+
const resolvedMethod = engine.subst.apply(methodType);
|
|
762
|
+
if (isStatic) {
|
|
763
|
+
(0, methodRegistry_1.registerMethod)(className, methodName, {
|
|
764
|
+
signature: `${className}.${methodName} :: ${(0, types_1.prettyTypeToString)(resolvedMethod)}`,
|
|
765
|
+
factory: (_typeArgs) => resolvedMethod,
|
|
766
|
+
isStatic: true,
|
|
767
|
+
});
|
|
768
|
+
engine.methodSignatures.set(member.key.start, `${className}.${methodName} :: ${(0, types_1.prettyTypeToString)(resolvedMethod)}`);
|
|
769
|
+
}
|
|
770
|
+
else {
|
|
771
|
+
const resolvedInstance = engine.subst.apply(instanceType);
|
|
772
|
+
engine.methodSignatures.set(member.key.start, `${className}#${methodName} :: ${(0, types_1.prettyTypeToString)(resolvedInstance)} ~> ${(0, types_1.prettyTypeToString)(resolvedMethod)}`);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
//# sourceMappingURL=modules.js.map
|