Skip to content
Merged
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
26 changes: 20 additions & 6 deletions crates/oxc_linter/src/rules/eslint/id_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use oxc_ast::ast::{
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{ContentEq, GetSpan, Span};
use schemars::JsonSchema;

use crate::{AstNode, context::LintContext, rule::Rule};
use icu_segmenter::GraphemeClusterSegmenter;
Expand All @@ -24,7 +25,8 @@ fn id_length_is_too_long_diagnostic(span: Span, config_max: u64) -> OxcDiagnosti
const DEFAULT_MAX_LENGTH: u64 = u64::MAX;
const DEFAULT_MIN_LENGTH: u64 = 2;

#[derive(Debug, Default, Clone, PartialEq)]
#[derive(Debug, Default, Clone, PartialEq, JsonSchema)]
#[serde(rename_all = "lowercase")]
enum PropertyKind {
#[default]
Always,
Expand All @@ -37,7 +39,7 @@ impl PropertyKind {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct IdLength(Box<IdLengthConfig>);

impl Deref for IdLength {
Expand All @@ -48,24 +50,35 @@ impl Deref for IdLength {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, JsonSchema)]
#[serde(rename_all = "camelCase", default)]
pub struct IdLengthConfig {
/// An array of regex patterns for identifiers to exclude from the rule.
/// For example, `["^x.*"]` would exclude all identifiers starting with "x".
#[schemars(with = "Vec<String>")]
exception_patterns: Vec<Regex>,
/// An array of identifier names that are excluded from the rule.
/// For example, `["x", "y", "z"]` would allow single-letter identifiers "x", "y", and "z".
exceptions: Vec<String>,
/// The maximum number of graphemes allowed in an identifier.
/// Defaults to no maximum (effectively unlimited).
max: u64,
/// The minimum number of graphemes required in an identifier.
min: u64,
/// When set to `"never"`, property names are not checked for length.
/// When set to `"always"` (default), property names are checked just like other identifiers.
properties: PropertyKind,
}

impl Default for IdLength {
impl Default for IdLengthConfig {
fn default() -> Self {
Self(Box::new(IdLengthConfig {
Self {
exception_patterns: vec![],
exceptions: vec![],
max: DEFAULT_MAX_LENGTH,
min: DEFAULT_MIN_LENGTH,
properties: PropertyKind::default(),
}))
}
}
}

Expand Down Expand Up @@ -149,6 +162,7 @@ declare_oxc_lint!(
IdLength,
eslint,
style,
config = IdLengthConfig
);

impl Rule for IdLength {
Expand Down
Loading