@openforis/arena-core 2.16.0 → 2.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/expression/javascript/global.js +3 -1
- package/dist/esm/expression/javascript/global.js.map +1 -1
- package/dist/esm/expression/javascript/node/identifier.js +4 -1
- package/dist/esm/expression/javascript/node/identifier.js.map +1 -1
- package/dist/esm/nodeDefExpressionEvaluator/node/identifier.js +4 -1
- package/dist/esm/nodeDefExpressionEvaluator/node/identifier.js.map +1 -1
- package/dist/esm/record/recordExpressionEvaluator/node/identifier/identifier.js +4 -0
- package/dist/esm/record/recordExpressionEvaluator/node/identifier/identifier.js.map +1 -1
- package/dist/expression/javascript/global.d.ts +1 -0
- package/dist/expression/javascript/global.js +5 -2
- package/dist/expression/javascript/global.js.map +1 -1
- package/dist/expression/javascript/node/identifier.js +3 -0
- package/dist/expression/javascript/node/identifier.js.map +1 -1
- package/dist/nodeDefExpressionEvaluator/node/identifier.js +3 -0
- package/dist/nodeDefExpressionEvaluator/node/identifier.js.map +1 -1
- package/dist/record/recordExpressionEvaluator/node/identifier/identifier.js +4 -0
- package/dist/record/recordExpressionEvaluator/node/identifier/identifier.js.map +1 -1
- package/package.json +1 -1
|
@@ -8,13 +8,15 @@ const globalObjectsDictionary = {
|
|
|
8
8
|
String,
|
|
9
9
|
};
|
|
10
10
|
const globalObjectsArray = Object.values(globalObjectsDictionary);
|
|
11
|
+
// properties leading to Object, Function or the prototypes would let an expression run arbitrary code
|
|
12
|
+
export const isForbiddenPropertyName = (name) => name === 'constructor' || name === 'prototype' || name.startsWith('__');
|
|
11
13
|
export const getGlobalObjectProperty = (name, object) => {
|
|
12
14
|
const globalObject = globalObjectsDictionary[name];
|
|
13
15
|
if (globalObject) {
|
|
14
16
|
return globalObject;
|
|
15
17
|
}
|
|
16
18
|
if (object && globalObjectsArray.includes(object)) {
|
|
17
|
-
const property = object[name];
|
|
19
|
+
const property = isForbiddenPropertyName(name) ? undefined : object[name];
|
|
18
20
|
if (!property) {
|
|
19
21
|
throw new SystemError(SystemErrorKeys.expression.undefinedFunction, { name });
|
|
20
22
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"global.js","sourceRoot":"","sources":["../../../../src/expression/javascript/global.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAE1D,MAAM,uBAAuB,GAAoB;IAC/C,KAAK;IACL,OAAO;IACP,IAAI;IACJ,IAAI;IACJ,MAAM;IACN,MAAM;CACP,CAAA;AACD,MAAM,kBAAkB,GAAG,MAAM,CAAC,MAAM,CAAC,uBAAuB,CAAC,CAAA;AAEjE,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,IAAY,EAAE,MAAW,EAAO,EAAE;IACxE,MAAM,YAAY,GAAG,uBAAuB,CAAC,IAAI,CAAC,CAAA;IAClD,IAAI,YAAY,EAAE,CAAC;QACjB,OAAO,YAAY,CAAA;IACrB,CAAC;IACD,IAAI,MAAM,IAAI,kBAAkB,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;QAClD,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"global.js","sourceRoot":"","sources":["../../../../src/expression/javascript/global.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAE1D,MAAM,uBAAuB,GAAoB;IAC/C,KAAK;IACL,OAAO;IACP,IAAI;IACJ,IAAI;IACJ,MAAM;IACN,MAAM;CACP,CAAA;AACD,MAAM,kBAAkB,GAAG,MAAM,CAAC,MAAM,CAAC,uBAAuB,CAAC,CAAA;AAEjE,sGAAsG;AACtG,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,IAAY,EAAW,EAAE,CAC/D,IAAI,KAAK,aAAa,IAAI,IAAI,KAAK,WAAW,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;AAEzE,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,IAAY,EAAE,MAAW,EAAO,EAAE;IACxE,MAAM,YAAY,GAAG,uBAAuB,CAAC,IAAI,CAAC,CAAA;IAClD,IAAI,YAAY,EAAE,CAAC;QACjB,OAAO,YAAY,CAAA;IACrB,CAAC;IACD,IAAI,MAAM,IAAI,kBAAkB,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;QAClD,MAAM,QAAQ,GAAG,uBAAuB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAEzE,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,MAAM,IAAI,WAAW,CAAC,eAAe,CAAC,UAAU,CAAC,iBAAiB,EAAE,EAAE,IAAI,EAAE,CAAC,CAAA;QAC/E,CAAC;QACD,OAAO,QAAQ,CAAA;IACjB,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC,CAAA","sourcesContent":["import { Dictionary } from '../../common'\nimport { SystemError, SystemErrorKeys } from '../../error'\n\nconst globalObjectsDictionary: Dictionary<any> = {\n Array,\n Boolean,\n Date,\n Math,\n Number,\n String,\n}\nconst globalObjectsArray = Object.values(globalObjectsDictionary)\n\n// properties leading to Object, Function or the prototypes would let an expression run arbitrary code\nexport const isForbiddenPropertyName = (name: string): boolean =>\n name === 'constructor' || name === 'prototype' || name.startsWith('__')\n\nexport const getGlobalObjectProperty = (name: string, object: any): any => {\n const globalObject = globalObjectsDictionary[name]\n if (globalObject) {\n return globalObject\n }\n if (object && globalObjectsArray.includes(object)) {\n const property = isForbiddenPropertyName(name) ? undefined : object[name]\n\n if (!property) {\n throw new SystemError(SystemErrorKeys.expression.undefinedFunction, { name })\n }\n return property\n }\n return null\n}\n"]}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { SystemError, SystemErrorKeys } from '../../../error';
|
|
2
2
|
import { ExpressionNodeEvaluator } from '../../node';
|
|
3
|
-
import { getGlobalObjectProperty } from '../global';
|
|
3
|
+
import { getGlobalObjectProperty, isForbiddenPropertyName } from '../global';
|
|
4
4
|
const getNativeProperty = (name, object) => {
|
|
5
|
+
if (isForbiddenPropertyName(name)) {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
5
8
|
const prop = object === null || object === void 0 ? void 0 : object[name];
|
|
6
9
|
if (prop === undefined) {
|
|
7
10
|
return undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../../../src/expression/javascript/node/identifier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAE7D,OAAO,EAAE,uBAAuB,EAAwB,MAAM,YAAY,CAAA;AAC1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../../../src/expression/javascript/node/identifier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAE7D,OAAO,EAAE,uBAAuB,EAAwB,MAAM,YAAY,CAAA;AAC1E,OAAO,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAA;AAE5E,MAAM,iBAAiB,GAAG,CAAC,IAAY,EAAE,MAAW,EAAE,EAAE;IACtD,IAAI,uBAAuB,CAAC,IAAI,CAAC,EAAE,CAAC;QAClC,OAAO,SAAS,CAAA;IAClB,CAAC;IACD,MAAM,IAAI,GAAG,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAG,IAAI,CAAC,CAAA;IAC3B,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACvB,OAAO,SAAS,CAAA;IAClB,CAAC;IACD,OAAO,OAAO,IAAI,KAAK,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;AAC9D,CAAC,CAAA;AAED,MAAM,OAAO,mBAAiD,SAAQ,uBAAgD;IACpH;;;;;;OAMG;IACO,0BAA0B,CAAC,cAAoC;QACvE,MAAM,EAAE,IAAI,EAAE,GAAG,cAAc,CAAA;QAC/B,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,GAAG,IAAI,CAAC,OAAO,CAAA;QAE9C,MAAM,UAAU,GAAG,uBAAuB,CAAC,IAAI,EAAE,aAAa,CAAC,CAAA;QAC/D,IAAI,UAAU,KAAK,IAAI,EAAE,CAAC;YACxB,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,CAAA;QAC9B,CAAC;QACD,MAAM,cAAc,GAAG,iBAAiB,CAAC,IAAI,EAAE,aAAa,CAAC,CAAA;QAC7D,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;YACjC,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,CAAA;QAClC,CAAC;QACD,OAAO,IAAI,CAAA;IACb,CAAC;IAED,QAAQ,CAAC,cAAoC;QAC3C,MAAM,QAAQ,GAAG,IAAI,CAAC,0BAA0B,CAAC,cAAc,CAAC,CAAA;QAChE,IAAI,QAAQ,EAAE,CAAC;YACb,OAAO,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;QACxC,CAAC;QACD,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,WAAW,CAAC,eAAe,CAAC,UAAU,CAAC,kBAAkB,EAAE,EAAE,IAAI,EAAE,cAAc,CAAC,IAAI,EAAE,CAAC,CAAC,CAAA;IACtH,CAAC;CACF","sourcesContent":["import { SystemError, SystemErrorKeys } from '../../../error'\nimport { ExpressionContext } from '../../context'\nimport { ExpressionNodeEvaluator, IdentifierExpression } from '../../node'\nimport { getGlobalObjectProperty, isForbiddenPropertyName } from '../global'\n\nconst getNativeProperty = (name: string, object: any) => {\n if (isForbiddenPropertyName(name)) {\n return undefined\n }\n const prop = object?.[name]\n if (prop === undefined) {\n return undefined\n }\n return typeof prop === 'function' ? prop.bind(object) : prop\n}\n\nexport class IdentifierEvaluator<C extends ExpressionContext> extends ExpressionNodeEvaluator<C, IdentifierExpression> {\n /**\n * Looks for the identifier among global or native properties, without throwing when it is not found\n * (creating errors captures stack traces, which is expensive when it happens for every evaluated identifier).\n *\n * @param expressionNode - The identifier expression node.\n * @returns The property value wrapped in an object, or null if the identifier has not been found.\n */\n protected findGlobalOrNativeProperty(expressionNode: IdentifierExpression): { value: any } | null {\n const { name } = expressionNode\n const { object: contextObject } = this.context\n\n const globalProp = getGlobalObjectProperty(name, contextObject)\n if (globalProp !== null) {\n return { value: globalProp }\n }\n const nativeProperty = getNativeProperty(name, contextObject)\n if (nativeProperty !== undefined) {\n return { value: nativeProperty }\n }\n return null\n }\n\n evaluate(expressionNode: IdentifierExpression): Promise<any> {\n const property = this.findGlobalOrNativeProperty(expressionNode)\n if (property) {\n return Promise.resolve(property.value)\n }\n return Promise.reject(new SystemError(SystemErrorKeys.expression.identifierNotFound, { name: expressionNode.name }))\n }\n}\n"]}
|
|
@@ -2,7 +2,7 @@ import { getNodeDefChildren, getNodeDefParent, getNodeDefSource } from '../../su
|
|
|
2
2
|
import { NodeDefs } from '../../nodeDef/nodeDefs';
|
|
3
3
|
import { Objects, Queue } from '../../utils';
|
|
4
4
|
import { IdentifierEvaluator } from '../../expression/javascript/node/identifier';
|
|
5
|
-
import { getGlobalObjectProperty } from '../../expression/javascript/global';
|
|
5
|
+
import { getGlobalObjectProperty, isForbiddenPropertyName } from '../../expression/javascript/global';
|
|
6
6
|
import { ExpressionVariable } from '../../expression';
|
|
7
7
|
import { SystemError, SystemErrorKeys } from '../../error';
|
|
8
8
|
import { ValidatorErrorKeys } from '../../validation';
|
|
@@ -42,6 +42,9 @@ export class NodeDefIdentifierEvaluator extends IdentifierEvaluator {
|
|
|
42
42
|
const { context } = this;
|
|
43
43
|
const { nodeDefContext, object: objectContext, itemsFilter, memberProperty } = context;
|
|
44
44
|
const { name: exprName } = expressionNode;
|
|
45
|
+
if (isForbiddenPropertyName(exprName)) {
|
|
46
|
+
this.throwIdentifierNotFound(expressionNode);
|
|
47
|
+
}
|
|
45
48
|
if (exprName === ExpressionVariable.CONTEXT) {
|
|
46
49
|
if (nodeDefContext) {
|
|
47
50
|
this.addReferencedNodeDefUuid(nodeDefContext.uuid);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../../src/nodeDefExpressionEvaluator/node/identifier.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AACtG,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAA;AACjD,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,EAAE,mBAAmB,EAAE,MAAM,6CAA6C,CAAA;AACjF,OAAO,EAAE,uBAAuB,EAAE,MAAM,oCAAoC,CAAA;AAE5E,OAAO,EAAE,kBAAkB,EAAwB,MAAM,kBAAkB,CAAA;AAC3E,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAA;AAC1E,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAElD;;;;;GAKG;AACH,MAAM,qBAAqB,GAAG,CAAC,MAE9B,EAAkD,EAAE;IACnD,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CAAA;IAC1B,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,oBAAoB,EAAE,GAAG,OAAO,CAAA;IAExD,IAAI,CAAC,oBAAoB;QAAE,OAAO,SAAS,CAAA;IAE3C,IAAI,QAAQ,CAAC,WAAW,CAAC,oB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G,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC;oBACtD,qBAAqB,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAA;oBAChD,MAAM,gBAAgB,CAAA;gBACxB,CAAC;gBACD,YAAY,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAA;YACzC,CAAC;QACH,CAAC;IACH,CAAC;CACF","sourcesContent":["import { NodeDef, NodeDefProps, NodeDefType } from '../../nodeDef/nodeDef'\nimport { getNodeDefChildren, getNodeDefParent, getNodeDefSource } from '../../survey/surveys/nodeDefs'\nimport { NodeDefs } from '../../nodeDef/nodeDefs'\nimport { Objects, Queue } from '../../utils'\nimport { IdentifierEvaluator } from '../../expression/javascript/node/identifier'\nimport { getGlobalObjectProperty } from '../../expression/javascript/global'\nimport { NodeDefExpressionContext } from '../context'\nimport { ExpressionVariable, IdentifierExpression } from '../../expression'\nimport { SystemError, SystemErrorKeys } from '../../error'\nimport { ValidatorErrorKeys } from '../../validation'\nimport { NodeNativeProperties } from './nodeDefExpressionNativeProperties'\nimport { NodeValues } from '../../node/nodeValues'\n\n/**\n * Determines the actual context node def\n * - attribute def => parent entity def\n * - virtual entity def => source node def\n * - entity def => entity def itself\n */\nconst findActualContextNode = (params: {\n context: NodeDefExpressionContext\n}): NodeDef<NodeDefType, NodeDefProps> | undefined => {\n const { context } = params\n const { survey, object: nodeDefObjectContext } = context\n\n if (!nodeDefObjectContext) return undefined\n\n if (NodeDefs.isAttribute(nodeDefObjectContext)) {\n return getNodeDefParent({ survey, nodeDef: nodeDefObjectContext })\n }\n if (nodeDefObjectContext.virtual) {\n return getNodeDefSource({ survey, nodeDef: nodeDefObjectContext })\n }\n return nodeDefObjectContext\n}\n\nexport class NodeDefIdentifierEvaluator extends IdentifierEvaluator<NodeDefExpressionContext> {\n evaluate(expressionNode: IdentifierExpression): Promise<any> {\n try {\n return Promise.resolve(this.evaluateIdentifier(expressionNode))\n } catch (error) {\n return Promise.reject(error)\n }\n }\n\n private evaluateIdentifier(expressionNode: IdentifierExpression): any {\n const { context } = this\n const { nodeDefContext, object: objectContext, itemsFilter, memberProperty } = context\n const { name: exprName } = expressionNode\n\n if (exprName === ExpressionVariable.CONTEXT) {\n if (nodeDefContext) {\n this.addReferencedNodeDefUuid(nodeDefContext.uuid)\n }\n return nodeDefContext\n }\n\n // global objects (e.g. Math, String)\n const globalObjectProperty = getGlobalObjectProperty(exprName, objectContext)\n if (globalObjectProperty !== null) {\n return globalObjectProperty\n }\n\n if (itemsFilter) {\n const prop = objectContext?.props?.[exprName] ?? objectContext?.props?.extra?.[exprName]\n if (!Objects.isEmpty(prop)) {\n return prop\n }\n }\n\n // check if identifier is a composite attribute value prop\n if (NodeDefs.isAttribute(objectContext) && NodeValues.isValueProp({ nodeDef: objectContext, prop: exprName })) {\n return objectContext\n }\n\n if (memberProperty) {\n // property of a member expression (e.g. text_attr.length or text_attr.toUpperCase()): native properties first\n const nativeProperty = this.findNativeProperty(expressionNode)\n if (nativeProperty) return nativeProperty.value\n return this.getReferencedNodeDef(expressionNode)\n }\n // identifier not in a member expression: node defs first, so that node defs named like native properties\n // (e.g. \"name\", \"type\", \"trim\") are always found (and registered as dependencies)\n const referencedNodeDef = this.findReferencedNodeDef(expressionNode)\n if (referencedNodeDef) return referencedNodeDef\n\n const nativeProperty = this.findNativeProperty(expressionNode)\n if (nativeProperty) return nativeProperty.value\n\n return this.throwIdentifierNotFound(expressionNode)\n }\n\n /**\n * Finds the identifier among the native properties or functions of the context object\n * (e.g. String.length or String.toUpperCase()).\n */\n private findNativeProperty(expressionNode: IdentifierExpression): { value: any } | null {\n const { object: objectContext } = this.context\n const { name: exprName } = expressionNode\n\n const globalOrNativeProperty = this.findGlobalOrNativeProperty(expressionNode)\n if (globalOrNativeProperty) {\n return globalOrNativeProperty\n }\n if (NodeNativeProperties.hasNativeProperty({ nodeDefOrValue: objectContext, propName: exprName })) {\n return {\n value: NodeNativeProperties.evalNodeDefProperty({ nodeDefOrValue: objectContext, propName: exprName }),\n }\n }\n return null\n }\n\n private findReferencedNodeDef(expressionNode: IdentifierExpression): NodeDef<NodeDefType, NodeDefProps> | undefined {\n const { nodeDefCurrent, selfReferenceAllowed } = this.context\n const referencedNodeDef = this.findIdentifierAmongReachableNodeDefs(expressionNode)\n if (!referencedNodeDef) return undefined\n\n if (!selfReferenceAllowed && referencedNodeDef.uuid === nodeDefCurrent?.uuid) {\n throw new SystemError(ValidatorErrorKeys.expressions.cannotUseCurrentNode, { name: expressionNode.name })\n }\n this.addReferencedNodeDefUuid(referencedNodeDef.uuid)\n return referencedNodeDef\n }\n\n private getReferencedNodeDef(expressionNode: IdentifierExpression): NodeDef<NodeDefType, NodeDefProps> {\n return this.findReferencedNodeDef(expressionNode) ?? this.throwIdentifierNotFound(expressionNode)\n }\n\n private throwIdentifierNotFound(expressionNode: IdentifierExpression): never {\n throw new SystemError(SystemErrorKeys.expression.identifierNotFound, {\n name: expressionNode.name,\n contextObject: this.context.object?.props?.name,\n })\n }\n\n private addReferencedNodeDefUuid(uuid: string) {\n const { context } = this\n const { referencedNodeDefUuids = new Set() } = context\n context.referencedNodeDefUuids = referencedNodeDefUuids.add(uuid)\n }\n\n /**\n * Tries to find the specified identifier among the node defs that can be \"reached\" from the context node def.\n */\n protected findIdentifierAmongReachableNodeDefs(\n expressionNode: IdentifierExpression\n ): NodeDef<NodeDefType, NodeDefProps> | undefined {\n const { object: contextObject } = this.context\n\n if (!contextObject) return undefined\n\n // stop at the first match (same order as getReachableNodeDefs), without visiting all the reachable node defs\n for (const reachableNodeDef of this.iterateReachableNodeDefs()) {\n if (reachableNodeDef.props.name === expressionNode.name) {\n return reachableNodeDef\n }\n }\n return undefined\n }\n\n /**\n * Get reachable node defs, i.e. the children of the node definition's ancestors.\n * NOTE: The root node def is excluded, but it _should_ be an entity, so that is fine.\n */\n protected getReachableNodeDefs(): NodeDef<NodeDefType, NodeDefProps>[] {\n return Array.from(this.iterateReachableNodeDefs())\n }\n\n /**\n * Iterates over the reachable node defs (see getReachableNodeDefs), yielding every node def only once.\n */\n private *iterateReachableNodeDefs(): Generator<NodeDef<NodeDefType, NodeDefProps>> {\n const { context } = this\n const { survey, includeAnalysis } = context\n\n const reachableNodeDefUuids = new Set<string>()\n const visitedUuids = new Set<string>()\n\n const queue = new Queue<NodeDef<NodeDefType>>()\n\n const actualContextNode = findActualContextNode({ context })\n if (actualContextNode) {\n queue.enqueue(actualContextNode)\n reachableNodeDefUuids.add(actualContextNode.uuid)\n yield actualContextNode\n }\n\n while (!queue.isEmpty()) {\n const entityDefCurrent = queue.dequeue()!\n const entityDefCurrentChildren = getNodeDefChildren({ survey, nodeDef: entityDefCurrent, includeAnalysis })\n for (const childDef of entityDefCurrentChildren) {\n if (!reachableNodeDefUuids.has(childDef.uuid)) {\n reachableNodeDefUuids.add(childDef.uuid)\n yield childDef\n }\n }\n // visit nodes inside single entities\n queue.enqueueItems(entityDefCurrentChildren.filter(NodeDefs.isSingleEntity))\n\n // avoid visiting 2 times the same entity definition when traversing single entities\n if (!visitedUuids.has(entityDefCurrent.uuid)) {\n const entityDefCurrentParent = getNodeDefParent({ survey, nodeDef: entityDefCurrent })\n if (entityDefCurrentParent) {\n queue.enqueue(entityDefCurrentParent)\n }\n if (!reachableNodeDefUuids.has(entityDefCurrent.uuid)) {\n reachableNodeDefUuids.add(entityDefCurrent.uuid)\n yield entityDefCurrent\n }\n visitedUuids.add(entityDefCurrent.uuid)\n }\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../../src/nodeDefExpressionEvaluator/node/identifier.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AACtG,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAA;AACjD,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,EAAE,m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BAAkB,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,CAAC,CAAA;YAC3G,KAAK,MAAM,QAAQ,IAAI,wBAAwB,EAAE,CAAC;gBAChD,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;oBAC9C,qBAAqB,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;oBACxC,MAAM,QAAQ,CAAA;gBAChB,CAAC;YACH,CAAC;YACD,qCAAqC;YACrC,KAAK,CAAC,YAAY,CAAC,wBAAwB,CAAC,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAA;YAE5E,oFAAoF;YACpF,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC7C,MAAM,sBAAsB,GAAG,gBAAgB,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,CAAC,CAAA;gBACtF,IAAI,sBAAsB,EAAE,CAAC;oBAC3B,KAAK,CAAC,OAAO,CAAC,sBAAsB,CAAC,CAAA;gBACvC,CAAC;gBACD,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC;oBACtD,qBAAqB,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAA;oBAChD,MAAM,gBAAgB,CAAA;gBACxB,CAAC;gBACD,YAAY,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAA;YACzC,CAAC;QACH,CAAC;IACH,CAAC;CACF","sourcesContent":["import { NodeDef, NodeDefProps, NodeDefType } from '../../nodeDef/nodeDef'\nimport { getNodeDefChildren, getNodeDefParent, getNodeDefSource } from '../../survey/surveys/nodeDefs'\nimport { NodeDefs } from '../../nodeDef/nodeDefs'\nimport { Objects, Queue } from '../../utils'\nimport { IdentifierEvaluator } from '../../expression/javascript/node/identifier'\nimport { getGlobalObjectProperty, isForbiddenPropertyName } from '../../expression/javascript/global'\nimport { NodeDefExpressionContext } from '../context'\nimport { ExpressionVariable, IdentifierExpression } from '../../expression'\nimport { SystemError, SystemErrorKeys } from '../../error'\nimport { ValidatorErrorKeys } from '../../validation'\nimport { NodeNativeProperties } from './nodeDefExpressionNativeProperties'\nimport { NodeValues } from '../../node/nodeValues'\n\n/**\n * Determines the actual context node def\n * - attribute def => parent entity def\n * - virtual entity def => source node def\n * - entity def => entity def itself\n */\nconst findActualContextNode = (params: {\n context: NodeDefExpressionContext\n}): NodeDef<NodeDefType, NodeDefProps> | undefined => {\n const { context } = params\n const { survey, object: nodeDefObjectContext } = context\n\n if (!nodeDefObjectContext) return undefined\n\n if (NodeDefs.isAttribute(nodeDefObjectContext)) {\n return getNodeDefParent({ survey, nodeDef: nodeDefObjectContext })\n }\n if (nodeDefObjectContext.virtual) {\n return getNodeDefSource({ survey, nodeDef: nodeDefObjectContext })\n }\n return nodeDefObjectContext\n}\n\nexport class NodeDefIdentifierEvaluator extends IdentifierEvaluator<NodeDefExpressionContext> {\n evaluate(expressionNode: IdentifierExpression): Promise<any> {\n try {\n return Promise.resolve(this.evaluateIdentifier(expressionNode))\n } catch (error) {\n return Promise.reject(error)\n }\n }\n\n private evaluateIdentifier(expressionNode: IdentifierExpression): any {\n const { context } = this\n const { nodeDefContext, object: objectContext, itemsFilter, memberProperty } = context\n const { name: exprName } = expressionNode\n\n if (isForbiddenPropertyName(exprName)) {\n this.throwIdentifierNotFound(expressionNode)\n }\n\n if (exprName === ExpressionVariable.CONTEXT) {\n if (nodeDefContext) {\n this.addReferencedNodeDefUuid(nodeDefContext.uuid)\n }\n return nodeDefContext\n }\n\n // global objects (e.g. Math, String)\n const globalObjectProperty = getGlobalObjectProperty(exprName, objectContext)\n if (globalObjectProperty !== null) {\n return globalObjectProperty\n }\n\n if (itemsFilter) {\n const prop = objectContext?.props?.[exprName] ?? objectContext?.props?.extra?.[exprName]\n if (!Objects.isEmpty(prop)) {\n return prop\n }\n }\n\n // check if identifier is a composite attribute value prop\n if (NodeDefs.isAttribute(objectContext) && NodeValues.isValueProp({ nodeDef: objectContext, prop: exprName })) {\n return objectContext\n }\n\n if (memberProperty) {\n // property of a member expression (e.g. text_attr.length or text_attr.toUpperCase()): native properties first\n const nativeProperty = this.findNativeProperty(expressionNode)\n if (nativeProperty) return nativeProperty.value\n return this.getReferencedNodeDef(expressionNode)\n }\n // identifier not in a member expression: node defs first, so that node defs named like native properties\n // (e.g. \"name\", \"type\", \"trim\") are always found (and registered as dependencies)\n const referencedNodeDef = this.findReferencedNodeDef(expressionNode)\n if (referencedNodeDef) return referencedNodeDef\n\n const nativeProperty = this.findNativeProperty(expressionNode)\n if (nativeProperty) return nativeProperty.value\n\n return this.throwIdentifierNotFound(expressionNode)\n }\n\n /**\n * Finds the identifier among the native properties or functions of the context object\n * (e.g. String.length or String.toUpperCase()).\n */\n private findNativeProperty(expressionNode: IdentifierExpression): { value: any } | null {\n const { object: objectContext } = this.context\n const { name: exprName } = expressionNode\n\n const globalOrNativeProperty = this.findGlobalOrNativeProperty(expressionNode)\n if (globalOrNativeProperty) {\n return globalOrNativeProperty\n }\n if (NodeNativeProperties.hasNativeProperty({ nodeDefOrValue: objectContext, propName: exprName })) {\n return {\n value: NodeNativeProperties.evalNodeDefProperty({ nodeDefOrValue: objectContext, propName: exprName }),\n }\n }\n return null\n }\n\n private findReferencedNodeDef(expressionNode: IdentifierExpression): NodeDef<NodeDefType, NodeDefProps> | undefined {\n const { nodeDefCurrent, selfReferenceAllowed } = this.context\n const referencedNodeDef = this.findIdentifierAmongReachableNodeDefs(expressionNode)\n if (!referencedNodeDef) return undefined\n\n if (!selfReferenceAllowed && referencedNodeDef.uuid === nodeDefCurrent?.uuid) {\n throw new SystemError(ValidatorErrorKeys.expressions.cannotUseCurrentNode, { name: expressionNode.name })\n }\n this.addReferencedNodeDefUuid(referencedNodeDef.uuid)\n return referencedNodeDef\n }\n\n private getReferencedNodeDef(expressionNode: IdentifierExpression): NodeDef<NodeDefType, NodeDefProps> {\n return this.findReferencedNodeDef(expressionNode) ?? this.throwIdentifierNotFound(expressionNode)\n }\n\n private throwIdentifierNotFound(expressionNode: IdentifierExpression): never {\n throw new SystemError(SystemErrorKeys.expression.identifierNotFound, {\n name: expressionNode.name,\n contextObject: this.context.object?.props?.name,\n })\n }\n\n private addReferencedNodeDefUuid(uuid: string) {\n const { context } = this\n const { referencedNodeDefUuids = new Set() } = context\n context.referencedNodeDefUuids = referencedNodeDefUuids.add(uuid)\n }\n\n /**\n * Tries to find the specified identifier among the node defs that can be \"reached\" from the context node def.\n */\n protected findIdentifierAmongReachableNodeDefs(\n expressionNode: IdentifierExpression\n ): NodeDef<NodeDefType, NodeDefProps> | undefined {\n const { object: contextObject } = this.context\n\n if (!contextObject) return undefined\n\n // stop at the first match (same order as getReachableNodeDefs), without visiting all the reachable node defs\n for (const reachableNodeDef of this.iterateReachableNodeDefs()) {\n if (reachableNodeDef.props.name === expressionNode.name) {\n return reachableNodeDef\n }\n }\n return undefined\n }\n\n /**\n * Get reachable node defs, i.e. the children of the node definition's ancestors.\n * NOTE: The root node def is excluded, but it _should_ be an entity, so that is fine.\n */\n protected getReachableNodeDefs(): NodeDef<NodeDefType, NodeDefProps>[] {\n return Array.from(this.iterateReachableNodeDefs())\n }\n\n /**\n * Iterates over the reachable node defs (see getReachableNodeDefs), yielding every node def only once.\n */\n private *iterateReachableNodeDefs(): Generator<NodeDef<NodeDefType, NodeDefProps>> {\n const { context } = this\n const { survey, includeAnalysis } = context\n\n const reachableNodeDefUuids = new Set<string>()\n const visitedUuids = new Set<string>()\n\n const queue = new Queue<NodeDef<NodeDefType>>()\n\n const actualContextNode = findActualContextNode({ context })\n if (actualContextNode) {\n queue.enqueue(actualContextNode)\n reachableNodeDefUuids.add(actualContextNode.uuid)\n yield actualContextNode\n }\n\n while (!queue.isEmpty()) {\n const entityDefCurrent = queue.dequeue()!\n const entityDefCurrentChildren = getNodeDefChildren({ survey, nodeDef: entityDefCurrent, includeAnalysis })\n for (const childDef of entityDefCurrentChildren) {\n if (!reachableNodeDefUuids.has(childDef.uuid)) {\n reachableNodeDefUuids.add(childDef.uuid)\n yield childDef\n }\n }\n // visit nodes inside single entities\n queue.enqueueItems(entityDefCurrentChildren.filter(NodeDefs.isSingleEntity))\n\n // avoid visiting 2 times the same entity definition when traversing single entities\n if (!visitedUuids.has(entityDefCurrent.uuid)) {\n const entityDefCurrentParent = getNodeDefParent({ survey, nodeDef: entityDefCurrent })\n if (entityDefCurrentParent) {\n queue.enqueue(entityDefCurrentParent)\n }\n if (!reachableNodeDefUuids.has(entityDefCurrent.uuid)) {\n reachableNodeDefUuids.add(entityDefCurrent.uuid)\n yield entityDefCurrent\n }\n visitedUuids.add(entityDefCurrent.uuid)\n }\n }\n }\n}\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ExpressionVariable } from '../../../../expression';
|
|
2
|
+
import { isForbiddenPropertyName } from '../../../../expression/javascript/global';
|
|
2
3
|
import { IdentifierEvaluator } from '../../../../expression/javascript/node/identifier';
|
|
3
4
|
import { NodeDefs, NodeDefType } from '../../../../nodeDef';
|
|
4
5
|
import { Records } from '../../../records';
|
|
@@ -77,6 +78,9 @@ export class RecordIdentifierEvaluator extends IdentifierEvaluator {
|
|
|
77
78
|
if (propName === ExpressionVariable.CONTEXT) {
|
|
78
79
|
return this.context.nodeContext;
|
|
79
80
|
}
|
|
81
|
+
if (isForbiddenPropertyName(propName)) {
|
|
82
|
+
throw new SystemError(SystemErrorKeys.expression.identifierNotFound, { name: propName });
|
|
83
|
+
}
|
|
80
84
|
// try to find identifier among global or native properties
|
|
81
85
|
// (not-found is signaled without throwing; the try/catch only handles the rare case of an unknown member of a global object)
|
|
82
86
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../../../../src/record/recordExpressionEvaluator/node/identifier/identifier.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C,CAAC,gBAAgB,CAAC,CAAA;YAChF,CAAC,CAAC,CAAA;QACJ,CAAC;QACD,OAAO,wBAAwB,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,QAAQ,EAAE,CAAC,CAAA;IAC1G,CAAC;CACF","sourcesContent":["import { ExpressionVariable, IdentifierExpression } from '../../../../expression'\nimport { IdentifierEvaluator } from '../../../../expression/javascript/node/identifier'\nimport { ArenaRecordNode } from '../../../../node'\nimport { NodeDef, NodeDefs, NodeDefType } from '../../../../nodeDef'\nimport { Records } from '../../../records'\nimport { NodeValues } from '../../../../node/nodeValues'\nimport { Survey, Surveys } from '../../../../survey'\nimport { RecordExpressionContext } from '../../context'\nimport { NodesFinder } from './nodesFinder'\nimport { SystemError, SystemErrorKeys } from '../../../../error'\nimport { NodeValueExtractor } from '../../nodeValueExtractor'\nimport { ArenaRecord } from '../../../record'\n\nconst getNodesOrValues = (params: {\n survey: Survey\n referencedNodes: ArenaRecordNode[]\n nodeDefReferenced: NodeDef<any>\n propName: string\n evaluateToNode?: boolean\n}): any[] => {\n const { survey, nodeDefReferenced, referencedNodes, propName, evaluateToNode } = params\n const single = !nodeDefReferenced.props.multiple\n if (single) {\n if (referencedNodes.length === 0) {\n // do not throw error if node is missing, consider its value as null\n // throw new SystemError(SystemErrorKeys.expression.nodeNotFoundForNodeDef, { nodeDefName: propName })\n return []\n }\n if (referencedNodes.length > 1)\n throw new SystemError(SystemErrorKeys.expression.multipleNodesFoundForNodeDef, { nodeDefName: propName })\n }\n if (nodeDefReferenced.type === NodeDefType.entity || evaluateToNode) {\n // return nodes\n return referencedNodes\n }\n // return node values\n return referencedNodes.map((referencedNode) =>\n NodeValueExtractor.getNodeValue({ survey, node: referencedNode, nodeDef: nodeDefReferenced })\n )\n}\n\nconst evaluateIdentifierOnNode = (params: {\n survey: Survey\n record: ArenaRecord\n nodeObject: ArenaRecordNode\n evaluateToNode?: boolean\n propName: string\n}) => {\n const { survey, record, nodeObject, evaluateToNode = false, propName } = params\n const { nodeDefUuid: nodeDefObjectUuid, value } = nodeObject\n if (!nodeDefObjectUuid) {\n throw new SystemError(SystemErrorKeys.expression.identifierNotFound, { name: propName })\n }\n\n const nodeDefObject = Surveys.getNodeDefByUuid({ survey, uuid: nodeDefObjectUuid })\n\n // node value prop (native)\n const valueProp = value?.[propName]\n if (valueProp !== undefined) {\n return valueProp\n }\n if (NodeDefs.isAttribute(nodeDefObject)) {\n // node value prop (Arena specific value property)\n if (NodeValues.isValueProp({ nodeDef: nodeDefObject, prop: propName })) {\n return NodeValues.getNodeValueProp({ nodeDef: nodeDefObject, node: nodeObject, prop: propName })\n } else {\n throw new SystemError(SystemErrorKeys.expression.invalidAttributeValuePropertyName, {\n attributeName: NodeDefs.getName(nodeDefObject),\n propName,\n })\n }\n }\n\n const nodeDefReferenced = Surveys.findNodeDefByName({ survey, name: propName })\n\n if (!nodeDefReferenced) {\n throw new SystemError(SystemErrorKeys.expression.identifierNotFound, { name: propName })\n }\n\n if (nodeObject.nodeDefUuid === nodeDefReferenced.uuid) {\n // the referenced node is the current node itself\n return nodeObject\n }\n if (Surveys.isNodeDefAncestor({ nodeDefAncestor: nodeDefReferenced, nodeDefDescendant: nodeDefObject })) {\n return Records.getAncestor(nodeObject, nodeDefReferenced.uuid)(record)\n }\n // the referenced nodes can be siblings of the current node\n const referencedNodes = NodesFinder.findDescendants({ survey, record, nodeContext: nodeObject, nodeDefReferenced })\n const nodesOrValues = getNodesOrValues({ survey, referencedNodes, nodeDefReferenced, propName, evaluateToNode })\n return nodeDefReferenced.props.multiple ? nodesOrValues : nodesOrValues[0]\n}\n\nexport class RecordIdentifierEvaluator extends IdentifierEvaluator<RecordExpressionContext> {\n evaluate(expressionNode: IdentifierExpression): Promise<any> {\n return Promise.resolve(this.evaluateValue(expressionNode))\n }\n\n private evaluateValue(expressionNode: IdentifierExpression): any {\n const { name: propName } = expressionNode\n\n if (propName === ExpressionVariable.CONTEXT) {\n return this.context.nodeContext\n }\n\n // try to find identifier among global or native properties\n // (not-found is signaled without throwing; the try/catch only handles the rare case of an unknown member of a global object)\n try {\n const property = this.findGlobalOrNativeProperty(expressionNode)\n if (property) {\n return property.value\n }\n } catch {\n // ignore it\n }\n\n // identifier not found among global or native properties\n // identifier should be a node or a node value property\n const { survey, record, evaluateToNode, object: contextObject, item } = this.context\n\n if (item && item === contextObject) {\n // evaluating category or taxon item prop or extra prop\n return item.props?.[propName] ?? item.props?.extra?.[propName]\n }\n\n if (Array.isArray(contextObject)) {\n return contextObject.flatMap((contextNode) => {\n const evaluationResult = evaluateIdentifierOnNode({\n survey,\n record,\n nodeObject: contextNode,\n evaluateToNode,\n propName,\n })\n return Array.isArray(evaluationResult) ? evaluationResult : [evaluationResult]\n })\n }\n return evaluateIdentifierOnNode({ survey, record, nodeObject: contextObject, evaluateToNode, propName })\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../../../../src/record/recordExpressionEvaluator/node/identifier/identifier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAwB,MAAM,wBAAwB,CAAA;AACjF,OAAO,EAAE,uBAAuB,EAAE,MAAM,0CAA0C,CAAA;AAClF,OAAO,EAAE,mBAAmB,EAAE,MAAM,mDAAmD,CAAA;AAEvF,OAAO,EAAW,QAAQ,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACpE,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAA;AACxD,OAAO,EAAU,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAEpD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAChE,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAG7D,MAAM,gBAAgB,GAAG,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0BAA0B,CAAC,cAAc,CAAC,CAAA;YAChE,IAAI,QAAQ,EAAE,CAAC;gBACb,OAAO,QAAQ,CAAC,KAAK,CAAA;YACvB,CAAC;QACH,CAAC;mBAAO,CAAC;YACP,YAAY;QACd,CAAC;QAED,yDAAyD;QACzD,uDAAuD;QACvD,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,aAAa,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC,OAAO,CAAA;QAEpF,IAAI,IAAI,IAAI,IAAI,KAAK,aAAa,EAAE,CAAC;YACnC,uDAAuD;YACvD,aAAO,MAAA,IAAI,CAAC,KAAK,0CAAG,QAAQ,CAAC,mCAAI,MAAA,MAAA,IAAI,CAAC,KAAK,0CAAE,KAAK,0CAAG,QAAQ,CAAC,CAAA;QAChE,CAAC;QAED,IAAI,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC;YACjC,OAAO,aAAa,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;gBAC3C,MAAM,gBAAgB,GAAG,wBAAwB,CAAC;oBAChD,MAAM;oBACN,MAAM;oBACN,UAAU,EAAE,WAAW;oBACvB,cAAc;oBACd,QAAQ;iBACT,CAAC,CAAA;gBACF,OAAO,KAAK,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAA;YAChF,CAAC,CAAC,CAAA;QACJ,CAAC;QACD,OAAO,wBAAwB,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,QAAQ,EAAE,CAAC,CAAA;IAC1G,CAAC;CACF","sourcesContent":["import { ExpressionVariable, IdentifierExpression } from '../../../../expression'\nimport { isForbiddenPropertyName } from '../../../../expression/javascript/global'\nimport { IdentifierEvaluator } from '../../../../expression/javascript/node/identifier'\nimport { ArenaRecordNode } from '../../../../node'\nimport { NodeDef, NodeDefs, NodeDefType } from '../../../../nodeDef'\nimport { Records } from '../../../records'\nimport { NodeValues } from '../../../../node/nodeValues'\nimport { Survey, Surveys } from '../../../../survey'\nimport { RecordExpressionContext } from '../../context'\nimport { NodesFinder } from './nodesFinder'\nimport { SystemError, SystemErrorKeys } from '../../../../error'\nimport { NodeValueExtractor } from '../../nodeValueExtractor'\nimport { ArenaRecord } from '../../../record'\n\nconst getNodesOrValues = (params: {\n survey: Survey\n referencedNodes: ArenaRecordNode[]\n nodeDefReferenced: NodeDef<any>\n propName: string\n evaluateToNode?: boolean\n}): any[] => {\n const { survey, nodeDefReferenced, referencedNodes, propName, evaluateToNode } = params\n const single = !nodeDefReferenced.props.multiple\n if (single) {\n if (referencedNodes.length === 0) {\n // do not throw error if node is missing, consider its value as null\n // throw new SystemError(SystemErrorKeys.expression.nodeNotFoundForNodeDef, { nodeDefName: propName })\n return []\n }\n if (referencedNodes.length > 1)\n throw new SystemError(SystemErrorKeys.expression.multipleNodesFoundForNodeDef, { nodeDefName: propName })\n }\n if (nodeDefReferenced.type === NodeDefType.entity || evaluateToNode) {\n // return nodes\n return referencedNodes\n }\n // return node values\n return referencedNodes.map((referencedNode) =>\n NodeValueExtractor.getNodeValue({ survey, node: referencedNode, nodeDef: nodeDefReferenced })\n )\n}\n\nconst evaluateIdentifierOnNode = (params: {\n survey: Survey\n record: ArenaRecord\n nodeObject: ArenaRecordNode\n evaluateToNode?: boolean\n propName: string\n}) => {\n const { survey, record, nodeObject, evaluateToNode = false, propName } = params\n const { nodeDefUuid: nodeDefObjectUuid, value } = nodeObject\n if (!nodeDefObjectUuid) {\n throw new SystemError(SystemErrorKeys.expression.identifierNotFound, { name: propName })\n }\n\n const nodeDefObject = Surveys.getNodeDefByUuid({ survey, uuid: nodeDefObjectUuid })\n\n // node value prop (native)\n const valueProp = value?.[propName]\n if (valueProp !== undefined) {\n return valueProp\n }\n if (NodeDefs.isAttribute(nodeDefObject)) {\n // node value prop (Arena specific value property)\n if (NodeValues.isValueProp({ nodeDef: nodeDefObject, prop: propName })) {\n return NodeValues.getNodeValueProp({ nodeDef: nodeDefObject, node: nodeObject, prop: propName })\n } else {\n throw new SystemError(SystemErrorKeys.expression.invalidAttributeValuePropertyName, {\n attributeName: NodeDefs.getName(nodeDefObject),\n propName,\n })\n }\n }\n\n const nodeDefReferenced = Surveys.findNodeDefByName({ survey, name: propName })\n\n if (!nodeDefReferenced) {\n throw new SystemError(SystemErrorKeys.expression.identifierNotFound, { name: propName })\n }\n\n if (nodeObject.nodeDefUuid === nodeDefReferenced.uuid) {\n // the referenced node is the current node itself\n return nodeObject\n }\n if (Surveys.isNodeDefAncestor({ nodeDefAncestor: nodeDefReferenced, nodeDefDescendant: nodeDefObject })) {\n return Records.getAncestor(nodeObject, nodeDefReferenced.uuid)(record)\n }\n // the referenced nodes can be siblings of the current node\n const referencedNodes = NodesFinder.findDescendants({ survey, record, nodeContext: nodeObject, nodeDefReferenced })\n const nodesOrValues = getNodesOrValues({ survey, referencedNodes, nodeDefReferenced, propName, evaluateToNode })\n return nodeDefReferenced.props.multiple ? nodesOrValues : nodesOrValues[0]\n}\n\nexport class RecordIdentifierEvaluator extends IdentifierEvaluator<RecordExpressionContext> {\n evaluate(expressionNode: IdentifierExpression): Promise<any> {\n return Promise.resolve(this.evaluateValue(expressionNode))\n }\n\n private evaluateValue(expressionNode: IdentifierExpression): any {\n const { name: propName } = expressionNode\n\n if (propName === ExpressionVariable.CONTEXT) {\n return this.context.nodeContext\n }\n if (isForbiddenPropertyName(propName)) {\n throw new SystemError(SystemErrorKeys.expression.identifierNotFound, { name: propName })\n }\n\n // try to find identifier among global or native properties\n // (not-found is signaled without throwing; the try/catch only handles the rare case of an unknown member of a global object)\n try {\n const property = this.findGlobalOrNativeProperty(expressionNode)\n if (property) {\n return property.value\n }\n } catch {\n // ignore it\n }\n\n // identifier not found among global or native properties\n // identifier should be a node or a node value property\n const { survey, record, evaluateToNode, object: contextObject, item } = this.context\n\n if (item && item === contextObject) {\n // evaluating category or taxon item prop or extra prop\n return item.props?.[propName] ?? item.props?.extra?.[propName]\n }\n\n if (Array.isArray(contextObject)) {\n return contextObject.flatMap((contextNode) => {\n const evaluationResult = evaluateIdentifierOnNode({\n survey,\n record,\n nodeObject: contextNode,\n evaluateToNode,\n propName,\n })\n return Array.isArray(evaluationResult) ? evaluationResult : [evaluationResult]\n })\n }\n return evaluateIdentifierOnNode({ survey, record, nodeObject: contextObject, evaluateToNode, propName })\n }\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getGlobalObjectProperty = void 0;
|
|
3
|
+
exports.getGlobalObjectProperty = exports.isForbiddenPropertyName = void 0;
|
|
4
4
|
const error_1 = require("../../error");
|
|
5
5
|
const globalObjectsDictionary = {
|
|
6
6
|
Array,
|
|
@@ -11,13 +11,16 @@ const globalObjectsDictionary = {
|
|
|
11
11
|
String,
|
|
12
12
|
};
|
|
13
13
|
const globalObjectsArray = Object.values(globalObjectsDictionary);
|
|
14
|
+
// properties leading to Object, Function or the prototypes would let an expression run arbitrary code
|
|
15
|
+
const isForbiddenPropertyName = (name) => name === 'constructor' || name === 'prototype' || name.startsWith('__');
|
|
16
|
+
exports.isForbiddenPropertyName = isForbiddenPropertyName;
|
|
14
17
|
const getGlobalObjectProperty = (name, object) => {
|
|
15
18
|
const globalObject = globalObjectsDictionary[name];
|
|
16
19
|
if (globalObject) {
|
|
17
20
|
return globalObject;
|
|
18
21
|
}
|
|
19
22
|
if (object && globalObjectsArray.includes(object)) {
|
|
20
|
-
const property = object[name];
|
|
23
|
+
const property = (0, exports.isForbiddenPropertyName)(name) ? undefined : object[name];
|
|
21
24
|
if (!property) {
|
|
22
25
|
throw new error_1.SystemError(error_1.SystemErrorKeys.expression.undefinedFunction, { name });
|
|
23
26
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"global.js","sourceRoot":"","sources":["../../../src/expression/javascript/global.ts"],"names":[],"mappings":";;;AACA,uCAA0D;AAE1D,MAAM,uBAAuB,GAAoB;IAC/C,KAAK;IACL,OAAO;IACP,IAAI;IACJ,IAAI;IACJ,MAAM;IACN,MAAM;CACP,CAAA;AACD,MAAM,kBAAkB,GAAG,MAAM,CAAC,MAAM,CAAC,uBAAuB,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"global.js","sourceRoot":"","sources":["../../../src/expression/javascript/global.ts"],"names":[],"mappings":";;;AACA,uCAA0D;AAE1D,MAAM,uBAAuB,GAAoB;IAC/C,KAAK;IACL,OAAO;IACP,IAAI;IACJ,IAAI;IACJ,MAAM;IACN,MAAM;CACP,CAAA;AACD,MAAM,kBAAkB,GAAG,MAAM,CAAC,MAAM,CAAC,uBAAuB,CAAC,CAAA;AAEjE,sGAAsG;AAC/F,MAAM,uBAAuB,GAAG,CAAC,IAAY,EAAW,EAAE,CAC/D,IAAI,KAAK,aAAa,IAAI,IAAI,KAAK,WAAW,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;AAD5D,QAAA,uBAAuB,GAAvB,uBAAuB,CACqC;AAElE,MAAM,uBAAuB,GAAG,CAAC,IAAY,EAAE,MAAW,EAAO,EAAE;IACxE,MAAM,YAAY,GAAG,uBAAuB,CAAC,IAAI,CAAC,CAAA;IAClD,IAAI,YAAY,EAAE,CAAC;QACjB,OAAO,YAAY,CAAA;IACrB,CAAC;IACD,IAAI,MAAM,IAAI,kBAAkB,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;QAClD,MAAM,QAAQ,GAAG,IAAA,QAAA,uBAAuB,EAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAEzE,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,MAAM,IAAI,mBAAW,CAAC,uBAAe,CAAC,UAAU,CAAC,iBAAiB,EAAE,EAAE,IAAI,EAAE,CAAC,CAAA;QAC/E,CAAC;QACD,OAAO,QAAQ,CAAA;IACjB,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC,CAAA;AAdY,QAAA,uBAAuB,GAAvB,uBAAuB,CAcnC","sourcesContent":["import { Dictionary } from '../../common'\nimport { SystemError, SystemErrorKeys } from '../../error'\n\nconst globalObjectsDictionary: Dictionary<any> = {\n Array,\n Boolean,\n Date,\n Math,\n Number,\n String,\n}\nconst globalObjectsArray = Object.values(globalObjectsDictionary)\n\n// properties leading to Object, Function or the prototypes would let an expression run arbitrary code\nexport const isForbiddenPropertyName = (name: string): boolean =>\n name === 'constructor' || name === 'prototype' || name.startsWith('__')\n\nexport const getGlobalObjectProperty = (name: string, object: any): any => {\n const globalObject = globalObjectsDictionary[name]\n if (globalObject) {\n return globalObject\n }\n if (object && globalObjectsArray.includes(object)) {\n const property = isForbiddenPropertyName(name) ? undefined : object[name]\n\n if (!property) {\n throw new SystemError(SystemErrorKeys.expression.undefinedFunction, { name })\n }\n return property\n }\n return null\n}\n"]}
|
|
@@ -5,6 +5,9 @@ const error_1 = require("../../../error");
|
|
|
5
5
|
const node_1 = require("../../node");
|
|
6
6
|
const global_1 = require("../global");
|
|
7
7
|
const getNativeProperty = (name, object) => {
|
|
8
|
+
if ((0, global_1.isForbiddenPropertyName)(name)) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
8
11
|
const prop = object === null || object === void 0 ? void 0 : object[name];
|
|
9
12
|
if (prop === undefined) {
|
|
10
13
|
return undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../../src/expression/javascript/node/identifier.ts"],"names":[],"mappings":";;;AAAA,0CAA6D;AAE7D,qCAA0E;AAC1E,
|
|
1
|
+
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../../src/expression/javascript/node/identifier.ts"],"names":[],"mappings":";;;AAAA,0CAA6D;AAE7D,qCAA0E;AAC1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AAA;QACxC,CAAC;QACD,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,mBAAW,CAAC,uBAAe,CAAC,UAAU,CAAC,kBAAkB,EAAE,EAAE,IAAI,EAAE,cAAc,CAAC,IAAI,EAAE,CAAC,CAAC,CAAA;IACtH,CAAC;CACF","sourcesContent":["import { SystemError, SystemErrorKeys } from '../../../error'\nimport { ExpressionContext } from '../../context'\nimport { ExpressionNodeEvaluator, IdentifierExpression } from '../../node'\nimport { getGlobalObjectProperty, isForbiddenPropertyName } from '../global'\n\nconst getNativeProperty = (name: string, object: any) => {\n if (isForbiddenPropertyName(name)) {\n return undefined\n }\n const prop = object?.[name]\n if (prop === undefined) {\n return undefined\n }\n return typeof prop === 'function' ? prop.bind(object) : prop\n}\n\nexport class IdentifierEvaluator<C extends ExpressionContext> extends ExpressionNodeEvaluator<C, IdentifierExpression> {\n /**\n * Looks for the identifier among global or native properties, without throwing when it is not found\n * (creating errors captures stack traces, which is expensive when it happens for every evaluated identifier).\n *\n * @param expressionNode - The identifier expression node.\n * @returns The property value wrapped in an object, or null if the identifier has not been found.\n */\n protected findGlobalOrNativeProperty(expressionNode: IdentifierExpression): { value: any } | null {\n const { name } = expressionNode\n const { object: contextObject } = this.context\n\n const globalProp = getGlobalObjectProperty(name, contextObject)\n if (globalProp !== null) {\n return { value: globalProp }\n }\n const nativeProperty = getNativeProperty(name, contextObject)\n if (nativeProperty !== undefined) {\n return { value: nativeProperty }\n }\n return null\n }\n\n evaluate(expressionNode: IdentifierExpression): Promise<any> {\n const property = this.findGlobalOrNativeProperty(expressionNode)\n if (property) {\n return Promise.resolve(property.value)\n }\n return Promise.reject(new SystemError(SystemErrorKeys.expression.identifierNotFound, { name: expressionNode.name }))\n }\n}\n"]}
|
|
@@ -45,6 +45,9 @@ class NodeDefIdentifierEvaluator extends identifier_1.IdentifierEvaluator {
|
|
|
45
45
|
const { context } = this;
|
|
46
46
|
const { nodeDefContext, object: objectContext, itemsFilter, memberProperty } = context;
|
|
47
47
|
const { name: exprName } = expressionNode;
|
|
48
|
+
if ((0, global_1.isForbiddenPropertyName)(exprName)) {
|
|
49
|
+
this.throwIdentifierNotFound(expressionNode);
|
|
50
|
+
}
|
|
48
51
|
if (exprName === expression_1.ExpressionVariable.CONTEXT) {
|
|
49
52
|
if (nodeDefContext) {
|
|
50
53
|
this.addReferencedNodeDefUuid(nodeDefContext.uuid);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../src/nodeDefExpressionEvaluator/node/identifier.ts"],"names":[],"mappings":";;;AACA,4DAAsG;AACtG,qDAAiD;AACjD,uCAA4C;AAC5C,4EAAiF;AACjF,+DAA4E;AAE5E,iDAA2E;AAC3E,uCAA0D;AAC1D,iDAAqD;AACrD,2FAA0E;AAC1E,sDAAkD;AAElD;;;;;GAKG;AACH,MAAM,qBAAqB,GAAG,CAAC,MAE9B,EAAkD,EAAE;IACnD,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CAAA;IAC1B,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,oBAAoB,EAAE,GAAG,OAAO,CAAA;IAExD,IAAI,CAAC,oBAAoB;QAAE,OAAO,SAAS,CAAA;IAE3C,IAAI,mBAAQ,CAAC,WAAW,CAAC,oBAAoB,CAAC,EAAE,CAAC;QAC/C,OAAO,IAAA,2BAAgB,EAAC,EAAE,MAAM,EAAE,OAAO,EAAE,oBAAoB,EAAE,CAAC,CAAA;IACpE,CAAC;IACD,IAAI,oBAAoB,CAAC,OAAO,EAAE,CAAC;QACjC,OAAO,IAAA,2BAAgB,EAAC,EAAE,MAAM,EAAE,OAAO,EAAE,oBAAoB,EAAE,CAAC,CAAA;IACpE,CAAC;IACD,OAAO,oBAAoB,CAAA;AAC7B,CAAC,CAAA;AAED,gCAAwC,SAAQ,gCAA6C;IAC3F,QAAQ,CAAC,cAAoC;QAC3C,IAAI,CAAC;YACH,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,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AAG,CAAA;YACzC,MAAM,wBAAwB,GAAG,IAAA,6BAAkB,EAAC,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,CAAC,CAAA;YAC3G,KAAK,MAAM,QAAQ,IAAI,wBAAwB,EAAE,CAAC;gBAChD,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;oBAC9C,qBAAqB,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;oBACxC,MAAM,QAAQ,CAAA;gBAChB,CAAC;YACH,CAAC;YACD,qCAAqC;YACrC,KAAK,CAAC,YAAY,CAAC,wBAAwB,CAAC,MAAM,CAAC,mBAAQ,CAAC,cAAc,CAAC,CAAC,CAAA;YAE5E,oFAAoF;YACpF,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC7C,MAAM,sBAAsB,GAAG,IAAA,2BAAgB,EAAC,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,CAAC,CAAA;gBACtF,IAAI,sBAAsB,EAAE,CAAC;oBAC3B,KAAK,CAAC,OAAO,CAAC,sBAAsB,CAAC,CAAA;gBACvC,CAAC;gBACD,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC;oBACtD,qBAAqB,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAA;oBAChD,MAAM,gBAAgB,CAAA;gBACxB,CAAC;gBACD,YAAY,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAA;YACzC,CAAC;QACH,CAAC;IACH,CAAC;CACF","sourcesContent":["import { NodeDef, NodeDefProps, NodeDefType } from '../../nodeDef/nodeDef'\nimport { getNodeDefChildren, getNodeDefParent, getNodeDefSource } from '../../survey/surveys/nodeDefs'\nimport { NodeDefs } from '../../nodeDef/nodeDefs'\nimport { Objects, Queue } from '../../utils'\nimport { IdentifierEvaluator } from '../../expression/javascript/node/identifier'\nimport { getGlobalObjectProperty } from '../../expression/javascript/global'\nimport { NodeDefExpressionContext } from '../context'\nimport { ExpressionVariable, IdentifierExpression } from '../../expression'\nimport { SystemError, SystemErrorKeys } from '../../error'\nimport { ValidatorErrorKeys } from '../../validation'\nimport { NodeNativeProperties } from './nodeDefExpressionNativeProperties'\nimport { NodeValues } from '../../node/nodeValues'\n\n/**\n * Determines the actual context node def\n * - attribute def => parent entity def\n * - virtual entity def => source node def\n * - entity def => entity def itself\n */\nconst findActualContextNode = (params: {\n context: NodeDefExpressionContext\n}): NodeDef<NodeDefType, NodeDefProps> | undefined => {\n const { context } = params\n const { survey, object: nodeDefObjectContext } = context\n\n if (!nodeDefObjectContext) return undefined\n\n if (NodeDefs.isAttribute(nodeDefObjectContext)) {\n return getNodeDefParent({ survey, nodeDef: nodeDefObjectContext })\n }\n if (nodeDefObjectContext.virtual) {\n return getNodeDefSource({ survey, nodeDef: nodeDefObjectContext })\n }\n return nodeDefObjectContext\n}\n\nexport class NodeDefIdentifierEvaluator extends IdentifierEvaluator<NodeDefExpressionContext> {\n evaluate(expressionNode: IdentifierExpression): Promise<any> {\n try {\n return Promise.resolve(this.evaluateIdentifier(expressionNode))\n } catch (error) {\n return Promise.reject(error)\n }\n }\n\n private evaluateIdentifier(expressionNode: IdentifierExpression): any {\n const { context } = this\n const { nodeDefContext, object: objectContext, itemsFilter, memberProperty } = context\n const { name: exprName } = expressionNode\n\n if (exprName === ExpressionVariable.CONTEXT) {\n if (nodeDefContext) {\n this.addReferencedNodeDefUuid(nodeDefContext.uuid)\n }\n return nodeDefContext\n }\n\n // global objects (e.g. Math, String)\n const globalObjectProperty = getGlobalObjectProperty(exprName, objectContext)\n if (globalObjectProperty !== null) {\n return globalObjectProperty\n }\n\n if (itemsFilter) {\n const prop = objectContext?.props?.[exprName] ?? objectContext?.props?.extra?.[exprName]\n if (!Objects.isEmpty(prop)) {\n return prop\n }\n }\n\n // check if identifier is a composite attribute value prop\n if (NodeDefs.isAttribute(objectContext) && NodeValues.isValueProp({ nodeDef: objectContext, prop: exprName })) {\n return objectContext\n }\n\n if (memberProperty) {\n // property of a member expression (e.g. text_attr.length or text_attr.toUpperCase()): native properties first\n const nativeProperty = this.findNativeProperty(expressionNode)\n if (nativeProperty) return nativeProperty.value\n return this.getReferencedNodeDef(expressionNode)\n }\n // identifier not in a member expression: node defs first, so that node defs named like native properties\n // (e.g. \"name\", \"type\", \"trim\") are always found (and registered as dependencies)\n const referencedNodeDef = this.findReferencedNodeDef(expressionNode)\n if (referencedNodeDef) return referencedNodeDef\n\n const nativeProperty = this.findNativeProperty(expressionNode)\n if (nativeProperty) return nativeProperty.value\n\n return this.throwIdentifierNotFound(expressionNode)\n }\n\n /**\n * Finds the identifier among the native properties or functions of the context object\n * (e.g. String.length or String.toUpperCase()).\n */\n private findNativeProperty(expressionNode: IdentifierExpression): { value: any } | null {\n const { object: objectContext } = this.context\n const { name: exprName } = expressionNode\n\n const globalOrNativeProperty = this.findGlobalOrNativeProperty(expressionNode)\n if (globalOrNativeProperty) {\n return globalOrNativeProperty\n }\n if (NodeNativeProperties.hasNativeProperty({ nodeDefOrValue: objectContext, propName: exprName })) {\n return {\n value: NodeNativeProperties.evalNodeDefProperty({ nodeDefOrValue: objectContext, propName: exprName }),\n }\n }\n return null\n }\n\n private findReferencedNodeDef(expressionNode: IdentifierExpression): NodeDef<NodeDefType, NodeDefProps> | undefined {\n const { nodeDefCurrent, selfReferenceAllowed } = this.context\n const referencedNodeDef = this.findIdentifierAmongReachableNodeDefs(expressionNode)\n if (!referencedNodeDef) return undefined\n\n if (!selfReferenceAllowed && referencedNodeDef.uuid === nodeDefCurrent?.uuid) {\n throw new SystemError(ValidatorErrorKeys.expressions.cannotUseCurrentNode, { name: expressionNode.name })\n }\n this.addReferencedNodeDefUuid(referencedNodeDef.uuid)\n return referencedNodeDef\n }\n\n private getReferencedNodeDef(expressionNode: IdentifierExpression): NodeDef<NodeDefType, NodeDefProps> {\n return this.findReferencedNodeDef(expressionNode) ?? this.throwIdentifierNotFound(expressionNode)\n }\n\n private throwIdentifierNotFound(expressionNode: IdentifierExpression): never {\n throw new SystemError(SystemErrorKeys.expression.identifierNotFound, {\n name: expressionNode.name,\n contextObject: this.context.object?.props?.name,\n })\n }\n\n private addReferencedNodeDefUuid(uuid: string) {\n const { context } = this\n const { referencedNodeDefUuids = new Set() } = context\n context.referencedNodeDefUuids = referencedNodeDefUuids.add(uuid)\n }\n\n /**\n * Tries to find the specified identifier among the node defs that can be \"reached\" from the context node def.\n */\n protected findIdentifierAmongReachableNodeDefs(\n expressionNode: IdentifierExpression\n ): NodeDef<NodeDefType, NodeDefProps> | undefined {\n const { object: contextObject } = this.context\n\n if (!contextObject) return undefined\n\n // stop at the first match (same order as getReachableNodeDefs), without visiting all the reachable node defs\n for (const reachableNodeDef of this.iterateReachableNodeDefs()) {\n if (reachableNodeDef.props.name === expressionNode.name) {\n return reachableNodeDef\n }\n }\n return undefined\n }\n\n /**\n * Get reachable node defs, i.e. the children of the node definition's ancestors.\n * NOTE: The root node def is excluded, but it _should_ be an entity, so that is fine.\n */\n protected getReachableNodeDefs(): NodeDef<NodeDefType, NodeDefProps>[] {\n return Array.from(this.iterateReachableNodeDefs())\n }\n\n /**\n * Iterates over the reachable node defs (see getReachableNodeDefs), yielding every node def only once.\n */\n private *iterateReachableNodeDefs(): Generator<NodeDef<NodeDefType, NodeDefProps>> {\n const { context } = this\n const { survey, includeAnalysis } = context\n\n const reachableNodeDefUuids = new Set<string>()\n const visitedUuids = new Set<string>()\n\n const queue = new Queue<NodeDef<NodeDefType>>()\n\n const actualContextNode = findActualContextNode({ context })\n if (actualContextNode) {\n queue.enqueue(actualContextNode)\n reachableNodeDefUuids.add(actualContextNode.uuid)\n yield actualContextNode\n }\n\n while (!queue.isEmpty()) {\n const entityDefCurrent = queue.dequeue()!\n const entityDefCurrentChildren = getNodeDefChildren({ survey, nodeDef: entityDefCurrent, includeAnalysis })\n for (const childDef of entityDefCurrentChildren) {\n if (!reachableNodeDefUuids.has(childDef.uuid)) {\n reachableNodeDefUuids.add(childDef.uuid)\n yield childDef\n }\n }\n // visit nodes inside single entities\n queue.enqueueItems(entityDefCurrentChildren.filter(NodeDefs.isSingleEntity))\n\n // avoid visiting 2 times the same entity definition when traversing single entities\n if (!visitedUuids.has(entityDefCurrent.uuid)) {\n const entityDefCurrentParent = getNodeDefParent({ survey, nodeDef: entityDefCurrent })\n if (entityDefCurrentParent) {\n queue.enqueue(entityDefCurrentParent)\n }\n if (!reachableNodeDefUuids.has(entityDefCurrent.uuid)) {\n reachableNodeDefUuids.add(entityDefCurrent.uuid)\n yield entityDefCurrent\n }\n visitedUuids.add(entityDefCurrent.uuid)\n }\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../src/nodeDefExpressionEvaluator/node/identifier.ts"],"names":[],"mappings":";;;AACA,4DAAsG;AACtG,qDAAiD;AACjD,uCAA4C;AAC5C,4EAAiF;AACjF,+DAAqG;AAErG,iDAA2E;AAC3E,uCAA0D;AAC1D,iDAAqD;AACrD,2FAA0E;AAC1E,sDAAkD;AAElD;;;;;GAKG;AACH,MAAM,qBAAqB,GAAG,CAAC,MAE9B,EAAkD,EAAE;IACnD,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CAAA;IAC1B,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,oBAAoB,EAAE,GAAG,OAAO,CAAA;IAExD,IAAI,CAAC,oBAAoB;QAAE,OAAO,SAAS,CAAA;IAE3C,IAAI,mBAAQ,CAAC,WAAW,CAAC,oBAAoB,CAAC,EAAE,CAAC;QAC/C,OAAO,IAAA,2BAAgB,EAAC,EAAE,MAAM,EAAE,OAAO,EAAE,oBAAoB,EAAE,CAAC,CAAA;IACpE,CAAC;IACD,IAAI,oBAAoB,CAAC,OAAO,EAAE,CAAC;QACjC,OAAO,IAA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,CAAC;oBACtD,qBAAqB,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAA;oBAChD,MAAM,gBAAgB,CAAA;gBACxB,CAAC;gBACD,YAAY,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAA;YACzC,CAAC;QACH,CAAC;IACH,CAAC;CACF","sourcesContent":["import { NodeDef, NodeDefProps, NodeDefType } from '../../nodeDef/nodeDef'\nimport { getNodeDefChildren, getNodeDefParent, getNodeDefSource } from '../../survey/surveys/nodeDefs'\nimport { NodeDefs } from '../../nodeDef/nodeDefs'\nimport { Objects, Queue } from '../../utils'\nimport { IdentifierEvaluator } from '../../expression/javascript/node/identifier'\nimport { getGlobalObjectProperty, isForbiddenPropertyName } from '../../expression/javascript/global'\nimport { NodeDefExpressionContext } from '../context'\nimport { ExpressionVariable, IdentifierExpression } from '../../expression'\nimport { SystemError, SystemErrorKeys } from '../../error'\nimport { ValidatorErrorKeys } from '../../validation'\nimport { NodeNativeProperties } from './nodeDefExpressionNativeProperties'\nimport { NodeValues } from '../../node/nodeValues'\n\n/**\n * Determines the actual context node def\n * - attribute def => parent entity def\n * - virtual entity def => source node def\n * - entity def => entity def itself\n */\nconst findActualContextNode = (params: {\n context: NodeDefExpressionContext\n}): NodeDef<NodeDefType, NodeDefProps> | undefined => {\n const { context } = params\n const { survey, object: nodeDefObjectContext } = context\n\n if (!nodeDefObjectContext) return undefined\n\n if (NodeDefs.isAttribute(nodeDefObjectContext)) {\n return getNodeDefParent({ survey, nodeDef: nodeDefObjectContext })\n }\n if (nodeDefObjectContext.virtual) {\n return getNodeDefSource({ survey, nodeDef: nodeDefObjectContext })\n }\n return nodeDefObjectContext\n}\n\nexport class NodeDefIdentifierEvaluator extends IdentifierEvaluator<NodeDefExpressionContext> {\n evaluate(expressionNode: IdentifierExpression): Promise<any> {\n try {\n return Promise.resolve(this.evaluateIdentifier(expressionNode))\n } catch (error) {\n return Promise.reject(error)\n }\n }\n\n private evaluateIdentifier(expressionNode: IdentifierExpression): any {\n const { context } = this\n const { nodeDefContext, object: objectContext, itemsFilter, memberProperty } = context\n const { name: exprName } = expressionNode\n\n if (isForbiddenPropertyName(exprName)) {\n this.throwIdentifierNotFound(expressionNode)\n }\n\n if (exprName === ExpressionVariable.CONTEXT) {\n if (nodeDefContext) {\n this.addReferencedNodeDefUuid(nodeDefContext.uuid)\n }\n return nodeDefContext\n }\n\n // global objects (e.g. Math, String)\n const globalObjectProperty = getGlobalObjectProperty(exprName, objectContext)\n if (globalObjectProperty !== null) {\n return globalObjectProperty\n }\n\n if (itemsFilter) {\n const prop = objectContext?.props?.[exprName] ?? objectContext?.props?.extra?.[exprName]\n if (!Objects.isEmpty(prop)) {\n return prop\n }\n }\n\n // check if identifier is a composite attribute value prop\n if (NodeDefs.isAttribute(objectContext) && NodeValues.isValueProp({ nodeDef: objectContext, prop: exprName })) {\n return objectContext\n }\n\n if (memberProperty) {\n // property of a member expression (e.g. text_attr.length or text_attr.toUpperCase()): native properties first\n const nativeProperty = this.findNativeProperty(expressionNode)\n if (nativeProperty) return nativeProperty.value\n return this.getReferencedNodeDef(expressionNode)\n }\n // identifier not in a member expression: node defs first, so that node defs named like native properties\n // (e.g. \"name\", \"type\", \"trim\") are always found (and registered as dependencies)\n const referencedNodeDef = this.findReferencedNodeDef(expressionNode)\n if (referencedNodeDef) return referencedNodeDef\n\n const nativeProperty = this.findNativeProperty(expressionNode)\n if (nativeProperty) return nativeProperty.value\n\n return this.throwIdentifierNotFound(expressionNode)\n }\n\n /**\n * Finds the identifier among the native properties or functions of the context object\n * (e.g. String.length or String.toUpperCase()).\n */\n private findNativeProperty(expressionNode: IdentifierExpression): { value: any } | null {\n const { object: objectContext } = this.context\n const { name: exprName } = expressionNode\n\n const globalOrNativeProperty = this.findGlobalOrNativeProperty(expressionNode)\n if (globalOrNativeProperty) {\n return globalOrNativeProperty\n }\n if (NodeNativeProperties.hasNativeProperty({ nodeDefOrValue: objectContext, propName: exprName })) {\n return {\n value: NodeNativeProperties.evalNodeDefProperty({ nodeDefOrValue: objectContext, propName: exprName }),\n }\n }\n return null\n }\n\n private findReferencedNodeDef(expressionNode: IdentifierExpression): NodeDef<NodeDefType, NodeDefProps> | undefined {\n const { nodeDefCurrent, selfReferenceAllowed } = this.context\n const referencedNodeDef = this.findIdentifierAmongReachableNodeDefs(expressionNode)\n if (!referencedNodeDef) return undefined\n\n if (!selfReferenceAllowed && referencedNodeDef.uuid === nodeDefCurrent?.uuid) {\n throw new SystemError(ValidatorErrorKeys.expressions.cannotUseCurrentNode, { name: expressionNode.name })\n }\n this.addReferencedNodeDefUuid(referencedNodeDef.uuid)\n return referencedNodeDef\n }\n\n private getReferencedNodeDef(expressionNode: IdentifierExpression): NodeDef<NodeDefType, NodeDefProps> {\n return this.findReferencedNodeDef(expressionNode) ?? this.throwIdentifierNotFound(expressionNode)\n }\n\n private throwIdentifierNotFound(expressionNode: IdentifierExpression): never {\n throw new SystemError(SystemErrorKeys.expression.identifierNotFound, {\n name: expressionNode.name,\n contextObject: this.context.object?.props?.name,\n })\n }\n\n private addReferencedNodeDefUuid(uuid: string) {\n const { context } = this\n const { referencedNodeDefUuids = new Set() } = context\n context.referencedNodeDefUuids = referencedNodeDefUuids.add(uuid)\n }\n\n /**\n * Tries to find the specified identifier among the node defs that can be \"reached\" from the context node def.\n */\n protected findIdentifierAmongReachableNodeDefs(\n expressionNode: IdentifierExpression\n ): NodeDef<NodeDefType, NodeDefProps> | undefined {\n const { object: contextObject } = this.context\n\n if (!contextObject) return undefined\n\n // stop at the first match (same order as getReachableNodeDefs), without visiting all the reachable node defs\n for (const reachableNodeDef of this.iterateReachableNodeDefs()) {\n if (reachableNodeDef.props.name === expressionNode.name) {\n return reachableNodeDef\n }\n }\n return undefined\n }\n\n /**\n * Get reachable node defs, i.e. the children of the node definition's ancestors.\n * NOTE: The root node def is excluded, but it _should_ be an entity, so that is fine.\n */\n protected getReachableNodeDefs(): NodeDef<NodeDefType, NodeDefProps>[] {\n return Array.from(this.iterateReachableNodeDefs())\n }\n\n /**\n * Iterates over the reachable node defs (see getReachableNodeDefs), yielding every node def only once.\n */\n private *iterateReachableNodeDefs(): Generator<NodeDef<NodeDefType, NodeDefProps>> {\n const { context } = this\n const { survey, includeAnalysis } = context\n\n const reachableNodeDefUuids = new Set<string>()\n const visitedUuids = new Set<string>()\n\n const queue = new Queue<NodeDef<NodeDefType>>()\n\n const actualContextNode = findActualContextNode({ context })\n if (actualContextNode) {\n queue.enqueue(actualContextNode)\n reachableNodeDefUuids.add(actualContextNode.uuid)\n yield actualContextNode\n }\n\n while (!queue.isEmpty()) {\n const entityDefCurrent = queue.dequeue()!\n const entityDefCurrentChildren = getNodeDefChildren({ survey, nodeDef: entityDefCurrent, includeAnalysis })\n for (const childDef of entityDefCurrentChildren) {\n if (!reachableNodeDefUuids.has(childDef.uuid)) {\n reachableNodeDefUuids.add(childDef.uuid)\n yield childDef\n }\n }\n // visit nodes inside single entities\n queue.enqueueItems(entityDefCurrentChildren.filter(NodeDefs.isSingleEntity))\n\n // avoid visiting 2 times the same entity definition when traversing single entities\n if (!visitedUuids.has(entityDefCurrent.uuid)) {\n const entityDefCurrentParent = getNodeDefParent({ survey, nodeDef: entityDefCurrent })\n if (entityDefCurrentParent) {\n queue.enqueue(entityDefCurrentParent)\n }\n if (!reachableNodeDefUuids.has(entityDefCurrent.uuid)) {\n reachableNodeDefUuids.add(entityDefCurrent.uuid)\n yield entityDefCurrent\n }\n visitedUuids.add(entityDefCurrent.uuid)\n }\n }\n }\n}\n"]}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RecordIdentifierEvaluator = void 0;
|
|
4
4
|
const expression_1 = require("../../../../expression");
|
|
5
|
+
const global_1 = require("../../../../expression/javascript/global");
|
|
5
6
|
const identifier_1 = require("../../../../expression/javascript/node/identifier");
|
|
6
7
|
const nodeDef_1 = require("../../../../nodeDef");
|
|
7
8
|
const records_1 = require("../../../records");
|
|
@@ -80,6 +81,9 @@ class RecordIdentifierEvaluator extends identifier_1.IdentifierEvaluator {
|
|
|
80
81
|
if (propName === expression_1.ExpressionVariable.CONTEXT) {
|
|
81
82
|
return this.context.nodeContext;
|
|
82
83
|
}
|
|
84
|
+
if ((0, global_1.isForbiddenPropertyName)(propName)) {
|
|
85
|
+
throw new error_1.SystemError(error_1.SystemErrorKeys.expression.identifierNotFound, { name: propName });
|
|
86
|
+
}
|
|
83
87
|
// try to find identifier among global or native properties
|
|
84
88
|
// (not-found is signaled without throwing; the try/catch only handles the rare case of an unknown member of a global object)
|
|
85
89
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../../../src/record/recordExpressionEvaluator/node/identifier/identifier.ts"],"names":[],"mappings":";;;AAAA,uDAAiF;AACjF,kFAAuF;AAEvF,iDAAoE;AACpE,8CAA0C;AAC1C,4DAAwD;AACxD,+CAAoD;AAEpD,+CAA2C;AAC3C,6CAAgE;AAChE,iEAA6D;AAG7D,MAAM,gBAAgB,GAAG,CAAC,MAMzB,EAAS,EAAE;IACV,MAAM,EAAE,MAAM,EAAE,iBAAiB,EAAE,eAAe,EAAE,QAAQ,EAAE,cAAc,EAAE,GAAG,MAAM,CAAA;IACvF,MAAM,MAAM,GAAG,CAAC,iBAAiB,CAAC,KAAK,CAAC,QAAQ,CAAA;IAChD,IAAI,MAAM,EAAE,CAAC;QACX,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACjC,oEAAoE;YACpE,sGAAsG;YACtG,OAAO,EAAE,CAAA;QACX,CAAC;QACD,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC;YAC5B,MAAM,IAAI,mBAAW,CAAC,uBAAe,CAAC,UAAU,CAAC,4BAA4B,EAAE,EAAE,WAAW,EAAE,QAAQ,EAAE,CAAC,CAAA;IAC7G,CAAC;IACD,IAAI,iBAAiB,CAAC,IAAI,KAAK,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N,UAAU,EAAE,WAAW;oBACvB,cAAc;oBACd,QAAQ;iBACT,CAAC,CAAA;gBACF,OAAO,KAAK,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAA;YAChF,CAAC,CAAC,CAAA;QACJ,CAAC;QACD,OAAO,wBAAwB,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,QAAQ,EAAE,CAAC,CAAA;IAC1G,CAAC;CACF","sourcesContent":["import { ExpressionVariable, IdentifierExpression } from '../../../../expression'\nimport { IdentifierEvaluator } from '../../../../expression/javascript/node/identifier'\nimport { ArenaRecordNode } from '../../../../node'\nimport { NodeDef, NodeDefs, NodeDefType } from '../../../../nodeDef'\nimport { Records } from '../../../records'\nimport { NodeValues } from '../../../../node/nodeValues'\nimport { Survey, Surveys } from '../../../../survey'\nimport { RecordExpressionContext } from '../../context'\nimport { NodesFinder } from './nodesFinder'\nimport { SystemError, SystemErrorKeys } from '../../../../error'\nimport { NodeValueExtractor } from '../../nodeValueExtractor'\nimport { ArenaRecord } from '../../../record'\n\nconst getNodesOrValues = (params: {\n survey: Survey\n referencedNodes: ArenaRecordNode[]\n nodeDefReferenced: NodeDef<any>\n propName: string\n evaluateToNode?: boolean\n}): any[] => {\n const { survey, nodeDefReferenced, referencedNodes, propName, evaluateToNode } = params\n const single = !nodeDefReferenced.props.multiple\n if (single) {\n if (referencedNodes.length === 0) {\n // do not throw error if node is missing, consider its value as null\n // throw new SystemError(SystemErrorKeys.expression.nodeNotFoundForNodeDef, { nodeDefName: propName })\n return []\n }\n if (referencedNodes.length > 1)\n throw new SystemError(SystemErrorKeys.expression.multipleNodesFoundForNodeDef, { nodeDefName: propName })\n }\n if (nodeDefReferenced.type === NodeDefType.entity || evaluateToNode) {\n // return nodes\n return referencedNodes\n }\n // return node values\n return referencedNodes.map((referencedNode) =>\n NodeValueExtractor.getNodeValue({ survey, node: referencedNode, nodeDef: nodeDefReferenced })\n )\n}\n\nconst evaluateIdentifierOnNode = (params: {\n survey: Survey\n record: ArenaRecord\n nodeObject: ArenaRecordNode\n evaluateToNode?: boolean\n propName: string\n}) => {\n const { survey, record, nodeObject, evaluateToNode = false, propName } = params\n const { nodeDefUuid: nodeDefObjectUuid, value } = nodeObject\n if (!nodeDefObjectUuid) {\n throw new SystemError(SystemErrorKeys.expression.identifierNotFound, { name: propName })\n }\n\n const nodeDefObject = Surveys.getNodeDefByUuid({ survey, uuid: nodeDefObjectUuid })\n\n // node value prop (native)\n const valueProp = value?.[propName]\n if (valueProp !== undefined) {\n return valueProp\n }\n if (NodeDefs.isAttribute(nodeDefObject)) {\n // node value prop (Arena specific value property)\n if (NodeValues.isValueProp({ nodeDef: nodeDefObject, prop: propName })) {\n return NodeValues.getNodeValueProp({ nodeDef: nodeDefObject, node: nodeObject, prop: propName })\n } else {\n throw new SystemError(SystemErrorKeys.expression.invalidAttributeValuePropertyName, {\n attributeName: NodeDefs.getName(nodeDefObject),\n propName,\n })\n }\n }\n\n const nodeDefReferenced = Surveys.findNodeDefByName({ survey, name: propName })\n\n if (!nodeDefReferenced) {\n throw new SystemError(SystemErrorKeys.expression.identifierNotFound, { name: propName })\n }\n\n if (nodeObject.nodeDefUuid === nodeDefReferenced.uuid) {\n // the referenced node is the current node itself\n return nodeObject\n }\n if (Surveys.isNodeDefAncestor({ nodeDefAncestor: nodeDefReferenced, nodeDefDescendant: nodeDefObject })) {\n return Records.getAncestor(nodeObject, nodeDefReferenced.uuid)(record)\n }\n // the referenced nodes can be siblings of the current node\n const referencedNodes = NodesFinder.findDescendants({ survey, record, nodeContext: nodeObject, nodeDefReferenced })\n const nodesOrValues = getNodesOrValues({ survey, referencedNodes, nodeDefReferenced, propName, evaluateToNode })\n return nodeDefReferenced.props.multiple ? nodesOrValues : nodesOrValues[0]\n}\n\nexport class RecordIdentifierEvaluator extends IdentifierEvaluator<RecordExpressionContext> {\n evaluate(expressionNode: IdentifierExpression): Promise<any> {\n return Promise.resolve(this.evaluateValue(expressionNode))\n }\n\n private evaluateValue(expressionNode: IdentifierExpression): any {\n const { name: propName } = expressionNode\n\n if (propName === ExpressionVariable.CONTEXT) {\n return this.context.nodeContext\n }\n\n // try to find identifier among global or native properties\n // (not-found is signaled without throwing; the try/catch only handles the rare case of an unknown member of a global object)\n try {\n const property = this.findGlobalOrNativeProperty(expressionNode)\n if (property) {\n return property.value\n }\n } catch {\n // ignore it\n }\n\n // identifier not found among global or native properties\n // identifier should be a node or a node value property\n const { survey, record, evaluateToNode, object: contextObject, item } = this.context\n\n if (item && item === contextObject) {\n // evaluating category or taxon item prop or extra prop\n return item.props?.[propName] ?? item.props?.extra?.[propName]\n }\n\n if (Array.isArray(contextObject)) {\n return contextObject.flatMap((contextNode) => {\n const evaluationResult = evaluateIdentifierOnNode({\n survey,\n record,\n nodeObject: contextNode,\n evaluateToNode,\n propName,\n })\n return Array.isArray(evaluationResult) ? evaluationResult : [evaluationResult]\n })\n }\n return evaluateIdentifierOnNode({ survey, record, nodeObject: contextObject, evaluateToNode, propName })\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../../../src/record/recordExpressionEvaluator/node/identifier/identifier.ts"],"names":[],"mappings":";;;AAAA,uDAAiF;AACjF,qEAAkF;AAClF,kFAAuF;AAEvF,iDAAoE;AACpE,8CAA0C;AAC1C,4DAAwD;AACxD,+CAAoD;AAEpD,+CAA2C;AAC3C,6CAAgE;AAChE,iEAA6D;AAG7D,MAAM,gBAAgB,GAAG,CAAC,MAMzB,EAAS,EAAE;IACV,MAAM,EAAE,MAAM,EAAE,iBAAiB,EAAE,eAAe,EAAE,QAAQ,EAAE,cAAc,EAAE,GAAG,MAAM,CAAA;IACvF,MAAM,MAAM,GAAG,CAAC,iBAAiB,CAAC,KAAK,CAAC,QAAQ,CAAA;IAChD,IAAI,MAAM,EAAE,CAAC;QACX,IAAI,e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AAM,EAAE,cAAc,EAAE,MAAM,EAAE,aAAa,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC,OAAO,CAAA;QAEpF,IAAI,IAAI,IAAI,IAAI,KAAK,aAAa,EAAE,CAAC;YACnC,uDAAuD;YACvD,aAAO,MAAA,IAAI,CAAC,KAAK,0CAAG,QAAQ,CAAC,mCAAI,MAAA,MAAA,IAAI,CAAC,KAAK,0CAAE,KAAK,0CAAG,QAAQ,CAAC,CAAA;QAChE,CAAC;QAED,IAAI,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC;YACjC,OAAO,aAAa,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;gBAC3C,MAAM,gBAAgB,GAAG,wBAAwB,CAAC;oBAChD,MAAM;oBACN,MAAM;oBACN,UAAU,EAAE,WAAW;oBACvB,cAAc;oBACd,QAAQ;iBACT,CAAC,CAAA;gBACF,OAAO,KAAK,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAA;YAChF,CAAC,CAAC,CAAA;QACJ,CAAC;QACD,OAAO,wBAAwB,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,QAAQ,EAAE,CAAC,CAAA;IAC1G,CAAC;CACF","sourcesContent":["import { ExpressionVariable, IdentifierExpression } from '../../../../expression'\nimport { isForbiddenPropertyName } from '../../../../expression/javascript/global'\nimport { IdentifierEvaluator } from '../../../../expression/javascript/node/identifier'\nimport { ArenaRecordNode } from '../../../../node'\nimport { NodeDef, NodeDefs, NodeDefType } from '../../../../nodeDef'\nimport { Records } from '../../../records'\nimport { NodeValues } from '../../../../node/nodeValues'\nimport { Survey, Surveys } from '../../../../survey'\nimport { RecordExpressionContext } from '../../context'\nimport { NodesFinder } from './nodesFinder'\nimport { SystemError, SystemErrorKeys } from '../../../../error'\nimport { NodeValueExtractor } from '../../nodeValueExtractor'\nimport { ArenaRecord } from '../../../record'\n\nconst getNodesOrValues = (params: {\n survey: Survey\n referencedNodes: ArenaRecordNode[]\n nodeDefReferenced: NodeDef<any>\n propName: string\n evaluateToNode?: boolean\n}): any[] => {\n const { survey, nodeDefReferenced, referencedNodes, propName, evaluateToNode } = params\n const single = !nodeDefReferenced.props.multiple\n if (single) {\n if (referencedNodes.length === 0) {\n // do not throw error if node is missing, consider its value as null\n // throw new SystemError(SystemErrorKeys.expression.nodeNotFoundForNodeDef, { nodeDefName: propName })\n return []\n }\n if (referencedNodes.length > 1)\n throw new SystemError(SystemErrorKeys.expression.multipleNodesFoundForNodeDef, { nodeDefName: propName })\n }\n if (nodeDefReferenced.type === NodeDefType.entity || evaluateToNode) {\n // return nodes\n return referencedNodes\n }\n // return node values\n return referencedNodes.map((referencedNode) =>\n NodeValueExtractor.getNodeValue({ survey, node: referencedNode, nodeDef: nodeDefReferenced })\n )\n}\n\nconst evaluateIdentifierOnNode = (params: {\n survey: Survey\n record: ArenaRecord\n nodeObject: ArenaRecordNode\n evaluateToNode?: boolean\n propName: string\n}) => {\n const { survey, record, nodeObject, evaluateToNode = false, propName } = params\n const { nodeDefUuid: nodeDefObjectUuid, value } = nodeObject\n if (!nodeDefObjectUuid) {\n throw new SystemError(SystemErrorKeys.expression.identifierNotFound, { name: propName })\n }\n\n const nodeDefObject = Surveys.getNodeDefByUuid({ survey, uuid: nodeDefObjectUuid })\n\n // node value prop (native)\n const valueProp = value?.[propName]\n if (valueProp !== undefined) {\n return valueProp\n }\n if (NodeDefs.isAttribute(nodeDefObject)) {\n // node value prop (Arena specific value property)\n if (NodeValues.isValueProp({ nodeDef: nodeDefObject, prop: propName })) {\n return NodeValues.getNodeValueProp({ nodeDef: nodeDefObject, node: nodeObject, prop: propName })\n } else {\n throw new SystemError(SystemErrorKeys.expression.invalidAttributeValuePropertyName, {\n attributeName: NodeDefs.getName(nodeDefObject),\n propName,\n })\n }\n }\n\n const nodeDefReferenced = Surveys.findNodeDefByName({ survey, name: propName })\n\n if (!nodeDefReferenced) {\n throw new SystemError(SystemErrorKeys.expression.identifierNotFound, { name: propName })\n }\n\n if (nodeObject.nodeDefUuid === nodeDefReferenced.uuid) {\n // the referenced node is the current node itself\n return nodeObject\n }\n if (Surveys.isNodeDefAncestor({ nodeDefAncestor: nodeDefReferenced, nodeDefDescendant: nodeDefObject })) {\n return Records.getAncestor(nodeObject, nodeDefReferenced.uuid)(record)\n }\n // the referenced nodes can be siblings of the current node\n const referencedNodes = NodesFinder.findDescendants({ survey, record, nodeContext: nodeObject, nodeDefReferenced })\n const nodesOrValues = getNodesOrValues({ survey, referencedNodes, nodeDefReferenced, propName, evaluateToNode })\n return nodeDefReferenced.props.multiple ? nodesOrValues : nodesOrValues[0]\n}\n\nexport class RecordIdentifierEvaluator extends IdentifierEvaluator<RecordExpressionContext> {\n evaluate(expressionNode: IdentifierExpression): Promise<any> {\n return Promise.resolve(this.evaluateValue(expressionNode))\n }\n\n private evaluateValue(expressionNode: IdentifierExpression): any {\n const { name: propName } = expressionNode\n\n if (propName === ExpressionVariable.CONTEXT) {\n return this.context.nodeContext\n }\n if (isForbiddenPropertyName(propName)) {\n throw new SystemError(SystemErrorKeys.expression.identifierNotFound, { name: propName })\n }\n\n // try to find identifier among global or native properties\n // (not-found is signaled without throwing; the try/catch only handles the rare case of an unknown member of a global object)\n try {\n const property = this.findGlobalOrNativeProperty(expressionNode)\n if (property) {\n return property.value\n }\n } catch {\n // ignore it\n }\n\n // identifier not found among global or native properties\n // identifier should be a node or a node value property\n const { survey, record, evaluateToNode, object: contextObject, item } = this.context\n\n if (item && item === contextObject) {\n // evaluating category or taxon item prop or extra prop\n return item.props?.[propName] ?? item.props?.extra?.[propName]\n }\n\n if (Array.isArray(contextObject)) {\n return contextObject.flatMap((contextNode) => {\n const evaluationResult = evaluateIdentifierOnNode({\n survey,\n record,\n nodeObject: contextNode,\n evaluateToNode,\n propName,\n })\n return Array.isArray(evaluationResult) ? evaluationResult : [evaluationResult]\n })\n }\n return evaluateIdentifierOnNode({ survey, record, nodeObject: contextObject, evaluateToNode, propName })\n }\n}\n"]}
|