Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
Unexpected: rename ast to expected and make the field required
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Dec 28, 2023
1 parent 719ba0f commit f173e75
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 32 deletions.
1 change: 1 addition & 0 deletions .changeset/gentle-readers-call.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Refactor `ParseResult` module:
- rename `UnionMember` to `Member`
- ast `ast` field to `Member`
- replace `Index` with `Tuple`
- `Unexpected`: rename `ast` to `expected` and make the field required
4 changes: 2 additions & 2 deletions docs/modules/ParseResult.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ Added in v1.0.0
**Signature**
```ts
export declare const unexpected: (ast: Option.Option<AST.AST>) => Unexpected
export declare const unexpected: (expected: AST.AST) => Unexpected
```
Added in v1.0.0
Expand Down Expand Up @@ -327,7 +327,7 @@ Error that occurs when an unexpected key or index is present.
```ts
export interface Unexpected {
readonly _tag: "Unexpected"
readonly ast: Option.Option<AST.AST>
readonly expected: AST.AST
}
```

Expand Down
4 changes: 1 addition & 3 deletions src/ArrayFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @since 1.0.0
*/
import * as Option from "effect/Option"
import * as ReadonlyArray from "effect/ReadonlyArray"
import type { ParseIssue } from "./ParseResult.js"
import { formatExpected, getMessage } from "./TreeFormatter.js"
Expand Down Expand Up @@ -31,8 +30,7 @@ const format = (self: ParseIssue, path: ReadonlyArray<PropertyKey> = []): Array<
return [{
_tag,
path,
message: "Unexpected" +
(Option.isSome(self.ast) ? `, expected ${formatExpected(self.ast.value)}` : "")
message: `Unexpected, expected ${formatExpected(self.expected)}`
}]
case "Union":
return ReadonlyArray.flatMap(self.errors, (e) => {
Expand Down
6 changes: 3 additions & 3 deletions src/ParseResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,16 @@ export const missing: Missing = { _tag: "Missing" }
*/
export interface Unexpected {
readonly _tag: "Unexpected"
readonly ast: Option.Option<AST.AST>
readonly expected: AST.AST
}

/**
* @category constructors
* @since 1.0.0
*/
export const unexpected = (
ast: Option.Option<AST.AST>
): Unexpected => ({ _tag: "Unexpected", ast })
expected: AST.AST
): Unexpected => ({ _tag: "Unexpected", expected })

/**
* Error that occurs when a union has an error.
Expand Down
5 changes: 3 additions & 2 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser<any, any> => {
if (Option.isSome(ast.rest)) {
requiredLen += ast.rest.value.length - 1
}
const expectedAST = AST.createUnion(ast.elements.map((_, i) => AST.createLiteral(i)))
return (input: unknown, options) => {
if (!Array.isArray(input)) {
return ParseResult.fail(ParseResult.type(ast, input))
Expand All @@ -383,7 +384,7 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser<any, any> => {
// ---------------------------------------------
if (Option.isNone(ast.rest)) {
for (let i = ast.elements.length; i <= len - 1; i++) {
const e = ParseResult.index(i, [ParseResult.unexpected(Option.none())])
const e = ParseResult.index(i, [ParseResult.unexpected(expectedAST)])
if (allErrors) {
es.push([stepKey++, e])
continue
Expand Down Expand Up @@ -614,7 +615,7 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser<any, any> => {
for (const key of Internal.ownKeys(input)) {
const eu = ParseResult.eitherOrUndefined(expected(key, options))
if (eu && Either.isLeft(eu)) {
const e = ParseResult.key(key, [ParseResult.unexpected(Option.some(expectedAST))])
const e = ParseResult.key(key, [ParseResult.unexpected(expectedAST)])
if (allErrors) {
es.push([stepKey++, e])
continue
Expand Down
2 changes: 1 addition & 1 deletion src/TreeFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const go = (e: ParseIssue): Tree<string> => {
return make("is forbidden")
case "Unexpected":
return make(
"is unexpected" + (Option.isSome(e.ast) ? `, expected ${formatExpected(e.ast.value)}` : "")
`is unexpected, expected ${formatExpected(e.expected)}`
)
case "Key": {
const es = e.errors.map(go)
Expand Down
2 changes: 1 addition & 1 deletion test/ArrayFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe("ArrayFormatter", () => {
expectIssues(schema, ["a", 1], [{
_tag: "Unexpected",
path: [1],
message: "Unexpected"
message: "Unexpected, expected 0"
}])
})

Expand Down
8 changes: 4 additions & 4 deletions test/Schema/allErrors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ describe("Schema > allErrors option", () => {
["a", "b"],
`Tuple or array: <anonymous tuple or array schema>
├─ [0]
│ └─ is unexpected
│ └─ is unexpected, expected never
└─ [1]
└─ is unexpected`,
└─ is unexpected, expected never`,
Util.allErrors
)
})
Expand Down Expand Up @@ -174,9 +174,9 @@ describe("Schema > allErrors option", () => {
[1, 1] as any,
`Tuple or array: <anonymous tuple or array schema>
├─ [0]
│ └─ is unexpected
│ └─ is unexpected, expected never
└─ [1]
└─ is unexpected`,
└─ is unexpected, expected never`,
Util.allErrors
)
})
Expand Down
12 changes: 6 additions & 6 deletions test/Schema/onExcess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ describe("Schema > onExcess", () => {
[1, "b"],
`Tuple or array: <anonymous tuple or array schema>
└─ [1]
└─ is unexpected`
└─ is unexpected, expected 0`
)
await Util.expectEncodeFailure(
schema,
[1, "b"] as any,
`Tuple or array: <anonymous tuple or array schema>
└─ [1]
└─ is unexpected`
└─ is unexpected, expected 0`
)
})

Expand Down Expand Up @@ -70,11 +70,11 @@ describe("Schema > onExcess", () => {
├─ Union member: <anonymous tuple or array schema>
│ └─ Tuple or array: <anonymous tuple or array schema>
│ └─ [2]
│ └─ is unexpected
│ └─ is unexpected, expected 0 or 1
└─ Union member: <anonymous tuple or array schema>
└─ Tuple or array: <anonymous tuple or array schema>
└─ [1]
└─ is unexpected`
└─ is unexpected, expected 0`
)
await Util.expectParseFailure(
schema,
Expand All @@ -83,11 +83,11 @@ describe("Schema > onExcess", () => {
├─ Union member: <anonymous tuple or array schema>
│ └─ Tuple or array: <anonymous tuple or array schema>
│ └─ [2]
│ └─ is unexpected
│ └─ is unexpected, expected 0 or 1
└─ Union member: <anonymous tuple or array schema>
└─ Tuple or array: <anonymous tuple or array schema>
└─ [1]
└─ is unexpected`,
└─ is unexpected, expected 0`,
Util.onExcessPropertyError
)
await Util.expectEncodeSuccess(
Expand Down
20 changes: 10 additions & 10 deletions test/Schema/tuple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ describe("Schema > tuple", () => {
[undefined],
`Tuple or array: <anonymous tuple or array schema>
└─ [0]
└─ is unexpected`
└─ is unexpected, expected never`
)
await Util.expectParseFailure(
schema,
[1],
`Tuple or array: <anonymous tuple or array schema>
└─ [0]
└─ is unexpected`
└─ is unexpected, expected never`
)
})

Expand Down Expand Up @@ -99,7 +99,7 @@ describe("Schema > tuple", () => {
[1, "b"],
`Tuple or array: <anonymous tuple or array schema>
└─ [1]
└─ is unexpected`
└─ is unexpected, expected 0`
)
})

Expand Down Expand Up @@ -136,7 +136,7 @@ describe("Schema > tuple", () => {
[1, "b"],
`Tuple or array: <anonymous tuple or array schema>
└─ [1]
└─ is unexpected`
└─ is unexpected, expected 0`
)
})

Expand All @@ -162,7 +162,7 @@ describe("Schema > tuple", () => {
[1, "b"],
`Tuple or array: <anonymous tuple or array schema>
└─ [1]
└─ is unexpected`
└─ is unexpected, expected 0`
)
})

Expand Down Expand Up @@ -193,7 +193,7 @@ describe("Schema > tuple", () => {
[1, "b"],
`Tuple or array: <anonymous tuple or array schema>
└─ [1]
└─ is unexpected`
└─ is unexpected, expected 0`
)
})

Expand Down Expand Up @@ -365,7 +365,7 @@ describe("Schema > tuple", () => {
[1, "b"] as any,
`Tuple or array: <anonymous tuple or array schema>
└─ [1]
└─ is unexpected`
└─ is unexpected, expected 0`
)
})

Expand All @@ -378,7 +378,7 @@ describe("Schema > tuple", () => {
[1, "b"] as any,
`Tuple or array: <anonymous tuple or array schema>
└─ [1]
└─ is unexpected`
└─ is unexpected, expected 0`
)
})

Expand All @@ -398,7 +398,7 @@ describe("Schema > tuple", () => {
[1, "b"] as any,
`Tuple or array: <anonymous tuple or array schema>
└─ [1]
└─ is unexpected`
└─ is unexpected, expected 0`
)
})

Expand All @@ -412,7 +412,7 @@ describe("Schema > tuple", () => {
[1, "b"] as any,
`Tuple or array: <anonymous tuple or array schema>
└─ [1]
└─ is unexpected`
└─ is unexpected, expected 0`
)
})

Expand Down

0 comments on commit f173e75

Please sign in to comment.