Skip to content

Commit 66c591f

Browse files
committed
fixes from review from dyc3
1 parent 509f4cd commit 66c591f

File tree

2 files changed

+23
-68
lines changed

2 files changed

+23
-68
lines changed

.changeset/brave-scripts-dance.md

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,4 @@
22
"@biomejs/biome": patch
33
---
44

5-
Added the new nursery rule [`useRequiredScripts`](https://biomejs.dev/linter/rules/use-required-scripts/), which enforces the presence of required scripts in `package.json`. This rule is particularly useful in monorepo environments where consistency across workspaces is important.
6-
7-
The rule accepts a `requiredScripts` option to specify which scripts must be present:
8-
9-
```json
10-
{
11-
"linter": {
12-
"rules": {
13-
"nursery": {
14-
"useRequiredScripts": {
15-
"level": "error",
16-
"options": {
17-
"requiredScripts": ["test", "build", "lint"]
18-
}
19-
}
20-
}
21-
}
22-
}
23-
}
24-
```
25-
26-
For example, the following `package.json` triggers the rule when `["test", "build"]` are required:
27-
28-
```json
29-
{
30-
"scripts": {
31-
"test": "vitest"
32-
}
33-
}
34-
```
35-
36-
```
37-
package.json:1:1 lint/nursery/useRequiredScripts ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
38-
39-
✖ The required script "build" is missing from package.json.
40-
41-
> 1 │ {
42-
│ ^
43-
2 │ "scripts": {
44-
3 │ "test": "vitest"
45-
46-
ℹ Add the missing script to your package.json.
47-
```
5+
Added the rule [`useRequiredScripts`](https://biomejs.dev/linter/rules/use-required-scripts/), which enforces presence of configurable entries in the `scripts` section of `package.json` files.

crates/biome_json_analyze/src/lint/nursery/use_required_scripts.rs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,46 @@ use biome_console::markup;
33
use biome_json_syntax::JsonRoot;
44
use biome_rowan::{AstNode, AstSeparatedList};
55
use biome_rule_options::use_required_scripts::UseRequiredScriptsOptions;
6-
use rustc_hash::FxHashSet;
76

87
declare_lint_rule! {
98
/// Enforce the presence of required scripts in package.json.
109
///
1110
/// This rule ensures that specified scripts are defined in the `scripts` section of a `package.json` file.
1211
/// It's particularly useful in monorepo environments where consistency across workspaces is important.
1312
///
14-
/// ## Examples
13+
/// ## Options
1514
///
16-
/// ### Invalid
15+
/// ### `requiredScripts`
1716
///
18-
/// ```json
17+
/// An array of script names that must be present in the `scripts` section of `package.json`.
18+
///
19+
/// ```json,options
1920
/// {
20-
/// "scripts": {
21-
/// "test": "vitest"
21+
/// "options": {
22+
/// "requiredScripts": ["test", "build"]
2223
/// }
2324
/// }
2425
/// ```
2526
///
26-
/// With options `{ "requiredScripts": ["test", "build"] }`, this is invalid because `build` is missing.
27+
/// ## Examples
2728
///
28-
/// ### Valid
29+
/// ### Invalid
2930
///
30-
/// ```json
31+
/// ```json,expect_diagnostic,use_options
3132
/// {
3233
/// "scripts": {
33-
/// "test": "vitest",
34-
/// "build": "tsc"
34+
/// "test": "vitest"
3535
/// }
3636
/// }
3737
/// ```
3838
///
39-
/// With options `{ "requiredScripts": ["test", "build"] }`, this is valid because all required scripts are present.
40-
///
41-
/// ## Options
42-
///
43-
/// The rule accepts an array of script names that must be present:
39+
/// ### Valid
4440
///
45-
/// ```json,options
41+
/// ```json,use_options
4642
/// {
47-
/// "options": {
48-
/// "requiredScripts": ["test", "build", "lint"]
43+
/// "scripts": {
44+
/// "test": "vitest",
45+
/// "build": "tsc"
4946
/// }
5047
/// }
5148
/// ```
@@ -86,18 +83,18 @@ impl Rule for UseRequiredScripts {
8683
.iter()
8784
.flatten()
8885
.find(|member| {
89-
if let Ok(name) = member.name() {
90-
if let Ok(text) = name.inner_string_text() {
91-
return text.text() == "scripts";
92-
}
86+
if let Ok(name) = member.name()
87+
&& let Ok(text) = name.inner_string_text()
88+
{
89+
return text.text() == "scripts";
9390
}
9491
false
9592
})?;
9693

9794
let scripts_value = scripts_member.value().ok()?;
9895
let scripts_object = scripts_value.as_json_object_value()?;
9996

100-
let existing_scripts: FxHashSet<String> = scripts_object
97+
let existing_scripts: Vec<String> = scripts_object
10198
.json_member_list()
10299
.iter()
103100
.flatten()
@@ -111,7 +108,7 @@ impl Rule for UseRequiredScripts {
111108
let missing_scripts: Vec<String> = options
112109
.required_scripts
113110
.iter()
114-
.filter(|script| !existing_scripts.contains(*script))
111+
.filter(|script| !existing_scripts.iter().any(|s| s == *script))
115112
.cloned()
116113
.collect();
117114

0 commit comments

Comments
 (0)