babel / babel/minify

babel-plugin-minify-dead-code-elimination fails to remove variable declaration after return statement

Open
#1,020 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
4.4k
Forks
217
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**

As described in title

**To Reproduce**

Minimal code to reproduce the bug

```js
function foo() {
if ( true ) {
return
}

const timer = setInterval(
() => { console.log( timer ) }, 1000
)
}
```

**Actual Output**

there is no Error thrown

```js
function foo() {
var timer = setInterval(function () {
console.log(timer);
}, 1000);
}
```

**Expected Output**

```js
function foo() {}
```

**Possible solution**

If the `timer` reference doesn't appeared before `return` in current scope, it can be safely removed

**Additional context**

Here is the [Babel REPL](https://babeljs.io/repl#?browsers=defaults&build=&builtIns=false&corejs=3.6&spec=false&loose=false&code_lz=GYVwdgxgLglg9mABMOcAUBKRBvAUIxGYRNRKAJxAFNEs8CDyqoRyx9EBfXDiBAZyhkYAWyrlEAXkT9mASTBRxANwCGAGzQcCmKQD4ciPmH5x1VAHTq4Ac1KwxErJwA0iAIwAGbxwy5OQA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=true&fileSize=true&timeTravel=true&sourceType=unambiguous&lineWrap=true&presets=env%2Creact%2Cstage-0%2Cstage-1%2Cstage-2%2Cstage-3&prettier=false&targets=&version=7.15.8&externalPlugins=babel-plugin-minify-dead-code-elimination%400.5.1&assumptions=%7B%7D) link to reproduce

Contributor guide

Open the contributing guide

Research direction

Start by running the provided JavaScript example through babel-plugin-minify-dead-code-elimination and compare the actual and expected output. Trace the plugin's handling of unreachable variable declarations and verify that the result for this example is function foo() {}.

Written by the indexing model from the issue text.

Assessment

Tech stack
babel, javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.