Skip to content

Commit 4f70fe5

Browse files
committedSep 10, 2024·
refactor(linter): start internal/external split of LintPluginOptions (#5660)
Re-creation of #5142
1 parent 5ae9b48 commit 4f70fe5

File tree

4 files changed

+97
-8
lines changed

4 files changed

+97
-8
lines changed
 

‎crates/oxc_linter/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl Linter {
161161
.with_frameworks(self.options.framework_hints);
162162

163163
// set file-specific jest/vitest flags
164-
if self.options.plugins.jest || self.options.plugins.vitest {
164+
if self.options.plugins.has_test() {
165165
let mut test_flags = FrameworkFlags::empty();
166166

167167
if frameworks::has_vitest_imports(ctx.module_record()) {
@@ -191,7 +191,7 @@ impl Linter {
191191
}
192192

193193
fn map_jest(&self, plugin_name: &'static str, rule_name: &str) -> &'static str {
194-
if self.options.plugins.vitest
194+
if self.options.plugins.has_vitest()
195195
&& plugin_name == "jest"
196196
&& utils::is_jest_rule_adapted_to_vitest(rule_name)
197197
{

‎crates/oxc_linter/src/options/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{convert::From, path::PathBuf};
66
pub use allow_warn_deny::AllowWarnDeny;
77
use oxc_diagnostics::Error;
88
pub use plugins::LintPluginOptions;
9+
use plugins::LintPlugins;
910
use rustc_hash::FxHashSet;
1011

1112
use crate::{
@@ -26,15 +27,15 @@ use crate::{
2627
pub(crate) struct LintOptions {
2728
pub fix: FixKind,
2829
pub framework_hints: FrameworkFlags,
29-
pub plugins: LintPluginOptions,
30+
pub plugins: LintPlugins,
3031
}
3132

3233
impl From<OxlintOptions> for LintOptions {
3334
fn from(options: OxlintOptions) -> Self {
3435
Self {
3536
fix: options.fix,
3637
framework_hints: options.framework_hints,
37-
plugins: options.plugins,
38+
plugins: options.plugins.into(),
3839
}
3940
}
4041
}

‎crates/oxc_linter/src/options/plugins.rs

+88
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,84 @@
1+
use bitflags::bitflags;
2+
3+
bitflags! {
4+
// NOTE: may be increased to a u32 if needed
5+
#[derive(Debug, Clone, Copy, PartialEq, Hash)]
6+
pub(crate) struct LintPlugins: u16 {
7+
/// `eslint-plugin-react`, plus `eslint-plugin-react-hooks`
8+
const REACT = 1 << 0;
9+
/// `eslint-plugin-unicorn`
10+
const UNICORN = 1 << 1;
11+
/// `@typescript-eslint/eslint-plugin`
12+
const TYPESCRIPT = 1 << 2;
13+
/// Custom rules for Oxc, plus some ported from Deepscan
14+
const OXC = 1 << 3;
15+
/// `eslint-plugin-import`
16+
const IMPORT = 1 << 4;
17+
/// `eslint-plugin-jsdoc`
18+
const JSDOC = 1 << 5;
19+
/// `eslint-plugin-jest`
20+
const JEST = 1 << 6;
21+
/// `eslint-plugin-vitest`
22+
const VITEST = 1 << 7;
23+
/// `eslint-plugin-jsx-a11y`
24+
const JSX_A11Y = 1 << 8;
25+
/// `eslint-plugin-next`
26+
const NEXTJS = 1 << 9;
27+
/// `eslint-plugin-react-perf`
28+
const REACT_PERF = 1 << 10;
29+
/// `eslint-plugin-promise`
30+
const PROMISE = 1 << 11;
31+
/// `eslint-plugin-node`
32+
const NODE = 1 << 12;
33+
}
34+
}
35+
impl Default for LintPlugins {
36+
#[inline]
37+
fn default() -> Self {
38+
LintPlugins::REACT | LintPlugins::UNICORN | LintPlugins::TYPESCRIPT | LintPlugins::OXC
39+
}
40+
}
41+
42+
impl From<LintPluginOptions> for LintPlugins {
43+
fn from(options: LintPluginOptions) -> Self {
44+
let mut plugins = LintPlugins::empty();
45+
plugins.set(LintPlugins::REACT, options.react);
46+
plugins.set(LintPlugins::UNICORN, options.unicorn);
47+
plugins.set(LintPlugins::TYPESCRIPT, options.typescript);
48+
plugins.set(LintPlugins::OXC, options.oxc);
49+
plugins.set(LintPlugins::IMPORT, options.import);
50+
plugins.set(LintPlugins::JSDOC, options.jsdoc);
51+
plugins.set(LintPlugins::JEST, options.jest);
52+
plugins.set(LintPlugins::VITEST, options.vitest);
53+
plugins.set(LintPlugins::JSX_A11Y, options.jsx_a11y);
54+
plugins.set(LintPlugins::NEXTJS, options.nextjs);
55+
plugins.set(LintPlugins::REACT_PERF, options.react_perf);
56+
plugins.set(LintPlugins::PROMISE, options.promise);
57+
plugins.set(LintPlugins::NODE, options.node);
58+
plugins
59+
}
60+
}
61+
62+
impl LintPlugins {
63+
/// Returns `true` if the Vitest plugin is enabled.
64+
#[inline]
65+
pub fn has_vitest(self) -> bool {
66+
self.contains(LintPlugins::VITEST)
67+
}
68+
69+
/// Returns `true` if Jest or Vitest plugins are enabled.
70+
#[inline]
71+
pub fn has_test(self) -> bool {
72+
self.intersects(LintPlugins::JEST.union(LintPlugins::VITEST))
73+
}
74+
75+
/// Returns `true` if the import plugin is enabled.
76+
#[inline]
77+
pub fn has_import(self) -> bool {
78+
self.contains(LintPlugins::IMPORT)
79+
}
80+
}
81+
182
#[derive(Debug)]
283
#[non_exhaustive]
384
pub struct LintPluginOptions {
@@ -138,6 +219,13 @@ mod test {
138219
}
139220
}
140221

222+
#[test]
223+
fn test_default_conversion() {
224+
let plugins = LintPlugins::default();
225+
let options = LintPluginOptions::default();
226+
assert_eq!(LintPlugins::from(options), plugins);
227+
}
228+
141229
#[test]
142230
fn test_collect_empty() {
143231
let empty: &[&str] = &[];

‎crates/oxc_linter/src/service.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pub struct Runtime {
165165

166166
impl Runtime {
167167
fn new(linter: Linter, options: LintServiceOptions) -> Self {
168-
let resolver = linter.options().plugins.import.then(|| {
168+
let resolver = linter.options().plugins.has_import().then(|| {
169169
Self::get_resolver(options.tsconfig.or_else(|| Some(options.cwd.join("tsconfig.json"))))
170170
});
171171
Self {
@@ -310,7 +310,7 @@ impl Runtime {
310310
.build_module_record(path, program);
311311
let module_record = semantic_builder.module_record();
312312

313-
if self.linter.options().plugins.import {
313+
if self.linter.options().plugins.has_import() {
314314
self.module_map.insert(
315315
path.to_path_buf().into_boxed_path(),
316316
ModuleState::Resolved(Arc::clone(&module_record)),
@@ -392,7 +392,7 @@ impl Runtime {
392392
}
393393

394394
fn init_cache_state(&self, path: &Path) -> bool {
395-
if !self.linter.options().plugins.import {
395+
if !self.linter.options().plugins.has_import() {
396396
return false;
397397
}
398398

@@ -447,7 +447,7 @@ impl Runtime {
447447
}
448448

449449
fn ignore_path(&self, path: &Path) {
450-
if self.linter.options().plugins.import {
450+
if self.linter.options().plugins.has_import() {
451451
self.module_map.insert(path.to_path_buf().into_boxed_path(), ModuleState::Ignored);
452452
self.update_cache_state(path);
453453
}

0 commit comments

Comments
 (0)
Please sign in to comment.