-
-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Emanuele Stoppa <[email protected]>
- Loading branch information
1 parent
5b6e55b
commit 35bb699
Showing
16 changed files
with
271 additions
and
64 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
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
89 changes: 89 additions & 0 deletions
89
crates/biome_js_analyze/src/lint/nursery/no_head_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,89 @@ | ||
use biome_analyze::RuleSourceKind; | ||
use biome_analyze::{ | ||
context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic, RuleSource, | ||
}; | ||
use biome_console::markup; | ||
use biome_js_syntax::JsxOpeningElement; | ||
use biome_rowan::AstNode; | ||
use biome_rowan::TextRange; | ||
|
||
declare_lint_rule! { | ||
/// Prevent usage of `<head>` element in a Next.js project. | ||
/// | ||
/// Next.js provides a specialized `<Head />` component from `next/head` that manages | ||
/// the `<head>` tag for optimal server-side rendering, client-side navigation, and | ||
/// automatic deduplication of tags such as `<meta>` and `<title>`. | ||
/// | ||
/// This rule only checks files that are outside of the [`app/` directory](https://nextjs.org/docs/app), as it's typically | ||
/// handled differently in Next.js. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// ```jsx,expect_diagnostic | ||
/// function Index() { | ||
/// return ( | ||
/// <head> | ||
/// <title>Invalid</title> | ||
/// </head> | ||
/// ) | ||
/// } | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```jsx | ||
/// import Head from 'next/head' | ||
/// | ||
/// function Index() { | ||
/// return ( | ||
/// <Head> | ||
/// <title>All good!</title> | ||
/// </Head> | ||
/// ) | ||
/// } | ||
/// ``` | ||
pub NoHeadElement { | ||
version: "next", | ||
name: "noHeadElement", | ||
language: "jsx", | ||
sources: &[RuleSource::EslintNext("no-head-element")], | ||
source_kind: RuleSourceKind::SameLogic, | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for NoHeadElement { | ||
type Query = Ast<JsxOpeningElement>; | ||
type State = TextRange; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let element = ctx.query(); | ||
let name = element.name().ok()?.name_value_token()?; | ||
|
||
if name.text_trimmed() == "head" { | ||
let is_in_app_dir = ctx | ||
.file_path() | ||
.ancestors() | ||
.any(|a| a.file_name().map_or(false, |f| f == "app" && a.is_dir())); | ||
|
||
if !is_in_app_dir { | ||
return Some(element.syntax().text_range()); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
fn diagnostic(_: &RuleContext<Self>, range: &Self::State) -> Option<RuleDiagnostic> { | ||
return Some(RuleDiagnostic::new( | ||
rule_category!(), | ||
range, | ||
markup! { "Don't use "<Emphasis>"<head>"</Emphasis>" element." }, | ||
).note(markup! { | ||
"Using the "<Emphasis>"<head>"</Emphasis>" element can cause unexpected behavior in a Next.js application. Use "<Emphasis>"<Head />"</Emphasis>" from "<Emphasis>"next/head"</Emphasis>" instead." | ||
})); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
crates/biome_js_analyze/tests/specs/nursery/noHeadElement/app/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,3 @@ | ||
<head> | ||
<title>No diagnostic</title> | ||
</head> |
12 changes: 12 additions & 0 deletions
12
crates/biome_js_analyze/tests/specs/nursery/noHeadElement/app/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,12 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
assertion_line: 86 | ||
expression: valid.jsx | ||
--- | ||
# Input | ||
```jsx | ||
<head> | ||
<title>No diagnostic</title> | ||
</head> | ||
|
||
``` |
3 changes: 3 additions & 0 deletions
3
crates/biome_js_analyze/tests/specs/nursery/noHeadElement/pages/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,3 @@ | ||
<head> | ||
<title>Invalid</title> | ||
</head> |
27 changes: 27 additions & 0 deletions
27
crates/biome_js_analyze/tests/specs/nursery/noHeadElement/pages/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,27 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.jsx | ||
--- | ||
# Input | ||
```jsx | ||
<head> | ||
<title>Invalid</title> | ||
</head> | ||
|
||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.jsx:1:1 lint/nursery/noHeadElement ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Don't use <head> element. | ||
> 1 │ <head> | ||
│ ^^^^^^ | ||
2 │ <title>Invalid</title> | ||
3 │ </head> | ||
i Using the <head> element can cause unexpected behavior in a Next.js application. Use <Head /> from next/head instead. | ||
``` |
4 changes: 4 additions & 0 deletions
4
crates/biome_js_analyze/tests/specs/nursery/noHeadElement/pages/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,4 @@ | ||
<Head> | ||
<title>Valid</title> | ||
<meta name="viewport" content="initial-scale=1.0, width=device-width" /> | ||
</Head> |
13 changes: 13 additions & 0 deletions
13
crates/biome_js_analyze/tests/specs/nursery/noHeadElement/pages/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,13 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
assertion_line: 86 | ||
expression: valid.jsx | ||
--- | ||
# Input | ||
```jsx | ||
<Head> | ||
<title>Valid</title> | ||
<meta name="viewport" content="initial-scale=1.0, width=device-width" /> | ||
</Head> | ||
|
||
``` |
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
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.