Skip to content

Commit

Permalink
fix(useArrowFunction): preserve directives in the code fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Nov 14, 2024
1 parent 6d75a4c commit e442a93
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,21 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

Contributed by @Conaclos

- [useArrowFunction](https://biomejs.dev/linter/rules/use-arrow-function/) now preserves directives ([#4530](https://github.com/biomejs/biome/issues/4530)).

Previously the rule removed the directives when a function expression was turned into an arrow function.
The rule now correctly keeps the directives.

```diff
- const withDirective = function () {
+ const withDirective = () => {
"use server";
return 0;
}
```

Contributed by @Conaclos

### Parser

#### Bug fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,14 @@ impl Visitor for AnyThisScopeVisitor {

/// Get a minimal arrow function body from a regular function body.
fn to_arrow_body(body: JsFunctionBody) -> AnyJsFunctionBody {
let directives = body.directives();
let body_statements = body.statements();
// () => { ... }
let early_result = AnyJsFunctionBody::from(body);
if !directives.is_empty() {
// The function body has at least one directive.
// e.g. `function() { "directive"; return 0; }`
return early_result;
}
let Some(AnyJsStatement::JsReturnStatement(return_statement)) = body_statements.iter().next()
else {
return early_result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ const call = function () {}();
const staticMember = function(a) {}.bind(null, 0);
const computedMember = function(a) {}["bind"](null, 0);
const logical = false || function () {};
const binary = false + function () {};
const binary = false + function () {};

const withDirective = function () {
"use server";
return 0;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid.ts
snapshot_kind: text
---
# Input
```ts
Expand Down Expand Up @@ -57,6 +58,12 @@ const staticMember = function(a) {}.bind(null, 0);
const computedMember = function(a) {}["bind"](null, 0);
const logical = false || function () {};
const binary = false + function () {};

const withDirective = function () {
"use server";
return 0;
}

```

# Diagnostics
Expand Down Expand Up @@ -520,6 +527,7 @@ invalid.ts:52:26 lint/complexity/useArrowFunction FIXABLE ━━━━━━
> 52 │ const logical = false || function () {};
│ ^^^^^^^^^^^^^^
53 │ const binary = false + function () {};
54 │
i Function expressions that don't use this can be turned into arrow functions.
Expand All @@ -530,6 +538,7 @@ invalid.ts:52:26 lint/complexity/useArrowFunction FIXABLE ━━━━━━
52 │ - const·logical·=·false·||·function·()·{};
52 │ + const·logical·=·false·||·(()·=>·{});
53 53 │ const binary = false + function () {};
54 54 │
```
Expand All @@ -543,6 +552,8 @@ invalid.ts:53:24 lint/complexity/useArrowFunction FIXABLE ━━━━━━
52 │ const logical = false || function () {};
> 53 │ const binary = false + function () {};
│ ^^^^^^^^^^^^^^
54 │
55 │ const withDirective = function () {
i Function expressions that don't use this can be turned into arrow functions.
Expand All @@ -552,8 +563,37 @@ invalid.ts:53:24 lint/complexity/useArrowFunction FIXABLE ━━━━━━
52 52 │ const logical = false || function () {};
53 │ - const·binary·=·false·+·function·()·{};
53 │ + const·binary·=·false·+·(()·=>·{});
54 54 │
55 55 │ const withDirective = function () {
```

```
invalid.ts:55:23 lint/complexity/useArrowFunction FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This function expression can be turned into an arrow function.
53 │ const binary = false + function () {};
54 │
> 55 │ const withDirective = function () {
│ ^^^^^^^^^^^^^
> 56 │ "use server";
> 57 │ return 0;
> 58 │ }
│ ^
59 │
i Function expressions that don't use this can be turned into arrow functions.
i Safe fix: Use an arrow function instead.
53 53 │ const binary = false + function () {};
54 54 │
55 │ - const·withDirective·=·function·()·{
55 │ + const·withDirective·=·()·=>·{
56 56 │ "use server";
57 57 │ return 0;
```

0 comments on commit e442a93

Please sign in to comment.