arktype 2.0.0-dev.10 → 2.0.0-dev.11
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/{main.ts → out/api.d.ts} +6 -6
- package/out/api.js +5 -0
- package/out/ark.d.ts +1 -1
- package/out/config.d.ts +1 -0
- package/out/config.js +2 -0
- package/out/generic.d.ts +3 -3
- package/out/match.js +1 -1
- package/out/module.d.ts +4 -2
- package/out/parser/definition.d.ts +10 -8
- package/out/parser/definition.js +2 -2
- package/out/parser/objectLiteral.d.ts +8 -13
- package/out/parser/objectLiteral.js +20 -23
- package/out/parser/semantic/bounds.d.ts +3 -3
- package/out/parser/semantic/divisor.d.ts +2 -2
- package/out/parser/semantic/infer.d.ts +1 -1
- package/out/parser/semantic/validate.d.ts +6 -4
- package/out/parser/string/reduce/dynamic.d.ts +6 -6
- package/out/parser/string/reduce/shared.d.ts +16 -16
- package/out/parser/string/reduce/shared.js +4 -2
- package/out/parser/string/shift/operand/date.d.ts +2 -2
- package/out/parser/string/shift/operand/enclosed.d.ts +17 -3
- package/out/parser/string/shift/operand/enclosed.js +2 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
- package/out/parser/string/shift/operand/unenclosed.d.ts +8 -6
- package/out/parser/string/shift/operand/unenclosed.js +2 -2
- package/out/parser/string/shift/operator/bounds.d.ts +5 -5
- package/out/parser/string/shift/operator/bounds.js +3 -3
- package/out/parser/string/shift/operator/divisor.d.ts +1 -1
- package/out/parser/string/shift/operator/operator.d.ts +1 -1
- package/out/parser/string/string.d.ts +2 -2
- package/out/parser/tuple.d.ts +11 -11
- package/out/parser/tuple.js +3 -3
- package/out/scope.d.ts +13 -11
- package/out/scope.js +5 -5
- package/out/type.d.ts +4 -4
- package/out/type.js +2 -2
- package/package.json +8 -10
- package/__tests__/array.test.ts +0 -391
- package/__tests__/badDefinitionType.test.ts +0 -58
- package/__tests__/basis.test.ts +0 -50
- package/__tests__/bounds.test.ts +0 -318
- package/__tests__/cast.test.ts +0 -37
- package/__tests__/completions.test.ts +0 -51
- package/__tests__/config.test.ts +0 -96
- package/__tests__/dateLiteral.test.ts +0 -36
- package/__tests__/declared.test.ts +0 -124
- package/__tests__/discrimination.test.ts +0 -74
- package/__tests__/divisor.test.ts +0 -114
- package/__tests__/enclosed.test.ts +0 -107
- package/__tests__/examples.test.ts +0 -56
- package/__tests__/expressions.test.ts +0 -210
- package/__tests__/generateBenchData.ts +0 -89
- package/__tests__/generated/cyclic.ts +0 -3058
- package/__tests__/generic.test.ts +0 -313
- package/__tests__/group.test.ts +0 -85
- package/__tests__/hkt.test.ts +0 -22
- package/__tests__/imports.test.ts +0 -126
- package/__tests__/instanceof.test.ts +0 -104
- package/__tests__/intersection.test.ts +0 -108
- package/__tests__/keyTraversal.test.ts +0 -77
- package/__tests__/keyof.test.ts +0 -150
- package/__tests__/keywords.test.ts +0 -214
- package/__tests__/literal.test.ts +0 -90
- package/__tests__/match.bench.ts +0 -31
- package/__tests__/match.test.ts +0 -234
- package/__tests__/methods.bench.ts +0 -22
- package/__tests__/narrow.test.ts +0 -100
- package/__tests__/object.bench.ts +0 -28
- package/__tests__/objectLiteral.test.ts +0 -442
- package/__tests__/operand.bench.ts +0 -45
- package/__tests__/operator.bench.ts +0 -130
- package/__tests__/pipe.test.ts +0 -501
- package/__tests__/realWorld.test.ts +0 -63
- package/__tests__/regex.test.ts +0 -68
- package/__tests__/scope.bench.ts +0 -39
- package/__tests__/scope.test.ts +0 -374
- package/__tests__/serialization.test.ts +0 -19
- package/__tests__/string.test.ts +0 -52
- package/__tests__/submodule.test.ts +0 -86
- package/__tests__/this.test.ts +0 -83
- package/__tests__/thunk.test.ts +0 -86
- package/__tests__/traverse.test.ts +0 -141
- package/__tests__/tsGenerics.test.ts +0 -12
- package/__tests__/type.test.ts +0 -36
- package/__tests__/typeReference.test.ts +0 -39
- package/__tests__/unenclosed.test.ts +0 -136
- package/__tests__/union.test.ts +0 -214
- package/ark.ts +0 -47
- package/generic.ts +0 -69
- package/match.ts +0 -218
- package/module.ts +0 -15
- package/out/ark.js.map +0 -1
- package/out/generic.js.map +0 -1
- package/out/main.js.map +0 -1
- package/out/match.js.map +0 -1
- package/out/module.js.map +0 -1
- package/out/parser/definition.js.map +0 -1
- package/out/parser/generic.js.map +0 -1
- package/out/parser/objectLiteral.js.map +0 -1
- package/out/parser/semantic/bounds.js.map +0 -1
- package/out/parser/semantic/divisor.js.map +0 -1
- package/out/parser/semantic/infer.js.map +0 -1
- package/out/parser/semantic/utils.js.map +0 -1
- package/out/parser/semantic/validate.js.map +0 -1
- package/out/parser/string/reduce/dynamic.js.map +0 -1
- package/out/parser/string/reduce/shared.js.map +0 -1
- package/out/parser/string/reduce/static.js.map +0 -1
- package/out/parser/string/shift/operand/date.js.map +0 -1
- package/out/parser/string/shift/operand/enclosed.js.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
- package/out/parser/string/shift/operand/operand.js.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
- package/out/parser/string/shift/operator/bounds.js.map +0 -1
- package/out/parser/string/shift/operator/divisor.js.map +0 -1
- package/out/parser/string/shift/operator/operator.js.map +0 -1
- package/out/parser/string/shift/scanner.js.map +0 -1
- package/out/parser/string/string.js.map +0 -1
- package/out/parser/tuple.js.map +0 -1
- package/out/scope.js.map +0 -1
- package/out/type.js.map +0 -1
- package/parser/definition.ts +0 -143
- package/parser/generic.ts +0 -66
- package/parser/objectLiteral.ts +0 -233
- package/parser/semantic/bounds.ts +0 -62
- package/parser/semantic/divisor.ts +0 -11
- package/parser/semantic/infer.ts +0 -141
- package/parser/semantic/utils.ts +0 -15
- package/parser/semantic/validate.ts +0 -117
- package/parser/string/reduce/dynamic.ts +0 -208
- package/parser/string/reduce/shared.ts +0 -99
- package/parser/string/reduce/static.ts +0 -296
- package/parser/string/shift/operand/date.ts +0 -55
- package/parser/string/shift/operand/enclosed.ts +0 -123
- package/parser/string/shift/operand/genericArgs.ts +0 -128
- package/parser/string/shift/operand/operand.ts +0 -42
- package/parser/string/shift/operand/unenclosed.ts +0 -217
- package/parser/string/shift/operator/bounds.ts +0 -270
- package/parser/string/shift/operator/divisor.ts +0 -33
- package/parser/string/shift/operator/operator.ts +0 -71
- package/parser/string/shift/scanner.ts +0 -210
- package/parser/string/string.ts +0 -85
- package/parser/tuple.ts +0 -542
- package/scope.ts +0 -291
- package/type.ts +0 -266
package/parser/objectLiteral.ts
DELETED
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type NodeDef,
|
|
3
|
-
type RawSchema,
|
|
4
|
-
tsKeywords,
|
|
5
|
-
type writeInvalidPropertyKeyMessage
|
|
6
|
-
} from "@arktype/schema"
|
|
7
|
-
import {
|
|
8
|
-
type Dict,
|
|
9
|
-
type ErrorMessage,
|
|
10
|
-
type Key,
|
|
11
|
-
type merge,
|
|
12
|
-
printable,
|
|
13
|
-
type show,
|
|
14
|
-
spliterate,
|
|
15
|
-
stringAndSymbolicEntriesOf,
|
|
16
|
-
throwParseError
|
|
17
|
-
} from "@arktype/util"
|
|
18
|
-
import type { ParseContext } from "../scope.js"
|
|
19
|
-
import type { inferDefinition, validateDefinition } from "./definition.js"
|
|
20
|
-
import type { astToString } from "./semantic/utils.js"
|
|
21
|
-
import type { validateString } from "./semantic/validate.js"
|
|
22
|
-
import { Scanner } from "./string/shift/scanner.js"
|
|
23
|
-
|
|
24
|
-
export const parseObjectLiteral = (def: Dict, ctx: ParseContext): RawSchema => {
|
|
25
|
-
const propNodes: NodeDef<"prop">[] = []
|
|
26
|
-
const indexNodes: NodeDef<"index">[] = []
|
|
27
|
-
// We only allow a spread operator to be used as the first key in an object
|
|
28
|
-
// because to match JS behavior any keys before the spread are overwritten
|
|
29
|
-
// by the values in the target object, so there'd be no useful purpose in having it
|
|
30
|
-
// anywhere except for the beginning.
|
|
31
|
-
const parsedEntries = stringAndSymbolicEntriesOf(def).map(parseEntry)
|
|
32
|
-
if (parsedEntries[0]?.kind === "spread") {
|
|
33
|
-
// remove the spread entry so we can iterate over the remaining entries
|
|
34
|
-
// expecting non-spread entries
|
|
35
|
-
const spreadEntry = parsedEntries.shift()!
|
|
36
|
-
const spreadNode = ctx.$.parse(spreadEntry.value, ctx)
|
|
37
|
-
if (
|
|
38
|
-
!spreadNode.hasKind("intersection") ||
|
|
39
|
-
!spreadNode.extends(tsKeywords.object)
|
|
40
|
-
) {
|
|
41
|
-
return throwParseError(
|
|
42
|
-
writeInvalidSpreadTypeMessage(printable(spreadEntry.value))
|
|
43
|
-
)
|
|
44
|
-
}
|
|
45
|
-
// TODO: move to props group merge in schema
|
|
46
|
-
// For each key on spreadNode, add it to our object.
|
|
47
|
-
// We filter out keys from the spreadNode that will be defined later on this same object
|
|
48
|
-
// because the currently parsed definition will overwrite them.
|
|
49
|
-
spreadNode.prop?.forEach(
|
|
50
|
-
spreadRequired =>
|
|
51
|
-
!parsedEntries.some(
|
|
52
|
-
({ inner: innerKey }) => innerKey === spreadRequired.key
|
|
53
|
-
) && propNodes.push(spreadRequired)
|
|
54
|
-
)
|
|
55
|
-
}
|
|
56
|
-
for (const entry of parsedEntries) {
|
|
57
|
-
if (entry.kind === "spread") return throwParseError(nonLeadingSpreadError)
|
|
58
|
-
|
|
59
|
-
if (entry.kind === "index") {
|
|
60
|
-
// handle key parsing first to match type behavior
|
|
61
|
-
const key = ctx.$.parse(entry.inner, ctx)
|
|
62
|
-
const value = ctx.$.parse(entry.value, ctx)
|
|
63
|
-
|
|
64
|
-
// extract enumerable named props from the index signature
|
|
65
|
-
const [enumerable, nonEnumerable] = spliterate(key.branches, k =>
|
|
66
|
-
k.hasKind("unit")
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
if (enumerable.length) {
|
|
70
|
-
propNodes.push(
|
|
71
|
-
...enumerable.map(k =>
|
|
72
|
-
ctx.$.node("prop", { key: k.unit as Key, value })
|
|
73
|
-
)
|
|
74
|
-
)
|
|
75
|
-
if (nonEnumerable.length)
|
|
76
|
-
indexNodes.push(ctx.$.node("index", { key: nonEnumerable, value }))
|
|
77
|
-
} else indexNodes.push(ctx.$.node("index", { key, value }))
|
|
78
|
-
} else {
|
|
79
|
-
const value = ctx.$.parse(entry.value, ctx)
|
|
80
|
-
propNodes.push({
|
|
81
|
-
key: entry.inner,
|
|
82
|
-
value,
|
|
83
|
-
optional: entry.kind === "optional"
|
|
84
|
-
})
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return ctx.$.schema({
|
|
89
|
-
domain: "object",
|
|
90
|
-
prop: propNodes,
|
|
91
|
-
index: indexNodes
|
|
92
|
-
})
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export const nonLeadingSpreadError =
|
|
96
|
-
"Spread operator may only be used as the first key in an object"
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Infers the contents of an object literal, ignoring a spread definition
|
|
100
|
-
* You probably want to use {@link inferObjectLiteral} instead.
|
|
101
|
-
*/
|
|
102
|
-
type inferObjectLiteralInner<def extends object, $, args> = {
|
|
103
|
-
// since def is a const parameter, we remove the readonly modifier here
|
|
104
|
-
// support for builtin readonly tracked here:
|
|
105
|
-
// https://github.com/arktypeio/arktype/issues/808
|
|
106
|
-
-readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]: inferDefinition<
|
|
107
|
-
def[k],
|
|
108
|
-
$,
|
|
109
|
-
args
|
|
110
|
-
>
|
|
111
|
-
} & {
|
|
112
|
-
-readonly [k in keyof def as optionalKeyFrom<k>]?: inferDefinition<
|
|
113
|
-
def[k],
|
|
114
|
-
$,
|
|
115
|
-
args
|
|
116
|
-
>
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export type inferObjectLiteral<def extends object, $, args> = show<
|
|
120
|
-
"..." extends keyof def ?
|
|
121
|
-
merge<
|
|
122
|
-
inferDefinition<def["..."], $, args>,
|
|
123
|
-
inferObjectLiteralInner<def, $, args>
|
|
124
|
-
>
|
|
125
|
-
: inferObjectLiteralInner<def, $, args>
|
|
126
|
-
>
|
|
127
|
-
|
|
128
|
-
export type validateObjectLiteral<def, $, args> = {
|
|
129
|
-
[k in keyof def]: k extends IndexKey<infer indexDef> ?
|
|
130
|
-
validateString<indexDef, $, args> extends ErrorMessage<infer message> ?
|
|
131
|
-
// add a nominal type here to avoid allowing the error message as input
|
|
132
|
-
indexParseError<message>
|
|
133
|
-
: inferDefinition<indexDef, $, args> extends PropertyKey ?
|
|
134
|
-
// if the indexDef is syntactically and semantically valid,
|
|
135
|
-
// move on to the validating the value definition
|
|
136
|
-
validateDefinition<def[k], $, args>
|
|
137
|
-
: indexParseError<writeInvalidPropertyKeyMessage<indexDef>>
|
|
138
|
-
: k extends "..." ?
|
|
139
|
-
inferDefinition<def[k], $, args> extends object ?
|
|
140
|
-
validateDefinition<def[k], $, args>
|
|
141
|
-
: indexParseError<writeInvalidSpreadTypeMessage<astToString<def[k]>>>
|
|
142
|
-
: validateDefinition<def[k], $, args>
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
type nonOptionalKeyFrom<k, $, args> =
|
|
146
|
-
parseKey<k> extends PreparsedKey<"required", infer inner> ? inner
|
|
147
|
-
: parseKey<k> extends PreparsedKey<"index", infer inner> ?
|
|
148
|
-
inferDefinition<inner, $, args> & Key
|
|
149
|
-
: // spread key is handled at the type root so is handled neither here nor in optionalKeyFrom
|
|
150
|
-
never
|
|
151
|
-
|
|
152
|
-
type optionalKeyFrom<k> =
|
|
153
|
-
parseKey<k> extends PreparsedKey<"optional", infer inner> ? inner : never
|
|
154
|
-
|
|
155
|
-
type PreparsedKey<
|
|
156
|
-
kind extends ParsedKeyKind = ParsedKeyKind,
|
|
157
|
-
inner extends Key = Key
|
|
158
|
-
> = {
|
|
159
|
-
kind: kind
|
|
160
|
-
inner: inner
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
namespace PreparsedKey {
|
|
164
|
-
export type from<t extends PreparsedKey> = t
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
type ParsedKeyKind = "required" | "optional" | "index" | "spread"
|
|
168
|
-
|
|
169
|
-
export type IndexKey<def extends string = string> = `[${def}]`
|
|
170
|
-
|
|
171
|
-
type PreparsedEntry = PreparsedKey & { value: unknown }
|
|
172
|
-
|
|
173
|
-
export const parseEntry = (entry: readonly [Key, unknown]): PreparsedEntry =>
|
|
174
|
-
Object.assign(parseKey(entry[0]), { value: entry[1] })
|
|
175
|
-
|
|
176
|
-
const parseKey = (key: Key): PreparsedKey =>
|
|
177
|
-
typeof key === "symbol" ? { inner: key, kind: "required" }
|
|
178
|
-
: key.at(-1) === "?" ?
|
|
179
|
-
key.at(-2) === Scanner.escapeToken ?
|
|
180
|
-
{ inner: `${key.slice(0, -2)}?`, kind: "required" }
|
|
181
|
-
: {
|
|
182
|
-
inner: key.slice(0, -1),
|
|
183
|
-
kind: "optional"
|
|
184
|
-
}
|
|
185
|
-
: key[0] === "[" && key.at(-1) === "]" ?
|
|
186
|
-
{ inner: key.slice(1, -1), kind: "index" }
|
|
187
|
-
: key[0] === Scanner.escapeToken && key[1] === "[" && key.at(-1) === "]" ?
|
|
188
|
-
{ inner: key.slice(1), kind: "required" }
|
|
189
|
-
: key === "..." ? { inner: "...", kind: "spread" }
|
|
190
|
-
: { inner: key === "\\..." ? "..." : key, kind: "required" }
|
|
191
|
-
|
|
192
|
-
type parseKey<k> =
|
|
193
|
-
k extends `${infer inner}?` ?
|
|
194
|
-
inner extends `${infer baseName}${Scanner.EscapeToken}` ?
|
|
195
|
-
PreparsedKey.from<{
|
|
196
|
-
kind: "required"
|
|
197
|
-
inner: `${baseName}?`
|
|
198
|
-
}>
|
|
199
|
-
: PreparsedKey.from<{
|
|
200
|
-
kind: "optional"
|
|
201
|
-
inner: inner
|
|
202
|
-
}>
|
|
203
|
-
: k extends "..." ? PreparsedKey.from<{ kind: "spread"; inner: "..." }>
|
|
204
|
-
: k extends `${Scanner.EscapeToken}...` ?
|
|
205
|
-
PreparsedKey.from<{ kind: "required"; inner: "..." }>
|
|
206
|
-
: k extends IndexKey<infer def> ?
|
|
207
|
-
PreparsedKey.from<{
|
|
208
|
-
kind: "index"
|
|
209
|
-
inner: def
|
|
210
|
-
}>
|
|
211
|
-
: PreparsedKey.from<{
|
|
212
|
-
kind: "required"
|
|
213
|
-
inner: k extends (
|
|
214
|
-
`${Scanner.EscapeToken}${infer escapedIndexKey extends IndexKey}`
|
|
215
|
-
) ?
|
|
216
|
-
escapedIndexKey
|
|
217
|
-
: k extends Key ? k
|
|
218
|
-
: `${k & number}`
|
|
219
|
-
}>
|
|
220
|
-
|
|
221
|
-
declare const indexParseSymbol: unique symbol
|
|
222
|
-
|
|
223
|
-
export type indexParseError<message extends string = string> = {
|
|
224
|
-
[indexParseSymbol]: message
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
export const writeInvalidSpreadTypeMessage = <def extends string>(
|
|
228
|
-
def: def
|
|
229
|
-
): writeInvalidSpreadTypeMessage<def> =>
|
|
230
|
-
`Spread operand must resolve to an object literal type (was ${def})`
|
|
231
|
-
|
|
232
|
-
type writeInvalidSpreadTypeMessage<def extends string> =
|
|
233
|
-
`Spread operand must resolve to an object literal type (was ${def})`
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import type { LimitLiteral, writeUnboundableMessage } from "@arktype/schema"
|
|
2
|
-
import type { ErrorMessage, array } from "@arktype/util"
|
|
3
|
-
import type {
|
|
4
|
-
Comparator,
|
|
5
|
-
InvertedComparators
|
|
6
|
-
} from "../string/reduce/shared.js"
|
|
7
|
-
import type {
|
|
8
|
-
BoundExpressionKind,
|
|
9
|
-
writeInvalidLimitMessage
|
|
10
|
-
} from "../string/shift/operator/bounds.js"
|
|
11
|
-
import type { inferAstIn } from "./infer.js"
|
|
12
|
-
import type { astToString } from "./utils.js"
|
|
13
|
-
import type { validateAst } from "./validate.js"
|
|
14
|
-
|
|
15
|
-
export type validateRange<l, comparator extends Comparator, r, $, args> =
|
|
16
|
-
l extends LimitLiteral ?
|
|
17
|
-
validateBound<r, InvertedComparators[comparator], l, "left", $, args>
|
|
18
|
-
: l extends [infer leftAst, Comparator, unknown] ?
|
|
19
|
-
ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>>
|
|
20
|
-
: validateBound<l, comparator, r & LimitLiteral, "right", $, args>
|
|
21
|
-
|
|
22
|
-
export type validateBound<
|
|
23
|
-
boundedAst,
|
|
24
|
-
comparator extends Comparator,
|
|
25
|
-
limit extends LimitLiteral,
|
|
26
|
-
boundKind extends BoundExpressionKind,
|
|
27
|
-
$,
|
|
28
|
-
args
|
|
29
|
-
> =
|
|
30
|
-
inferAstIn<boundedAst, $, args> extends infer bounded ?
|
|
31
|
-
isNumericallyBoundable<bounded> extends true ?
|
|
32
|
-
limit extends number ?
|
|
33
|
-
validateAst<boundedAst, $, args>
|
|
34
|
-
: ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>>
|
|
35
|
-
: bounded extends Date ?
|
|
36
|
-
// allow numeric or date literal as a Date limit
|
|
37
|
-
validateAst<boundedAst, $, args>
|
|
38
|
-
: ErrorMessage<
|
|
39
|
-
writeUnboundableMessage<
|
|
40
|
-
astToString<
|
|
41
|
-
boundKind extends "left" ? boundedAst[0 & keyof boundedAst]
|
|
42
|
-
: boundedAst
|
|
43
|
-
>
|
|
44
|
-
>
|
|
45
|
-
>
|
|
46
|
-
: never
|
|
47
|
-
|
|
48
|
-
// Check each numerically boundable type individually so an expression comprised
|
|
49
|
-
// of mixed bound kinds like (string|number)<5 isn't allowed
|
|
50
|
-
type isNumericallyBoundable<bounded> =
|
|
51
|
-
[bounded] extends [number] ? true
|
|
52
|
-
: [bounded] extends [string] ? true
|
|
53
|
-
: [bounded] extends [array] ? true
|
|
54
|
-
: false
|
|
55
|
-
|
|
56
|
-
export const writeDoubleRightBoundMessage = <root extends string>(
|
|
57
|
-
root: root
|
|
58
|
-
): writeDoubleRightBoundMessage<root> =>
|
|
59
|
-
`Expression ${root} must have at most one right bound`
|
|
60
|
-
|
|
61
|
-
type writeDoubleRightBoundMessage<root extends string> =
|
|
62
|
-
`Expression ${root} must have at most one right bound`
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { Schema, writeIndivisibleMessage } from "@arktype/schema"
|
|
2
|
-
import type { ErrorMessage } from "@arktype/util"
|
|
3
|
-
import type { inferAstIn } from "./infer.js"
|
|
4
|
-
import type { validateAst } from "./validate.js"
|
|
5
|
-
|
|
6
|
-
export type validateDivisor<l, $, args> =
|
|
7
|
-
inferAstIn<l, $, args> extends infer data ?
|
|
8
|
-
[data] extends [number] ?
|
|
9
|
-
validateAst<l, $, args>
|
|
10
|
-
: ErrorMessage<writeIndivisibleMessage<Schema<data>>>
|
|
11
|
-
: never
|
package/parser/semantic/infer.ts
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Date,
|
|
3
|
-
DateLiteral,
|
|
4
|
-
GenericProps,
|
|
5
|
-
LimitLiteral,
|
|
6
|
-
RegexLiteral,
|
|
7
|
-
constrain,
|
|
8
|
-
distillIn,
|
|
9
|
-
inferIntersection,
|
|
10
|
-
normalizeLimit,
|
|
11
|
-
string
|
|
12
|
-
} from "@arktype/schema"
|
|
13
|
-
import type { BigintLiteral, array } from "@arktype/util"
|
|
14
|
-
import type {
|
|
15
|
-
UnparsedScope,
|
|
16
|
-
resolve,
|
|
17
|
-
tryInferSubmoduleReference
|
|
18
|
-
} from "../../scope.js"
|
|
19
|
-
import type { inferDefinition } from "../definition.js"
|
|
20
|
-
import type { Comparator, MinComparator } from "../string/reduce/shared.js"
|
|
21
|
-
import type { StringLiteral } from "../string/shift/operand/enclosed.js"
|
|
22
|
-
|
|
23
|
-
export type inferAstRoot<ast, $, args> = inferConstrainableAst<ast, $, args>
|
|
24
|
-
|
|
25
|
-
export type inferAstIn<ast, $, args> = distillIn<inferAstRoot<ast, $, args>>
|
|
26
|
-
|
|
27
|
-
export type inferConstrainableAst<ast, $, args> =
|
|
28
|
-
ast extends array ? inferExpression<ast, $, args>
|
|
29
|
-
: ast extends string ? inferTerminal<ast, $, args>
|
|
30
|
-
: never
|
|
31
|
-
|
|
32
|
-
export type GenericInstantiationAst<
|
|
33
|
-
generic extends GenericProps = GenericProps,
|
|
34
|
-
argAsts extends unknown[] = unknown[]
|
|
35
|
-
> = [generic, "<>", argAsts]
|
|
36
|
-
|
|
37
|
-
export type inferExpression<ast extends array, $, args> =
|
|
38
|
-
ast extends GenericInstantiationAst<infer generic, infer argAsts> ?
|
|
39
|
-
inferDefinition<
|
|
40
|
-
generic["def"],
|
|
41
|
-
generic["$"]["$"] extends UnparsedScope ?
|
|
42
|
-
// If the generic was defined in the current scope, its definition can be
|
|
43
|
-
// resolved using the same scope as that of the input args.
|
|
44
|
-
$
|
|
45
|
-
: // Otherwise, use the scope that was explicitly associated with it.
|
|
46
|
-
generic["$"]["$"],
|
|
47
|
-
{
|
|
48
|
-
// Using keyof g["params"] & number here results in the element types
|
|
49
|
-
// being mixed- another reason TS should not have separate `${number}` and number keys!
|
|
50
|
-
[i in keyof generic["params"] &
|
|
51
|
-
`${number}` as generic["params"][i]]: inferConstrainableAst<
|
|
52
|
-
argAsts[i & keyof argAsts],
|
|
53
|
-
$,
|
|
54
|
-
args
|
|
55
|
-
>
|
|
56
|
-
}
|
|
57
|
-
>
|
|
58
|
-
: ast[1] extends "[]" ? inferConstrainableAst<ast[0], $, args>[]
|
|
59
|
-
: ast[1] extends "|" ?
|
|
60
|
-
| inferConstrainableAst<ast[0], $, args>
|
|
61
|
-
| inferConstrainableAst<ast[2], $, args>
|
|
62
|
-
: ast[1] extends "&" ?
|
|
63
|
-
inferIntersection<
|
|
64
|
-
inferConstrainableAst<ast[0], $, args>,
|
|
65
|
-
inferConstrainableAst<ast[2], $, args>
|
|
66
|
-
>
|
|
67
|
-
: ast[1] extends Comparator ?
|
|
68
|
-
ast[0] extends LimitLiteral ?
|
|
69
|
-
constrainBound<inferConstrainableAst<ast[2], $, args>, ast[1], ast[0]>
|
|
70
|
-
: constrainBound<inferConstrainableAst<ast[0], $, args>, ast[1], ast[2]>
|
|
71
|
-
: ast[1] extends "%" ?
|
|
72
|
-
constrain<
|
|
73
|
-
inferConstrainableAst<ast[0], $, args>,
|
|
74
|
-
"divisor",
|
|
75
|
-
ast[2] & number
|
|
76
|
-
>
|
|
77
|
-
: ast[0] extends "keyof" ? keyof inferConstrainableAst<ast[1], $, args>
|
|
78
|
-
: never
|
|
79
|
-
|
|
80
|
-
export type constrainBound<
|
|
81
|
-
constrainableIn,
|
|
82
|
-
comparator extends Comparator,
|
|
83
|
-
limit
|
|
84
|
-
> =
|
|
85
|
-
distillIn<constrainableIn> extends infer In ?
|
|
86
|
-
comparator extends "==" ?
|
|
87
|
-
In extends number ? limit
|
|
88
|
-
: In extends Date ? Date.literal<normalizeLimit<limit>>
|
|
89
|
-
: constrain<constrainableIn, "exactLength", limit & number>
|
|
90
|
-
: constrain<
|
|
91
|
-
constrainableIn,
|
|
92
|
-
In extends number ?
|
|
93
|
-
comparator extends MinComparator ?
|
|
94
|
-
"min"
|
|
95
|
-
: "max"
|
|
96
|
-
: In extends string | array ?
|
|
97
|
-
comparator extends MinComparator ?
|
|
98
|
-
"minLength"
|
|
99
|
-
: "maxLength"
|
|
100
|
-
: comparator extends MinComparator ? "after"
|
|
101
|
-
: "before",
|
|
102
|
-
{
|
|
103
|
-
rule: normalizeLimit<limit>
|
|
104
|
-
exclusive: comparator extends ">" | "<" ? true : false
|
|
105
|
-
}
|
|
106
|
-
>
|
|
107
|
-
: never
|
|
108
|
-
|
|
109
|
-
export type PrefixOperator = "keyof" | "instanceof" | "===" | "node"
|
|
110
|
-
|
|
111
|
-
export type PrefixExpression<
|
|
112
|
-
operator extends PrefixOperator = PrefixOperator,
|
|
113
|
-
operand = unknown
|
|
114
|
-
> = [operator, operand]
|
|
115
|
-
|
|
116
|
-
export type PostfixOperator = "[]"
|
|
117
|
-
|
|
118
|
-
export type PostfixExpression<
|
|
119
|
-
operator extends PostfixOperator = PostfixOperator,
|
|
120
|
-
operand = unknown
|
|
121
|
-
> = readonly [operand, operator]
|
|
122
|
-
|
|
123
|
-
export type InfixOperator = "|" | "&" | Comparator | "%" | ":" | "=>" | "@"
|
|
124
|
-
|
|
125
|
-
export type InfixExpression<
|
|
126
|
-
operator extends InfixOperator = InfixOperator,
|
|
127
|
-
l = unknown,
|
|
128
|
-
r = unknown
|
|
129
|
-
> = [l, operator, r]
|
|
130
|
-
|
|
131
|
-
export type inferTerminal<token extends string, $, args> =
|
|
132
|
-
token extends keyof args | keyof $ ? resolve<token, $, args>
|
|
133
|
-
: `#${token}` extends keyof $ ? resolve<`#${token}`, $, args>
|
|
134
|
-
: token extends StringLiteral<infer text> ? text
|
|
135
|
-
: token extends `${infer n extends number}` ? n
|
|
136
|
-
: token extends BigintLiteral<infer b> ? b
|
|
137
|
-
: token extends RegexLiteral<infer source> ? string.matching<source>
|
|
138
|
-
: token extends DateLiteral<infer source> ? Date.literal<source>
|
|
139
|
-
: // doing this last allows us to infer never if it isn't valid rather than check
|
|
140
|
-
// if it's a valid submodule reference ahead of time
|
|
141
|
-
tryInferSubmoduleReference<$, token>
|
package/parser/semantic/utils.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { Stringifiable } from "@arktype/util"
|
|
2
|
-
import type { Comparator } from "../string/reduce/shared.js"
|
|
3
|
-
import type { InfixExpression, PostfixExpression } from "./infer.js"
|
|
4
|
-
|
|
5
|
-
export type astToString<ast> =
|
|
6
|
-
ast extends PostfixExpression<infer operator, infer operand> ?
|
|
7
|
-
operator extends "[]" ?
|
|
8
|
-
`${astToString<operand>}[]`
|
|
9
|
-
: never
|
|
10
|
-
: ast extends InfixExpression<infer operator, infer l, infer r> ?
|
|
11
|
-
operator extends "&" | "|" | "%" | Comparator ?
|
|
12
|
-
`${astToString<l>} ${operator} ${astToString<r>}`
|
|
13
|
-
: never
|
|
14
|
-
: ast extends Stringifiable ? `${ast extends bigint ? `${ast}n` : ast}`
|
|
15
|
-
: "..."
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
arkKind,
|
|
3
|
-
GenericProps,
|
|
4
|
-
PrivateDeclaration,
|
|
5
|
-
writeMissingSubmoduleAccessMessage
|
|
6
|
-
} from "@arktype/schema"
|
|
7
|
-
import type {
|
|
8
|
-
BigintLiteral,
|
|
9
|
-
charsAfterFirst,
|
|
10
|
-
Completion,
|
|
11
|
-
ErrorMessage,
|
|
12
|
-
isAnyOrNever,
|
|
13
|
-
writeMalformedNumericLiteralMessage
|
|
14
|
-
} from "@arktype/util"
|
|
15
|
-
import type { Comparator } from "../string/reduce/shared.js"
|
|
16
|
-
import type { writeInvalidGenericArgsMessage } from "../string/shift/operand/genericArgs.js"
|
|
17
|
-
import type { parseString } from "../string/string.js"
|
|
18
|
-
import type { validateRange } from "./bounds.js"
|
|
19
|
-
import type { validateDivisor } from "./divisor.js"
|
|
20
|
-
import type {
|
|
21
|
-
GenericInstantiationAst,
|
|
22
|
-
InfixExpression,
|
|
23
|
-
PostfixExpression
|
|
24
|
-
} from "./infer.js"
|
|
25
|
-
import type { astToString } from "./utils.js"
|
|
26
|
-
|
|
27
|
-
export type validateAst<ast, $, args> =
|
|
28
|
-
ast extends string ? validateStringAst<ast, $>
|
|
29
|
-
: ast extends PostfixExpression<infer operator, infer operand> ?
|
|
30
|
-
operator extends "[]" ?
|
|
31
|
-
validateAst<operand, $, args>
|
|
32
|
-
: never
|
|
33
|
-
: ast extends InfixExpression<infer operator, infer l, infer r> ?
|
|
34
|
-
operator extends "&" | "|" ? validateInfix<ast, $, args>
|
|
35
|
-
: operator extends Comparator ? validateRange<l, operator, r, $, args>
|
|
36
|
-
: operator extends "%" ? validateDivisor<l, $, args>
|
|
37
|
-
: undefined
|
|
38
|
-
: ast extends readonly ["keyof", infer operand] ?
|
|
39
|
-
validateAst<operand, $, args>
|
|
40
|
-
: ast extends GenericInstantiationAst ? validateGenericArgs<ast[2], $, args>
|
|
41
|
-
: ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>> & {
|
|
42
|
-
ast: ast
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
type writeUnexpectedExpressionMessage<expression extends string> =
|
|
46
|
-
`Unexpectedly failed to parse the expression resulting from ${expression}`
|
|
47
|
-
|
|
48
|
-
type validateGenericArgs<argAsts extends unknown[], $, args> =
|
|
49
|
-
argAsts extends [infer head, ...infer tail] ?
|
|
50
|
-
validateAst<head, $, args> extends ErrorMessage<infer message> ?
|
|
51
|
-
ErrorMessage<message>
|
|
52
|
-
: validateGenericArgs<tail, $, args>
|
|
53
|
-
: undefined
|
|
54
|
-
|
|
55
|
-
export const writeUnsatisfiableExpressionError = <expression extends string>(
|
|
56
|
-
expression: expression
|
|
57
|
-
): writeUnsatisfiableExpressionError<expression> =>
|
|
58
|
-
`${expression} results in an unsatisfiable type`
|
|
59
|
-
|
|
60
|
-
export type writeUnsatisfiableExpressionError<expression extends string> =
|
|
61
|
-
`${expression} results in an unsatisfiable type`
|
|
62
|
-
|
|
63
|
-
export const writePrefixedPrivateReferenceMessage = <
|
|
64
|
-
def extends PrivateDeclaration
|
|
65
|
-
>(
|
|
66
|
-
def: def
|
|
67
|
-
): writePrefixedPrivateReferenceMessage<def> =>
|
|
68
|
-
`Private type references should not include '#'. Use '${def.slice(1) as charsAfterFirst<def>}' instead.`
|
|
69
|
-
|
|
70
|
-
export type writePrefixedPrivateReferenceMessage<
|
|
71
|
-
def extends PrivateDeclaration
|
|
72
|
-
> =
|
|
73
|
-
`Private type references should not include '#'. Use '${charsAfterFirst<def>}' instead.`
|
|
74
|
-
|
|
75
|
-
type validateStringAst<def extends string, $> =
|
|
76
|
-
def extends `${infer n extends number}` ?
|
|
77
|
-
number extends n ?
|
|
78
|
-
ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">>
|
|
79
|
-
: undefined
|
|
80
|
-
: def extends BigintLiteral<infer b> ?
|
|
81
|
-
bigint extends b ?
|
|
82
|
-
ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">>
|
|
83
|
-
: undefined
|
|
84
|
-
: maybeExtractAlias<def, $> extends infer alias extends keyof $ ?
|
|
85
|
-
isAnyOrNever<$[alias]> extends true ? def
|
|
86
|
-
: def extends PrivateDeclaration ?
|
|
87
|
-
ErrorMessage<writePrefixedPrivateReferenceMessage<def>>
|
|
88
|
-
: // these problems would've been caught during a fullStringParse, but it's most
|
|
89
|
-
// efficient to check for them here in case the string was naively parsed
|
|
90
|
-
$[alias] extends GenericProps ?
|
|
91
|
-
ErrorMessage<writeInvalidGenericArgsMessage<def, $[alias]["params"], []>>
|
|
92
|
-
: $[alias] extends { [arkKind]: "module" } ?
|
|
93
|
-
ErrorMessage<writeMissingSubmoduleAccessMessage<def>>
|
|
94
|
-
: undefined
|
|
95
|
-
: def extends ErrorMessage ? def
|
|
96
|
-
: undefined
|
|
97
|
-
|
|
98
|
-
export type maybeExtractAlias<def extends string, $> =
|
|
99
|
-
def extends keyof $ ? def
|
|
100
|
-
: `#${def}` extends keyof $ ? `#${def}`
|
|
101
|
-
: null
|
|
102
|
-
|
|
103
|
-
export type validateString<def extends string, $, args> =
|
|
104
|
-
validateAst<parseString<def, $, args>, $, args> extends (
|
|
105
|
-
infer result extends ErrorMessage
|
|
106
|
-
) ?
|
|
107
|
-
result extends Completion<infer text> ?
|
|
108
|
-
text
|
|
109
|
-
: result
|
|
110
|
-
: def
|
|
111
|
-
|
|
112
|
-
type validateInfix<ast extends InfixExpression, $, args> =
|
|
113
|
-
validateAst<ast[0], $, args> extends ErrorMessage<infer message> ?
|
|
114
|
-
ErrorMessage<message>
|
|
115
|
-
: validateAst<ast[2], $, args> extends ErrorMessage<infer message> ?
|
|
116
|
-
ErrorMessage<message>
|
|
117
|
-
: undefined
|