New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@player-ui/player

Package Overview
Dependencies
Maintainers
0
Versions
343
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@player-ui/player - npm Package Compare versions

Comparing version 0.7.5--canary.452.15858 to 0.7.5-next.0

src/view/plugins/__tests__/__snapshots__/asset.test.ts.snap

8

package.json
{
"name": "@player-ui/player",
"version": "0.7.5--canary.452.15858",
"version": "0.7.5-next.0",
"main": "dist/cjs/index.cjs",
"dependencies": {
"@player-ui/partial-match-registry": "0.7.5--canary.452.15858",
"@player-ui/make-flow": "0.7.5--canary.452.15858",
"@player-ui/types": "0.7.5--canary.452.15858",
"@player-ui/partial-match-registry": "0.7.5-next.0",
"@player-ui/make-flow": "0.7.5-next.0",
"@player-ui/types": "0.7.5-next.0",
"@types/dlv": "^1.1.4",

@@ -10,0 +10,0 @@ "dequal": "^2.0.2",

import type { Player, PlayerPlugin } from "../player";
import {
ApplicabilityPlugin,
AssetPlugin,
MultiNodePlugin,
StringResolverPlugin,

@@ -20,2 +22,3 @@ SwitchPlugin,

const pluginOptions = toNodeResolveOptions(view.resolverOptions);
new AssetPlugin().apply(view);
new SwitchPlugin(pluginOptions).apply(view);

@@ -27,2 +30,3 @@ new ApplicabilityPlugin().apply(view);

view.hooks.onTemplatePluginCreated.call(templatePlugin);
new MultiNodePlugin().apply(view);
});

@@ -29,0 +33,0 @@ });

@@ -6,3 +6,8 @@ import { test, expect, vitest } from "vitest";

import { SchemaController } from "../../schema";
import { ApplicabilityPlugin, StringResolverPlugin, ViewInstance } from "..";
import {
ApplicabilityPlugin,
AssetPlugin,
StringResolverPlugin,
ViewInstance,
} from "..";
import { NodeType } from "../parser";

@@ -38,2 +43,3 @@

new StringResolverPlugin().apply(view);
new AssetPlugin().apply(view);

@@ -210,2 +216,3 @@ view.hooks.resolver.tap("input", (resolver) => {

new StringResolverPlugin().apply(view);
new AssetPlugin().apply(view);

@@ -212,0 +219,0 @@ const resolved = view.update();

@@ -7,2 +7,3 @@ import { describe, it, expect, test } from "vitest";

import {
MultiNodePlugin,
StringResolverPlugin,

@@ -84,3 +85,3 @@ SwitchPlugin,

test("works with no valid switch cases in an array", () => {
test("works with no valid static switch cases in an array", () => {
const model = withParser(new LocalModel({}), parseBinding);

@@ -127,2 +128,3 @@ const evaluator = new ExpressionEvaluator({ model });

new SwitchPlugin(pluginOptions).apply(view);
new MultiNodePlugin().apply(view);
new StringResolverPlugin().apply(view);

@@ -138,2 +140,56 @@

test("works with no valid dynamic switch cases in an array", () => {
const model = withParser(new LocalModel({}), parseBinding);
const evaluator = new ExpressionEvaluator({ model });
const schema = new SchemaController();
const view = new ViewInstance(
{
id: "test",
type: "view",
title: [
{
dynamicSwitch: [
{
case: false,
asset: {
id: "false-case",
type: "text",
value: "some text",
},
},
{
case: false,
asset: {
id: "false-case-2",
type: "text",
value: "some text",
},
},
],
},
],
},
{
model,
parseBinding,
evaluator,
schema,
},
);
const pluginOptions = toNodeResolveOptions(view.resolverOptions);
new SwitchPlugin(pluginOptions).apply(view);
new MultiNodePlugin().apply(view);
new StringResolverPlugin().apply(view);
const resolved = view.update();
expect(resolved).toStrictEqual({
id: "test",
title: [],
type: "view",
});
});
it("does not return a field object if the case does not resolve an asset", () => {

@@ -388,2 +444,3 @@ const model = withParser(

new SwitchPlugin(pluginOptions).apply(view);
new MultiNodePlugin().apply(view);
new StringResolverPlugin().apply(view);

@@ -743,2 +800,3 @@

new SwitchPlugin(pluginOptions).apply(view);
new MultiNodePlugin().apply(view);
new StringResolverPlugin().apply(view);

@@ -745,0 +803,0 @@

@@ -6,3 +6,9 @@ import { describe, expect, beforeEach, test } from "vitest";

import { NodeType, Parser } from "../index";
import { SwitchPlugin, ApplicabilityPlugin, TemplatePlugin } from "../..";
import {
SwitchPlugin,
ApplicabilityPlugin,
TemplatePlugin,
MultiNodePlugin,
AssetPlugin,
} from "../..";
import type { Options } from "../../plugins/options";

@@ -34,5 +40,7 @@ import { ExpressionEvaluator } from "../../../expressions";

};
new AssetPlugin().applyParser(parser);
new TemplatePlugin(options).applyParser(parser);
new ApplicabilityPlugin().applyParser(parser);
new SwitchPlugin(options).applyParser(parser);
new MultiNodePlugin().applyParser(parser);
});

@@ -159,4 +167,6 @@

};
new AssetPlugin().applyParser(parser);
new TemplatePlugin(options).applyParser(parser);
new ApplicabilityPlugin().applyParser(parser);
new MultiNodePlugin().applyParser(parser);
new SwitchPlugin(options).applyParser(parser);

@@ -255,4 +265,9 @@ });

} as any);
const multiNodePlugin = new MultiNodePlugin();
const assetPlugin = new AssetPlugin();
const parser = new Parser();
switchPlugin.applyParser(parser);
multiNodePlugin.applyParser(parser);
assetPlugin.applyParser(parser);

@@ -259,0 +274,0 @@ test("works with asset wrapped objects", () => {

@@ -1,7 +0,5 @@

import { omit, setIn } from "timm";
import { setIn } from "timm";
import { SyncBailHook, SyncWaterfallHook } from "tapable-ts";
import type { Template } from "@player-ui/types";
import type { AnyAssetType, Node } from "./types";
import { NodeType } from "./types";
import { getNodeID, hasAsync } from "./utils";

@@ -20,2 +18,8 @@ export * from "./types";

export interface ParseObjectChildOptions {
key: string;
path: Node.PathSegment[];
parentObj: object;
}
interface NestedObj {

@@ -56,4 +60,2 @@ /** The values of a nested local object */

determineNodeType: new SyncBailHook<[object | string], NodeType>(),
parseNode: new SyncBailHook<

@@ -64,5 +66,5 @@ [

parseOptions: ParseObjectOptions,
determinedNodeType: NodeType | null,
childOptions?: ParseObjectChildOptions,
],
Node.Node
Node.Node | Node.Child[]
>(),

@@ -81,23 +83,2 @@ };

private parseAsync(
obj: object,
type: Node.ChildrenTypes,
options: ParseObjectOptions,
): Node.Node | null {
const parsedAsync = this.parseObject(omit(obj, "async"), type, options);
const parsedNodeId = getNodeID(parsedAsync);
if (parsedAsync !== null && parsedNodeId) {
return this.createASTNode(
{
id: parsedNodeId,
type: NodeType.Async,
value: parsedAsync,
},
obj,
);
}
return null;
}
public createASTNode(node: Node.Node | null, value: any): Node.Node | null {

@@ -113,21 +94,2 @@ const tapped = this.hooks.onCreateASTNode.call(node, value);

/**
* Checks if there are templated values in the object
*
* @param obj - The Parsed Object to check to see if we have a template array type for
* @param localKey - The key being checked
*/
private hasTemplateValues(obj: any, localKey: string) {
return (
Object.hasOwnProperty.call(obj, "template") &&
Array.isArray(obj?.template) &&
obj.template.length &&
obj.template.find((tmpl: any) => tmpl.output === localKey)
);
}
private hasSwitchKey(localKey: string) {
return localKey === ("staticSwitch" || "dynamicSwitch");
}
public parseObject(

@@ -138,19 +100,12 @@ obj: object,

): Node.Node | null {
const nodeType = this.hooks.determineNodeType.call(obj);
const parsedNode = this.hooks.parseNode.call(
obj,
type,
options,
) as Node.Node | null;
if (nodeType !== undefined) {
const parsedNode = this.hooks.parseNode.call(
obj,
type,
options,
nodeType,
);
if (parsedNode) {
return parsedNode;
}
if (parsedNode || parsedNode === null) {
return parsedNode;
}
/**
*
*/
const parseLocalObject = (

@@ -187,205 +142,35 @@ currentValue: any,

const newValue = objEntries.reduce((accumulation, current): NestedObj => {
const { children, ...rest } = accumulation;
let { value } = accumulation;
const { children } = accumulation;
const [localKey, localValue] = current;
if (localKey === "asset" && typeof localValue === "object") {
const assetAST = this.parseObject(
localValue,
NodeType.Asset,
options,
);
if (assetAST) {
return {
...rest,
children: [
...children,
{
path: [...path, "asset"],
value: assetAST,
},
],
};
}
} else if (
this.hooks.determineNodeType.call(localKey) === NodeType.Template &&
Array.isArray(localValue)
) {
const templateChildren = localValue
.map((template: Template) => {
const templateAST = this.hooks.onCreateASTNode.call(
{
type: NodeType.Template,
depth: options.templateDepth ?? 0,
data: template.data,
template: template.value,
dynamic: template.dynamic ?? false,
},
template,
);
const newChildren = this.hooks.parseNode.call(
localValue,
NodeType.Value,
options,
{
path,
key: localKey,
parentObj: localObj,
},
) as Node.Child[];
if (templateAST?.type === NodeType.MultiNode) {
templateAST.values.forEach((v) => {
// eslint-disable-next-line no-param-reassign
v.parent = templateAST;
});
}
if (templateAST) {
return {
path: [...path, template.output],
value: templateAST,
};
}
// eslint-disable-next-line no-useless-return
return;
})
.filter((element) => !!element);
return {
...rest,
children: [...children, ...templateChildren],
} as NestedObj;
} else if (
(localValue &&
this.hooks.determineNodeType.call(localValue) ===
NodeType.Switch) ||
this.hasSwitchKey(localKey)
) {
const localSwitch = this.hooks.parseNode.call(
this.hasSwitchKey(localKey)
? { [localKey]: localValue }
: localValue,
NodeType.Value,
options,
NodeType.Switch,
);
if (
localSwitch &&
localSwitch.type === NodeType.Value &&
localSwitch.children?.length === 1 &&
localSwitch.value === undefined
) {
const firstChild = localSwitch.children[0];
return {
...rest,
children: [
...children,
{
path: [...path, localKey, ...firstChild.path],
value: firstChild.value,
},
],
};
}
if (localSwitch) {
return {
...rest,
children: [
...children,
{
path: [...path, localKey],
value: localSwitch,
},
],
};
}
} else if (localValue && hasAsync(localValue)) {
const localAsync = this.parseAsync(
localValue,
NodeType.Value,
options,
);
if (localAsync) {
children.push({
path: [...path, localKey],
value: localAsync,
});
}
} else if (localValue && Array.isArray(localValue)) {
const childValues = localValue
.map((childVal) =>
this.parseObject(childVal, NodeType.Value, options),
)
.filter((child): child is Node.Node => !!child);
if (childValues.length > 0) {
const multiNode = this.hooks.onCreateASTNode.call(
{
type: NodeType.MultiNode,
override: !this.hasTemplateValues(localObj, localKey),
values: childValues,
},
localValue,
);
if (multiNode?.type === NodeType.MultiNode) {
multiNode.values.forEach((v) => {
// eslint-disable-next-line no-param-reassign
v.parent = multiNode;
});
}
if (multiNode) {
return {
...rest,
children: [
...children,
{
path: [...path, localKey],
value: multiNode,
},
],
};
}
}
if (newChildren) {
children.push(...newChildren);
} else if (localValue && typeof localValue === "object") {
const determineNodeType =
this.hooks.determineNodeType.call(localValue);
const result = parseLocalObject(accumulation.value, localValue, [
...path,
localKey,
]);
if (determineNodeType === NodeType.Applicability) {
const parsedNode = this.hooks.parseNode.call(
localValue,
NodeType.Value,
options,
determineNodeType,
);
if (parsedNode) {
return {
...rest,
children: [
...children,
{
path: [...path, localKey],
value: parsedNode,
},
],
};
}
} else {
const result = parseLocalObject(accumulation.value, localValue, [
...path,
localKey,
]);
return {
value: result.value,
children: [...children, ...result.children],
};
}
value = result.value;
children.push(...result.children);
} else {
const value = setIn(
accumulation.value,
[...path, localKey],
localValue,
);
return {
children,
value,
};
value = setIn(accumulation.value, [...path, localKey], localValue);
}
return accumulation;
return {
value,
children,
};
}, defaultValue);

@@ -399,3 +184,3 @@

const baseAst =
value === undefined && children.length === 0
value === undefined && !children.length
? undefined

@@ -407,7 +192,6 @@ : {

if (baseAst !== undefined && children.length > 0) {
const parent = baseAst as Node.BaseWithChildren<any>;
if (baseAst && children.length) {
const parent: Node.BaseWithChildren<any> = baseAst;
parent.children = children;
children.forEach((child) => {
// eslint-disable-next-line no-param-reassign
child.value.parent = parent;

@@ -414,0 +198,0 @@ });

import type { Node } from "./types";
/** Check to see if the object contains async */
export function hasAsync(obj: object): boolean {
return Object.prototype.hasOwnProperty.call(obj, "async");
/**
* Checks if there are templated values in the object
*
* @param obj - The Parsed Object to check to see if we have a template array type for
* @param localKey - The key being checked
*/
export function hasTemplateValues(obj: any, localKey: string) {
return (
Object.hasOwnProperty.call(obj, "template") &&
Array.isArray(obj?.template) &&
obj.template.length &&
obj.template.find((tmpl: any) => tmpl.output === localKey)
);
}
/** Check to see if the string is a valid switch key */
export function hasSwitchKey(localKey: string) {
return localKey === "staticSwitch" || localKey === "dynamicSwitch";
}
/** Check to see if the string is a valid template key */
export function hasTemplateKey(localKey: string) {
return localKey === "template";
}
/** Get the ID of the Node if there is one */

@@ -9,0 +29,0 @@ export function getNodeID(node?: Node.Node | null): string | undefined {

@@ -11,3 +11,3 @@ import { describe, it, expect, beforeEach } from "vitest";

import { Parser } from "../../parser";
import { ApplicabilityPlugin, StringResolverPlugin } from "..";
import { ApplicabilityPlugin, MultiNodePlugin, StringResolverPlugin } from "..";

@@ -41,4 +41,6 @@ const parseBinding = new BindingParser().parse;

const sP = new StringResolverPlugin();
const mnP = new MultiNodePlugin();
aP.applyParser(parser);
mnP.applyParser(parser);

@@ -75,2 +77,3 @@ const root = parser.parseObject({

new ApplicabilityPlugin().applyParser(parser);
new MultiNodePlugin().applyParser(parser);
const root = parser.parseObject({

@@ -251,19 +254,24 @@ asset: {

it("determines if nodeType is applicability", () => {
new ApplicabilityPlugin().applyParser(parser);
const nodeTest = {
applicability: "{{bar}} == true",
};
const nodeType = parser.hooks.determineNodeType.call(nodeTest);
expect(nodeType).toStrictEqual("applicability");
});
it("does not return field object if applicability node does not resolve", () => {
const applicabilityPlugin = new ApplicabilityPlugin();
const stringResolverPlugin = new StringResolverPlugin();
it("Does not return a nodeType", () => {
new ApplicabilityPlugin().applyParser(parser);
const nodeTest = {
value: "foo",
};
const nodeType = parser.hooks.determineNodeType.call(nodeTest);
expect(nodeType).toBe(undefined);
applicabilityPlugin.applyParser(parser);
const root = parser.parseObject({
id: "foo",
fields: {
applicability: "{{foo.bar}}",
},
} as any);
const resolver = new Resolver(root as Node.Node, resolverOptions);
applicabilityPlugin.applyResolver(resolver);
stringResolverPlugin.applyResolver(resolver);
const resolved = resolver.update();
expect(resolved).toStrictEqual({
id: "foo",
});
});
});

@@ -7,134 +7,26 @@ import { describe, it, expect, beforeEach } from "vitest";

import { SchemaController } from "../../../schema";
import { NodeType } from "../../parser";
import { Parser } from "../../parser";
import { ViewInstance } from "../../view";
import type { Options } from "../options";
import TemplatePlugin from "../template-plugin";
import { TemplatePlugin, MultiNodePlugin, AssetPlugin } from "../";
import { StringResolverPlugin, toNodeResolveOptions } from "../..";
const templateJoinValues = {
id: "snippet-of-json",
topic: "Snippet",
schema: {},
data: {
forms: {
"1099-A": [
{
description: "Desciption of concept 1099 1",
amount: "Help",
},
],
"1099-B": [
{
description: "Desciption of concept 1099 2",
amount: "Help",
},
],
},
},
id: "generated-flow",
views: [
{
id: "overviewGroup",
type: "overviewGroup",
metaData: {
role: "stateful",
},
modifiers: [
{
type: "tag",
value: "fancy-header",
},
],
headers: {
label: {
asset: {
id: "line-of-work-summary-gh-header-label",
type: "text",
value: "Header",
},
},
values: [
{
asset: {
id: "line-of-work-summary-gh-expenses-simple-header-previous-year",
type: "text",
value: "Type",
},
},
{
asset: {
id: "line-of-work-summary-gh-expenses-simple-header-cy",
type: "text",
value: "2022",
},
},
],
},
id: "collection",
type: "collection",
template: [
{
data: "forms.1099-A",
data: "foo",
output: "values",
value: {
asset: {
id: "overviewItem3",
type: "overviewItem",
label: {
asset: {
id: "overviewItem3-label",
type: "text",
value: "1099-A",
},
},
values: [
{
asset: {
id: "overviewItem3-year",
type: "text",
value: "Desciption of concept 1099 1",
},
},
{
asset: {
id: "loverviewItem3-cy",
type: "text",
value: "4000",
},
},
],
id: "value-_index_",
type: "text",
value: "item {{foo._index_}}",
},
},
},
{
data: "forms.1099-B",
output: "values",
value: {
asset: {
id: "overviewItem4",
type: "overviewItem",
label: {
asset: {
id: "overviewItem4-label",
type: "text",
value: "1099-B",
},
},
values: [
{
asset: {
id: "overviewItem4-year",
type: "text",
value: "Desciption of concept 1099 2",
},
},
{
asset: {
id: "loverviewItem3-cy",
type: "text",
value: "6000",
},
},
],
},
},
},
],

@@ -144,27 +36,5 @@ values: [

asset: {
id: "overviewItem1",
type: "overviewItem",
label: {
asset: {
id: "overviewItem1-label",
type: "text",
value: "First Summary",
},
},
values: [
{
asset: {
id: "overviewItem1-year",
type: "text",
value: "Desciption of year summary 1",
},
},
{
asset: {
id: "loverviewItem1-cy",
type: "text",
value: "14000",
},
},
],
id: "value-2",
type: "text",
value: "First value in the collection",
},

@@ -174,27 +44,5 @@ },

asset: {
id: "overviewItem2",
type: "overviewItem",
label: {
asset: {
id: "overviewItem2-label",
type: "text",
value: "Second year Summary",
},
},
values: [
{
asset: {
id: "overviewItem2-year",
type: "text",
value: "Desciption of year summary item 2",
},
},
{
asset: {
id: "loverviewItem1-cy",
type: "text",
value: "19000",
},
},
],
id: "value-3",
type: "text",
value: "Second value in the collection",
},

@@ -205,176 +53,53 @@ },

{
id: "overviewGroup",
type: "overviewGroup",
metaData: {
role: "stateful",
},
modifiers: [
id: "collection",
type: "collection",
values: [
{
type: "tag",
value: "fancy-header",
},
],
headers: {
label: {
asset: {
id: "line-of-work-summary-gh-header-label",
id: "value-2",
type: "text",
value: "Header",
value: "First value in the collection",
},
},
values: [
{
asset: {
id: "line-of-work-summary-gh-expenses-simple-header-previous-year",
type: "text",
value: "Type",
},
},
{
asset: {
id: "line-of-work-summary-gh-expenses-simple-header-cy",
type: "text",
value: "2022",
},
},
],
},
values: [
{
asset: {
id: "overviewItem1",
type: "overviewItem",
label: {
asset: {
id: "overviewItem1-label",
type: "text",
value: "First Summary",
},
},
values: [
{
asset: {
id: "overviewItem1-year",
type: "text",
value: "Desciption of year summary 1",
},
},
{
asset: {
id: "loverviewItem1-cy",
type: "text",
value: "14000",
},
},
],
id: "value-3",
type: "text",
value: "Second value in the collection",
},
},
{
asset: {
id: "overviewItem2",
type: "overviewItem",
label: {
asset: {
id: "overviewItem2-label",
type: "text",
value: "Second year Summary",
},
},
values: [
{
asset: {
id: "overviewItem2-year",
type: "text",
value: "Desciption of year summary item 2",
},
},
{
asset: {
id: "loverviewItem1-cy",
type: "text",
value: "19000",
},
},
],
},
},
],
template: [
{
data: "forms.1099-A",
data: "foo",
output: "values",
value: {
asset: {
id: "overviewItem3",
type: "overviewItem",
label: {
asset: {
id: "overviewItem3-label",
type: "text",
value: "1099-A",
},
},
values: [
{
asset: {
id: "overviewItem3-year",
type: "text",
value: "Desciption of concept 1099 1",
},
},
{
asset: {
id: "loverviewItem3-cy",
type: "text",
value: "4000",
},
},
],
id: "value-_index_",
type: "text",
value: "item {{foo._index_}}",
},
},
},
{
data: "forms.1099-B",
output: "values",
value: {
asset: {
id: "overviewItem4",
type: "overviewItem",
label: {
asset: {
id: "overviewItem4-label",
type: "text",
value: "1099-B",
},
},
values: [
{
asset: {
id: "overviewItem4-year",
type: "text",
value: "Desciption of concept 1099 2",
},
},
{
asset: {
id: "loverviewItem3-cy",
type: "text",
value: "6000",
},
},
],
},
},
},
],
},
],
data: {
foo: [1, 2],
},
navigation: {
BEGIN: "SnippetFlow",
SnippetFlow: {
startState: "VIEW_Snippet-View1",
"VIEW_Snippet-View1": {
ref: "overviewGroup",
BEGIN: "FLOW_1",
FLOW_1: {
startState: "VIEW_1",
VIEW_1: {
state_type: "VIEW",
ref: "collection",
transitions: {
"*": "END_Done",
},
},
END_Done: {
state_type: "END",
outcome: "done",
},
},

@@ -408,2 +133,3 @@ },

new TemplatePlugin(options).applyParser(parser);
new AssetPlugin().applyParser(parser);
});

@@ -467,72 +193,2 @@

});
it("determines if nodeType is template", () => {
const nodeTest = "template";
const nodeType = parser.hooks.determineNodeType.call(nodeTest);
expect(nodeType).toStrictEqual("template");
});
it("Does not return a nodeType", () => {
const nodeTest = {
value: "foo",
};
const nodeType = parser.hooks.determineNodeType.call(nodeTest);
expect(nodeType).toBe(undefined);
});
it("returns templateNode if template exists", () => {
const obj = {
dynamic: true,
data: "foo.bar",
output: "values",
value: {
value: "{{foo.bar._index_}}",
},
};
const nodeOptions = {
templateDepth: 1,
};
const parsedNode = parser.hooks.parseNode.call(
obj,
NodeType.Value,
nodeOptions,
NodeType.Template,
);
expect(parsedNode).toStrictEqual({
data: "foo.bar",
depth: 1,
dynamic: true,
template: {
value: "{{foo.bar._index_}}",
},
type: "template",
});
});
it("returns templateNode if template exists, and templateDepth is not set", () => {
const obj = {
data: "foo.bar2",
output: "values",
dynamic: true,
value: {
value: "{{foo.bar2._index_}}",
},
};
const nodeOptions = {};
const parsedNode = parser.hooks.parseNode.call(
obj,
NodeType.Value,
nodeOptions,
NodeType.Template,
);
expect(parsedNode).toStrictEqual({
data: "foo.bar2",
depth: 0,
dynamic: true,
template: {
value: "{{foo.bar2._index_}}",
},
type: "template",
});
});
});

@@ -704,4 +360,6 @@

const pluginOptions = toNodeResolveOptions(view.resolverOptions);
new AssetPlugin().apply(view);
new TemplatePlugin(pluginOptions).apply(view);
new StringResolverPlugin().apply(view);
new MultiNodePlugin().apply(view);

@@ -711,3 +369,3 @@ const resolved = view.update();

expect(resolved.values).toHaveLength(4);
expect(resolved.values[0]).toMatchSnapshot();
expect(resolved.values).toMatchSnapshot();
});

@@ -723,4 +381,6 @@ it("Should show template item last when coming after values on lexical order", () => {

const pluginOptions = toNodeResolveOptions(view.resolverOptions);
new AssetPlugin().apply(view);
new TemplatePlugin(pluginOptions).apply(view);
new StringResolverPlugin().apply(view);
new MultiNodePlugin().apply(view);

@@ -730,5 +390,5 @@ const resolved = view.update();

expect(resolved.values).toHaveLength(4);
expect(resolved.values[0]).toMatchSnapshot();
expect(resolved.values).toMatchSnapshot();
});
});
});
import { omit } from "timm";
import type { Options } from "./options";
import type { Resolver } from "../resolver";
import type { Node, ParseObjectOptions, Parser } from "../parser";
import type {
Node,
ParseObjectOptions,
ParseObjectChildOptions,
Parser,
} from "../parser";
import { NodeType } from "../parser";

@@ -10,2 +15,6 @@ import { ViewInstance, ViewPlugin } from "../view";

export default class ApplicabilityPlugin implements ViewPlugin {
private isApplicability(obj: any) {
return obj && Object.prototype.hasOwnProperty.call(obj, "applicability");
}
applyResolver(resolver: Resolver) {

@@ -33,9 +42,2 @@ resolver.hooks.beforeResolve.tap(

applyParser(parser: Parser) {
/** Switches resolved during the parsing phase are static */
parser.hooks.determineNodeType.tap("applicability", (obj: any) => {
if (Object.prototype.hasOwnProperty.call(obj, "applicability")) {
return NodeType.Applicability;
}
});
parser.hooks.parseNode.tap(

@@ -47,5 +49,5 @@ "applicability",

options: ParseObjectOptions,
determinedNodeType: null | NodeType,
childOptions?: ParseObjectChildOptions,
) => {
if (determinedNodeType === NodeType.Applicability) {
if (this.isApplicability(obj)) {
const parsedApplicability = parser.parseObject(

@@ -56,18 +58,32 @@ omit(obj, "applicability"),

);
if (parsedApplicability !== null) {
const applicabilityNode = parser.createASTNode(
{
type: NodeType.Applicability,
expression: (obj as any).applicability,
value: parsedApplicability,
},
obj,
);
if (applicabilityNode?.type === NodeType.Applicability) {
applicabilityNode.value.parent = applicabilityNode;
}
if (!parsedApplicability) {
return childOptions ? [] : null;
}
return applicabilityNode;
const applicabilityNode = parser.createASTNode(
{
type: NodeType.Applicability,
expression: (obj as any).applicability,
value: parsedApplicability,
},
obj,
);
if (!applicabilityNode) {
return childOptions ? [] : null;
}
if (applicabilityNode.type === NodeType.Applicability) {
applicabilityNode.value.parent = applicabilityNode;
}
return childOptions
? [
{
path: [...childOptions.path, childOptions.key],
value: applicabilityNode,
},
]
: applicabilityNode;
}

@@ -74,0 +90,0 @@ },

@@ -1,4 +0,6 @@

export { default as TemplatePlugin } from "./template-plugin";
export { default as TemplatePlugin } from "./template";
export { default as StringResolverPlugin } from "./string-resolver";
export { default as ApplicabilityPlugin } from "./applicability";
export { default as SwitchPlugin } from "./switch";
export { default as MultiNodePlugin } from "./multi-node";
export { default as AssetPlugin } from "./asset";

@@ -0,6 +1,12 @@

import { ViewInstance, ViewPlugin } from "../view";
import type { Options } from "./options";
import type { Parser, Node, ParseObjectOptions } from "../parser";
import type {
Parser,
Node,
ParseObjectOptions,
ParseObjectChildOptions,
} from "../parser";
import { EMPTY_NODE, NodeType } from "../parser";
import type { Resolver } from "../resolver";
import { ViewInstance, ViewPlugin } from "../view";
import { hasSwitchKey } from "../parser/utils";

@@ -26,2 +32,10 @@ /** A view plugin to resolve switches */

private isSwitch(obj: any) {
return (
obj &&
(Object.prototype.hasOwnProperty.call(obj, "dynamicSwitch") ||
Object.prototype.hasOwnProperty.call(obj, "staticSwitch"))
);
}
applyParser(parser: Parser) {

@@ -37,11 +51,2 @@ /** Switches resolved during the parsing phase are static */

parser.hooks.determineNodeType.tap("switch", (obj) => {
if (
Object.prototype.hasOwnProperty.call(obj, "dynamicSwitch") ||
Object.prototype.hasOwnProperty.call(obj, "staticSwitch")
) {
return NodeType.Switch;
}
});
parser.hooks.parseNode.tap(

@@ -53,36 +58,46 @@ "switch",

options: ParseObjectOptions,
determinedNodeType: null | NodeType,
childOptions?: ParseObjectChildOptions,
) => {
if (determinedNodeType === NodeType.Switch) {
const dynamic = "dynamicSwitch" in obj;
const switchContent =
"dynamicSwitch" in obj ? obj.dynamicSwitch : obj.staticSwitch;
if (
this.isSwitch(obj) ||
(childOptions && hasSwitchKey(childOptions.key))
) {
const objToParse =
childOptions && hasSwitchKey(childOptions.key)
? { [childOptions.key]: obj }
: obj;
const dynamic = "dynamicSwitch" in objToParse;
const switchContent = dynamic
? objToParse.dynamicSwitch
: objToParse.staticSwitch;
const cases: Node.SwitchCase[] = [];
const cases: Node.SwitchCase[] = switchContent
.map(
(switchCase: {
[x: string]: any;
/**
*
*/
case: any;
}) => {
const { case: switchCaseExpr, ...switchBody } = switchCase;
const value = parser.parseObject(
switchBody,
NodeType.Value,
options,
);
switchContent.forEach(
(switchCase: {
[x: string]: any;
/**
*
*/
case: any;
}) => {
const { case: switchCaseExpr, ...switchBody } = switchCase;
const value = parser.parseObject(
switchBody,
NodeType.Value,
options,
);
if (value) {
return {
case: switchCaseExpr,
value: value as Node.Value,
};
}
if (value) {
cases.push({
case: switchCaseExpr,
value: value as Node.Value,
});
}
},
);
return;
},
)
.filter(Boolean);
const switchAST = parser.hooks.onCreateASTNode.call(
const switchAST = parser.createASTNode(
{

@@ -93,8 +108,11 @@ type: NodeType.Switch,

},
obj,
objToParse,
);
if (switchAST?.type === NodeType.Switch) {
if (!switchAST || switchAST.type === NodeType.Empty) {
return childOptions ? [] : null;
}
if (switchAST.type === NodeType.Switch) {
switchAST.cases.forEach((sCase) => {
// eslint-disable-next-line no-param-reassign
sCase.value.parent = switchAST;

@@ -104,7 +122,20 @@ });

if (switchAST?.type === NodeType.Empty) {
return null;
if (childOptions) {
let path = [...childOptions.path, childOptions.key];
let value: any = switchAST;
if (
switchAST.type === NodeType.Value &&
switchAST.children?.length === 1 &&
switchAST.value === undefined
) {
const firstChild = switchAST.children[0];
path = [...path, ...firstChild.path];
value = firstChild.value;
}
return [{ path, value }];
}
return switchAST ?? null;
return switchAST;
}

@@ -111,0 +142,0 @@ },

@@ -12,3 +12,7 @@ import { describe, it, expect, vitest } from "vitest";

import { NodeType, Parser } from "../../parser";
import { StringResolverPlugin } from "../../plugins";
import {
StringResolverPlugin,
MultiNodePlugin,
AssetPlugin,
} from "../../plugins";

@@ -44,2 +48,4 @@ describe("Dynamic AST Transforms", () => {

const parser = new Parser();
new MultiNodePlugin().applyParser(parser);
const bindingParser = new BindingParser();

@@ -223,2 +229,3 @@ const inputBinding = bindingParser.parse("year");

const parser = new Parser();
new AssetPlugin().applyParser(parser);
const bindingParser = new BindingParser();

@@ -277,2 +284,3 @@ const inputBinding = bindingParser.parse("year");

const parser = new Parser();
new AssetPlugin().applyParser(parser);
const bindingParser = new BindingParser();

@@ -361,2 +369,3 @@ const rootNode = parser.parseObject(content);

const parser = new Parser();
new AssetPlugin().applyParser(parser);
const bindingParser = new BindingParser();

@@ -457,2 +466,4 @@ const rootNode = parser.parseObject(content, NodeType.View);

const parser = new Parser();
new MultiNodePlugin().applyParser(parser);
const bindingParser = new BindingParser();

@@ -521,2 +532,4 @@ const rootNode = parser.parseObject(content, NodeType.View);

const parser = new Parser();
new MultiNodePlugin().applyParser(parser);
const bindingParser = new BindingParser();

@@ -523,0 +536,0 @@ const rootNode = parser.parseObject(content, NodeType.View);

@@ -7,11 +7,6 @@ import { SyncHook } from "tapable-ts";

import type { Resolve } from "./resolver";
import { Resolver, toNodeResolveOptions } from "./resolver";
import { Resolver } from "./resolver";
import type { Node } from "./parser";
import { Parser } from "./parser";
import {
TemplatePlugin,
StringResolverPlugin,
ApplicabilityPlugin,
SwitchPlugin,
} from "./plugins";
import { TemplatePlugin } from "./plugins";

@@ -18,0 +13,0 @@ /**

@@ -11,2 +11,7 @@ import { SyncBailHook, SyncWaterfallHook } from "tapable-ts";

}
export interface ParseObjectChildOptions {
key: string;
path: Node.PathSegment[];
parentObj: object;
}
/**

@@ -37,18 +42,8 @@ * The Parser is the way to take an incoming view from the user and parse it into an AST.

onCreateASTNode: SyncWaterfallHook<[Node.Node | null | undefined, object], Record<string, any>>;
determineNodeType: SyncBailHook<[string | object], NodeType, Record<string, any>>;
parseNode: SyncBailHook<[obj: object, nodeType: Node.ChildrenTypes, parseOptions: ParseObjectOptions, determinedNodeType: NodeType | null], Node.Node, Record<string, any>>;
parseNode: SyncBailHook<[obj: object, nodeType: Node.ChildrenTypes, parseOptions: ParseObjectOptions, childOptions?: ParseObjectChildOptions | undefined], Node.Node | Node.Child[], Record<string, any>>;
};
parseView(value: AnyAssetType): Node.View;
private parseAsync;
createASTNode(node: Node.Node | null, value: any): Node.Node | null;
/**
* Checks if there are templated values in the object
*
* @param obj - The Parsed Object to check to see if we have a template array type for
* @param localKey - The key being checked
*/
private hasTemplateValues;
private hasSwitchKey;
parseObject(obj: object, type?: Node.ChildrenTypes, options?: ParseObjectOptions): Node.Node | null;
}
//# sourceMappingURL=index.d.ts.map
import type { Node } from "./types";
/** Check to see if the object contains async */
export declare function hasAsync(obj: object): boolean;
/**
* Checks if there are templated values in the object
*
* @param obj - The Parsed Object to check to see if we have a template array type for
* @param localKey - The key being checked
*/
export declare function hasTemplateValues(obj: any, localKey: string): any;
/** Check to see if the string is a valid switch key */
export declare function hasSwitchKey(localKey: string): boolean;
/** Check to see if the string is a valid template key */
export declare function hasTemplateKey(localKey: string): boolean;
/** Get the ID of the Node if there is one */
export declare function getNodeID(node?: Node.Node | null): string | undefined;
//# sourceMappingURL=utils.d.ts.map

@@ -6,2 +6,3 @@ import type { Resolver } from "../resolver";

export default class ApplicabilityPlugin implements ViewPlugin {
private isApplicability;
applyResolver(resolver: Resolver): void;

@@ -8,0 +9,0 @@ applyParser(parser: Parser): void;

@@ -1,5 +0,7 @@

export { default as TemplatePlugin } from "./template-plugin";
export { default as TemplatePlugin } from "./template";
export { default as StringResolverPlugin } from "./string-resolver";
export { default as ApplicabilityPlugin } from "./applicability";
export { default as SwitchPlugin } from "./switch";
export { default as MultiNodePlugin } from "./multi-node";
export { default as AssetPlugin } from "./asset";
//# sourceMappingURL=index.d.ts.map

@@ -0,5 +1,5 @@

import { ViewInstance, ViewPlugin } from "../view";
import type { Options } from "./options";
import type { Parser } from "../parser";
import type { Resolver } from "../resolver";
import { ViewInstance, ViewPlugin } from "../view";
/** A view plugin to resolve switches */

@@ -10,2 +10,3 @@ export default class SwitchPlugin implements ViewPlugin {

private resolveSwitch;
private isSwitch;
applyParser(parser: Parser): void;

@@ -12,0 +13,0 @@ applyResolver(resolver: Resolver): void;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc