-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lint): add
noImgElement
rule (#4155)
- Loading branch information
1 parent
0398828
commit 0d8cc8f
Showing
12 changed files
with
295 additions
and
59 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
137 changes: 78 additions & 59 deletions
137
crates/biome_configuration/src/analyzer/linter/rules.rs
Large diffs are not rendered by default.
Oops, something went wrong.
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
113 changes: 113 additions & 0 deletions
113
crates/biome_js_analyze/src/lint/nursery/no_img_element.rs
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,113 @@ | ||
use biome_analyze::RuleSourceKind; | ||
use biome_analyze::{ | ||
context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic, RuleSource, | ||
}; | ||
use biome_console::markup; | ||
use biome_js_syntax::jsx_ext::AnyJsxElement; | ||
use biome_js_syntax::{JsxChildList, JsxElement}; | ||
use biome_rowan::{AstNode, AstNodeList}; | ||
|
||
declare_lint_rule! { | ||
/// Prevent usage of `<img>` element in a Next.js project. | ||
/// | ||
/// Using the `<img>` element can result in slower Largest Contentful Paint (LCP) | ||
/// and higher bandwidth usage, as it lacks the optimizations provided by the `<Image />` | ||
/// component from `next/image`. Next.js's `<Image />` automatically optimizes images | ||
/// by serving responsive sizes and using modern formats, improving performance and reducing bandwidth. | ||
/// | ||
/// If your project is self-hosted, ensure that you have sufficient storage and have | ||
/// installed the `sharp` package to support optimized images. When deploying to managed | ||
/// hosting providers, be aware of potential additional costs or usage. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <img alt="Foo" /> | ||
/// ``` | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <div> | ||
/// <img alt="Foo" /> | ||
/// </div> | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```jsx | ||
/// <img /> | ||
/// ``` | ||
/// | ||
/// ```jsx | ||
/// <Image src="https://example.com/hero.jpg" /> | ||
/// ``` | ||
/// | ||
/// ```jsx | ||
/// <picture> | ||
/// <source srcSet="https://example.com/hero.avif" type="image/avif" /> | ||
/// <source srcSet="https://example.com/hero.webp" type="image/webp" /> | ||
/// <img src="https://example.com/hero.jpg" /> | ||
/// </picture> | ||
/// ``` | ||
/// | ||
pub NoImgElement { | ||
version: "next", | ||
name: "noImgElement", | ||
language: "jsx", | ||
sources: &[RuleSource::EslintNext("no-img-element")], | ||
source_kind: RuleSourceKind::SameLogic, | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for NoImgElement { | ||
type Query = Ast<AnyJsxElement>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
|
||
if node.name().ok()?.name_value_token()?.text_trimmed() != "img" | ||
|| node.attributes().is_empty() | ||
{ | ||
return None; | ||
} | ||
|
||
if let AnyJsxElement::JsxSelfClosingElement(jsx) = &node { | ||
let Some(parent) = jsx.parent::<JsxChildList>() else { | ||
return Some(()); | ||
}; | ||
let Some(parent) = parent.parent::<JsxElement>() else { | ||
return Some(()); | ||
}; | ||
let Some(opening_element) = parent.opening_element().ok() else { | ||
return Some(()); | ||
}; | ||
let name = opening_element.name().ok()?.name_value_token()?; | ||
|
||
if name.text_trimmed() == "picture" { | ||
return None; | ||
} | ||
} | ||
|
||
Some(()) | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> { | ||
return Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
ctx.query().range(), | ||
markup! { | ||
"Don't use "<Emphasis>"<img>"</Emphasis>" element." | ||
}, | ||
) | ||
.note(markup! { | ||
"Using the "<Emphasis>"<img>"</Emphasis>" can lead to slower LCP and higher bandwidth. Consider using "<Emphasis>"<Image />"</Emphasis>" from "<Emphasis>"next/image"</Emphasis>" to automatically optimize images." | ||
}) | ||
); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
crates/biome_js_analyze/tests/specs/nursery/noImgElement/invalid.jsx
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,7 @@ | ||
<> | ||
<img alt="Foo" /> | ||
|
||
<div> | ||
<img alt="Foo" /> | ||
</div> | ||
</> |
49 changes: 49 additions & 0 deletions
49
crates/biome_js_analyze/tests/specs/nursery/noImgElement/invalid.jsx.snap
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,49 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
assertion_line: 86 | ||
expression: invalid.jsx | ||
--- | ||
# Input | ||
```jsx | ||
<> | ||
<img alt="Foo" /> | ||
|
||
<div> | ||
<img alt="Foo" /> | ||
</div> | ||
</> | ||
|
||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.jsx:2:2 lint/nursery/noImgElement ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Don't use <img> element. | ||
1 │ <> | ||
> 2 │ <img alt="Foo" /> | ||
│ ^^^^^^^^^^^^^^^^^ | ||
3 │ | ||
4 │ <div> | ||
i Using the <img> can lead to slower LCP and higher bandwidth. Consider using <Image /> from next/image to automatically optimize images. | ||
``` | ||
|
||
``` | ||
invalid.jsx:5:3 lint/nursery/noImgElement ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Don't use <img> element. | ||
4 │ <div> | ||
> 5 │ <img alt="Foo" /> | ||
│ ^^^^^^^^^^^^^^^^^ | ||
6 │ </div> | ||
7 │ </> | ||
i Using the <img> can lead to slower LCP and higher bandwidth. Consider using <Image /> from next/image to automatically optimize images. | ||
``` |
7 changes: 7 additions & 0 deletions
7
crates/biome_js_analyze/tests/specs/nursery/noImgElement/valid.jsx
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,7 @@ | ||
<> | ||
<img /> | ||
|
||
<picture> | ||
<img alt="Foo" /> | ||
</picture> | ||
</> |
16 changes: 16 additions & 0 deletions
16
crates/biome_js_analyze/tests/specs/nursery/noImgElement/valid.jsx.snap
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,16 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
assertion_line: 86 | ||
expression: valid.jsx | ||
--- | ||
# Input | ||
```jsx | ||
<> | ||
<img /> | ||
|
||
<picture> | ||
<img alt="Foo" /> | ||
</picture> | ||
</> | ||
|
||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.