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,620 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ============================================================================
|
|
3
|
+
// Assignment-expression inference and module.exports tracking
|
|
4
|
+
// (extracted from expressions.ts)
|
|
5
|
+
//
|
|
6
|
+
// Plain language: handles `x = value`, `obj.field = value` and the CommonJS
|
|
7
|
+
// export forms (`module.exports = …`, `exports.name = …`). The job is to
|
|
8
|
+
// check the assigned value against what the target is allowed to hold, and
|
|
9
|
+
// to keep the record of "what does this file export" up to date.
|
|
10
|
+
// ============================================================================
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.stripUndefinedFromUnion = stripUndefinedFromUnion;
|
|
13
|
+
exports.inferAssignmentExpression = inferAssignmentExpression;
|
|
14
|
+
exports.trackModuleExports = trackModuleExports;
|
|
15
|
+
const types_1 = require("../hm/types");
|
|
16
|
+
const env_1 = require("../hm/env");
|
|
17
|
+
const commentParser_1 = require("../commentParser");
|
|
18
|
+
const annotation_rigid_1 = require("./annotation-rigid");
|
|
19
|
+
const methodRegistry_1 = require("../registries/methodRegistry");
|
|
20
|
+
const dataTypeRegistry_1 = require("../registries/dataTypeRegistry");
|
|
21
|
+
const typeUtils_1 = require("../hm/typeUtils");
|
|
22
|
+
const annotations_1 = require("./annotations");
|
|
23
|
+
const expr_prototype_1 = require("./expr-prototype");
|
|
24
|
+
const expressions_1 = require("./expressions");
|
|
25
|
+
/**
|
|
26
|
+
* Detect a sumtype-factory member assignment such as
|
|
27
|
+
* `Param.Path = (...) => Object.create(proto)`.
|
|
28
|
+
*
|
|
29
|
+
* When the annotation's return type is a registered sumtype TCon and the
|
|
30
|
+
* inferred body produces a record / sumtype TCon / TVar, the annotation is
|
|
31
|
+
* the source of truth and rigid + unification mismatch checks must be
|
|
32
|
+
* skipped — exactly mirroring `shouldSuppressSumTypeCheck` in `statements.ts`.
|
|
33
|
+
*/
|
|
34
|
+
/**
|
|
35
|
+
* `this.<field> = <array/object literal>` inside a constructor whose field is
|
|
36
|
+
* annotated (`T#field :: T a b ~> [a, b]`): the annotated field type is the
|
|
37
|
+
* expected type of the literal, so an array literal becomes a tuple (as it
|
|
38
|
+
* does for an annotated `const` or `return`) instead of `[a | b]`.
|
|
39
|
+
*/
|
|
40
|
+
function annotatedThisFieldType(engine, node, env) {
|
|
41
|
+
const left = node.left;
|
|
42
|
+
if (left?.type !== 'MemberExpression' || left.computed)
|
|
43
|
+
return undefined;
|
|
44
|
+
if (left.object?.type !== 'ThisExpression' || left.property?.type !== 'Identifier')
|
|
45
|
+
return undefined;
|
|
46
|
+
if (node.right?.type !== 'ArrayExpression' && node.right?.type !== 'ObjectExpression')
|
|
47
|
+
return undefined;
|
|
48
|
+
const thisScheme = env.lookup('this');
|
|
49
|
+
if (!thisScheme)
|
|
50
|
+
return undefined;
|
|
51
|
+
const thisResolved = engine.subst.apply((0, env_1.instantiate)(thisScheme));
|
|
52
|
+
if (thisResolved.tag !== 'TCon')
|
|
53
|
+
return undefined;
|
|
54
|
+
const entry = (0, methodRegistry_1.lookupMethod)(thisResolved.name, left.property.name);
|
|
55
|
+
return entry ? entry.factory(thisResolved.args) : undefined;
|
|
56
|
+
}
|
|
57
|
+
function isSumTypeFactoryAssignment(scheme, rightType) {
|
|
58
|
+
const annReturn = (0, types_1.getReturnType)(scheme.type);
|
|
59
|
+
if (annReturn.tag !== 'TCon' || !(0, dataTypeRegistry_1.isDataType)(annReturn.name))
|
|
60
|
+
return false;
|
|
61
|
+
const inferredReturn = (0, types_1.getReturnType)(rightType);
|
|
62
|
+
return inferredReturn.tag === 'TVar'
|
|
63
|
+
|| inferredReturn.tag === 'TRecord'
|
|
64
|
+
|| (inferredReturn.tag === 'TCon' && (0, dataTypeRegistry_1.isDataType)(inferredReturn.name));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* If `t` is a Union containing `Undefined`, return the Union with
|
|
68
|
+
* `Undefined` removed (collapsing to a single member when only one
|
|
69
|
+
* remains). Returns the input unchanged otherwise.
|
|
70
|
+
*
|
|
71
|
+
* Used by object-spread merging: a field typed `T | Undefined` from a
|
|
72
|
+
* heterogeneous array element merge means "this key may be absent".
|
|
73
|
+
* When such a value is spread *over* a record that already has the key,
|
|
74
|
+
* the absent case leaves the prior value untouched, so the result type
|
|
75
|
+
* is `T` (not `T | Undefined`).
|
|
76
|
+
*/
|
|
77
|
+
function stripUndefinedFromUnion(t) {
|
|
78
|
+
if (t.tag !== 'TCon' || t.name !== 'Union')
|
|
79
|
+
return t;
|
|
80
|
+
const kept = t.args.filter(a => !(a.tag === 'TCon' && a.name === 'Undefined'));
|
|
81
|
+
if (kept.length === t.args.length)
|
|
82
|
+
return t;
|
|
83
|
+
if (kept.length === 1)
|
|
84
|
+
return kept[0];
|
|
85
|
+
if (kept.length === 0)
|
|
86
|
+
return t;
|
|
87
|
+
return (0, types_1.tUnion)(kept);
|
|
88
|
+
}
|
|
89
|
+
/** Build an expected function type that keeps the first `arity` parameter
|
|
90
|
+
* types of `annotated` but replaces the final result with a fresh variable.
|
|
91
|
+
* Used to bind an annotated arrow's parameters during body inference without
|
|
92
|
+
* performing the return-type unification inside the body (that check stays at
|
|
93
|
+
* the assignment node). */
|
|
94
|
+
function paramsOnlyExpected(annotated, arity) {
|
|
95
|
+
if (arity <= 0)
|
|
96
|
+
return (0, types_1.tVar)();
|
|
97
|
+
if (annotated.tag !== 'TFun')
|
|
98
|
+
return annotated;
|
|
99
|
+
return (0, types_1.tFun)(annotated.param, paramsOnlyExpected(annotated.result, arity - 1));
|
|
100
|
+
}
|
|
101
|
+
function inferAssignmentExpression(engine, node, env) {
|
|
102
|
+
// --- Prototype method: X.prototype.method = function() {...} ---
|
|
103
|
+
const protoResult = (0, expr_prototype_1.tryInferPrototypeMethodAssignment)(engine, node, env);
|
|
104
|
+
if (protoResult)
|
|
105
|
+
return protoResult;
|
|
106
|
+
// Unannotated `X.prototype.method = ...`: the user has NOT supplied a
|
|
107
|
+
// signature, so we deliberately do NOT couple the method to the type
|
|
108
|
+
// (no implicit prototype registration) — but we also must NOT emit
|
|
109
|
+
// "Property 'prototype' does not exist on type X". Infer the RHS so its
|
|
110
|
+
// body is still checked on its own, then return without touching the LHS
|
|
111
|
+
// member chain (which is what produced the spurious prototype diagnostic).
|
|
112
|
+
if (node.left?.type === 'MemberExpression' && !node.left.computed
|
|
113
|
+
&& (0, commentParser_1.extractPrototypeMethod)(node.left)) {
|
|
114
|
+
return (0, expressions_1.inferExpression)(engine, node.right, env);
|
|
115
|
+
}
|
|
116
|
+
// Pre-check for annotated member assignments (e.g. `Box.fromPair ::
|
|
117
|
+
// Pair a b -> Box b`, `EitherT.of :: Monad m => ...`) so we can guide body
|
|
118
|
+
// inference with the expected type. Pushing the annotated type binds an
|
|
119
|
+
// annotated arrow's PARAMETERS to their declared types, so field accesses in
|
|
120
|
+
// the body resolve from the annotation (`pair :: Pair a b` ⇒ `pair.fst :: a`)
|
|
121
|
+
// instead of relying on structural coercion — a mismatched return is still
|
|
122
|
+
// caught by the rigid check further below. It also enables constraint-based
|
|
123
|
+
// method lookup on parameters like `M` in `M.of(...)`.
|
|
124
|
+
let constrainedAnnotation;
|
|
125
|
+
if (node.left.type === 'MemberExpression' && !node.left.computed) {
|
|
126
|
+
const obj = node.left.object;
|
|
127
|
+
const prop = node.left.property;
|
|
128
|
+
if (obj?.type === 'Identifier' && prop?.type === 'Identifier') {
|
|
129
|
+
const dottedName = `${obj.name}.${prop.name}`;
|
|
130
|
+
const ann = engine.annotationMap.get(dottedName);
|
|
131
|
+
// Only honour file-local annotations: a sidecar / globals /
|
|
132
|
+
// `::types` `Foo.bar :: (Constraint c) => ...` entry must not
|
|
133
|
+
// silently inject constraints into an unrelated user assignment
|
|
134
|
+
// with the same dotted name.
|
|
135
|
+
//
|
|
136
|
+
// Method-form annotations (`Receiver ~> MethodType`) are intentionally
|
|
137
|
+
// excluded: passing an expected type here would bypass the inline
|
|
138
|
+
// method-`this` binding in `inferFunctionExpression` (which only fires
|
|
139
|
+
// when no expected type is set). Those go through `coordinateInstantiate`
|
|
140
|
+
// instead, which registers the same constraints AND binds `this` from
|
|
141
|
+
// the receiver. Without this exclusion a constrained method like
|
|
142
|
+
// `Alt f => Function [a] (f a) ~> ...` would leave `this` an unbound var.
|
|
143
|
+
if ((0, commentParser_1.isLocalAnnotation)(ann) && ann.scheme && !ann.receiver) {
|
|
144
|
+
const hasConstraints = !!(ann.scheme.constraints && ann.scheme.constraints.length > 0);
|
|
145
|
+
const rhsIsFn = node.right?.type === 'ArrowFunctionExpression'
|
|
146
|
+
|| node.right?.type === 'FunctionExpression';
|
|
147
|
+
if (hasConstraints) {
|
|
148
|
+
// Constrained: push the full annotated type (unchanged behaviour) so
|
|
149
|
+
// constraint-based method lookup on parameters works.
|
|
150
|
+
const annotatedType = engine.instantiateWithConstraints(ann.scheme, node, ann);
|
|
151
|
+
constrainedAnnotation = { annotatedType };
|
|
152
|
+
}
|
|
153
|
+
else if (rhsIsFn) {
|
|
154
|
+
// Unconstrained function: bind the arrow's PARAMETERS to their
|
|
155
|
+
// declared types but leave the RESULT free. This resolves field
|
|
156
|
+
// accesses in the body from the annotation (`pair :: Pair a b` ⇒
|
|
157
|
+
// `pair.fst :: a`) without performing the return-type unification
|
|
158
|
+
// inside the body — that check stays at the controlled rigid/unify
|
|
159
|
+
// site on the assignment node, which preserves its diagnostic
|
|
160
|
+
// location AND the unknown-type-constructor suppression.
|
|
161
|
+
const full = engine.instantiateWithConstraints(ann.scheme, node, ann);
|
|
162
|
+
const arity = node.right.params?.length ?? 0;
|
|
163
|
+
constrainedAnnotation = { annotatedType: paramsOnlyExpected(full, arity) };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// Method-form annotation on a property assignment whose RHS is a function
|
|
169
|
+
// expression (e.g. `builtIn.Array = function (other) { ... }` with
|
|
170
|
+
// `// builtIn.Array :: Array a ~> Array a -> Boolean`). The dotted-name
|
|
171
|
+
// annotation lookup above stores the annotation only by dotted name, but
|
|
172
|
+
// `inferFunctionExpression` binds `this` from the receiver only when the
|
|
173
|
+
// annotation is reachable by node offset. Stash it under the RHS offset
|
|
174
|
+
// so the existing inline-method-this binding path fires, giving every
|
|
175
|
+
// `this` in the body a consistent receiver type.
|
|
176
|
+
if (node.left.type === 'MemberExpression' && !node.left.computed) {
|
|
177
|
+
const obj = node.left.object;
|
|
178
|
+
const prop = node.left.property;
|
|
179
|
+
if (obj?.type === 'Identifier' && prop?.type === 'Identifier'
|
|
180
|
+
&& (node.right?.type === 'FunctionExpression' || node.right?.type === 'ArrowFunctionExpression')) {
|
|
181
|
+
const dottedName = `${obj.name}.${prop.name}`;
|
|
182
|
+
const ann = engine.annotationMap.get(dottedName);
|
|
183
|
+
if (ann?.receiver && ann?.methodType && ann?.scheme && (0, commentParser_1.isLocalAnnotation)(ann)
|
|
184
|
+
&& !engine.annotationByOffset.has(node.right.start)) {
|
|
185
|
+
engine.annotationByOffset.set(node.right.start, ann);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
// A plain polymorphic static (`// _Task.loop :: a -> [a]` above
|
|
190
|
+
// `_Task.loop = Task.loop = function (…) {…}`) checks its body against
|
|
191
|
+
// rigid skolems, like a `const` arrow or a `function` declaration, so
|
|
192
|
+
// nested annotations share its type variables (lexical scoping). The
|
|
193
|
+
// assignment below still validates the result.
|
|
194
|
+
const staticFn = staticAnnotatedFunction(engine, node);
|
|
195
|
+
const prevCheckedByBinding = engine.annotationCheckedByBinding;
|
|
196
|
+
if (staticFn) {
|
|
197
|
+
engine.annotationByOffset.set(staticFn.fn.start, staticFn.ann);
|
|
198
|
+
engine.annotationCheckedByBinding = staticFn.fn;
|
|
199
|
+
}
|
|
200
|
+
let rightType;
|
|
201
|
+
try {
|
|
202
|
+
if (constrainedAnnotation) {
|
|
203
|
+
engine.suppressInfiniteType = true;
|
|
204
|
+
try {
|
|
205
|
+
rightType = (0, expressions_1.inferExpressionWithExpected)(engine, node.right, env, constrainedAnnotation.annotatedType);
|
|
206
|
+
}
|
|
207
|
+
finally {
|
|
208
|
+
engine.suppressInfiniteType = false;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
const fieldType = annotatedThisFieldType(engine, node, env);
|
|
213
|
+
rightType = fieldType
|
|
214
|
+
? (0, expressions_1.inferExpressionWithExpected)(engine, node.right, env, fieldType)
|
|
215
|
+
: (0, expressions_1.inferExpression)(engine, node.right, env);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
finally {
|
|
219
|
+
if (staticFn) {
|
|
220
|
+
engine.annotationByOffset.delete(staticFn.fn.start);
|
|
221
|
+
engine.annotationCheckedByBinding = prevCheckedByBinding;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (node.left.type === 'MemberExpression' && !node.left.computed) {
|
|
225
|
+
// Handle annotated member assignments: Box.fromPair = pair => ...
|
|
226
|
+
const obj = node.left.object;
|
|
227
|
+
const prop = node.left.property;
|
|
228
|
+
if (obj?.type === 'Identifier' && prop?.type === 'Identifier') {
|
|
229
|
+
const dottedName = `${obj.name}.${prop.name}`;
|
|
230
|
+
// Track JS-body parameter defaults for member assignments — used by
|
|
231
|
+
// call-site arity checks so `Param.Path("k", [1,2])` is accepted
|
|
232
|
+
// when the implementation declares `(k, r, d = undefined)`.
|
|
233
|
+
if (node.right?.type === 'ArrowFunctionExpression' || node.right?.type === 'FunctionExpression') {
|
|
234
|
+
const rfn = node.right;
|
|
235
|
+
const slots = [];
|
|
236
|
+
for (let i = 0; i < (rfn.params?.length ?? 0); i++) {
|
|
237
|
+
if (rfn.params[i]?.type === 'AssignmentPattern')
|
|
238
|
+
slots.push(i);
|
|
239
|
+
}
|
|
240
|
+
if (slots.length > 0)
|
|
241
|
+
engine.optionalParamSlotsByDottedName.set(dottedName, slots);
|
|
242
|
+
}
|
|
243
|
+
const annotation = engine.annotationMap.get(dottedName);
|
|
244
|
+
// Honour only file-local annotations: a sidecar / globals /
|
|
245
|
+
// `::types` `Foo.bar :: T` entry must not silently define the
|
|
246
|
+
// type of an unrelated user assignment that happens to share the
|
|
247
|
+
// dotted name. The else branch below (no-annotation) handles
|
|
248
|
+
// both "no entry" and "non-local entry" the same way.
|
|
249
|
+
if (annotation?.scheme && (0, commentParser_1.isLocalAnnotation)(annotation)) {
|
|
250
|
+
// Chained static assignment: `_Task.ask = Task.ask = fn` annotates
|
|
251
|
+
// only the OUTER dotted name. The inner unannotated target was
|
|
252
|
+
// already registered (below, on a previous visit of the inner
|
|
253
|
+
// AssignmentExpression) with the inferred implementation type, so
|
|
254
|
+
// call sites resolving the static through the inner constructor
|
|
255
|
+
// name — which is how a module's default export resolves statics —
|
|
256
|
+
// would miss annotation-only information such as overload unions.
|
|
257
|
+
// Propagate the annotation to every chained inner dotted target.
|
|
258
|
+
let chained = node.right;
|
|
259
|
+
while (chained?.type === 'AssignmentExpression') {
|
|
260
|
+
const innerLeft = chained.left;
|
|
261
|
+
if (innerLeft?.type === 'MemberExpression' && !innerLeft.computed
|
|
262
|
+
&& innerLeft.object?.type === 'Identifier'
|
|
263
|
+
&& innerLeft.property?.type === 'Identifier') {
|
|
264
|
+
const innerDotted = `${innerLeft.object.name}.${innerLeft.property.name}`;
|
|
265
|
+
if (innerDotted !== dottedName
|
|
266
|
+
&& !engine.annotationMap.get(innerDotted)?.scheme) {
|
|
267
|
+
(0, annotations_1.registerAnnotationMethod)(innerDotted, annotation);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
chained = chained.right;
|
|
271
|
+
}
|
|
272
|
+
// Extend the LHS record with the new field so subsequent reads
|
|
273
|
+
// (e.g. `Param.Query("...")` after `Param.Query = ...`) resolve
|
|
274
|
+
// through the normal record-field path instead of emitting
|
|
275
|
+
// "Property 'X' does not exist on type ...". We store the
|
|
276
|
+
// *annotated* type (instantiated) rather than the inferred RHS
|
|
277
|
+
// type so that downstream reads see e.g. `(String, [a], b) -> Param`
|
|
278
|
+
// instead of the structural record produced by the body.
|
|
279
|
+
// The annotation is the source of truth.
|
|
280
|
+
{
|
|
281
|
+
const sch = env.lookup(obj.name);
|
|
282
|
+
if (sch && sch.type.tag === 'TRecord' && !(prop.name in sch.type.fields)) {
|
|
283
|
+
const annotatedFieldType = engine.instantiateWithConstraints(annotation.scheme, node, annotation);
|
|
284
|
+
sch.type.fields[prop.name] = annotatedFieldType;
|
|
285
|
+
// Also stash the SCHEME on the record so reads through ANY
|
|
286
|
+
// alias of this record (not just the original `obj.name`)
|
|
287
|
+
// re-instantiate freshly per use site. Without this,
|
|
288
|
+
// `const Aliased = Param; Aliased.Query(...)` would fall
|
|
289
|
+
// through to `fields[prop.name]` (the already-instantiated
|
|
290
|
+
// type) and bypass call-site argument unification + constraint
|
|
291
|
+
// validation.
|
|
292
|
+
if (!sch.type.fieldSchemes)
|
|
293
|
+
sch.type.fieldSchemes = {};
|
|
294
|
+
// Enrich the stored scheme with optionalParamSlots when the
|
|
295
|
+
// RHS is a function expression with JS body defaults
|
|
296
|
+
// (`(x = …)`), so call sites can treat trailing missing args
|
|
297
|
+
// as optional via member-access too.
|
|
298
|
+
let storedScheme = annotation.scheme;
|
|
299
|
+
if (node.right?.type === 'ArrowFunctionExpression' || node.right?.type === 'FunctionExpression') {
|
|
300
|
+
const rfn = node.right;
|
|
301
|
+
const slots = [];
|
|
302
|
+
for (let i = 0; i < (rfn.params?.length ?? 0); i++) {
|
|
303
|
+
if (rfn.params[i]?.type === 'AssignmentPattern')
|
|
304
|
+
slots.push(i);
|
|
305
|
+
}
|
|
306
|
+
if (slots.length > 0)
|
|
307
|
+
storedScheme = { ...storedScheme, optionalParamSlots: slots };
|
|
308
|
+
}
|
|
309
|
+
sch.type.fieldSchemes[prop.name] = storedScheme;
|
|
310
|
+
// For method-form annotations (`Receiver ~> MethodType`), the
|
|
311
|
+
// stored field type is only the MethodType — pretty-printing
|
|
312
|
+
// the host record would lose the `Receiver ~>` prefix.
|
|
313
|
+
// Stash the original raw signature so hover renders the full
|
|
314
|
+
// `Array a ~> (a -> Array b) -> Array b` form for the field.
|
|
315
|
+
if (annotation.receiver && annotation.raw) {
|
|
316
|
+
if (!sch.type.fieldDisplays)
|
|
317
|
+
sch.type.fieldDisplays = {};
|
|
318
|
+
sch.type.fieldDisplays[prop.name] = annotation.raw;
|
|
319
|
+
}
|
|
320
|
+
// Fase B3: a `::hidden` / `::private` member annotation must also
|
|
321
|
+
// hide the corresponding field on the host record, so hover and
|
|
322
|
+
// completion over the host (e.g. `builtIn`) do not surface it.
|
|
323
|
+
if (annotation.hidden || annotation.private) {
|
|
324
|
+
if (!sch.type.hiddenFields)
|
|
325
|
+
sch.type.hiddenFields = new Set();
|
|
326
|
+
sch.type.hiddenFields.add(prop.name);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
// Skip mismatch checks when annotation has unknown type constructors —
|
|
331
|
+
// the "unknown type constructor" diagnostic is the root cause.
|
|
332
|
+
if ((0, annotations_1.hasUnknownTConDiagForScheme)(engine.diagnostics, annotation.scheme)) {
|
|
333
|
+
// Don't checkRigid or unify — the unknown type makes it meaningless.
|
|
334
|
+
}
|
|
335
|
+
else if (isSumTypeFactoryAssignment(annotation.scheme, rightType)) {
|
|
336
|
+
// Sumtype factory assigned to a member (e.g. `Param.Path = (...) => Object.create(proto)`).
|
|
337
|
+
// The annotation is the source of truth; the body returns a record that
|
|
338
|
+
// structurally represents the sumtype value. Skip rigid/unify mismatch.
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
const hasConstraints = !!(annotation.scheme.constraints && annotation.scheme.constraints.length > 0);
|
|
342
|
+
const diagsBefore = engine.diagnostics.length;
|
|
343
|
+
if (!hasConstraints) {
|
|
344
|
+
// Non-constrained: normal flow
|
|
345
|
+
engine.checkRigid(rightType, annotation.scheme, node);
|
|
346
|
+
if (engine.diagnostics.length === diagsBefore) {
|
|
347
|
+
const annotatedType = engine.instantiateWithConstraints(annotation.scheme, node, annotation);
|
|
348
|
+
engine.unifyWithDiagnostic(rightType, annotatedType, node);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
// Constrained: already instantiated and inferred with expected type;
|
|
353
|
+
// just unify and suppress infinite type during the unification too.
|
|
354
|
+
engine.checkRigid(rightType, annotation.scheme, node);
|
|
355
|
+
if (engine.diagnostics.length === diagsBefore) {
|
|
356
|
+
engine.suppressInfiniteType = true;
|
|
357
|
+
try {
|
|
358
|
+
// `constrainedAnnotation` is only populated for constrained
|
|
359
|
+
// annotations WITHOUT a receiver (method-form `Receiver ~> …`
|
|
360
|
+
// annotations are handled by the inline-`this` binding path and
|
|
361
|
+
// intentionally leave it unset — see the guard above). Fall
|
|
362
|
+
// back to a fresh instantiation so a constrained *method* such as
|
|
363
|
+
// `Alt f => Function [a] (f a) ~> …` still unifies its body
|
|
364
|
+
// against the annotated type instead of dereferencing undefined.
|
|
365
|
+
const annotatedType = constrainedAnnotation
|
|
366
|
+
? constrainedAnnotation.annotatedType
|
|
367
|
+
: engine.instantiateWithConstraints(annotation.scheme, node, annotation);
|
|
368
|
+
engine.unifyWithDiagnostic(rightType, annotatedType, node);
|
|
369
|
+
}
|
|
370
|
+
finally {
|
|
371
|
+
engine.suppressInfiniteType = false;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
// Register constraint info for body parameter TVars so that
|
|
375
|
+
// constraint-based method lookup works on parameters (e.g. M.of()
|
|
376
|
+
// inside `EitherT.of = function(M, value) { ... }`).
|
|
377
|
+
if (annotation.scheme.constraints) {
|
|
378
|
+
engine.registerStaticMethodConstraints(annotation.scheme, rightType);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
else if ((!annotation || !(0, commentParser_1.isLocalAnnotation)(annotation))
|
|
384
|
+
&& obj.name !== 'module' && obj.name !== 'exports'
|
|
385
|
+
&& (node.right.type === 'FunctionExpression' || node.right.type === 'ArrowFunctionExpression'
|
|
386
|
+
|| (() => {
|
|
387
|
+
// Unannotated `Obj.prop = <identifier/expr>` whose RHS resolves to
|
|
388
|
+
// a function type (e.g. `Response.Redirect = _Redirect;` where
|
|
389
|
+
// `_Redirect :: Number -> String -> Response a`). Treat it as a
|
|
390
|
+
// static factory so hover + cross-file export mirror the
|
|
391
|
+
// function-expression case below.
|
|
392
|
+
const rt = engine.subst.apply(rightType);
|
|
393
|
+
return rt.tag === 'TFun' || (0, types_1.isAppliedFunction)(rt);
|
|
394
|
+
})())) {
|
|
395
|
+
// Unannotated static method assignment: X.y = function(...) { ... }
|
|
396
|
+
// Register as a static method and record method signature for hover.
|
|
397
|
+
const resolvedType = engine.subst.apply(rightType);
|
|
398
|
+
(0, methodRegistry_1.registerMethod)(obj.name, prop.name, {
|
|
399
|
+
signature: `${dottedName} :: ${(0, types_1.prettyTypeToString)(resolvedType)}`,
|
|
400
|
+
factory: (_typeArgs) => resolvedType,
|
|
401
|
+
isStatic: true,
|
|
402
|
+
});
|
|
403
|
+
engine.recordNodeType(prop, resolvedType);
|
|
404
|
+
engine.methodSignatures.set(prop.start, `${dottedName} :: ${(0, types_1.prettyTypeToString)(resolvedType)}`);
|
|
405
|
+
// Record for the cross-file export map so a default export's static
|
|
406
|
+
// factories are re-exported even without an explicit annotation.
|
|
407
|
+
let members = engine.staticMemberTypes.get(obj.name);
|
|
408
|
+
if (!members) {
|
|
409
|
+
members = new Map();
|
|
410
|
+
engine.staticMemberTypes.set(obj.name, members);
|
|
411
|
+
}
|
|
412
|
+
if (!members.has(prop.name))
|
|
413
|
+
members.set(prop.name, resolvedType);
|
|
414
|
+
}
|
|
415
|
+
// Track CommonJS exports
|
|
416
|
+
if (obj.name === 'module' && prop.name === 'exports') {
|
|
417
|
+
// module.exports = { Box, Pair } or module.exports = expr
|
|
418
|
+
engine.usesCjsExports = true;
|
|
419
|
+
trackModuleExports(engine, node.right, env, rightType);
|
|
420
|
+
}
|
|
421
|
+
else if (obj.name === 'exports') {
|
|
422
|
+
engine.usesCjsExports = true;
|
|
423
|
+
// exports.Box = Box
|
|
424
|
+
engine.exportedNames.add(prop.name);
|
|
425
|
+
// Capture the RHS type so re-exports like `exports.Limit = Limit_1.default;`
|
|
426
|
+
// (where `Limit` is not bound in env) still surface a typed export.
|
|
427
|
+
engine.cjsExportTypes.set(prop.name, rightType);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
// Handle `module.exports.X = expr` (LHS object is itself a MemberExpression)
|
|
431
|
+
if (obj?.type === 'MemberExpression'
|
|
432
|
+
&& !obj.computed
|
|
433
|
+
&& obj.object?.type === 'Identifier' && obj.object.name === 'module'
|
|
434
|
+
&& obj.property?.type === 'Identifier' && obj.property.name === 'exports'
|
|
435
|
+
&& prop?.type === 'Identifier') {
|
|
436
|
+
engine.usesCjsExports = true;
|
|
437
|
+
engine.exportedNames.add(prop.name);
|
|
438
|
+
engine.cjsExportTypes.set(prop.name, rightType);
|
|
439
|
+
}
|
|
440
|
+
// Handle this.<prop> = ... inside constructors: record method signature
|
|
441
|
+
// so hover on the property shows Type#prop :: Type ~> PropType.
|
|
442
|
+
if (obj?.type === 'ThisExpression' && prop?.type === 'Identifier') {
|
|
443
|
+
const thisScheme = env.lookup('this');
|
|
444
|
+
if (thisScheme) {
|
|
445
|
+
const thisResolved = engine.subst.apply((0, env_1.instantiate)(thisScheme));
|
|
446
|
+
if (thisResolved.tag === 'TCon') {
|
|
447
|
+
// Track this property as assigned in the constructor
|
|
448
|
+
engine.constructorProperties.add(`${thisResolved.name}#${prop.name}`);
|
|
449
|
+
const entry = (0, methodRegistry_1.lookupMethod)(thisResolved.name, prop.name);
|
|
450
|
+
if (entry) {
|
|
451
|
+
const propType = entry.factory(thisResolved.args);
|
|
452
|
+
engine.recordNodeType(prop, propType);
|
|
453
|
+
engine.methodSignatures.set(prop.start, `${thisResolved.name}#${prop.name} :: ${(0, types_1.prettyTypeToString)(thisResolved)} ~> ${(0, types_1.prettyTypeToString)(propType)}`);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
// Generic member assignment unification (e.g. `record.field = value`,
|
|
459
|
+
// `this.#priv = value`). The annotated static-method path above already
|
|
460
|
+
// handles `Type.method = ...` assignments via `unifyWithDiagnostic`
|
|
461
|
+
// against the annotation, and `this.<prop> = ...` constructor-style
|
|
462
|
+
// assignments are typically against fresh TVars (so unification is a
|
|
463
|
+
// no-op there). For everything else we infer the LHS and unify it with
|
|
464
|
+
// the RHS so concrete field types are enforced. Logical assignment
|
|
465
|
+
// operators (`||=`, `&&=`, `??=`) require RHS to be assignable to LHS
|
|
466
|
+
// *unless* the LHS resolves to `Null`, in which case the operator is
|
|
467
|
+
// being used as a nullish-coalescing initialiser and the RHS may legally
|
|
468
|
+
// have an unrelated type (e.g. `let label = null; label ??= "fallback"`).
|
|
469
|
+
const op = node.operator;
|
|
470
|
+
const handledAsAnnotatedStatic = obj?.type === 'Identifier' && prop?.type === 'Identifier'
|
|
471
|
+
&& (() => {
|
|
472
|
+
const a = engine.annotationMap.get(`${obj.name}.${prop.name}`);
|
|
473
|
+
return !!a?.scheme && (0, commentParser_1.isLocalAnnotation)(a);
|
|
474
|
+
})();
|
|
475
|
+
// CommonJS sentinel writes are tracked separately and don't represent
|
|
476
|
+
// a typed slot to unify against.
|
|
477
|
+
const isCommonJsExport = (obj?.type === 'Identifier' && prop?.type === 'Identifier'
|
|
478
|
+
&& (obj.name === 'module' || obj.name === 'exports'))
|
|
479
|
+
|| (obj?.type === 'MemberExpression' && !obj.computed
|
|
480
|
+
&& obj.object?.type === 'Identifier' && obj.object.name === 'module'
|
|
481
|
+
&& obj.property?.type === 'Identifier' && obj.property.name === 'exports');
|
|
482
|
+
if (!handledAsAnnotatedStatic && !isCommonJsExport) {
|
|
483
|
+
// Allow extending records via property assignment without diagnostics:
|
|
484
|
+
// when the LHS is a non-computed member expression on a record value
|
|
485
|
+
// and the property doesn't exist yet, silently add it instead of
|
|
486
|
+
// emitting "Property 'x' does not exist on type ...". This makes
|
|
487
|
+
// common JS patterns (`const Param = {}; Param.Path = ...;` or
|
|
488
|
+
// `const x = Object.create(proto); x.key = key;`) work cleanly and
|
|
489
|
+
// surfaces the new field on subsequent intellisense lookups.
|
|
490
|
+
if (prop?.type === 'Identifier'
|
|
491
|
+
&& obj?.type !== 'ThisExpression') {
|
|
492
|
+
const propName = prop.name;
|
|
493
|
+
const objType = (0, expressions_1.inferExpression)(engine, obj, env);
|
|
494
|
+
const resolvedObj = engine.subst.apply(objType);
|
|
495
|
+
if (resolvedObj.tag === 'TRecord' && !(propName in resolvedObj.fields)) {
|
|
496
|
+
const envSch = obj.type === 'Identifier' ? env.lookup(obj.name) : undefined;
|
|
497
|
+
const envRecord = envSch && envSch.type.tag === 'TRecord' ? envSch.type : undefined;
|
|
498
|
+
// Break object-reference cycles before storing the field: in the
|
|
499
|
+
// tagged-constructor pattern (`Ns.Make = (...) => Object.create(proto)`
|
|
500
|
+
// with `proto.constructor` pointing back at `Ns`) the RHS type
|
|
501
|
+
// contains the very record we are about to extend. Storing it
|
|
502
|
+
// verbatim would turn the type graph into a loop and every
|
|
503
|
+
// recursive type walker (substitution, free-var collection,
|
|
504
|
+
// printing) would recurse forever. Replace back-references with a
|
|
505
|
+
// fresh type variable so the stored field type stays finite.
|
|
506
|
+
const isBackRef = (n) => n === resolvedObj || (envRecord !== undefined && n === envRecord);
|
|
507
|
+
const safeRight = (0, typeUtils_1.someType)(rightType, isBackRef)
|
|
508
|
+
? (0, typeUtils_1.mapType)(rightType, n => (isBackRef(n) ? (0, types_1.tVar)() : undefined))
|
|
509
|
+
: rightType;
|
|
510
|
+
// Mutate the resolved copy so this assignment node sees the field.
|
|
511
|
+
resolvedObj.fields[propName] = safeRight;
|
|
512
|
+
// Walk Identifier chains (Param, this.x.y is excluded) to mutate
|
|
513
|
+
// the env-stored original record. subst.apply rebuilds closed
|
|
514
|
+
// records, so the env-bound TRecord is the durable one — adding
|
|
515
|
+
// to its `fields` map makes the property visible on later reads.
|
|
516
|
+
if (envRecord) {
|
|
517
|
+
envRecord.fields[propName] = safeRight;
|
|
518
|
+
}
|
|
519
|
+
engine.recordNodeType(prop, safeRight);
|
|
520
|
+
engine.recordNodeType(node.left, safeRight);
|
|
521
|
+
return rightType;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
const leftType = (0, expressions_1.inferExpression)(engine, node.left, env);
|
|
525
|
+
const isLogicalAssign = op === '||=' || op === '&&=' || op === '??=';
|
|
526
|
+
const lhsResolved = engine.subst.apply(leftType);
|
|
527
|
+
const lhsIsNull = lhsResolved.tag === 'TCon' && lhsResolved.name === 'Null';
|
|
528
|
+
if (!isLogicalAssign || !lhsIsNull) {
|
|
529
|
+
engine.unifyWithDiagnostic(leftType, rightType, node);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
else if (node.left.type === 'Identifier') {
|
|
534
|
+
const leftType = (0, expressions_1.inferExpression)(engine, node.left, env);
|
|
535
|
+
// Logical assignments (`||=`, `&&=`, `??=`) require the RHS to be
|
|
536
|
+
// assignable to the LHS slot, except when the LHS resolves to `Null` —
|
|
537
|
+
// that's the nullish-coalescing initialiser pattern and the fallback
|
|
538
|
+
// may legally have a different type (e.g. `let label = null; label ??= "x"`).
|
|
539
|
+
const op = node.operator;
|
|
540
|
+
const isLogicalAssign = op === '||=' || op === '&&=' || op === '??=';
|
|
541
|
+
const lhsResolved = engine.subst.apply(leftType);
|
|
542
|
+
const lhsIsNull = lhsResolved.tag === 'TCon' && lhsResolved.name === 'Null';
|
|
543
|
+
if (!isLogicalAssign || !lhsIsNull) {
|
|
544
|
+
engine.unifyWithDiagnostic(leftType, rightType, node);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
else if (node.left.type === 'ObjectPattern' || node.left.type === 'ArrayPattern') {
|
|
548
|
+
// Destructuring assignment: ({ a, b } = obj) or ([a, b] = arr)
|
|
549
|
+
engine.inferDestructuringPattern(node.left, rightType, env, 'let');
|
|
550
|
+
}
|
|
551
|
+
return rightType;
|
|
552
|
+
}
|
|
553
|
+
/** Track names from `module.exports = { ... }` or `module.exports = expr`. */
|
|
554
|
+
function trackModuleExports(engine, node, env, rightType) {
|
|
555
|
+
if (node.type === 'ObjectExpression') {
|
|
556
|
+
// module.exports = { Box, Pair, add } or a re-export barrel
|
|
557
|
+
// module.exports = { RTask: require ("./RTask"), of: _RTask.of }
|
|
558
|
+
const recordType = rightType ? engine.subst.apply(rightType) : undefined;
|
|
559
|
+
for (const prop of node.properties) {
|
|
560
|
+
if (prop.type === 'Property') {
|
|
561
|
+
const keyName = prop.key?.type === 'Identifier' ? prop.key.name :
|
|
562
|
+
prop.key?.type === 'Literal' ? String(prop.key.value) : null;
|
|
563
|
+
if (keyName) {
|
|
564
|
+
engine.exportedNames.add(keyName);
|
|
565
|
+
// Value is not the same-named identifier (`{ Box }` / `{ Box: Box }`):
|
|
566
|
+
// the export's type is the property value's inferred type.
|
|
567
|
+
const sameNamedIdent = prop.value?.type === 'Identifier' && prop.value.name === keyName;
|
|
568
|
+
if (!sameNamedIdent && recordType?.tag === 'TRecord' && recordType.fields[keyName]) {
|
|
569
|
+
engine.cjsExportTypes.set(keyName, recordType.fields[keyName]);
|
|
570
|
+
engine.cjsValueExports.add(keyName);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
if (rightType)
|
|
576
|
+
engine.cjsObjectExportType = rightType;
|
|
577
|
+
}
|
|
578
|
+
else if (node.type === 'Identifier') {
|
|
579
|
+
// module.exports = Box — export as 'default'
|
|
580
|
+
engine.defaultExportName = node.name;
|
|
581
|
+
engine.exportedNames.add('default');
|
|
582
|
+
// Also export with the identifier's name
|
|
583
|
+
engine.exportedNames.add(node.name);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* The innermost function of an annotated dotted static assignment
|
|
588
|
+
* (`A.m = B.m = function …`) whose plain, unconstrained, polymorphic
|
|
589
|
+
* annotation should check the body against rigid skolems. Method-form
|
|
590
|
+
* (`~>`), constrained, overloaded and variadic annotations keep their own
|
|
591
|
+
* paths.
|
|
592
|
+
*/
|
|
593
|
+
function staticAnnotatedFunction(engine, node) {
|
|
594
|
+
if (node.left.type !== 'MemberExpression' || node.left.computed)
|
|
595
|
+
return undefined;
|
|
596
|
+
const obj = node.left.object;
|
|
597
|
+
const prop = node.left.property;
|
|
598
|
+
if (obj?.type !== 'Identifier' || prop?.type !== 'Identifier')
|
|
599
|
+
return undefined;
|
|
600
|
+
const ann = engine.annotationMap.get(`${obj.name}.${prop.name}`);
|
|
601
|
+
if (!ann?.scheme || ann.receiver || !(0, commentParser_1.isLocalAnnotation)(ann))
|
|
602
|
+
return undefined;
|
|
603
|
+
if (ann.scheme.vars.length === 0)
|
|
604
|
+
return undefined;
|
|
605
|
+
if (ann.scheme.constraints && ann.scheme.constraints.length > 0)
|
|
606
|
+
return undefined;
|
|
607
|
+
if ((0, annotation_rigid_1.usesLenientArrowCheck)(ann.scheme))
|
|
608
|
+
return undefined;
|
|
609
|
+
if ((0, annotations_1.hasUnknownTConDiagForScheme)(engine.diagnostics, ann.scheme))
|
|
610
|
+
return undefined;
|
|
611
|
+
let fn = node.right;
|
|
612
|
+
while (fn?.type === 'AssignmentExpression')
|
|
613
|
+
fn = fn.right;
|
|
614
|
+
if (!fn || (fn.type !== 'FunctionExpression' && fn.type !== 'ArrowFunctionExpression'))
|
|
615
|
+
return undefined;
|
|
616
|
+
if (engine.annotationByOffset.has(fn.start))
|
|
617
|
+
return undefined;
|
|
618
|
+
return { fn, ann };
|
|
619
|
+
}
|
|
620
|
+
//# sourceMappingURL=expr-assignment.js.map
|