Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Ensure linter types/severities are adapted correctly (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
hansonw committed Sep 15, 2017
1 parent eda7299 commit 204b354
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {NuclideUri} from 'nuclide-commons/nuclideUri';
import type {
DiagnosticInvalidationMessage,
DiagnosticMessage,
DiagnosticMessageType,
DiagnosticProviderUpdate,
FileDiagnosticMessage,
LinterMessage,
Expand Down Expand Up @@ -44,13 +45,14 @@ export function linterMessageToDiagnosticMessage(
const trace = msg.trace
? msg.trace.map(component => ({...component}))
: undefined;
const type = convertLinterType(msg.type);
// flowlint-next-line sketchy-null-string:off
if (msg.filePath) {
const {fix} = msg;
return ({
scope: 'file',
providerName: msg.name != null ? msg.name : providerName,
type: msg.type,
type,
filePath: msg.filePath,
text: msg.text,
html: msg.html,
Expand All @@ -69,7 +71,7 @@ export function linterMessageToDiagnosticMessage(
return ({
scope: 'project',
providerName: msg.name != null ? msg.name : providerName,
type: msg.type,
type,
text: msg.text,
html: msg.html,
range: msg.range && Range.fromObject(msg.range),
Expand All @@ -78,11 +80,21 @@ export function linterMessageToDiagnosticMessage(
}
}

const LinterSeverityMap = {
error: 'Error',
warning: 'Warning',
info: 'Info',
};
// Be flexible in accepting various linter types/severities.
function convertLinterType(type: string): DiagnosticMessageType {
switch (type) {
case 'Error':
case 'error':
return 'Error';
case 'Warning':
case 'warning':
return 'Warning';
case 'Info':
case 'info':
return 'Info';
}
return 'Error';
}

// Version 2 only handles file-level diagnostics.
export function linterMessageV2ToDiagnosticMessage(
Expand Down Expand Up @@ -130,7 +142,7 @@ export function linterMessageV2ToDiagnosticMessage(
scope: 'file',
// flowlint-next-line sketchy-null-string:off
providerName: msg.linterName || providerName,
type: LinterSeverityMap[msg.severity],
type: convertLinterType(msg.severity),
filePath: msg.location.file,
text,
range: Range.fromObject(msg.location.position),
Expand Down
3 changes: 2 additions & 1 deletion modules/atom-ide-ui/pkg/atom-ide-diagnostics/lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ export type LinterTrace = {
};

export type LinterMessageV1 = {
type: 'Error' | 'Warning' | 'Info',
// Should be Error / Warning / Info, but no guarantees.
type: string,
text?: string,
html?: string,
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ describe('message transformation functions', () => {
filePath: fileMessage.filePath,
text: fileMessage.text,
});

// Invalid types are automatically turned into "Error".
checkMessage(
{...fileMessage, type: 'blah'},
{
scope: 'file',
providerName,
type: 'Error',
filePath: fileMessage.filePath,
text: fileMessage.text,
},
);
});

it('should turn a message without a filePath into a project scope diagnostic', () => {
Expand Down

0 comments on commit 204b354

Please sign in to comment.