Skip to content

Commit

Permalink
(feat) support for diagnostic tags (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyu123 committed Sep 17, 2020
1 parent 5f34419 commit 3fee94d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ts from 'typescript';
import { Diagnostic, DiagnosticSeverity } from 'vscode-languageserver';
import { Diagnostic, DiagnosticSeverity, DiagnosticTag } from 'vscode-languageserver';
import { Document, mapObjWithRangeToOriginal, getTextInRange } from '../../../lib/documents';
import { DiagnosticsProvider } from '../../interfaces';
import { LSAndTSDocResolver } from '../LSAndTSDocResolver';
Expand Down Expand Up @@ -35,19 +35,31 @@ export class DiagnosticsProviderImpl implements DiagnosticsProvider {
const fragment = await tsDoc.getFragment();

return diagnostics
.map((diagnostic) => ({
.map<Diagnostic>((diagnostic) => ({
range: convertRange(tsDoc, diagnostic),
severity: mapSeverity(diagnostic.category),
source: isTypescript ? 'ts' : 'js',
message: ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'),
code: diagnostic.code,
tags: this.getDiagnosticTag(diagnostic)
}))
.map((diagnostic) => mapObjWithRangeToOriginal(fragment, diagnostic))
.filter(hasNoNegativeLines)
.filter(isNoFalsePositive(document.getText(), tsDoc))
.map(enhanceIfNecessary);
}

private getDiagnosticTag(diagnostic: ts.Diagnostic) {
const tags: DiagnosticTag[] = [];
if (diagnostic.reportsUnnecessary) {
tags.push(DiagnosticTag.Unnecessary);
}
if (diagnostic.reportsDeprecated) {
tags.push(DiagnosticTag.Deprecated);
}
return tags;
}

private getLSAndTSDoc(document: Document) {
return this.lsAndTsDocResolver.getLSAndTSDoc(document);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('DiagnosticsProvider', () => {
},
severity: 1,
source: 'ts',
tags: [],
},
]);
});
Expand All @@ -62,6 +63,7 @@ describe('DiagnosticsProvider', () => {
},
severity: 1,
source: 'js',
tags: [],
},
]);
});
Expand Down Expand Up @@ -109,4 +111,46 @@ describe('DiagnosticsProvider', () => {
const diagnostics = await plugin.getDiagnostics(document);
assert.deepStrictEqual(diagnostics, []);
});

it('provide diagnostics tags', async () => {
const { plugin, document } = setup('diagnostics-tag.svelte');
const diagnostics = await plugin.getDiagnostics(document);

assert.deepStrictEqual(diagnostics, [
{
code: 6385,
message: "'a' is deprecated",
range: {
end: {
character: 5,
line: 3,
},
start: {
character: 4,
line: 3,
},
},
severity: 4,
source: 'ts',
tags: [2],
},
{
code: 6133,
message: "'c' is declared but its value is never read.",
range: {
end: {
character: 9,
line: 4,
},
start: {
character: 8,
line: 4,
},
},
severity: 4,
source: 'ts',
tags: [1],
},
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script lang="ts">
/**@deprecated*/
let a;
a;
let c;
</script>

0 comments on commit 3fee94d

Please sign in to comment.