Skip to content

Commit 32532aa

Browse files
author
Niklas Kiefer
committed
feat(viewer): retrieve variables mentioned in expressions
1 parent eccfb0b commit 32532aa

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed

packages/form-js-viewer/src/util/feel.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parseUnaryTests } from 'feelin';
1+
import { parseExpressions, parseUnaryTests } from 'feelin';
22

33
/**
44
* Retrieve variable names from given FEEL unary test.
@@ -20,5 +20,28 @@ export function getVariableNames(unaryTest) {
2020

2121
} while (cursor.next());
2222

23+
return Array.from(variables);
24+
}
25+
26+
/**
27+
* Retrieve variable names from given FEEL expression.
28+
*
29+
* @param {string} expression
30+
* @returns {string[]}
31+
*/
32+
export function getExpressionVariableNames(expression) {
33+
const tree = parseExpressions(expression);
34+
const cursor = tree.cursor();
35+
36+
const variables = new Set();
37+
do {
38+
const node = cursor.node;
39+
40+
if (node.type.name === 'VariableName') {
41+
variables.add(expression.slice(node.from, node.to));
42+
}
43+
44+
} while (cursor.next());
45+
2346
return Array.from(variables);
2447
}

packages/form-js-viewer/src/util/index.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import { getVariableNames } from './feel';
1+
import { isString } from 'min-dash';
2+
3+
import { getExpressionVariableNames, getVariableNames } from './feel';
24

35
export * from './injector';
46
export * from './form';
57

8+
const EXPRESSION_PROPERTIES = [
9+
'alt',
10+
'source'
11+
];
12+
613
export function findErrors(errors, path) {
714
return errors[ pathStringify(path) ];
815
}
@@ -106,9 +113,28 @@ export function getSchemaVariables(schema) {
106113
variables = [ ...variables, ...conditionVariables ];
107114
}
108115

116+
EXPRESSION_PROPERTIES.forEach((prop) => {
117+
const property = component[prop];
118+
119+
if (property && isExpression(property)) {
120+
121+
// cut off initial '='
122+
const expressionVariables = getExpressionVariableNames(property.slice(1));
123+
124+
variables = [ ...variables, ...expressionVariables ];
125+
}
126+
});
127+
109128
return variables;
110129
}, []);
111130

112131
// remove duplicates
113132
return Array.from(new Set(variables));
133+
}
134+
135+
136+
// helper ///////////////
137+
138+
function isExpression(value) {
139+
return isString(value) && value.startsWith('=');
114140
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"components": [
3+
{
4+
"label": "An image",
5+
"source": "=logo",
6+
"type": "image"
7+
},
8+
{
9+
"label": "An image with alt text",
10+
"alt": "=alt",
11+
"type": "image"
12+
},
13+
{
14+
"label": "Another image",
15+
"alt": "This is just an image",
16+
"type": "image"
17+
}
18+
],
19+
"type": "default",
20+
"id": "Form",
21+
"schemaVersion": 6
22+
}

packages/form-js-viewer/test/spec/util/GetSchemaVariables.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
import schema from '../form.json';
66
import dynamicSchema from '../dynamic.json';
77
import conditionalSchema from '../condition-external-variable.json';
8+
import expressionSchema from '../expression-external-variable.json';
89

910
describe('util/getSchemaVariables', () => {
1011

@@ -33,4 +34,20 @@ describe('util/getSchemaVariables', () => {
3334
expect(variables).to.eql([ 'amount', 'externalVariable' ]);
3435
});
3536

37+
38+
it('should include variables in expressions', () => {
39+
40+
const variables = getSchemaVariables(expressionSchema);
41+
42+
expect(variables).to.eql([ 'logo', 'alt' ]);
43+
});
44+
45+
46+
it('should NOT include variables - no expression', () => {
47+
48+
const variables = getSchemaVariables(expressionSchema);
49+
50+
expect(variables).to.not.have.members([ 'This', 'is', 'just', 'an', 'image' ]);
51+
});
52+
3653
});

0 commit comments

Comments
 (0)