Skip to content

Commit

Permalink
chore: refactor task headers rule
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed Aug 16, 2022
1 parent d6b923e commit 3795971
Show file tree
Hide file tree
Showing 5 changed files with 359 additions and 146 deletions.
42 changes: 4 additions & 38 deletions rules/duplicate-task-headers.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
const { has } = require('min-dash');

const {
is,
} = require('bpmnlint-utils');

const { getPath } = require('@bpmn-io/moddle-utils');
const { is } = require('bpmnlint-utils');

const {
findExtensionElement,
getMessageEventDefinition
getMessageEventDefinition,
hasDuplicatedPropertyValues
} = require('./utils/element');

const { ERROR_TYPES } = require('./utils/element');

const { reportErrors } = require('./utils/reporter');

module.exports = function() {
Expand All @@ -21,40 +14,13 @@ module.exports = function() {
return;
}

let errors = [];

const taskHeaders = findExtensionElement(node, 'zeebe:TaskHeaders');

if (!taskHeaders) {
return;
}

const headers = taskHeaders.get('values');

const keys = {};

headers.forEach(header => {
const { key } = header;

if (has(keys, key)) {

// todo(pinussilvestrus): consider moving this to
// element-helper once we see a recurring use case
errors.push({
message: `Element of type <zeebe:TaskHeaders> contains duplicate <zeebe:Header> elements for key <${key}>`,
path: getPath(header, node),
error: {
type: ERROR_TYPES.EXTENSION_ELEMENT_DUPLICATE,
node: header,
parentNode: node,
property: 'key',
duplicateValue: key
}
});
}

keys[key] = true;
});
const errors = hasDuplicatedPropertyValues(taskHeaders, 'values', 'key', node);

if (errors && errors.length) {
reportErrors(node, reporter, errors);
Expand Down
52 changes: 44 additions & 8 deletions rules/utils/element.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const {
find,
isArray,
isDefined,
isNil,
Expand All @@ -17,24 +16,22 @@ const { ERROR_TYPES } = require('./error-types');

module.exports.ERROR_TYPES = ERROR_TYPES;

module.exports.getEventDefinition = function(node) {
function getEventDefinition(node) {
const eventDefinitions = node.get('eventDefinitions');

if (eventDefinitions) {
return eventDefinitions[ 0 ];
}
};
}

module.exports.getEventDefinition = getEventDefinition;

module.exports.getMessageEventDefinition = function(node) {
if (is(node, 'bpmn:ReceiveTask')) {
return node;
}

const eventDefinitions = node.get('eventDefinitions') || [];

return find(eventDefinitions, function(definition) {
return is(definition, 'bpmn:MessageEventDefinition');
});
return getEventDefinition(node);
};

function findExtensionElements(node, types) {
Expand Down Expand Up @@ -88,6 +85,45 @@ function formatTypes(types, exclusive = false) {

module.exports.formatTypes = formatTypes;

module.exports.hasDuplicatedPropertyValues = function(node, propertiesName, propertyName, parentNode = null) {
const properties = node.get(propertiesName);

const propertyValues = properties.map(property => property.get(propertyName));

const duplicates = propertyValues.reduce((duplicates, propertyValue, index) => {
if (propertyValues.indexOf(propertyValue) !== index && !duplicates.includes(propertyValue)) {
return [
...duplicates,
propertyValue
];
}

return duplicates;
}, []);

if (duplicates.length) {
return duplicates.map(duplicate => {
const duplicateProperties = properties.filter(property => property.get(propertyName) === duplicate);

return {
message: `Properties of type <${ duplicateProperties[ 0 ].$type }> have property <${ propertyName }> with duplicate value of <${ duplicate }>`,
path: null,
error: {
type: ERROR_TYPES.PROPERTY_VALUE_DUPLICATED,
node,
parentNode: parentNode == node ? null : parentNode,
duplicatedProperty: propertyName,
duplicatedPropertyValue: duplicate,
properties: duplicateProperties,
propertiesName
}
};
});
}

return [];
};

module.exports.hasProperties = function(node, properties, parentNode = null) {
return Object.entries(properties).reduce((results, property) => {
const [ propertyName, propertyChecks ] = property;
Expand Down
4 changes: 2 additions & 2 deletions rules/utils/error-types.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports.ERROR_TYPES = Object.freeze({
ELEMENT_TYPE_NOT_ALLOWED: 'elementTypeNotAllowed',
EXTENSION_ELEMENT_DUPLICATE: 'extensionElementDuplicate',
EXTENSION_ELEMENT_NOT_ALLOWED: 'extensionElementNotAllowed',
EXTENSION_ELEMENT_REQUIRED: 'extensionElementRequired',
PROPERTY_DEPENDEND_REQUIRED: 'propertyDependendRequired',
PROPERTY_NOT_ALLOWED: 'propertyNotAllowed',
PROPERTY_REQUIRED: 'propertyRequired',
PROPERTY_TYPE_NOT_ALLOWED: 'propertyTypeNotAllowed'
PROPERTY_TYPE_NOT_ALLOWED: 'propertyTypeNotAllowed',
PROPERTY_VALUE_DUPLICATED: 'propertyValueDuplicated'
});
Loading

0 comments on commit 3795971

Please sign in to comment.