Skip to content

Commit

Permalink
Merge pull request #89 from player-ui/feat/better-dsl-expressions
Browse files Browse the repository at this point in the history
Better DSL Expression Compilation
  • Loading branch information
KetanReddy authored Apr 10, 2024
2 parents 4962959 + f3f383e commit 4c0c815
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
3 changes: 1 addition & 2 deletions cli/src/commands/dsl/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,8 @@ export default class DSLCompile extends BaseCommand {
results.exitCode = 100;
this.log("");
this.log(
chalk.red(`${logSymbols.error} Error compiling ${file}: ${e.message}`)
chalk.red(`${logSymbols.error} Error compiling ${file}: ${e.stack}`)
);
this.debug(e);
compilerResults.push({
state: "completed",
error: e,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ test("works with nested expressions", () => {

expect(exp2.toString()).toBe(`@[conditional(foo() == bar())]@`);
});

test("throws errors for syntactically wrong expressions", () => {
expect(() => {
const exp = e`something(1,2`;
}).toThrowErrorMatchingInlineSnapshot(`
[Error: Error: Expected ) at character 13 in expression:
something(1,2█]
`);
});
21 changes: 17 additions & 4 deletions language/dsl/src/string-templates/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import { parseExpression } from "@player-ui/player";
import { parseExpression, isErrorWithLocation } from "@player-ui/player";

export type TemplateInstanceRefStringContext = "binding" | "expression";
export interface TemplateRefStringOptions {
Expand Down Expand Up @@ -74,9 +74,22 @@ const createTemplateInstance = (

/** Try to parse the expression as valid */
if (options.nestedContext === "expression") {
const parsedExpression = parseExpression(value, { strict: false });
if (parsedExpression.error) {
throw parsedExpression.error;
try {
parseExpression(value);
} catch (e) {
if (e instanceof Error) {
let message: string;
if (isErrorWithLocation(e)) {
message = `${e} in expression: \r\n ${
value.slice(0, e.index + 1) + "\u2588" + value.slice(e.index + 1)
}`;
} else {
message = `${e} in expression ${value}`;
}
throw new Error(message);
}

throw new Error(`Unknown problem parsing expression ${e}`);
}
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"@oclif/core": "1.9.0",
"@oclif/plugin-legacy": "^1.2.7",
"@oclif/plugin-plugins": "^1.9.0",
"@player-ui/player": "0.7.1-next.0",
"@player-ui/types": "0.7.1-next.0",
"@player-ui/player": "0.7.2-next.4",
"@player-ui/types": "0.7.2-next.4",
"@reduxjs/toolkit": "^1.6.1",
"@rollup/plugin-image": "^2.1.1",
"@rollup/plugin-json": "^4.1.0",
Expand Down
24 changes: 12 additions & 12 deletions pnpm-lock.yaml

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

0 comments on commit 4c0c815

Please sign in to comment.