Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lint): add missing domain to next.js rules #5046

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions crates/biome_cli/tests/cases/rules_via_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,55 @@ function Component2() {
result,
));
}

#[test]
fn enables_next_rules_via_dependencies() {
let mut console = BufferConsole::default();
let mut fs = MemoryFileSystem::default();
let file_path = Utf8Path::new("package.json");

fs.insert(
file_path.into(),
r#"{
"dependencies": {
"next": ">=14.0.0"
}
}"#
.as_bytes(),
);

let test_file = Utf8Path::new("test.jsx");
fs.insert(
test_file.into(),
r#"import React from 'react';

function IndexPage() {
return (
<div>
<img alt="Foo" />
<p>Some content</p>
</div>
);
}

export default IndexPage;
"#
.as_bytes(),
);

let (fs, result) = run_cli(
fs,
&mut console,
Args::from(["lint", test_file.as_str()].as_slice()),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"enables_next_rules_via_dependencies",
fs,
console,
result,
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: redactor(content)
snapshot_kind: text
---
## `package.json`

```json
{
"dependencies": {
"next": ">=14.0.0"
}
}
```

## `test.jsx`

```jsx
import React from 'react';

function IndexPage() {
return (
<div>
<img alt="Foo" />
<p>Some content</p>
</div>
);
}

export default IndexPage;

```

# Emitted Messages

```block
test.jsx:6:13 lint/nursery/noImgElement ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

i Don't use <img> element.

4 │ return (
5 │ <div>
> 6 │ <img alt="Foo" />
│ ^^^^^^^^^^^^^^^^^
7 │ <p>Some content</p>
8 │ </div>

i Using the <img> can lead to slower LCP and higher bandwidth. Consider using <Image /> from next/image to automatically optimize images.


```

```block
Checked 1 file in <TIME>. No fixes applied.
```
7 changes: 7 additions & 0 deletions crates/biome_configuration/src/analyzer/linter/rules.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ declare_lint_rule! {
language: "jsx",
sources: &[RuleSource::EslintNext("no-document-import-in-page")],
source_kind: RuleSourceKind::SameLogic,
recommended: false,
recommended: true,
domains: &[RuleDomain::Next],
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/biome_js_analyze/src/lint/nursery/no_head_element.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use biome_analyze::RuleSourceKind;
use biome_analyze::{
context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic, RuleSource,
context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic, RuleDomain, RuleSource,
};
use biome_console::markup;
use biome_js_syntax::JsxOpeningElement;
Expand Down Expand Up @@ -49,7 +49,8 @@ declare_lint_rule! {
language: "jsx",
sources: &[RuleSource::EslintNext("no-head-element")],
source_kind: RuleSourceKind::SameLogic,
recommended: false,
recommended: true,
domains: &[RuleDomain::Next],
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ declare_lint_rule! {
language: "jsx",
sources: &[RuleSource::EslintNext("no-head-import-in-document")],
source_kind: RuleSourceKind::SameLogic,
recommended: false,
recommended: true,
domains: &[RuleDomain::Next],
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_js_analyze/src/lint/nursery/no_img_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ declare_lint_rule! {
language: "jsx",
sources: &[RuleSource::EslintNext("no-img-element")],
source_kind: RuleSourceKind::SameLogic,
recommended: false,
recommended: true,
domains: &[RuleDomain::Next],
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ declare_lint_rule! {
language: "jsx",
sources: &[RuleSource::EslintNext("no-unwanted-polyfillio")],
source_kind: RuleSourceKind::SameLogic,
recommended: false,
recommended: true,
severity: Severity::Warning,
domains: &[RuleDomain::Next],
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use biome_analyze::{
context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic, RuleSource, RuleSourceKind,
context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic, RuleDomain, RuleSource,
RuleSourceKind,
};
use biome_console::markup;
use biome_js_syntax::jsx_ext::AnyJsxElement;
Expand Down Expand Up @@ -54,7 +55,8 @@ declare_lint_rule! {
language: "jsx",
sources: &[RuleSource::EslintNext("google-font-display")],
source_kind: RuleSourceKind::SameLogic,
recommended: false,
recommended: true,
domains: &[RuleDomain::Next],
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use biome_analyze::{
context::RuleContext, declare_lint_rule, Ast, FixKind, Rule, RuleDiagnostic, RuleSource,
RuleSourceKind,
context::RuleContext, declare_lint_rule, Ast, FixKind, Rule, RuleDiagnostic, RuleDomain,
RuleSource, RuleSourceKind,
};
use biome_console::markup;
use biome_js_factory::make;
Expand Down Expand Up @@ -51,8 +51,9 @@ declare_lint_rule! {
language: "jsx",
sources: &[RuleSource::EslintNext("google-font-preconnect")],
source_kind: RuleSourceKind::SameLogic,
recommended: false,
recommended: true,
fix_kind: FixKind::Safe,
domains: &[RuleDomain::Next],
}
}

Expand Down
109 changes: 56 additions & 53 deletions crates/biome_service/src/file_handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use biome_json_analyze::METADATA as json_metadata;
use biome_json_syntax::{JsonFileSource, JsonLanguage};
use biome_parser::AnyParse;
use biome_project_layout::ProjectLayout;
use biome_rowan::{FileSourceError, NodeCache};
use biome_rowan::{declare_node_union, FileSourceError, NodeCache};
use biome_string_case::StrLikeExtension;

use camino::Utf8Path;
Expand Down Expand Up @@ -996,20 +996,21 @@ impl<'a, 'b> LintVisitor<'a, 'b> {
.settings
.and_then(|settings| settings.as_linter_domains(self.path.expect("File path")))
.is_none_or(|d| d.is_empty());
if !(no_only && no_domains) {
return;
}

if no_only && no_domains {
if let Some((_, manifest)) = self
.path
.and_then(|path| self.project_layout.get_node_manifest_for_path(path))
{
for domain in R::METADATA.domains {
self.analyzer_options
.push_globals(domain.globals().iter().map(|s| Box::from(*s)).collect());

for (dependency, range) in domain.manifest_dependencies() {
if manifest.matches_dependency(dependency, range) {
self.enabled_rules.insert(rule_filter);
}
if let Some((_, manifest)) = self
.path
.and_then(|path| self.project_layout.get_node_manifest_for_path(path))
{
for domain in R::METADATA.domains {
self.analyzer_options
.push_globals(domain.globals().iter().map(|s| Box::from(*s)).collect());

for (dependency, range) in domain.manifest_dependencies() {
if manifest.matches_dependency(dependency, range) {
self.enabled_rules.insert(rule_filter);
}
}
}
Expand All @@ -1028,32 +1029,50 @@ impl<'a, 'b> LintVisitor<'a, 'b> {
{
let no_only = self.only.is_some_and(|only| only.is_empty());

if no_only {
let domains = self
.settings
.and_then(|settings| settings.as_linter_domains(self.path.expect("File path")));
if !no_only {
return;
}

// domains, no need to record the rule
if domains.as_ref().is_none_or(|d| d.is_empty()) {
return;
}
let domains = self
.settings
.and_then(|settings| settings.as_linter_domains(self.path.expect("File path")));

// If the rule is recommended, and it has some domains, it should be disabled, but only if the configuration doesn't enable some domains.
if R::METADATA.recommended
&& !R::METADATA.domains.is_empty()
&& domains.as_ref().is_none_or(|d| d.is_empty())
{
self.disabled_rules.insert(rule_filter);
return;
}
// domains, no need to record the rule
if domains.as_ref().is_none_or(|d| d.is_empty()) {
return;
}

// If the rule is recommended, and it has some domains, it should be disabled, but only if the configuration doesn't enable some domains.
if R::METADATA.recommended
&& !R::METADATA.domains.is_empty()
&& domains.as_ref().is_none_or(|d| d.is_empty())
{
self.disabled_rules.insert(rule_filter);
return;
}

for rule_domain in R::METADATA.domains {
if let Some((configured_domain, configured_domain_value)) = domains
.as_ref()
.and_then(|domains| domains.get_key_value(rule_domain))
{
match configured_domain_value {
RuleDomainValue::All => {
for rule_domain in R::METADATA.domains {
if let Some((configured_domain, configured_domain_value)) = domains
.as_ref()
.and_then(|domains| domains.get_key_value(rule_domain))
{
match configured_domain_value {
RuleDomainValue::All => {
self.enabled_rules.insert(rule_filter);

self.analyzer_options.push_globals(
configured_domain
.globals()
.iter()
.map(|s| Box::from(*s))
.collect::<Vec<_>>(),
);
}
RuleDomainValue::None => {
self.disabled_rules.insert(rule_filter);
}
RuleDomainValue::Recommended => {
if R::METADATA.recommended {
self.enabled_rules.insert(rule_filter);

self.analyzer_options.push_globals(
Expand All @@ -1064,22 +1083,6 @@ impl<'a, 'b> LintVisitor<'a, 'b> {
.collect::<Vec<_>>(),
);
}
RuleDomainValue::None => {
self.disabled_rules.insert(rule_filter);
}
RuleDomainValue::Recommended => {
if R::METADATA.recommended {
self.enabled_rules.insert(rule_filter);

self.analyzer_options.push_globals(
configured_domain
.globals()
.iter()
.map(|s| Box::from(*s))
.collect::<Vec<_>>(),
);
}
}
}
}
}
Expand Down
Loading