-
-
Notifications
You must be signed in to change notification settings - Fork 487
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
no-img-element
from eslint-plugin-next
- Loading branch information
1 parent
35bb699
commit 4e10665
Showing
12 changed files
with
323 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
137 changes: 137 additions & 0 deletions
137
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,137 @@ | ||
use biome_analyze::RuleSourceKind; | ||
use biome_analyze::{ | ||
context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic, RuleSource, | ||
}; | ||
use biome_console::markup; | ||
use biome_js_syntax::{ | ||
JsSyntaxToken, JsxAttributeList, JsxChildList, JsxElement, JsxOpeningElement, | ||
JsxSelfClosingElement, | ||
}; | ||
use biome_rowan::{declare_node_union, AstNode, AstNodeList, TextRange}; | ||
|
||
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. | ||
/// | ||
/// ## 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, | ||
} | ||
} | ||
|
||
declare_node_union! { | ||
pub NoImgElementQuery = JsxOpeningElement | JsxSelfClosingElement | ||
} | ||
|
||
impl Rule for NoImgElement { | ||
type Query = Ast<NoImgElementQuery>; | ||
type State = TextRange; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
node.is_target_valid()?.then(|| node.range()) | ||
} | ||
|
||
fn diagnostic(_: &RuleContext<Self>, range: &Self::State) -> Option<RuleDiagnostic> { | ||
return Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
range, | ||
markup! { | ||
"Using "<Emphasis>"<img>"</Emphasis>" could result in slower LCP and higher bandwidth." | ||
}, | ||
) | ||
.note(markup! { "Consider using "<Emphasis>"<Image />"</Emphasis>" from "<Emphasis>"next/image"</Emphasis>" to automatically optimize images." }) | ||
.note(markup! { "This may incur additional usage or cost from your provider." }) | ||
); | ||
} | ||
} | ||
|
||
impl NoImgElementQuery { | ||
fn range(&self) -> TextRange { | ||
match self { | ||
NoImgElementQuery::JsxOpeningElement(jsx) => jsx.range(), | ||
NoImgElementQuery::JsxSelfClosingElement(jsx) => jsx.range(), | ||
} | ||
} | ||
|
||
fn name(&self) -> Option<JsSyntaxToken> { | ||
match self { | ||
NoImgElementQuery::JsxOpeningElement(jsx) => jsx.name().ok()?.name_value_token(), | ||
NoImgElementQuery::JsxSelfClosingElement(jsx) => jsx.name().ok()?.name_value_token(), | ||
} | ||
} | ||
|
||
fn attributes(&self) -> JsxAttributeList { | ||
match self { | ||
NoImgElementQuery::JsxOpeningElement(jsx) => jsx.attributes(), | ||
NoImgElementQuery::JsxSelfClosingElement(jsx) => jsx.attributes(), | ||
} | ||
} | ||
|
||
fn get_grandparent(&self) -> Option<JsxOpeningElement> { | ||
if let NoImgElementQuery::JsxSelfClosingElement(jsx) = self { | ||
return jsx | ||
.parent::<JsxChildList>()? | ||
.parent::<JsxElement>()? | ||
.opening_element() | ||
.ok(); | ||
} | ||
None | ||
} | ||
|
||
fn is_target_valid(&self) -> Option<bool> { | ||
if self.name()?.text_trimmed() != "img" || self.attributes().is_empty() { | ||
return Some(false); | ||
} | ||
|
||
if let Some(grandparent) = self.get_grandparent() { | ||
let name = grandparent.name().ok()?.name_value_token(); | ||
return Some(name.map_or(true, |name| name.text_trimmed() != "picture")); | ||
} | ||
|
||
Some(true) | ||
} | ||
} |
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> | ||
</> |
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
--- | ||
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 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Using <img> could result in slower LCP and higher bandwidth. | ||
1 │ <> | ||
> 2 │ <img alt="Foo" /> | ||
│ ^^^^^^^^^^^^^^^^^ | ||
3 │ | ||
4 │ <div> | ||
i Consider using <Image /> from next/image to automatically optimize images. | ||
i This may incur additional usage or cost from your provider. | ||
``` | ||
|
||
``` | ||
invalid.jsx:5:3 lint/nursery/noImgElement ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Using <img> could result in slower LCP and higher bandwidth. | ||
4 │ <div> | ||
> 5 │ <img alt="Foo" /> | ||
│ ^^^^^^^^^^^^^^^^^ | ||
6 │ </div> | ||
7 │ </> | ||
i Consider using <Image /> from next/image to automatically optimize images. | ||
i This may incur additional usage or cost from your provider. | ||
``` |
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.