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
39 changes: 23 additions & 16 deletions crates/oxc_linter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use oxc_semantic::{AstNodes, JSDocFinder, ScopeTree, Semantic, SymbolTable};
use oxc_span::{GetSpan, SourceType, Span};
use oxc_syntax::module_record::ModuleRecord;

#[cfg(debug_assertions)]
use crate::rule::RuleFixMeta;
use crate::{
config::OxlintRules,
disable_directives::{DisableDirectives, DisableDirectivesBuilder},
Expand Down Expand Up @@ -41,6 +43,8 @@ pub struct LintContext<'a> {
// states
current_plugin_prefix: &'static str,
current_rule_name: &'static str,
#[cfg(debug_assertions)]
current_rule_fix_capabilities: RuleFixMeta,

/// Current rule severity. Allows for user severity overrides, e.g.
/// ```json
Expand Down Expand Up @@ -79,6 +83,8 @@ impl<'a> LintContext<'a> {
eslint_config: Arc::new(OxlintConfig::default()),
current_plugin_prefix: "eslint",
current_rule_name: "",
#[cfg(debug_assertions)]
current_rule_fix_capabilities: RuleFixMeta::None,
severity: Severity::Warning,
frameworks: FrameworkFlags::empty(),
}
Expand All @@ -105,6 +111,12 @@ impl<'a> LintContext<'a> {
self
}

#[cfg(debug_assertions)]
pub fn with_rule_fix_capabilities(mut self, capabilities: RuleFixMeta) -> Self {
self.current_rule_fix_capabilities = capabilities;
self
}

pub fn with_severity(mut self, severity: AllowWarnDeny) -> Self {
self.severity = Severity::from(severity);
self
Expand Down Expand Up @@ -287,6 +299,7 @@ impl<'a> LintContext<'a> {
self.diagnostic_with_fix_of_kind(diagnostic, FixKind::DangerousFix, fix);
}

#[allow(clippy::missing_panics_doc)] // only panics in debug mode
pub fn diagnostic_with_fix_of_kind<C, F>(
&self,
diagnostic: OxcDiagnostic,
Expand All @@ -296,24 +309,18 @@ impl<'a> LintContext<'a> {
C: Into<RuleFix<'a>>,
F: FnOnce(RuleFixer<'_, 'a>) -> C,
{
// if let Some(accepted_fix_kind) = self.fix {
// let fixer = RuleFixer::new(fix_kind, self);
// let rule_fix: RuleFix<'a> = fix(fixer).into();
// let diagnostic = match (rule_fix.message(), &diagnostic.help) {
// (Some(message), None) => diagnostic.with_help(message.to_owned()),
// _ => diagnostic,
// };
// if rule_fix.kind() <= accepted_fix_kind {
// let fix = rule_fix.into_fix(self.source_text());
// self.add_diagnostic(Message::new(diagnostic, Some(fix)));
// } else {
// self.diagnostic(diagnostic);
// }
// } else {
// self.diagnostic(diagnostic);
// }
let fixer = RuleFixer::new(fix_kind, self);
let rule_fix: RuleFix<'a> = fix(fixer).into();
#[cfg(debug_assertions)]
{
assert!(
self.current_rule_fix_capabilities.supports_fix(fix_kind),
"Rule `{}` does not support safe fixes. Did you forget to update fix capabilities in declare_oxc_lint?.\n\tSupported fix kinds: {:?}\n\tAttempted fix kind: {:?}",
self.current_rule_name,
FixKind::from(self.current_rule_fix_capabilities),
rule_fix.kind()
);
}
let diagnostic = match (rule_fix.message(), &diagnostic.help) {
(Some(message), None) => diagnostic.with_help(message.to_owned()),
_ => diagnostic,
Expand Down
25 changes: 13 additions & 12 deletions crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,7 @@ impl Linter {
.rules
.iter()
.filter(|rule| rule.should_run(&ctx))
.map(|rule| {
let rule_name = rule.name();
let plugin_name = self.map_jest(rule.plugin_name(), rule_name);

(
rule,
ctx.clone()
.with_plugin_name(plugin_name)
.with_rule_name(rule_name)
.with_severity(rule.severity),
)
})
.map(|rule| (rule, self.ctx_for_rule(&ctx, rule)))
.collect::<Vec<_>>();

for (rule, ctx) in &rules {
Expand Down Expand Up @@ -186,6 +175,18 @@ impl Linter {
ctx
}

fn ctx_for_rule<'a>(&self, ctx: &LintContext<'a>, rule: &RuleWithSeverity) -> LintContext<'a> {
let rule_name = rule.name();
let plugin_name = self.map_jest(rule.plugin_name(), rule_name);

#[cfg(debug_assertions)]
let ctx = ctx.clone().with_rule_fix_capabilities(rule.rule.fix());
#[cfg(not(debug_assertions))]
let ctx = ctx.clone();

ctx.with_plugin_name(plugin_name).with_rule_name(rule_name).with_severity(rule.severity)
}

fn map_jest(&self, plugin_name: &'static str, rule_name: &str) -> &'static str {
if self.options.vitest_plugin
&& plugin_name == "jest"
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/eslint/eqeqeq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ declare_oxc_lint!(
/// a == b
/// ```
Eqeqeq,
pedantic
pedantic,
conditional_fix
);

impl Rule for Eqeqeq {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ declare_oxc_lint!(
/// debugger;
/// ```
NoDebugger,
correctness
correctness,
fix
);

impl Rule for NoDebugger {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/eslint/no_div_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ declare_oxc_lint!(
/// ```
NoDivRegex,
restriction,
fix
);

impl Rule for NoDivRegex {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_unsafe_negation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ declare_oxc_lint!(
/// }
/// ```
NoUnsafeNegation,
correctness
correctness,
fix
);

impl Rule for NoUnsafeNegation {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_unused_labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ declare_oxc_lint!(
/// }
/// ```
NoUnusedLabels,
correctness
correctness,
fix
);

impl Rule for NoUnusedLabels {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ declare_oxc_lint!(
///```
NoUselessConstructor,
suspicious,
fix
);

impl Rule for NoUselessConstructor {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_useless_escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ declare_oxc_lint!(
/// ```javascript
/// ```
NoUselessEscape,
correctness
correctness,
fix
);

impl Rule for NoUselessEscape {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/eslint/sort_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ declare_oxc_lint!(
/// import e from 'bar.js';
/// ```
SortImports,
style
style,
conditional_fix
);

impl Rule for SortImports {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/eslint/unicode_bom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ declare_oxc_lint!(
/// ```
UnicodeBom,
restriction,
fix
);

impl Rule for UnicodeBom {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/eslint/use_isnan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ declare_oxc_lint!(
/// ```
UseIsnan,
correctness,
conditional_fix
);

impl Rule for UseIsnan {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/eslint/valid_typeof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ declare_oxc_lint!(
/// ```
ValidTypeof,
correctness,
conditional_fix
);

impl Rule for ValidTypeof {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/jest/consistent_test_it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ declare_oxc_lint!(
/// }
ConsistentTestIt,
style,
fix
);

impl Rule for ConsistentTestIt {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/jest/no_alias_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ declare_oxc_lint!(
/// }
/// ```
NoAliasMethods,
style
style,
fix
);

impl Rule for NoAliasMethods {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ declare_oxc_lint!(
/// ```
NoDeprecatedFunctions,
style,
fix
);

const DEPRECATED_FUNCTIONS_MAP: Map<&'static str, (usize, &'static str)> = phf_map! {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/jest/no_focused_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ declare_oxc_lint!(
/// }
/// ```
NoFocusedTests,
correctness
correctness,
fix
);

impl Rule for NoFocusedTests {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/jest/no_jasmine_globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ declare_oxc_lint!(
/// });
/// ```
NoJasmineGlobals,
style
style,
conditional_fix
);

const NON_JASMINE_PROPERTY_NAMES: [&str; 4] = ["spyOn", "spyOnProperty", "fail", "pending"];
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/jest/no_test_prefixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ declare_oxc_lint!(
/// }
/// ```
NoTestPrefixes,
style
style,
fix
);

impl Rule for NoTestPrefixes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ declare_oxc_lint!(
///
NoUntypedMockFactory,
style,
conditional_fix
);

impl Rule for NoUntypedMockFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ declare_oxc_lint!(
///
PreferComparisonMatcher,
style,
fix
);

impl Rule for PreferComparisonMatcher {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ declare_oxc_lint!(
/// ```
PreferExpectResolves,
style,
fix
);

impl Rule for PreferExpectResolves {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/jest/prefer_jest_mocked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ declare_oxc_lint!(
/// ```
PreferJestMocked,
style,
fix
);

impl Rule for PreferJestMocked {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ declare_oxc_lint!(
///
PreferLowercaseTitle,
style,
fix
);

impl Rule for PreferLowercaseTitle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ declare_oxc_lint!(
///
PreferMockPromiseShorthand,
style,
conditional_fix
);

impl Rule for PreferMockPromiseShorthand {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/jest/prefer_spy_on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ declare_oxc_lint!(
/// ```
PreferSpyOn,
style,
fix
);

impl Rule for PreferSpyOn {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/jest/prefer_strict_equal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ declare_oxc_lint!(
///
PreferStrictEqual,
style,
fix
);

impl Rule for PreferStrictEqual {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/jest/prefer_to_be.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ declare_oxc_lint!(
/// ```
PreferToBe,
style,
fix
);

#[derive(Clone, Debug, PartialEq)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ declare_oxc_lint!(
///
PreferToHaveLength,
style,
fix
);

impl Rule for PreferToHaveLength {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/jest/prefer_todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ declare_oxc_lint!(
/// ```
PreferTodo,
style,
fix
);

impl Rule for PreferTodo {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/jsx_a11y/aria_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ declare_oxc_lint!(
/// <input aria-labelledby="address_label" />
/// ```
AriaProps,
correctness
correctness,
conditional_fix
);

impl Rule for AriaProps {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/jsx_a11y/no_autofocus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ declare_oxc_lint!(
/// ```
///
NoAutofocus,
correctness
correctness,
fix
);

impl NoAutofocus {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/oxc/no_const_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ declare_oxc_lint!(
/// ```
NoConstEnum,
restriction,
fix
);

impl Rule for NoConstEnum {
Expand Down
Loading