diff --git a/crates/oxc_linter/src/generated/rule_runner_impls.rs b/crates/oxc_linter/src/generated/rule_runner_impls.rs index d089ddeedf212..e2c1a155a7705 100644 --- a/crates/oxc_linter/src/generated/rule_runner_impls.rs +++ b/crates/oxc_linter/src/generated/rule_runner_impls.rs @@ -3551,6 +3551,11 @@ impl RuleRunner for crate::rules::vue::no_required_prop_with_default::NoRequired const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run; } +impl RuleRunner for crate::rules::vue::no_this_in_before_route_enter::NoThisInBeforeRouteEnter { + const NODE_TYPES: Option<&AstTypesBitset> = None; + const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run; +} + impl RuleRunner for crate::rules::vue::prefer_import_from_vue::PreferImportFromVue { const NODE_TYPES: Option<&AstTypesBitset> = None; const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::RunOnce; diff --git a/crates/oxc_linter/src/rules.rs b/crates/oxc_linter/src/rules.rs index 11a83fb0570ae..b541ba92a1daa 100644 --- a/crates/oxc_linter/src/rules.rs +++ b/crates/oxc_linter/src/rules.rs @@ -650,6 +650,7 @@ pub(crate) mod vue { pub mod no_import_compiler_macros; pub mod no_multiple_slot_args; pub mod no_required_prop_with_default; + pub mod no_this_in_before_route_enter; pub mod prefer_import_from_vue; pub mod require_default_export; pub mod require_typed_ref; @@ -1258,6 +1259,7 @@ oxc_macros::declare_all_lint_rules! { vue::no_export_in_script_setup, vue::no_multiple_slot_args, vue::no_required_prop_with_default, + vue::no_this_in_before_route_enter, vue::prefer_import_from_vue, vue::require_default_export, vue::require_typed_ref, diff --git a/crates/oxc_linter/src/rules/vue/no_this_in_before_route_enter.rs b/crates/oxc_linter/src/rules/vue/no_this_in_before_route_enter.rs new file mode 100644 index 0000000000000..26832c2b44374 --- /dev/null +++ b/crates/oxc_linter/src/rules/vue/no_this_in_before_route_enter.rs @@ -0,0 +1,109 @@ +use oxc_ast::{ + AstKind, + ast::{Argument, Expression, Function, ThisExpression}, +}; +use oxc_ast_visit::Visit; +use oxc_diagnostics::OxcDiagnostic; +use oxc_macros::declare_oxc_lint; +use oxc_semantic::ScopeFlags; +use oxc_span::Span; + +use crate::{AstNode, context::LintContext, rule::Rule}; + +fn no_this_in_before_route_enter_diagnostic(span: Span) -> OxcDiagnostic { + // See for details + OxcDiagnostic::warn("Should be an imperative statement about what is wrong") + .with_help("Should be a command-like statement that tells the user how to fix the issue") + .with_label(span) +} + +#[derive(Debug, Default, Clone)] +pub struct NoThisInBeforeRouteEnter; + +// See for documentation details. +declare_oxc_lint!( + /// ### What it does + /// + /// Briefly describe the rule's purpose. + /// + /// ### Why is this bad? + /// + /// Explain why violating this rule is problematic. + /// + /// ### Examples + /// + /// Examples of **incorrect** code for this rule: + /// ```js + /// FIXME: Tests will fail if examples are missing or syntactically incorrect. + /// ``` + /// + /// Examples of **correct** code for this rule: + /// ```js + /// FIXME: Tests will fail if examples are missing or syntactically incorrect. + /// ``` + NoThisInBeforeRouteEnter, + vue, + correctness, +); + +impl Rule for NoThisInBeforeRouteEnter { + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { + let AstKind::CallExpression(call_expr) = node.kind() else { + return; + }; + + let Some(ident) = call_expr.callee.get_identifier_reference() else { + return; + }; + + if ident.name != "beforeRouteEnter" { + return; + } + } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.file_path().extension().is_some_and(|ext| ext == "vue") + } +} + +struct ThisFinder { + found: bool, +} + +impl<'a> Visit<'a> for ThisFinder { + fn visit_this_expression(&mut self, _expr: &ThisExpression) { + self.found = true; + } + + fn visit_function(&mut self, _func: &Function<'a>, _flags: ScopeFlags) {} +} + +#[test] +fn test() { + use crate::tester::Tester; + use std::path::PathBuf; + + let pass = vec![( + r#" + + +