Skip to content

Commit

Permalink
refactor(linter): rename variable names prefix ESLint to Oxlint
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed May 24, 2024
1 parent 809e4fc commit 5bf595d
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 113 deletions.
8 changes: 4 additions & 4 deletions crates/oxc_cli/src/command/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ pub struct LintOptions {
/// Basic Configuration
#[derive(Debug, Clone, Bpaf)]
pub struct BasicOptions {
/// ESLint configuration file (experimental)
///
/// * only `.json` extension is supported
#[bpaf(long, short, argument("./eslintrc.json"))]
/// Oxlint configuration file (experimental)
/// * only `.json` extension is supported
/// * tries to be compatible with the ESLint v8's format
#[bpaf(long, short, argument("./oxlintrc.json"))]
pub config: Option<PathBuf>,

/// TypeScript `tsconfig.json` path for reading path alias and project references for import plugin
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_linter/src/config/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use serde::Deserialize;
// TODO: list the keys we support
// <https://eslint.org/docs/v8.x/use/configure/language-options#specifying-environments>
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct ESLintEnv(FxHashMap<String, bool>);
pub struct OxlintEnv(FxHashMap<String, bool>);

impl ESLintEnv {
impl OxlintEnv {
pub fn from_vec(env: Vec<String>) -> Self {
let map = env.into_iter().map(|key| (key, true)).collect();

Expand All @@ -21,7 +21,7 @@ impl ESLintEnv {
}
}

impl Default for ESLintEnv {
impl Default for OxlintEnv {
fn default() -> Self {
let mut map = FxHashMap::default();
map.insert("builtin".to_string(), true);
Expand All @@ -32,13 +32,13 @@ impl Default for ESLintEnv {

#[cfg(test)]
mod test {
use super::ESLintEnv;
use super::OxlintEnv;
use itertools::Itertools;
use serde::Deserialize;

#[test]
fn test_parse_env() {
let env = ESLintEnv::deserialize(&serde_json::json!({
let env = OxlintEnv::deserialize(&serde_json::json!({
"browser": true, "node": true, "es6": false
}))
.unwrap();
Expand All @@ -50,7 +50,7 @@ mod test {
}
#[test]
fn test_parse_env_default() {
let env = ESLintEnv::default();
let env = OxlintEnv::default();
assert_eq!(env.iter().count(), 1);
assert!(env.iter().contains(&"builtin"));
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/config/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_hash::FxHashMap;
/// Add or remove global variables.
// <https://eslint.org/docs/v8.x/use/configure/language-options#using-configuration-files-1>
#[derive(Debug, Default, Deserialize, JsonSchema)]
pub struct ESLintGlobals(FxHashMap<String, GlobalValue>);
pub struct OxlintGlobals(FxHashMap<String, GlobalValue>);

// TODO: support deprecated `false`
#[derive(Debug, Eq, PartialEq, Deserialize, JsonSchema)]
Expand All @@ -17,7 +17,7 @@ pub enum GlobalValue {
Off,
}

impl ESLintGlobals {
impl OxlintGlobals {
pub fn is_enabled(&self, name: &str) -> bool {
self.0.get(name).is_some_and(|value| *value != GlobalValue::Off)
}
Expand Down
24 changes: 12 additions & 12 deletions crates/oxc_linter/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use serde::Deserialize;
use crate::{rules::RuleEnum, AllowWarnDeny, RuleWithSeverity};

pub use self::{
env::ESLintEnv, globals::ESLintGlobals, rules::ESLintRules,
settings::jsdoc::JSDocPluginSettings, settings::ESLintSettings,
env::OxlintEnv, globals::OxlintGlobals, rules::OxlintRules,
settings::jsdoc::JSDocPluginSettings, settings::OxlintSettings,
};

/// Oxlint Configuration File
Expand Down Expand Up @@ -50,15 +50,15 @@ pub use self::{
/// ```
#[derive(Debug, Default, Deserialize, JsonSchema)]
#[serde(default)]
pub struct ESLintConfig {
pub struct OxlintConfig {
/// See [Oxlint Rules](./rules)
pub(crate) rules: ESLintRules,
pub(crate) settings: ESLintSettings,
pub(crate) env: ESLintEnv,
pub(crate) globals: ESLintGlobals,
pub(crate) rules: OxlintRules,
pub(crate) settings: OxlintSettings,
pub(crate) env: OxlintEnv,
pub(crate) globals: OxlintGlobals,
}

impl ESLintConfig {
impl OxlintConfig {
/// # Errors
///
/// * Parse Failure
Expand Down Expand Up @@ -169,20 +169,20 @@ impl ESLintConfig {

#[cfg(test)]
mod test {
use super::ESLintConfig;
use super::OxlintConfig;
use serde::Deserialize;
use std::env;

#[test]
fn test_from_file() {
let fixture_path = env::current_dir().unwrap().join("fixtures/eslint_config.json");
let config = ESLintConfig::from_file(&fixture_path).unwrap();
let config = OxlintConfig::from_file(&fixture_path).unwrap();
assert!(!config.rules.is_empty());
}

#[test]
fn test_deserialize() {
let config = ESLintConfig::deserialize(&serde_json::json!({
let config = OxlintConfig::deserialize(&serde_json::json!({
"rules": {
"no-console": "off",
"no-debugger": 2,
Expand Down Expand Up @@ -212,7 +212,7 @@ mod test {
}));
assert!(config.is_ok());

let ESLintConfig { rules, settings, env, globals } = config.unwrap();
let OxlintConfig { rules, settings, env, globals } = config.unwrap();
assert!(!rules.is_empty());
assert_eq!(settings.jsx_a11y.polymorphic_prop_name, Some("role".to_string()));
assert_eq!(env.iter().count(), 1);
Expand Down
28 changes: 14 additions & 14 deletions crates/oxc_linter/src/config/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::AllowWarnDeny;
// - type RuleConf = SeverityConf | [SeverityConf, ...any[]];
// <https://github.com/eslint/eslint/blob/ce838adc3b673e52a151f36da0eedf5876977514/lib/shared/types.js#L12>
#[derive(Debug, Clone, Default)]
pub struct ESLintRules(Vec<ESLintRule>);
pub struct OxlintRules(Vec<ESLintRule>);

#[derive(Debug, Clone)]
pub struct ESLintRule {
Expand All @@ -25,13 +25,13 @@ pub struct ESLintRule {
pub config: Option<serde_json::Value>,
}

impl JsonSchema for ESLintRules {
impl JsonSchema for OxlintRules {
fn schema_name() -> String {
"ESLintRules".to_owned()
"OxlintRules".to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed("ESLintRules")
Cow::Borrowed("OxlintRules")
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
Expand All @@ -52,15 +52,15 @@ impl JsonSchema for ESLintRules {
// - Handle single value form and array form
// - SeverityConf into AllowWarnDeny
// - Align plugin names
impl<'de> Deserialize<'de> for ESLintRules {
impl<'de> Deserialize<'de> for OxlintRules {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct ESLintRulesVisitor;
struct OxlintRulesVisitor;

impl<'de> Visitor<'de> for ESLintRulesVisitor {
type Value = ESLintRules;
impl<'de> Visitor<'de> for OxlintRulesVisitor {
type Value = OxlintRules;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Record<string, SeverityConf | [SeverityConf, ...any[]]>")
Expand All @@ -77,11 +77,11 @@ impl<'de> Deserialize<'de> for ESLintRules {
rules.push(ESLintRule { plugin_name, rule_name, severity, config });
}

Ok(ESLintRules(rules))
Ok(OxlintRules(rules))
}
}

deserializer.deserialize_any(ESLintRulesVisitor)
deserializer.deserialize_any(OxlintRulesVisitor)
}
}

Expand Down Expand Up @@ -145,7 +145,7 @@ fn parse_rule_value(
}
}

impl Deref for ESLintRules {
impl Deref for OxlintRules {
type Target = Vec<ESLintRule>;

fn deref(&self) -> &Self::Target {
Expand All @@ -159,12 +159,12 @@ fn failed_to_parse_rule_value(value: &str, err: &str) -> OxcDiagnostic {

#[cfg(test)]
mod test {
use super::ESLintRules;
use super::OxlintRules;
use serde::Deserialize;

#[test]
fn test_parse_rules() {
let rules = ESLintRules::deserialize(&serde_json::json!({
let rules = OxlintRules::deserialize(&serde_json::json!({
"no-console": "off",
"foo/no-unused-vars": [1],
"dummy": ["error", "arg1", "args2"],
Expand Down Expand Up @@ -200,7 +200,7 @@ mod test {

#[test]
fn test_parse_rules_default() {
let rules = ESLintRules::default();
let rules = OxlintRules::default();
assert!(rules.is_empty());
}
}
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/config/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use self::{

/// Shared settings for plugins
#[derive(Debug, Deserialize, Default, JsonSchema)]
pub struct ESLintSettings {
pub struct OxlintSettings {
#[serde(default)]
#[serde(rename = "jsx-a11y")]
pub jsx_a11y: JSXA11yPluginSettings,
Expand All @@ -30,12 +30,12 @@ pub struct ESLintSettings {

#[cfg(test)]
mod test {
use super::ESLintSettings;
use super::OxlintSettings;
use serde::Deserialize;

#[test]
fn test_parse_settings() {
let settings = ESLintSettings::deserialize(&serde_json::json!({
let settings = OxlintSettings::deserialize(&serde_json::json!({
"jsx-a11y": {
"polymorphicPropName": "role",
"components": {
Expand Down Expand Up @@ -82,7 +82,7 @@ mod test {

#[test]
fn test_parse_settings_default() {
let settings = ESLintSettings::default();
let settings = OxlintSettings::default();
assert!(settings.jsx_a11y.polymorphic_prop_name.is_none());
assert!(settings.jsx_a11y.components.is_empty());
}
Expand Down
14 changes: 7 additions & 7 deletions crates/oxc_linter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
disable_directives::{DisableDirectives, DisableDirectivesBuilder},
fixer::{Fix, Message},
javascript_globals::GLOBALS,
AllowWarnDeny, ESLintConfig, ESLintEnv, ESLintGlobals, ESLintSettings,
AllowWarnDeny, OxlintConfig, OxlintEnv, OxlintGlobals, OxlintSettings,
};

#[derive(Clone)]
Expand All @@ -25,7 +25,7 @@ pub struct LintContext<'a> {

file_path: Rc<Path>,

eslint_config: Arc<ESLintConfig>,
eslint_config: Arc<OxlintConfig>,

// states
current_rule_name: &'static str,
Expand All @@ -43,7 +43,7 @@ impl<'a> LintContext<'a> {
disable_directives: Rc::new(disable_directives),
fix: false,
file_path: file_path.into(),
eslint_config: Arc::new(ESLintConfig::default()),
eslint_config: Arc::new(OxlintConfig::default()),
current_rule_name: "",
severity: Severity::Warning,
}
Expand All @@ -56,7 +56,7 @@ impl<'a> LintContext<'a> {
}

#[must_use]
pub fn with_eslint_config(mut self, eslint_config: &Arc<ESLintConfig>) -> Self {
pub fn with_eslint_config(mut self, eslint_config: &Arc<OxlintConfig>) -> Self {
self.eslint_config = Arc::clone(eslint_config);
self
}
Expand Down Expand Up @@ -93,15 +93,15 @@ impl<'a> LintContext<'a> {
&self.file_path
}

pub fn settings(&self) -> &ESLintSettings {
pub fn settings(&self) -> &OxlintSettings {
&self.eslint_config.settings
}

pub fn globals(&self) -> &ESLintGlobals {
pub fn globals(&self) -> &OxlintGlobals {
&self.eslint_config.globals
}

pub fn env(&self) -> &ESLintEnv {
pub fn env(&self) -> &OxlintEnv {
&self.eslint_config.env
}

Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ use oxc_diagnostics::Error;
use oxc_semantic::AstNode;

pub use crate::{
config::ESLintConfig,
config::OxlintConfig,
context::LintContext,
options::{AllowWarnDeny, LintOptions},
rule::{RuleCategory, RuleMeta, RuleWithSeverity},
service::{LintService, LintServiceOptions},
};
use crate::{
config::{ESLintEnv, ESLintGlobals, ESLintSettings},
config::{OxlintEnv, OxlintGlobals, OxlintSettings},
fixer::Fix,
fixer::{Fixer, Message},
rules::RuleEnum,
Expand All @@ -54,7 +54,7 @@ fn size_asserts() {
pub struct Linter {
rules: Vec<RuleWithSeverity>,
options: LintOptions,
eslint_config: Arc<ESLintConfig>,
eslint_config: Arc<OxlintConfig>,
}

impl Default for Linter {
Expand All @@ -80,7 +80,7 @@ impl Linter {
}

#[must_use]
pub fn with_eslint_config(mut self, eslint_config: ESLintConfig) -> Self {
pub fn with_eslint_config(mut self, eslint_config: OxlintConfig) -> Self {
self.eslint_config = Arc::new(eslint_config);
self
}
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde_json::{Number, Value};

use oxc_diagnostics::{Error, OxcDiagnostic, Severity};

use crate::{config::ESLintConfig, rules::RULES, RuleCategory, RuleEnum, RuleWithSeverity};
use crate::{config::OxlintConfig, rules::RULES, RuleCategory, RuleEnum, RuleWithSeverity};

#[derive(Debug)]
pub struct LintOptions {
Expand Down Expand Up @@ -204,9 +204,9 @@ impl LintOptions {
/// # Errors
///
/// * Returns `Err` if there are any errors parsing the configuration file.
pub fn derive_rules_and_config(&self) -> Result<(Vec<RuleWithSeverity>, ESLintConfig), Error> {
pub fn derive_rules_and_config(&self) -> Result<(Vec<RuleWithSeverity>, OxlintConfig), Error> {
let config =
self.config_path.as_ref().map(|path| ESLintConfig::from_file(path)).transpose()?;
self.config_path.as_ref().map(|path| OxlintConfig::from_file(path)).transpose()?;

let mut rules: FxHashSet<RuleWithSeverity> = FxHashSet::default();
let all_rules = self.get_filtered_rules();
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/react/rules_of_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ fn is_memo_or_forward_ref_callback(nodes: &AstNodes, node_id: AstNodeId) -> bool
fn test() {
/// Copyright (c) Meta Platforms, Inc. and affiliates.
/// Most of these tests are sourced from the original react `eslint-plugin-react-hooks` package.
/// https://github.com/facebook/react/blob/5b903cdaa94c78e8fabb985d8daca5bd7d266323/packages/eslint-plugin-react-hooks/__tests__/ESLintRulesOfHooks-test.js#L43
/// https://github.com/facebook/react/blob/5b903cdaa94c78e8fabb985d8daca5bd7d266323/packages/eslint-plugin-react-hooks/__tests__/ESLintRulesOfHooks-test.js
use crate::tester::Tester;

let pass = vec![
Expand Down
Loading

0 comments on commit 5bf595d

Please sign in to comment.