Skip to content

Commit

Permalink
feat(linter): implement no-case-declarations (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
cijiugechu authored Jun 28, 2023
1 parent a323206 commit be2200d
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ oxc_macros::declare_all_lint_rules! {
eslint::getter_return,
eslint::no_array_constructor,
eslint::no_async_promise_executor,
eslint::no_case_declarations,
eslint::no_bitwise,
eslint::no_caller,
eslint::no_class_assign,
Expand Down
97 changes: 97 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_case_declarations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use oxc_ast::{AstKind, ast::{Statement, Declaration}};
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

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

#[derive(Debug, Error, Diagnostic)]
#[error("eslint(no-case-declarations): Unexpected lexical declaration in case block.")]
#[diagnostic(severity(warning))]
struct NoCaseDeclarationsDiagnostic(#[label] pub Span);

#[derive(Debug, Default, Clone)]
pub struct NoCaseDeclarations;

declare_oxc_lint!(
/// ### What it does
/// Disallow lexical declarations in case clauses.
///
/// ### Why is this bad?
/// The reason is that the lexical declaration is visible
/// in the entire switch block but it only gets initialized when it is assigned,
/// which will only happen if the case where it is defined is reached.
///
/// ### Example
/// ```javascript
// switch (foo) {
// case 1:
// let x = 1;
// break;
// case 2:
// const y = 2;
// break;
// case 3:
// function f() {}
// break;
// default:
// class C {}
// }
/// ```
NoCaseDeclarations,
correctness
);


impl Rule for NoCaseDeclarations {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::SwitchCase(switch_case) = node.kind() {
let consequent = &switch_case.consequent;

for stmt in consequent {
if let Statement::Declaration(dcl) = stmt {
match dcl {
Declaration::FunctionDeclaration(d) => {
ctx.diagnostic(NoCaseDeclarationsDiagnostic(d.span));
}
Declaration::ClassDeclaration(d) => {
ctx.diagnostic(NoCaseDeclarationsDiagnostic(d.span));
}
Declaration::VariableDeclaration(var) if var.kind.is_lexical() => {
ctx.diagnostic(NoCaseDeclarationsDiagnostic(var.span));
}
_ => {}
}
};
}
}
}
}

#[test]
fn test() {
use crate::tester::Tester;

let pass = vec![
("switch (a) { case 1: { let x = 1; break; } default: { let x = 2; break; } }", None),
("switch (a) { case 1: { const x = 1; break; } default: { const x = 2; break; } }", None),
("switch (a) { case 1: { function f() {} break; } default: { function f() {} break; } }", None),
("switch (a) { case 1: { class C {} break; } default: { class C {} break; } }", None)
];

let fail = vec![
("switch (a) { case 1: let x = 1; break; }", None),
("switch (a) { default: let x = 2; break; }", None),
("switch (a) { case 1: const x = 1; break; }", None),
("switch (a) { default: const x = 2; break; }", None),
("switch (a) { case 1: function f() {} break; }", None),
("switch (a) { default: function f() {} break; }", None),
("switch (a) { case 1: class C {} break; }", None),
("switch (a) { default: class C {} break; }", None)
];

Tester::new(NoCaseDeclarations::NAME, pass, fail).test_and_snapshot();
}
53 changes: 53 additions & 0 deletions crates/oxc_linter/src/snapshots/no_case_declarations.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
source: crates/oxc_linter/src/tester.rs
expression: no_case_declarations
---
eslint(no-case-declarations): Unexpected lexical declaration in case block.
╭─[no_case_declarations.tsx:1:1]
1switch (a) { case 1: let x = 1; break; }
· ──────────
╰────

eslint(no-case-declarations): Unexpected lexical declaration in case block.
╭─[no_case_declarations.tsx:1:1]
1switch (a) { default: let x = 2; break; }
· ──────────
╰────

eslint(no-case-declarations): Unexpected lexical declaration in case block.
╭─[no_case_declarations.tsx:1:1]
1switch (a) { case 1: const x = 1; break; }
· ────────────
╰────

eslint(no-case-declarations): Unexpected lexical declaration in case block.
╭─[no_case_declarations.tsx:1:1]
1switch (a) { default: const x = 2; break; }
· ────────────
╰────

eslint(no-case-declarations): Unexpected lexical declaration in case block.
╭─[no_case_declarations.tsx:1:1]
1switch (a) { case 1: function f() {} break; }
· ───────────────
╰────

eslint(no-case-declarations): Unexpected lexical declaration in case block.
╭─[no_case_declarations.tsx:1:1]
1switch (a) { default: function f() {} break; }
· ───────────────
╰────

eslint(no-case-declarations): Unexpected lexical declaration in case block.
╭─[no_case_declarations.tsx:1:1]
1switch (a) { case 1: class C {} break; }
· ──────────
╰────

eslint(no-case-declarations): Unexpected lexical declaration in case block.
╭─[no_case_declarations.tsx:1:1]
1switch (a) { default: class C {} break; }
· ──────────
╰────


0 comments on commit be2200d

Please sign in to comment.