Skip to content

Commit

Permalink
feat: support rule meta and pass to reports
Browse files Browse the repository at this point in the history
Closes #18
  • Loading branch information
nikku committed Feb 17, 2025
1 parent cc1db52 commit a29bcda
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const testRule = require('./test-rule');
* } } ResolvedRuleDefinition
*
* @typedef { import('./types.js').Report } Report
* @typedef { Report & {
* @typedef { Report & Pick<RuleDefinition, 'meta'> & {
* category: ReportingCategory | RuleErrorCategory
* } } AnnotatedReport
*
Expand Down Expand Up @@ -111,6 +111,7 @@ Linter.prototype.applyRule = function applyRule(moddleRoot, ruleDefinition) {
return reports.map(function(report) {
return {
...report,
meta: rule.meta,
category
};
});
Expand Down
5 changes: 5 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export type CheckDefinition = EnterFn | {
};

export type RuleDefinition = {
meta?: {
documentation?: {
url?: string
}
},
check: CheckDefinition
};

Expand Down
68 changes: 61 additions & 7 deletions test/spec/linter-spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -949,9 +949,64 @@ describe('linter', function() {

it('caching behavior');


describe('reporting', function() {

let moddleRoot;

const linter = new Linter({
resolver: fakeResolver()
});

const annotateRule = (rule, meta) => {

return {
...rule,
meta
};
};

beforeEach(async function() {
const result = await readModdle(__dirname + '/diagram.bpmn');

moddleRoot = result.root;
});


it('should attach rule <meta> to report', function() {

// given
const meta = { foo: 'BAR' };

const rule = annotateRule(
createRule(fakeRule),
meta
);

// when
const results = linter.applyRule(
moddleRoot,
{
name: 'test-rule',
config: { },
rule,
category: 'error'
}
);

const expectedResult = buildFakeResults('error', null, meta);

// then
expect(results).to.eql(expectedResult);
});

});

});


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

function fakeResolver(cache = {}) {
return {
resolveRule(pkg, ruleName) {
Expand All @@ -964,7 +1019,6 @@ function fakeResolver(cache = {}) {
};
}


async function fakeAsyncRule() {

function check(node, reporter) {
Expand Down Expand Up @@ -995,7 +1049,7 @@ function fakeRule(config = {}) {
}


function buildFakeResults(category, message) {
function buildFakeResults(category, message, meta) {
const results = [
{
id: 'sid-38422fae-e03e-43a3-bef4-bd33b32041b2',
Expand All @@ -1010,12 +1064,12 @@ function buildFakeResults(category, message) {
return results.map((result) => {
return {
...result,
category
category,
meta
};
});
}


function fakeEnterLeaveRule() {

const seen = {};
Expand Down Expand Up @@ -1047,8 +1101,7 @@ function fakeEnterLeaveRule() {
};
}


function buildFakeEnterLeaveResults(category, message) {
function buildFakeEnterLeaveResults(category, message, meta) {
const results = [
{
id: 'sid-38422fae-e03e-43a3-bef4-bd33b32041b2',
Expand All @@ -1063,7 +1116,8 @@ function buildFakeEnterLeaveResults(category, message) {
return results.map((result) => {
return {
...result,
category
category,
meta
};
});
}

0 comments on commit a29bcda

Please sign in to comment.