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

Enhancement/Parser Rewrite for plugins #80

Merged
merged 20 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from 19 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
2 changes: 1 addition & 1 deletion core/view/src/__tests__/view.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LocalModel, withParser } from '@player-ui/data';
import { DataModelWithParser, LocalModel, withParser } from '@player-ui/data';
mercillo marked this conversation as resolved.
Show resolved Hide resolved
import { ExpressionEvaluator } from '@player-ui/expressions';
import { BindingParser } from '@player-ui/binding';
import { SchemaController } from '@player-ui/schema';
Expand Down
177 changes: 0 additions & 177 deletions core/view/src/parser/__tests__/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,143 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`generates the correct AST parses a nested switch 1`] = `
Object {
"children": Array [
Object {
"path": Array [
"fields",
],
"value": Object {
"cases": Array [
Object {
"case": "{{bar}}",
"value": Object {
"children": Array [
Object {
"path": Array [
"asset",
],
"value": Object {
"parent": [Circular],
"type": "asset",
"value": Object {
"id": "input-1",
"type": "input",
},
},
},
],
"parent": [Circular],
"type": "value",
"value": undefined,
},
},
Object {
"case": "{{foo.bar}}",
"value": Object {
"children": Array [
Object {
"path": Array [
"asset",
],
"value": Object {
"parent": [Circular],
"type": "asset",
"value": Object {
"id": "input-2",
"type": "input",
},
},
},
],
"parent": [Circular],
"type": "value",
"value": undefined,
},
},
],
"dynamic": false,
"parent": [Circular],
"type": "switch",
},
},
],
"type": "value",
"value": Object {
"id": "foo",
},
}
`;

exports[`generates the correct AST parses a switch 1`] = `
Object {
"cases": Array [
Object {
"case": "{{foo.bar}}",
"value": Object {
"children": Array [
Object {
"path": Array [
"asset",
],
"value": Object {
"parent": [Circular],
"type": "asset",
"value": Object {
"type": "foo",
},
},
},
],
"parent": [Circular],
"type": "value",
"value": undefined,
},
},
],
"dynamic": false,
"type": "switch",
}
`;

exports[`generates the correct AST parses a template 1`] = `
Object {
"children": Array [
Object {
"path": Array [
"asset",
],
"value": Object {
"children": Array [
Object {
"path": Array [
"values",
],
"value": Object {
"data": "foo.bar",
"depth": 0,
"dynamic": false,
"parent": [Circular],
"template": Object {
"value": "{{foo.bar._index_}}",
},
"type": "template",
},
},
],
"parent": [Circular],
"type": "asset",
"value": Object {
"type": "collection",
},
},
},
],
"type": "value",
"value": undefined,
}
`;

exports[`generates the correct AST parses an exp array 1`] = `
Object {
"children": Array [
Expand Down Expand Up @@ -188,45 +50,6 @@ Object {
}
`;

exports[`generates the correct AST parses nested templates 1`] = `
Object {
"children": Array [
Object {
"path": Array [
"values",
],
"value": Object {
"data": "foo.pets",
"depth": 0,
"dynamic": false,
"parent": [Circular],
"template": Object {
"asset": Object {
"id": "outer-collection-_index_",
"template": Array [
Object {
"data": "foo.people",
"output": "values",
"value": Object {
"text": "{{foo.pets._index_}} + {{foo.people._index1_}}",
},
},
],
"type": "collection",
},
},
"type": "template",
},
},
],
"type": "value",
"value": Object {
"id": "foo",
"type": "collection",
},
}
`;

exports[`generates the correct AST works with applicability things 1`] = `
Object {
"expression": "{{baz}}",
Expand Down
148 changes: 56 additions & 92 deletions core/view/src/parser/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
import { BindingParser } from '@player-ui/binding';
import { LocalModel, withParser } from '@player-ui/data';
import { SchemaController } from '@player-ui/schema';
import { NodeType, Parser } from '../index';
import { SwitchPlugin, ApplicabilityPlugin, TemplatePlugin } from '../..';
import type { Options } from '../../plugins/options';
import { ExpressionEvaluator } from '../../../../expressions/src';
import type { DataModelWithParser } from '../../../../player/src';

const parseBinding = new BindingParser().parse;
describe('generates the correct AST', () => {
const parser = new Parser();
let model: DataModelWithParser;
let expressionEvaluator: ExpressionEvaluator;
let options: Options;
let parser: Parser;

beforeEach(() => {
model = withParser(new LocalModel(), parseBinding);
expressionEvaluator = new ExpressionEvaluator({
model,
});
parser = new Parser();
options = {
evaluate: expressionEvaluator.evaluate,
schema: new SchemaController(),
data: {
format: (binding, val) => val,
formatValue: (val) => val,
model,
},
};
new TemplatePlugin(options).applyParserHooks(parser);
new ApplicabilityPlugin().applyParser(parser);
new SwitchPlugin(options).applyParser(parser);
mercillo marked this conversation as resolved.
Show resolved Hide resolved
});

test('works with basic objects', () => {
expect(parser.parseObject({ foo: 'bar' })).toStrictEqual({
Expand Down Expand Up @@ -38,96 +69,6 @@ describe('generates the correct AST', () => {
expect(parser.parseObject({ asset: { type: 'bar' } })).toMatchSnapshot();
});

test('parses a template', () => {
expect(
parser.parseObject({
asset: {
type: 'collection',
template: [
{
data: 'foo.bar',
output: 'values',
value: {
value: '{{foo.bar._index_}}',
},
},
],
},
})
).toMatchSnapshot();
});

test('parses nested templates', () => {
expect(
parser.parseObject({
id: 'foo',
type: 'collection',
template: [
{
data: 'foo.pets',
output: 'values',
value: {
asset: {
type: 'collection',
id: 'outer-collection-_index_',
template: [
{
data: 'foo.people',
output: 'values',
value: {
text: '{{foo.pets._index_}} + {{foo.people._index1_}}',
},
},
],
},
},
},
],
})
).toMatchSnapshot();
});

test('parses a switch', () => {
expect(
parser.parseObject({
staticSwitch: [
{
case: '{{foo.bar}}',
asset: {
type: 'foo',
},
},
],
})
).toMatchSnapshot();
});

test('parses a nested switch', () => {
expect(
parser.parseObject({
id: 'foo',
fields: {
staticSwitch: [
{
case: '{{bar}}',
asset: {
id: 'input-1',
type: 'input',
},
},
{
case: '{{foo.bar}}',
asset: {
id: 'input-2',
type: 'input',
},
},
],
},
})
).toMatchSnapshot();
});

test('parses an exp array', () => {
expect(
parser.parseObject(
Expand All @@ -153,7 +94,30 @@ describe('generates the correct AST', () => {
});

describe('parseView', () => {
const parser = new Parser();
let model: DataModelWithParser;
let expressionEvaluator: ExpressionEvaluator;
let options: Options;
let parser: Parser;

beforeEach(() => {
model = withParser(new LocalModel(), parseBinding);
expressionEvaluator = new ExpressionEvaluator({
model,
});
parser = new Parser();
options = {
evaluate: expressionEvaluator.evaluate,
schema: new SchemaController(),
data: {
format: (binding, val) => val,
formatValue: (val) => val,
model,
},
};
new TemplatePlugin(options).applyParserHooks(parser);
new ApplicabilityPlugin().applyParser(parser);
new SwitchPlugin(options).applyParser(parser);
});

test('parses a simple view', () => {
expect(
Expand Down
Loading