Repository: swc-project/swc (rust)

Issue:
Incorrect minification of rollup commonjs wrapper
### Describe the bug

I am using swc to minify a bundled version of Node.js-powered CLI.

Here is a part of the code (CommonJS wrapper generated by `@rollup/plugin-commonjs`):

```js
var state;
var hasRequiredState;

function requireState () {
	if (hasRequiredState) return state;
	hasRequiredState = 1;

	var ERR_INVALID_OPT_VALUE = requireErrors().codes.ERR_INVALID_OPT_VALUE;
	function highWaterMarkFrom(options, isDuplex, duplexKey) {
	  return options.highWaterMark != null ? options.highWaterMark : isDuplex ? options[duplexKey] : null;
	}
	function getHighWaterMark(state, options, duplexKey, isDuplex) {
	  var hwm = highWaterMarkFrom(options, isDuplex, duplexKey);
	  if (hwm != null) {
	    if (!(isFinite(hwm) && Math.floor(hwm) === hwm) || hwm < 0) {
	      var name = isDuplex ? duplexKey : 'highWaterMark';
	      throw new ERR_INVALID_OPT_VALUE(name, hwm);
	    }
	    return Math.floor(hwm);
	  }

	  // Default value
	  return state.objectMode ? 16 : 16 * 1024;
	}
	state = {
	  getHighWaterMark: getHighWaterMark
	};
	return state;
}
```

After minification, it becomes this:

```js
var fv,fE;
// redacted
function cz(){if(fE)return fv;fE=1;var e=cH().codes.ERR_INVALID_OPT_VALUE;return{getHighWaterMark:function(t,r,n,i){var s=null!=r.highWaterMark?r.highWaterMark:i?r[n]:null;if(null!=s){if(!(isFinite(s)&&Math.floor(s)===s)||s<0){var o=i?n:"highWaterMark";throw new e(o,s)}return Math.floor(s)}return t.objectMode?16:16384}}}
```

Note that after the minification, the `fv` (which originally is the `state`) is no longer assigned.

I couldn't find the minimum reproduction (my guess is that some code somewhere has interfered with the compression), so I have attached the full code in the GitHub Gist.


### Input code

```typescript
// https://gist.github.com/SukkaW/0a02d14521418685c6bc822c043bbe6a
// The playground URL is too long to attach
```


### Config

```json
{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": true
    },
    "target": "es2022",
    "minify": {
      "compress": true,
      "mangle": true,
      "module": true
    }
  },
  "minify": true,
  "isModule": true,
  "module": {
    "type": "commonjs"
  }
}
```


### Playground link

_No response_

### Expected behavior

The minified code should not produce any error.

### Actual behavior

```
TypeError can not read property (`getHighWaterMark`) of `undefined`
```

### Version

1.3.78

### Additional context

I am able to reproduce the issue since swc 1.2.166. The issue doesn't happen on 1.2.165. 

Pull request:
fix(es/minifier): Report `is_fn_local` even if var is hoisted
<!-- Note: CI script will automatically rebase your PR so please do not rebase unless required -->

<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. -->

**Description:**

<!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? -->

**BREAKING CHANGE:**

<!--
If this PR introduces a breaking change, it must contain a notice for it to be included in the CHANGELOG. Add description or remove entirely if not breaking.

You may need to update `MIGRATION.md` for the breaking changes.
-->

**Related issue (if exists):**
Closes #7847 

Patch:
diff --git a/crates/swc_ecma_minifier/src/compress/optimize/dead_code.rs b/crates/swc_ecma_minifier/src/compress/optimize/dead_code.rs
index 79f94304d1..614a2fc5a4 100644
--- a/crates/swc_ecma_minifier/src/compress/optimize/dead_code.rs
+++ b/crates/swc_ecma_minifier/src/compress/optimize/dead_code.rs
@@ -47,7 +47,6 @@ impl Optimizer<'_> {
 
             // We only handle identifiers on lhs for now.
             if let Some(lhs) = assign.left.as_ident() {
-                //
                 if self
                     .data
                     .vars
diff --git a/crates/swc_ecma_minifier/src/program_data.rs b/crates/swc_ecma_minifier/src/program_data.rs
index 9ba24bdbbe..a84bc601dd 100644
--- a/crates/swc_ecma_minifier/src/program_data.rs
+++ b/crates/swc_ecma_minifier/src/program_data.rs
@@ -315,7 +315,7 @@ impl Storage for ProgramData {
                             }
                         }
                         ScopeKind::Block => {
-                            if var_info.used_in_non_child_fn {
+                            if e.get().used_in_non_child_fn {
                                 e.get_mut().is_fn_local = false;
                                 e.get_mut().used_in_non_child_fn = true;
                             }


Test patch:
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7847/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/7847/input.js
new file mode 100644
index 0000000000..46f2fd4e23
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/7847/input.js
@@ -0,0 +1,17 @@
+function requireState() {
+    if (hasRequiredState) return state;
+    hasRequiredState = 1;
+    return state = {
+        getHighWaterMark: function(o) {
+            return o.objectMode ? 16 : 16384;
+        }
+    };
+}
+if (g()) {
+    var state, hasRequiredState;
+    const a = requireState()
+	console.log(a.getHighWaterMark())
+    
+    const b = requireState()
+	console.log(b.getHighWaterMark())
+}
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7847/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/7847/output.js
new file mode 100644
index 0000000000..0819771a20
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/7847/output.js
@@ -0,0 +1,14 @@
+function requireState() {
+    return hasRequiredState ? state : (hasRequiredState = 1, state = {
+        getHighWaterMark: function(o) {
+            return o.objectMode ? 16 : 16384;
+        }
+    });
+}
+if (g()) {
+    var state, hasRequiredState;
+    const a = requireState();
+    console.log(a.getHighWaterMark());
+    const b = requireState();
+    console.log(b.getHighWaterMark());
+}