Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import {DiagnosticSeverity} from 'vscode-languageserver-types';
import invariant from 'assert';
import nullthrows from 'nullthrows';
import URI from 'vscode-uri';
const FlowSeverity = {
Error: 'Error',
Warning: 'Warning',
};
const flowSeverityToLSPSeverityMap: {
[string]: DiagnosticSeverityType,
} = {
[FlowSeverity.Error]: DiagnosticSeverity.Error,
[FlowSeverity.Warning]: DiagnosticSeverity.Warning,
};
export function toURI(filePath: string): URI {
return URI.file(filePath);
}
export function hasFlowPragma(content: string) {
const hasPragma =
content.startsWith('/* @flow */') ||
content.startsWith('// @flow\n') ||
/^\s*\/\*+\s*@flow\s*\*+\//m.test(content) ||
/^\s*\/\/\s*@flow\s*$/m.test(content);
return hasPragma;
}
export function flowSeverityToLSPSeverity(
// Get the list of problem markers for the given resource URI.
const markers: Marker[] = this.problemManager.findMarkers({ uri: resourceUri });
// If no markers are available, return early.
if (markers.length === 0) {
return [];
}
// Store the marker with the highest severity.
let maxSeverity: Marker | undefined;
// Iterate over available markers to determine that which has the highest severity.
// Only display a decoration if an error or warning marker is available.
for (const marker of markers) {
// Break early if an error marker is present, since it represents the highest severity.
if (marker.data.severity === DiagnosticSeverity.Error) {
maxSeverity = marker;
break;
} else if (marker.data.severity === DiagnosticSeverity.Warning) {
maxSeverity = marker;
}
}
// Decorate the tabbar with the highest marker severity if available.
return maxSeverity ? [this.toDecorator(maxSeverity)] : [];
}
}
return [];
}
end: document.positionAt(offset + attr.length)
},
severity: DiagnosticSeverity.Error,
message: `Unknown attribute "${attr}"`
})
}
continue
}
if (attr === 'wx:else') {
if (value) {
result.push({
range:{
start: document.positionAt(offset),
end: document.positionAt(offset + attr.length)
},
severity: DiagnosticSeverity.Warning,
message: `Unnecessary value for "${attr}"`
})
}
continue
}
if (value && isDynamicValue(value)) continue
let type = allowedAttrs[attr].type
switch (type) {
case 'logic':
case 'iterate':
case 'event':
case 'function':
if (!value) {
result.push({
range:{
start: document.positionAt(offset),
protected convertSeverity(severity: lg.DiagnosticSeverity): DiagnosticSeverity {
switch (severity) {
case lg.DiagnosticSeverity.Error:
return DiagnosticSeverity.Error;
case lg.DiagnosticSeverity.Hint:
return DiagnosticSeverity.Hint;
case lg.DiagnosticSeverity.Information:
return DiagnosticSeverity.Information;
case lg.DiagnosticSeverity.Warning:
return DiagnosticSeverity.Warning;
}
}
export function convertSeverity(severity: lg.DiagnosticSeverity): DiagnosticSeverity {
switch (severity) {
case lg.DiagnosticSeverity.Error:
return DiagnosticSeverity.Error;
case lg.DiagnosticSeverity.Hint:
return DiagnosticSeverity.Hint;
case lg.DiagnosticSeverity.Information:
return DiagnosticSeverity.Information;
case lg.DiagnosticSeverity.Warning:
return DiagnosticSeverity.Warning;
}
}
uri: new sourcegraph.URI(location.uri),
range: convertRange(location.range),
})
export function convertLocations(locationOrLocations: Location | Location[] | null): sourcegraph.Location[] | null {
if (!locationOrLocations) {
return null
}
const locations = Array.isArray(locationOrLocations) ? locationOrLocations : [locationOrLocations]
return locations.map(convertLocation)
}
const DIAGNOSTIC_COLORS: Readonly> = {
[DiagnosticSeverity.Error]: 'var(--danger, #dc3545)',
[DiagnosticSeverity.Information]: 'var(--info, #17a2b8)',
[DiagnosticSeverity.Warning]: 'var(--success, #ffc107)',
[DiagnosticSeverity.Hint]: 'var(--secondary, #6c757d)',
}
export const convertDiagnosticToDecoration = (diagnostic: Diagnostic): sourcegraph.TextDocumentDecoration => ({
after: {
color: DIAGNOSTIC_COLORS[diagnostic.severity || DiagnosticSeverity.Hint],
contentText: diagnostic.message.replace(/\n/g, ' '),
},
range: convertRange(diagnostic.range),
})
expect(allMatches.length).to.eq(4);
expect((allMatches[0] as ProblemMatchData).resource!.path).eq('/home/test/test-dir.js');
expect((allMatches[0] as ProblemMatchData).marker).deep.equal({
range: { start: { line: 13, character: 20 }, end: { line: 13, character: 20 } },
severity: DiagnosticSeverity.Warning,
source: 'test2',
message: 'Missing semicolon',
code: 'semi'
});
expect((allMatches[1] as ProblemMatchData).resource!.path).eq('/home/test/test-dir.js');
expect((allMatches[1] as ProblemMatchData).marker).deep.equal({
range: { start: { line: 14, character: 22 }, end: { line: 14, character: 22 } },
severity: DiagnosticSeverity.Warning,
source: 'test2',
message: 'Missing semicolon',
code: 'semi'
});
expect((allMatches[2] as ProblemMatchData).resource!.path).eq('/home/test/test-dir.js');
expect((allMatches[2] as ProblemMatchData).marker).deep.equal({
range: { start: { line: 102, character: 8 }, end: { line: 102, character: 8 } },
severity: DiagnosticSeverity.Error,
source: 'test2',
message: 'Parsing error: Unexpected token inte'
});
expect((allMatches[3] as ProblemMatchData).resource!.path).eq('/home/test/more-test.js');
expect((allMatches[3] as ProblemMatchData).marker).deep.equal({
range: { start: { line: 12, character: 8 }, end: { line: 12, character: 8 } },
protected filterMarker(marker: Marker): boolean {
const { severity } = marker.data;
return severity === DiagnosticSeverity.Error
|| severity === DiagnosticSeverity.Warning
|| severity === DiagnosticSeverity.Information;
}
function toDiagnostic(marker) {
var range = Range.create(document.positionAt(marker.getOffset()), document.positionAt(marker.getOffset() + marker.getLength()));
var source = document.languageId;
return {
code: marker.getRule().id,
source: source,
message: marker.getMessage(),
severity: marker.getLevel() === nodes.Level.Warning ? DiagnosticSeverity.Warning : DiagnosticSeverity.Error,
range: range
};
}
return entries.filter(function (entry) { return entry.getLevel() !== nodes.Level.Ignore; }).map(toDiagnostic);
' 15:23 warning Missing semicolon semi',
' 103:9 error Parsing error: Unexpected token inte',
'',
'/home/test/more-test.js',
' 13:9 error Parsing error: Unexpected token 1000',
'',
'✖ 3 problems (1 error, 2 warnings)',
' 0 errors and 2 warnings potentially fixable with the `--fix` option.'
]);
expect(allMatches.length).to.eq(4);
expect((allMatches[0] as ProblemMatchData).resource!.path).eq('/home/test/test-dir.js');
expect((allMatches[0] as ProblemMatchData).marker).deep.equal({
range: { start: { line: 13, character: 20 }, end: { line: 13, character: 20 } },
severity: DiagnosticSeverity.Warning,
source: 'test2',
message: 'Missing semicolon',
code: 'semi'
});
expect((allMatches[1] as ProblemMatchData).resource!.path).eq('/home/test/test-dir.js');
expect((allMatches[1] as ProblemMatchData).marker).deep.equal({
range: { start: { line: 14, character: 22 }, end: { line: 14, character: 22 } },
severity: DiagnosticSeverity.Warning,
source: 'test2',
message: 'Missing semicolon',
code: 'semi'
});
expect((allMatches[2] as ProblemMatchData).resource!.path).eq('/home/test/test-dir.js');
expect((allMatches[2] as ProblemMatchData).marker).deep.equal({