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

feat(formatters): add sarif formatter #2532

Merged
merged 12 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 2 deletions docs/guides/2-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ Other options include:
[string] [choices: "utf8", "ascii", "utf-8", "utf16le", "ucs2", "ucs-2", "base64", "latin1"] [default: "utf8"]
-f, --format formatters to use for outputting results, more than one can be given joining them with
a comma
[string] [choices: "json", "stylish", "junit", "html", "text", "teamcity", "pretty", "github-actions"] [default:
"stylish"]
[string] [choices: "json", "stylish", "junit", "html", "text", "teamcity", "pretty", "github-actions", "sarif"]
[default: "stylish"]
-o, --output where to output results, can be a single file name, multiple "output.<format>" or
missing to print to stdout [string]
--stdin-filepath path to a file to pretend that stdin comes from [string]
Expand Down
2 changes: 2 additions & 0 deletions karma.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module.exports = (config: Config): void => {
exclude: [
'packages/cli/**',
'packages/formatters/src/pretty.ts',
'packages/formatters/src/github-actions.ts',
'packages/formatters/src/sarif.ts',
'packages/formatters/src/index.node.ts',
'packages/ruleset-bundler/src/plugins/commonjs.ts',
'**/*.jest.test.ts',
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/__tests__/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('lint', () => {
];

beforeEach(() => {
(lint as jest.Mock).mockResolvedValueOnce(results);
(lint as jest.Mock).mockResolvedValueOnce({ results: results, resolvedRuleset: {} });
(formatOutput as jest.Mock).mockReturnValueOnce('<formatted output>');
(writeOutput as jest.Mock).mockResolvedValueOnce(undefined);
});
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('lint', () => {

it.each(['json', 'stylish'])('calls formatOutput with %s format', async format => {
await run(`lint -f ${format} ./__fixtures__/empty-oas2-document.json`);
expect(formatOutput).toBeCalledWith(results, format, { failSeverity: DiagnosticSeverity.Error });
expect(formatOutput).toBeCalledWith(results, format, { failSeverity: DiagnosticSeverity.Error }, expect.anything());
});

it('writes formatted output to a file', async () => {
Expand Down
15 changes: 10 additions & 5 deletions packages/cli/src/commands/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ const lintCommand: CommandModule = {
};

try {
let results = await lint(documents, {
const linterResult = await lint(documents, {
format,
output,
encoding,
Expand All @@ -194,18 +194,23 @@ const lintCommand: CommandModule = {
});

if (displayOnlyFailures) {
results = filterResultsBySeverity(results, failSeverity);
linterResult.results = filterResultsBySeverity(linterResult.results, failSeverity);
}

await Promise.all(
format.map(f => {
const formattedOutput = formatOutput(results, f, { failSeverity: getDiagnosticSeverity(failSeverity) });
const formattedOutput = formatOutput(
linterResult.results,
f,
{ failSeverity: getDiagnosticSeverity(failSeverity) },
linterResult.resolvedRuleset,
);
return writeOutput(formattedOutput, output?.[f] ?? '<stdout>');
}),
);

if (results.length > 0) {
process.exit(severeEnoughToFail(results, failSeverity) ? 1 : 0);
if (linterResult.results.length > 0) {
process.exit(severeEnoughToFail(linterResult.results, failSeverity) ? 1 : 0);
} else if (config.quiet !== true) {
const isErrorSeverity = getDiagnosticSeverity(failSeverity) === DiagnosticSeverity.Error;
process.stdout.write(
Expand Down
Loading