diff --git a/src/ESLint.Formatter/sarif.js b/src/ESLint.Formatter/sarif.js index 942626bae..16db1b19a 100644 --- a/src/ESLint.Formatter/sarif.js +++ b/src/ESLint.Formatter/sarif.js @@ -145,14 +145,16 @@ module.exports = function (results, data) { // Create a new entry in the rules dictionary. sarifRules[message.ruleId] = { id: message.ruleId, - shortDescription: { - text: meta.docs.description - }, helpUri: meta.docs.url, properties: { category: meta.docs.category } }; + if (meta.docs.description) { + sarifRules[message.ruleId].shortDescription = { + text: meta.docs.description + }; + } } } @@ -179,9 +181,12 @@ module.exports = function (results, data) { } if (message.line > 0 || message.column > 0) { - sarifRepresentation.locations[0].physicalLocation.region = { - startLine: message.line, - startColumn: message.column + sarifRepresentation.locations[0].physicalLocation.region = {}; + if (message.line > 0) { + sarifRepresentation.locations[0].physicalLocation.region.startLine = message.line; + } + if (message.column > 0) { + sarifRepresentation.locations[0].physicalLocation.region.startColumn = message.column; }; } diff --git a/src/ESLint.Formatter/test/sarif.js b/src/ESLint.Formatter/test/sarif.js index 9f44e0b38..ab7a306c3 100644 --- a/src/ESLint.Formatter/test/sarif.js +++ b/src/ESLint.Formatter/test/sarif.js @@ -161,6 +161,28 @@ describe("formatter:sarif", () => { }); }); +describe("formatter:sarif", () => { + describe("when passed one message with line and invalid column", () => { + const code = [{ + filePath: sourceFilePath1, + messages: [{ + message: "Unexpected value.", + ruleId: testRuleId, + line: 10, + column: 0 + }] + }]; + + it("should return a log with one result whose location contains a region with line # and no column #", () => { + const log = JSON.parse(formatter(code)); + + assert.strictEqual(log.runs[0].results[0].locations[0].physicalLocation.region.startLine, code[0].messages[0].line); + assert.isUndefined(log.runs[0].results[0].locations[0].physicalLocation.region.startColumn); + assert.isUndefined(log.runs[0].results[0].locations[0].physicalLocation.region.snippet); + }); + }); +}); + describe("formatter:sarif", () => { describe("when passed one message with line and column but no source string", () => { const code = [{ @@ -473,3 +495,54 @@ describe("formatter:sarif", () => { }); }); }); + +describe("formatter:sarif", () => { + describe("when passed a rule with no description", () => { + const ruleid = "custom-rule-no-description"; + + rules[ruleid] = { + type: "suggestion", + docs: { + category: "Possible Errors" + } + }; + const code = [{ + filePath: sourceFilePath1, + messages: [{ + message: "Custom error.", + ruleId: ruleid, + line: 42 + }] + }]; + it("should return a log with one file, one rule, and one result", () => { + const log = JSON.parse(formatter(code, { rulesMeta: rules })); + const rule = rules[ruleid]; + + assert.lengthOf(log.runs[0].artifacts, 1); + assert.lengthOf(log.runs[0].results, 1); + + assert.strictEqual(log.runs[0].tool.driver.rules[0].id, ruleid); + + assert(log.runs[0].artifacts[0].location.uri.startsWith(uriPrefix)); + assert(log.runs[0].artifacts[0].location.uri.endsWith(sourceFilePath1)); + + assert.strictEqual(log.runs[0].tool.driver.rules[0].id, ruleid); + assert.isUndefined(log.runs[0].tool.driver.rules[0].shortDescription); + assert.strictEqual(log.runs[0].tool.driver.rules[0].helpUri, rule.docs.url); + assert.strictEqual(log.runs[0].tool.driver.rules[0].properties.category, rule.docs.category); + + assert.strictEqual(log.runs[0].results[0].ruleId, ruleid); + + assert.strictEqual(log.runs[0].results[0].level, "warning"); + + assert.strictEqual(log.runs[0].results[0].message.text, "Custom error."); + + assert(log.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri.startsWith(uriPrefix)); + assert(log.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri.endsWith(sourceFilePath1)); + + assert.strictEqual(log.runs[0].results[0].locations[0].physicalLocation.region.startLine, 42); + assert.isUndefined(log.runs[0].results[0].locations[0].physicalLocation.region.startColumn); + assert.isUndefined(log.runs[0].results[0].locations[0].physicalLocation.region.snippet); + }); + }); +});