Skip to content

Commit

Permalink
feat: allow path property in Reporter#report
Browse files Browse the repository at this point in the history
Closes #69
  • Loading branch information
philippfromme committed Mar 7, 2022
1 parent c3cf69e commit 4355283
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
28 changes: 25 additions & 3 deletions bin/bpmnlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,21 @@ const categoryMap = {
* Logs a formatted message
*/
function tableEntry(report) {
const category = report.category;
let {
category,
id = '',
message,
name = '',
path
} = report;

if (path) {
id = `${ id }#${ pathStringify(path) }`;
}

const color = category === 'error' ? red : yellow;

return [ report.id || '', color(categoryMap[category] || category), report.message, report.name || '' ];
return [ id, color(categoryMap[ category ] || category), message, name ];
}

function createTable() {
Expand Down Expand Up @@ -384,4 +394,16 @@ Learn more about configuring bpmnlint: https://github.com/bpmn-io/bpmnlint#confi
return lint(actualFiles, config);
}

run().catch(errorAndExit);
run().catch(errorAndExit);

// helpers //////////

/**
* @param {(number|string)[]} path
* @param {string} [separator]
*
* @returns {string}
*/
function pathStringify(path, separator = '.') {
return path.join(separator);
}
16 changes: 14 additions & 2 deletions lib/test-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@ class Reporter {
this.report = this.report.bind(this);
}

report(id, message) {
this.messages.push({ id, message });
report(id, message, path) {
let report = {
id,
message
};

if (path) {
report = {
...report,
path
};
}

this.messages.push(report);
}
}

Expand Down
32 changes: 31 additions & 1 deletion test/spec/test-rule-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('test-rule', function() {
});


it('should return check function reported messages', () => {
it('should return check function reported messages (id, message)', () => {

// given
const expectedMessages = [
Expand All @@ -35,6 +35,26 @@ describe('test-rule', function() {
});


it('should return check function reported messages (id, message, path)', () => {

// given
const expectedMessages = [
{
id: 'Collaboration_0wzd2dx',
message: 'Collaboration detected',
path: [ 'rootElements', 0 ]
}
];
const messages = testRule({
moddleRoot,
rule: createRule(fakeCheckRuleWithPath)
});

// then
expect(messages).to.eql(expectedMessages);
});


it('should return { enter, leave } hook reported messages', () => {

// given
Expand Down Expand Up @@ -85,6 +105,16 @@ function fakeCheckRuleWithReports() {
return { check };
}

function fakeCheckRuleWithPath() {
function check(node, reporter) {
if (is(node, 'Collaboration')) {
reporter.report(node.id, 'Collaboration detected', [ 'rootElements', 0 ]);
}
}

return { check };
}

function fakeEnterLeaveRuleWithReports() {
function enter(node, reporter) {
if (is(node, 'Definitions')) {
Expand Down

0 comments on commit 4355283

Please sign in to comment.