Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(no-ex-assign): handle nested assignments correctly #545

Merged
merged 4 commits into from
Nov 19, 2020
Merged
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
91 changes: 51 additions & 40 deletions src/rules/no_ex_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@
use super::Context;
use super::LintRule;
use crate::{scopes::BindingKind, swc_util::find_lhs_ids};
use derive_more::Display;

use swc_ecmascript::ast::AssignExpr;
use swc_ecmascript::visit::noop_visit_type;
use swc_ecmascript::visit::Node;
use swc_ecmascript::visit::Visit;
use swc_ecmascript::ast::{AssignExpr, Program};
use swc_ecmascript::visit::{noop_visit_type, Node, VisitAll, VisitAllWith};

pub struct NoExAssign;

const CODE: &str = "no-ex-assign";
const MESSAGE: &str = "Reassigning exception parameter is not allowed";
const HINT: &str = "Use a different variable for the assignment";

#[derive(Display)]
enum NoExAssignMessage {
#[display(fmt = "Reassigning exception parameter is not allowed")]
NotAllowed,
}

#[derive(Display)]
enum NoExAssignHint {
#[display(fmt = "Use a different variable for the assignment")]
UseDifferent,
}

impl LintRule for NoExAssign {
fn new() -> Box<Self> {
Expand All @@ -27,13 +36,9 @@ impl LintRule for NoExAssign {
CODE
}

fn lint_program(
&self,
context: &mut Context,
program: &swc_ecmascript::ast::Program,
) {
fn lint_program(&self, context: &mut Context, program: &Program) {
let mut visitor = NoExAssignVisitor::new(context);
visitor.visit_program(program, program);
program.visit_all_with(program, &mut visitor);
}

fn docs(&self) -> &'static str {
Expand Down Expand Up @@ -74,10 +79,10 @@ impl<'c> NoExAssignVisitor<'c> {
}
}

impl<'c> Visit for NoExAssignVisitor<'c> {
impl<'c> VisitAll for NoExAssignVisitor<'c> {
noop_visit_type!();

fn visit_assign_expr(&mut self, assign_expr: &AssignExpr, _node: &dyn Node) {
fn visit_assign_expr(&mut self, assign_expr: &AssignExpr, _: &dyn Node) {
let ids = find_lhs_ids(&assign_expr.left);

for id in ids {
Expand All @@ -88,8 +93,8 @@ impl<'c> Visit for NoExAssignVisitor<'c> {
self.context.add_diagnostic_with_hint(
assign_expr.span,
CODE,
MESSAGE,
HINT,
NoExAssignMessage::NotAllowed,
NoExAssignHint::UseDifferent,
);
}
}
Expand Down Expand Up @@ -118,44 +123,50 @@ function foo() { try { } catch (e) { return false; } }
fn no_ex_assign_invalid() {
assert_lint_err! {
NoExAssign,
r#"
try {} catch (e) { e = 1; }
try {} catch (ex) { ex = 1; }
try {} catch (ex) { [ex] = []; }
try {} catch (ex) { ({x: ex = 0} = {}); }
try {} catch ({message}) { message = 1; }
"#: [
r#"try {} catch (e) { e = 1; }"#: [
{
line: 2,
col: 19,
message: MESSAGE,
hint: HINT,
message: NoExAssignMessage::NotAllowed,
hint: NoExAssignHint::UseDifferent,
},
],
r#"try {} catch (ex) { ex = 1; }"#: [
{
line: 3,
col: 20,
message: MESSAGE,
hint: HINT,
message: NoExAssignMessage::NotAllowed,
hint: NoExAssignHint::UseDifferent,
},
],
r#"try {} catch (ex) { [ex] = []; }"#: [
{
line: 4,
col: 20,
message: MESSAGE,
hint: HINT,
message: NoExAssignMessage::NotAllowed,
hint: NoExAssignHint::UseDifferent,
},
],
r#"try {} catch (ex) { ({x: ex = 0} = {}); }"#: [
{
line: 5,
col: 21,
message: MESSAGE,
hint: HINT,
message: NoExAssignMessage::NotAllowed,
hint: NoExAssignHint::UseDifferent,
},
],
r#"try {} catch ({message}) { message = 1; }"#: [
{
line: 6,
col: 27,
message: MESSAGE,
hint: HINT,
message: NoExAssignMessage::NotAllowed,
hint: NoExAssignHint::UseDifferent,
},
]
}
],

// nested
r#"a = () => { try {} catch (e) { e = 1; } };"#: [
{
col: 31,
message: NoExAssignMessage::NotAllowed,
hint: NoExAssignHint::UseDifferent,
},
],
};
}
}