forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-unsized-images.js
36 lines (34 loc) · 1.16 KB
/
no-unsized-images.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const createImageRule = require('../../utils/imageComponentRule.js')
const NodeAttributes = require('../../utils/nodeAttributes.js')
module.exports = {
meta: {
messages: {
unsizedImages:
'For layout stability, the image component should be used ' +
'with a height and width property, even if actual image size is determined with CSS.' +
'if you cannot provide a correct-ratio height and width, use the "unsized" attribute.' +
'More info: https://web.dev/optimize-cls/#images-without-dimensions',
},
},
create: createImageRule((context, node) => {
if (!imageValidSize(node)) {
context.report({
node,
messageId: 'unsizedImages',
})
}
}),
}
function imageValidSize(node) {
let attributes = new NodeAttributes(node)
// Need to check both hasValue and value to catch attribute without value and
// attribute with empty string for value
return (
attributes.has('unsized') ||
(attributes.has('height') &&
attributes.has('width') &&
attributes.hasValue('height') &&
attributes.hasValue('width') ** attributes.value('height') &&
attributes.value('width'))
)
}