@slim-lang/core 1.2.12 → 1.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +522 -14
- package/package.json +2 -6
- package/packages/slim/server/main.slim +433 -422
- package/run-dev-slim.js +10 -5
- package/run-slim.js +10 -20
- package/src/bin/api/github_get.js +7 -12
- package/src/bin/api/github_req.js +54 -241
- package/src/bin/api/lock.js +2 -1
- package/src/bin/api/spm.js +20 -91
- package/src/bin/cli.js +73 -102
- package/src/bin/config.default.json +11 -11
- package/src/bin/helpers.js +2 -7
- package/src/bin/spm.js +3 -7
- package/src/checker.js +1705 -388
- package/src/compile.js +131 -100
- package/src/external/classErrors.js +5 -6
- package/src/external/client.js +0 -2
- package/src/external/core.js +355 -237
- package/src/external/defaults.js +12 -4
- package/src/external/dom.js +663 -0
- package/src/external/helpers.js +87 -58
- package/src/external/reactive.js +601 -0
- package/src/external/router.js +117 -0
- package/src/external/slim-globals.d.ts +43 -4
- package/src/external/typeSyntax.js +258 -0
- package/src/external/types.js +1 -1
- package/src/format.js +183 -12
- package/src/handlers/errorHandler.js +9 -36
- package/src/jsdoc.js +36 -16
- package/src/modulePaths.js +15 -5
- package/src/reactivity.js +774 -0
- package/src/repl.js +1 -1
- package/src/suggest.js +60 -0
- package/src/syntax/babel.js +46 -0
- package/src/syntax/index.js +39 -0
- package/src/syntax/lower.js +666 -0
- package/src/syntax/markup.js +195 -0
- package/src/syntax/plugin.js +903 -0
- package/src/syntax/types.js +106 -0
- package/src/test-runner.js +5 -5
- package/src/transform.js +209 -244
- package/packages/slim/helpers/string.js +0 -43
- package/packages/slim/types/hash.js +0 -11
- package/src/handlers/parser/components.js +0 -316
- package/src/handlers/parserHandler.js +0 -789
- package/src/lexer.js +0 -174
- package/src/parser.js +0 -803
- package/src/sourcemap.js +0 -0
package/src/checker.js
CHANGED
|
@@ -1,88 +1,147 @@
|
|
|
1
1
|
import _traverse from "@babel/traverse"
|
|
2
2
|
import * as t from "@babel/types"
|
|
3
3
|
import path from "node:path"
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
ANY, named, literal, arrayOf, tupleOf, genericOf, enumMember, unionOf, functionOf,
|
|
6
|
+
readType, formatType, substituteType
|
|
7
|
+
} from "./external/typeSyntax.js"
|
|
8
|
+
import { closest, didYouMean, isKnownGlobal } from "./suggest.js"
|
|
9
|
+
import { parseMarkup, holeMark } from "./syntax/markup.js"
|
|
5
10
|
|
|
6
11
|
const traverse = _traverse.default ?? _traverse
|
|
7
12
|
|
|
13
|
+
function computeLineStarts(text) {
|
|
14
|
+
const starts = [0]
|
|
15
|
+
for (let i = 0; i < text.length; i++) {
|
|
16
|
+
if (text[i] === "\n") starts.push(i + 1)
|
|
17
|
+
}
|
|
18
|
+
return starts
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function offsetToLineCol(offset, lineStarts) {
|
|
22
|
+
let lo = 0
|
|
23
|
+
let hi = lineStarts.length - 1
|
|
24
|
+
while (lo < hi) {
|
|
25
|
+
const mid = (lo + hi + 1) >> 1
|
|
26
|
+
if (lineStarts[mid] <= offset) lo = mid
|
|
27
|
+
else hi = mid - 1
|
|
28
|
+
}
|
|
29
|
+
return { line: lo, column: offset - lineStarts[lo] }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const INT = named("int")
|
|
33
|
+
const FLOAT = named("float")
|
|
34
|
+
const NUMBER = named("number")
|
|
35
|
+
const STRING = named("string")
|
|
36
|
+
const BOOL = named("bool")
|
|
37
|
+
const NULL = named("null")
|
|
38
|
+
const UNDEFINED = named("undefined")
|
|
39
|
+
const OBJECT = named("object")
|
|
40
|
+
const ARRAY = named("array")
|
|
41
|
+
const FUNCTION = named("function")
|
|
42
|
+
|
|
8
43
|
const NUMERIC = new Set(["int", "float", "number"])
|
|
9
44
|
const KNOWN = new Set([
|
|
10
45
|
"int", "float", "number", "string", "bool", "null", "undefined",
|
|
11
|
-
"object", "array", "function", "any", "element"
|
|
46
|
+
"object", "array", "function", "any", "element", "bigint", "symbol"
|
|
12
47
|
])
|
|
48
|
+
const BUILTIN_CLASSES = new Set(["Map", "Set", "Date", "Promise", "RegExp", "Error"])
|
|
13
49
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
let current = ""
|
|
50
|
+
const isNamed = (type, name) => type?.kind === "named" && type.name === name
|
|
51
|
+
const isNumeric = type => type?.kind === "named" && NUMERIC.has(type.name)
|
|
52
|
+
const isNullish = type => isNamed(type, "null") || isNamed(type, "undefined")
|
|
18
53
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
current += char
|
|
24
|
-
}
|
|
25
|
-
if (current.trim()) parts.push(current.trim())
|
|
26
|
-
return parts
|
|
54
|
+
function literalBase(type) {
|
|
55
|
+
if (typeof type.value === "string") return STRING
|
|
56
|
+
if (typeof type.value === "boolean") return BOOL
|
|
57
|
+
return Number.isInteger(type.value) ? INT : FLOAT
|
|
27
58
|
}
|
|
28
59
|
|
|
29
|
-
function
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
60
|
+
function widen(type) {
|
|
61
|
+
if (!type) return type
|
|
62
|
+
switch (type.kind) {
|
|
63
|
+
case "literal": return literalBase(type)
|
|
64
|
+
case "union": return unionOf(type.types.map(widen))
|
|
65
|
+
case "array": return arrayOf(widen(type.element))
|
|
66
|
+
case "tuple": return tupleOf(type.items.map(widen))
|
|
67
|
+
}
|
|
68
|
+
return type
|
|
33
69
|
}
|
|
34
70
|
|
|
35
|
-
function
|
|
36
|
-
|
|
71
|
+
function describe(type) {
|
|
72
|
+
const text = formatType(type)
|
|
73
|
+
return (type.kind === "union" || type.kind === "intersection") && !text.endsWith("?") ? `(${text})` : text
|
|
37
74
|
}
|
|
38
75
|
|
|
39
|
-
function
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return inner
|
|
76
|
+
function propertyKey(property) {
|
|
77
|
+
if (t.isStringLiteral(property.key)) return property.key.value
|
|
78
|
+
if (t.isIdentifier(property.key) && !property.computed) return property.key.name
|
|
79
|
+
return null
|
|
44
80
|
}
|
|
45
81
|
|
|
46
|
-
|
|
47
|
-
|
|
82
|
+
|
|
83
|
+
export function createEnvironment() {
|
|
84
|
+
return {
|
|
85
|
+
structs: new Map(),
|
|
86
|
+
enums: new Map(),
|
|
87
|
+
customTypes: new Set(),
|
|
88
|
+
aliases: new Map(),
|
|
89
|
+
functions: new Map(),
|
|
90
|
+
bindings: new Map(),
|
|
91
|
+
classes: new Set(),
|
|
92
|
+
methodOwners: new Map(),
|
|
93
|
+
returns: new Map(),
|
|
94
|
+
functionsByName: new Map(),
|
|
95
|
+
importedFunctions: new Map(),
|
|
96
|
+
importedLocals: new Set(),
|
|
97
|
+
openGlobals: false,
|
|
98
|
+
functionPaths: new Map(),
|
|
99
|
+
initTypes: new Map(),
|
|
100
|
+
refinedTypes: new Map(),
|
|
101
|
+
inferredReturns: new Map(),
|
|
102
|
+
inferring: new Set(),
|
|
103
|
+
facts: []
|
|
104
|
+
}
|
|
48
105
|
}
|
|
49
106
|
|
|
50
|
-
|
|
51
|
-
const parts = []
|
|
52
|
-
let depth = 0
|
|
53
|
-
let current = ""
|
|
107
|
+
const moduleTypes = new Map()
|
|
54
108
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (current.trim()) parts.push(current.trim())
|
|
62
|
-
return parts
|
|
109
|
+
function encode(value) {
|
|
110
|
+
if (value instanceof Map) return { $map: [...value].map(([key, item]) => [key, encode(item)]) }
|
|
111
|
+
if (value instanceof Set) return { $set: [...value] }
|
|
112
|
+
if (Array.isArray(value)) return value.map(encode)
|
|
113
|
+
if (!value || typeof value !== "object") return value
|
|
114
|
+
return Object.fromEntries(Object.entries(value).filter(([key]) => key !== "node").map(([key, item]) => [key, encode(item)]))
|
|
63
115
|
}
|
|
64
116
|
|
|
65
|
-
function
|
|
66
|
-
|
|
67
|
-
if (
|
|
68
|
-
return
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
117
|
+
function decode(value) {
|
|
118
|
+
if (Array.isArray(value)) return value.map(decode)
|
|
119
|
+
if (!value || typeof value !== "object") return value
|
|
120
|
+
if (value.$map) return new Map(value.$map.map(([key, item]) => [key, decode(item)]))
|
|
121
|
+
if (value.$set) return new Set(value.$set)
|
|
122
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, decode(item)]))
|
|
72
123
|
}
|
|
73
124
|
|
|
74
|
-
function
|
|
75
|
-
|
|
76
|
-
return
|
|
125
|
+
export function exportModuleTypes(sourceFile) {
|
|
126
|
+
const record = moduleTypes.get(path.resolve(sourceFile))
|
|
127
|
+
return record ? JSON.stringify(encode(record)) : null
|
|
77
128
|
}
|
|
78
129
|
|
|
79
|
-
function
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
130
|
+
export function importModuleTypes(sourceFile, saved) {
|
|
131
|
+
if (saved) moduleTypes.set(path.resolve(sourceFile), decode(JSON.parse(saved)))
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function atoms(type, env, seen = new Set()) {
|
|
135
|
+
if (type.kind === "named" && env.aliases.has(type.name) && !seen.has(type.name)) {
|
|
136
|
+
return atoms(env.aliases.get(type.name), env, new Set(seen).add(type.name))
|
|
84
137
|
}
|
|
85
|
-
return
|
|
138
|
+
if (type.kind === "union") return type.types.flatMap(part => atoms(part, env, seen))
|
|
139
|
+
return [type]
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function single(type, env) {
|
|
143
|
+
const all = atoms(type, env)
|
|
144
|
+
return all.length === 1 ? all[0] : null
|
|
86
145
|
}
|
|
87
146
|
|
|
88
147
|
function resolveStruct(name, env, seen = new Set()) {
|
|
@@ -105,132 +164,396 @@ function resolveStruct(name, env, seen = new Set()) {
|
|
|
105
164
|
return {
|
|
106
165
|
fields,
|
|
107
166
|
defaults: new Set([...parent.defaults, ...definition.defaults]),
|
|
108
|
-
methods: new
|
|
167
|
+
methods: new Map([...parent.methods, ...definition.methods]),
|
|
168
|
+
typeParams: definition.typeParams,
|
|
109
169
|
ancestors: new Set([definition.parent, ...parent.ancestors]),
|
|
110
170
|
parent: null,
|
|
111
171
|
resolved: true
|
|
112
172
|
}
|
|
113
173
|
}
|
|
114
174
|
|
|
175
|
+
function structFor(type, env) {
|
|
176
|
+
const atom = type && single(type, env)
|
|
177
|
+
if (!atom || (atom.kind !== "named" && atom.kind !== "generic")) return null
|
|
178
|
+
|
|
179
|
+
const definition = resolveStruct(atom.name, env)
|
|
180
|
+
if (!definition) return null
|
|
181
|
+
if (atom.kind !== "generic" || !definition.typeParams?.length) return { ...definition, label: formatType(atom) }
|
|
182
|
+
|
|
183
|
+
const bound = new Map(definition.typeParams.map((param, index) => [param, atom.args[index] ?? ANY]))
|
|
184
|
+
const fields = new Map()
|
|
185
|
+
for (const [name, field] of definition.fields) fields.set(name, { ...field, type: substituteType(field.type, bound) })
|
|
186
|
+
return { ...definition, fields, label: formatType(atom), bound }
|
|
187
|
+
}
|
|
188
|
+
|
|
115
189
|
function inherits(name, ancestor, env) {
|
|
116
190
|
if (name === ancestor) return true
|
|
117
191
|
return resolveStruct(name, env)?.ancestors.has(ancestor) ?? false
|
|
118
192
|
}
|
|
119
193
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
194
|
+
|
|
195
|
+
function isOpen(type, env) {
|
|
196
|
+
switch (type.kind) {
|
|
197
|
+
case "any":
|
|
198
|
+
case "intersection":
|
|
199
|
+
return true
|
|
200
|
+
case "named":
|
|
201
|
+
if (env.customTypes.has(type.name)) return true
|
|
202
|
+
return !(KNOWN.has(type.name) || BUILTIN_CLASSES.has(type.name) || env.structs.has(type.name) || env.enums.has(type.name))
|
|
203
|
+
case "generic":
|
|
204
|
+
return env.customTypes.has(type.name) || !(env.structs.has(type.name) || BUILTIN_CLASSES.has(type.name))
|
|
205
|
+
case "enumMember":
|
|
206
|
+
return !env.enums.has(type.name)
|
|
132
207
|
}
|
|
208
|
+
return false
|
|
133
209
|
}
|
|
134
210
|
|
|
135
|
-
const
|
|
211
|
+
const isArrayLike = type => type.kind === "array" || type.kind === "tuple" || isNamed(type, "array")
|
|
136
212
|
|
|
137
|
-
function
|
|
138
|
-
const binding = path_.scope.getBinding(name)
|
|
139
|
-
if (binding) return env.functions.get(binding) ?? null
|
|
140
|
-
return env.importedFunctions.get(name) ?? null
|
|
141
|
-
}
|
|
213
|
+
const isFunctionLike = type => type.kind === "function" || isNamed(type, "function")
|
|
142
214
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (!base) return true
|
|
215
|
+
function acceptsFunction(target, source, env) {
|
|
216
|
+
if (!isFunctionLike(target)) return isOpen(target, env)
|
|
217
|
+
if (!isFunctionLike(source)) return isOpen(source, env)
|
|
218
|
+
if (target.kind !== "function" || source.kind !== "function") return true
|
|
148
219
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
220
|
+
const paramsFit = source.params.every((param, index) => {
|
|
221
|
+
const given = target.params[index]
|
|
222
|
+
return !given || accepts(param.type, given.type, env)
|
|
223
|
+
})
|
|
224
|
+
const returnsFit = isNamed(target.returns, "undefined") || accepts(target.returns, source.returns, env)
|
|
225
|
+
return paramsFit && returnsFit
|
|
154
226
|
}
|
|
155
227
|
|
|
156
228
|
function acceptsAtom(target, source, env) {
|
|
157
|
-
if (
|
|
158
|
-
|
|
229
|
+
if (source.kind === "literal") {
|
|
230
|
+
if (target.kind === "literal") return target.value === source.value
|
|
231
|
+
return acceptsAtom(target, literalBase(source), env)
|
|
232
|
+
}
|
|
233
|
+
if (isFunctionLike(target) || isFunctionLike(source)) return acceptsFunction(target, source, env)
|
|
159
234
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (source === "array") return true
|
|
164
|
-
if (!sourceElement) return isOpen(source, env)
|
|
165
|
-
if (sourceElement === "null") return true
|
|
166
|
-
return accepts(targetElement, sourceElement, env, true)
|
|
235
|
+
if (target.kind === "literal") {
|
|
236
|
+
const base = literalBase(target)
|
|
237
|
+
return isNamed(source, base.name) || (isNumeric(base) && isNumeric(source)) || isOpen(source, env)
|
|
167
238
|
}
|
|
168
|
-
|
|
169
|
-
if (
|
|
239
|
+
|
|
240
|
+
if (target.kind === "array") {
|
|
241
|
+
if (source.kind === "array") return isNamed(source.element, "null") || accepts(target.element, source.element, env)
|
|
242
|
+
if (source.kind === "tuple") return source.items.every(item => accepts(target.element, item, env))
|
|
243
|
+
return isNamed(source, "array") || isOpen(source, env)
|
|
244
|
+
}
|
|
245
|
+
if (target.kind === "tuple") {
|
|
246
|
+
if (source.kind === "tuple") {
|
|
247
|
+
return source.items.length === target.items.length &&
|
|
248
|
+
target.items.every((item, index) => accepts(item, source.items[index], env))
|
|
249
|
+
}
|
|
250
|
+
return source.kind === "array" || isNamed(source, "array") || isOpen(source, env)
|
|
251
|
+
}
|
|
252
|
+
if (isNamed(target, "array")) return isArrayLike(source) || isOpen(source, env)
|
|
253
|
+
if (isArrayLike(source)) return isNamed(target, "object") || isOpen(target, env)
|
|
170
254
|
|
|
171
255
|
if (isOpen(target, env) || isOpen(source, env)) return true
|
|
172
256
|
|
|
173
|
-
if (
|
|
174
|
-
|
|
175
|
-
|
|
257
|
+
if (isNumeric(target) && isNumeric(source)) return target.name !== "int" || source.name === "int"
|
|
258
|
+
|
|
259
|
+
if (target.kind === "generic") {
|
|
260
|
+
const sameArgs = () => target.args.every((arg, index) => !source.args?.[index] || accepts(arg, source.args[index], env))
|
|
261
|
+
if (env.structs.has(target.name)) {
|
|
262
|
+
if (isNamed(source, "object")) return true
|
|
263
|
+
if (source.kind === "generic" && source.name === target.name) return sameArgs()
|
|
264
|
+
return (source.kind === "named" || source.kind === "generic") && inherits(source.name, target.name, env)
|
|
265
|
+
}
|
|
266
|
+
if (source.kind === "generic") return source.name === target.name && sameArgs()
|
|
267
|
+
return source.kind === "named" && source.name === target.name
|
|
268
|
+
}
|
|
269
|
+
if (source.kind === "generic") {
|
|
270
|
+
if (target.kind === "named" && target.name === source.name) return true
|
|
271
|
+
return isNamed(target, "object") && env.structs.has(source.name)
|
|
176
272
|
}
|
|
177
273
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
274
|
+
if (target.kind === "enumMember") {
|
|
275
|
+
if (source.kind === "enumMember") return source.name === target.name && source.member === target.member
|
|
276
|
+
return source.name === target.name || env.enums.get(target.name).valueTypes.has(source.name)
|
|
277
|
+
}
|
|
278
|
+
if (env.enums.has(target.name)) {
|
|
279
|
+
if (source.kind === "enumMember") return source.name === target.name
|
|
280
|
+
return source.name === target.name || env.enums.get(target.name).valueTypes.has(source.name)
|
|
184
281
|
}
|
|
282
|
+
if (source.kind === "enumMember") return false
|
|
283
|
+
|
|
284
|
+
if (target.name === source.name) return true
|
|
285
|
+
if (BUILTIN_CLASSES.has(target.name) || BUILTIN_CLASSES.has(source.name)) return false
|
|
286
|
+
if (target.name === "object") return env.structs.has(source.name)
|
|
287
|
+
if (env.structs.has(target.name)) return source.name === "object" || inherits(source.name, target.name, env)
|
|
288
|
+
return false
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function accepts(target, source, env) {
|
|
292
|
+
if (!target || !source) return true
|
|
293
|
+
|
|
294
|
+
const targets = atoms(target, env)
|
|
295
|
+
if (targets.some(atom => atom.kind === "any" || atom.kind === "intersection")) return true
|
|
296
|
+
|
|
297
|
+
return atoms(source, env).every(atom =>
|
|
298
|
+
atom.kind === "any" || atom.kind === "intersection" || targets.some(other => acceptsAtom(other, atom, env)))
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
const bindingIds = new WeakMap()
|
|
303
|
+
let nextBindingId = 0
|
|
185
304
|
|
|
186
|
-
|
|
187
|
-
if (
|
|
188
|
-
|
|
305
|
+
function refKey(node, path_) {
|
|
306
|
+
if (t.isIdentifier(node)) {
|
|
307
|
+
const binding = path_.scope.getBinding(node.name)
|
|
308
|
+
if (!binding) return null
|
|
309
|
+
if (!bindingIds.has(binding)) bindingIds.set(binding, `#${nextBindingId++}`)
|
|
310
|
+
return bindingIds.get(binding)
|
|
189
311
|
}
|
|
190
|
-
if (
|
|
312
|
+
if (t.isThisExpression(node)) return "this"
|
|
313
|
+
if ((t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) && !node.computed && t.isIdentifier(node.property)) {
|
|
314
|
+
const base = refKey(node.object, path_)
|
|
315
|
+
return base && `${base}.${node.property.name}`
|
|
316
|
+
}
|
|
317
|
+
return null
|
|
318
|
+
}
|
|
191
319
|
|
|
192
|
-
|
|
193
|
-
|
|
320
|
+
function keep(type, env, predicate) {
|
|
321
|
+
const kept = atoms(type, env).filter(predicate)
|
|
322
|
+
return kept.length === 0 ? type : unionOf(kept)
|
|
323
|
+
}
|
|
194
324
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
return
|
|
325
|
+
function typeofName(atom, env) {
|
|
326
|
+
switch (atom.kind) {
|
|
327
|
+
case "literal": return typeof atom.value === "boolean" ? "boolean" : typeof atom.value
|
|
328
|
+
case "array":
|
|
329
|
+
case "tuple":
|
|
330
|
+
case "generic":
|
|
331
|
+
return "object"
|
|
332
|
+
case "function":
|
|
333
|
+
return "function"
|
|
334
|
+
case "named":
|
|
335
|
+
if (NUMERIC.has(atom.name)) return "number"
|
|
336
|
+
if (atom.name === "bool") return "boolean"
|
|
337
|
+
if (atom.name === "null") return "object"
|
|
338
|
+
if (["string", "undefined", "function", "bigint", "symbol"].includes(atom.name)) return atom.name
|
|
339
|
+
if (["object", "array", "element"].includes(atom.name) || BUILTIN_CLASSES.has(atom.name) || env.structs.has(atom.name)) return "object"
|
|
198
340
|
}
|
|
341
|
+
return null
|
|
342
|
+
}
|
|
199
343
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
if (
|
|
204
|
-
if (
|
|
205
|
-
return
|
|
344
|
+
function kindName(atom, env) {
|
|
345
|
+
if (atom.kind === "literal") return literalBase(atom).name
|
|
346
|
+
if (atom.kind === "named") {
|
|
347
|
+
if (["int", "float", "string", "bool", "null", "undefined", "element"].includes(atom.name)) return atom.name
|
|
348
|
+
if (env.structs.has(atom.name)) return "struct"
|
|
349
|
+
if (BUILTIN_CLASSES.has(atom.name)) return atom.name.toLowerCase()
|
|
206
350
|
}
|
|
351
|
+
if (atom.kind === "generic" && BUILTIN_CLASSES.has(atom.name)) return atom.name.toLowerCase()
|
|
352
|
+
if (atom.kind === "enumMember") return "enum"
|
|
353
|
+
if (atom.kind === "function") return "function"
|
|
354
|
+
return null
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const isNullLiteral = node => t.isNullLiteral(node) || t.isIdentifier(node, { name: "undefined" })
|
|
358
|
+
const isLiteralNode = node => t.isStringLiteral(node) || t.isNumericLiteral(node) || t.isBooleanLiteral(node)
|
|
359
|
+
|
|
360
|
+
function equalityFacts(test, positive, path_, env) {
|
|
361
|
+
const { left, right } = test
|
|
362
|
+
const loose = test.operator.length === 2
|
|
207
363
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
364
|
+
const typeofSide = [[left, right], [right, left]].find(([a, b]) => t.isUnaryExpression(a, { operator: "typeof" }) && t.isStringLiteral(b))
|
|
365
|
+
if (typeofSide) {
|
|
366
|
+
const [{ argument }, { value }] = typeofSide
|
|
367
|
+
const key = refKey(argument, path_)
|
|
368
|
+
return key ? [{ key, refine: type => keep(type, env, atom => { const name = typeofName(atom, env); return name === null || (name === value) === positive }) }] : []
|
|
211
369
|
}
|
|
212
370
|
|
|
371
|
+
const kindSide = [[left, right], [right, left]].find(([a, b]) =>
|
|
372
|
+
t.isCallExpression(a) && t.isIdentifier(a.callee, { name: "type" }) && a.arguments.length === 1 && t.isStringLiteral(b))
|
|
373
|
+
if (kindSide) {
|
|
374
|
+
const [{ arguments: [argument] }, { value }] = kindSide
|
|
375
|
+
const key = refKey(argument, path_)
|
|
376
|
+
return key ? [{ key, refine: type => keep(type, env, atom => { const name = kindName(atom, env); return name === null || (name === value) === positive }) }] : []
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const [ref, other] = refKey(left, path_) ? [left, right] : [right, left]
|
|
380
|
+
const key = refKey(ref, path_)
|
|
381
|
+
if (!key) return []
|
|
382
|
+
|
|
383
|
+
if (isNullLiteral(other)) {
|
|
384
|
+
const names = loose ? ["null", "undefined"] : [t.isNullLiteral(other) ? "null" : "undefined"]
|
|
385
|
+
const matches = atom => atom.kind === "named" && names.includes(atom.name)
|
|
386
|
+
return [{ key, refine: type => keep(type, env, atom => matches(atom) === positive) }]
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (isLiteralNode(other)) {
|
|
390
|
+
const value = other.value
|
|
391
|
+
const refine = positive
|
|
392
|
+
? type => keep(type, env, atom => atom.kind === "literal" ? atom.value === value : isOpen(atom, env) || acceptsAtom(atom, literal(value), env))
|
|
393
|
+
: type => keep(type, env, atom => !(atom.kind === "literal" && atom.value === value))
|
|
394
|
+
return [{ key, refine }]
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
if (t.isMemberExpression(other) && !other.computed && t.isIdentifier(other.object) && t.isIdentifier(other.property) &&
|
|
398
|
+
env.enums.get(other.object.name)?.members.has(other.property.name)) {
|
|
399
|
+
const member = enumMember(other.object.name, other.property.name)
|
|
400
|
+
const same = atom => atom.kind === "enumMember" && atom.name === member.name && atom.member === member.member
|
|
401
|
+
return [{ key, refine: type => keep(type, env, atom => positive ? same(atom) || atom.kind !== "enumMember" : !same(atom)) }]
|
|
402
|
+
}
|
|
403
|
+
return []
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function staticCheck(test, path_) {
|
|
407
|
+
if (!t.isCallExpression(test) || test.arguments.length !== 1) return null
|
|
408
|
+
const callee = test.callee
|
|
409
|
+
if (!t.isMemberExpression(callee) || callee.computed || !t.isIdentifier(callee.object) || !t.isIdentifier(callee.property)) return null
|
|
410
|
+
if (path_.scope.getBinding(callee.object.name)) return null
|
|
411
|
+
return `${callee.object.name}.${callee.property.name}`
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function factsFrom(test, truthy, path_, env) {
|
|
415
|
+
const at = typeof test.end === "number" ? test.end : Infinity
|
|
416
|
+
return testFacts(test, truthy, path_, env).map(fact => ({ ...fact, at }))
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function testFacts(test, truthy, path_, env) {
|
|
420
|
+
if (t.isUnaryExpression(test, { operator: "!" })) return factsFrom(test.argument, !truthy, path_, env)
|
|
421
|
+
|
|
422
|
+
if (t.isLogicalExpression(test)) {
|
|
423
|
+
if (test.operator === "&&" && truthy) return [...factsFrom(test.left, true, path_, env), ...factsFrom(test.right, true, path_, env)]
|
|
424
|
+
if (test.operator === "||" && !truthy) return [...factsFrom(test.left, false, path_, env), ...factsFrom(test.right, false, path_, env)]
|
|
425
|
+
return []
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const key = refKey(test, path_)
|
|
429
|
+
if (key) return truthy ? [{ key, refine: type => keep(type, env, atom => !isNullish(atom)) }] : []
|
|
430
|
+
|
|
431
|
+
if (t.isBinaryExpression(test) && ["==", "===", "!=", "!=="].includes(test.operator)) {
|
|
432
|
+
return equalityFacts(test, test.operator.startsWith("=") === truthy, path_, env)
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
if (t.isBinaryExpression(test, { operator: "instanceof" }) && t.isIdentifier(test.right)) {
|
|
436
|
+
const key = refKey(test.left, path_)
|
|
437
|
+
const name = test.right.name
|
|
438
|
+
const matches = atom => (atom.kind === "named" || atom.kind === "generic") && atom.name === name
|
|
439
|
+
if (!key) return []
|
|
440
|
+
return [{ key, refine: truthy
|
|
441
|
+
? type => atoms(type, env).some(matches) ? keep(type, env, matches) : named(name)
|
|
442
|
+
: type => keep(type, env, atom => !matches(atom)) }]
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const check = staticCheck(test, path_)
|
|
446
|
+
if (check === "Array.isArray") {
|
|
447
|
+
const key = refKey(test.arguments[0], path_)
|
|
448
|
+
return key ? [{ key, refine: type => keep(type, env, atom => isArrayLike(atom) === truthy) }] : []
|
|
449
|
+
}
|
|
450
|
+
if (check === "Number.isInteger" && truthy) {
|
|
451
|
+
const key = refKey(test.arguments[0], path_)
|
|
452
|
+
const toInt = atom => isNamed(atom, "number") ? INT : atom
|
|
453
|
+
return key ? [{ key, refine: type => unionOf(atoms(keep(type, env, atom => !isNamed(atom, "float")), env).map(toInt)) }] : []
|
|
454
|
+
}
|
|
455
|
+
return []
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function exits(statement) {
|
|
459
|
+
if (!statement) return false
|
|
460
|
+
if (t.isReturnStatement(statement) || t.isThrowStatement(statement) ||
|
|
461
|
+
t.isContinueStatement(statement) || t.isBreakStatement(statement)) return true
|
|
462
|
+
if (t.isBlockStatement(statement)) return statement.body.some(exits)
|
|
463
|
+
if (t.isIfStatement(statement)) return exits(statement.consequent) && exits(statement.alternate)
|
|
213
464
|
return false
|
|
214
465
|
}
|
|
215
466
|
|
|
216
|
-
|
|
217
|
-
|
|
467
|
+
function ancestorFacts(path_, env) {
|
|
468
|
+
const facts = []
|
|
469
|
+
let child = path_
|
|
470
|
+
let parent = path_.parentPath
|
|
218
471
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
if (isIntersection(targetLabel) || isIntersection(sourceLabel)) return true
|
|
472
|
+
while (parent) {
|
|
473
|
+
const node = parent.node
|
|
222
474
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
475
|
+
if ((parent.isIfStatement() || parent.isConditionalExpression()) && child.key !== "test") {
|
|
476
|
+
facts.push(...factsFrom(node.test, child.key === "consequent", parent, env))
|
|
477
|
+
} else if (parent.isLogicalExpression() && child.key === "right" && node.operator !== "??") {
|
|
478
|
+
facts.push(...factsFrom(node.left, node.operator === "&&", parent, env))
|
|
479
|
+
} else if ((parent.isWhileStatement() || parent.isForStatement()) && child.key === "body" && node.test) {
|
|
480
|
+
facts.push(...factsFrom(node.test, true, parent, env))
|
|
481
|
+
}
|
|
226
482
|
|
|
227
|
-
|
|
483
|
+
if (child.listKey === "body" && typeof child.key === "number") {
|
|
484
|
+
const siblings = child.container
|
|
485
|
+
for (let i = 0; i < child.key; i++) {
|
|
486
|
+
const statement = siblings[i]
|
|
487
|
+
if (!t.isIfStatement(statement)) continue
|
|
488
|
+
if (exits(statement.consequent) && !exits(statement.alternate)) {
|
|
489
|
+
facts.push(...factsFrom(statement.test, false, parent, env))
|
|
490
|
+
} else if (statement.alternate && exits(statement.alternate)) {
|
|
491
|
+
facts.push(...factsFrom(statement.test, true, parent, env))
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
child = parent
|
|
497
|
+
parent = parent.parentPath
|
|
498
|
+
}
|
|
499
|
+
return facts
|
|
228
500
|
}
|
|
229
501
|
|
|
230
|
-
function
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
502
|
+
function withFacts(env, facts, run) {
|
|
503
|
+
env.facts.push(...facts)
|
|
504
|
+
try {
|
|
505
|
+
return run()
|
|
506
|
+
} finally {
|
|
507
|
+
env.facts.length -= facts.length
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
function narrowed(node, path_, env, type) {
|
|
512
|
+
if (!type) return type
|
|
513
|
+
|
|
514
|
+
const key = refKey(node, path_)
|
|
515
|
+
if (!key) return type
|
|
516
|
+
const violations = t.isIdentifier(node) ? path_.scope.getBinding(node.name)?.constantViolations ?? [] : []
|
|
517
|
+
const stillHolds = fact => violations.every(violation =>
|
|
518
|
+
typeof violation.node.start === "number" && typeof node.start === "number" &&
|
|
519
|
+
!(violation.node.start > fact.at && violation.node.start < node.start))
|
|
520
|
+
|
|
521
|
+
const facts = [...env.facts, ...ancestorFacts(path_, env)].filter(fact => fact.key === key && stillHolds(fact))
|
|
522
|
+
return facts.reduce((current, fact) => fact.refine(current), type)
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
function signatureFor(name, path_, env) {
|
|
527
|
+
const binding = path_.scope.getBinding(name)
|
|
528
|
+
if (binding) return env.functions.get(binding) ?? signatureOfType(env.bindings.get(binding), env)
|
|
529
|
+
return env.importedFunctions.get(name) ?? null
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function signatureOfType(type, env) {
|
|
533
|
+
const atom = type && single(type, env)
|
|
534
|
+
if (atom?.kind !== "function") return null
|
|
535
|
+
return {
|
|
536
|
+
parameters: atom.params.map((param, index) => ({ name: param.name ?? `#${index + 1}`, type: param.type, required: true })),
|
|
537
|
+
rest: false,
|
|
538
|
+
returns: atom.returns,
|
|
539
|
+
async: false,
|
|
540
|
+
typed: true,
|
|
541
|
+
node: null,
|
|
542
|
+
typeParams: []
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function functionTypeOf(fn, env) {
|
|
547
|
+
const fnPath = env.functionPaths.get(fn)
|
|
548
|
+
if (!fnPath) return FUNCTION
|
|
549
|
+
|
|
550
|
+
const signature = collectParameters(fn)
|
|
551
|
+
const params = signature.parameters.map(param => {
|
|
552
|
+
const binding = param.name && fnPath.scope.getBinding(param.name)
|
|
553
|
+
const type = param.type ?? (binding && binding.kind === "param" ? contextualParam(binding, env) : null)
|
|
554
|
+
return { name: param.name, type: type ?? ANY }
|
|
555
|
+
})
|
|
556
|
+
return functionOf(params, returnType(signature, env) ?? ANY)
|
|
234
557
|
}
|
|
235
558
|
|
|
236
559
|
function typedVariableLabel(node) {
|
|
@@ -239,7 +562,7 @@ function typedVariableLabel(node) {
|
|
|
239
562
|
return t.isStringLiteral(node.arguments[1]) ? node.arguments[1].value : null
|
|
240
563
|
}
|
|
241
564
|
|
|
242
|
-
function
|
|
565
|
+
function structLiteralName(node, env) {
|
|
243
566
|
if (!t.isCallExpression(node)) return null
|
|
244
567
|
const callee = node.callee
|
|
245
568
|
if (!t.isMemberExpression(callee) || callee.computed) return null
|
|
@@ -248,130 +571,652 @@ function structLiteralLabel(node, env) {
|
|
|
248
571
|
return callee.object.name
|
|
249
572
|
}
|
|
250
573
|
|
|
251
|
-
|
|
252
|
-
|
|
574
|
+
function elementType(type, env) {
|
|
575
|
+
const all = type ? atoms(type, env).filter(atom => !isNullish(atom)) : []
|
|
576
|
+
const elements = []
|
|
577
|
+
for (const atom of all) {
|
|
578
|
+
if (atom.kind === "array" && !isNamed(atom.element, "null")) elements.push(atom.element)
|
|
579
|
+
else if (atom.kind === "tuple") elements.push(...atom.items)
|
|
580
|
+
else if (atom.kind === "generic" && (atom.name === "Set" || atom.name === "Array")) elements.push(atom.args[0] ?? ANY)
|
|
581
|
+
else if (isNamed(atom, "string")) elements.push(STRING)
|
|
582
|
+
else return null
|
|
583
|
+
}
|
|
584
|
+
return elements.length ? unionOf(elements) : null
|
|
585
|
+
}
|
|
253
586
|
|
|
254
|
-
|
|
255
|
-
if (
|
|
256
|
-
|
|
257
|
-
if (
|
|
258
|
-
if (
|
|
259
|
-
|
|
260
|
-
|
|
587
|
+
function returnType(signature, env, call = null, path_ = null, bound = null) {
|
|
588
|
+
if (!signature) return null
|
|
589
|
+
let returns = signature.returns ?? signature.inferred ?? inferReturn(signature.node, env)
|
|
590
|
+
if (!returns) return null
|
|
591
|
+
if (call && (signature.typeParams?.length || bound?.size)) {
|
|
592
|
+
returns = substituteType(returns, bindTypeParams(signature, call, path_, env, bound))
|
|
593
|
+
}
|
|
594
|
+
return signature.async ? genericOf("Promise", [returns]) : returns
|
|
595
|
+
}
|
|
261
596
|
|
|
262
|
-
|
|
263
|
-
if (node.elements.length === 0) return "null[]"
|
|
597
|
+
const mentions = (type, names) => typeNames(type).some(name => names.includes(name))
|
|
264
598
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
599
|
+
function unify(pattern, actual, params, bound, env) {
|
|
600
|
+
if (!pattern || !actual) return
|
|
601
|
+
if (pattern.kind === "named" && params.includes(pattern.name)) {
|
|
602
|
+
if (!bound.has(pattern.name)) bound.set(pattern.name, widen(actual))
|
|
603
|
+
return
|
|
604
|
+
}
|
|
605
|
+
if (!mentions(pattern, params)) return
|
|
606
|
+
|
|
607
|
+
const present = atoms(actual, env).filter(atom => !isNullish(atom))
|
|
608
|
+
const atom = present.length === 1 ? present[0] : null
|
|
609
|
+
|
|
610
|
+
switch (pattern.kind) {
|
|
611
|
+
case "array":
|
|
612
|
+
unify(pattern.element, elementType(actual, env), params, bound, env)
|
|
613
|
+
break
|
|
614
|
+
case "tuple":
|
|
615
|
+
if (atom?.kind === "tuple") pattern.items.forEach((item, index) => unify(item, atom.items[index], params, bound, env))
|
|
616
|
+
break
|
|
617
|
+
case "generic":
|
|
618
|
+
if (atom?.kind === "generic" && atom.name === pattern.name) {
|
|
619
|
+
pattern.args.forEach((arg, index) => unify(arg, atom.args[index], params, bound, env))
|
|
620
|
+
}
|
|
621
|
+
break
|
|
622
|
+
case "function":
|
|
623
|
+
if (atom?.kind === "function") {
|
|
624
|
+
pattern.params.forEach((param, index) => atom.params[index] && unify(param.type, atom.params[index].type, params, bound, env))
|
|
625
|
+
unify(pattern.returns, atom.returns, params, bound, env)
|
|
626
|
+
}
|
|
627
|
+
break
|
|
628
|
+
case "union": {
|
|
629
|
+
const generic = pattern.types.filter(part => mentions(part, params))
|
|
630
|
+
if (generic.length === 1) unify(generic[0], present.length ? unionOf(present) : actual, params, bound, env)
|
|
271
631
|
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
272
634
|
|
|
273
|
-
|
|
274
|
-
|
|
635
|
+
function bindTypeParams(signature, call, path_, env, bound = null, skipCallbacks = false) {
|
|
636
|
+
const result = new Map(bound ?? [])
|
|
637
|
+
const params = signature.typeParams ?? []
|
|
638
|
+
if (params.length === 0 || !path_) return result
|
|
639
|
+
|
|
640
|
+
const order = call.arguments.map((argument, index) => index).sort((a, b) =>
|
|
641
|
+
t.isFunction(call.arguments[a]) - t.isFunction(call.arguments[b]))
|
|
642
|
+
for (const index of order) {
|
|
643
|
+
const argument = call.arguments[index]
|
|
644
|
+
const parameter = signature.parameters[index]
|
|
645
|
+
if (!parameter?.type || t.isSpreadElement(argument) || !mentions(parameter.type, params)) continue
|
|
646
|
+
if (skipCallbacks && t.isFunction(argument)) continue
|
|
647
|
+
unify(parameter.type, infer(argument, path_, env), params, result, env)
|
|
275
648
|
}
|
|
649
|
+
for (const name of params) if (!result.has(name)) result.set(name, ANY)
|
|
650
|
+
return result
|
|
651
|
+
}
|
|
276
652
|
|
|
277
|
-
|
|
278
|
-
if (
|
|
653
|
+
function inferReturn(fn, env, skip = null) {
|
|
654
|
+
if (!fn || fn.generator) return null
|
|
655
|
+
if (!skip && env.inferredReturns.has(fn)) return env.inferredReturns.get(fn)
|
|
656
|
+
const fnPath = env.functionPaths.get(fn)
|
|
657
|
+
if (!fnPath) return null
|
|
658
|
+
|
|
659
|
+
if (!skip) env.inferredReturns.set(fn, null)
|
|
660
|
+
|
|
661
|
+
let result = null
|
|
662
|
+
if (!t.isBlockStatement(fn.body)) {
|
|
663
|
+
result = widen(infer(fn.body, fnPath.get("body"), env))
|
|
664
|
+
} else {
|
|
665
|
+
const found = []
|
|
666
|
+
let unknown = false
|
|
667
|
+
fnPath.traverse({
|
|
668
|
+
Function(inner) { inner.skip() },
|
|
669
|
+
ReturnStatement(statement) {
|
|
670
|
+
if (statement.node === skip) return
|
|
671
|
+
if (!statement.node.argument) { found.push(UNDEFINED); return }
|
|
672
|
+
const type = infer(statement.node.argument, statement, env)
|
|
673
|
+
if (type) found.push(widen(type))
|
|
674
|
+
else unknown = true
|
|
675
|
+
}
|
|
676
|
+
})
|
|
279
677
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
if (
|
|
283
|
-
return null
|
|
678
|
+
const last = fn.body.body.at(-1)
|
|
679
|
+
if (found.length === 0) result = fn.async || !unknown ? UNDEFINED : null
|
|
680
|
+
else if (!unknown && (exits(last) || skip)) result = unionOf(found)
|
|
284
681
|
}
|
|
285
682
|
|
|
286
|
-
if (
|
|
287
|
-
|
|
288
|
-
|
|
683
|
+
if (!skip) env.inferredReturns.set(fn, result)
|
|
684
|
+
return result
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
function callbackReturn(callback, path_, env) {
|
|
688
|
+
if (t.isFunction(callback)) return inferReturn(callback, env)
|
|
689
|
+
if (t.isIdentifier(callback)) return returnType(signatureFor(callback.name, path_, env), env)
|
|
690
|
+
return null
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
const STRING_METHODS = {
|
|
694
|
+
string: ["toUpperCase", "toLowerCase", "toLocaleUpperCase", "toLocaleLowerCase", "trim", "trimStart", "trimEnd",
|
|
695
|
+
"padStart", "padEnd", "repeat", "replace", "replaceAll", "slice", "substring", "substr", "charAt", "concat", "normalize"],
|
|
696
|
+
bool: ["includes", "startsWith", "endsWith"],
|
|
697
|
+
int: ["indexOf", "lastIndexOf", "search", "charCodeAt", "codePointAt", "localeCompare"]
|
|
698
|
+
}
|
|
699
|
+
const STRING_RESULT = new Map(Object.entries(STRING_METHODS).flatMap(([result, methods]) => methods.map(method => [method, named(result)])))
|
|
700
|
+
|
|
701
|
+
const SAME_ARRAY = new Set(["filter", "slice", "reverse", "sort", "toSorted", "toReversed", "fill", "splice", "toSpliced", "with", "copyWithin"])
|
|
702
|
+
const CALLBACK_ELEMENT = new Set([
|
|
703
|
+
"map", "filter", "find", "findIndex", "findLast", "findLastIndex", "some", "every", "forEach", "flatMap"
|
|
704
|
+
])
|
|
705
|
+
|
|
706
|
+
function arrayMethodType(receiver, method, call, path_, env) {
|
|
707
|
+
const element = elementType(receiver, env)
|
|
708
|
+
switch (method) {
|
|
709
|
+
case "map": {
|
|
710
|
+
const result = callbackReturn(call.arguments[0], path_, env)
|
|
711
|
+
return result ? arrayOf(result) : ARRAY
|
|
712
|
+
}
|
|
713
|
+
case "flatMap": {
|
|
714
|
+
const result = callbackReturn(call.arguments[0], path_, env)
|
|
715
|
+
if (!result) return ARRAY
|
|
716
|
+
return arrayOf(elementType(result, env) ?? result)
|
|
717
|
+
}
|
|
718
|
+
case "concat": {
|
|
719
|
+
const parts = [element]
|
|
720
|
+
for (const argument of call.arguments) {
|
|
721
|
+
const type = infer(argument, path_, env)
|
|
722
|
+
parts.push(type && isArrayLike(single(type, env) ?? ANY) ? elementType(type, env) : type)
|
|
723
|
+
}
|
|
724
|
+
return parts.every(Boolean) ? arrayOf(unionOf(parts)) : ARRAY
|
|
725
|
+
}
|
|
726
|
+
case "find":
|
|
727
|
+
case "findLast":
|
|
728
|
+
case "at":
|
|
729
|
+
case "pop":
|
|
730
|
+
case "shift":
|
|
731
|
+
return element ? unionOf([element, UNDEFINED]) : null
|
|
732
|
+
case "findIndex":
|
|
733
|
+
case "findLastIndex":
|
|
734
|
+
case "indexOf":
|
|
735
|
+
case "lastIndexOf":
|
|
736
|
+
case "push":
|
|
737
|
+
case "unshift":
|
|
738
|
+
return INT
|
|
739
|
+
case "some":
|
|
740
|
+
case "every":
|
|
741
|
+
case "includes":
|
|
742
|
+
return BOOL
|
|
743
|
+
case "join":
|
|
744
|
+
return STRING
|
|
745
|
+
case "forEach":
|
|
746
|
+
return UNDEFINED
|
|
747
|
+
case "reduce":
|
|
748
|
+
case "reduceRight":
|
|
749
|
+
return call.arguments.length > 1 ? widen(infer(call.arguments[1], path_, env)) : element
|
|
289
750
|
}
|
|
751
|
+
if (SAME_ARRAY.has(method)) return element ? arrayOf(element) : ARRAY
|
|
752
|
+
return null
|
|
753
|
+
}
|
|
290
754
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
755
|
+
function methodType(receiver, method, call, path_, env) {
|
|
756
|
+
if (method === "toString" || method === "toFixed" || method === "toLocaleString") return STRING
|
|
757
|
+
|
|
758
|
+
const results = []
|
|
759
|
+
for (const atom of atoms(receiver, env)) {
|
|
760
|
+
if (isNullish(atom)) continue
|
|
761
|
+
let result = null
|
|
762
|
+
|
|
763
|
+
const struct = structFor(atom, env)
|
|
764
|
+
if (struct?.methods.has(method)) result = returnType(struct.methods.get(method), env, call, path_, struct.bound)
|
|
765
|
+
else if (isArrayLike(atom)) result = arrayMethodType(atom, method, call, path_, env)
|
|
766
|
+
else if (isNamed(atom, "string") || (atom.kind === "literal" && typeof atom.value === "string")) {
|
|
767
|
+
result = method === "split" ? arrayOf(STRING) : method === "at" ? unionOf([STRING, UNDEFINED]) : STRING_RESULT.get(method) ?? null
|
|
768
|
+
} else if (atom.kind === "generic" && atom.name === "Map") {
|
|
769
|
+
if (method === "get") result = unionOf([atom.args[1] ?? ANY, UNDEFINED])
|
|
770
|
+
else if (method === "has" || method === "delete") result = BOOL
|
|
771
|
+
else if (method === "set") result = atom
|
|
772
|
+
} else if (atom.kind === "generic" && atom.name === "Set") {
|
|
773
|
+
if (method === "has" || method === "delete") result = BOOL
|
|
774
|
+
else if (method === "add") result = atom
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
if (!result) return null
|
|
778
|
+
results.push(result)
|
|
779
|
+
}
|
|
780
|
+
return results.length ? unionOf(results) : null
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
const GLOBAL_CALLS = { String: STRING, Number: NUMBER, Boolean: BOOL, parseInt: INT, parseFloat: FLOAT }
|
|
784
|
+
const RUNTIME_CALLS = { __sizeof__: INT, type: STRING, __is_empty__: BOOL, __intdiv__: INT }
|
|
785
|
+
const INTEGER_MATH = new Set(["floor", "ceil", "round", "trunc", "sign"])
|
|
786
|
+
|
|
787
|
+
function staticCallType(object, method, call, path_, env) {
|
|
788
|
+
if (path_.scope.getBinding(object)) return null
|
|
789
|
+
if (object === "Math") {
|
|
790
|
+
if (INTEGER_MATH.has(method)) return INT
|
|
791
|
+
if (method === "max" || method === "min" || method === "abs") {
|
|
792
|
+
const args = call.arguments.map(argument => widen(infer(argument, path_, env)))
|
|
793
|
+
return args.length && args.every(arg => isNamed(arg, "int")) ? INT : FLOAT
|
|
794
|
+
}
|
|
795
|
+
return FLOAT
|
|
796
|
+
}
|
|
797
|
+
if (object === "JSON" && method === "stringify") return STRING
|
|
798
|
+
if (object === "Object" && method === "keys") return arrayOf(STRING)
|
|
799
|
+
if (object === "Array" && method === "isArray") return BOOL
|
|
800
|
+
if (object === "Number" && method.startsWith("is")) return BOOL
|
|
801
|
+
if (object === "Date" && method === "now") return INT
|
|
802
|
+
return null
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
function formType(argument, env) {
|
|
806
|
+
if (!t.isIdentifier(argument) || !resolveStruct(argument.name, env)) return null
|
|
807
|
+
const name = argument.name
|
|
808
|
+
const key = `form of ${name}`
|
|
809
|
+
if (!env.structs.has(key)) {
|
|
810
|
+
const struct = resolveStruct(name, env)
|
|
811
|
+
const own = type => ({ type, optional: false, readonly: true })
|
|
812
|
+
const fields = new Map([...struct.fields].map(([field, info]) => [field, { type: info.type, optional: true, readonly: false }]))
|
|
813
|
+
env.structs.set(`errors of ${name}`, {
|
|
814
|
+
fields: new Map([...struct.fields.keys()].map(field => [field, own(unionOf([STRING, named("null")]))])),
|
|
815
|
+
defaults: new Set(), methods: new Map(), parent: null, typeParams: []
|
|
816
|
+
})
|
|
817
|
+
fields.set("errors", own(named(`errors of ${name}`)))
|
|
818
|
+
fields.set("valid", own(BOOL))
|
|
819
|
+
fields.set("submitting", own(BOOL))
|
|
820
|
+
fields.set("values", own(ANY))
|
|
821
|
+
env.structs.set(key, {
|
|
822
|
+
fields, defaults: new Set(), parent: null, typeParams: [],
|
|
823
|
+
methods: new Map([["submit", null], ["reset", null], ["touched", null]])
|
|
824
|
+
})
|
|
825
|
+
}
|
|
826
|
+
return named(key)
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
function callType(node, path_, env) {
|
|
830
|
+
const callee = node.callee
|
|
831
|
+
|
|
832
|
+
if (t.isFunction(callee)) {
|
|
833
|
+
const match = matchExpression(node)
|
|
834
|
+
if (match) return inferReturn(callee, env, missingCases(match, path_, env)?.missing.length === 0 ? match.fallback : null)
|
|
835
|
+
return returnType({ async: callee.async, node: callee }, env)
|
|
294
836
|
}
|
|
295
837
|
|
|
296
|
-
if (t.
|
|
297
|
-
const
|
|
298
|
-
if (
|
|
838
|
+
if (t.isIdentifier(callee)) {
|
|
839
|
+
const label = typedVariableLabel(node)
|
|
840
|
+
if (label) return readType(label)
|
|
841
|
+
|
|
842
|
+
const name = callee.name
|
|
843
|
+
if (!path_.scope.getBinding(name)) {
|
|
844
|
+
if (name === "createForm") return formType(node.arguments[0], env)
|
|
845
|
+
if (name in RUNTIME_CALLS) return RUNTIME_CALLS[name]
|
|
846
|
+
if (name in GLOBAL_CALLS) return GLOBAL_CALLS[name]
|
|
847
|
+
}
|
|
848
|
+
if (name === "__copyof__" || name === "__lock_object__" || name === "__as_struct__") return infer(node.arguments[0], path_, env)
|
|
849
|
+
return returnType(signatureFor(name, path_, env), env, node, path_)
|
|
850
|
+
}
|
|
299
851
|
|
|
300
|
-
|
|
301
|
-
|
|
852
|
+
if (!t.isMemberExpression(callee) || callee.computed || !t.isIdentifier(callee.property)) return null
|
|
853
|
+
const method = callee.property.name
|
|
854
|
+
const owner = callee.object
|
|
302
855
|
|
|
303
|
-
|
|
304
|
-
if (
|
|
856
|
+
if (t.isIdentifier(owner)) {
|
|
857
|
+
if (env.enums.has(owner.name) && method === "from") return named(owner.name)
|
|
858
|
+
if (env.structs.has(owner.name) && method === "verify") return named(owner.name)
|
|
859
|
+
const result = staticCallType(owner.name, method, node, path_, env)
|
|
860
|
+
if (result) return result
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
const receiver = infer(owner, path_, env)
|
|
864
|
+
return receiver ? methodType(receiver, method, node, path_, env) : null
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
function memberType(node, path_, env) {
|
|
868
|
+
const object = infer(node.object, path_, env)
|
|
869
|
+
|
|
870
|
+
if (node.computed) {
|
|
871
|
+
if (!object) return null
|
|
872
|
+
const atom = single(object, env)
|
|
873
|
+
if (atom?.kind === "tuple" && t.isNumericLiteral(node.property)) return atom.items[node.property.value] ?? UNDEFINED
|
|
874
|
+
if (atom && (isArrayLike(atom) || isNamed(atom, "string"))) return elementType(atom, env)
|
|
305
875
|
return null
|
|
306
876
|
}
|
|
877
|
+
if (!t.isIdentifier(node.property)) return null
|
|
878
|
+
|
|
879
|
+
const name = node.property.name
|
|
880
|
+
if (t.isIdentifier(node.object) && !path_.scope.getBinding(node.object.name)?.constantViolations.length) {
|
|
881
|
+
const definition = env.enums.get(node.object.name)
|
|
882
|
+
if (definition?.members.has(name)) return enumMember(node.object.name, name)
|
|
883
|
+
}
|
|
884
|
+
if (!object) return null
|
|
307
885
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
886
|
+
const results = []
|
|
887
|
+
for (const atom of atoms(object, env)) {
|
|
888
|
+
if (isNullish(atom)) {
|
|
889
|
+
if (node.optional) results.push(UNDEFINED)
|
|
890
|
+
continue
|
|
311
891
|
}
|
|
312
|
-
const
|
|
313
|
-
const
|
|
314
|
-
|
|
892
|
+
const struct = structFor(atom, env)
|
|
893
|
+
const field = struct?.fields.get(name)
|
|
894
|
+
if (field) results.push(field.optional ? unionOf([field.type, UNDEFINED]) : field.type)
|
|
895
|
+
else if (struct?.methods.has(name)) results.push(FUNCTION)
|
|
896
|
+
else if (name === "length" && (isArrayLike(atom) || isNamed(atom, "string") || (atom.kind === "literal" && typeof atom.value === "string"))) results.push(INT)
|
|
897
|
+
else if (name === "size" && (atom.kind === "generic" || atom.kind === "named") && (atom.name === "Map" || atom.name === "Set")) results.push(INT)
|
|
898
|
+
else return null
|
|
315
899
|
}
|
|
900
|
+
return results.length ? unionOf(results) : null
|
|
901
|
+
}
|
|
316
902
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
903
|
+
function expectedCallback(call, index, env) {
|
|
904
|
+
const node = call.node
|
|
905
|
+
const callee = node.callee
|
|
906
|
+
let signature = null
|
|
907
|
+
let bound = null
|
|
908
|
+
|
|
909
|
+
if (t.isIdentifier(callee, { name: "__typed_variable__" }) && index === 0 && t.isStringLiteral(node.arguments[1])) {
|
|
910
|
+
const atom = single(readType(node.arguments[1].value), env)
|
|
911
|
+
return atom?.kind === "function" ? atom : null
|
|
912
|
+
}
|
|
913
|
+
if (t.isIdentifier(callee)) {
|
|
914
|
+
signature = signatureFor(callee.name, call, env)
|
|
915
|
+
} else if (t.isMemberExpression(callee) && !callee.computed && t.isIdentifier(callee.property)) {
|
|
916
|
+
const receiver = infer(callee.object, call, env)
|
|
917
|
+
const struct = receiver && structFor(receiver, env)
|
|
918
|
+
signature = struct?.methods.get(callee.property.name) ?? null
|
|
919
|
+
bound = struct?.bound ?? null
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
const parameter = signature?.parameters[index]
|
|
923
|
+
if (!parameter?.type) return null
|
|
924
|
+
const types = bindTypeParams(signature, node, call, env, bound, true)
|
|
925
|
+
const atom = single(substituteType(parameter.type, types), env)
|
|
926
|
+
return atom?.kind === "function" ? atom : null
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
const EVENT = "DOM Event"
|
|
930
|
+
const EVENT_TARGET = "DOM Event target"
|
|
931
|
+
|
|
932
|
+
function eventType(env) {
|
|
933
|
+
if (!env.structs.has(EVENT)) {
|
|
934
|
+
const field = type => ({ type, optional: false, readonly: true })
|
|
935
|
+
env.structs.set(EVENT_TARGET, {
|
|
936
|
+
fields: new Map([
|
|
937
|
+
["value", field(STRING)], ["checked", field(BOOL)], ["valueAsNumber", field(FLOAT)],
|
|
938
|
+
["id", field(STRING)], ["name", field(STRING)], ["tagName", field(STRING)]
|
|
939
|
+
]),
|
|
940
|
+
defaults: new Set(), methods: new Map(), parent: null, typeParams: [], open: true
|
|
941
|
+
})
|
|
942
|
+
env.structs.set(EVENT, {
|
|
943
|
+
fields: new Map([
|
|
944
|
+
["target", field(named(EVENT_TARGET))], ["currentTarget", field(named(EVENT_TARGET))],
|
|
945
|
+
["type", field(STRING)], ["key", field(STRING)], ["code", field(STRING)],
|
|
946
|
+
["clientX", field(FLOAT)], ["clientY", field(FLOAT)], ["button", field(INT)], ["deltaY", field(FLOAT)],
|
|
947
|
+
["shiftKey", field(BOOL)], ["ctrlKey", field(BOOL)], ["altKey", field(BOOL)], ["metaKey", field(BOOL)]
|
|
948
|
+
]),
|
|
949
|
+
defaults: new Set(),
|
|
950
|
+
methods: new Map([["preventDefault", null], ["stopPropagation", null]]),
|
|
951
|
+
parent: null, typeParams: [], open: true
|
|
952
|
+
})
|
|
953
|
+
}
|
|
954
|
+
return named(EVENT)
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
const eventHolesCache = new WeakMap()
|
|
958
|
+
|
|
959
|
+
function eventHoles(markup) {
|
|
960
|
+
let holes = eventHolesCache.get(markup)
|
|
961
|
+
if (holes) return holes
|
|
962
|
+
holes = new Set()
|
|
963
|
+
try {
|
|
964
|
+
const visit = node => {
|
|
965
|
+
if (node.type !== "element") return
|
|
966
|
+
if (!/^[A-Z]/.test(node.tag)) for (const attr of node.attrs) if (attr.event !== undefined) holes.add(attr.hole)
|
|
967
|
+
node.children.forEach(visit)
|
|
322
968
|
}
|
|
323
|
-
|
|
969
|
+
parseMarkup(markup._slimQuasis, message => { throw new Error(message) }).forEach(visit)
|
|
970
|
+
} catch {
|
|
971
|
+
}
|
|
972
|
+
eventHolesCache.set(markup, holes)
|
|
973
|
+
return holes
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
function contextualParam(binding, env) {
|
|
977
|
+
const param = binding.path
|
|
978
|
+
const fn = param.parentPath
|
|
979
|
+
if (!fn?.isFunction() || param.listKey !== "params" || !t.isIdentifier(param.node)) return null
|
|
980
|
+
|
|
981
|
+
const template = fn.parentPath
|
|
982
|
+
if (template?.isTemplateLiteral() && fn.listKey === "expressions" && param.key === 0) {
|
|
983
|
+
const markup = template.parentPath?.node
|
|
984
|
+
if (markup?._slimMarkup && markup._slimQuasis && eventHoles(markup).has(fn.key)) return eventType(env)
|
|
324
985
|
return null
|
|
325
986
|
}
|
|
326
987
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
if (["==", "!=", "===", "!==", "<", ">", "<=", ">=", "instanceof", "in"].includes(operator)) {
|
|
330
|
-
return "bool"
|
|
331
|
-
}
|
|
988
|
+
const call = fn.parentPath
|
|
989
|
+
if (!call?.isCallExpression() || fn.listKey !== "arguments") return null
|
|
332
990
|
|
|
333
|
-
|
|
334
|
-
|
|
991
|
+
const expected = expectedCallback(call, fn.key, env)
|
|
992
|
+
if (expected) return expected.params[param.key]?.type ?? null
|
|
335
993
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
994
|
+
if (fn.key !== 0) return null
|
|
995
|
+
const callee = call.node.callee
|
|
996
|
+
if (!t.isMemberExpression(callee) || callee.computed || !t.isIdentifier(callee.property)) return null
|
|
997
|
+
|
|
998
|
+
const receiver = infer(callee.object, call, env)
|
|
999
|
+
if (!receiver) return null
|
|
1000
|
+
|
|
1001
|
+
const method = callee.property.name
|
|
1002
|
+
const index = param.key
|
|
1003
|
+
const atom = single(receiver, env)
|
|
1004
|
+
|
|
1005
|
+
if (atom?.kind === "generic" && (atom.name === "Map" || atom.name === "Set") && method === "forEach") {
|
|
1006
|
+
const [first = ANY, second = first] = atom.args
|
|
1007
|
+
return atom.name === "Map" ? [second, first][index] ?? null : [first, first][index] ?? null
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
const element = elementType(receiver, env)
|
|
1011
|
+
if (!element) return null
|
|
1012
|
+
|
|
1013
|
+
if (CALLBACK_ELEMENT.has(method)) return [element, INT, arrayOf(element)][index] ?? null
|
|
1014
|
+
if (method === "sort" || method === "toSorted") return index < 2 ? element : null
|
|
1015
|
+
if (method === "reduce" || method === "reduceRight") {
|
|
1016
|
+
const initial = call.node.arguments[1]
|
|
1017
|
+
const accumulator = initial ? widen(infer(initial, call, env)) : element
|
|
1018
|
+
return [accumulator, element, INT][index] ?? null
|
|
1019
|
+
}
|
|
1020
|
+
return null
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
function declaredInitType(binding, env) {
|
|
1024
|
+
if (env.initTypes.has(binding)) return env.initTypes.get(binding)
|
|
1025
|
+
env.initTypes.set(binding, null)
|
|
1026
|
+
|
|
1027
|
+
let type = null
|
|
1028
|
+
const declarator = binding.path
|
|
1029
|
+
if (declarator.isVariableDeclarator() && t.isIdentifier(declarator.node.id)) {
|
|
1030
|
+
const { init } = declarator.node
|
|
1031
|
+
if (init && binding.constantViolations.length === 0) {
|
|
1032
|
+
type = infer(init, declarator, env)
|
|
1033
|
+
if (type && binding.kind !== "const") type = widen(type)
|
|
1034
|
+
} else if (!init) {
|
|
1035
|
+
const loop = declarator.parentPath.parentPath
|
|
1036
|
+
if (loop?.isForOfStatement() && declarator.parentPath.key === "left") {
|
|
1037
|
+
type = elementType(infer(loop.node.right, loop, env), env)
|
|
340
1038
|
}
|
|
341
|
-
return null
|
|
342
1039
|
}
|
|
1040
|
+
}
|
|
343
1041
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
1042
|
+
env.initTypes.set(binding, type)
|
|
1043
|
+
return type
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
function refinedDeclared(binding, declared, env) {
|
|
1047
|
+
if (binding.constantViolations.length > 0) return declared
|
|
1048
|
+
if (env.refinedTypes.has(binding)) return env.refinedTypes.get(binding)
|
|
1049
|
+
env.refinedTypes.set(binding, declared)
|
|
1050
|
+
|
|
1051
|
+
const declarator = binding.path
|
|
1052
|
+
let result = declared
|
|
1053
|
+
if (declarator.isVariableDeclarator() && t.isCallExpression(declarator.node.init) && typedVariableLabel(declarator.node.init)) {
|
|
1054
|
+
const actual = infer(declarator.node.init.arguments[0], declarator, env)
|
|
1055
|
+
if (actual && atoms(declared, env).length > 1) {
|
|
1056
|
+
const fitting = atoms(declared, env).filter(atom => accepts(atom, actual, env))
|
|
1057
|
+
if (fitting.length > 0) result = unionOf(fitting)
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
env.refinedTypes.set(binding, result)
|
|
1062
|
+
return result
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
function identifierType(node, path_, env) {
|
|
1066
|
+
const binding = path_.scope.getBinding(node.name)
|
|
1067
|
+
if (!binding) return null
|
|
1068
|
+
if (env.bindings.has(binding)) return refinedDeclared(binding, env.bindings.get(binding), env)
|
|
1069
|
+
if (binding.kind === "param") return contextualParam(binding, env)
|
|
1070
|
+
if (binding.kind === "const" || binding.kind === "let" || binding.kind === "var") return declaredInitType(binding, env)
|
|
1071
|
+
if (binding.kind === "hoisted" && binding.path.isFunctionDeclaration()) return functionTypeOf(binding.path.node, env)
|
|
1072
|
+
return null
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
function thisOwner(path_, env) {
|
|
1076
|
+
let fn = path_.getFunctionParent()
|
|
1077
|
+
while (fn && fn.isArrowFunctionExpression()) fn = fn.parentPath.getFunctionParent()
|
|
1078
|
+
return fn ? env.methodOwners.get(fn.node) ?? null : null
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
function falsyParts(type, env) {
|
|
1082
|
+
return atoms(type, env).filter(atom => {
|
|
1083
|
+
if (atom.kind === "literal") return !atom.value
|
|
1084
|
+
if (atom.kind !== "named") return false
|
|
1085
|
+
return ["null", "undefined", "bool", "int", "float", "number", "string", "any"].includes(atom.name) || isOpen(atom, env)
|
|
1086
|
+
})
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
function truthyParts(type, env) {
|
|
1090
|
+
return atoms(type, env).filter(atom => !isNullish(atom) && !(atom.kind === "literal" && !atom.value))
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
function inferNode(node, path_, env) {
|
|
1094
|
+
if (t.isNumericLiteral(node) && Number.isInteger(node.value) && /[.eE]/.test(node.extra?.raw ?? "")) return FLOAT
|
|
1095
|
+
if (t.isNumericLiteral(node) || t.isStringLiteral(node) || t.isBooleanLiteral(node)) return literal(node.value)
|
|
1096
|
+
if (t.isTemplateLiteral(node)) return STRING
|
|
1097
|
+
if (t.isNullLiteral(node)) return NULL
|
|
1098
|
+
if (t.isIdentifier(node, { name: "undefined" }) && !path_.scope.getBinding("undefined")) return UNDEFINED
|
|
1099
|
+
if (t.isObjectExpression(node)) return OBJECT
|
|
1100
|
+
if (t.isFunction(node)) return functionTypeOf(node, env)
|
|
1101
|
+
|
|
1102
|
+
if (t.isArrayExpression(node)) {
|
|
1103
|
+
if (node.elements.length === 0) return arrayOf(NULL)
|
|
1104
|
+
|
|
1105
|
+
const kinds = []
|
|
1106
|
+
for (const item of node.elements) {
|
|
1107
|
+
if (!item) return ARRAY
|
|
1108
|
+
const kind = t.isSpreadElement(item)
|
|
1109
|
+
? elementType(infer(item.argument, path_, env), env)
|
|
1110
|
+
: widen(infer(item, path_, env))
|
|
1111
|
+
if (!kind) return ARRAY
|
|
1112
|
+
kinds.push(kind)
|
|
1113
|
+
}
|
|
1114
|
+
return arrayOf(unionOf(kinds))
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
const structInstance = structLiteralName(node, env)
|
|
1118
|
+
if (structInstance) return named(structInstance)
|
|
1119
|
+
|
|
1120
|
+
if (t.isNewExpression(node) && t.isIdentifier(node.callee)) {
|
|
1121
|
+
const name = node.callee.name
|
|
1122
|
+
return env.classes.has(name) || BUILTIN_CLASSES.has(name) ? named(name) : null
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
if (t.isIdentifier(node)) return narrowed(node, path_, env, identifierType(node, path_, env))
|
|
1126
|
+
|
|
1127
|
+
if (t.isThisExpression(node)) {
|
|
1128
|
+
const owner = thisOwner(path_, env)
|
|
1129
|
+
return owner ? named(owner) : null
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) {
|
|
1133
|
+
return narrowed(node, path_, env, memberType(node, path_, env))
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
if (t.isUnaryExpression(node)) {
|
|
1137
|
+
switch (node.operator) {
|
|
1138
|
+
case "!": return BOOL
|
|
1139
|
+
case "typeof": return STRING
|
|
1140
|
+
case "void": return UNDEFINED
|
|
1141
|
+
case "~": return INT
|
|
1142
|
+
case "-": {
|
|
1143
|
+
const operand = widen(infer(node.argument, path_, env))
|
|
1144
|
+
return isNumeric(operand) ? operand : NUMBER
|
|
1145
|
+
}
|
|
1146
|
+
case "+": {
|
|
1147
|
+
const operand = widen(infer(node.argument, path_, env))
|
|
1148
|
+
return isNumeric(operand) ? operand : NUMBER
|
|
347
1149
|
}
|
|
348
|
-
return null
|
|
349
1150
|
}
|
|
1151
|
+
return null
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
if (t.isBinaryExpression(node)) {
|
|
1155
|
+
const operator = node.operator
|
|
1156
|
+
if (["==", "!=", "===", "!==", "<", ">", "<=", ">=", "instanceof", "in"].includes(operator)) return BOOL
|
|
1157
|
+
if (["&", "|", "^", "<<", ">>", ">>>"].includes(operator)) return INT
|
|
350
1158
|
|
|
351
|
-
|
|
1159
|
+
const left = widen(infer(node.left, path_, env))
|
|
1160
|
+
const right = widen(infer(node.right, path_, env))
|
|
1161
|
+
const bothNumeric = isNumeric(left) && isNumeric(right)
|
|
1162
|
+
const integer = isNamed(left, "int") && isNamed(right, "int")
|
|
1163
|
+
|
|
1164
|
+
if (operator === "+") {
|
|
1165
|
+
if (isNamed(left, "string") || isNamed(right, "string")) return STRING
|
|
1166
|
+
return bothNumeric ? (integer ? INT : FLOAT) : null
|
|
1167
|
+
}
|
|
1168
|
+
if (["-", "*", "%", "**"].includes(operator)) return bothNumeric ? (integer ? INT : FLOAT) : null
|
|
1169
|
+
if (operator === "/") return bothNumeric ? FLOAT : null
|
|
352
1170
|
return null
|
|
353
1171
|
}
|
|
354
1172
|
|
|
355
1173
|
if (t.isLogicalExpression(node)) {
|
|
356
|
-
|
|
1174
|
+
const left = infer(node.left, path_, env)
|
|
1175
|
+
if (!left) return null
|
|
1176
|
+
|
|
1177
|
+
if (node.operator === "??") {
|
|
1178
|
+
const right = infer(node.right, path_, env)
|
|
1179
|
+
const present = atoms(left, env).filter(atom => !isNullish(atom))
|
|
1180
|
+
return right ? unionOf([...present, right]) : null
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
const andAlso = node.operator === "&&"
|
|
1184
|
+
const right = withFacts(env, factsFrom(node.left, andAlso, path_, env), () => infer(node.right, path_, env))
|
|
1185
|
+
if (!right) return null
|
|
1186
|
+
const kept = andAlso ? falsyParts(left, env) : truthyParts(left, env)
|
|
1187
|
+
return unionOf([...kept, right])
|
|
357
1188
|
}
|
|
358
1189
|
|
|
359
1190
|
if (t.isConditionalExpression(node)) {
|
|
360
|
-
|
|
1191
|
+
const consequent = withFacts(env, factsFrom(node.test, true, path_, env), () => infer(node.consequent, path_, env))
|
|
1192
|
+
const alternate = withFacts(env, factsFrom(node.test, false, path_, env), () => infer(node.alternate, path_, env))
|
|
1193
|
+
return consequent && alternate ? unionOf([consequent, alternate]) : null
|
|
361
1194
|
}
|
|
362
1195
|
|
|
363
|
-
if (t.isAwaitExpression(node))
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
if (label) return label
|
|
368
|
-
|
|
369
|
-
return signatureFor(node.callee.name, path_, env)?.returns ?? null
|
|
1196
|
+
if (t.isAwaitExpression(node)) {
|
|
1197
|
+
const awaited = infer(node.argument, path_, env)
|
|
1198
|
+
const atom = awaited && single(awaited, env)
|
|
1199
|
+
return atom?.kind === "generic" && atom.name === "Promise" ? atom.args[0] ?? null : awaited
|
|
370
1200
|
}
|
|
371
1201
|
|
|
1202
|
+
if (t.isSequenceExpression(node)) return infer(node.expressions.at(-1), path_, env)
|
|
1203
|
+
if (t.isAssignmentExpression(node, { operator: "=" })) return infer(node.right, path_, env)
|
|
1204
|
+
if (t.isCallExpression(node) || t.isOptionalCallExpression(node)) return callType(node, path_, env)
|
|
1205
|
+
|
|
372
1206
|
return null
|
|
373
1207
|
}
|
|
374
1208
|
|
|
1209
|
+
export function infer(node, path_, env) {
|
|
1210
|
+
if (!node || env.inferring.has(node)) return null
|
|
1211
|
+
env.inferring.add(node)
|
|
1212
|
+
try {
|
|
1213
|
+
return inferNode(node, path_, env)
|
|
1214
|
+
} finally {
|
|
1215
|
+
env.inferring.delete(node)
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
|
|
375
1220
|
function declaredReturn(fn) {
|
|
376
1221
|
if (!t.isBlockStatement(fn.body)) return null
|
|
377
1222
|
|
|
@@ -380,7 +1225,7 @@ function declaredReturn(fn) {
|
|
|
380
1225
|
const call = statement.expression
|
|
381
1226
|
if (!t.isCallExpression(call)) continue
|
|
382
1227
|
if (!t.isIdentifier(call.callee, { name: "__declare_return__" })) continue
|
|
383
|
-
if (t.isStringLiteral(call.arguments[0])) return call.arguments[0].value
|
|
1228
|
+
if (t.isStringLiteral(call.arguments[0])) return readType(call.arguments[0].value)
|
|
384
1229
|
}
|
|
385
1230
|
return null
|
|
386
1231
|
}
|
|
@@ -398,27 +1243,31 @@ function collectParameters(fn) {
|
|
|
398
1243
|
const [value, label, , optional] = call.arguments
|
|
399
1244
|
if (!t.isIdentifier(value) || !t.isStringLiteral(label)) continue
|
|
400
1245
|
declared.set(value.name, {
|
|
401
|
-
type: label.value,
|
|
1246
|
+
type: readType(label.value),
|
|
402
1247
|
optional: t.isBooleanLiteral(optional) ? optional.value : false
|
|
403
1248
|
})
|
|
404
1249
|
}
|
|
405
1250
|
}
|
|
406
1251
|
|
|
1252
|
+
const generic = fn._slimSignature
|
|
1253
|
+
const genericType = index => generic?.params[index] ? readType(generic.params[index]) : null
|
|
1254
|
+
|
|
407
1255
|
let rest = false
|
|
408
|
-
const parameters = fn.params.map(param => {
|
|
1256
|
+
const parameters = fn.params.map((param, index) => {
|
|
409
1257
|
if (t.isRestElement(param)) { rest = true; return { name: null, type: null, required: false } }
|
|
410
1258
|
if (t.isAssignmentPattern(param) && t.isIdentifier(param.left)) {
|
|
411
|
-
return { name: param.left.name, type: declared.get(param.left.name)?.type ?? null, required: false }
|
|
1259
|
+
return { name: param.left.name, type: genericType(index) ?? declared.get(param.left.name)?.type ?? null, required: false }
|
|
412
1260
|
}
|
|
413
1261
|
if (t.isIdentifier(param)) {
|
|
414
1262
|
const info = declared.get(param.name)
|
|
415
|
-
return { name: param.name, type: info?.type ?? null, required: !info?.optional }
|
|
1263
|
+
return { name: param.name, type: genericType(index) ?? info?.type ?? null, required: !info?.optional && !generic?.optional[index] }
|
|
416
1264
|
}
|
|
417
1265
|
return { name: null, type: null, required: false }
|
|
418
1266
|
})
|
|
419
1267
|
|
|
420
|
-
const returns = declaredReturn(fn)
|
|
421
|
-
|
|
1268
|
+
const returns = generic?.returns ? readType(generic.returns) : declaredReturn(fn)
|
|
1269
|
+
const typed = declared.size > 0 || !!returns || parameters.some(parameter => parameter.type)
|
|
1270
|
+
return { parameters, rest, returns, async: !!fn.async, typed, node: fn, typeParams: generic?.typeParams ?? [] }
|
|
422
1271
|
}
|
|
423
1272
|
|
|
424
1273
|
function functionName(path_) {
|
|
@@ -437,6 +1286,88 @@ function functionName(path_) {
|
|
|
437
1286
|
return "function"
|
|
438
1287
|
}
|
|
439
1288
|
|
|
1289
|
+
function collectStruct(init, env) {
|
|
1290
|
+
const [nameArg, schema] = init.arguments
|
|
1291
|
+
const fields = new Map()
|
|
1292
|
+
for (const property of schema.properties) {
|
|
1293
|
+
if (!t.isObjectProperty(property) || !t.isStringLiteral(property.value)) continue
|
|
1294
|
+
const key = propertyKey(property)
|
|
1295
|
+
if (key === null) continue
|
|
1296
|
+
const optional = key.startsWith("*")
|
|
1297
|
+
fields.set(optional ? key.slice(1).trim() : key, { type: readType(property.value.value), optional, readonly: false })
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
if (t.isArrayExpression(init.arguments[7])) {
|
|
1301
|
+
for (const element of init.arguments[7].elements) {
|
|
1302
|
+
if (t.isStringLiteral(element) && fields.has(element.value)) fields.get(element.value).readonly = true
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
const typeParams = t.isArrayExpression(init.arguments[6])
|
|
1307
|
+
? init.arguments[6].elements.filter(element => t.isStringLiteral(element)).map(element => element.value)
|
|
1308
|
+
: []
|
|
1309
|
+
|
|
1310
|
+
const defaults = new Set()
|
|
1311
|
+
if (t.isObjectExpression(init.arguments[3])) {
|
|
1312
|
+
for (const property of init.arguments[3].properties) {
|
|
1313
|
+
if (!t.isObjectProperty(property)) continue
|
|
1314
|
+
const key = propertyKey(property)
|
|
1315
|
+
if (key !== null) defaults.add(key)
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
const methods = new Map()
|
|
1320
|
+
if (t.isObjectExpression(init.arguments[5])) {
|
|
1321
|
+
for (const property of init.arguments[5].properties) {
|
|
1322
|
+
if (!t.isObjectProperty(property)) continue
|
|
1323
|
+
const key = propertyKey(property)
|
|
1324
|
+
if (key === null) continue
|
|
1325
|
+
const isFunction = t.isFunction(property.value)
|
|
1326
|
+
methods.set(key, isFunction ? collectParameters(property.value) : null)
|
|
1327
|
+
if (isFunction) env.methodOwners.set(property.value, nameArg.value)
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
const parent = t.isStringLiteral(init.arguments[4]) ? init.arguments[4].value : null
|
|
1332
|
+
env.structs.set(nameArg.value, { fields, defaults, methods, parent, typeParams })
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
function componentSignature(name, props, env) {
|
|
1336
|
+
let type
|
|
1337
|
+
let required = false
|
|
1338
|
+
if (props.type) {
|
|
1339
|
+
type = readType(props.type)
|
|
1340
|
+
required = true
|
|
1341
|
+
} else {
|
|
1342
|
+
const key = `${name} props`
|
|
1343
|
+
const fields = new Map(props.fields.map(field =>
|
|
1344
|
+
[field.name, { type: field.type ? readType(field.type) : ANY, optional: field.optional, readonly: true }]))
|
|
1345
|
+
env.structs.set(key, { fields, defaults: new Set(), methods: new Map(), parent: null, typeParams: [], component: name })
|
|
1346
|
+
type = named(key)
|
|
1347
|
+
required = props.fields.some(field => !field.optional)
|
|
1348
|
+
}
|
|
1349
|
+
return {
|
|
1350
|
+
parameters: [{ name: "props", type, required }],
|
|
1351
|
+
rest: false, returns: null, async: false, typed: true, node: null, typeParams: []
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
function collectEnum(init, env) {
|
|
1356
|
+
const [nameArg, schema] = init.arguments
|
|
1357
|
+
const members = new Set()
|
|
1358
|
+
const valueTypes = new Set()
|
|
1359
|
+
for (const property of schema.properties) {
|
|
1360
|
+
if (!t.isObjectProperty(property)) continue
|
|
1361
|
+
const key = propertyKey(property)
|
|
1362
|
+
if (key !== null) members.add(key)
|
|
1363
|
+
if (t.isStringLiteral(property.value)) valueTypes.add("string")
|
|
1364
|
+
else if (t.isNumericLiteral(property.value)) {
|
|
1365
|
+
valueTypes.add(Number.isInteger(property.value.value) ? "int" : "float")
|
|
1366
|
+
} else valueTypes.add("any")
|
|
1367
|
+
}
|
|
1368
|
+
env.enums.set(nameArg.value, { members, valueTypes })
|
|
1369
|
+
}
|
|
1370
|
+
|
|
440
1371
|
function collect(ast, env) {
|
|
441
1372
|
traverse(ast, {
|
|
442
1373
|
ClassDeclaration(path_) {
|
|
@@ -448,83 +1379,40 @@ function collect(ast, env) {
|
|
|
448
1379
|
if (!t.isIdentifier(id)) return
|
|
449
1380
|
|
|
450
1381
|
const binding = path_.scope.getBinding(id.name)
|
|
1382
|
+
if (path_.node._slimType && binding) {
|
|
1383
|
+
env.bindings.set(binding, readType(path_.node._slimType))
|
|
1384
|
+
return
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
if (init?._slimComponentProps && binding) {
|
|
1388
|
+
const signature = componentSignature(id.name, init._slimComponentProps, env)
|
|
1389
|
+
env.functions.set(binding, signature)
|
|
1390
|
+
if (binding.scope.path.isProgram()) env.functionsByName.set(id.name, signature)
|
|
1391
|
+
return
|
|
1392
|
+
}
|
|
451
1393
|
|
|
452
1394
|
if (t.isCallExpression(init) && t.isIdentifier(init.callee)) {
|
|
453
1395
|
const name = init.callee.name
|
|
454
1396
|
const [nameArg, schema] = init.arguments
|
|
455
1397
|
|
|
456
1398
|
if (name === "__def_struct__" && t.isStringLiteral(nameArg) && t.isObjectExpression(schema)) {
|
|
457
|
-
|
|
458
|
-
for (const property of schema.properties) {
|
|
459
|
-
if (!t.isObjectProperty(property) || !t.isStringLiteral(property.value)) continue
|
|
460
|
-
const key = t.isStringLiteral(property.key) ? property.key.value
|
|
461
|
-
: t.isIdentifier(property.key) ? property.key.name : null
|
|
462
|
-
if (key === null) continue
|
|
463
|
-
const optional = key.startsWith("*")
|
|
464
|
-
fields.set(optional ? key.slice(1).trim() : key, {
|
|
465
|
-
type: property.value.value,
|
|
466
|
-
optional
|
|
467
|
-
})
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
const defaults = new Set()
|
|
471
|
-
if (t.isObjectExpression(init.arguments[3])) {
|
|
472
|
-
for (const property of init.arguments[3].properties) {
|
|
473
|
-
if (!t.isObjectProperty(property)) continue
|
|
474
|
-
const key = t.isStringLiteral(property.key) ? property.key.value
|
|
475
|
-
: t.isIdentifier(property.key) ? property.key.name : null
|
|
476
|
-
if (key !== null) defaults.add(key)
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
const methods = new Set()
|
|
481
|
-
if (t.isObjectExpression(init.arguments[5])) {
|
|
482
|
-
for (const property of init.arguments[5].properties) {
|
|
483
|
-
if (!t.isObjectProperty(property)) continue
|
|
484
|
-
const key = t.isStringLiteral(property.key) ? property.key.value
|
|
485
|
-
: t.isIdentifier(property.key) ? property.key.name : null
|
|
486
|
-
if (key === null) continue
|
|
487
|
-
methods.add(key)
|
|
488
|
-
if (t.isFunction(property.value)) {
|
|
489
|
-
env.methodOwners.set(property.value, nameArg.value)
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
const parent = t.isStringLiteral(init.arguments[4]) ? init.arguments[4].value : null
|
|
495
|
-
env.structs.set(nameArg.value, { fields, defaults, methods, parent })
|
|
1399
|
+
collectStruct(init, env)
|
|
496
1400
|
return
|
|
497
1401
|
}
|
|
498
|
-
|
|
499
1402
|
if (name === "__def_enum__" && t.isStringLiteral(nameArg) && t.isObjectExpression(schema)) {
|
|
500
|
-
|
|
501
|
-
const valueTypes = new Set()
|
|
502
|
-
for (const property of schema.properties) {
|
|
503
|
-
if (!t.isObjectProperty(property)) continue
|
|
504
|
-
const key = t.isStringLiteral(property.key) ? property.key.value
|
|
505
|
-
: t.isIdentifier(property.key) ? property.key.name : null
|
|
506
|
-
if (key !== null) members.add(key)
|
|
507
|
-
if (t.isStringLiteral(property.value)) valueTypes.add("string")
|
|
508
|
-
else if (t.isNumericLiteral(property.value)) {
|
|
509
|
-
valueTypes.add(Number.isInteger(property.value.value) ? "int" : "float")
|
|
510
|
-
} else valueTypes.add("any")
|
|
511
|
-
}
|
|
512
|
-
env.enums.set(nameArg.value, { members, valueTypes })
|
|
1403
|
+
collectEnum(init, env)
|
|
513
1404
|
return
|
|
514
1405
|
}
|
|
515
|
-
|
|
516
1406
|
if (name === "__type_def__" && t.isStringLiteral(nameArg)) {
|
|
517
|
-
env.
|
|
1407
|
+
if (t.isStringLiteral(schema)) env.aliases.set(nameArg.value, readType(schema.value))
|
|
1408
|
+
else env.customTypes.add(nameArg.value)
|
|
518
1409
|
return
|
|
519
1410
|
}
|
|
520
1411
|
|
|
521
1412
|
const label = typedVariableLabel(init)
|
|
522
|
-
if (label && binding) env.bindings.set(binding, label)
|
|
1413
|
+
if (label && binding) env.bindings.set(binding, readType(label))
|
|
523
1414
|
}
|
|
524
1415
|
|
|
525
|
-
const instance = structLiteralLabel(init, env)
|
|
526
|
-
if (instance && binding) env.bindings.set(binding, instance)
|
|
527
|
-
|
|
528
1416
|
if ((t.isArrowFunctionExpression(init) || t.isFunctionExpression(init)) && binding) {
|
|
529
1417
|
const signature = collectParameters(init)
|
|
530
1418
|
env.functions.set(binding, signature)
|
|
@@ -544,6 +1432,8 @@ function collect(ast, env) {
|
|
|
544
1432
|
},
|
|
545
1433
|
|
|
546
1434
|
Function(path_) {
|
|
1435
|
+
env.functionPaths.set(path_.node, path_)
|
|
1436
|
+
|
|
547
1437
|
const { parameters, returns } = collectParameters(path_.node)
|
|
548
1438
|
for (const parameter of parameters) {
|
|
549
1439
|
if (!parameter.name || !parameter.type) continue
|
|
@@ -552,44 +1442,43 @@ function collect(ast, env) {
|
|
|
552
1442
|
}
|
|
553
1443
|
|
|
554
1444
|
if (returns) {
|
|
555
|
-
env.returns.set(path_.node, {
|
|
1445
|
+
env.returns.set(path_.node, { type: returns, name: functionName(path_) })
|
|
556
1446
|
}
|
|
557
1447
|
}
|
|
558
1448
|
})
|
|
559
1449
|
}
|
|
560
1450
|
|
|
561
|
-
function describe(label) {
|
|
562
|
-
return label.includes("|") || label.includes("&") ? `(${label})` : label
|
|
563
|
-
}
|
|
564
1451
|
|
|
565
|
-
function checkStructLiteral(
|
|
566
|
-
const definition =
|
|
1452
|
+
function checkStructLiteral(target, node, path_, env, report, requireAll = true) {
|
|
1453
|
+
const definition = structFor(target, env)
|
|
567
1454
|
if (!definition) return
|
|
1455
|
+
const name = definition.component ?? definition.label
|
|
1456
|
+
const noun = definition.component ? "prop" : "field"
|
|
1457
|
+
const subject = key => definition.component ? `prop "${key}" of "${name}"` : `"${name}.${key}"`
|
|
568
1458
|
|
|
569
1459
|
const seen = new Set()
|
|
570
1460
|
for (const property of node.properties) {
|
|
571
|
-
if (!t.isObjectProperty(property)) return
|
|
572
|
-
const key =
|
|
573
|
-
: t.isStringLiteral(property.key) ? property.key.value : null
|
|
1461
|
+
if (!t.isObjectProperty(property) && !t.isObjectMethod(property)) return
|
|
1462
|
+
const key = propertyKey(property)
|
|
574
1463
|
if (key === null) return
|
|
575
1464
|
|
|
576
1465
|
seen.add(key)
|
|
577
1466
|
const field = definition.fields.get(key)
|
|
578
1467
|
if (!field) {
|
|
579
|
-
report(property, `"${
|
|
1468
|
+
report(property, `"${name}" has no ${noun} "${key}"${didYouMean(key, definition.fields.keys())}`)
|
|
580
1469
|
continue
|
|
581
1470
|
}
|
|
1471
|
+
if (t.isObjectMethod(property)) continue
|
|
582
1472
|
|
|
583
|
-
const
|
|
584
|
-
|
|
585
|
-
report(property.value,
|
|
586
|
-
`"${structName}.${key}" expects ${describe(field.type)}, got ${describe(value)}`)
|
|
587
|
-
}
|
|
1473
|
+
const expected = field.optional ? unionOf([field.type, UNDEFINED]) : field.type
|
|
1474
|
+
checkValue(expected, property.value, path_, env, report, subject(key))
|
|
588
1475
|
}
|
|
589
1476
|
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
1477
|
+
if (!requireAll) return
|
|
1478
|
+
|
|
1479
|
+
for (const [field, info] of definition.fields) {
|
|
1480
|
+
if (info.optional || seen.has(field) || definition.defaults.has(field)) continue
|
|
1481
|
+
report(node, `"${name}" is missing ${noun} "${field}" of type ${describe(info.type)}`)
|
|
593
1482
|
}
|
|
594
1483
|
}
|
|
595
1484
|
|
|
@@ -602,8 +1491,10 @@ function matchExpression(node) {
|
|
|
602
1491
|
if (!t.isBlockStatement(callee.body)) return null
|
|
603
1492
|
|
|
604
1493
|
const covered = []
|
|
1494
|
+
const types = []
|
|
605
1495
|
let wildcard = false
|
|
606
1496
|
let guarded = false
|
|
1497
|
+
let fallback = null
|
|
607
1498
|
|
|
608
1499
|
for (const statement of callee.body.body) {
|
|
609
1500
|
if (t.isBlockStatement(statement)) { guarded = true; continue }
|
|
@@ -612,57 +1503,451 @@ function matchExpression(node) {
|
|
|
612
1503
|
const test = statement.test
|
|
613
1504
|
if (t.isCallExpression(test) && t.isIdentifier(test.callee, { name: "__match_eq__" })) {
|
|
614
1505
|
covered.push(test.arguments[1])
|
|
1506
|
+
} else if (t.isCallExpression(test) && t.isIdentifier(test.callee, { name: "__match_type__" }) && t.isStringLiteral(test.arguments[1])) {
|
|
1507
|
+
const body = statement.consequent.body ?? []
|
|
1508
|
+
if (!t.isIfStatement(body.at(-1))) types.push(readType(test.arguments[1].value))
|
|
615
1509
|
} else guarded = true
|
|
616
1510
|
continue
|
|
617
1511
|
}
|
|
618
1512
|
|
|
619
1513
|
if (t.isReturnStatement(statement)) {
|
|
620
1514
|
wildcard = !t.isIdentifier(statement.argument, { name: "undefined" })
|
|
1515
|
+
if (!wildcard) fallback = statement
|
|
621
1516
|
}
|
|
622
1517
|
}
|
|
623
1518
|
|
|
624
|
-
return { covered, wildcard, guarded, scrutinee: node.arguments[0] }
|
|
1519
|
+
return { covered, types, wildcard, guarded, fallback, scrutinee: node.arguments[0] }
|
|
625
1520
|
}
|
|
626
1521
|
|
|
627
|
-
function
|
|
628
|
-
if (match.guarded
|
|
1522
|
+
function missingCases(match, path_, env) {
|
|
1523
|
+
if (match.guarded) return null
|
|
629
1524
|
|
|
630
|
-
const
|
|
631
|
-
|
|
1525
|
+
const type = infer(match.scrutinee, path_, env)
|
|
1526
|
+
const all = type ? atoms(type, env) : []
|
|
1527
|
+
if (all.length === 0) return null
|
|
632
1528
|
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
1529
|
+
if (match.types.length > 0) {
|
|
1530
|
+
if (match.covered.length > 0) return null
|
|
1531
|
+
const missing = all.filter(atom => !match.types.some(covered => accepts(covered, atom, env)))
|
|
1532
|
+
return { subject: describe(type), missing: missing.map(formatType) }
|
|
1533
|
+
}
|
|
636
1534
|
|
|
637
|
-
const
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
1535
|
+
const enumName = all.length === 1 && (all[0].kind === "enumMember" || env.enums.has(all[0].name)) ? all[0].name : null
|
|
1536
|
+
if (enumName && env.enums.has(enumName)) {
|
|
1537
|
+
const covered = new Set()
|
|
1538
|
+
for (const arm of match.covered) {
|
|
1539
|
+
if (!t.isMemberExpression(arm) || arm.computed) return null
|
|
1540
|
+
if (!t.isIdentifier(arm.object, { name: enumName })) return null
|
|
1541
|
+
if (!t.isIdentifier(arm.property)) return null
|
|
1542
|
+
covered.add(arm.property.name)
|
|
1543
|
+
}
|
|
1544
|
+
const missing = [...env.enums.get(enumName).members].filter(member => !covered.has(member))
|
|
1545
|
+
return { subject: `"${enumName}"`, missing: missing.map(member => `${enumName}.${member}`) }
|
|
643
1546
|
}
|
|
644
1547
|
|
|
645
|
-
|
|
646
|
-
|
|
1548
|
+
if (all.length > 1 && all.every(atom => atom.kind === "literal")) {
|
|
1549
|
+
if (!match.covered.every(isLiteralNode)) return null
|
|
1550
|
+
const covered = new Set(match.covered.map(arm => arm.value))
|
|
1551
|
+
const missing = all.filter(atom => !covered.has(atom.value)).map(formatType)
|
|
1552
|
+
return { subject: describe(type), missing }
|
|
1553
|
+
}
|
|
1554
|
+
return null
|
|
1555
|
+
}
|
|
647
1556
|
|
|
648
|
-
|
|
1557
|
+
function checkExhaustive(node, match, path_, env, report) {
|
|
1558
|
+
if (match.wildcard) return
|
|
1559
|
+
|
|
1560
|
+
const result = missingCases(match, path_, env)
|
|
1561
|
+
if (!result || result.missing.length === 0) return
|
|
1562
|
+
|
|
1563
|
+
const { subject, missing } = result
|
|
1564
|
+
report(node, `match on ${subject} does not handle ${missing.join(", ")}` +
|
|
649
1565
|
` — add the missing case${missing.length === 1 ? "" : "s"} or a "_" fallback`)
|
|
650
1566
|
}
|
|
651
1567
|
|
|
652
|
-
function
|
|
653
|
-
|
|
654
|
-
|
|
1568
|
+
function hasLiterals(type, env) {
|
|
1569
|
+
return atoms(type, env).some(atom => atom.kind === "literal")
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
function arrayLiteralAsTuple(target, node, path_, env) {
|
|
1573
|
+
if (!t.isArrayExpression(node) || !atoms(target, env).some(atom => atom.kind === "tuple")) return null
|
|
1574
|
+
const items = []
|
|
1575
|
+
for (const element of node.elements) {
|
|
1576
|
+
if (!element || t.isSpreadElement(element)) return null
|
|
1577
|
+
const type = infer(element, path_, env)
|
|
1578
|
+
if (!type) return null
|
|
1579
|
+
items.push(type)
|
|
1580
|
+
}
|
|
1581
|
+
return tupleOf(items)
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1584
|
+
function checkValue(target, node, path_, env, report, describeTarget, phrase = "expects") {
|
|
1585
|
+
if (t.isCallExpression(node) && t.isIdentifier(node.callee, { name: "__as_struct__" })) node = node.arguments[0]
|
|
1586
|
+
|
|
1587
|
+
if (t.isObjectExpression(node) && structFor(target, env)) {
|
|
1588
|
+
checkStructLiteral(target, node, path_, env, report)
|
|
1589
|
+
return
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
const list = single(target, env)
|
|
1593
|
+
if (t.isArrayExpression(node) && list?.kind === "array" && structFor(list.element, env)) {
|
|
1594
|
+
node.elements.forEach((element, index) => {
|
|
1595
|
+
if (element && !t.isSpreadElement(element)) checkValue(list.element, element, path_, env, report, `${describeTarget}[${index}]`, phrase)
|
|
1596
|
+
})
|
|
1597
|
+
return
|
|
1598
|
+
}
|
|
1599
|
+
|
|
1600
|
+
const actual = arrayLiteralAsTuple(target, node, path_, env) ?? infer(node, path_, env)
|
|
1601
|
+
if (accepts(target, actual, env)) return
|
|
1602
|
+
|
|
1603
|
+
const shown = hasLiterals(target, env) ? actual : widen(actual)
|
|
1604
|
+
const present = atoms(actual, env).filter(atom => !isNullish(atom))
|
|
1605
|
+
const hint = present.length && present.length < atoms(actual, env).length && accepts(target, unionOf(present), env)
|
|
1606
|
+
? ` — check it for ${atoms(actual, env).some(atom => isNamed(atom, "null")) ? "null" : "undefined"} first`
|
|
1607
|
+
: ""
|
|
1608
|
+
report(node, `${describeTarget} ${phrase} ${describe(target)}, got ${describe(shown)}${hint}`)
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
function checkCall(signature, node, name, path_, env, report, bound = null) {
|
|
1612
|
+
if (!signature || !signature.typed) return
|
|
1613
|
+
if (node.arguments.some(argument => t.isSpreadElement(argument))) return
|
|
1614
|
+
|
|
1615
|
+
const required = signature.parameters.filter(parameter => parameter.required).length
|
|
1616
|
+
|
|
1617
|
+
if (node.arguments.length < required) {
|
|
1618
|
+
report(node, `"${name}" expects ${required} argument${required === 1 ? "" : "s"}, got ${node.arguments.length}`)
|
|
1619
|
+
return
|
|
1620
|
+
}
|
|
1621
|
+
if (!signature.rest && node.arguments.length > signature.parameters.length) {
|
|
1622
|
+
report(node,
|
|
1623
|
+
`"${name}" takes ${signature.parameters.length} argument${signature.parameters.length === 1 ? "" : "s"}, got ${node.arguments.length}`)
|
|
1624
|
+
return
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
const types = bindTypeParams(signature, node, path_, env, bound)
|
|
1628
|
+
node.arguments.forEach((argument, index) => {
|
|
1629
|
+
const parameter = signature.parameters[index]
|
|
1630
|
+
if (!parameter?.type) return
|
|
1631
|
+
const expected = types.size ? substituteType(parameter.type, types) : parameter.type
|
|
1632
|
+
checkValue(expected, argument, path_, env, report, `argument "${parameter.name}" of "${name}"`)
|
|
1633
|
+
})
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
function unwrapHandledPromise(node) {
|
|
1637
|
+
if (t.isCallExpression(node) && t.isMemberExpression(node.callee) &&
|
|
1638
|
+
t.isIdentifier(node.callee.property, { name: "catch" }) &&
|
|
1639
|
+
t.isIdentifier(node.arguments[0], { name: "__handle_async_error__" })) {
|
|
1640
|
+
return node.callee.object
|
|
1641
|
+
}
|
|
1642
|
+
return node
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1645
|
+
function calleeName(call) {
|
|
1646
|
+
const callee = call.callee
|
|
1647
|
+
if (t.isIdentifier(callee)) return callee.name
|
|
1648
|
+
if (t.isMemberExpression(callee) && t.isIdentifier(callee.property)) return callee.property.name
|
|
1649
|
+
return "function"
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
function calleeSignature(call, path_, env) {
|
|
1653
|
+
if (!t.isCallExpression(call)) return null
|
|
1654
|
+
const callee = call.callee
|
|
1655
|
+
if (t.isIdentifier(callee)) return signatureFor(callee.name, path_, env)
|
|
1656
|
+
if (t.isMemberExpression(callee) && !callee.computed && t.isIdentifier(callee.property)) {
|
|
1657
|
+
return structFor(infer(callee.object, path_, env), env)?.methods.get(callee.property.name) ?? null
|
|
1658
|
+
}
|
|
1659
|
+
return null
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
const IGNORES_PROMISES = new Set(["forEach", "filter", "some", "every", "find", "findIndex", "findLast", "findLastIndex"])
|
|
1663
|
+
|
|
1664
|
+
function checkAsyncCallback(node, path_, env, report) {
|
|
1665
|
+
const callee = node.callee
|
|
1666
|
+
if (!t.isMemberExpression(callee) || callee.computed || !t.isIdentifier(callee.property)) return
|
|
1667
|
+
|
|
1668
|
+
const method = callee.property.name
|
|
1669
|
+
if (!IGNORES_PROMISES.has(method)) return
|
|
1670
|
+
|
|
1671
|
+
const callback = node.arguments[0]
|
|
1672
|
+
const isAsync = t.isFunction(callback)
|
|
1673
|
+
? callback.async
|
|
1674
|
+
: t.isIdentifier(callback) && signatureFor(callback.name, path_, env)?.async
|
|
1675
|
+
if (!isAsync) return
|
|
1676
|
+
|
|
1677
|
+
report(callback, method === "forEach"
|
|
1678
|
+
? `forEach doesn't wait for async callbacks: use for...of with await, or await Promise.all(items.map(...))`
|
|
1679
|
+
: `${method} doesn't wait for async callbacks: it gets a promise, which is always truthy`)
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
const memberNames = definition => [...definition.fields.keys(), ...definition.methods.keys()]
|
|
1683
|
+
|
|
1684
|
+
function prototypeNames(value, own = []) {
|
|
1685
|
+
const names = new Set(own)
|
|
1686
|
+
for (let proto = Object.getPrototypeOf(value); proto; proto = Object.getPrototypeOf(proto)) {
|
|
1687
|
+
for (const name of Object.getOwnPropertyNames(proto)) names.add(name)
|
|
1688
|
+
}
|
|
1689
|
+
return names
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
const BUILTIN_MEMBERS = {
|
|
1693
|
+
string: prototypeNames("", ["length"]),
|
|
1694
|
+
number: prototypeNames(0),
|
|
1695
|
+
bool: prototypeNames(true),
|
|
1696
|
+
array: prototypeNames([], ["length"]),
|
|
1697
|
+
Map: prototypeNames(new Map()),
|
|
1698
|
+
Set: prototypeNames(new Set()),
|
|
1699
|
+
Date: prototypeNames(new Date()),
|
|
1700
|
+
RegExp: prototypeNames(/x/, ["lastIndex"]),
|
|
1701
|
+
Promise: prototypeNames(Promise.resolve()),
|
|
1702
|
+
Error: prototypeNames(new Error(), ["stack", "cause"])
|
|
1703
|
+
}
|
|
1704
|
+
|
|
1705
|
+
function builtinMembers(atom) {
|
|
1706
|
+
if (isArrayLike(atom)) return BUILTIN_MEMBERS.array
|
|
1707
|
+
const name = atom.kind === "literal" ? literalBase(atom).name : atom.kind === "named" || atom.kind === "generic" ? atom.name : null
|
|
1708
|
+
if (NUMERIC.has(name)) return BUILTIN_MEMBERS.number
|
|
1709
|
+
return Object.hasOwn(BUILTIN_MEMBERS, name) ? BUILTIN_MEMBERS[name] : null
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
function checkBuiltinMember(node, type, env, report) {
|
|
1713
|
+
const present = atoms(type, env).filter(atom => !isNullish(atom))
|
|
1714
|
+
const sets = present.map(builtinMembers)
|
|
1715
|
+
if (present.length === 0 || sets.some(set => !set)) return false
|
|
1716
|
+
|
|
1717
|
+
const member = node.property.name
|
|
1718
|
+
if (sets.every(set => set.has(member))) return true
|
|
1719
|
+
|
|
1720
|
+
const missing = sets.find(set => !set.has(member))
|
|
1721
|
+
report(node.property, `${describe(widen(unionOf(present)))} has no property "${member}"${didYouMean(member, missing)}`)
|
|
1722
|
+
return true
|
|
1723
|
+
}
|
|
1724
|
+
|
|
1725
|
+
const ENUM_API = new Set(["from", "has", "keys", "values", "scheme", "name", "type"])
|
|
1726
|
+
|
|
1727
|
+
function checkEnumMember(node, path_, env, report) {
|
|
1728
|
+
if (!t.isIdentifier(node.object)) return false
|
|
1729
|
+
const definition = env.enums.get(node.object.name)
|
|
1730
|
+
const binding = path_.scope.getBinding(node.object.name)
|
|
1731
|
+
if (!definition || (binding && !isDefinedBy(binding, "__def_enum__"))) return false
|
|
1732
|
+
|
|
1733
|
+
const member = node.property.name
|
|
1734
|
+
if (!definition.members.has(member) && !ENUM_API.has(member)) {
|
|
1735
|
+
report(node.property, `"${node.object.name}" has no member "${member}"${didYouMean(member, definition.members)}`)
|
|
1736
|
+
}
|
|
1737
|
+
return true
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
function isDefinedBy(binding, runtimeCall) {
|
|
1741
|
+
const init = binding.path.isVariableDeclarator() ? binding.path.node.init : null
|
|
1742
|
+
return t.isCallExpression(init) && t.isIdentifier(init.callee, { name: runtimeCall })
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
function visibleNames(path_, env) {
|
|
1746
|
+
const names = new Set([...env.importedLocals, ...env.structs.keys(), ...env.enums.keys()])
|
|
1747
|
+
for (let scope = path_.scope; scope; scope = scope.parent) {
|
|
1748
|
+
for (const name of Object.keys(scope.bindings)) names.add(name)
|
|
1749
|
+
}
|
|
1750
|
+
return names
|
|
1751
|
+
}
|
|
1752
|
+
|
|
1753
|
+
function checkUndefinedName(path_, env, report) {
|
|
1754
|
+
if (env.openGlobals || !path_.isReferencedIdentifier()) return
|
|
1755
|
+
const { name } = path_.node
|
|
1756
|
+
if (name.startsWith("__") || path_.scope.getBinding(name) || env.importedLocals.has(name)) return
|
|
1757
|
+
if (env.structs.has(name) || env.enums.has(name) || env.aliases.has(name) || env.customTypes.has(name)) return
|
|
1758
|
+
if (isKnownGlobal(name) || t.isUnaryExpression(path_.parent, { operator: "typeof" })) return
|
|
1759
|
+
|
|
1760
|
+
const suggestion = didYouMean(name, visibleNames(path_, env))
|
|
1761
|
+
if (suggestion) report(path_.node, `"${name}" is not defined${suggestion}`)
|
|
1762
|
+
}
|
|
1763
|
+
|
|
1764
|
+
function typeNames(type, found = []) {
|
|
1765
|
+
switch (type.kind) {
|
|
1766
|
+
case "named":
|
|
1767
|
+
case "enumMember":
|
|
1768
|
+
found.push(type.name)
|
|
1769
|
+
break
|
|
1770
|
+
case "generic":
|
|
1771
|
+
found.push(type.name)
|
|
1772
|
+
type.args.forEach(arg => typeNames(arg, found))
|
|
1773
|
+
break
|
|
1774
|
+
case "array":
|
|
1775
|
+
typeNames(type.element, found)
|
|
1776
|
+
break
|
|
1777
|
+
case "function":
|
|
1778
|
+
type.params.forEach(param => typeNames(param.type, found))
|
|
1779
|
+
typeNames(type.returns, found)
|
|
1780
|
+
break
|
|
1781
|
+
case "tuple":
|
|
1782
|
+
case "union":
|
|
1783
|
+
case "intersection":
|
|
1784
|
+
(type.items ?? type.types).forEach(part => typeNames(part, found))
|
|
1785
|
+
}
|
|
1786
|
+
return found
|
|
1787
|
+
}
|
|
1788
|
+
|
|
1789
|
+
function checkTypeNames(type, node, path_, env, report, typeParams = []) {
|
|
1790
|
+
if (env.openGlobals) return
|
|
1791
|
+
const known = name => KNOWN.has(name) || BUILTIN_CLASSES.has(name) || typeParams.includes(name) ||
|
|
1792
|
+
env.structs.has(name) || env.enums.has(name) || env.aliases.has(name) || env.customTypes.has(name) ||
|
|
1793
|
+
env.importedLocals.has(name) || !!path_.scope.getBinding(name) || isKnownGlobal(name)
|
|
1794
|
+
const candidates = () => [
|
|
1795
|
+
...KNOWN, ...BUILTIN_CLASSES, ...typeParams, ...env.structs.keys(), ...env.enums.keys(),
|
|
1796
|
+
...env.aliases.keys(), ...env.customTypes, ...env.importedLocals
|
|
1797
|
+
]
|
|
1798
|
+
|
|
1799
|
+
for (const name of new Set(typeNames(type))) {
|
|
1800
|
+
if (known(name)) continue
|
|
1801
|
+
const suggestion = didYouMean(name, candidates())
|
|
1802
|
+
if (suggestion) report(node, `unknown type "${name}"${suggestion}`)
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1806
|
+
function checkDeclaredTypes(path_, env, report) {
|
|
1807
|
+
const call = path_.node
|
|
1808
|
+
if (!t.isIdentifier(call.callee)) return
|
|
1809
|
+
const fn = path_.getFunctionParent()
|
|
1810
|
+
const [first, second] = call.arguments
|
|
1811
|
+
|
|
1812
|
+
if (call.callee.name === "__typed_parameter__" && t.isIdentifier(first) && t.isStringLiteral(second)) {
|
|
1813
|
+
const param = fn?.node.params.find(p => (t.isAssignmentPattern(p) ? p.left : p)?.name === first.name)
|
|
1814
|
+
checkTypeNames(readType(second.value), param ?? fn?.node ?? call, path_, env, report)
|
|
1815
|
+
} else if (call.callee.name === "__declare_return__" && t.isStringLiteral(first)) {
|
|
1816
|
+
checkTypeNames(readType(first.value), fn?.node ?? call, path_, env, report)
|
|
1817
|
+
} else if (call.callee.name === "__def_struct__" && t.isObjectExpression(second)) {
|
|
1818
|
+
const typeParams = t.isArrayExpression(call.arguments[6])
|
|
1819
|
+
? call.arguments[6].elements.filter(element => t.isStringLiteral(element)).map(element => element.value)
|
|
1820
|
+
: []
|
|
1821
|
+
for (const property of second.properties) {
|
|
1822
|
+
if (t.isObjectProperty(property) && t.isStringLiteral(property.value)) {
|
|
1823
|
+
checkTypeNames(readType(property.value.value), call, path_, env, report, typeParams)
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
|
|
1830
|
+
const DOM_EVENTS = new Set([
|
|
1831
|
+
"click", "dblclick", "mousedown", "mouseup", "mousemove", "mouseenter", "mouseleave", "mouseover", "mouseout",
|
|
1832
|
+
"contextmenu", "wheel", "keydown", "keyup", "keypress", "input", "change", "submit", "reset", "focus", "blur",
|
|
1833
|
+
"focusin", "focusout", "pointerdown", "pointerup", "pointermove", "pointerenter", "pointerleave", "pointercancel",
|
|
1834
|
+
"pointerover", "pointerout", "touchstart", "touchend", "touchmove", "touchcancel", "drag", "dragstart", "dragend",
|
|
1835
|
+
"dragenter", "dragleave", "dragover", "drop", "scroll", "scrollend", "resize", "load", "error", "abort", "select",
|
|
1836
|
+
"copy", "cut", "paste", "animationstart", "animationend", "animationiteration", "transitionstart",
|
|
1837
|
+
"transitionend", "transitionrun", "transitioncancel", "play", "pause", "ended", "timeupdate", "volumechange",
|
|
1838
|
+
"loadeddata", "loadedmetadata", "canplay", "canplaythrough", "seeking", "seeked", "waiting", "playing",
|
|
1839
|
+
"progress", "stalled", "suspend", "emptied", "ratechange", "durationchange", "invalid", "toggle", "beforeinput",
|
|
1840
|
+
"compositionstart", "compositionend", "compositionupdate", "gotpointercapture", "lostpointercapture",
|
|
1841
|
+
"selectionchange", "slotchange", "close", "cancel", "beforetoggle"
|
|
1842
|
+
])
|
|
1843
|
+
|
|
1844
|
+
const HTML_ATTRIBUTES = new Set([
|
|
1845
|
+
"accept", "accept-charset", "accesskey", "action", "align", "allow", "alt", "async", "autocapitalize",
|
|
1846
|
+
"autocomplete", "autofocus", "autoplay", "background", "capture", "charset", "checked", "cite", "class", "color",
|
|
1847
|
+
"cols", "colspan", "content", "contenteditable", "controls", "coords", "crossorigin", "data", "datetime",
|
|
1848
|
+
"decoding", "default", "defer", "dir", "dirname", "disabled", "download", "draggable", "enctype",
|
|
1849
|
+
"enterkeyhint", "for", "form", "formaction", "formenctype", "formmethod", "formnovalidate", "formtarget",
|
|
1850
|
+
"headers", "height", "hidden", "high", "href", "hreflang", "http-equiv", "id", "inert", "inputmode",
|
|
1851
|
+
"integrity", "is", "ismap", "itemprop", "kind", "label", "lang", "list", "loading", "loop", "low", "max",
|
|
1852
|
+
"maxlength", "media", "method", "min", "minlength", "multiple", "muted", "name", "nonce", "novalidate", "open",
|
|
1853
|
+
"optimum", "pattern", "ping", "placeholder", "playsinline", "popover", "popovertarget", "poster", "preload",
|
|
1854
|
+
"readonly", "referrerpolicy", "rel", "required", "reversed", "role", "rows", "rowspan", "sandbox", "scope",
|
|
1855
|
+
"selected", "shape", "size", "sizes", "slot", "span", "spellcheck", "src", "srcdoc", "srclang", "srcset",
|
|
1856
|
+
"start", "step", "style", "tabindex", "target", "title", "translate", "type", "usemap", "value", "width", "wrap",
|
|
1857
|
+
"xmlns", "key", "enter", "leave", "move"
|
|
1858
|
+
])
|
|
1859
|
+
|
|
1860
|
+
function markupOffsets(node, code) {
|
|
1861
|
+
const quasis = node._slimQuasis
|
|
1862
|
+
const expressions = node.quasi.expressions
|
|
1863
|
+
if (typeof node.start !== "number" || !code) return null
|
|
1864
|
+
const segments = []
|
|
1865
|
+
let joined = 0
|
|
1866
|
+
for (let i = 0; i < quasis.length; i++) {
|
|
1867
|
+
let start = node.start
|
|
1868
|
+
if (i > 0) {
|
|
1869
|
+
const end = expressions[i - 1]?.end
|
|
1870
|
+
if (typeof end !== "number") return null
|
|
1871
|
+
start = code.indexOf("}", end) + 1
|
|
1872
|
+
}
|
|
1873
|
+
segments.push([joined, start])
|
|
1874
|
+
joined += quasis[i].length + (i < quasis.length - 1 ? holeMark(i).length : 0)
|
|
1875
|
+
}
|
|
1876
|
+
return offset => {
|
|
1877
|
+
let found = segments[0]
|
|
1878
|
+
for (const segment of segments) if (segment[0] <= offset) found = segment
|
|
1879
|
+
return found[1] + offset - found[0]
|
|
1880
|
+
}
|
|
1881
|
+
}
|
|
1882
|
+
|
|
1883
|
+
const camel = name => name[0].toUpperCase() + name.slice(1)
|
|
1884
|
+
|
|
1885
|
+
function checkComponentTag(element, at, path_, env, report) {
|
|
1886
|
+
const name = element.tag.split(".").pop()
|
|
1887
|
+
if (element.tag.includes(".")) return
|
|
1888
|
+
const signature = signatureFor(name, path_, env)
|
|
1889
|
+
const definition = signature?.parameters[0]?.type && structFor(signature.parameters[0].type, env)
|
|
1890
|
+
if (!definition?.component && !definition) return
|
|
1891
|
+
const shown = definition.component ?? definition.label
|
|
1892
|
+
const given = new Set()
|
|
1893
|
+
for (const attr of element.attrs) {
|
|
1894
|
+
if (attr.spread !== undefined) return
|
|
1895
|
+
if (attr.at === undefined || attr.name === "key") continue
|
|
1896
|
+
given.add(attr.name)
|
|
1897
|
+
if (!definition.fields.has(attr.name)) {
|
|
1898
|
+
report({ start: at(attr.at) }, `"${shown}" has no prop "${attr.name}"${didYouMean(attr.name, definition.fields.keys())}`)
|
|
1899
|
+
}
|
|
1900
|
+
}
|
|
1901
|
+
if (element.children.some(child => !(child.type === "text" && child.value.trim() === ""))) given.add("children")
|
|
1902
|
+
for (const [field, info] of definition.fields) {
|
|
1903
|
+
if (info.optional || given.has(field) || definition.defaults.has(field)) continue
|
|
1904
|
+
report({ start: at(element.at ?? 0) }, `"${shown}" is missing prop "${field}" of type ${describe(info.type)}`)
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
|
|
1908
|
+
function checkMarkup(node, path_, env, report) {
|
|
1909
|
+
const at = markupOffsets(node, env.code)
|
|
1910
|
+
if (!at) return
|
|
1911
|
+
let tree
|
|
1912
|
+
try {
|
|
1913
|
+
tree = parseMarkup(node._slimQuasis, message => { throw new Error(message) })
|
|
1914
|
+
} catch {
|
|
655
1915
|
return
|
|
656
1916
|
}
|
|
657
1917
|
|
|
658
|
-
const
|
|
659
|
-
|
|
660
|
-
|
|
1918
|
+
const visit = (element, svg) => {
|
|
1919
|
+
if (element.type !== "element") return
|
|
1920
|
+
if (/^[A-Z]/.test(element.tag)) {
|
|
1921
|
+
checkComponentTag(element, at, path_, env, report)
|
|
1922
|
+
for (const child of element.children) visit(child, svg)
|
|
1923
|
+
return
|
|
1924
|
+
}
|
|
1925
|
+
const inSvg = svg || element.tag === "svg"
|
|
1926
|
+
const custom = element.tag.includes("-")
|
|
1927
|
+
for (const attr of element.attrs) {
|
|
1928
|
+
if (attr.at === undefined) continue
|
|
1929
|
+
if (attr.event !== undefined) {
|
|
1930
|
+
if (DOM_EVENTS.has(attr.event)) continue
|
|
1931
|
+
const match = closest(attr.event, DOM_EVENTS)
|
|
1932
|
+
if (match) report({ start: at(attr.at) }, `unknown event "${attr.name}" — did you mean "on${camel(match)}"?`)
|
|
1933
|
+
continue
|
|
1934
|
+
}
|
|
1935
|
+
const name = attr.name.toLowerCase()
|
|
1936
|
+
if (inSvg || custom || HTML_ATTRIBUTES.has(name) || /[-:]/.test(name) || name.startsWith("on")) continue
|
|
1937
|
+
const match = closest(name, HTML_ATTRIBUTES)
|
|
1938
|
+
if (match) report({ start: at(attr.at) }, `unknown attribute "${attr.name}" on <${element.tag}> — did you mean "${match}"?`)
|
|
1939
|
+
}
|
|
1940
|
+
for (const child of element.children) visit(child, inSvg && element.tag !== "foreignObject")
|
|
661
1941
|
}
|
|
1942
|
+
for (const root of tree) visit(root, false)
|
|
662
1943
|
}
|
|
663
1944
|
|
|
664
1945
|
function verify(ast, env, report) {
|
|
665
1946
|
traverse(ast, {
|
|
1947
|
+
TaggedTemplateExpression(path_) {
|
|
1948
|
+
if (path_.node._slimMarkup && path_.node._slimQuasis) checkMarkup(path_.node, path_, env, report)
|
|
1949
|
+
},
|
|
1950
|
+
|
|
666
1951
|
VariableDeclarator(path_) {
|
|
667
1952
|
const { id, init } = path_.node
|
|
668
1953
|
if (!t.isIdentifier(id) || !t.isCallExpression(init)) return
|
|
@@ -670,77 +1955,81 @@ function verify(ast, env, report) {
|
|
|
670
1955
|
const label = typedVariableLabel(init)
|
|
671
1956
|
if (!label) return
|
|
672
1957
|
|
|
673
|
-
|
|
1958
|
+
checkTypeNames(readType(label), id, path_, env, report)
|
|
1959
|
+
checkValue(readType(label), init.arguments[0], path_, env, report, `"${id.name}"`)
|
|
674
1960
|
},
|
|
675
1961
|
|
|
676
1962
|
AssignmentExpression(path_) {
|
|
677
1963
|
const { node } = path_
|
|
678
|
-
if (
|
|
1964
|
+
if (node.operator !== "=") return
|
|
1965
|
+
|
|
1966
|
+
const target = node.left
|
|
1967
|
+
if (t.isMemberExpression(target) && !target.computed && t.isIdentifier(target.property)) {
|
|
1968
|
+
const definition = structFor(infer(target.object, path_, env), env)
|
|
1969
|
+
if (!definition) return
|
|
1970
|
+
|
|
1971
|
+
const owner = definition.label
|
|
1972
|
+
const name = target.property.name
|
|
1973
|
+
const field = definition.fields.get(name)
|
|
1974
|
+
if (!field) {
|
|
1975
|
+
if (!definition.methods.has(name) && !definition.open) report(target.property, `"${owner}" has no field "${name}"${didYouMean(name, memberNames(definition))}`)
|
|
1976
|
+
return
|
|
1977
|
+
}
|
|
1978
|
+
if (field.readonly) {
|
|
1979
|
+
report(target, `"${owner}.${name}" is readonly`)
|
|
1980
|
+
return
|
|
1981
|
+
}
|
|
1982
|
+
|
|
1983
|
+
const expected = field.optional ? unionOf([field.type, UNDEFINED]) : field.type
|
|
1984
|
+
checkValue(expected, node.right, path_, env, report, `"${owner}.${name}"`)
|
|
1985
|
+
return
|
|
1986
|
+
}
|
|
1987
|
+
|
|
1988
|
+
if (!t.isIdentifier(node.left)) return
|
|
679
1989
|
|
|
680
1990
|
const binding = path_.scope.getBinding(node.left.name)
|
|
681
|
-
const
|
|
682
|
-
if (!
|
|
1991
|
+
const declared = binding ? env.bindings.get(binding) : null
|
|
1992
|
+
if (!declared) return
|
|
1993
|
+
|
|
1994
|
+
checkValue(declared, node.right, path_, env, report, `"${node.left.name}"`)
|
|
1995
|
+
},
|
|
1996
|
+
|
|
1997
|
+
ExpressionStatement(path_) {
|
|
1998
|
+
if (!path_.getFunctionParent()?.node.async) return
|
|
1999
|
+
|
|
2000
|
+
const call = unwrapHandledPromise(path_.node.expression)
|
|
2001
|
+
if (!calleeSignature(call, path_, env)?.async) return
|
|
683
2002
|
|
|
684
|
-
|
|
2003
|
+
const name = calleeName(call)
|
|
2004
|
+
report(call, `"${name}" is async and nothing awaits it: add await, or write "void ${name}(...)" to run it in the background`)
|
|
685
2005
|
},
|
|
686
2006
|
|
|
687
2007
|
CallExpression(path_) {
|
|
688
2008
|
const { node } = path_
|
|
689
2009
|
|
|
2010
|
+
checkDeclaredTypes(path_, env, report)
|
|
690
2011
|
const match = matchExpression(node)
|
|
691
2012
|
if (match) checkExhaustive(node, match, path_, env, report)
|
|
692
2013
|
|
|
693
|
-
|
|
694
|
-
if (constructed && t.isObjectExpression(node.arguments[0])) {
|
|
695
|
-
const definition = resolveStruct(constructed, env)
|
|
696
|
-
if (!definition) return
|
|
2014
|
+
checkAsyncCallback(node, path_, env, report)
|
|
697
2015
|
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
: t.isStringLiteral(property.key) ? property.key.value : null
|
|
702
|
-
if (key === null) return
|
|
703
|
-
|
|
704
|
-
const field = definition.fields.get(key)
|
|
705
|
-
if (!field) {
|
|
706
|
-
report(property, `"${constructed}" has no field "${key}"`)
|
|
707
|
-
continue
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
const value = infer(property.value, path_, env)
|
|
711
|
-
if (!accepts(field.type, value, env)) {
|
|
712
|
-
report(property.value,
|
|
713
|
-
`"${constructed}.${key}" expects ${describe(field.type)}, got ${describe(value)}`)
|
|
714
|
-
}
|
|
715
|
-
}
|
|
2016
|
+
const constructed = structLiteralName(node, env)
|
|
2017
|
+
if (constructed && t.isObjectExpression(node.arguments[0])) {
|
|
2018
|
+
checkStructLiteral(named(constructed), node.arguments[0], path_, env, report, false)
|
|
716
2019
|
return
|
|
717
2020
|
}
|
|
718
2021
|
|
|
719
|
-
if (
|
|
720
|
-
|
|
721
|
-
const signature = signatureFor(node.callee.name, path_, env)
|
|
722
|
-
if (!signature || !signature.typed) return
|
|
723
|
-
if (node.arguments.some(argument => t.isSpreadElement(argument))) return
|
|
724
|
-
|
|
725
|
-
const name = node.callee.name
|
|
726
|
-
const required = signature.parameters.filter(parameter => parameter.required).length
|
|
727
|
-
|
|
728
|
-
if (node.arguments.length < required) {
|
|
729
|
-
report(node, `"${name}" expects ${required} argument${required === 1 ? "" : "s"}, got ${node.arguments.length}`)
|
|
730
|
-
return
|
|
731
|
-
}
|
|
732
|
-
if (!signature.rest && node.arguments.length > signature.parameters.length) {
|
|
733
|
-
report(node,
|
|
734
|
-
`"${name}" takes ${signature.parameters.length} argument${signature.parameters.length === 1 ? "" : "s"}, got ${node.arguments.length}`)
|
|
2022
|
+
if (t.isIdentifier(node.callee)) {
|
|
2023
|
+
checkCall(signatureFor(node.callee.name, path_, env), node, node.callee.name, path_, env, report)
|
|
735
2024
|
return
|
|
736
2025
|
}
|
|
737
2026
|
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
})
|
|
2027
|
+
const callee = node.callee
|
|
2028
|
+
if (!t.isMemberExpression(callee) || callee.computed || !t.isIdentifier(callee.property)) return
|
|
2029
|
+
|
|
2030
|
+
const definition = structFor(infer(callee.object, path_, env), env)
|
|
2031
|
+
const method = definition?.methods.get(callee.property.name)
|
|
2032
|
+
if (method) checkCall(method, node, `${definition.label}.${callee.property.name}`, path_, env, report, definition.bound)
|
|
744
2033
|
},
|
|
745
2034
|
|
|
746
2035
|
ReturnStatement(path_) {
|
|
@@ -750,34 +2039,42 @@ function verify(ast, env, report) {
|
|
|
750
2039
|
|
|
751
2040
|
const value = path_.node.argument
|
|
752
2041
|
if (!value) {
|
|
753
|
-
if (!accepts(declared.
|
|
754
|
-
report(path_.node,
|
|
755
|
-
`"${declared.name}" must return ${describe(declared.label)}, got undefined`)
|
|
2042
|
+
if (!accepts(declared.type, UNDEFINED, env)) {
|
|
2043
|
+
report(path_.node, `"${declared.name}" must return ${describe(declared.type)}, got undefined`)
|
|
756
2044
|
}
|
|
757
2045
|
return
|
|
758
2046
|
}
|
|
759
2047
|
|
|
760
|
-
checkValue(declared.
|
|
2048
|
+
checkValue(declared.type, value, path_, env, report, `"${declared.name}"`, "must return")
|
|
761
2049
|
},
|
|
762
2050
|
|
|
763
2051
|
MemberExpression(path_) {
|
|
764
2052
|
const { node } = path_
|
|
765
2053
|
if (node.computed || !t.isIdentifier(node.property)) return
|
|
766
|
-
if (!t.isIdentifier(node.object) && !t.isThisExpression(node.object)) return
|
|
767
2054
|
if (t.isAssignmentExpression(path_.parent) && path_.parent.left === node) return
|
|
768
2055
|
|
|
769
|
-
|
|
770
|
-
|
|
2056
|
+
if (checkEnumMember(node, path_, env, report)) return
|
|
2057
|
+
|
|
2058
|
+
const objectType = infer(node.object, path_, env)
|
|
2059
|
+
if (!objectType || checkBuiltinMember(node, objectType, env, report)) return
|
|
2060
|
+
|
|
2061
|
+
const present = atoms(objectType, env).filter(atom => !isNullish(atom))
|
|
2062
|
+
const definition = present.length === 1 ? structFor(present[0], env) : null
|
|
771
2063
|
if (!definition) return
|
|
772
2064
|
|
|
773
2065
|
const field = node.property.name
|
|
774
|
-
if (definition.fields.has(field) || definition.methods.has(field)) return
|
|
2066
|
+
if (definition.fields.has(field) || definition.methods.has(field) || definition.open) return
|
|
2067
|
+
|
|
2068
|
+
report(node.property, `"${definition.label}" has no field "${field}"${didYouMean(field, memberNames(definition))}`)
|
|
2069
|
+
},
|
|
775
2070
|
|
|
776
|
-
|
|
2071
|
+
Identifier(path_) {
|
|
2072
|
+
checkUndefinedName(path_, env, report)
|
|
777
2073
|
}
|
|
778
2074
|
})
|
|
779
2075
|
}
|
|
780
2076
|
|
|
2077
|
+
|
|
781
2078
|
function exportedNames(ast) {
|
|
782
2079
|
const names = new Map()
|
|
783
2080
|
|
|
@@ -806,28 +2103,38 @@ function exportedNames(ast) {
|
|
|
806
2103
|
return names
|
|
807
2104
|
}
|
|
808
2105
|
|
|
2106
|
+
function settle(signature, env) {
|
|
2107
|
+
if (!signature || signature.returns || "inferred" in signature) return signature
|
|
2108
|
+
return { ...signature, inferred: inferReturn(signature.node, env) }
|
|
2109
|
+
}
|
|
2110
|
+
|
|
809
2111
|
function buildModuleTypes(ast, env) {
|
|
810
2112
|
const record = {
|
|
811
2113
|
structs: new Map(),
|
|
812
2114
|
enums: new Map(),
|
|
813
2115
|
customTypes: new Set(),
|
|
814
2116
|
functions: new Map(),
|
|
815
|
-
|
|
2117
|
+
aliases: new Map(),
|
|
2118
|
+
ambient: { structs: new Map(), enums: new Map(), customTypes: new Set(), aliases: new Map() }
|
|
816
2119
|
}
|
|
817
2120
|
|
|
818
2121
|
for (const name of env.structs.keys()) {
|
|
819
2122
|
const resolved = resolveStruct(name, env)
|
|
820
|
-
if (resolved)
|
|
2123
|
+
if (!resolved) continue
|
|
2124
|
+
const methods = new Map([...resolved.methods].map(([method, signature]) => [method, settle(signature, env)]))
|
|
2125
|
+
record.ambient.structs.set(name, { ...resolved, methods })
|
|
821
2126
|
}
|
|
822
2127
|
for (const [name, definition] of env.enums) record.ambient.enums.set(name, definition)
|
|
823
2128
|
for (const name of env.customTypes) record.ambient.customTypes.add(name)
|
|
2129
|
+
for (const [name, type] of env.aliases) record.ambient.aliases.set(name, type)
|
|
824
2130
|
|
|
825
2131
|
for (const [exported, local] of exportedNames(ast)) {
|
|
826
2132
|
const struct = record.ambient.structs.get(local)
|
|
827
2133
|
if (struct) record.structs.set(exported, struct)
|
|
828
2134
|
if (env.enums.has(local)) record.enums.set(exported, env.enums.get(local))
|
|
829
2135
|
if (env.customTypes.has(local)) record.customTypes.add(exported)
|
|
830
|
-
if (env.
|
|
2136
|
+
if (env.aliases.has(local)) record.aliases.set(exported, env.aliases.get(local))
|
|
2137
|
+
if (env.functionsByName.has(local)) record.functions.set(exported, settle(env.functionsByName.get(local), env))
|
|
831
2138
|
}
|
|
832
2139
|
|
|
833
2140
|
return record
|
|
@@ -838,6 +2145,7 @@ function seedImports(env, imports, wildcards) {
|
|
|
838
2145
|
if (record.structs.has(name)) env.structs.set(local, record.structs.get(name))
|
|
839
2146
|
if (record.enums.has(name)) env.enums.set(local, record.enums.get(name))
|
|
840
2147
|
if (record.customTypes.has(name)) env.customTypes.add(local)
|
|
2148
|
+
if (record.aliases.has(name)) env.aliases.set(local, record.aliases.get(name))
|
|
841
2149
|
if (record.functions.has(name)) env.importedFunctions.set(local, record.functions.get(name))
|
|
842
2150
|
}
|
|
843
2151
|
|
|
@@ -854,6 +2162,9 @@ function seedImports(env, imports, wildcards) {
|
|
|
854
2162
|
if (!env.enums.has(name)) env.enums.set(name, definition)
|
|
855
2163
|
}
|
|
856
2164
|
for (const name of record.ambient.customTypes) env.customTypes.add(name)
|
|
2165
|
+
for (const [name, type] of record.ambient.aliases) {
|
|
2166
|
+
if (!env.aliases.has(name)) env.aliases.set(name, type)
|
|
2167
|
+
}
|
|
857
2168
|
}
|
|
858
2169
|
|
|
859
2170
|
for (const source of wildcards) {
|
|
@@ -862,6 +2173,7 @@ function seedImports(env, imports, wildcards) {
|
|
|
862
2173
|
for (const name of record.structs.keys()) merge(record, name, name)
|
|
863
2174
|
for (const name of record.enums.keys()) merge(record, name, name)
|
|
864
2175
|
for (const name of record.customTypes) merge(record, name, name)
|
|
2176
|
+
for (const name of record.aliases.keys()) merge(record, name, name)
|
|
865
2177
|
for (const name of record.functions.keys()) merge(record, name, name)
|
|
866
2178
|
}
|
|
867
2179
|
|
|
@@ -871,31 +2183,36 @@ function seedImports(env, imports, wildcards) {
|
|
|
871
2183
|
}
|
|
872
2184
|
}
|
|
873
2185
|
|
|
874
|
-
export function checkTypes(ast, {
|
|
2186
|
+
export function checkTypes(ast, { originalCode, sourceFile, imports = [], wildcards = [], locals = [], openGlobals = false }) {
|
|
875
2187
|
const env = createEnvironment()
|
|
2188
|
+
env.code = originalCode
|
|
2189
|
+
env.importedLocals = new Set(locals)
|
|
2190
|
+
env.openGlobals = openGlobals
|
|
876
2191
|
const diagnostics = []
|
|
877
2192
|
const originalLines = computeLineStarts(originalCode)
|
|
878
2193
|
const sourceLines = originalCode.split("\n")
|
|
2194
|
+
const reported = new Set()
|
|
879
2195
|
|
|
880
2196
|
const report = (node, message) => {
|
|
881
2197
|
const offset = typeof node.start === "number" ? node.start : 0
|
|
882
|
-
const
|
|
883
|
-
|
|
2198
|
+
const key = `${offset}:${message}`
|
|
2199
|
+
if (reported.has(key)) return
|
|
2200
|
+
reported.add(key)
|
|
884
2201
|
|
|
2202
|
+
const location = offsetToLineCol(offset, originalLines)
|
|
885
2203
|
diagnostics.push({
|
|
886
2204
|
message,
|
|
887
2205
|
sourceFile,
|
|
888
|
-
line: location
|
|
889
|
-
column: location
|
|
890
|
-
sourceLine:
|
|
2206
|
+
line: location.line + 1,
|
|
2207
|
+
column: location.column + 1,
|
|
2208
|
+
sourceLine: (sourceLines[location.line] ?? "").replace(/\r$/, "")
|
|
891
2209
|
})
|
|
892
2210
|
}
|
|
893
2211
|
|
|
894
2212
|
seedImports(env, imports, wildcards)
|
|
895
2213
|
collect(ast, env)
|
|
896
|
-
moduleTypes.set(path.resolve(sourceFile), buildModuleTypes(ast, env))
|
|
897
|
-
|
|
898
2214
|
verify(ast, env, report)
|
|
2215
|
+
moduleTypes.set(path.resolve(sourceFile), buildModuleTypes(ast, env))
|
|
899
2216
|
|
|
900
2217
|
diagnostics.sort((a, b) => a.line - b.line || a.column - b.column)
|
|
901
2218
|
return diagnostics
|