Skip to content
Merged
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
16 changes: 13 additions & 3 deletions crates/oxc_linter/src/rules/eslint/class_methods_use_this.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::AstNode;
use oxc_span::{CompactStr, GetSpan, Span};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{LintContext, rule::Rule};

Expand All @@ -20,11 +22,17 @@ fn class_methods_use_this_diagnostic(span: Span, name: Option<Cow<'_, str>>) ->
.with_label(span)
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, JsonSchema, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct ClassMethodsUseThisConfig {
/// List of method names to exempt from this rule.
except_methods: Vec<MethodException>,
/// Enforce this rule for class fields that are functions.
enforce_for_class_fields: bool,
/// Whether to ignore methods that are overridden.
ignore_override_methods: bool,
/// Whether to ignore classes that implement interfaces.
#[schemars(with = "IgnoreClassWithImplements")]
ignore_classes_with_implements: Option<IgnoreClassWithImplements>,
}

Expand All @@ -50,13 +58,14 @@ impl Deref for ClassMethodsUseThis {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, JsonSchema, Serialize, Deserialize)]
struct MethodException {
name: CompactStr,
private: bool,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, JsonSchema, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
enum IgnoreClassWithImplements {
All,
PublicFields,
Expand Down Expand Up @@ -101,6 +110,7 @@ declare_oxc_lint!(
ClassMethodsUseThis,
eslint,
restriction,
config = ClassMethodsUseThisConfig,
);

impl Rule for ClassMethodsUseThis {
Expand Down
89 changes: 43 additions & 46 deletions crates/oxc_linter/src/rules/eslint/grouped_accessor_pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use rustc_hash::FxHashMap;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{AstNode, context::LintContext, rule::Rule};
Expand All @@ -31,7 +33,8 @@ fn grouped_accessor_pairs_diagnostic(
.with_labels([getter_label_span, setter_label_span])
}

#[derive(Debug, Default, PartialEq, Clone, Copy)]
#[derive(Debug, Default, PartialEq, Clone, Copy, JsonSchema, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
enum PairOrder {
#[default]
AnyOrder,
Expand All @@ -49,9 +52,46 @@ impl PairOrder {
}
}

#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, JsonSchema, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct GroupedAccessorPairs {
/// A string value to control the order of the getter/setter pairs:
/// - `"anyOrder"`: Accessors can be in any order
/// - `"getBeforeSet"`: Getters must come before setters
/// - `"setBeforeGet"`: Setters must come before getters
pair_order: PairOrder,
/// When `enforceForTSTypes` is enabled, this rule also applies to TypeScript interfaces and type aliases:
///
/// Examples of **incorrect** TypeScript code:
/// ```ts
/// interface Foo {
/// get a(): string;
/// someProperty: string;
/// set a(value: string);
/// }
///
/// type Bar = {
/// get b(): string;
/// someProperty: string;
/// set b(value: string);
/// };
/// ```
///
/// Examples of **correct** TypeScript code:
/// ```ts
/// interface Foo {
/// get a(): string;
/// set a(value: string);
/// someProperty: string;
/// }
///
/// type Bar = {
/// get b(): string;
/// set b(value: string);
/// someProperty: string;
/// };
/// ```
#[serde(rename = "enforceForTSTypes")]
enforce_for_ts_types: bool,
}

Expand Down Expand Up @@ -140,54 +180,11 @@ declare_oxc_lint!(
/// }
/// };
/// ```
///
/// ### Options
///
/// This rule accepts two arguments:
/// 1. A string value to control the order of the getter/setter pairs:
/// - `"anyOrder"` (default): Accessors can be in any order
/// - `"getBeforeSet"`: Getters must come before setters
/// - `"setBeforeGet"`: Setters must come before getters
/// 2. An object with the following option:
/// - `enforceForTSTypes` (boolean, default: false): When enabled, also checks TypeScript interfaces and type aliases for grouped accessor pairs
///
/// ### TypeScript
///
/// When `enforceForTSTypes` is enabled, this rule also applies to TypeScript interfaces and type aliases:
///
/// Examples of **incorrect** TypeScript code:
/// ```ts
/// interface Foo {
/// get a(): string;
/// someProperty: string;
/// set a(value: string);
/// }
///
/// type Bar = {
/// get b(): string;
/// someProperty: string;
/// set b(value: string);
/// };
/// ```
///
/// Examples of **correct** TypeScript code:
/// ```ts
/// interface Foo {
/// get a(): string;
/// set a(value: string);
/// someProperty: string;
/// }
///
/// type Bar = {
/// get b(): string;
/// set b(value: string);
/// someProperty: string;
/// };
/// ```
GroupedAccessorPairs,
eslint,
style,
pending,
config = GroupedAccessorPairs,
);

impl Rule for GroupedAccessorPairs {
Expand Down
10 changes: 8 additions & 2 deletions crates/oxc_linter/src/rules/eslint/max_lines.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use schemars::JsonSchema;
use serde_json::Value;

use crate::{context::LintContext, rule::Rule, utils::count_comment_lines};
Expand All @@ -14,10 +15,14 @@ fn max_lines_diagnostic(count: usize, max: usize, span: Span) -> OxcDiagnostic {
#[derive(Debug, Default, Clone)]
pub struct MaxLines(Box<MaxLinesConfig>);

#[derive(Debug, Clone)]
#[derive(Debug, Clone, JsonSchema)]
#[serde(rename_all = "camelCase", default)]
pub struct MaxLinesConfig {
/// Maximum number of lines allowed per file.
max: usize,
/// Whether to ignore blank lines when counting.
skip_blank_lines: bool,
/// Whether to ignore comments when counting.
skip_comments: bool,
}

Expand Down Expand Up @@ -49,7 +54,8 @@ declare_oxc_lint!(
/// Recommendations usually range from 100 to 500 lines.
MaxLines,
eslint,
pedantic
pedantic,
config = MaxLinesConfig,
);

impl Rule for MaxLines {
Expand Down
71 changes: 32 additions & 39 deletions crates/oxc_linter/src/rules/typescript/no_require_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::IsGlobalReference;
use oxc_span::{CompactStr, Span};
use schemars::JsonSchema;

use crate::{AstNode, context::LintContext, rule::Rule};

Expand All @@ -20,9 +21,37 @@ fn no_require_imports_diagnostic(span: Span) -> OxcDiagnostic {
#[derive(Debug, Default, Clone)]
pub struct NoRequireImports(Box<NoRequireImportsConfig>);

#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, JsonSchema)]
#[serde(rename_all = "camelCase", default)]
pub struct NoRequireImportsConfig {
/// These strings will be compiled into regular expressions with the u flag and be used to test against the imported path.
/// A common use case is to allow importing `package.json`. This is because `package.json` commonly lives outside of the TS root directory,
/// so statically importing it would lead to root directory conflicts, especially with `resolveJsonModule` enabled.
/// You can also use it to allow importing any JSON if your environment doesn't support JSON modules, or use it for other cases where `import` statements cannot work.
///
/// With `{ allow: ['/package\\.json$'] }`:
///
/// Examples of **correct** code for this rule:
/// ```ts
/// console.log(require('../package.json').version);
/// ```
allow: Vec<CompactStr>,
/// When set to `true`, `import ... = require(...)` declarations won't be reported.
/// This is useful if you use certain module options that require strict CommonJS interop semantics.
///
/// When set to `true`:
///
/// Examples of **incorrect** code for this rule:
/// ```ts
/// var foo = require('foo');
/// const foo = require('foo');
/// let foo = require('foo');
/// ```
/// Examples of **correct** code for this rule:
/// ```ts
/// import foo = require('foo');
/// import foo from 'foo';
/// ```
allow_as_import: bool,
}

Expand Down Expand Up @@ -75,47 +104,11 @@ declare_oxc_lint!(
/// import { lib2 } from 'lib2';
/// import * as lib3 from 'lib3';
/// ```
///
/// ### Options
///
/// #### `allow`
///
/// array of strings
///
/// These strings will be compiled into regular expressions with the u flag and be used to test against the imported path.
/// A common use case is to allow importing `package.json`. This is because `package.json` commonly lives outside of the TS root directory,
/// so statically importing it would lead to root directory conflicts, especially with `resolveJsonModule` enabled.
/// You can also use it to allow importing any JSON if your environment doesn't support JSON modules, or use it for other cases where `import` statements cannot work.
///
/// With { allow: ['/package\\.json$'] }:
///
/// Examples of **correct** code for this rule:
/// ```ts
/// console.log(require('../package.json').version);
/// ```
///
/// #### `allowAsImport`
///
/// When set to `true`, `import ... = require(...)` declarations won't be reported.
/// This is useful if you use certain module options that require strict CommonJS interop semantics.
///
/// With `{ allowAsImport: true }`:
///
/// Examples of **incorrect** code for this rule:
/// ```ts
/// var foo = require('foo');
/// const foo = require('foo');
/// let foo = require('foo');
/// ```
/// Examples of **correct** code for this rule:
/// ```ts
/// import foo = require('foo');
/// import foo from 'foo';
/// ```
NoRequireImports,
typescript,
restriction,
pending // TODO: fixer (change require to import)
pending, // TODO: fixer (change require to import)
config = NoRequireImportsConfig,
);

fn match_argument_value_with_regex(allow: &[CompactStr], argument_value: &str) -> bool {
Expand Down
Loading