mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
> This PR is (unfortunately) quite large, but all changes are needed in tandem for this to work properly. ## What This PR Does Updates the linter to populate diagnostics reported by rules with error codes statically derived from `RuleMeta` + `RuleEnum`. Doing so required changing how we handle vitest rules. I know @mysterven was hoping to refactor that part of the code, and I think this approach is an improvement (but could probably be cleaned up further). ## Changes ### 1. Auto-Populate Error Codes `LintContext` now sets an error code scope + error code number for diagnostics reported by lint rules. `LintContext` will not clobber existing codes set by rules, allowing for rule-specific override behavior (e.g. to use `eslint-plugin-react-hooks` as an error scope). In order to accomplish this, I had to update every diagnostic factory for every rule. While doing this I found some incorrect error messages, or messages that could be easily improved. This is where a large majority of the snapshot diffs come from. Additionally, I was able to reduce string allocations from `format!` usages in diagnostic factories, especially within jest rules. ### 2. Framework and Library Detection This PR adds `FrameworkFlags`, which specify what (if any) set of libraries and frameworks are being used by a project and/or file. They are passed in two ways: 1. `LintOptions` can specify a set of `framework_hints` that apply to the entire target codebase. Right now these are always empty, but I'm thinking in the future we could sniff `package.json`. It may be helpful for enabling/disabling default rules. 2. When `Linter` gets run on a file, framework information is sniffed from the `LintContext`. Right now, we are only checking for `vitest` imports in `ModuleRecord` and test path prefixes from `source_path`. It may be useful to do something similar for React/NextJS rules in the future. I know that [next/no-html-link-for-pages](https://nextjs.org/docs/messages/no-html-link-for-pages) could benefit greatly from this.
85 lines
2.4 KiB
Rust
85 lines
2.4 KiB
Rust
use bitflags::bitflags;
|
|
use oxc_semantic::ModuleRecord;
|
|
use std::{hash, path::Path};
|
|
|
|
bitflags! {
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub struct FrameworkFlags: u32 {
|
|
// front-end frameworks
|
|
|
|
/// Uses [React](https://reactjs.org/).
|
|
///
|
|
/// May be part of a meta-framework like Next.js.
|
|
const React = 1 << 0;
|
|
/// Uses [Preact](https://preactjs.com/).
|
|
const Preact = 1 << 1;
|
|
/// Uses [Next.js](https://nextjs.org/).
|
|
const NextOnly = 1 << 2;
|
|
const Next = Self::NextOnly.bits() | Self::React.bits();
|
|
const JsxLike = Self::React.bits() | Self::Preact.bits() | Self::Next.bits();
|
|
|
|
const Vue = 1 << 3;
|
|
const NuxtOnly = 1 << 4;
|
|
const Nuxt = Self::NuxtOnly.bits() | Self::Vue.bits();
|
|
|
|
const Angular = 1 << 5;
|
|
|
|
const Svelte = 1 << 6;
|
|
const SvelteKitOnly = 1 << 7;
|
|
const SvelteKit = Self::SvelteKitOnly.bits() | Self::Svelte.bits();
|
|
|
|
const Astro = 1 << 8;
|
|
|
|
// Testing frameworks
|
|
const Jest = 1 << 9;
|
|
const Vitest = 1 << 10;
|
|
const OtherTest = 1 << 11;
|
|
const Test = Self::Jest.bits() | Self::Vitest.bits() | Self::OtherTest.bits();
|
|
}
|
|
}
|
|
|
|
impl Default for FrameworkFlags {
|
|
#[inline]
|
|
fn default() -> Self {
|
|
Self::empty()
|
|
}
|
|
}
|
|
impl hash::Hash for FrameworkFlags {
|
|
#[inline]
|
|
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
|
state.write_u32(self.bits());
|
|
}
|
|
}
|
|
|
|
impl FrameworkFlags {
|
|
#[inline]
|
|
pub const fn is_test(self) -> bool {
|
|
self.intersects(Self::Test)
|
|
}
|
|
|
|
#[inline]
|
|
pub const fn is_vitest(self) -> bool {
|
|
self.contains(Self::Vitest)
|
|
}
|
|
}
|
|
|
|
/// <https://jestjs.io/docs/configuration#testmatch-arraystring>
|
|
pub(crate) fn is_jestlike_file(path: &Path) -> bool {
|
|
use std::ffi::OsStr;
|
|
|
|
if path.components().any(|c| match c {
|
|
std::path::Component::Normal(p) => p == OsStr::new("__tests__"),
|
|
_ => false,
|
|
}) {
|
|
return true;
|
|
}
|
|
|
|
path.file_name() // foo/bar/baz.test.ts -> baz.test.ts
|
|
.and_then(OsStr::to_str)
|
|
.and_then(|filename| filename.split('.').rev().nth(1)) // baz.test.ts -> test
|
|
.is_some_and(|name_or_first_ext| name_or_first_ext == "test" || name_or_first_ext == "spec")
|
|
}
|
|
|
|
pub(crate) fn has_vitest_imports(module_record: &ModuleRecord) -> bool {
|
|
module_record.import_entries.iter().any(|entry| entry.module_request.name() == "vitest")
|
|
}
|