@@ -19,6 +19,7 @@ import path from 'path'
19
19
import { program } from 'commander'
20
20
import chalk from 'chalk'
21
21
import cheerio from 'cheerio'
22
+ // @ts -ignore see https://github.com/sindresorhus/file-type/issues/652
22
23
import { fileTypeFromFile } from 'file-type'
23
24
import walk from 'walk-sync'
24
25
import isSVG from 'is-svg'
@@ -43,7 +44,7 @@ const EXPECT = {
43
44
'.ico' : 'image/x-icon' ,
44
45
'.pdf' : 'application/pdf' ,
45
46
'.webp' : 'image/webp' ,
46
- }
47
+ } as Record < string , string >
47
48
48
49
const CRITICAL = 'critical'
49
50
const WARNING = 'warning'
@@ -56,7 +57,7 @@ program
56
57
57
58
main ( program . opts ( ) )
58
59
59
- async function main ( opts ) {
60
+ async function main ( opts : { dryRun: boolean ; verbose: boolean } ) {
60
61
let errors = 0
61
62
62
63
const files = walk ( ASSETS_ROOT , { includeBasePath : true , directories : false } ) . filter (
@@ -71,7 +72,11 @@ async function main(opts) {
71
72
)
72
73
} ,
73
74
)
74
- const results = ( await Promise . all ( files . map ( checkFile ) ) ) . filter ( Boolean )
75
+ const results = ( await Promise . all ( files . map ( checkFile ) ) ) . filter ( Boolean ) as [
76
+ level : string ,
77
+ filePath : string ,
78
+ error : string ,
79
+ ] [ ]
75
80
for ( const [ level , filePath , error ] of results ) {
76
81
console . log (
77
82
level === CRITICAL ? chalk . red ( level ) : chalk . yellow ( level ) ,
@@ -94,7 +99,7 @@ async function main(opts) {
94
99
process . exitCode = errors
95
100
}
96
101
97
- async function checkFile ( filePath ) {
102
+ async function checkFile ( filePath : string ) {
98
103
const ext = path . extname ( filePath )
99
104
100
105
const { size } = await fs . stat ( filePath )
@@ -113,7 +118,7 @@ async function checkFile(filePath) {
113
118
}
114
119
try {
115
120
checkSVGContent ( content )
116
- } catch ( error ) {
121
+ } catch ( error : any ) {
117
122
return [ CRITICAL , filePath , error . message ]
118
123
}
119
124
} else if ( EXPECT [ ext ] ) {
@@ -135,15 +140,15 @@ async function checkFile(filePath) {
135
140
// All is well. Nothing to complain about.
136
141
}
137
142
138
- function checkSVGContent ( content ) {
143
+ function checkSVGContent ( content : string ) {
139
144
const $ = cheerio . load ( content )
140
145
const disallowedTagNames = new Set ( [ 'script' , 'object' , 'iframe' , 'embed' ] )
141
146
$ ( '*' ) . each ( ( i , element ) => {
142
- const { tagName } = element
147
+ const { tagName } = $ ( element ) . get ( 0 )
143
148
if ( disallowedTagNames . has ( tagName ) ) {
144
149
throw new Error ( `contains a <${ tagName } > tag` )
145
150
}
146
- for ( const key in element . attribs ) {
151
+ for ( const key in $ ( element ) . get ( 0 ) . attribs ) {
147
152
// Looks for suspicious event handlers on tags.
148
153
// For example `<path oNload="alert(1)"" d="M28 0l4.59 4.59-9.76`
149
154
// We don't need to do a case-sensitive regex here because cheerio
0 commit comments