import * as ts from "typescript" ; import * as vscode from "vscode" ; const JSDOC_DEPRECATED_ANNOTATION = "*@deprecated*" ; function getIdentifierPositions ( document ) { const positions = [ ] ; const file = document . uri . fsPath ; const program = ts . createProgram ( [ file ] , { allowJs : true } ) ; const source = program . getSourceFile ( file ) ; const visit = ( node ) : void => { if ( ts . isIdentifier ( node ) ) { positions . push ( document . positionAt ( node . end ) ) ; } ts . forEachChild ( node , visit ) ; } ; ts . forEachChild ( source , visit ) ; return positions ; } async function getHoverAnnotations ( document , positions ) { return Promise . all ( positions . map ( ( position ) : Thenable < vscode . Hover [ ] > => vscode . commands . executeCommand ( "vscode.executeHoverProvider" , document . uri , position ) ) ) ; } function containsDeprecatedAnnotation ( hovers ) { return hovers . some ( ( hover ) => hover . contents . some ( ( content ) => content . value . includes ( JSDOC_DEPRECATED_ANNOTATION ) ) ) ; } function getDeprecatedRanges ( hovers ) { return hovers . reduce ( ( ranges , hover ) => { if ( containsDeprecatedAnnotation ( hover ) ) { return [ ... ranges , hover . pop ( ) . range as vscode . Range ] ; } return ranges ; } , [ ] ) ; } function paintAnnotations ( editor , ranges , decorationType ) { editor . setDecorations ( decorationType , [ ] ) ; editor . setDecorations ( decorationType , ranges ) ; } async function onDidUpdateTextDocument ( document , editor , decorationType ) { if ( editor ) { const positions = getIdentifierPositions ( document ) ; const annotations = await getHoverAnnotations ( document , positions ) ; const deprecated = getDeprecatedRanges ( annotations ) ; paintAnnotations ( editor , deprecated , decorationType ) ; } } export function activate ( ) { const decorationType = vscode . window . createTextEditorDecorationType ( { textDecoration : "line-through" , } ) ; setImmediate ( ( ) => onDidUpdateTextDocument ( vscode . window . activeTextEditor . document , vscode . window . activeTextEditor , decorationType ) ) ; vscode . workspace . onDidOpenTextDocument ( ( document ) => { onDidUpdateTextDocument ( document , vscode . window . activeTextEditor , decorationType ) ; } ) ; vscode . workspace . onDidSaveTextDocument ( ( document ) => { onDidUpdateTextDocument ( document , vscode . window . activeTextEditor , decorationType ) ; } ) ; vscode . window . onDidChangeActiveTextEditor ( ( editor ) => { onDidUpdateTextDocument ( editor . document , editor , decorationType ) ; } ) ; }