Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"nearley@2.20.1": "patches/nearley@2.20.1.patch"
},
"overrides": {
"@docusaurus/core>webpack-dev-server": "5.2.2"
"@docusaurus/core>webpack-dev-server": "5.2.2",
"@yarnpkg/shell>cross-spawn": "7.0.6"
}
}
}
2 changes: 1 addition & 1 deletion packages/cursorless-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"nearley": "2.20.1",
"talon-snippets": "1.3.0",
"uuid": "11.1.0",
"zod": "3.25.76"
"zod": "4.0.2"
},
"devDependencies": {
"@types/js-yaml": "4.0.9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import type { MutableQueryCapture } from "./QueryCapture";
* captures to nodes, because that will be done dynamically each time we run a
* query.
*/
type OperandListSchemaType = z.ZodType<
SchemaOutputType[],
z.ZodTypeDef,
SchemaInputType[]
>;
type OperandListSchemaType = z.ZodType<SchemaOutputType[], SchemaInputType[]>;

// These two types are used to allow us to infer the schema type from the
// operator type. For example, if we have a type `NotType` that extends
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,52 @@ import { operandToString } from "./predicateToString";

export function constructZodErrorMessages(
inputOperands: PredicateStep[],
error: z.ZodError<PredicateStep[]>,
error: z.ZodError<z.core.output<PredicateStep[]>>,
): string[] {
return error.errors
return error.issues
.filter(
// If the user has supplied a capture instead of a string, or vice versa,
// we'll get two errors instead of one; we prefer to show the more helpful
// we'll get two issues instead of one; we prefer to show the more helpful
// one.
(error) =>
(issue) =>
!(
error.code === "invalid_type" &&
error.path.length === 2 &&
(error.path[1] === "name" || error.path[1] === "value")
issue.code === "invalid_type" &&
issue.path.length === 2 &&
(issue.path[1] === "name" || issue.path[1] === "value")
),
)
.map((error) => getErrorMessage(inputOperands, error));
.map((issue) => getErrorMessage(inputOperands, issue));
}

function getErrorMessage(inputOperands: PredicateStep[], error: z.ZodIssue) {
if (error.path.length === 0) {
if (error.code === "too_small") {
function getErrorMessage(
inputOperands: PredicateStep[],
issue: z.core.$ZodIssue,
) {
if (issue.path.length === 0) {
if (issue.code === "too_small") {
return "Too few arguments";
} else if (error.code === "too_big") {
} else if (issue.code === "too_big") {
return "Too many arguments";
}

return error.message;
return issue.message;
}

const argIndex = issue.path[0] as number;

if (argIndex >= inputOperands.length) {
return "Too few arguments";
}

let message = error.message;
let message = issue.message;

if (error.code === "invalid_literal" && error.path[1] === "type") {
if (issue.code === "invalid_value" && issue.path[1] === "type") {
message =
error.expected === "capture"
issue.values[0] === "capture"
? "Capture names must be prefixed with @"
: "Expected string, but received capture";
}

const argIndex = error.path[0] as number;
const operandString = operandToString(inputOperands[argIndex]);
return `Error on argument ${argIndex} (\`${operandString}\`): ${message}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ const predicates: QueryPredicate[][] = [
],
},

// (#is-nth-child? @statement 1 2)
// Error: too many orgs
{
operator: "is-nth-child?",
operands: [
{
type: "capture",
name: "statement",
},
{
type: "string",
value: "1",
},
{
type: "string",
value: "2",
},
],
},

// (#not-parent-type? statement foo)
// Error: capture names must be prefixed with @
{
Expand Down Expand Up @@ -95,19 +115,24 @@ const expectedErrors = [
{
patternIdx: 0,
predicateIdx: 2,
error: "Too many arguments",
},
{
patternIdx: 0,
predicateIdx: 3,
error:
"Error on argument 0 (`statement`): Capture names must be prefixed with @",
},
{
patternIdx: 0,
predicateIdx: 3,
predicateIdx: 4,
error: "Error on argument 1 (`hello`): Expected an integer",
},
{
patternIdx: 0,
predicateIdx: 5,
error:
"Error on argument 2 (`@foo`): Expected string, but received capture",
patternIdx: 0,
predicateIdx: 4,
},
];

Expand Down
27 changes: 9 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading