-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A rule that verifies that global elements are properly used. Currently recognized global elements are: * `bpmn:Error` * `bpmn:Escalation` * `bpmn:Signal` * `bpmn:Message` For each of these elements proper usage implies: * element must have a name * element is used (referenced) from event definitions * there exists only a single element per type with a given name Related to camunda/camunda-modeler#4339
- Loading branch information
1 parent
74bceb4
commit d1e37ff
Showing
22 changed files
with
1,100 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
const { | ||
isAny, is | ||
} = require('bpmnlint-utils'); | ||
|
||
|
||
/** | ||
* A rule that verifies that global elements are properly used. | ||
* | ||
* Currently recognized global elements are: | ||
* | ||
* * `bpmn:Error` | ||
* * `bpmn:Escalation` | ||
* * `bpmn:Signal` | ||
* * `bpmn:Message` | ||
* | ||
* For each of these elements proper usage implies: | ||
* | ||
* * element must have a name | ||
* * element is used (referenced) from event definitions | ||
* * there exists only a single element per type with a given name | ||
*/ | ||
module.exports = function() { | ||
|
||
function check(node, reporter) { | ||
|
||
if (!is(node, 'bpmn:Definitions')) { | ||
return false; | ||
} | ||
|
||
const events = getEvents(node); | ||
const eventDefinitions = getEventDefinitions(node); | ||
|
||
events.forEach(event => { | ||
if (!hasName(event)) { | ||
reporter.report(event.id, 'Element is missing name'); | ||
} | ||
|
||
if (!isReferenced(event, eventDefinitions)) { | ||
reporter.report(event.id, 'Element is unused'); | ||
} | ||
|
||
if (!isUnique(event, events)) { | ||
reporter.report(event.id, 'Element name is not unique'); | ||
} | ||
}); | ||
|
||
} | ||
|
||
return { | ||
check | ||
}; | ||
|
||
// helpers ///////////////////////////// | ||
|
||
function getEvents(definition) { | ||
return definition.rootElements.filter(node => isAny(node, [ 'bpmn:Error', 'bpmn:Escalation', 'bpmn:Message', 'bpmn:Signal' ])); | ||
} | ||
|
||
function getEventDefinitions(definition) { | ||
const eventDefinitions = []; | ||
|
||
function traverse(element) { | ||
if (element.rootElements) { | ||
element.rootElements.forEach(traverse); | ||
} | ||
|
||
if (element.flowElements) { | ||
element.flowElements.forEach(traverse); | ||
} | ||
|
||
if (element.eventDefinitions) { | ||
element.eventDefinitions.forEach(eventDefinition => eventDefinitions.push(eventDefinition)); | ||
} | ||
} | ||
|
||
traverse(definition); | ||
return eventDefinitions; | ||
} | ||
|
||
function hasName(event) { | ||
return ( | ||
event.name?.trim() !== '' | ||
); | ||
} | ||
|
||
function isReferenced(event, eventDefinitions) { | ||
if (is(event, 'bpmn:Error')) { | ||
return ( | ||
eventDefinitions.some(node => is(node, 'bpmn:ErrorEventDefinition') && event.id === node.errorRef?.id) | ||
); | ||
} | ||
|
||
if (is(event, 'bpmn:Escalation')) { | ||
return ( | ||
eventDefinitions.some(node => is(node, 'bpmn:EscalationEventDefinition') && event.id === node.escalationRef?.id) | ||
); | ||
} | ||
|
||
if (is(event, 'bpmn:Message')) { | ||
return ( | ||
eventDefinitions.some(node => is(node, 'bpmn:MessageEventDefinition') && event.id === node.messageRef?.id) | ||
); | ||
} | ||
|
||
if (is(event, 'bpmn:Signal')) { | ||
return ( | ||
eventDefinitions.some(node => is(node, 'bpmn:SignalEventDefinition') && event.id === node.signalRef?.id) | ||
); | ||
} | ||
} | ||
|
||
function isUnique(event, events) { | ||
return ( | ||
events.filter(node => is(node, event.$type) && event.name === node.name).length === 1 | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.