Skip to content

FIO-9783: refactor evaluator #245

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module.exports = {
mode: 'development',
entry: {
'formio.core.js': './lib/index.js',
'formio.modules.js': './lib/modules/index.js',
'formio.js': './lib/sdk/index.js',
'formio.utils.js': './lib/utils/index.js',
'formio.process.js': './lib/process/index.js',
Expand Down
1 change: 0 additions & 1 deletion config/webpack.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const config = require('./webpack.config');
config.mode = 'production';
config.entry = {
'formio.core.min.js': './lib/index.js',
'formio.modules.min.js': './lib/modules/index.js',
'formio.min.js': './lib/sdk/index.js',
'formio.utils.min.js': './lib/utils/index.js',
};
Expand Down
6 changes: 2 additions & 4 deletions src/experimental/core.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import 'core-js/features/object/from-entries';
import { Formio } from '../sdk';
import { Evaluator, Utils } from '../utils';
import { Evaluator, Utils, registerEvaluator } from '../utils';
import { Components, render } from './base';
import { Template } from './template';
import { merge } from 'lodash';
import components from './components';
import modules from '../modules';

export default class FormioCore extends Formio {
static Components = Components;
Expand Down Expand Up @@ -60,7 +59,7 @@ export default class FormioCore extends Formio {
if (!(Formio as any).Evaluator) {
return;
}
(Formio as any).Evaluator.registerEvaluator(plugin);
registerEvaluator(plugin);
break;
default:
console.log('Unknown plugin option', key);
Expand Down Expand Up @@ -95,4 +94,3 @@ export default class FormioCore extends Formio {
}

FormioCore.use(components);
FormioCore.use(modules);
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './modules';
export * from './utils';
export * from './process/validation';
export * from './process/validation/rules';
Expand Down
2 changes: 0 additions & 2 deletions src/modules/index.ts

This file was deleted.

82 changes: 0 additions & 82 deletions src/modules/jsonlogic/index.ts

This file was deleted.

22 changes: 13 additions & 9 deletions src/process/calculation/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { JSONLogicEvaluator } from 'modules/jsonlogic';
import {
ProcessorFn,
ProcessorFnSync,
Expand All @@ -8,7 +7,7 @@ import {
FetchScope,
} from 'types';
import { set } from 'lodash';
import { normalizeContext } from 'utils/formUtil';
import { evaluate } from 'utils';

export const shouldCalculate = (context: CalculationContext): boolean => {
const { component, config } = context;
Expand All @@ -21,20 +20,25 @@ export const shouldCalculate = (context: CalculationContext): boolean => {
export const calculateProcessSync: ProcessorFnSync<CalculationScope> = (
context: CalculationContext,
) => {
const { component, data, evalContext, scope, path, value } = context;
if (!shouldCalculate(context)) {
const { component, data, scope, path, value } = context;
if (!shouldCalculate(context) || !component.calculateValue) {
return;
}

const calculationContext = (scope as FetchScope).fetched
? { ...context, data: { ...data, ...(scope as FetchScope).fetched } }
: context;
const evalContextValue = evalContext
? evalContext(normalizeContext(calculationContext))
: normalizeContext(calculationContext);
evalContextValue.value = value || null;

if (!scope.calculated) scope.calculated = [];
const newValue = JSONLogicEvaluator.evaluate(component.calculateValue, evalContextValue, 'value');
const newValue = evaluate(
component.calculateValue,
calculationContext,
'value',
false,
(context) => {
context.value = value || null;
},
);

// Only set a new value if it is not "null" which would be the case if no calculation occurred.
if (newValue !== null) {
Expand Down
8 changes: 4 additions & 4 deletions src/process/clearHidden/__tests__/clearHidden.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';

import { clearHiddenProcess } from '../index';
import { clearHiddenProcessSync } from '../index';

describe('clearHidden', function () {
it('Shoud not clear conditionally hidden component data when clearOnHide is false', function () {
Expand Down Expand Up @@ -28,7 +28,7 @@ describe('clearHidden', function () {
},
path: 'foo',
};
clearHiddenProcess(context);
clearHiddenProcessSync(context);
expect(context.data).to.deep.equal({ foo: 'bar' });
});

Expand Down Expand Up @@ -57,7 +57,7 @@ describe('clearHidden', function () {
},
path: 'foo',
};
clearHiddenProcess(context);
clearHiddenProcessSync(context);
expect(context.data).to.deep.equal({});
});

Expand All @@ -81,7 +81,7 @@ describe('clearHidden', function () {
},
path: 'foo',
};
clearHiddenProcess(context);
clearHiddenProcessSync(context);
expect(context.data).to.deep.equal({ foo: 'bar' });
});
});
10 changes: 8 additions & 2 deletions src/process/clearHidden/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ProcessorInfo,
ProcessorFnSync,
ConditionsScope,
ProcessorFn,
} from 'types';

type ClearHiddenScope = ProcessorScope & {
Expand All @@ -16,7 +17,7 @@ type ClearHiddenScope = ProcessorScope & {
/**
* This processor function checks components for the `hidden` property and unsets corresponding data
*/
export const clearHiddenProcess: ProcessorFnSync<ClearHiddenScope> = (context) => {
export const clearHiddenProcessSync: ProcessorFnSync<ClearHiddenScope> = (context) => {
const { component, data, value, scope, path } = context;

// No need to unset the value if it's undefined
Expand Down Expand Up @@ -45,8 +46,13 @@ export const clearHiddenProcess: ProcessorFnSync<ClearHiddenScope> = (context) =
}
};

export const clearHiddenProcess: ProcessorFn<ClearHiddenScope> = async (context) => {
return clearHiddenProcessSync(context);
};

export const clearHiddenProcessInfo: ProcessorInfo<ProcessorContext<ClearHiddenScope>, void> = {
name: 'clearHidden',
shouldProcess: () => true,
processSync: clearHiddenProcess,
process: clearHiddenProcess,
processSync: clearHiddenProcessSync,
};
16 changes: 7 additions & 9 deletions src/process/defaultValue/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { JSONLogicEvaluator } from 'modules/jsonlogic';
import {
ProcessorFn,
ProcessorFnSync,
Expand All @@ -7,7 +6,8 @@ import {
DefaultValueContext,
} from 'types';
import { set, has } from 'lodash';
import { getComponentKey, normalizeContext } from 'utils/formUtil';
import { evaluate } from 'utils';
import { getComponentKey } from 'utils/formUtil';

export const hasCustomDefaultValue = (context: DefaultValueContext): boolean => {
const { component } = context;
Expand Down Expand Up @@ -38,7 +38,7 @@ export const customDefaultValueProcess: ProcessorFn<ConditionsScope> = async (
export const customDefaultValueProcessSync: ProcessorFnSync<ConditionsScope> = (
context: DefaultValueContext,
) => {
const { component, row, data, scope, evalContext, path } = context;
const { component, row, data, scope, path } = context;
if (!hasCustomDefaultValue(context)) {
return;
}
Expand All @@ -48,14 +48,12 @@ export const customDefaultValueProcessSync: ProcessorFnSync<ConditionsScope> = (
}
let defaultValue = null;
if (component.customDefaultValue) {
const evalContextValue = evalContext
? evalContext(normalizeContext(context))
: normalizeContext(context);
evalContextValue.value = null;
defaultValue = JSONLogicEvaluator.evaluate(
defaultValue = evaluate(
component.customDefaultValue,
evalContextValue,
context,
'value',
false,
(context) => (context.value = null),
);
if (component.multiple && !Array.isArray(defaultValue)) {
defaultValue = defaultValue ? [defaultValue] : [];
Expand Down
19 changes: 8 additions & 11 deletions src/process/fetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
FilterContext,
} from 'types';
import { get, set } from 'lodash';
import { Evaluator } from 'utils';
import { getComponentKey, normalizeContext } from 'utils/formUtil';
import { evaluate, interpolate } from 'utils';
import { getComponentKey } from 'utils/formUtil';

export const shouldFetch = (context: FetchContext): boolean => {
const { component, config } = context;
Expand All @@ -23,7 +23,7 @@ export const shouldFetch = (context: FetchContext): boolean => {
};

export const fetchProcess: ProcessorFn<FetchScope> = async (context: FetchContext) => {
const { component, row, evalContext, path, scope, config } = context;
const { component, row, path, scope, config } = context;
let _fetch: FetchFn | null = null;
try {
_fetch = context.fetch ? context.fetch : fetch;
Expand All @@ -38,10 +38,7 @@ export const fetchProcess: ProcessorFn<FetchScope> = async (context: FetchContex
return;
}
if (!scope.fetched) scope.fetched = {};
const evalContextValue = evalContext
? evalContext(normalizeContext(context))
: normalizeContext(context);
const url = Evaluator.interpolateString(get(component, 'fetch.url', ''), evalContextValue);
const url = interpolate(get(component, 'fetch.url', ''), context);
if (!url) {
return;
}
Expand All @@ -66,7 +63,7 @@ export const fetchProcess: ProcessorFn<FetchScope> = async (context: FetchContex
request.headers['Accept'] = '*/*';
request.headers['user-agent'] = 'Form.io DataSource Component';
get(component, 'fetch.headers', []).map((header: any) => {
header.value = Evaluator.interpolateString(header.value, evalContextValue);
header.value = interpolate(header.value, context);
if (header.value && header.key) {
request.headers[header.key] = header.value;
}
Expand All @@ -79,7 +76,7 @@ export const fetchProcess: ProcessorFn<FetchScope> = async (context: FetchContex

const body = get(component, 'fetch.specifyBody', '');
if (request.method === 'POST') {
request.body = JSON.stringify(Evaluator.evaluate(body, evalContextValue, 'body'));
request.body = JSON.stringify(evaluate(body, context, 'body'));
}

try {
Expand All @@ -93,10 +90,10 @@ export const fetchProcess: ProcessorFn<FetchScope> = async (context: FetchContex
row,
key,
mapFunction
? Evaluator.evaluate(
? evaluate(
mapFunction,
{
...evalContextValue,
...context,
...{ responseData: result },
},
'value',
Expand Down
6 changes: 3 additions & 3 deletions src/process/validation/rules/validateAvailableItems.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isEmpty, isUndefined, difference } from 'lodash';
import { FieldError, ProcessorError } from 'error';
import { Evaluator } from 'utils';
import { evaluate } from 'utils';
import {
RadioComponent,
SelectComponent,
Expand Down Expand Up @@ -102,7 +102,7 @@ async function getAvailableSelectValues(component: SelectComponent, context: Val
}
}
case 'custom': {
const customItems = Evaluator.evaluate(
const customItems = evaluate(
component.data.custom,
{
values: [],
Expand Down Expand Up @@ -178,7 +178,7 @@ function getAvailableSelectValuesSync(component: SelectComponent, context: Valid
}
}
case 'custom': {
const customItems = Evaluator.evaluate(
const customItems = evaluate(
component.data.custom,
{
values: [],
Expand Down
Loading
Loading