Skip to content

Commit 86f2509

Browse files
implemented @deprecated diagnostics reporting (#430)
1 parent 1b4d55f commit 86f2509

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/** @deprecated This class is deprecated */
2+
class Foo {}
3+
4+
/** @deprecated This function is deprecated */
5+
function bar() {}
6+
7+
/**
8+
* @deprecated This is a function that has
9+
* multiple lines and is also deprecated. Make
10+
* sure to reference {@link bar} for some reason
11+
*/
12+
function car() {}
13+
14+
function main() {
15+
new Foo()
16+
bar()
17+
car()
18+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "diagnostics",
3+
"version": "0.0.1"
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// < definition diagnostics 0.0.1 `index.ts`/
2+
3+
/** @deprecated This class is deprecated */
4+
class Foo {}
5+
// ^^^ definition diagnostics 0.0.1 `index.ts`/Foo#
6+
7+
/** @deprecated This function is deprecated */
8+
function bar() {}
9+
// ^^^ definition diagnostics 0.0.1 `index.ts`/bar().
10+
11+
/**
12+
* @deprecated This is a function that has
13+
* multiple lines and is also deprecated. Make
14+
* sure to reference {@link bar} for some reason
15+
*/
16+
function car() {}
17+
// ^^^ definition diagnostics 0.0.1 `index.ts`/car().
18+
19+
function main() {
20+
// ^^^^ definition diagnostics 0.0.1 `index.ts`/main().
21+
new Foo()
22+
// ^^^ reference diagnostics 0.0.1 `index.ts`/Foo#
23+
// diagnostic Information:
24+
// > This class is deprecated
25+
bar()
26+
//^^^ reference diagnostics 0.0.1 `index.ts`/bar().
27+
//diagnostic Information:
28+
//> This function is deprecated
29+
car()
30+
//^^^ reference diagnostics 0.0.1 `index.ts`/car().
31+
//diagnostic Information:
32+
//> This is a function that has
33+
//> multiple lines and is also deprecated. Make
34+
//> sure to reference {@link bar } for some reason
35+
}
36+

src/FileIndexer.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ export class FileIndexer {
242242
range,
243243
symbol: scipSymbol.value,
244244
symbol_roles: role,
245+
246+
diagnostics: FileIndexer.diagnosticsFor(sym, isDefinitionNode),
245247
})
246248
)
247249
if (isDefinitionNode) {
@@ -718,6 +720,42 @@ export class FileIndexer {
718720
}
719721
loop(node)
720722
}
723+
724+
/**
725+
* Returns the scip diagnostics for a given typescript symbol.
726+
* @param sym - The TypeScript symbol to get diagnostics for
727+
* @param isDefinition - Whether this occurrence is a definition of the symbol
728+
*/
729+
private static diagnosticsFor(
730+
sym: ts.Symbol,
731+
isDefinition: boolean
732+
): scip.scip.Diagnostic[] | undefined {
733+
// Currently, the logic below only supports deprecation
734+
// diagnostics. Since linters typically only emit such
735+
// diagnostics at reference sites, skip the check if we're
736+
// not at a definition.
737+
if (isDefinition) {
738+
return undefined
739+
}
740+
741+
const jsDocTags = sym.getJsDocTags()
742+
743+
const deprecatedTag = jsDocTags.find(tag => tag.name === 'deprecated')
744+
if (deprecatedTag) {
745+
return [
746+
new scip.scip.Diagnostic({
747+
severity: scip.scip.Severity.Information,
748+
code: 'DEPRECATED',
749+
// jsDocInfo.text is a tokenized representation of the tag text.
750+
// Concatenate the elements to get the full message
751+
message: deprecatedTag.text?.map(part => part.text).join(''),
752+
tags: [scip.scip.DiagnosticTag.Deprecated],
753+
}),
754+
]
755+
}
756+
757+
return undefined
758+
}
721759
}
722760

723761
function isAnonymousContainerOfSymbols(node: ts.Node): boolean {

src/SnapshotTesting.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,19 @@ export function formatSnapshot(
295295
out.push(symbol.replace('\n', '|'))
296296

297297
pushDoc(range, occurrence.symbol, isDefinition, isStartOfLine)
298+
299+
if (occurrence.diagnostics && occurrence.diagnostics.length > 0) {
300+
for (const diagnostic of occurrence.diagnostics) {
301+
const indent = ' '.repeat(range.start.character - 2)
302+
out.push(commentSyntax + indent)
303+
out.push(`diagnostic ${scip.Severity[diagnostic.severity]}:\n`)
304+
if (diagnostic.message) {
305+
for (const messageLine of diagnostic.message.split('\n')) {
306+
out.push(`${commentSyntax + indent}> ${messageLine}\n`)
307+
}
308+
}
309+
}
310+
}
298311
}
299312

300313
// Check if any enclosing ranges end on this line

0 commit comments

Comments
 (0)