import * as vscode from 'vscode' ; import * as yaml from 'js-yaml' ; import { JsonALikeYamlDocumentSymbolProvider } from '../../yaml-support/jsonalike-symbol-provider' ; import { Linter } from './linters' ; import { JsonHierarchicalDocumentSymbolProvider } from '../json/jsonhierarchicalsymbolprovider' ; const jsonalikeYamlSymboliser = new JsonALikeYamlDocumentSymbolProvider ( ) ; const jsonSymboliser = new JsonHierarchicalDocumentSymbolProvider ( ) ; export function expose ( impl ) { return new StandardLinter ( impl ) ; } export interface Syntax { load ( text ) : any [ ] ; symbolise ( document ) : Promise < vscode . SymbolInformation [ ] | null | undefined > ; } class StandardLinter implements Linter { constructor ( private readonly impl ) { } name ( ) { return this . impl . name ( ) ; } async lint ( document ) { try { switch ( document . languageId ) { case 'json' : return await this . impl . lint ( document , jsonSyntax ) ; case 'yaml' : return await this . impl . lint ( document , yamlSyntax ) ; default : return [ ] ; } } catch { return [ ] ; } } } const jsonSyntax = { load ( text ) { return [ JSON . parse ( text ) ] ; } , async symbolise ( document ) { return await jsonSymboliser . provideDocumentSymbols ( document , new vscode . CancellationTokenSource ( ) . token ) ; } } ; const yamlSyntax = { load ( text ) { return yaml . safeLoadAll ( text ) ; } , async symbolise ( document ) { return await jsonalikeYamlSymboliser . provideDocumentSymbols ( document , new vscode . CancellationTokenSource ( ) . token ) ; } } ; export interface LinterImpl { name ( ) : string ; lint ( document , syntax ) : Promise < vscode . Diagnostic [ ] > ; }