Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of Bindings with Escaped Numbers #533

Merged
merged 7 commits into from
Nov 7, 2024
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
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 @@
return;
}

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

while (next()) {
if (!isIdentifierChar(ch)) {
Expand All @@ -93,6 +93,8 @@
}

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

let bracketSegment = parseBracket();

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

Check warning on line 270 in core/player/src/binding-grammar/custom/index.ts

View check run for this annotation

Codecov / codecov/patch

core/player/src/binding-grammar/custom/index.ts#L270

Added line #L270 was not covered by tests
}

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