import { sortBy , uniq } from "lodash" ; import { LinterGetOffensesFunction , LinterGetIgnoreLinePragmaFunction , LinterOffense , LinterOffenseSeverity , } from "vscode-linter-api" ; import { camelizeObject } from "../helpers/camelizeObject" ; export interface HadolintOffense { line : number ; code : string ; message : string ; column : number ; file : string ; level : string ; } const linePragmaRegex = /^# hadolint ignore=(.+)$/ ; const offenseSeverity = { error : LinterOffenseSeverity . error , warning : LinterOffenseSeverity . warning , } ; export const getOffenses = ( { stdout , uri } ) => { const result = JSON . parse ( stdout ) ; const offenses = [ ] ; result . forEach ( ( offense ) => { offense = camelizeObject ( offense ) ; const lineStart = Math . max ( 0 , offense . line - 1 ) ; const columnStart = Math . max ( 0 , offense . column - 1 ) ; offenses . push ( { uri , lineStart , lineEnd : lineStart , columnStart , columnEnd : columnStart , message : offense . message , code : offense . code , source : "hadolint" , correctable : false , severity : offenseSeverity [ offense . level ] , docsUrl : getDocsUrl ( offense . code ) , } ) ; } ) ; return offenses ; } ; export const getIgnoreLinePragma = ( { line , code , indent , } ) => { const { text } = line ; const matches = text . match ( linePragmaRegex ) ; let existingRules = [ code ] ; if ( matches ) { existingRules . push ( ... ( matches [ 1 ] ?? "" ) . split ( "," ) . map ( ( item ) => item . trim ( ) ) ) ; } const pragma = [ `${ indent }# hadolint ignore=` , sortBy ( uniq ( existingRules ) ) . join ( "," ) , ] . join ( "" ) ; if ( matches ) { return pragma ; } return line . number === 0 ? [ pragma , text ] . join ( "\n" ) : [ text , "" , pragma ] . join ( "\n" ) ; } ; function getDocsUrl ( code ) { return `https://github.com/hadolint/hadolint/wiki/${ code }` ; }