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 SARIF support #1221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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