babel / babel/minify

when function write inside if condition statment, the declare function will only usable in that statment

Open
#929 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**
when function write inside if condition statment, the declare function will only usable in that statment

**To Reproduce**
I wrote example program, and upload to my github repo, please check this href https://github.com/LaiHuanMin/babelminify-issuecheck-for-function-scope

origin js
```
window.onload = () => {
var obj = {
testfunc: function(value){
if(value === 'test1'){
function scopefunc1(){
console.log('this is scope func 1');
}
var scopefunc2 = () => {
console.log();
console.log('this is scope func 2');
}
let scopefunc3 = () => {

}
}
// that func should be exists
scopefunc1();
// if the if condition is true, also should be exists
scopefunc2();
// never exists whatever the condition result is
scopefunc3();
}
}
obj.testfunc("value");
}
```

**Actual Output**

```javascript
window.onload = function() {
({
testfunc: function c(a) {
if ("test1" === a) var b = function() {
console.log(), console.log("this is scope func 2")
};
scopefunc1(), b(), scopefunc3()
}
}).testfunc("value")
};
```

**Expected Output**

```js
window.onload = function() {
({
testfunc: function c(a) {
if ("test1" === a) {
function scopefun1(){} ; var scopefunc3 = () => {}; // i ignore the function content
};
scopefunc1(), b(), scopefunc3()
}
}).testfunc("value")
};
```

**Stack Trace**

If applicable,

```

```

**Configuration**

How are you using babel-minify?

* babel-preset-minify in babelrc

"@babel/cli": "^7.1.2",
"@babel/core": "^7.1.2",
"@babel/plugin-transform-async-to-generator": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"babel-loader": "^8.0.4",
"babel-plugin-transform-remove-console": "^6.9.4",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "^6.26.0"

babelrc:

module.exports = {
"presets": [['@babel/preset-env', {
modules: false,
useBuiltIns: 'entry'
}],
"minify"
],
"plugins": [
"@babel/plugin-transform-async-to-generator"
]
}

**Possible solution**
Function should be usable in the same scope, may be there's look like a bad code style, but it's very safty for compile old javascript file. I'm using babel minify to compile old project which using requirejs, i hope there's a config option can set the loose true, and safty compile the code into a runnable minify code.

**Additional context**
node v 8.4.0
UBuntu 16.04 LTS

Contributor guide

Open the contributing guide

Research direction

Start with the linked reproduction repository and the supplied babel-preset-minify configuration, then compare the original and actual output for obj.testfunc. Confirm the intended behavior for scopefunc1, scopefunc2, and scopefunc3 and reproduce the issue before investigating a fix. Done means the minified output preserves the requested function behavior and the reproduction passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.