Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no rule metadata test case to sarif-test and fix that test failure. #27

Merged
merged 7 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/eslint-formatter-sarif/__tests__/sarif-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ const rules = {
unexpected: 'Unnecessary semicolon.',
},
},
'fake/no-missing-meta': {
message:
'This rule is fake, and we expect it to have no docs',
},
};

const sourceFilePath1 = 'service.js';
Expand Down Expand Up @@ -677,4 +681,29 @@ describe('formatter:sarif', () => {
expect(log.runs[0].results[0].locations[0].physicalLocation.region.snippet).not.toBeDefined();
});
});

describe('when passed rules without metadata', () => {
const ruleId = 'fake/no-missing-meta';
const code = [
{
filePath: sourceFilePath1,
messages: [
{
message: 'Unexpected value.',
ruleId: ruleId,
source: 'getValue()',
},
],
},
];

it('should return a log with one rule', () => {
const log = JSON.parse(formatter(code, { rulesMeta: rules }));

expect(log.runs[0].tool.driver.rules).toHaveLength(1);
expect(log.runs[0].tool.driver.rules[0].id).toBe(ruleId);
expect(log.runs[0].tool.driver.rules[0].helpUri).toBe('Please see details in message');
expect(log.runs[0].tool.driver.rules[0].properties.category).toBe('No category provided');
});
});
});
33 changes: 22 additions & 11 deletions packages/eslint-formatter-sarif/sarif.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,28 @@ module.exports = function (results, data) {
if (meta) {
sarifRuleIndices[message.ruleId] = nextRuleIndex++;

// Create a new entry in the rules dictionary.
sarifRules[message.ruleId] = {
id: message.ruleId,
helpUri: meta.docs.url,
properties: {
category: meta.docs.category,
},
};
if (meta.docs.description) {
sarifRules[message.ruleId].shortDescription = {
text: meta.docs.description,
if (meta.docs) {
// Create a new entry in the rules dictionary.
sarifRules[message.ruleId] = {
id: message.ruleId,
helpUri: meta.docs.url,
properties: {
category: meta.docs.category,
},
};
if (meta.docs.description) {
sarifRules[message.ruleId].shortDescription = {
text: meta.docs.description,
};
}
// Some rulesMetas do not have docs property
} else {
sarifRules[message.ruleId] = {
id: message.ruleId,
helpUri: 'Please see details in message',
properties: {
category: 'No category provided',
},
};
}
}
Expand Down