import { ResultParser } from '@config/types' ; import { LintMessage } from '@engine/types' ; const NEW_LINE_REGEX = /\n/g ; export interface Result { repository : string ; repositoryOwner : string ; rule : LintMessage [ 'ruleId' ] ; message : LintMessage [ 'message' ] ; path : string ; link : string ; extension ? : string ; source : LintMessage [ 'source' ] ; error ? : LintMessage [ 'error' ] ; __internalHash : string & { readonly _ : unique symbol ; } ; } export interface ComparisonResults { /** New results compared to previous scan */ added : Result [ ] ; /** Removed results compared to previous scan */ removed : Result [ ] ; } type ComparisonType = keyof ComparisonResults ; export const ComparisonTypes = [ 'added' , 'removed' ] ; export type ResultTemplate = ( result : Result ) => string ; export type ComparisonResultTemplate = { header : ( type : ComparisonType ) => string ; results : ( result : Result ) => string ; } ; const RESULT_TEMPLATE_PLAINTEXT = ( result ) : string => `Rule: ${ result . rule }
Message: ${ result . message }
Path: ${ result . path }
Link: ${ result . link }

${ result . source }
${ result . error ? `
Error:
${ result . error }
` : '' }` ; const RESULT_TEMPLATE_MARKDOWN = ( result ) : string => `## Rule: ${ result . rule }

-   Message: \`${ result . message . replace ( NEW_LINE_REGEX , ' ' ) }\`
-   Path: \`${ result . path }\`
-   [Link](${ result . link })

\`\`\`${ result . extension || '' }
${ result . source || '' }
\`\`\`
${ result . error ? `
\`\`\`
${ result . error }
\`\`\`
` : '' }` ; export const RESULTS_TEMPLATE_CI_BASE = ( result ) : string => `Repository: ${ result . repositoryOwner }/${ result . repository }` ; export const RESULT_PARSER_TO_TEMPLATE = { plaintext : RESULT_TEMPLATE_PLAINTEXT , markdown : RESULT_TEMPLATE_MARKDOWN , } as const ; const COMPARISON_RESULT_HEADER_PLAINTEXT = ( type ) => `${ upperCaseFirstLetter ( type ) }:` ; const COMPARISON_RESULT_HEADER_MARKDOWN = ( type ) => `# ${ upperCaseFirstLetter ( type ) }:` ; export const RESULT_PARSER_TO_COMPARE_TEMPLATE = { plaintext : { header : COMPARISON_RESULT_HEADER_PLAINTEXT , results : RESULT_TEMPLATE_PLAINTEXT , } , markdown : { header : COMPARISON_RESULT_HEADER_MARKDOWN , results : RESULT_TEMPLATE_MARKDOWN , } , } as const ; export const RESULT_PARSER_TO_EXTENSION = { plaintext : '' , markdown : '.md' , } as const ; function upperCaseFirstLetter ( text ) { const [ firstLetter , ... letters ] = text ; return [ firstLetter . toUpperCase ( ) , ... letters ] . join ( '' ) ; }