|
| 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 | +} |
0 commit comments