fileoverview import { HintContext } from 'hint/dist/src/lib/hint-context' ; import { IHint } from 'hint/dist/src/lib/types' ; import { debug as d } from '@hint/utils-debug' ; import { Severity } from '@hint/utils-types' ; import { ScriptEvents , ScriptParse } from '@hint/parser-javascript' ; import meta from './meta' ; import { getMessage } from './i18n.import' ; const debug = d ( __filename ) ; export default class MinifiedJsHint implements IHint { public static readonly meta = meta ; public constructor ( context ) { let threshold = 75 ; if ( context . hintOptions && context . hintOptions . threshold ) { threshold = context . hintOptions . threshold ; } const getImprovementIndex = ( { sourceCode , tokens } ) => { const contentLength = sourceCode . length ; const tokenRatio = tokens . length / contentLength ; return Math . round ( ( 1 - tokenRatio ) * 100 ) ; } ; const validateContentMinified = ( scriptData ) => { const { element , resource , sourceCode } = scriptData ; const improvementIndex = getImprovementIndex ( scriptData ) ; if ( sourceCode . length < 1024 ) { debug ( `Ignoring minification for script under 1KB: ${ resource }` ) ; return ; } debug ( `Calculated improvementIndex for ${ resource }: ${ improvementIndex }` ) ; if ( improvementIndex > threshold ) { context . report ( resource , getMessage ( 'shouldBeMinified' , context . language ) , { element , severity : Severity . warning } ) ; } } ; context . on ( 'parse::end::javascript' , validateContentMinified ) ; } }