-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add test & configure prefer-ideal-image rule
Signed-off-by: Devansu <devansuyadav@gmail.com>
1 parent
e983f87
commit 686311b
Showing
5 changed files
with
105 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
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
48 changes: 48 additions & 0 deletions
48
packages/eslint-plugin/src/rules/__tests__/prefer-ideal-image.test.ts
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,48 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import rule from '../prefer-ideal-image'; | ||
import {RuleTester} from './testUtils'; | ||
|
||
const errorsJSX = [{messageId: 'image'}] as const; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}); | ||
|
||
ruleTester.run('prefer-ideal-image', rule, { | ||
valid: [ | ||
{ | ||
code: "<IdealImage img='./path/to/img.png' />", | ||
}, | ||
{ | ||
code: "<IdealImage img={require('./path/to/img.png')} />", | ||
}, | ||
{ | ||
code: "<IdealImage img={{ src: { src: './path/to/img.png', preSrc: '', images: [{ width: 100 }]}, preSrc: './path/to/placeholder.png'}} />", | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: "<img src='./path/to/img.png' alt='some alt text' />", | ||
errors: errorsJSX, | ||
}, | ||
{ | ||
code: "<img src={require('./path/to/img.png')} alt='some alt text' />", | ||
errors: errorsJSX, | ||
}, | ||
{ | ||
code: "<img src='./path/to/img.png' srcset='./path/to/img-480w.jpg 480w, ./path/to/img-800w.png 800w' sizes='(max-width: 600px) 480px, 800px' alt='some alt text' />", | ||
errors: errorsJSX, | ||
}, | ||
], | ||
}); |
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,43 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {createRule} from '../util'; | ||
import type {TSESTree} from '@typescript-eslint/types/dist/ts-estree'; | ||
|
||
type Options = []; | ||
type MessageIds = 'image'; | ||
|
||
const docsUrl = | ||
'https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-ideal-image'; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
name: 'prefer-ideal-image', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'enforce using Docusaurus IdealImage plugin component instead of <img> tags', | ||
recommended: false, | ||
}, | ||
schema: [], | ||
messages: { | ||
image: `Do not use an \`<img>\` element to embed images. Use the \`<IdealImage />\` component from \`@theme/IdealImage\` instead. See ${docsUrl}`, | ||
}, | ||
}, | ||
defaultOptions: [], | ||
|
||
create(context) { | ||
return { | ||
JSXOpeningElement(node) { | ||
const elementName = (node.name as TSESTree.JSXIdentifier).name; | ||
|
||
console.log(elementName); | ||
context.report({node, messageId: 'image'}); | ||
}, | ||
}; | ||
}, | ||
}); |