Skip to content

Commit 0538e30

Browse files
authored
feat(lint): add usePackageJsonCpu (#12309)
1 parent 6b73fcf commit 0538e30

26 files changed

Lines changed: 373 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Added the new nursery rule [`usePackageJsonCpu`](https://biomejs.dev/linter/rules/use-package-json-cpu/), which reports `package.json` files that have no `cpu` field. The `cpu` field lists the processor types that the package supports.
6+
7+
The following `package.json` file satisfies the rule:
8+
9+
```json
10+
{
11+
"name": "my-package",
12+
"version": "1.0.0",
13+
"cpu": ["x64", "arm64"]
14+
}
15+
```

β€Žcrates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rsβ€Ž

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žcrates/biome_configuration/src/analyzer/linter/rules.rsβ€Ž

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žcrates/biome_configuration/src/generated/linter_options_check.rsβ€Ž

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žcrates/biome_diagnostics_categories/src/categories.rsβ€Ž

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use biome_analyze::{
2+
Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule,
3+
};
4+
use biome_console::markup;
5+
use biome_json_syntax::JsonRoot;
6+
use biome_rule_options::use_package_json_cpu::UsePackageJsonCpuOptions;
7+
8+
use crate::utils::is_package_json;
9+
10+
declare_lint_rule! {
11+
/// Require the `cpu` field in `package.json`.
12+
///
13+
/// The `cpu` field lists the processor types that a package can run on, such as `x64` or
14+
/// `arm64`. Package managers read it to avoid installing the package on a processor that the
15+
/// package doesn't support. Without the field, the package is installed on every processor,
16+
/// and a package that can't run there fails later, when it is used.
17+
///
18+
/// This rule reports a `package.json` file whose outermost object has no `cpu` field. A `cpu`
19+
/// field inside another object doesn't count. The rule doesn't check the value of the field.
20+
///
21+
/// ## Examples
22+
///
23+
/// ### Invalid
24+
///
25+
/// ```json,expect_diagnostic,file=package.json
26+
/// {
27+
/// "name": "my-package",
28+
/// "version": "1.0.0"
29+
/// }
30+
/// ```
31+
///
32+
/// ### Valid
33+
///
34+
/// ```json,file=package.json
35+
/// {
36+
/// "name": "my-package",
37+
/// "version": "1.0.0",
38+
/// "cpu": ["x64", "arm64"]
39+
/// }
40+
/// ```
41+
///
42+
pub UsePackageJsonCpu {
43+
version: "next",
44+
name: "usePackageJsonCpu",
45+
language: "json",
46+
recommended: false,
47+
sources: &[RuleSource::EslintPackageJson("require-cpu").same()],
48+
}
49+
}
50+
51+
impl Rule for UsePackageJsonCpu {
52+
type Query = Ast<JsonRoot>;
53+
type State = ();
54+
type Signals = Option<Self::State>;
55+
type Options = UsePackageJsonCpuOptions;
56+
57+
fn run(ctx: &RuleContext<Self>) -> Self::Signals {
58+
if !is_package_json(ctx.file_path()) {
59+
return None;
60+
}
61+
62+
let value = ctx.query().value().ok()?;
63+
let object = value.as_json_object_value()?;
64+
object.find_member("cpu").is_none().then_some(())
65+
}
66+
67+
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> {
68+
let value = ctx.query().value().ok()?;
69+
let object = value.as_json_object_value()?;
70+
let range = object.l_curly_token().ok()?.text_trimmed_range();
71+
72+
Some(
73+
RuleDiagnostic::new(
74+
rule_category!(),
75+
range,
76+
markup! {
77+
"This "<Emphasis>"package.json"</Emphasis>" file is missing the "<Emphasis>"cpu"</Emphasis>" field."
78+
},
79+
)
80+
.note(markup! {
81+
"Package managers read this field to avoid installing the package on a processor that the package doesn't support."
82+
})
83+
.note(markup! {
84+
"Add a "<Emphasis>"cpu"</Emphasis>" field that lists the processor types the package supports, for example "<Emphasis>"[\"x64\", \"arm64\"]"</Emphasis>"."
85+
}),
86+
)
87+
}
88+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
source: crates/biome_json_analyze/tests/spec_tests.rs
3+
expression: package.json
4+
---
5+
# Input
6+
```json
7+
{}
8+
9+
```
10+
11+
# Diagnostics
12+
```
13+
package.json:1:1 lint/nursery/usePackageJsonCpu ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
14+
15+
i This package.json file is missing the cpu field.
16+
17+
> 1 β”‚ {}
18+
β”‚ ^
19+
2 β”‚
20+
21+
i Package managers read this field to avoid installing the package on a processor that the package doesn't support.
22+
23+
i Add a cpu field that lists the processor types the package supports, for example ["x64", "arm64"].
24+
25+
i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
26+
27+
28+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "my-package",
3+
"version": "1.0.0"
4+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
source: crates/biome_json_analyze/tests/spec_tests.rs
3+
expression: package.json
4+
---
5+
# Input
6+
```json
7+
{
8+
"name": "my-package",
9+
"version": "1.0.0"
10+
}
11+
12+
```
13+
14+
# Diagnostics
15+
```
16+
package.json:1:1 lint/nursery/usePackageJsonCpu ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
17+
18+
i This package.json file is missing the cpu field.
19+
20+
> 1 β”‚ {
21+
β”‚ ^
22+
2 β”‚ "name": "my-package",
23+
3 β”‚ "version": "1.0.0"
24+
25+
i Package managers read this field to avoid installing the package on a processor that the package doesn't support.
26+
27+
i Add a cpu field that lists the processor types the package supports, for example ["x64", "arm64"].
28+
29+
i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
30+
31+
32+
```

0 commit comments

Comments
Β (0)