From 2a712cd1af5e9caf860c88312658305017e5bea6 Mon Sep 17 00:00:00 2001 From: Yusuke Tanaka Date: Fri, 12 Jun 2020 05:02:34 +0900 Subject: [PATCH] Fix no-empty for contructors (#152) --- src/rules/no_empty.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/rules/no_empty.rs b/src/rules/no_empty.rs index 7ada6d50398b4..c05ce631ba7e9 100644 --- a/src/rules/no_empty.rs +++ b/src/rules/no_empty.rs @@ -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; @@ -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. @@ -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) { @@ -94,6 +103,17 @@ mod tests { assert_lint_ok::(r#"function foobar() {}"#); } + #[test] + fn it_passes_for_an_empty_constructor() { + assert_lint_ok::( + r#" +class Foo { + constructor() {} +} + "#, + ); + } + #[test] fn it_passes_for_a_non_empty_block() { assert_lint_ok::(r#"if (foo) { var bar = ""; }"#);