Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl Linter {
.with_frameworks(self.options.framework_hints);

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

if frameworks::has_vitest_imports(ctx.module_record()) {
Expand Down Expand Up @@ -191,7 +191,7 @@ impl Linter {
}

fn map_jest(&self, plugin_name: &'static str, rule_name: &str) -> &'static str {
if self.options.plugins.vitest
if self.options.plugins.has_vitest()
&& plugin_name == "jest"
&& utils::is_jest_rule_adapted_to_vitest(rule_name)
{
Expand Down
5 changes: 3 additions & 2 deletions crates/oxc_linter/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod plugins;
use std::{convert::From, path::PathBuf};

use oxc_diagnostics::Error;
use plugins::LintPlugins;
use rustc_hash::FxHashSet;

use crate::{
Expand Down Expand Up @@ -38,7 +39,7 @@ pub struct OxlintOptions {
pub(crate) struct LintOptions {
pub fix: FixKind,
pub framework_hints: FrameworkFlags,
pub plugins: LintPluginOptions,
pub plugins: LintPlugins,
}

impl Default for OxlintOptions {
Expand All @@ -62,7 +63,7 @@ impl From<OxlintOptions> for LintOptions {
Self {
fix: options.fix,
framework_hints: options.framework_hints,
plugins: options.plugins,
plugins: options.plugins.into(),
}
}
}
Expand Down
71 changes: 71 additions & 0 deletions crates/oxc_linter/src/options/plugins.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,67 @@
use bitflags::bitflags;
bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Hash)]
pub(crate) struct LintPlugins: u32 {
const REACT = 1 << 0;
const UNICORN = 1 << 1;
const TYPESCRIPT = 1 << 2;
const OXC = 1 << 3;
const IMPORT = 1 << 4;
const JSDOC = 1 << 5;
const JEST = 1 << 6;
const VITEST = 1 << 7;
const JSX_A11Y = 1 << 8;
const NEXTJS = 1 << 9;
const REACT_PERF = 1 << 10;
const PROMISE = 1 << 11;
}
}
impl Default for LintPlugins {
#[inline]
fn default() -> Self {
LintPlugins::REACT | LintPlugins::UNICORN | LintPlugins::TYPESCRIPT | LintPlugins::OXC
}
}

impl From<LintPluginOptions> for LintPlugins {
fn from(options: LintPluginOptions) -> Self {
let mut plugins = LintPlugins::empty();
plugins.set(LintPlugins::REACT, options.react);
plugins.set(LintPlugins::UNICORN, options.unicorn);
plugins.set(LintPlugins::TYPESCRIPT, options.typescript);
plugins.set(LintPlugins::OXC, options.oxc);
plugins.set(LintPlugins::IMPORT, options.import);
plugins.set(LintPlugins::JSDOC, options.jsdoc);
plugins.set(LintPlugins::JEST, options.jest);
plugins.set(LintPlugins::VITEST, options.vitest);
plugins.set(LintPlugins::JSX_A11Y, options.jsx_a11y);
plugins.set(LintPlugins::NEXTJS, options.nextjs);
plugins.set(LintPlugins::REACT_PERF, options.react_perf);
plugins.set(LintPlugins::PROMISE, options.promise);
plugins
}
}

impl LintPlugins {
/// Returns `true` if the Vitest plugin is enabled.
#[inline]
pub fn has_vitest(self) -> bool {
self.contains(LintPlugins::VITEST)
}

/// Returns `true` if Jest or Vitest plugins are enabled.
#[inline]
pub fn has_test(self) -> bool {
self.intersects(LintPlugins::JEST.union(LintPlugins::VITEST))
}

/// Returns `true` if the import plugin is enabled.
#[inline]
pub fn has_import(self) -> bool {
self.contains(LintPlugins::IMPORT)
}
}

#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
#[non_exhaustive]
Expand Down Expand Up @@ -117,6 +181,13 @@ impl<'s> FromIterator<&'s str> for LintPluginOptions {
mod test {
use super::*;

#[test]
fn test_default_conversion() {
let plugins = LintPlugins::default();
let options = LintPluginOptions::default();
assert_eq!(LintPlugins::from(options), plugins);
}

#[test]
fn test_collect_empty() {
let empty: &[&str] = &[];
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub struct Runtime {

impl Runtime {
fn new(linter: Linter, options: LintServiceOptions) -> Self {
let resolver = linter.options().plugins.import.then(|| {
let resolver = linter.options().plugins.has_import().then(|| {
Self::get_resolver(options.tsconfig.or_else(|| Some(options.cwd.join("tsconfig.json"))))
});
Self {
Expand Down Expand Up @@ -280,7 +280,7 @@ impl Runtime {
.build_module_record(path.to_path_buf(), program);
let module_record = semantic_builder.module_record();

if self.linter.options().plugins.import {
if self.linter.options().plugins.has_import() {
self.module_map.insert(
path.to_path_buf().into_boxed_path(),
ModuleState::Resolved(Arc::clone(&module_record)),
Expand Down Expand Up @@ -362,7 +362,7 @@ impl Runtime {
}

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

Expand Down Expand Up @@ -417,7 +417,7 @@ impl Runtime {
}

fn ignore_path(&self, path: &Path) {
if self.linter.options().plugins.import {
if self.linter.options().plugins.has_import() {
self.module_map.insert(path.to_path_buf().into_boxed_path(), ModuleState::Ignored);
self.update_cache_state(path);
}
Expand Down