Skip to content

Commit

Permalink
Merge pull request #328 from player-ui/export-expression-parser-types
Browse files Browse the repository at this point in the history
Expose More Information About Expression Parsing Errors
  • Loading branch information
KetanReddy authored Apr 3, 2024
2 parents d938cab + df973cd commit 1249770
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
17 changes: 7 additions & 10 deletions core/player/src/expressions/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
/**
* An expression to AST parser based on JSEP: http://jsep.from.so/
*/
import type { ExpressionNode, ExpressionNodeType, NodeLocation } from './types';
import type {
ErrorWithLocation,
ExpressionNode,
ExpressionNodeType,
NodeLocation,
} from './types';
import { ExpNodeOpaqueIdentifier } from './types';

const PERIOD_CODE = 46; // '.'
Expand Down Expand Up @@ -62,16 +67,8 @@ const binaryOps: Record<string, number> = {
'%': 14,
};

interface ErrorWithLocation extends Error {
/** The place in the string where the error occurs */
index: number;

/** a helpful description */
description: string;
}

/** Wrap the message and index in an error and throw it */
function throwError(message: string, index: number) {
function throwError(message: string, index: number): ErrorWithLocation {
const err = new Error(`${message} at character ${index}`);

(err as ErrorWithLocation).index = index;
Expand Down
8 changes: 8 additions & 0 deletions core/player/src/expressions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,11 @@ export type ExpressionNode =
| ObjectNode;

export type ExpressionNodeType = ExpressionNode['type'];

export interface ErrorWithLocation extends Error {
/** The place in the string where the error occurs */
index: number;

/** a helpful description */
description: string;
}
11 changes: 11 additions & 0 deletions core/player/src/expressions/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isExpressionNode } from './types';
import type {
ErrorWithLocation,
ExpressionHandler,
ExpressionNode,
ExpressionObjectType,
Expand Down Expand Up @@ -148,3 +149,13 @@ export function isObjectExpression(
'value' in expr
);
}

/**
* Type guard for ErrorWithLocation
*/
export function isErrorWithLocation(error: Error): error is ErrorWithLocation {
return (
(error as ErrorWithLocation).index !== undefined &&
(error as ErrorWithLocation).description !== undefined
);
}

0 comments on commit 1249770

Please sign in to comment.