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 15, 2022
1 parent bf7bf82 commit b6d58f9
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 @@ -4,7 +4,7 @@
### Why This Error Occurred

An `<img>` element was used to display an image. For better performance and automatic Image Optimization, use `next/image` instead.
An `<img>` element was used to display an image. Use either `<picture>` in conjunction with `<img>` element, or use `next/image` that has better performance and automatic Image Optimization over `<img>`.

### Possible Ways to Fix It

Expand All @@ -29,6 +29,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 @@ -22,9 +22,15 @@ module.exports = {
return
}

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

context.report({
node,
message: `Do not use \`<img>\` element. Use \`<Image />\` from \`next/image\` instead. See: ${url}`,
message:
'Do not use `<img>` element. Use either Image from `next/image` or `<picture>` in conjunction with `<img>`. ' +
`See: ${url}`,
})
},
}
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>` element. Use `<Image />` from `next/image` instead. See: https://nextjs.org/docs/messages/no-img-element',
'Do not use `<img>` element. 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>` element. 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 b6d58f9

Please sign in to comment.