Skip to main content

oxlint_config/
lib.rs

1mod plugins;
2pub use plugins::*;
3
4use indexmap::IndexSet;
5use merge_it::*;
6#[cfg(feature = "schemars")]
7use schemars::JsonSchema;
8use serde::{Deserialize, Serialize};
9use serde_json::Value;
10use std::collections::{BTreeMap, BTreeSet};
11
12type JsonValueBTreeMap = BTreeMap<String, Value>;
13
14/// The configuration directives for `oxlint`. See more: https://oxc.rs/docs/guide/usage/linter/config-file-reference.html
15#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Merge)]
16#[cfg_attr(feature = "schemars", derive(JsonSchema))]
17#[serde(rename_all = "camelCase")]
18#[serde(default)]
19pub struct OxlintConfig {
20	/// Paths of configuration files that this configuration file extends (inherits from). The files are resolved relative to the location of the configuration file that contains the `extends` property. The configuration files are merged from the first to the last, with the last file overriding the previous ones.
21	#[serde(skip_serializing_if = "IndexSet::is_empty")]
22	pub extends: IndexSet<String>,
23
24	/// A list of plugins to enable for this config.
25	#[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
26	pub plugins: BTreeSet<Plugin>,
27
28	/// Contains the settings for various plugins.
29	#[serde(skip_serializing_if = "Option::is_none")]
30	pub settings: Option<PluginsSettings>,
31
32	/// Enables or disables specific global variables.
33	#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
34	pub globals: BTreeMap<String, GlobalValue>,
35
36	/// Environments enable and disable collections of global variables.
37	#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
38	pub env: BTreeMap<String, bool>,
39
40	/// Configure an entire category of rules all at once.
41	///
42	/// Rules enabled or disabled this way will be overwritten by individual rules in the rules field.
43	///
44	/// See more: https://oxc.rs/docs/guide/usage/linter/config-file-reference#categories
45	#[serde(skip_serializing_if = "Option::is_none")]
46	pub categories: Option<Categories>,
47
48	/// Globs to ignore during linting. These are resolved from the configuration file path.
49	#[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
50	pub ignore_patterns: BTreeSet<String>,
51
52	/// Settings for individual rules. See [Oxlint Rules](https://oxc.rs/docs/guide/usage/linter/rules.html) for the list of rules.
53	#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
54	pub rules: BTreeMap<String, RuleSetting>,
55
56	/// Add, remove, or otherwise reconfigure rules for specific files or groups of files.
57	#[serde(default, skip_serializing_if = "Vec::is_empty")]
58	pub overrides: Vec<Override>,
59
60	#[serde(flatten, default, skip_serializing_if = "BTreeMap::is_empty")]
61	pub extras: JsonValueBTreeMap,
62}
63
64impl Default for OxlintConfig {
65	fn default() -> Self {
66		Self {
67			extends: Default::default(),
68			env: Default::default(),
69			globals: Default::default(),
70			categories: Default::default(),
71			ignore_patterns: Default::default(),
72			overrides: Default::default(),
73			plugins: BTreeSet::from_iter([
74				Plugin::Known(Plugins::Oxc),
75				Plugin::Known(Plugins::Typescript),
76				Plugin::Known(Plugins::Unicorn),
77			]),
78			rules: Default::default(),
79			settings: Default::default(),
80			extras: Default::default(),
81		}
82	}
83}
84
85/// Settings for global variables.
86#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
87#[cfg_attr(feature = "schemars", derive(JsonSchema))]
88#[serde(rename_all = "lowercase")]
89pub enum GlobalValue {
90	/// Disallows overwriting a global variable.
91	Readonly,
92	/// Allows the global variable to be overwritten.
93	Writeable,
94	/// Disables a global variable entirely.
95	Off,
96}
97
98/// The enforcement setting for a linting rule.
99#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
100#[cfg_attr(feature = "schemars", derive(JsonSchema))]
101#[serde(rename_all = "lowercase")]
102pub enum RuleEnforcement {
103	/// Disables the rule.
104	#[serde(alias = "allow")]
105	Off,
106	/// Violating the rule triggers a warning.
107	Warn,
108	/// Violating the rule causes an error.
109	#[serde(alias = "deny")]
110	Error,
111}
112
113/// The settings for an individual rule. Can be a single value such as `warn` or `error`, or an array with the rule enforcement value as the first value, and the rule-specific settings in an object right after that. (example: ["allow", { "setting1": true }])
114#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
115#[cfg_attr(feature = "schemars", derive(JsonSchema))]
116#[serde(untagged)]
117pub enum RuleSetting {
118	Simple(RuleEnforcement),
119	Custom([(RuleEnforcement, JsonValueBTreeMap); 1]),
120}
121
122/// Configure an entire category of rules all at once.Rules enabled or disabled this way will be overwritten by individual rules in the `rules` field.
123#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
124#[cfg_attr(feature = "schemars", derive(JsonSchema))]
125#[serde(default)]
126#[serde(deny_unknown_fields)]
127pub struct Categories {
128	#[serde(skip_serializing_if = "Option::is_none")]
129	pub correctness: Option<RuleEnforcement>,
130
131	#[serde(skip_serializing_if = "Option::is_none")]
132	pub nursery: Option<RuleEnforcement>,
133
134	#[serde(skip_serializing_if = "Option::is_none")]
135	pub pedantic: Option<RuleEnforcement>,
136
137	#[serde(skip_serializing_if = "Option::is_none")]
138	pub perf: Option<RuleEnforcement>,
139
140	#[serde(skip_serializing_if = "Option::is_none")]
141	pub restriction: Option<RuleEnforcement>,
142
143	#[serde(skip_serializing_if = "Option::is_none")]
144	pub style: Option<RuleEnforcement>,
145
146	#[serde(skip_serializing_if = "Option::is_none")]
147	pub suspicious: Option<RuleEnforcement>,
148}
149
150/// Settings to override for a group of files.
151#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
152#[cfg_attr(feature = "schemars", derive(JsonSchema))]
153#[serde(default)]
154#[serde(deny_unknown_fields)]
155pub struct Override {
156	/// A list of glob patterns to override.
157	pub files: BTreeSet<String>,
158
159	/// Optionally change what plugins are enabled for this override. When omitted, the base config's plugins are used.
160	#[serde(skip_serializing_if = "Option::is_none")]
161	pub plugins: Option<BTreeSet<Plugin>>,
162
163	/// Enables or disables specific global variables.
164	#[serde(skip_serializing_if = "Option::is_none")]
165	pub globals: Option<BTreeMap<String, GlobalValue>>,
166
167	/// Environments enable and disable collections of global variables.
168	#[serde(skip_serializing_if = "Option::is_none")]
169	pub env: Option<BTreeMap<String, bool>>,
170
171	/// Override settings for specific rules.
172	#[serde(skip_serializing_if = "Option::is_none")]
173	pub rules: Option<BTreeMap<String, RuleSetting>>,
174}
175
176/// Configure the behavior of linter plugins.
177#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Merge, Default)]
178#[cfg_attr(feature = "schemars", derive(JsonSchema))]
179#[serde(default)]
180#[serde(deny_unknown_fields)]
181#[serde(rename_all = "camelCase")]
182pub struct PluginsSettings {
183	/// Settings for the Jsdoc plugin.
184	#[serde(skip_serializing_if = "Option::is_none")]
185	pub jsdoc: Option<JsDocPluginSettings>,
186
187	/// Settings for the jsx-a11y plugin.
188	#[serde(rename = "jsx-a11y")]
189	#[serde(skip_serializing_if = "Option::is_none")]
190	pub jsx_ally: Option<JsxA11yPluginSettings>,
191
192	/// Settings for the nextjs plugin.
193	#[serde(skip_serializing_if = "Option::is_none")]
194	pub next: Option<NextPluginSettings>,
195
196	/// Settings for the react plugin.
197	#[serde(skip_serializing_if = "Option::is_none")]
198	pub react: Option<ReactPluginSettings>,
199}