Skip to content

Commit

Permalink
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
Browse files Browse the repository at this point in the history
Signed-off-by: Devansu <devansuyadav@gmail.com>
Devansu-Yadav committed Mar 25, 2023
1 parent e983f87 commit 686311b
Showing 5 changed files with 105 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -376,6 +376,7 @@ module.exports = {
'@typescript-eslint/prefer-optional-chain': ERROR,
'@docusaurus/no-html-links': ERROR,
'@docusaurus/prefer-docusaurus-heading': ERROR,
'@docusaurus/prefer-ideal-image': ERROR,
'@docusaurus/no-untranslated-text': [
WARNING,
{
@@ -505,5 +506,14 @@ module.exports = {
'@docusaurus/prefer-docusaurus-heading': OFF,
},
},
{
files: [
'packages/docusaurus-plugin-debug/**',
'packages/docusaurus/src/**',
],
rules: {
'@docusaurus/prefer-ideal-image': OFF,
},
},
],
};
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ export = {
'@docusaurus/string-literal-i18n-messages': 'error',
'@docusaurus/no-html-links': 'warn',
'@docusaurus/prefer-docusaurus-heading': 'warn',
'@docusaurus/prefer-ideal-image': 'warn',
},
},
all: {
@@ -25,6 +26,7 @@ export = {
'@docusaurus/no-untranslated-text': 'warn',
'@docusaurus/no-html-links': 'warn',
'@docusaurus/prefer-docusaurus-heading': 'warn',
'@docusaurus/prefer-ideal-image': 'warn',
},
},
},
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,
},
],
});
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/rules/index.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@

import noHtmlLinks from './no-html-links';
import preferDocusaurusHeading from './prefer-docusaurus-heading';
import preferIdealImage from './prefer-ideal-image';
import noUntranslatedText from './no-untranslated-text';
import stringLiteralI18nMessages from './string-literal-i18n-messages';

@@ -15,4 +16,5 @@ export default {
'string-literal-i18n-messages': stringLiteralI18nMessages,
'no-html-links': noHtmlLinks,
'prefer-docusaurus-heading': preferDocusaurusHeading,
'prefer-ideal-image': preferIdealImage,
};
43 changes: 43 additions & 0 deletions packages/eslint-plugin/src/rules/prefer-ideal-image.ts
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'});
},
};
},
});

0 comments on commit 686311b

Please sign in to comment.