1
1
// W3C HTML Validator ~ MIT License
2
2
3
3
// Imports
4
- import chalk from 'chalk' ;
4
+ import chalk , { ChalkInstance } from 'chalk' ;
5
5
import fs from 'fs' ;
6
6
import log from 'fancy-log' ;
7
7
import request from 'superagent' ;
@@ -28,7 +28,7 @@ export type ValidatorResultsMessage = {
28
28
type : 'info' | 'error' | 'non-document-error' | 'network-error' ,
29
29
subType ?: 'warning' | 'fatal' | 'io' | 'schema' | 'internal' ,
30
30
message : string , //example: 'Section lacks heading.'
31
- extract : string , //example: '<section>Hi</section>'
31
+ extract ?: string , //example: '<section>Hi</section>'
32
32
lastLine : number ,
33
33
firstColumn : number ,
34
34
lastColumn : number ,
@@ -73,11 +73,11 @@ const w3cHtmlValidator = {
73
73
} ;
74
74
const settings = { ...defaults , ...options } ;
75
75
if ( ! settings . html && ! settings . filename && ! settings . website )
76
- throw Error ( '[w3c-html-validator] Must specify the "html", "filename", or "website" option.' ) ;
76
+ throw new Error ( '[w3c-html-validator] Must specify the "html", "filename", or "website" option.' ) ;
77
77
if ( ! [ null , 'info' , 'warning' ] . includes ( settings . ignoreLevel ) )
78
- throw Error ( ' [w3c-html-validator] Invalid ignoreLevel option: ' + settings . ignoreLevel ) ;
78
+ throw new Error ( ` [w3c-html-validator] Invalid ignoreLevel option: ${ settings . ignoreLevel } ` ) ;
79
79
if ( settings . output !== 'json' && settings . output !== 'html' )
80
- throw Error ( '[w3c-html-validator] Option "output" must be "json" or "html".' ) ;
80
+ throw new Error ( '[w3c-html-validator] Option "output" must be "json" or "html".' ) ;
81
81
const filename = settings . filename ? slash ( settings . filename ) : null ;
82
82
const mode = settings . html ? 'html' : filename ? 'filename' : 'website' ;
83
83
const readFile = ( filename : string ) => fs . readFileSync ( filename , 'utf-8' ) . replace ( / \r / g, '' ) ;
@@ -93,7 +93,7 @@ const w3cHtmlValidator = {
93
93
const json = settings . output === 'json' ;
94
94
const success = '<p class="success">' ;
95
95
const titleLookup = {
96
- html : ' HTML String (characters: ' + inputHtml ?. length + ')' ,
96
+ html : ` HTML String (characters: ${ inputHtml ?. length } )` ,
97
97
filename : filename ,
98
98
website : settings . website ,
99
99
} ;
@@ -108,28 +108,28 @@ const w3cHtmlValidator = {
108
108
const isImportant = ( message : ValidatorResultsMessage ) : boolean =>
109
109
aboveIgnoreLevel ( message ) && ! matchesSkipPattern ( message . message ) ;
110
110
if ( json )
111
- response . body . messages = response . body . messages ?. filter ( isImportant ) ?? [ ] ;
111
+ response . body . messages = response . body . messages ?. filter ( isImportant ) ?? [ ] ; //eslint-disable-line
112
112
return response ;
113
113
} ;
114
114
const toValidatorResults = ( response : request . Response ) : ValidatorResults => ( {
115
- validates : json ? ! response . body . messages . length : ! ! response . text ?. includes ( success ) ,
115
+ validates : json ? ! response . body . messages . length : ! ! response . text ?. includes ( success ) , //eslint-disable-line
116
116
mode : mode ,
117
117
title : < string > titleLookup [ mode ] ,
118
118
html : inputHtml ,
119
119
filename : filename ,
120
120
website : settings . website || null ,
121
121
output : < ValidatorResultsOutput > settings . output ,
122
122
status : response . statusCode || - 1 ,
123
- messages : json ? response . body . messages : null ,
123
+ messages : json ? response . body . messages : null , //eslint-disable-line
124
124
display : json ? null : response . text ,
125
125
dryRun : settings . dryRun ,
126
126
} ) ;
127
127
type ReasonResponse = { request : { url : string } , res : { statusMessage : string } } ;
128
128
type ReasonError = Error & { errno : number , response : request . Response & ReasonResponse } ;
129
129
const handleError = ( reason : ReasonError ) : ValidatorResults => {
130
- const errRes = reason . response ?? < ReasonError [ 'response' ] > { } ;
130
+ const errRes = reason . response ?? < ReasonError [ 'response' ] > { } ; //eslint-disable-line
131
131
const getMsg = ( ) => [ errRes . status , errRes . res . statusMessage , errRes . request . url ] ;
132
- const message = reason . response ? getMsg ( ) : [ reason . errno , reason . message ] ;
132
+ const message = reason . response ? getMsg ( ) : [ reason . errno , reason . message ] ; //eslint-disable-line
133
133
errRes . body = { messages : [ { type : 'network-error' , message : message . join ( ' ' ) } ] } ;
134
134
return toValidatorResults ( errRes ) ;
135
135
} ;
@@ -150,7 +150,7 @@ const w3cHtmlValidator = {
150
150
} ,
151
151
152
152
summary ( numFiles : number ) {
153
- log ( chalk . gray ( 'w3c-html-validator' ) , chalk . magenta ( 'files: ' + numFiles ) ) ;
153
+ log ( chalk . gray ( 'w3c-html-validator' ) , chalk . magenta ( 'files: ' + String ( numFiles ) ) ) ;
154
154
} ,
155
155
156
156
reporter ( results : ValidatorResults , options ?: Partial < ReporterSettings > ) : ValidatorResults {
@@ -161,21 +161,21 @@ const w3cHtmlValidator = {
161
161
title : null ,
162
162
} ;
163
163
const settings = { ...defaults , ...options } ;
164
- if ( typeof results ?. validates !== 'boolean' )
165
- throw Error ( '[w3c-html-validator] Invalid results for reporter(): ' + String ( results ) ) ;
164
+ if ( typeof results ?. validates !== 'boolean' ) //eslint-disable-line
165
+ throw new Error ( '[w3c-html-validator] Invalid results for reporter(): ' + String ( results ) ) ;
166
166
const messages = results . messages ?? [ ] ;
167
167
const title = settings . title ?? results . title ;
168
168
const status = results . validates ? chalk . green . bold ( '✔ pass' ) : chalk . red . bold ( '✘ fail' ) ;
169
- const count = results . validates ? '' : ' (messages: ' + messages ! . length + ')' ;
169
+ const count = results . validates ? '' : ` (messages: ${ messages . length } )` ;
170
170
if ( ! results . validates || ! settings . quiet )
171
171
log ( chalk . gray ( 'w3c-html-validator' ) , status , chalk . blue . bold ( title ) , chalk . white ( count ) ) ;
172
- const typeColorMap = {
172
+ const typeColorMap = < { [ messageType : string ] : ChalkInstance } > {
173
173
error : chalk . red . bold ,
174
174
warning : chalk . yellow . bold ,
175
175
info : chalk . white . bold ,
176
176
} ;
177
177
const logMessage = ( message : ValidatorResultsMessage ) => {
178
- const type = < keyof typeof typeColorMap > ( message . subType ?? message . type ) ;
178
+ const type = message . subType ?? message . type ;
179
179
const typeColor = typeColorMap [ type ] ?? chalk . redBright . bold ;
180
180
const location = `line ${ message . lastLine } , column ${ message . firstColumn } :` ;
181
181
const lineText = message . extract ?. replace ( / \n / g, '\\n' ) ;
@@ -190,11 +190,11 @@ const w3cHtmlValidator = {
190
190
const toString = ( message : ValidatorResultsMessage ) =>
191
191
`${ message . subType ?? message . type } line ${ message . lastLine } column ${ message . firstColumn } ` ;
192
192
const fileDetails = ( ) =>
193
- results . filename + ' -- ' + results . messages ! . map ( toString ) . join ( ', ' ) ;
193
+ ` ${ results . filename } -- ${ results . messages ! . map ( toString ) . join ( ', ' ) } ` ;
194
194
return ! results . filename ? results . messages ! [ 0 ] ! . message : fileDetails ( ) ;
195
195
} ;
196
196
if ( ! settings . continueOnFail && ! results . validates )
197
- throw Error ( '[w3c-html-validator] Failed: ' + failDetails ( ) ) ;
197
+ throw new Error ( '[w3c-html-validator] Failed: ' + failDetails ( ) ) ;
198
198
return results ;
199
199
} ,
200
200
0 commit comments