diff --git a/errors/no-img-element.md b/errors/no-img-element.md index db4d31f363f26..1e4d9f651442a 100644 --- a/errors/no-img-element.md +++ b/errors/no-img-element.md @@ -4,7 +4,7 @@ ### Why This Error Occurred -An `` element was used to display an image. For better performance and automatic Image Optimization, use `next/image` instead. +An `` element was used to display an image. Use either `` in conjunction with `` element, or use `next/image` that has better performance and automatic Image Optimization over ``. ### Possible Ways to Fix It @@ -29,6 +29,23 @@ function Home() { export default Home ``` +
+ +Use `` in conjunction with `` element: + +```jsx +function Home() { + return ( + <> + + + Landscape picture + + + ) +} +``` + ### Useful Links - [Image Component and Image Optimization](https://nextjs.org/docs/basic-features/image-optimization) diff --git a/packages/eslint-plugin-next/lib/rules/no-img-element.js b/packages/eslint-plugin-next/lib/rules/no-img-element.js index a9fcbf2bb1c0e..8724474ce3b9d 100644 --- a/packages/eslint-plugin-next/lib/rules/no-img-element.js +++ b/packages/eslint-plugin-next/lib/rules/no-img-element.js @@ -22,6 +22,10 @@ module.exports = { return } + if (node.parent?.parent?.openingElement?.name?.name === 'picture') { + return + } + context.report({ node, message: `Do not use \`\` element. Use \`\` from \`next/image\` instead. See: ${url}`, diff --git a/test/unit/eslint-plugin-next/no-img-element.test.ts b/test/unit/eslint-plugin-next/no-img-element.test.ts index e9d255a64d5dc..18efcff88a4da 100644 --- a/test/unit/eslint-plugin-next/no-img-element.test.ts +++ b/test/unit/eslint-plugin-next/no-img-element.test.ts @@ -30,6 +30,36 @@ ruleTester.run('no-img-element', rule, { ); } }`, + `export class MyComponent { + render() { + return ( + + Test picture + + ); + } + }`, + `export class MyComponent { + render() { + return ( +
+ + + Test picture + +
+ ); + } + }`, ], invalid: [ { @@ -51,7 +81,31 @@ ruleTester.run('no-img-element', rule, { errors: [ { message: - 'Do not use `` element. Use `` from `next/image` instead. See: https://nextjs.org/docs/messages/no-img-element', + 'Do not use `` element. Use `` from `next/image` instead. ' + + 'See: https://nextjs.org/docs/messages/no-img-element', + type: 'JSXOpeningElement', + }, + ], + }, + { + code: ` + export class MyComponent { + render() { + return ( + Test picture + ); + } + }`, + errors: [ + { + message: + 'Do not use `` element. Use `` from `next/image` instead. ' + + 'See: https://nextjs.org/docs/messages/no-img-element', type: 'JSXOpeningElement', }, ],