Skip to content

Commit

Permalink
Merge pull request #533 from player-ui/bugfix/bindings-with-escaped-n…
Browse files Browse the repository at this point in the history
…umbers

Fix parsing of Bindings with Escaped Numbers
  • Loading branch information
KetanReddy authored Nov 7, 2024
2 parents 4ba64f4 + a236f03 commit dd14225
Show file tree
Hide file tree
Showing 7 changed files with 385 additions and 352 deletions.
12 changes: 11 additions & 1 deletion core/player/src/binding-grammar/custom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const parse: Parser = (path) => {
return;
}

let value = ch;
let value: string | number = ch;

while (next()) {
if (!isIdentifierChar(ch)) {
Expand All @@ -93,6 +93,8 @@ export const parse: Parser = (path) => {
}

if (value) {
const maybeNumber = Number(value);
value = isNaN(maybeNumber) ? value : maybeNumber;
return toValue(value);
}
};
Expand Down Expand Up @@ -260,6 +262,14 @@ export const parse: Parser = (path) => {

let bracketSegment = parseBracket();

if (bracketSegment?.name === "Value") {
const maybeNumber = Number(bracketSegment.value);
bracketSegment.value =
isNaN(maybeNumber) || String(maybeNumber) !== bracketSegment.value
? bracketSegment.value
: maybeNumber;
}

while (bracketSegment !== undefined) {
parsed.push(bracketSegment);
bracketSegment = parseBracket();
Expand Down
22 changes: 22 additions & 0 deletions core/player/src/binding/__tests__/parser.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { bench, describe } from "vitest";
import get from "dlv";
import { getBindingSegments } from "..";
import { testCases, testModel } from "./resolver.test";
import { parseCustom, ParserSuccessResult } from "../../binding-grammar";
import { resolveBindingAST } from "../resolver";

describe("parser benchmarks", () => {
testCases.map(
([input, expectedOutput]) => {
bench(`Resolving binding: ${input}`, () => {
const parsedBinding = parseCustom(input);
resolveBindingAST((parsedBinding as ParserSuccessResult).path, {
getValue: (path) => get(testModel, getBindingSegments(path) as any),
convertToPath: (p) => p,
evaluate: () => undefined,
});
});
},
{ iterations: 10000 },
);
});
9 changes: 6 additions & 3 deletions core/player/src/binding/__tests__/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { parseCustom } from "../../binding-grammar";
import { resolveBindingAST } from "../resolver";
import { getBindingSegments } from "../utils";

const testModel = {
export const testModel = {
foo: {
pets: [
{
Expand All @@ -23,11 +23,14 @@ const testModel = {
"other",
],
},
};
} as const;

const testCases: Array<[string, string]> = [
export const testCases: Array<[string, string]> = [
["foo.bar", "foo.bar"],
["foo.pets.1.name", "foo.pets.1.name"],
["foo.pets.01.name", "foo.pets.1.name"],
["foo.pets['01'].name", "foo.pets.01.name"],
["foo.pets[01].name", "foo.pets.1.name"],
['foo.pets[name = "frodo"].type', "foo.pets.2.type"],
['foo.pets["name" = "sprinkles"].type', "foo.pets.4.type"],
];
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
"@mdx-js/react": "^1.6.22",
"@mdx-js/rollup": "^3.0.1",
"@monaco-editor/react": "^4.6.0",
"@player-tools/cli": "0.6.1-next.2",
"@player-tools/dsl": "0.6.1-next.2",
"@player-tools/xlr": "0.6.1-next.2",
"@player-tools/cli": "0.8.1",
"@player-tools/dsl": "0.8.1",
"@player-tools/xlr": "0.8.1",
"@player-tools/xlr-utils": "0.6.1-next.2",
"@radix-ui/react-label": "^2.0.2",
"@radix-ui/react-separator": "^1.0.3",
Expand Down Expand Up @@ -85,7 +85,7 @@
"@typescript-eslint/eslint-plugin": "^5.1.0",
"@typescript-eslint/parser": "^5.1.0",
"@vitejs/plugin-react": "^4.2.1",
"@vitest/coverage-v8": "^1.0.2",
"@vitest/coverage-v8": "^2.1.4",
"all-contributors-cli": "^6.20.0",
"arr-flatten": "^1.1.0",
"astro": "^4.16.7",
Expand Down Expand Up @@ -195,7 +195,7 @@
"unist-util-visit": "^5.0.0",
"uuid": "^8.3.2",
"vite": "^4.0.0",
"vitest": "^1.0.2",
"vitest": "^2.1.3",
"webpack": "^5.91.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
Expand Down
4 changes: 4 additions & 0 deletions plugins/reference-assets/components/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ js_pipeline(
":node_modules/@player-ui/common-types-plugin",
":node_modules/@player-ui/reference-assets-plugin",
],
test_deps = [
"//:vitest_config",
"//:node_modules/dlv"
],
)
Loading

0 comments on commit dd14225

Please sign in to comment.