arktype 2.0.0-dev.7 → 2.0.0-dev.8
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/__tests__/array.test.ts +23 -25
- package/__tests__/config.test.ts +8 -8
- package/__tests__/cyclic.test.ts +3 -3
- package/__tests__/examples.test.ts +3 -3
- package/__tests__/instanceof.test.ts +7 -7
- package/__tests__/intersection.test.ts +2 -2
- package/__tests__/keyTraversal.test.ts +15 -15
- package/__tests__/keywords.test.ts +50 -37
- package/__tests__/literal.test.ts +2 -2
- package/__tests__/match.bench.ts +6 -6
- package/__tests__/narrow.test.ts +17 -23
- package/__tests__/objectLiteral.test.ts +4 -4
- package/__tests__/operator.bench.ts +4 -4
- package/__tests__/{morph.test.ts → pipe.test.ts} +136 -75
- package/__tests__/scope.test.ts +5 -15
- package/__tests__/traverse.test.ts +36 -35
- package/__tests__/type.test.ts +8 -10
- package/ark.ts +14 -11
- package/generic.ts +8 -6
- package/match.ts +59 -70
- package/out/ark.d.ts +4 -3
- package/out/ark.d.ts.map +1 -1
- package/out/ark.js +4 -4
- package/out/ark.js.map +1 -1
- package/out/generic.d.ts.map +1 -1
- package/out/generic.js +8 -6
- package/out/generic.js.map +1 -1
- package/out/match.d.ts.map +1 -1
- package/out/match.js +50 -56
- package/out/match.js.map +1 -1
- package/out/parser/generic.d.ts +3 -3
- package/out/parser/generic.d.ts.map +1 -1
- package/out/parser/generic.js +4 -5
- package/out/parser/generic.js.map +1 -1
- package/out/parser/objectLiteral.d.ts.map +1 -1
- package/out/parser/objectLiteral.js +2 -3
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/validate.d.ts +2 -2
- package/out/parser/semantic/validate.d.ts.map +1 -1
- package/out/parser/semantic/validate.js.map +1 -1
- package/out/parser/string/reduce/dynamic.d.ts.map +1 -1
- package/out/parser/string/reduce/dynamic.js +4 -8
- package/out/parser/string/reduce/dynamic.js.map +1 -1
- package/out/parser/string/shift/operand/date.js +2 -4
- package/out/parser/string/shift/operand/date.js.map +1 -1
- package/out/parser/string/shift/operand/enclosed.js +6 -8
- package/out/parser/string/shift/operand/enclosed.js.map +1 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
- package/out/parser/string/shift/operand/genericArgs.js +4 -5
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts +4 -4
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -1
- package/out/parser/string/shift/operand/unenclosed.js +5 -10
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
- package/out/parser/string/shift/operator/bounds.d.ts.map +1 -1
- package/out/parser/string/shift/operator/bounds.js +4 -8
- package/out/parser/string/shift/operator/bounds.js.map +1 -1
- package/out/parser/string/shift/operator/divisor.d.ts.map +1 -1
- package/out/parser/string/shift/operator/divisor.js +1 -2
- package/out/parser/string/shift/operator/divisor.js.map +1 -1
- package/out/parser/string/shift/scanner.d.ts.map +1 -1
- package/out/parser/string/shift/scanner.js +2 -4
- package/out/parser/string/shift/scanner.js.map +1 -1
- package/out/parser/string/string.d.ts.map +1 -1
- package/out/parser/string/string.js +1 -2
- package/out/parser/string/string.js.map +1 -1
- package/out/parser/tuple.d.ts.map +1 -1
- package/out/parser/tuple.js +12 -15
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts +9 -9
- package/out/scope.d.ts.map +1 -1
- package/out/scope.js +11 -33
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +38 -18
- package/out/type.d.ts.map +1 -1
- package/out/type.js +32 -1
- package/out/type.js.map +1 -1
- package/package.json +3 -3
- package/parser/generic.ts +11 -12
- package/parser/objectLiteral.ts +2 -4
- package/parser/semantic/validate.ts +2 -3
- package/parser/string/reduce/dynamic.ts +7 -10
- package/parser/string/shift/operand/date.ts +3 -6
- package/parser/string/shift/operand/enclosed.ts +8 -8
- package/parser/string/shift/operand/genericArgs.ts +8 -8
- package/parser/string/shift/operand/unenclosed.ts +14 -23
- package/parser/string/shift/operator/bounds.ts +7 -9
- package/parser/string/shift/operator/divisor.ts +2 -3
- package/parser/string/shift/scanner.ts +2 -4
- package/parser/string/string.ts +2 -3
- package/parser/tuple.ts +15 -16
- package/scope.ts +24 -48
- package/type.ts +150 -35
package/out/type.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.js","sourceRoot":"","sources":["../type.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../type.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,SAAS,EAST,SAAS,EAcT,MAAM,iBAAiB,CAAA;AACxB,OAAO,EACN,QAAQ,EAIR,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,OAAO,EAAgC,MAAM,cAAc,CAAA;AAMpE,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAA;AA6CxD,MAAM,qBAAqB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC3C,MAAM,EAAE,SAAS;CACe,CAAC,CAAA;AAElC,MAAM,OAAO,aAAc,SAAQ,QAGlC;IACA,YAAY,CAAW;QACtB,KAAK,CACJ,CAAC,GAAG,IAAI,EAAE,EAAE;YACX,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACvB,+BAA+B;gBAC/B,OAAO,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;YAC5B,CAAC;YACD,IACC,IAAI,CAAC,MAAM,KAAK,CAAC;gBACjB,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ;gBAC3B,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG;gBAClB,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,EACrB,CAAC;gBACF,4EAA4E;gBAC5E,qBAAqB;gBACrB,MAAM,MAAM,GAAG,kBAAkB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAA;gBACvD,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,CAAA;gBACnB,wDAAwD;gBACxD,OAAO,IAAI,OAAO,CAAC,MAAM,EAAE,GAAG,EAAE,CAAU,CAAU,CAAA;YACrD,CAAC;YACD,wEAAwE;YACxE,0EAA0E;YAC1E,uDAAuD;YACvD,OAAO,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;QACzB,CAAC,EACD,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,EAAE,qBAAqB,EAAE,CAC1C,CAAA;IACF,CAAC;CACD;AAoID,MAAM,CAAC,MAAM,IAAI,GAAoB,SAAkB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "arktype",
|
|
3
3
|
"description": "TypeScript's 1:1 validator, optimized from editor to runtime",
|
|
4
|
-
"version": "2.0.0-dev.
|
|
4
|
+
"version": "2.0.0-dev.8",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "David Blass",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"test": "tsx ../repo/testPackage.ts"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@arktype/util": "0.0.
|
|
42
|
-
"@arktype/schema": "0.0.
|
|
41
|
+
"@arktype/util": "0.0.34",
|
|
42
|
+
"@arktype/schema": "0.0.8"
|
|
43
43
|
}
|
|
44
44
|
}
|
package/parser/generic.ts
CHANGED
|
@@ -13,10 +13,10 @@ export type GenericParamsParseError<message extends string = string> = [
|
|
|
13
13
|
]
|
|
14
14
|
|
|
15
15
|
export const parseGenericParams = (def: string): string[] =>
|
|
16
|
-
|
|
16
|
+
_parseGenericParams(new Scanner(def))
|
|
17
17
|
|
|
18
18
|
export type parseGenericParams<def extends string> =
|
|
19
|
-
|
|
19
|
+
_parseParams<def, "", []> extends infer result extends string[] ?
|
|
20
20
|
"" extends result[number] ?
|
|
21
21
|
GenericParamsParseError<emptyGenericParameterMessage>
|
|
22
22
|
: result
|
|
@@ -27,41 +27,40 @@ export const emptyGenericParameterMessage =
|
|
|
27
27
|
|
|
28
28
|
export type emptyGenericParameterMessage = typeof emptyGenericParameterMessage
|
|
29
29
|
|
|
30
|
-
const
|
|
30
|
+
const _parseGenericParams = (scanner: Scanner): string[] => {
|
|
31
31
|
const param = scanner.shiftUntilNextTerminator()
|
|
32
|
-
if (param === "")
|
|
33
|
-
|
|
34
|
-
}
|
|
32
|
+
if (param === "") throwParseError(emptyGenericParameterMessage)
|
|
33
|
+
|
|
35
34
|
scanner.shiftUntilNonWhitespace()
|
|
36
35
|
const nextNonWhitespace = scanner.shift()
|
|
37
36
|
return (
|
|
38
37
|
nextNonWhitespace === "" ? [param]
|
|
39
|
-
: nextNonWhitespace === "," ? [param,
|
|
38
|
+
: nextNonWhitespace === "," ? [param, ..._parseGenericParams(scanner)]
|
|
40
39
|
: throwParseError(writeUnexpectedCharacterMessage(nextNonWhitespace, ","))
|
|
41
40
|
)
|
|
42
41
|
}
|
|
43
42
|
|
|
44
|
-
type
|
|
43
|
+
type _parseParams<
|
|
45
44
|
unscanned extends string,
|
|
46
45
|
param extends string,
|
|
47
46
|
result extends string[]
|
|
48
47
|
> =
|
|
49
48
|
unscanned extends `${infer lookahead}${infer nextUnscanned}` ?
|
|
50
|
-
lookahead extends "," ?
|
|
49
|
+
lookahead extends "," ? _parseParams<nextUnscanned, "", [...result, param]>
|
|
51
50
|
: lookahead extends Scanner.WhiteSpaceToken ?
|
|
52
51
|
param extends "" ?
|
|
53
52
|
// if the next char is whitespace and we aren't in the middle of a param, skip to the next one
|
|
54
|
-
|
|
53
|
+
_parseParams<Scanner.skipWhitespace<nextUnscanned>, "", result>
|
|
55
54
|
: Scanner.skipWhitespace<nextUnscanned> extends (
|
|
56
55
|
`${infer nextNonWhitespace}${infer rest}`
|
|
57
56
|
) ?
|
|
58
57
|
nextNonWhitespace extends "," ?
|
|
59
|
-
|
|
58
|
+
_parseParams<rest, "", [...result, param]>
|
|
60
59
|
: GenericParamsParseError<
|
|
61
60
|
writeUnexpectedCharacterMessage<nextNonWhitespace, ",">
|
|
62
61
|
>
|
|
63
62
|
: // params end with a single whitespace character, add the current token
|
|
64
63
|
[...result, param]
|
|
65
|
-
:
|
|
64
|
+
: _parseParams<nextUnscanned, `${param}${lookahead}`, result>
|
|
66
65
|
: param extends "" ? result
|
|
67
66
|
: [...result, param]
|
package/parser/objectLiteral.ts
CHANGED
|
@@ -46,16 +46,14 @@ export const parseObjectLiteral = (def: Dict, ctx: ParseContext): RawSchema => {
|
|
|
46
46
|
// We filter out keys from the spreadNode that will be defined later on this same object
|
|
47
47
|
// because the currently parsed definition will overwrite them.
|
|
48
48
|
spreadNode.prop?.forEach(
|
|
49
|
-
|
|
49
|
+
spreadRequired =>
|
|
50
50
|
!parsedEntries.some(
|
|
51
51
|
({ inner: innerKey }) => innerKey === spreadRequired.key
|
|
52
52
|
) && propNodes.push(spreadRequired)
|
|
53
53
|
)
|
|
54
54
|
}
|
|
55
55
|
for (const entry of parsedEntries) {
|
|
56
|
-
if (entry.kind === "spread")
|
|
57
|
-
return throwParseError(nonLeadingSpreadError)
|
|
58
|
-
}
|
|
56
|
+
if (entry.kind === "spread") return throwParseError(nonLeadingSpreadError)
|
|
59
57
|
|
|
60
58
|
if (entry.kind === "index") {
|
|
61
59
|
// handle key parsing first to match type behavior
|
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
charsAfterFirst,
|
|
10
10
|
Completion,
|
|
11
11
|
ErrorMessage,
|
|
12
|
+
isAnyOrNever,
|
|
12
13
|
writeMalformedNumericLiteralMessage
|
|
13
14
|
} from "@arktype/util"
|
|
14
15
|
import type { Comparator } from "../string/reduce/shared.js"
|
|
@@ -81,9 +82,7 @@ type validateStringAst<def extends string, $> =
|
|
|
81
82
|
ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">>
|
|
82
83
|
: undefined
|
|
83
84
|
: maybeExtractAlias<def, $> extends infer alias extends keyof $ ?
|
|
84
|
-
|
|
85
|
-
// handle any/never
|
|
86
|
-
def
|
|
85
|
+
isAnyOrNever<$[alias]> extends true ? def
|
|
87
86
|
: def extends PrivateDeclaration ?
|
|
88
87
|
ErrorMessage<writePrefixedPrivateReferenceMessage<def>>
|
|
89
88
|
: // these problems would've been caught during a fullStringParse, but it's most
|
|
@@ -77,18 +77,17 @@ export class DynamicState {
|
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
finalize(finalizer: Scanner.FinalizingLookahead): void {
|
|
80
|
-
if (this.groups.length)
|
|
81
|
-
|
|
82
|
-
}
|
|
80
|
+
if (this.groups.length) return this.error(writeUnclosedGroupMessage(")"))
|
|
81
|
+
|
|
83
82
|
this.finalizeBranches()
|
|
84
83
|
this.finalizer = finalizer
|
|
85
84
|
}
|
|
86
85
|
|
|
87
86
|
reduceLeftBound(limit: LimitLiteral, comparator: Comparator): void {
|
|
88
87
|
const invertedComparator = invertedComparators[comparator]
|
|
89
|
-
if (!isKeyOf(invertedComparator, minComparators))
|
|
88
|
+
if (!isKeyOf(invertedComparator, minComparators))
|
|
90
89
|
return this.error(writeUnpairableComparatorMessage(comparator))
|
|
91
|
-
|
|
90
|
+
|
|
92
91
|
if (this.branches.leftBound) {
|
|
93
92
|
return this.error(
|
|
94
93
|
writeMultipleLeftBoundsMessage(
|
|
@@ -113,17 +112,15 @@ export class DynamicState {
|
|
|
113
112
|
} else if (this.branches.intersection) {
|
|
114
113
|
this.pushRootToBranch("&")
|
|
115
114
|
this.root = this.branches.intersection
|
|
116
|
-
} else
|
|
117
|
-
this.applyPrefixes()
|
|
118
|
-
}
|
|
115
|
+
} else this.applyPrefixes()
|
|
119
116
|
}
|
|
120
117
|
|
|
121
118
|
finalizeGroup(): void {
|
|
122
119
|
this.finalizeBranches()
|
|
123
120
|
const topBranchState = this.groups.pop()
|
|
124
|
-
if (!topBranchState)
|
|
121
|
+
if (!topBranchState)
|
|
125
122
|
return this.error(writeUnmatchedGroupCloseMessage(this.scanner.unscanned))
|
|
126
|
-
|
|
123
|
+
|
|
127
124
|
this.branches = topBranchState
|
|
128
125
|
}
|
|
129
126
|
|
|
@@ -40,15 +40,12 @@ const maybeParseDate = <errorOnFail extends boolean | string>(
|
|
|
40
40
|
errorOnFail?: errorOnFail
|
|
41
41
|
): DateParseResult<errorOnFail> => {
|
|
42
42
|
const stringParsedDate = new Date(source)
|
|
43
|
-
if (isValidDate(stringParsedDate))
|
|
44
|
-
|
|
45
|
-
}
|
|
43
|
+
if (isValidDate(stringParsedDate)) return stringParsedDate
|
|
44
|
+
|
|
46
45
|
const epochMillis = tryParseNumber(source)
|
|
47
46
|
if (epochMillis !== undefined) {
|
|
48
47
|
const numberParsedDate = new Date(epochMillis)
|
|
49
|
-
if (isValidDate(numberParsedDate))
|
|
50
|
-
return numberParsedDate
|
|
51
|
-
}
|
|
48
|
+
if (isValidDate(numberParsedDate)) return numberParsedDate
|
|
52
49
|
}
|
|
53
50
|
return errorOnFail ?
|
|
54
51
|
throwParseError(
|
|
@@ -21,11 +21,11 @@ export const parseEnclosed = (
|
|
|
21
21
|
const enclosed = s.scanner.shiftUntil(
|
|
22
22
|
untilLookaheadIsClosing[enclosingTokens[enclosing]]
|
|
23
23
|
)
|
|
24
|
-
if (s.scanner.lookahead === "")
|
|
24
|
+
if (s.scanner.lookahead === "")
|
|
25
25
|
return s.error(writeUnterminatedEnclosedMessage(enclosed, enclosing))
|
|
26
|
-
|
|
26
|
+
|
|
27
27
|
// Shift the scanner one additional time for the second enclosing token
|
|
28
|
-
|
|
28
|
+
s.scanner.shift()
|
|
29
29
|
if (enclosing === "/") {
|
|
30
30
|
// fail parsing if the regex is invalid
|
|
31
31
|
new RegExp(enclosed)
|
|
@@ -37,9 +37,9 @@ export const parseEnclosed = (
|
|
|
37
37
|
},
|
|
38
38
|
{ prereduced: true }
|
|
39
39
|
)
|
|
40
|
-
} else if (isKeyOf(enclosing, enclosingQuote))
|
|
40
|
+
} else if (isKeyOf(enclosing, enclosingQuote))
|
|
41
41
|
s.root = s.ctx.$.node("unit", { unit: enclosed })
|
|
42
|
-
|
|
42
|
+
else {
|
|
43
43
|
const date = tryParseDate(enclosed, writeInvalidDateMessage(enclosed))
|
|
44
44
|
s.root = s.ctx.$.node("unit", { unit: date, description: enclosed })
|
|
45
45
|
}
|
|
@@ -93,9 +93,9 @@ export const untilLookaheadIsClosing: Record<
|
|
|
93
93
|
EnclosingEndToken,
|
|
94
94
|
Scanner.UntilCondition
|
|
95
95
|
> = {
|
|
96
|
-
"'":
|
|
97
|
-
'"':
|
|
98
|
-
"/":
|
|
96
|
+
"'": scanner => scanner.lookahead === `'`,
|
|
97
|
+
'"': scanner => scanner.lookahead === `"`,
|
|
98
|
+
"/": scanner => scanner.lookahead === `/`
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
const enclosingCharDescriptions = {
|
|
@@ -17,7 +17,7 @@ export const parseGenericArgs = (
|
|
|
17
17
|
name: string,
|
|
18
18
|
params: string[],
|
|
19
19
|
s: DynamicState
|
|
20
|
-
): ParsedArgs<RawSchema[]> =>
|
|
20
|
+
): ParsedArgs<RawSchema[]> => _parseGenericArgs(name, params, s, [], [])
|
|
21
21
|
|
|
22
22
|
export type parseGenericArgs<
|
|
23
23
|
name extends string,
|
|
@@ -25,9 +25,9 @@ export type parseGenericArgs<
|
|
|
25
25
|
unscanned extends string,
|
|
26
26
|
$,
|
|
27
27
|
args
|
|
28
|
-
> =
|
|
28
|
+
> = _parseGenericArgs<name, params, unscanned, $, args, [], []>
|
|
29
29
|
|
|
30
|
-
const
|
|
30
|
+
const _parseGenericArgs = (
|
|
31
31
|
name: string,
|
|
32
32
|
params: string[],
|
|
33
33
|
s: DynamicState,
|
|
@@ -47,13 +47,13 @@ const $parseGenericArgs = (
|
|
|
47
47
|
}
|
|
48
48
|
return argState.error(writeInvalidGenericArgsMessage(name, params, argDefs))
|
|
49
49
|
}
|
|
50
|
-
if (argState.finalizer === ",")
|
|
51
|
-
return
|
|
52
|
-
|
|
50
|
+
if (argState.finalizer === ",")
|
|
51
|
+
return _parseGenericArgs(name, params, s, argDefs, argNodes)
|
|
52
|
+
|
|
53
53
|
return argState.error(writeUnclosedGroupMessage(">"))
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
type
|
|
56
|
+
type _parseGenericArgs<
|
|
57
57
|
name extends string,
|
|
58
58
|
params extends string[],
|
|
59
59
|
unscanned extends string,
|
|
@@ -85,7 +85,7 @@ type $parseGenericArgs<
|
|
|
85
85
|
ParsedArgs<nextAsts, nextUnscanned>
|
|
86
86
|
: state.error<writeInvalidGenericArgsMessage<name, params, nextDefs>>
|
|
87
87
|
: finalArgState["finalizer"] extends "," ?
|
|
88
|
-
|
|
88
|
+
_parseGenericArgs<
|
|
89
89
|
name,
|
|
90
90
|
params,
|
|
91
91
|
nextUnscanned,
|
|
@@ -2,7 +2,6 @@ import {
|
|
|
2
2
|
type GenericProps,
|
|
3
3
|
type PrivateDeclaration,
|
|
4
4
|
RawSchema,
|
|
5
|
-
type SchemaModule,
|
|
6
5
|
hasArkKind,
|
|
7
6
|
type writeNonSubmoduleDotMessage,
|
|
8
7
|
writeUnresolvableMessage
|
|
@@ -11,6 +10,7 @@ import {
|
|
|
11
10
|
type BigintLiteral,
|
|
12
11
|
type Completion,
|
|
13
12
|
type ErrorMessage,
|
|
13
|
+
type isAnyOrNever,
|
|
14
14
|
type join,
|
|
15
15
|
printable,
|
|
16
16
|
throwParseError,
|
|
@@ -33,11 +33,8 @@ import {
|
|
|
33
33
|
|
|
34
34
|
export const parseUnenclosed = (s: DynamicState): void => {
|
|
35
35
|
const token = s.scanner.shiftUntilNextTerminator()
|
|
36
|
-
if (token === "keyof")
|
|
37
|
-
|
|
38
|
-
} else {
|
|
39
|
-
s.root = unenclosedToNode(s, token)
|
|
40
|
-
}
|
|
36
|
+
if (token === "keyof") s.addPrefix("keyof")
|
|
37
|
+
else s.root = unenclosedToNode(s, token)
|
|
41
38
|
}
|
|
42
39
|
|
|
43
40
|
export type parseUnenclosed<s extends StaticState, $, args> =
|
|
@@ -48,7 +45,9 @@ export type parseUnenclosed<s extends StaticState, $, args> =
|
|
|
48
45
|
: tryResolve<s, token, $, args> extends infer result ?
|
|
49
46
|
result extends ErrorMessage<infer message> ? state.error<message>
|
|
50
47
|
: result extends keyof $ ?
|
|
51
|
-
|
|
48
|
+
isAnyOrNever<$[result]> extends true ?
|
|
49
|
+
state.setRoot<s, result, unscanned>
|
|
50
|
+
: $[result] extends GenericProps ?
|
|
52
51
|
parseGenericInstantiation<
|
|
53
52
|
token,
|
|
54
53
|
$[result],
|
|
@@ -68,9 +67,9 @@ export const parseGenericInstantiation = (
|
|
|
68
67
|
): RawSchema => {
|
|
69
68
|
s.scanner.shiftUntilNonWhitespace()
|
|
70
69
|
const lookahead = s.scanner.shift()
|
|
71
|
-
if (lookahead !== "<")
|
|
70
|
+
if (lookahead !== "<")
|
|
72
71
|
return s.error(writeInvalidGenericArgsMessage(name, g.params, []))
|
|
73
|
-
|
|
72
|
+
|
|
74
73
|
const parsedArgs = parseGenericArgs(name, g.params, s)
|
|
75
74
|
const remainingChars = parsedArgs.unscanned.length
|
|
76
75
|
// set the scanner position to where the args scanner left off
|
|
@@ -128,13 +127,12 @@ const maybeParseUnenclosedLiteral = (
|
|
|
128
127
|
token: string
|
|
129
128
|
): RawSchema | undefined => {
|
|
130
129
|
const maybeNumber = tryParseNumber(token, { strict: true })
|
|
131
|
-
if (maybeNumber !== undefined)
|
|
130
|
+
if (maybeNumber !== undefined)
|
|
132
131
|
return s.ctx.$.node("unit", { unit: maybeNumber })
|
|
133
|
-
|
|
132
|
+
|
|
134
133
|
const maybeBigint = tryParseWellFormedBigint(token)
|
|
135
|
-
if (maybeBigint !== undefined)
|
|
134
|
+
if (maybeBigint !== undefined)
|
|
136
135
|
return s.ctx.$.node("unit", { unit: maybeBigint })
|
|
137
|
-
}
|
|
138
136
|
}
|
|
139
137
|
|
|
140
138
|
type tryResolve<s extends StaticState, token extends string, $, args> =
|
|
@@ -146,16 +144,9 @@ type tryResolve<s extends StaticState, token extends string, $, args> =
|
|
|
146
144
|
: token extends (
|
|
147
145
|
`${infer submodule extends keyof $ & string}.${infer reference}`
|
|
148
146
|
) ?
|
|
149
|
-
$[submodule] extends
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
reference extends keyof sub$ ? token
|
|
153
|
-
: unknown extends sub$ ?
|
|
154
|
-
// not sure why we need the additional check here, but for now TS seems to
|
|
155
|
-
// hit this branch for a non-scope dot access rather than failing
|
|
156
|
-
// initially when we try to infer r. if this can be removed without breaking
|
|
157
|
-
// any submodule test cases, do it!
|
|
158
|
-
ErrorMessage<writeNonSubmoduleDotMessage<submodule>>
|
|
147
|
+
$[submodule] extends Module<infer sub$> ?
|
|
148
|
+
reference extends keyof sub$ ?
|
|
149
|
+
token
|
|
159
150
|
: unresolvableError<s, reference, $[submodule], args, [submodule]>
|
|
160
151
|
: ErrorMessage<writeNonSubmoduleDotMessage<submodule>>
|
|
161
152
|
: unresolvableError<s, token, $, args, []>
|
|
@@ -181,9 +181,8 @@ export const parseRightBound = (
|
|
|
181
181
|
if (
|
|
182
182
|
!limitNode.hasKind("unit") ||
|
|
183
183
|
(typeof limitNode.unit !== "number" && !(limitNode.unit instanceof Date))
|
|
184
|
-
)
|
|
184
|
+
)
|
|
185
185
|
return s.error(writeInvalidLimitMessage(comparator, limitToken, "right"))
|
|
186
|
-
}
|
|
187
186
|
|
|
188
187
|
const limit = limitNode.unit
|
|
189
188
|
// apply the newly-parsed right bound
|
|
@@ -194,16 +193,15 @@ export const parseRightBound = (
|
|
|
194
193
|
typeof limit === "number" ? limit : (limitToken as DateLiteral),
|
|
195
194
|
previousRoot,
|
|
196
195
|
"right"
|
|
197
|
-
))
|
|
196
|
+
))
|
|
198
197
|
s.constrainRoot(kind, { rule: limit, exclusive })
|
|
199
|
-
|
|
200
|
-
if (!s.branches.leftBound)
|
|
201
|
-
|
|
202
|
-
}
|
|
198
|
+
|
|
199
|
+
if (!s.branches.leftBound) return
|
|
200
|
+
|
|
203
201
|
// if there's an open left bound, perform additional validation and apply it
|
|
204
|
-
if (!isKeyOf(comparator, maxComparators))
|
|
202
|
+
if (!isKeyOf(comparator, maxComparators))
|
|
205
203
|
return s.error(writeUnpairableComparatorMessage(comparator))
|
|
206
|
-
|
|
204
|
+
|
|
207
205
|
const lowerBoundKind = getBoundKinds(
|
|
208
206
|
s.branches.leftBound.comparator,
|
|
209
207
|
s.branches.leftBound.limit,
|
|
@@ -8,9 +8,8 @@ export const parseDivisor = (s: DynamicStateWithRoot): void => {
|
|
|
8
8
|
const divisor = tryParseInteger(divisorToken, {
|
|
9
9
|
errorOnFail: writeInvalidDivisorMessage(divisorToken)
|
|
10
10
|
})
|
|
11
|
-
if (divisor === 0)
|
|
12
|
-
|
|
13
|
-
}
|
|
11
|
+
if (divisor === 0) s.error(writeInvalidDivisorMessage(0))
|
|
12
|
+
|
|
14
13
|
s.root = s.root.constrain("divisor", divisor)
|
|
15
14
|
}
|
|
16
15
|
|
|
@@ -27,11 +27,9 @@ export class Scanner<Lookahead extends string = string> {
|
|
|
27
27
|
let shifted = ""
|
|
28
28
|
while (this.lookahead) {
|
|
29
29
|
if (condition(this, shifted)) {
|
|
30
|
-
if (shifted[shifted.length - 1] === Scanner.escapeToken)
|
|
30
|
+
if (shifted[shifted.length - 1] === Scanner.escapeToken)
|
|
31
31
|
shifted = shifted.slice(0, -1)
|
|
32
|
-
|
|
33
|
-
break
|
|
34
|
-
}
|
|
32
|
+
else break
|
|
35
33
|
}
|
|
36
34
|
shifted += this.shift()
|
|
37
35
|
}
|
package/parser/string/string.ts
CHANGED
|
@@ -62,9 +62,8 @@ type fullStringParse<s extends StaticState, $, args> = extractFinalizedResult<
|
|
|
62
62
|
>
|
|
63
63
|
|
|
64
64
|
export const parseUntilFinalizer = (s: DynamicState): DynamicStateWithRoot => {
|
|
65
|
-
while (s.finalizer === undefined)
|
|
66
|
-
|
|
67
|
-
}
|
|
65
|
+
while (s.finalizer === undefined) next(s)
|
|
66
|
+
|
|
68
67
|
return s as DynamicStateWithRoot
|
|
69
68
|
}
|
|
70
69
|
|
package/parser/tuple.ts
CHANGED
|
@@ -53,34 +53,33 @@ export const parseTupleLiteral = (def: array, ctx: ParseContext): RawSchema => {
|
|
|
53
53
|
|
|
54
54
|
i++
|
|
55
55
|
if (def[i] === "?") {
|
|
56
|
-
if (spread)
|
|
57
|
-
|
|
58
|
-
}
|
|
56
|
+
if (spread) return throwParseError(spreadOptionalMessage)
|
|
57
|
+
|
|
59
58
|
optional = true
|
|
60
59
|
i++
|
|
61
60
|
}
|
|
62
61
|
if (spread) {
|
|
63
|
-
if (!element.extends(jsObjects.Array))
|
|
62
|
+
if (!element.extends(jsObjects.Array))
|
|
64
63
|
return throwParseError(writeNonArraySpreadMessage(element.expression))
|
|
65
|
-
|
|
64
|
+
|
|
66
65
|
// a spread must be distributed over branches e.g.:
|
|
67
66
|
// def: [string, ...(number[] | [true, false])]
|
|
68
67
|
// nodes: [string, ...number[]] | [string, true, false]
|
|
69
|
-
sequences = sequences.flatMap(
|
|
68
|
+
sequences = sequences.flatMap(base =>
|
|
70
69
|
// since appendElement mutates base, we have to shallow-ish clone it for each branch
|
|
71
|
-
element.branches.map(
|
|
70
|
+
element.branches.map(branch =>
|
|
72
71
|
appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)
|
|
73
72
|
)
|
|
74
73
|
)
|
|
75
74
|
} else {
|
|
76
|
-
sequences = sequences.map(
|
|
75
|
+
sequences = sequences.map(base =>
|
|
77
76
|
appendElement(base, optional ? "optional" : "required", element)
|
|
78
77
|
)
|
|
79
78
|
}
|
|
80
79
|
}
|
|
81
80
|
return ctx.$.raw.schema(
|
|
82
81
|
sequences.map(
|
|
83
|
-
|
|
82
|
+
sequence =>
|
|
84
83
|
({
|
|
85
84
|
proto: Array,
|
|
86
85
|
sequence
|
|
@@ -143,10 +142,10 @@ const appendSpreadBranch = (
|
|
|
143
142
|
// the only array with no sequence reference is unknown[]
|
|
144
143
|
return appendElement(base, "variadic", tsKeywords.unknown)
|
|
145
144
|
}
|
|
146
|
-
spread.prefix.forEach(
|
|
147
|
-
spread.optional.forEach(
|
|
145
|
+
spread.prefix.forEach(node => appendElement(base, "required", node))
|
|
146
|
+
spread.optional.forEach(node => appendElement(base, "optional", node))
|
|
148
147
|
spread.variadic && appendElement(base, "variadic", spread.variadic)
|
|
149
|
-
spread.postfix.forEach(
|
|
148
|
+
spread.postfix.forEach(node => appendElement(base, "required", node))
|
|
150
149
|
return base
|
|
151
150
|
}
|
|
152
151
|
|
|
@@ -414,9 +413,9 @@ export type inferKeyOfExpression<operandDef, $, args> = show<
|
|
|
414
413
|
>
|
|
415
414
|
|
|
416
415
|
const parseBranchTuple: PostfixParser<"|" | "&"> = (def, ctx) => {
|
|
417
|
-
if (def[2] === undefined)
|
|
416
|
+
if (def[2] === undefined)
|
|
418
417
|
return throwParseError(writeMissingRightOperandMessage(def[1], ""))
|
|
419
|
-
|
|
418
|
+
|
|
420
419
|
const l = ctx.$.parse(def[0], ctx)
|
|
421
420
|
const r = ctx.$.parse(def[2], ctx)
|
|
422
421
|
return def[1] === "&" ? l.and(r) : l.or(r)
|
|
@@ -459,7 +458,7 @@ export const parseMorphTuple: PostfixParser<"=>"> = (def, ctx) => {
|
|
|
459
458
|
)
|
|
460
459
|
}
|
|
461
460
|
// TODO: nested morphs?
|
|
462
|
-
return ctx.$.parse(def[0], ctx).
|
|
461
|
+
return ctx.$.parse(def[0], ctx).pipe(def[2] as Morph)
|
|
463
462
|
}
|
|
464
463
|
|
|
465
464
|
export const writeMalformedFunctionalExpressionMessage = (
|
|
@@ -518,7 +517,7 @@ const prefixParsers: {
|
|
|
518
517
|
}
|
|
519
518
|
const branches = def
|
|
520
519
|
.slice(1)
|
|
521
|
-
.map(
|
|
520
|
+
.map(ctor =>
|
|
522
521
|
typeof ctor === "function" ?
|
|
523
522
|
ctx.$.node("proto", { proto: ctor as Constructor })
|
|
524
523
|
: throwParseError(
|
package/scope.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type ArkConfig,
|
|
3
3
|
type GenericProps,
|
|
4
|
+
type NodeParseContext,
|
|
4
5
|
type NodeParseOptions,
|
|
5
6
|
type PreparsedNodeResolution,
|
|
6
7
|
type RawSchema,
|
|
@@ -18,6 +19,7 @@ import {
|
|
|
18
19
|
type Dict,
|
|
19
20
|
domainOf,
|
|
20
21
|
hasDomain,
|
|
22
|
+
type isAnyOrNever,
|
|
21
23
|
isThunk,
|
|
22
24
|
type nominal,
|
|
23
25
|
type show,
|
|
@@ -40,11 +42,12 @@ import {
|
|
|
40
42
|
} from "./parser/generic.js"
|
|
41
43
|
import { DynamicState } from "./parser/string/reduce/dynamic.js"
|
|
42
44
|
import { fullStringParse } from "./parser/string/string.js"
|
|
43
|
-
import
|
|
44
|
-
DeclarationParser,
|
|
45
|
-
DefinitionParser,
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
import {
|
|
46
|
+
type DeclarationParser,
|
|
47
|
+
type DefinitionParser,
|
|
48
|
+
RawTypeParser,
|
|
49
|
+
type Type,
|
|
50
|
+
type TypeParser
|
|
48
51
|
} from "./type.js"
|
|
49
52
|
|
|
50
53
|
export type ScopeParser = <const def>(
|
|
@@ -126,12 +129,9 @@ export type resolve<reference extends keyof $ | keyof args, $, args> =
|
|
|
126
129
|
args[reference]
|
|
127
130
|
: $[reference & keyof $]
|
|
128
131
|
) extends infer resolution ?
|
|
129
|
-
resolution extends
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
resolution
|
|
133
|
-
: inferDefinition<def, $, args>
|
|
134
|
-
: resolution
|
|
132
|
+
isAnyOrNever<resolution> extends true ? resolution
|
|
133
|
+
: resolution extends Def<infer def> ? inferDefinition<def, $, args>
|
|
134
|
+
: resolution
|
|
135
135
|
: never
|
|
136
136
|
|
|
137
137
|
export type moduleKeyOf<$> = {
|
|
@@ -148,7 +148,7 @@ export type tryInferSubmoduleReference<$, token> =
|
|
|
148
148
|
: never
|
|
149
149
|
: never
|
|
150
150
|
|
|
151
|
-
export interface ParseContext extends
|
|
151
|
+
export interface ParseContext extends NodeParseContext {
|
|
152
152
|
$: RawScope
|
|
153
153
|
}
|
|
154
154
|
|
|
@@ -191,65 +191,41 @@ export class RawScope<
|
|
|
191
191
|
super(aliases, config)
|
|
192
192
|
}
|
|
193
193
|
|
|
194
|
-
type
|
|
195
|
-
if (args.length === 1) {
|
|
196
|
-
// treat as a simple definition
|
|
197
|
-
return this.parseRoot(args[0])
|
|
198
|
-
}
|
|
199
|
-
if (
|
|
200
|
-
args.length === 2 &&
|
|
201
|
-
typeof args[0] === "string" &&
|
|
202
|
-
args[0][0] === "<" &&
|
|
203
|
-
args[0].at(-1) === ">"
|
|
204
|
-
) {
|
|
205
|
-
// if there are exactly two args, the first of which looks like <${string}>,
|
|
206
|
-
// treat as a generic
|
|
207
|
-
const params = parseGenericParams(args[0].slice(1, -1))
|
|
208
|
-
const def = args[1]
|
|
209
|
-
// TODO: validateUninstantiatedGeneric, remove this cast
|
|
210
|
-
return new Generic(params, def, this as never) as never
|
|
211
|
-
}
|
|
212
|
-
// otherwise, treat as a tuple expression. technically, this also allows
|
|
213
|
-
// non-expression tuple definitions to be parsed, but it's not a supported
|
|
214
|
-
// part of the API as specified by the associated types
|
|
215
|
-
return this.parseRoot(args)
|
|
216
|
-
}
|
|
194
|
+
type = new RawTypeParser(this as never)
|
|
217
195
|
|
|
218
196
|
match: MatchParser<$> = createMatchParser(this as never) as never
|
|
219
197
|
|
|
220
|
-
declare()
|
|
221
|
-
|
|
222
|
-
}
|
|
198
|
+
declare = (() => ({
|
|
199
|
+
type: this.type
|
|
200
|
+
})).bind(this)
|
|
223
201
|
|
|
224
|
-
define(def: unknown)
|
|
225
|
-
return def
|
|
226
|
-
}
|
|
202
|
+
define = ((def: unknown) => def).bind(this)
|
|
227
203
|
|
|
228
204
|
override preparseRoot(def: unknown): unknown {
|
|
229
|
-
if (isThunk(def) && !hasArkKind(def, "generic"))
|
|
230
|
-
|
|
231
|
-
}
|
|
205
|
+
if (isThunk(def) && !hasArkKind(def, "generic")) return def()
|
|
206
|
+
|
|
232
207
|
return def
|
|
233
208
|
}
|
|
234
209
|
|
|
235
210
|
override parseRoot(def: unknown, opts?: NodeParseOptions): RawSchema {
|
|
211
|
+
// args: { this: {} as RawSchema },
|
|
236
212
|
return this.parse(def, {
|
|
237
|
-
args: { this: {} as RawSchema },
|
|
238
213
|
$: this as never,
|
|
214
|
+
raw: def,
|
|
239
215
|
...opts
|
|
240
216
|
}).bindScope(this)
|
|
241
217
|
}
|
|
242
218
|
|
|
243
219
|
parse(def: unknown, ctx: ParseContext): RawSchema {
|
|
244
220
|
if (typeof def === "string") {
|
|
245
|
-
if (ctx.args && Object.keys(ctx.args).every(
|
|
221
|
+
if (ctx.args && Object.keys(ctx.args).every(k => !def.includes(k))) {
|
|
246
222
|
// we can only rely on the cache if there are no contextual
|
|
247
223
|
// resolutions like "this" or generic args
|
|
248
224
|
return this.parseString(def, ctx)
|
|
249
225
|
}
|
|
250
|
-
if (!this.parseCache[def])
|
|
226
|
+
if (!this.parseCache[def])
|
|
251
227
|
this.parseCache[def] = this.parseString(def, ctx)
|
|
252
|
-
|
|
228
|
+
|
|
253
229
|
return this.parseCache[def]
|
|
254
230
|
}
|
|
255
231
|
return hasDomain(def, "object") ?
|