Skip to content
Draft
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
5 changes: 5 additions & 0 deletions crates/oxc_linter/src/generated/rule_runner_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
109 changes: 109 additions & 0 deletions crates/oxc_linter/src/rules/vue/no_this_in_before_route_enter.rs
Original file line number Diff line number Diff line change
@@ -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 <https://oxc.rs/docs/contribute/linter/adding-rules.html#diagnostics> 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 <https://github.com/oxc-project/oxc/issues/6050> 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#"
<template>
<p>{{ greeting }} World!</p>
</template>

<script>
export default {
data () {
return {
greeting: "Hello"
};
},
};"#,
None,
None,
Some(PathBuf::from("test.vue")),
)];

let fail = vec![];

Tester::new(NoThisInBeforeRouteEnter::NAME, NoThisInBeforeRouteEnter::PLUGIN, pass, fail)
.test_and_snapshot();
}
Loading