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

feat: drop unused local variables #2360

Closed
wants to merge 1 commit into from

Conversation

sapphi-red
Copy link
Contributor

This PR partially implements dropping unused local variables.
Multi-level dropping (for example: a is unused and b depends on a) is not implemented.

This is useful for some cases like minifying IIFE wrapped output.

Example

Input

(function () {
  'use strict';

  const const1 = 0;
  let let1 = 0;
  var var1 = 0;

  const const2 = 0;
  let let2 = 0;
  var var2 = 0;
  console.log(const2, let2, var2);

})();

Current Output

(function() {
  "use strict";
  let let1 = 0;
  var var1 = 0;
  const const2 = 0;
  let let2 = 0;
  var var2 = 0;
  console.log(const2, let2, var2);
})();

New Output

(function() {
  "use strict";
  const const2 = 0;
  let let2 = 0;
  var var2 = 0;
  console.log(const2, let2, var2);
})();

@@ -255,6 +255,7 @@ async function test_case(esbuild, test, basename) {
'let.js: issue_4229',
'let.js: issue_4245',
'let.js: use_before_init_3',
'let.js: issue_4276_1',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Ignore declarations if the scope is shadowed by a direct "eval" call.
// The eval'd code may indirectly reference this symbol and the actual
// use count may be greater than 0.
if p.currentScope != p.moduleScope && !p.currentScope.ContainsDirectEval {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this costs too much, I think this condition could be p.currentScope.Parent == p.moduleScope && !p.currentScope.ContainsDirectEval instead.

expectPrintedMangle(t, "{ const v = (() => 'foo')() }", "{\n const v = (() => \"foo\")();\n}\n")
expectPrintedMangle(t, "{ const v = /* #__PURE__ */ (() => 'foo')() }", "")
expectPrintedMangle(t, "{ const v = 0; console.log(v) }", "console.log(0);\n")
// expectPrintedMangle(t, "function f() { const v = 0; eval('v') }", "function f() {\n const v = 0;\n eval(\"v\");\n}\n")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails without this PR.
const v = 0; is dropped at here.

// Only keep this declaration if it's top-level or exported (which
// could be in a nested TypeScript namespace), otherwise erase it
if p.currentScope.Parent == nil || s.IsExport {
s.Decls[end] = d
end++
}

Created an issue: #2361

unbyte added a commit to unbyte/esbuild that referenced this pull request Oct 17, 2022
@unbyte
Copy link

unbyte commented Oct 28, 2022

This PR breaks var hoist.

before

function test(v) {
  var v = 3
  return arguments[0]
}

test(1)
// output: 3

after

function test(v) {
  return arguments[0]
}

test(1)
// output: 1

@sapphi-red
Copy link
Contributor Author

@unbyte That's a good catch.
But I will not campion this PR. This is because this PR is no longer essential for me.
Feel free to create a PR that supersedes this one. 😃

@sapphi-red sapphi-red closed this Dec 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants