arktype 2.0.0-dev.1 → 2.0.0-dev.3
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 +252 -71
- package/__tests__/config.test.ts +75 -166
- package/__tests__/declared.test.ts +5 -4
- package/__tests__/divisor.test.ts +6 -2
- package/__tests__/instanceof.test.ts +1 -1
- package/__tests__/keyTraversal.test.ts +71 -70
- package/__tests__/match.bench.ts +31 -0
- package/__tests__/match.test.ts +216 -26
- package/__tests__/narrow.test.ts +3 -3
- package/__tests__/objectLiteral.test.ts +19 -18
- package/match.ts +206 -79
- package/out/match.d.ts +60 -31
- package/out/match.d.ts.map +1 -1
- package/out/match.js +52 -10
- package/out/match.js.map +1 -1
- package/out/parser/definition.d.ts +6 -4
- package/out/parser/definition.d.ts.map +1 -1
- package/out/parser/definition.js.map +1 -1
- package/out/parser/objectLiteral.d.ts.map +1 -1
- package/out/parser/objectLiteral.js +31 -35
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/semantic.d.ts +6 -7
- package/out/parser/semantic/semantic.d.ts.map +1 -1
- package/out/parser/shared.d.ts +10 -2
- package/out/parser/shared.d.ts.map +1 -1
- package/out/parser/shared.js +13 -4
- package/out/parser/shared.js.map +1 -1
- package/out/parser/string/reduce/static.d.ts +1 -1
- package/out/parser/string/reduce/static.d.ts.map +1 -1
- package/out/parser/tuple.d.ts +15 -14
- package/out/parser/tuple.d.ts.map +1 -1
- package/out/parser/tuple.js +94 -53
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts +6 -11
- package/out/scope.d.ts.map +1 -1
- package/out/scope.js +7 -25
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +5 -9
- package/out/type.d.ts.map +1 -1
- package/out/type.js +14 -10
- package/out/type.js.map +1 -1
- package/package.json +3 -3
- package/parser/definition.ts +7 -7
- package/parser/objectLiteral.ts +47 -51
- package/parser/semantic/semantic.ts +8 -9
- package/parser/semantic/validate.ts +1 -1
- package/parser/shared.ts +27 -9
- package/parser/string/reduce/static.ts +2 -2
- package/parser/tuple.ts +175 -86
- package/scope.ts +12 -35
- package/type.ts +20 -18
- package/parser/semantic/intersections.ts +0 -46
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
Constraints,
|
|
2
3
|
DateLiteral,
|
|
3
|
-
Refinements,
|
|
4
4
|
RegexLiteral,
|
|
5
5
|
distill,
|
|
6
|
+
inferIntersection,
|
|
6
7
|
is
|
|
7
8
|
} from "@arktype/schema"
|
|
8
9
|
import type {
|
|
9
10
|
BigintLiteral,
|
|
10
11
|
List,
|
|
11
12
|
NumberLiteral,
|
|
12
|
-
evaluate
|
|
13
|
-
extend
|
|
13
|
+
evaluate
|
|
14
14
|
} from "@arktype/util"
|
|
15
15
|
import type {
|
|
16
16
|
UnparsedScope,
|
|
@@ -25,7 +25,6 @@ import type {
|
|
|
25
25
|
LimitLiteral
|
|
26
26
|
} from "../string/reduce/shared.js"
|
|
27
27
|
import type { StringLiteral } from "../string/shift/operand/enclosed.js"
|
|
28
|
-
import type { inferIntersection } from "./intersections.js"
|
|
29
28
|
|
|
30
29
|
export type inferAstRoot<ast, $, args> = inferAst<ast, $, args, {}>
|
|
31
30
|
|
|
@@ -35,7 +34,7 @@ export type inferAst<
|
|
|
35
34
|
ast,
|
|
36
35
|
$,
|
|
37
36
|
args,
|
|
38
|
-
refinements extends
|
|
37
|
+
refinements extends Constraints
|
|
39
38
|
> = ast extends List
|
|
40
39
|
? inferExpression<ast, $, args, refinements>
|
|
41
40
|
: inferTerminal<ast, $, args, refinements>
|
|
@@ -49,7 +48,7 @@ export type inferExpression<
|
|
|
49
48
|
ast extends List,
|
|
50
49
|
$,
|
|
51
50
|
args,
|
|
52
|
-
refinements extends
|
|
51
|
+
refinements extends Constraints
|
|
53
52
|
> = ast extends GenericInstantiationAst
|
|
54
53
|
? inferDefinition<
|
|
55
54
|
ast[0]["definition"],
|
|
@@ -123,7 +122,7 @@ export type inferTerminal<
|
|
|
123
122
|
token,
|
|
124
123
|
$,
|
|
125
124
|
args,
|
|
126
|
-
refinements extends
|
|
125
|
+
refinements extends Constraints
|
|
127
126
|
> = token extends keyof args | keyof $
|
|
128
127
|
? {} extends refinements
|
|
129
128
|
? resolve<token, $, args>
|
|
@@ -131,9 +130,9 @@ export type inferTerminal<
|
|
|
131
130
|
: token extends StringLiteral<infer text>
|
|
132
131
|
? text
|
|
133
132
|
: token extends RegexLiteral
|
|
134
|
-
? is<string,
|
|
133
|
+
? is<string, evaluate<refinements & { [_ in token]: true }>>
|
|
135
134
|
: token extends DateLiteral
|
|
136
|
-
? is<Date,
|
|
135
|
+
? is<Date, evaluate<refinements & { [_ in token]: true }>>
|
|
137
136
|
: token extends NumberLiteral<infer value>
|
|
138
137
|
? value
|
|
139
138
|
: token extends BigintLiteral<infer value>
|
package/parser/shared.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BaseMeta, Node } from "@arktype/schema"
|
|
2
|
+
import type { evaluate } from "@arktype/util"
|
|
2
3
|
import type { validateDefinition } from "./definition.js"
|
|
3
4
|
import { Scanner } from "./string/shift/scanner.js"
|
|
4
5
|
|
|
@@ -11,15 +12,16 @@ type KeyParseResult<kind extends ParsedKeyKind = ParsedKeyKind> = {
|
|
|
11
12
|
innerKey: string | symbol
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
type
|
|
15
|
+
export type EntryValueParseResult<
|
|
16
|
+
kind extends ParsedValueKind = ParsedValueKind
|
|
17
|
+
> = {
|
|
15
18
|
kind: kind
|
|
16
19
|
innerValue: unknown
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
export type EntryParseResult<kind extends ParsedKeyKind = ParsedKeyKind> =
|
|
20
|
-
|
|
21
|
-
KeyParseResult<kind
|
|
22
|
-
{
|
|
23
|
+
evaluate<
|
|
24
|
+
KeyParseResult<kind> & {
|
|
23
25
|
innerValue: unknown
|
|
24
26
|
}
|
|
25
27
|
>
|
|
@@ -51,9 +53,9 @@ type validateObjectValueString<def, $, args> =
|
|
|
51
53
|
|
|
52
54
|
type DefinitionEntry = readonly [string | symbol, unknown]
|
|
53
55
|
|
|
54
|
-
const
|
|
56
|
+
export const parseEntryValue = (value: unknown): EntryValueParseResult => {
|
|
55
57
|
if (typeof value === "string") {
|
|
56
|
-
if (value
|
|
58
|
+
if (value.at(-1) === "?" && value.length > 1) {
|
|
57
59
|
return {
|
|
58
60
|
kind: "optional",
|
|
59
61
|
innerValue: value.slice(0, -1)
|
|
@@ -63,7 +65,8 @@ const getInnerValue = (value: unknown): ValueParseResult => {
|
|
|
63
65
|
if (value.length === 2 && value[1] === "?") {
|
|
64
66
|
return {
|
|
65
67
|
kind: "optional",
|
|
66
|
-
|
|
68
|
+
// allow redundant optionality like ["string?", "?"]
|
|
69
|
+
innerValue: parseEntryValue(value[0]).innerValue
|
|
67
70
|
}
|
|
68
71
|
}
|
|
69
72
|
}
|
|
@@ -87,7 +90,7 @@ export const parseEntry = ([key, value]: DefinitionEntry): EntryParseResult => {
|
|
|
87
90
|
: key === "\\..."
|
|
88
91
|
? { innerKey: "...", kind: "required" }
|
|
89
92
|
: { innerKey: key, kind: "required" }
|
|
90
|
-
const valueParseResult =
|
|
93
|
+
const valueParseResult = parseEntryValue(value)
|
|
91
94
|
return {
|
|
92
95
|
innerKey: keyParseResult.innerKey,
|
|
93
96
|
innerValue: valueParseResult.innerValue,
|
|
@@ -150,3 +153,18 @@ type parseKey<k> = k extends OptionalStringDefinition<infer inner>
|
|
|
150
153
|
kind: "required"
|
|
151
154
|
innerKey: k & (string | symbol)
|
|
152
155
|
}>
|
|
156
|
+
|
|
157
|
+
export const configureShallowDescendants = <node extends Node>(
|
|
158
|
+
node: node,
|
|
159
|
+
configOrDescription: BaseMeta | string
|
|
160
|
+
): node => {
|
|
161
|
+
const config: BaseMeta =
|
|
162
|
+
typeof configOrDescription === "string"
|
|
163
|
+
? { description: configOrDescription }
|
|
164
|
+
: (configOrDescription as never)
|
|
165
|
+
return node.transform(
|
|
166
|
+
(kind, inner) => ({ ...inner, ...config }),
|
|
167
|
+
// TODO: change to props
|
|
168
|
+
(node) => node.kind !== "required" && node.kind !== "optional"
|
|
169
|
+
) as never
|
|
170
|
+
}
|
|
@@ -24,8 +24,6 @@ export type StaticState = {
|
|
|
24
24
|
unscanned: string
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
export type AutocompletePrefix = `${StringifiablePrefixOperator} `
|
|
28
|
-
|
|
29
27
|
type BranchState = {
|
|
30
28
|
prefixes: StringifiablePrefixOperator[]
|
|
31
29
|
leftBound: OpenLeftBound | undefined
|
|
@@ -33,6 +31,8 @@ type BranchState = {
|
|
|
33
31
|
"|": unknown
|
|
34
32
|
}
|
|
35
33
|
|
|
34
|
+
export type AutocompletePrefix = `${StringifiablePrefixOperator} `
|
|
35
|
+
|
|
36
36
|
export namespace state {
|
|
37
37
|
export type initialize<def extends string> = from<{
|
|
38
38
|
root: undefined
|
package/parser/tuple.ts
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
import {
|
|
2
2
|
keywords,
|
|
3
|
+
makeRootAndArrayPropertiesMutable,
|
|
3
4
|
schema,
|
|
4
5
|
type BaseMeta,
|
|
5
6
|
type Morph,
|
|
7
|
+
type MorphChildKind,
|
|
8
|
+
type MutableInner,
|
|
9
|
+
type Node,
|
|
6
10
|
type Out,
|
|
7
11
|
type Predicate,
|
|
8
12
|
type Schema,
|
|
9
13
|
type TypeNode,
|
|
10
|
-
type
|
|
14
|
+
type UnionChildKind,
|
|
11
15
|
type extractIn,
|
|
12
16
|
type extractOut,
|
|
17
|
+
type inferIntersection,
|
|
13
18
|
type inferMorphOut,
|
|
14
19
|
type inferNarrow
|
|
15
20
|
} from "@arktype/schema"
|
|
16
21
|
import {
|
|
22
|
+
append,
|
|
17
23
|
isArray,
|
|
18
24
|
objectKindOrDomainOf,
|
|
19
25
|
printable,
|
|
@@ -21,6 +27,7 @@ import {
|
|
|
21
27
|
type BuiltinObjectKind,
|
|
22
28
|
type Constructor,
|
|
23
29
|
type Domain,
|
|
30
|
+
type ErrorMessage,
|
|
24
31
|
type List,
|
|
25
32
|
type conform,
|
|
26
33
|
type evaluate,
|
|
@@ -28,78 +35,144 @@ import {
|
|
|
28
35
|
} from "@arktype/util"
|
|
29
36
|
import type { ParseContext } from "../scope.js"
|
|
30
37
|
import type { inferDefinition, validateDefinition } from "./definition.js"
|
|
31
|
-
import type { inferIntersection } from "./semantic/intersections.js"
|
|
32
38
|
import type { InfixOperator, PostfixExpression } from "./semantic/semantic.js"
|
|
33
39
|
import { writeUnsatisfiableExpressionError } from "./semantic/validate.js"
|
|
34
40
|
import {
|
|
35
|
-
|
|
41
|
+
configureShallowDescendants,
|
|
42
|
+
parseEntryValue,
|
|
36
43
|
type EntryParseResult,
|
|
44
|
+
type EntryValueParseResult,
|
|
45
|
+
type parseEntry,
|
|
37
46
|
type validateObjectValue
|
|
38
47
|
} from "./shared.js"
|
|
39
48
|
import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js"
|
|
40
49
|
import type { BaseCompletions } from "./string/string.js"
|
|
41
50
|
|
|
42
|
-
export const parseTuple = (def:
|
|
51
|
+
export const parseTuple = (def: readonly unknown[], ctx: ParseContext) =>
|
|
43
52
|
maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx)
|
|
44
53
|
|
|
45
|
-
export const parseTupleLiteral = (
|
|
46
|
-
|
|
47
|
-
|
|
54
|
+
export const parseTupleLiteral = (
|
|
55
|
+
def: readonly unknown[],
|
|
56
|
+
ctx: ParseContext
|
|
57
|
+
): TypeNode => {
|
|
58
|
+
let sequences: MutableInner<"sequence">[] = [{}]
|
|
48
59
|
for (let i = 0; i < def.length; i++) {
|
|
49
|
-
let elementDef = def[i]
|
|
50
60
|
ctx.path.push(`${i}`)
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
elementDef.length === 2 &&
|
|
57
|
-
elementDef[0] === "..."
|
|
58
|
-
) {
|
|
59
|
-
elementDef = elementDef[1]
|
|
60
|
-
isVariadic = true
|
|
61
|
-
}
|
|
62
|
-
const parsedEntry = parseEntry([`${i}`, elementDef])
|
|
63
|
-
const value = ctx.scope.parse(parsedEntry.innerValue, ctx)
|
|
64
|
-
if (isVariadic) {
|
|
65
|
-
if (!value.extends(keywords.Array)) {
|
|
66
|
-
return throwParseError(writeNonArrayRestMessage(elementDef))
|
|
61
|
+
const parsedElementDef = parseSpreadable(def[i])
|
|
62
|
+
const element = ctx.scope.parse(parsedElementDef.innerValue, ctx)
|
|
63
|
+
if (parsedElementDef.spread) {
|
|
64
|
+
if (parsedElementDef.kind === "optional") {
|
|
65
|
+
return throwParseError(spreadOptionalMessage)
|
|
67
66
|
}
|
|
68
|
-
if (
|
|
69
|
-
return throwParseError(
|
|
67
|
+
if (!element.extends(keywords.Array)) {
|
|
68
|
+
return throwParseError(writeNonArraySpreadMessage(element))
|
|
70
69
|
}
|
|
71
|
-
//
|
|
72
|
-
|
|
73
|
-
//
|
|
74
|
-
|
|
70
|
+
// a spread must be distributed over branches e.g.:
|
|
71
|
+
// def: [string, ...(number[] | [true, false])]
|
|
72
|
+
// nodes: [string, ...number[]] | [string, true, false]
|
|
73
|
+
sequences = sequences.flatMap((base) =>
|
|
74
|
+
// since appendElement mutates base, we have to shallow-ish clone it for each branch
|
|
75
|
+
element.branches.map((branch) =>
|
|
76
|
+
appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)
|
|
77
|
+
)
|
|
78
|
+
)
|
|
75
79
|
} else {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
})
|
|
80
|
+
sequences = sequences.map((base) =>
|
|
81
|
+
appendElement(
|
|
82
|
+
base,
|
|
83
|
+
parsedElementDef.kind === "optional" ? "optional" : "required",
|
|
84
|
+
element
|
|
85
|
+
)
|
|
86
|
+
)
|
|
84
87
|
}
|
|
85
88
|
ctx.path.pop()
|
|
86
89
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
90
|
+
return schema(
|
|
91
|
+
...sequences.map((sequence) => ({
|
|
92
|
+
basis: Array,
|
|
93
|
+
sequence
|
|
94
|
+
}))
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const appendElement = (
|
|
99
|
+
base: MutableInner<"sequence">,
|
|
100
|
+
kind: "optional" | "required" | "variadic",
|
|
101
|
+
element: TypeNode
|
|
102
|
+
): MutableInner<"sequence"> => {
|
|
103
|
+
switch (kind) {
|
|
104
|
+
case "required":
|
|
105
|
+
if (base.optionals)
|
|
106
|
+
// e.g. [string?, number]
|
|
107
|
+
return throwParseError(requiredPostOptionalMessage)
|
|
108
|
+
if (base.variadic) {
|
|
109
|
+
// e.g. [...string[], number]
|
|
110
|
+
base.postfix = append(base.postfix, element)
|
|
111
|
+
} else {
|
|
112
|
+
// e.g. [string, number]
|
|
113
|
+
base.prefix = append(base.prefix, element)
|
|
114
|
+
}
|
|
115
|
+
return base
|
|
116
|
+
case "optional":
|
|
117
|
+
if (base.variadic)
|
|
118
|
+
// e.g. [...string[], number?]
|
|
119
|
+
return throwParseError(optionalPostVariadicMessage)
|
|
120
|
+
// e.g. [string, number?]
|
|
121
|
+
base.optionals = append(base.optionals, element)
|
|
122
|
+
return base
|
|
123
|
+
case "variadic":
|
|
124
|
+
// e.g. [...string[], number, ...string[]]
|
|
125
|
+
if (base.postfix) throwParseError(multipleVariadicMesage)
|
|
126
|
+
if (base.variadic) {
|
|
127
|
+
if (!base.variadic.equals(element)) {
|
|
128
|
+
// e.g. [...string[], ...number[]]
|
|
129
|
+
throwParseError(multipleVariadicMesage)
|
|
130
|
+
}
|
|
131
|
+
// e.g. [...string[], ...string[]]
|
|
132
|
+
// do nothing, second spread doesn't change the type
|
|
133
|
+
} else {
|
|
134
|
+
// e.g. [string, ...number[]]
|
|
135
|
+
base.variadic = element
|
|
136
|
+
}
|
|
137
|
+
return base
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const appendSpreadBranch = (
|
|
142
|
+
base: MutableInner<"sequence">,
|
|
143
|
+
branch: Node<UnionChildKind>
|
|
144
|
+
): MutableInner<"sequence"> => {
|
|
145
|
+
const spread = branch.firstReferenceOfKind("sequence")
|
|
146
|
+
if (!spread) {
|
|
147
|
+
// the only array with no sequence reference is unknown[]
|
|
148
|
+
return appendElement(base, "variadic", keywords.unknown)
|
|
149
|
+
}
|
|
150
|
+
spread.prefix.forEach((node) => appendElement(base, "required", node))
|
|
151
|
+
spread.optionals.forEach((node) => appendElement(base, "optional", node))
|
|
152
|
+
spread.variadic && appendElement(base, "variadic", spread.variadic)
|
|
153
|
+
spread.postfix.forEach((node) => appendElement(base, "required", node))
|
|
154
|
+
return base
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
type TupleElementParseResult = EntryValueParseResult & { spread?: true }
|
|
158
|
+
|
|
159
|
+
const parseSpreadable = (elementDef: unknown): TupleElementParseResult => {
|
|
160
|
+
if (typeof elementDef === "string" && elementDef.startsWith("...")) {
|
|
161
|
+
// variadic string definition like "...string[]"
|
|
162
|
+
return { ...parseEntryValue(elementDef.slice(3)), spread: true }
|
|
163
|
+
}
|
|
164
|
+
if (
|
|
165
|
+
isArray(elementDef) &&
|
|
166
|
+
elementDef.length === 2 &&
|
|
167
|
+
elementDef[0] === "..."
|
|
168
|
+
) {
|
|
169
|
+
// variadic tuple expression like ["...", { a: "1" }]
|
|
170
|
+
return { ...parseEntryValue(elementDef[1]), spread: true }
|
|
97
171
|
}
|
|
98
|
-
|
|
99
|
-
return schema(Array)
|
|
172
|
+
return parseEntryValue(elementDef)
|
|
100
173
|
}
|
|
101
174
|
|
|
102
|
-
|
|
175
|
+
const maybeParseTupleExpression = (
|
|
103
176
|
def: List,
|
|
104
177
|
ctx: ParseContext
|
|
105
178
|
): TypeNode | undefined => {
|
|
@@ -155,28 +228,32 @@ export type validateTupleLiteral<
|
|
|
155
228
|
tail,
|
|
156
229
|
$,
|
|
157
230
|
args,
|
|
158
|
-
[...result, validateTupleElement<head,
|
|
231
|
+
[...result, validateTupleElement<head, result, $, args>]
|
|
159
232
|
>
|
|
160
233
|
: result
|
|
161
234
|
|
|
162
|
-
type validateTupleElement<
|
|
163
|
-
head
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
235
|
+
type validateTupleElement<
|
|
236
|
+
head,
|
|
237
|
+
result extends unknown[],
|
|
238
|
+
$,
|
|
239
|
+
args
|
|
240
|
+
> = head extends variadicExpression<infer operand>
|
|
241
|
+
? validateDefinition<operand, $, args> extends infer syntacticResult
|
|
242
|
+
? syntacticResult extends operand
|
|
243
|
+
? semanticallyValidateRestElement<
|
|
244
|
+
operand,
|
|
245
|
+
$,
|
|
246
|
+
args
|
|
247
|
+
> extends infer semanticResult
|
|
248
|
+
? semanticResult extends operand
|
|
249
|
+
? Extract<result[number], variadicExpression> extends never
|
|
250
|
+
? head
|
|
251
|
+
: ErrorMessage<multipleVariadicMessage>
|
|
252
|
+
: semanticResult
|
|
253
|
+
: never
|
|
254
|
+
: syntacticResult
|
|
255
|
+
: never
|
|
256
|
+
: validateObjectValue<head, $, args>
|
|
180
257
|
|
|
181
258
|
type semanticallyValidateRestElement<operand, $, args> = inferDefinition<
|
|
182
259
|
operand,
|
|
@@ -184,26 +261,39 @@ type semanticallyValidateRestElement<operand, $, args> = inferDefinition<
|
|
|
184
261
|
args
|
|
185
262
|
> extends infer result
|
|
186
263
|
? result extends never
|
|
187
|
-
?
|
|
264
|
+
? writeNonArraySpreadMessage<operand>
|
|
188
265
|
: isAny<result> extends true
|
|
189
|
-
?
|
|
266
|
+
? writeNonArraySpreadMessage<operand>
|
|
190
267
|
: result extends readonly unknown[]
|
|
191
268
|
? operand
|
|
192
|
-
:
|
|
269
|
+
: writeNonArraySpreadMessage<operand>
|
|
193
270
|
: never
|
|
194
271
|
|
|
195
|
-
export const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
272
|
+
export const writeNonArraySpreadMessage = <operand extends string | TypeNode>(
|
|
273
|
+
operand: operand
|
|
274
|
+
) =>
|
|
275
|
+
`Spread element must be an array (was ${operand})` as writeNonArraySpreadMessage<operand>
|
|
199
276
|
|
|
200
|
-
type
|
|
201
|
-
|
|
202
|
-
|
|
277
|
+
type writeNonArraySpreadMessage<operand> =
|
|
278
|
+
`Spread element must be an array${operand extends string
|
|
279
|
+
? `(was ${operand})`
|
|
280
|
+
: ""}`
|
|
203
281
|
|
|
204
|
-
export const
|
|
282
|
+
export const multipleVariadicMesage = `A tuple may have at most one variadic element`
|
|
205
283
|
|
|
206
|
-
type
|
|
284
|
+
type multipleVariadicMessage = typeof multipleVariadicMesage
|
|
285
|
+
|
|
286
|
+
export const requiredPostOptionalMessage = `A required element may not follow an optional element`
|
|
287
|
+
|
|
288
|
+
type requiredPostOptionalMessage = typeof requiredPostOptionalMessage
|
|
289
|
+
|
|
290
|
+
export const optionalPostVariadicMessage = `An optional element may not follow a variadic element`
|
|
291
|
+
|
|
292
|
+
type optionalPostVariadicMessage = typeof optionalPostVariadicMessage
|
|
293
|
+
|
|
294
|
+
export const spreadOptionalMessage = `A spread element cannot be optional`
|
|
295
|
+
|
|
296
|
+
type spreadOptionalMessage = typeof optionalPostVariadicMessage
|
|
207
297
|
|
|
208
298
|
type inferTupleLiteral<
|
|
209
299
|
def extends List,
|
|
@@ -387,7 +477,7 @@ export const parseMorphTuple: PostfixParser<"=>"> = (def, ctx) => {
|
|
|
387
477
|
}
|
|
388
478
|
// TODO: nested morphs?
|
|
389
479
|
return schema({
|
|
390
|
-
in: ctx.scope.parse(def[0], ctx) as Schema<
|
|
480
|
+
in: ctx.scope.parse(def[0], ctx) as Schema<MorphChildKind>,
|
|
391
481
|
morph: def[2] as Morph
|
|
392
482
|
})
|
|
393
483
|
}
|
|
@@ -417,9 +507,8 @@ export const parseNarrowTuple: PostfixParser<":"> = (def, ctx) => {
|
|
|
417
507
|
.constrain("predicate", def[2] as Predicate)
|
|
418
508
|
}
|
|
419
509
|
|
|
420
|
-
const parseAttributeTuple: PostfixParser<"@"> = (def, ctx) =>
|
|
421
|
-
|
|
422
|
-
}
|
|
510
|
+
const parseAttributeTuple: PostfixParser<"@"> = (def, ctx) =>
|
|
511
|
+
configureShallowDescendants(ctx.scope.parse(def[0], ctx), def[2] as never)
|
|
423
512
|
|
|
424
513
|
const indexOneParsers: {
|
|
425
514
|
[token in IndexOneOperator]: PostfixParser<token>
|
package/scope.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BaseType,
|
|
3
3
|
keywords,
|
|
4
|
-
type
|
|
5
|
-
type KeyCheckKind,
|
|
4
|
+
type ArkConfig,
|
|
6
5
|
type TypeNode,
|
|
7
6
|
type extractIn,
|
|
8
7
|
type extractOut
|
|
@@ -11,7 +10,7 @@ import {
|
|
|
11
10
|
domainOf,
|
|
12
11
|
hasDomain,
|
|
13
12
|
isThunk,
|
|
14
|
-
|
|
13
|
+
morph,
|
|
15
14
|
throwParseError,
|
|
16
15
|
type Dict,
|
|
17
16
|
type evaluate,
|
|
@@ -47,7 +46,6 @@ import {
|
|
|
47
46
|
type DefinitionParser,
|
|
48
47
|
type Generic,
|
|
49
48
|
type GenericProps,
|
|
50
|
-
type TypeConfig,
|
|
51
49
|
type TypeParser
|
|
52
50
|
} from "./type.js"
|
|
53
51
|
import { addArkKind, hasArkKind, type arkKind } from "./util.js"
|
|
@@ -68,10 +66,8 @@ export type ScopeParser<parent, ambient> = {
|
|
|
68
66
|
}>
|
|
69
67
|
}
|
|
70
68
|
|
|
71
|
-
export
|
|
69
|
+
export interface ArkScopeConfig extends ArkConfig {
|
|
72
70
|
ambient?: Scope | null
|
|
73
|
-
codes?: Record<ArkErrorCode, { mustBe?: string }>
|
|
74
|
-
keys?: KeyCheckKind
|
|
75
71
|
}
|
|
76
72
|
|
|
77
73
|
type validateScope<def, $> = {
|
|
@@ -228,7 +224,7 @@ export class Scope<r extends Resolutions = any> {
|
|
|
228
224
|
declare infer: extractOut<r["exports"]>
|
|
229
225
|
declare inferIn: extractIn<r["exports"]>
|
|
230
226
|
|
|
231
|
-
config:
|
|
227
|
+
config: ArkScopeConfig
|
|
232
228
|
|
|
233
229
|
private parseCache: Record<string, TypeNode> = {}
|
|
234
230
|
private resolutions: MergedResolutions
|
|
@@ -239,7 +235,7 @@ export class Scope<r extends Resolutions = any> {
|
|
|
239
235
|
private exportedNames: exportedName<r>[] = []
|
|
240
236
|
private ambient: Scope | null
|
|
241
237
|
|
|
242
|
-
constructor(def: Dict, config:
|
|
238
|
+
constructor(def: Dict, config: ArkScopeConfig) {
|
|
243
239
|
for (const k in def) {
|
|
244
240
|
const parsedKey = parseScopeKey(k)
|
|
245
241
|
this.aliases[parsedKey.name] = parsedKey.params.length
|
|
@@ -273,7 +269,7 @@ export class Scope<r extends Resolutions = any> {
|
|
|
273
269
|
|
|
274
270
|
scope: ScopeParser<r["exports"], r["ambient"]> = ((
|
|
275
271
|
def: Dict,
|
|
276
|
-
config:
|
|
272
|
+
config: ArkScopeConfig = {}
|
|
277
273
|
) => {
|
|
278
274
|
return new Scope(def, {
|
|
279
275
|
ambient: this.ambient,
|
|
@@ -403,7 +399,7 @@ export class Scope<r extends Resolutions = any> {
|
|
|
403
399
|
names extends [] ? keyof r["exports"] & string : names[number]
|
|
404
400
|
> {
|
|
405
401
|
return addArkKind(
|
|
406
|
-
|
|
402
|
+
morph(this.export(...names) as Dict, (alias, value) => [
|
|
407
403
|
`#${alias}`,
|
|
408
404
|
value
|
|
409
405
|
]) as never,
|
|
@@ -411,28 +407,6 @@ export class Scope<r extends Resolutions = any> {
|
|
|
411
407
|
) as never
|
|
412
408
|
}
|
|
413
409
|
|
|
414
|
-
compile() {
|
|
415
|
-
return ""
|
|
416
|
-
// this.export()
|
|
417
|
-
// const references: Set<Root> = new Set()
|
|
418
|
-
// for (const k in this.exportedResolutions!) {
|
|
419
|
-
// const resolution = this.exportedResolutions[k]
|
|
420
|
-
// if (hasArkKind(resolution, "node") && !references.has(resolution)) {
|
|
421
|
-
// for (const reference of resolution.references) {
|
|
422
|
-
// references.add(reference)
|
|
423
|
-
// }
|
|
424
|
-
// }
|
|
425
|
-
// }
|
|
426
|
-
// return [...references]
|
|
427
|
-
// .map(
|
|
428
|
-
// (ref) => `const ${ref.alias} = (${In}) => {
|
|
429
|
-
// ${ref.condition}
|
|
430
|
-
// return true
|
|
431
|
-
// }`
|
|
432
|
-
// )
|
|
433
|
-
// .join("\n")
|
|
434
|
-
}
|
|
435
|
-
|
|
436
410
|
bindThis() {
|
|
437
411
|
// TODO: fix
|
|
438
412
|
return { this: keywords.unknown }
|
|
@@ -470,7 +444,10 @@ export class Scope<r extends Resolutions = any> {
|
|
|
470
444
|
}
|
|
471
445
|
const namesToExport = names.length ? names : this.exportedNames
|
|
472
446
|
return addArkKind(
|
|
473
|
-
|
|
447
|
+
morph(namesToExport, (_, name) => [
|
|
448
|
+
name,
|
|
449
|
+
this.exportCache![name]
|
|
450
|
+
]) as never,
|
|
474
451
|
"module"
|
|
475
452
|
) as never
|
|
476
453
|
}
|
|
@@ -484,7 +461,7 @@ const resolutionsOfModule = (typeSet: ExportCache) => {
|
|
|
484
461
|
const v = typeSet[k]
|
|
485
462
|
if (hasArkKind(v, "module")) {
|
|
486
463
|
const innerResolutions = resolutionsOfModule(v as never)
|
|
487
|
-
const prefixedResolutions =
|
|
464
|
+
const prefixedResolutions = morph(innerResolutions, (innerK, innerV) => [
|
|
488
465
|
`${k}.${innerK}`,
|
|
489
466
|
innerV
|
|
490
467
|
])
|