Skip to content

Commit

Permalink
Fix no-empty for contructors (denoland#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
magurotuna committed Jun 11, 2020
1 parent 40014a8 commit 2a712cd
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/rules/no_empty.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2020 the Deno authors. All rights reserved. MIT license.
use super::{Context, LintRule};
use swc_ecma_ast::{BlockStmt, Function, Module, SwitchStmt};
use swc_ecma_ast::{BlockStmt, Constructor, Function, Module, SwitchStmt};
use swc_ecma_visit::{Node, Visit};

pub struct NoEmpty;
Expand Down Expand Up @@ -32,7 +32,7 @@ impl NoEmptyVisitor {

impl Visit for NoEmptyVisitor {
fn visit_function(&mut self, function: &Function, _parent: &dyn Node) {
// Empty functions shouldn't be caught be this rule.
// Empty functions shouldn't be caught by this rule.
// Because function's body is a block statement, we're gonna
// manually visit each member; otherwise rule would produce errors
// for empty function body.
Expand All @@ -43,6 +43,15 @@ impl Visit for NoEmptyVisitor {
}
}

fn visit_constructor(&mut self, cons: &Constructor, _parent: &dyn Node) {
// Similar to the above, empty constructors shouldn't be caught.
if let Some(body) = &cons.body {
for stmt in &body.stmts {
swc_ecma_visit::visit_stmt(self, stmt, body);
}
}
}

fn visit_block_stmt(&mut self, block_stmt: &BlockStmt, _parent: &dyn Node) {
if block_stmt.stmts.is_empty() {
if !block_stmt.contains_comments(&self.context) {
Expand Down Expand Up @@ -94,6 +103,17 @@ mod tests {
assert_lint_ok::<NoEmpty>(r#"function foobar() {}"#);
}

#[test]
fn it_passes_for_an_empty_constructor() {
assert_lint_ok::<NoEmpty>(
r#"
class Foo {
constructor() {}
}
"#,
);
}

#[test]
fn it_passes_for_a_non_empty_block() {
assert_lint_ok::<NoEmpty>(r#"if (foo) { var bar = ""; }"#);
Expand Down

0 comments on commit 2a712cd

Please sign in to comment.