Skip to content

Commit

Permalink
Add SARIF support
Browse files Browse the repository at this point in the history
  • Loading branch information
shaopeng-gh committed Nov 2, 2022
1 parent c22c21d commit a05d02b
Show file tree
Hide file tree
Showing 8 changed files with 809 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Features

* add support format: sarif ([#1045](https://github.com/htmlhint/HTMLHint/issues/1045))

## [1.1.4](https://github.com/htmlhint/HTMLHint/compare/v1.1.3...v1.1.4) (2022-04-11)


Expand Down
59 changes: 59 additions & 0 deletions dist/cli/formatters/sarif.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/user-guide/usage/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Options are:
- `html`
- `junit`
- `markdown`
- `sarif`

## `ignore`

Expand Down
63 changes: 63 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"glob": "^8.0.3",
"is-glob": "^4.0.3",
"node-fetch": "^2.6.2",
"node-sarif-builder": "^2.0.3",
"strip-json-comments": "3.1.0",
"xml": "1.0.1"
},
Expand Down
76 changes: 76 additions & 0 deletions src/cli/formatters/sarif.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { FormatterCallback } from '../formatter'
import {
SarifBuilder,
SarifResultBuilder,
SarifRuleBuilder,
SarifRunBuilder,
} from 'node-sarif-builder'
import * as path from 'path'
import { pathToFileURL } from 'url'
import { Result } from 'sarif'

const pkg = require('../../../package.json')

const sarifFormatter: FormatterCallback = function (formatter) {
formatter.on('end', (event) => {
const arrAllMessages = event.arrAllMessages

// SARIF builder
const sarifBuilder = new SarifBuilder()

// SARIF Run builder
const sarifRunBuilder = new SarifRunBuilder().initSimple({
toolDriverName: 'HTMLHint',
toolDriverVersion: pkg.version,
url: 'https://htmlhint.com/',
})

// SARIF rules
const addedRuleSet = new Set<string>()
arrAllMessages.forEach((result) => {
result.messages.forEach((message) => {
const rule = message.rule
if (addedRuleSet.has(rule.id)) {
return
}
addedRuleSet.add(rule.id)
const sarifRuleBuiler = new SarifRuleBuilder().initSimple({
ruleId: rule.id,
shortDescriptionText: rule.description,
helpUri: rule.link,
})
sarifRunBuilder.addRule(sarifRuleBuiler)
})
})

// Add SARIF results (individual errors)
arrAllMessages.forEach((result) => {
result.messages.forEach((message) => {
const sarifResultBuilder = new SarifResultBuilder()
const ruleId = message.rule.id
const sarifResultInit = {
level:
message.type === 'info'
? 'note'
: (message.type.toString() as Result.level),
messageText: message.message,
ruleId: ruleId,
fileUri: process.env.SARIF_URI_ABSOLUTE
? pathToFileURL(result.file).toString()
: path.relative(process.cwd(), result.file).replace(/\\/g, '/'),
startLine: message.line,
startColumn: message.col,
endLine: message.line,
endColumn: message.col,
} as const
sarifResultBuilder.initSimple(sarifResultInit)
sarifRunBuilder.addResult(sarifResultBuilder)
})
})

sarifBuilder.addRun(sarifRunBuilder)
console.log(sarifBuilder.buildSarifJsonString({ indent: true }))
})
}

module.exports = sarifFormatter
Loading

0 comments on commit a05d02b

Please sign in to comment.