-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a new image rule to `eslint-plugin-next`: ``` Do not use `<img>`. Use Image from `next/image` instead ```
- Loading branch information
1 parent
d746db6
commit 7426ebc
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# No Img Element | ||
|
||
### 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. | ||
|
||
### Possible Ways to Fix It | ||
|
||
Import and use the `<Image />` component: | ||
|
||
```jsx | ||
import { Image } from 'next/image' | ||
|
||
function Home() { | ||
return ( | ||
<> | ||
<Image | ||
src="https://example.com/test" | ||
alt="Landscape picture" | ||
width={500} | ||
height={500} | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
export default Home | ||
``` | ||
|
||
### Useful Links | ||
|
||
- [Image Component and Image Optimization](https://nextjs.org/docs/basic-features/image-optimization) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Prohibit usage of HTML <img> element', | ||
category: 'HTML', | ||
recommended: true, | ||
}, | ||
fixable: 'code', | ||
}, | ||
|
||
create: function (context) { | ||
return { | ||
JSXOpeningElement(node) { | ||
if (node.name.name !== 'img') { | ||
return | ||
} | ||
|
||
if (node.attributes.length === 0) { | ||
return | ||
} | ||
|
||
context.report({ | ||
node, | ||
message: `Do not use <img>. Use Image from 'next/image' instead. See https://nextjs.org/docs/messages/no-img-element.`, | ||
}) | ||
}, | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const rule = require('@next/eslint-plugin-next/lib/rules/no-img-element') | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
|
||
RuleTester.setDefaultConfig({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
modules: true, | ||
jsx: true, | ||
}, | ||
}, | ||
}) | ||
|
||
var ruleTester = new RuleTester() | ||
ruleTester.run('no-img-element', rule, { | ||
valid: [ | ||
`import { Image } from 'next/image'; | ||
export class MyComponent { | ||
render() { | ||
return ( | ||
<div> | ||
<Image | ||
src="/test.png" | ||
alt="Test picture" | ||
width={500} | ||
height={500} | ||
/> | ||
</div> | ||
); | ||
} | ||
}`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
export class MyComponent { | ||
render() { | ||
return ( | ||
<div> | ||
<img | ||
src="/test.png" | ||
alt="Test picture" | ||
width={500} | ||
height={500} | ||
/> | ||
</div> | ||
); | ||
} | ||
}`, | ||
errors: [ | ||
{ | ||
message: | ||
"Do not use <img>. Use Image from 'next/image' instead. See https://nextjs.org/docs/messages/no-img-element.", | ||
type: 'JSXOpeningElement', | ||
}, | ||
], | ||
}, | ||
], | ||
}) |