Skip to content

Commit

Permalink
fix(eslint): allow <img> in conjunction with <picture> (vercel#37504)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreistefanwork committed Jun 9, 2022
1 parent 0bfdf0e commit ac706a0
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
19 changes: 18 additions & 1 deletion errors/no-img-element.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Why This Error Occurred

An HTML `<img>` element was used to display an image. For better performance and automatic Image Optimization, use Next.js' built-in image component instead.
An HTML `<img>` element was used to display an image. Use either `<picture>` in conjunction with `<img>` element, or use Next.js' built-in image component that has better performance and automatic Image Optimization over `<img>`.

### Possible Ways to Fix It

Expand All @@ -27,6 +27,23 @@ function Home() {
export default Home
```

<br />

Use `<picture>` in conjunction with `<img>` element:

```jsx
function Home() {
return (
<>
<picture>
<source srcSet="https://example.com/test" type="image/webp" />
<img src="https://example.com/test" alt="Landscape picture" />
</picture>
</>
)
}
```

### Useful Links

- [Image Component and Image Optimization](https://nextjs.org/docs/basic-features/image-optimization)
8 changes: 7 additions & 1 deletion packages/eslint-plugin-next/lib/rules/no-img-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ module.exports = {
return
}

if (node.parent?.parent?.openingElement?.name?.name === 'picture') {
return
}

context.report({
node,
message: `Do not use <img>. Use Image from 'next/image' instead. See: https://nextjs.org/docs/messages/no-img-element`,
message:
"Do not use <img>. Use either Image from 'next/image' or <picture> in conjunction with <img>. " +
'See: https://nextjs.org/docs/messages/no-img-element',
})
},
}
Expand Down
56 changes: 55 additions & 1 deletion test/unit/eslint-plugin-next/no-img-element.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,36 @@ ruleTester.run('no-img-element', rule, {
);
}
}`,
`export class MyComponent {
render() {
return (
<picture>
<img
src="/test.png"
alt="Test picture"
width={500}
height={500}
/>
</picture>
);
}
}`,
`export class MyComponent {
render() {
return (
<div>
<picture>
<source media="(min-width:650px)" srcset="/test.jpg"/>
<img
src="/test.png"
alt="Test picture"
style="width:auto;"
/>
</picture>
</div>
);
}
}`,
],
invalid: [
{
Expand All @@ -51,7 +81,31 @@ ruleTester.run('no-img-element', rule, {
errors: [
{
message:
"Do not use <img>. Use Image from 'next/image' instead. See: https://nextjs.org/docs/messages/no-img-element",
"Do not use <img>. Use either Image from 'next/image' or <picture> in conjunction with <img>. " +
'See: https://nextjs.org/docs/messages/no-img-element',
type: 'JSXOpeningElement',
},
],
},
{
code: `
export class MyComponent {
render() {
return (
<img
src="/test.png"
alt="Test picture"
width={500}
height={500}
/>
);
}
}`,
errors: [
{
message:
"Do not use <img>. Use either Image from 'next/image' or <picture> in conjunction with <img>. " +
'See: https://nextjs.org/docs/messages/no-img-element',
type: 'JSXOpeningElement',
},
],
Expand Down

0 comments on commit ac706a0

Please sign in to comment.