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(lint/noEmptyBlockStatements): fix false positive when considering constructors using property parameters #3009

Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

The rule now places inline comments after the declaration statement, instead of removing them.
The code action is now safe to apply.

Contributed by @lutaok


Expand Down Expand Up @@ -381,6 +381,8 @@ z.object({})
.describe('')
.describe('');
```
- `noEmptyBlockStatements` no longer reports empty constructors using typescript parameter properties. [#3005](https://github.com/biomejs/biome/issues/3005) Contributed by @dyc3
- `noEmptyBlockStatements` no longer reports empty private or protected constructors. Contributed by @dyc3

- [noExportsInTest](https://biomejs.dev/linter/rules/no-exports-in-test/) rule no longer treats files with in-source testing as test files https://github.com/biomejs/biome/issues/2859. Contributed by @ah-yu
- [useSortedClasses](https://biomejs.dev/linter/rules/use-sorted-classes/) now keeps leading and trailing spaces when applying the code action inside template literals:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_js_syntax::{
JsBlockStatement, JsFunctionBody, JsStaticInitializationBlockClassMember, JsSwitchStatement,
AnyJsConstructorParameter, JsBlockStatement, JsConstructorClassMember, JsFunctionBody,
JsStaticInitializationBlockClassMember, JsSwitchStatement,
};
use biome_rowan::{declare_node_union, AstNode, AstNodeList};
use biome_rowan::{declare_node_union, AstNode, AstNodeList, SyntaxNodeCast};

declare_rule! {
/// Disallow empty block statements and static blocks.
Expand Down Expand Up @@ -79,8 +80,10 @@ impl Rule for NoEmptyBlockStatements {
let query = ctx.query();
let is_empty = is_empty(query);
let has_comments = query.syntax().has_comments_descendants();
let is_constructor_with_ts_param_props_or_private =
is_constructor_with_ts_param_props_or_private(query);

(is_empty && !has_comments).then_some(())
(is_empty && !has_comments && !is_constructor_with_ts_param_props_or_private).then_some(())
}

fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> {
Expand Down Expand Up @@ -109,3 +112,33 @@ fn is_empty(query: &Query) -> bool {
JsSwitchStatement(statement) => statement.cases().len() == 0,
}
}

/// Check if the function is a constructor with TypeScript parameter properties, or a private/protected constructor.
///
/// https://www.typescriptlang.org/docs/handbook/2/classes.html#parameter-properties
fn is_constructor_with_ts_param_props_or_private(query: &Query) -> bool {
let Query::JsFunctionBody(body) = query else {
return false;
};

let Some(constructor) = body
.syntax()
.parent()
.and_then(|node| node.cast::<JsConstructorClassMember>())
else {
return false;
};

let Ok(params) = constructor.parameters() else {
return false;
};
let is_param_props = params
.parameters()
.into_iter()
.any(|param| matches!(param, Ok(AnyJsConstructorParameter::TsPropertyParameter(_))));
let is_private = constructor
.modifiers()
.into_iter()
.any(|modifier| modifier.is_private() || modifier.is_protected());
is_param_props || is_private
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,13 @@ function fooWithInternalEmptyBlocksTs(){
} finally {

}
}
}

export class FooBar {
constructor(
private foo: string,
) {
function bar() { }
bar();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ function fooWithInternalEmptyBlocksTs(){

}
}

export class FooBar {
constructor(
private foo: string,
) {
function bar() { }
bar();
}
}

```

# Diagnostics
Expand Down Expand Up @@ -392,10 +402,26 @@ invalid.ts:67:13 lint/suspicious/noEmptyBlockStatements ━━━━━━━━
> 69 │ }
│ ^
70 │ }
71 │

i Empty blocks are usually the result of an incomplete refactoring. Remove the empty block or add a comment inside it if it is intentional.


```

```
invalid.ts:76:20 lint/suspicious/noEmptyBlockStatements ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Unexpected empty block.

74 │ private foo: string,
75 │ ) {
> 76 │ function bar() { }
│ ^^^
77 │ bar();
78 │ }

i Empty blocks are usually the result of an incomplete refactoring. Remove the empty block or add a comment inside it if it is intentional.


```
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ const barWithCommentTs = () => {

function fooWithMultilineCommentTS() {
/**
* this should also work
* this should also work
*/
}

const barWithMultilineCommentTs = () => {
/**
* this should also work
* this should also work
*/
}

Expand Down Expand Up @@ -65,4 +65,15 @@ class FoozTs {
// biome-ignore lint/suspicious/noEmptyBlockStatements: this should be allowed
function shouldNotFailTs() {

}
}

// This is using parameter properties, and the empty constructor should be allowed
export class FooBar {
constructor(
private foo: string,
) { }
}

class FooBarPrivate {
private constructor() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ const barWithCommentTs = () => {

function fooWithMultilineCommentTS() {
/**
* this should also work
* this should also work
*/
}

const barWithMultilineCommentTs = () => {
/**
* this should also work
* this should also work
*/
}

Expand Down Expand Up @@ -72,6 +72,16 @@ class FoozTs {
function shouldNotFailTs() {

}
```

// This is using parameter properties, and the empty constructor should be allowed
export class FooBar {
constructor(
private foo: string,
) { }
}

class FooBarPrivate {
private constructor() { }
}

```