Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint): allow <img> in conjunction with <picture> (#37504) #37570

Merged
merged 4 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
4 changes: 4 additions & 0 deletions packages/eslint-plugin-next/lib/rules/no-img-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ 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}`,
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 `<Image />` from `next/image` instead. ' +
'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 `<Image />` from `next/image` instead. ' +
'See: https://nextjs.org/docs/messages/no-img-element',
type: 'JSXOpeningElement',
},
],
Expand Down