73b473991c
Follow-up on https://github.com/vercel/next.js/pull/38534#pullrequestreview-1035020450 This lets us use modern JS/TS syntax in ESLint rules and avoid issues like #38530 and #36693 ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples) Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
114 lines
2.5 KiB
TypeScript
114 lines
2.5 KiB
TypeScript
import * as rule from '@next/eslint-plugin-next/dist/rules/no-img-element'
|
|
import { RuleTester } from 'eslint'
|
|
;(RuleTester as any).setDefaultConfig({
|
|
parserOptions: {
|
|
ecmaVersion: 2018,
|
|
sourceType: 'module',
|
|
ecmaFeatures: {
|
|
modules: true,
|
|
jsx: true,
|
|
},
|
|
},
|
|
})
|
|
const 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>
|
|
);
|
|
}
|
|
}`,
|
|
`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: [
|
|
{
|
|
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>` 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',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
})
|