Skip to content

Commit 4e83c80

Browse files
Migrate zod dependency to v4 (#3034)
1 parent a1d3552 commit 4e83c80

File tree

6 files changed

+66
-45
lines changed

6 files changed

+66
-45
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
5757
},
5858
"overrides": {
59-
"@docusaurus/core>webpack-dev-server": "5.2.2"
59+
"@docusaurus/core>webpack-dev-server": "5.2.2",
60+
"@yarnpkg/shell>cross-spawn": "7.0.6"
6061
}
6162
}
6263
}

packages/cursorless-engine/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"nearley": "2.20.1",
3939
"talon-snippets": "1.3.0",
4040
"uuid": "11.1.0",
41-
"zod": "3.25.76"
41+
"zod": "4.0.2"
4242
},
4343
"devDependencies": {
4444
"@types/js-yaml": "4.0.9",

packages/cursorless-engine/src/languages/TreeSitterQuery/PredicateOperatorSchemaTypes.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ import type { MutableQueryCapture } from "./QueryCapture";
1313
* captures to nodes, because that will be done dynamically each time we run a
1414
* query.
1515
*/
16-
type OperandListSchemaType = z.ZodType<
17-
SchemaOutputType[],
18-
z.ZodTypeDef,
19-
SchemaInputType[]
20-
>;
16+
type OperandListSchemaType = z.ZodType<SchemaOutputType[], SchemaInputType[]>;
2117

2218
// These two types are used to allow us to infer the schema type from the
2319
// operator type. For example, if we have a type `NotType` that extends

packages/cursorless-engine/src/languages/TreeSitterQuery/constructZodErrorMessages.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,52 @@ import { operandToString } from "./predicateToString";
44

55
export function constructZodErrorMessages(
66
inputOperands: PredicateStep[],
7-
error: z.ZodError<PredicateStep[]>,
7+
error: z.ZodError<z.core.output<PredicateStep[]>>,
88
): string[] {
9-
return error.errors
9+
return error.issues
1010
.filter(
1111
// If the user has supplied a capture instead of a string, or vice versa,
12-
// we'll get two errors instead of one; we prefer to show the more helpful
12+
// we'll get two issues instead of one; we prefer to show the more helpful
1313
// one.
14-
(error) =>
14+
(issue) =>
1515
!(
16-
error.code === "invalid_type" &&
17-
error.path.length === 2 &&
18-
(error.path[1] === "name" || error.path[1] === "value")
16+
issue.code === "invalid_type" &&
17+
issue.path.length === 2 &&
18+
(issue.path[1] === "name" || issue.path[1] === "value")
1919
),
2020
)
21-
.map((error) => getErrorMessage(inputOperands, error));
21+
.map((issue) => getErrorMessage(inputOperands, issue));
2222
}
2323

24-
function getErrorMessage(inputOperands: PredicateStep[], error: z.ZodIssue) {
25-
if (error.path.length === 0) {
26-
if (error.code === "too_small") {
24+
function getErrorMessage(
25+
inputOperands: PredicateStep[],
26+
issue: z.core.$ZodIssue,
27+
) {
28+
if (issue.path.length === 0) {
29+
if (issue.code === "too_small") {
2730
return "Too few arguments";
28-
} else if (error.code === "too_big") {
31+
} else if (issue.code === "too_big") {
2932
return "Too many arguments";
3033
}
3134

32-
return error.message;
35+
return issue.message;
36+
}
37+
38+
const argIndex = issue.path[0] as number;
39+
40+
if (argIndex >= inputOperands.length) {
41+
return "Too few arguments";
3342
}
3443

35-
let message = error.message;
44+
let message = issue.message;
3645

37-
if (error.code === "invalid_literal" && error.path[1] === "type") {
46+
if (issue.code === "invalid_value" && issue.path[1] === "type") {
3847
message =
39-
error.expected === "capture"
48+
issue.values[0] === "capture"
4049
? "Capture names must be prefixed with @"
4150
: "Expected string, but received capture";
4251
}
4352

44-
const argIndex = error.path[0] as number;
4553
const operandString = operandToString(inputOperands[argIndex]);
4654
return `Error on argument ${argIndex} (\`${operandString}\`): ${message}`;
4755
}

packages/cursorless-engine/src/languages/TreeSitterQuery/parsePredicates.test.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ const predicates: QueryPredicate[][] = [
3232
],
3333
},
3434

35+
// (#is-nth-child? @statement 1 2)
36+
// Error: too many orgs
37+
{
38+
operator: "is-nth-child?",
39+
operands: [
40+
{
41+
type: "capture",
42+
name: "statement",
43+
},
44+
{
45+
type: "string",
46+
value: "1",
47+
},
48+
{
49+
type: "string",
50+
value: "2",
51+
},
52+
],
53+
},
54+
3555
// (#not-parent-type? statement foo)
3656
// Error: capture names must be prefixed with @
3757
{
@@ -95,19 +115,24 @@ const expectedErrors = [
95115
{
96116
patternIdx: 0,
97117
predicateIdx: 2,
118+
error: "Too many arguments",
119+
},
120+
{
121+
patternIdx: 0,
122+
predicateIdx: 3,
98123
error:
99124
"Error on argument 0 (`statement`): Capture names must be prefixed with @",
100125
},
101126
{
102127
patternIdx: 0,
103-
predicateIdx: 3,
128+
predicateIdx: 4,
104129
error: "Error on argument 1 (`hello`): Expected an integer",
105130
},
106131
{
132+
patternIdx: 0,
133+
predicateIdx: 5,
107134
error:
108135
"Error on argument 2 (`@foo`): Expected string, but received capture",
109-
patternIdx: 0,
110-
predicateIdx: 4,
111136
},
112137
];
113138

pnpm-lock.yaml

Lines changed: 9 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)