import { debug , log } from "../helpers/debug" ; import { LinterGetOffensesFunction , LinterParseFixOutputFunction , LinterOffense , LinterOffenseSeverity , } from "vscode-linter-api" ; interface StylelintPayload { source : string ; deprecations : any [ ] ; invalidOptionWarnings : any [ ] ; parseErrors : any [ ] ; errored : boolean ; warnings : StylelintOffense [ ] ; } interface StylelintOffense { line : number ; column : number ; rule : string ; severity : string ; text : string ; } const OFFENSE_SEVERITY = { 1 : LinterOffenseSeverity . warning , 2 : LinterOffenseSeverity . error , } ; export const getOffenses = ( { stdout , stderr , uri , } ) => { if ( ! stdout ) { debug ( "stylelint: stdout was empty, but here's stderr:" ) ; log ( stderr ) ; return [ ] ; } let result = [ ] ; try { result = JSON . parse ( stdout ) ; } catch ( error ) { return [ ] ; } const offenses = [ ] ; result [ 0 ] . warnings . forEach ( ( offense ) => { const message = offense . text . replace ( ` (${ offense . rule })` , "" ) ; offenses . push ( { uri , lineStart : Math . max ( 0 , offense . line - 1 ) , columnStart : Math . max ( 0 , offense . column - 1 ) , lineEnd : Math . max ( 0 , offense . line - 1 ) , columnEnd : Math . max ( 0 , offense . column - 1 ) , code : offense . rule , message , severity : OFFENSE_SEVERITY [ offense . severity ] , source : "stylelint" , correctable : false , docsUrl : getDocsUrl ( offense . rule ) , } ) ; } ) ; return offenses ; } ; export const parseFixOutput = ( { stdout } ) => stdout ; function getDocsUrl ( code ) { if ( ! code ) { return undefined ; } const [ plugin , rule ] = code . split ( "/" ) ; const urls = { standard : `https://stylelint.io/user-guide/rules/list/${ plugin }` , } ; if ( rule ) { return urls [ plugin ] ; } return urls . standard ; }