-
-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): implement
no-case-declarations
(#491)
- Loading branch information
1 parent
a323206
commit be2200d
Showing
3 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
crates/oxc_linter/src/rules/eslint/no_case_declarations.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
1 │ switch (a) { case 1: let x = 1; break; } | ||
· ────────── | ||
╰──── | ||
|
||
⚠ eslint(no-case-declarations): Unexpected lexical declaration in case block. | ||
╭─[no_case_declarations.tsx:1:1] | ||
1 │ switch (a) { default: let x = 2; break; } | ||
· ────────── | ||
╰──── | ||
|
||
⚠ eslint(no-case-declarations): Unexpected lexical declaration in case block. | ||
╭─[no_case_declarations.tsx:1:1] | ||
1 │ switch (a) { case 1: const x = 1; break; } | ||
· ──────────── | ||
╰──── | ||
|
||
⚠ eslint(no-case-declarations): Unexpected lexical declaration in case block. | ||
╭─[no_case_declarations.tsx:1:1] | ||
1 │ switch (a) { default: const x = 2; break; } | ||
· ──────────── | ||
╰──── | ||
|
||
⚠ eslint(no-case-declarations): Unexpected lexical declaration in case block. | ||
╭─[no_case_declarations.tsx:1:1] | ||
1 │ switch (a) { case 1: function f() {} break; } | ||
· ─────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-case-declarations): Unexpected lexical declaration in case block. | ||
╭─[no_case_declarations.tsx:1:1] | ||
1 │ switch (a) { default: function f() {} break; } | ||
· ─────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-case-declarations): Unexpected lexical declaration in case block. | ||
╭─[no_case_declarations.tsx:1:1] | ||
1 │ switch (a) { case 1: class C {} break; } | ||
· ────────── | ||
╰──── | ||
|
||
⚠ eslint(no-case-declarations): Unexpected lexical declaration in case block. | ||
╭─[no_case_declarations.tsx:1:1] | ||
1 │ switch (a) { default: class C {} break; } | ||
· ────────── | ||
╰──── | ||
|
||
|