-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from elisecodedestrucs/image-src-attribute-not…
…-empty add rule image-src-attribute-not-empty
- Loading branch information
Showing
6 changed files
with
159 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
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,26 @@ | ||
# Disallow usage of image with empty source attribute (`@ecocode/no-empty-image-src-attribute`) | ||
|
||
⚠️ This rule _warns_ in the ✅ `recommended` config. | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
## Rule Details | ||
|
||
This rule aims to prohibit the usage of <img/> without specifying its source attribute because it causes extra and unnecessary http requests. This rule is build for the [react library](https://react.dev/) and JSX. | ||
|
||
## Examples | ||
|
||
Examples of **non compliant** code for this rule: | ||
|
||
```js | ||
<img src='' /> | ||
<img/> | ||
``` | ||
|
||
Examples of **compliant** code for this rule: | ||
|
||
```js | ||
import imgSvg from './img.svg' | ||
<img src='./img.svg' /> | ||
<img src={imgSvg} /> | ||
``` |
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,58 @@ | ||
/** | ||
* Copyright (C) 2023 Green Code Initiative | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
"use strict"; | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: "suggestion", | ||
docs: { | ||
description: "Disallow usage of image with empty source attribute", | ||
category: "eco-design", | ||
recommended: "warn", | ||
}, | ||
messages: { | ||
SpecifySrcAttribute: | ||
"Make sure to specify src attribute when using <img/>.", | ||
}, | ||
schema: [], | ||
}, | ||
create(context) { | ||
return { | ||
JSXOpeningElement(node) { | ||
if (node.name.name === "img") { | ||
const srcValue = node.attributes.find( | ||
(attr) => attr.name.name === "src", | ||
); | ||
if (srcValue && srcValue.value && srcValue.value.value === "") { | ||
//to prevent <img src='' alt='Empty image'/> | ||
context.report({ | ||
node: srcValue, | ||
messageId: "SpecifySrcAttribute", | ||
}); | ||
} else if (!srcValue) { | ||
//to prevent <img /> | ||
context.report({ | ||
node, | ||
messageId: "SpecifySrcAttribute", | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions
73
eslint-plugin/tests/lib/rules/no-empty-image-src-attribute.js
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,73 @@ | ||
/** | ||
* Copyright (C) 2023 Green Code Initiative | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/no-empty-image-src-attribute"); | ||
const RuleTester = require("eslint").RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2021, | ||
sourceType: "module", | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}); | ||
const expectedError1 = { | ||
messageId: "SpecifySrcAttribute", | ||
type: "JSXAttribute", | ||
}; | ||
const expectedError2 = { | ||
messageId: "SpecifySrcAttribute", | ||
type: "JSXOpeningElement", | ||
}; | ||
|
||
ruleTester.run("image-src-attribute-not-empty", rule, { | ||
valid: [ | ||
` | ||
<img src='logo.svg' alt='This is a SVG image'/> | ||
`, | ||
` | ||
import logoSvg from "../files/logo.svg"; | ||
<img src={logoSvg} alt='This is a SVG image'/> | ||
`, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: ` | ||
<img src=''/> | ||
`, | ||
errors: [expectedError1], | ||
}, | ||
{ | ||
code: ` | ||
<img alt='This is an empty image'/> | ||
`, | ||
errors: [expectedError2], | ||
}, | ||
], | ||
}); |