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

When lowering optional chaining / nullish coalescing operator, the temporary variable declaration comes before "use strict" making the function to be non-strict mode #3322

Closed
sapphi-red opened this issue Aug 19, 2023 · 0 comments

Comments

@sapphi-red
Copy link
Contributor

sapphi-red commented Aug 19, 2023

When lowering optional chaining / nullish coalescing operator, the temporary variable declaration gets inject at the top of the function. Then, "use strict" becomes the non-first statement and that function becomes non-strict (sloppy) mode.

Input:

const foo = () => {
 'use strict';
  const f1 = function () {};
  const f2 = () => {};

  f1()?.foo;
  f2()?.foo;

  console.log(window.bar ?? 'foo');
};

const bar = function () {
  'use strict';
  const f1 = function () {};
  const f2 = () => {};

  f1()?.foo;
  f2()?.foo;

  console.log(window.bar ?? 'foo');
};

Options: --target es2015
Actual output:

const foo = () => {
  var _a, _b, _c;
  "use strict";
  const f1 = function() {
  };
  const f2 = () => {
  };
  (_a = f1()) == null ? void 0 : _a.foo;
  (_b = f2()) == null ? void 0 : _b.foo;
  console.log((_c = window.bar) != null ? _c : "foo");
};
const bar = function() {
  var _a, _b, _c;
  "use strict";
  const f1 = function() {
  };
  const f2 = () => {
  };
  (_a = f1()) == null ? void 0 : _a.foo;
  (_b = f2()) == null ? void 0 : _b.foo;
  console.log((_c = window.bar) != null ? _c : "foo");
};

Expected output:

const foo = () => {
  "use strict";
  var _a, _b, _c;
  const f1 = function() {
  };
  const f2 = () => {
  };
  (_a = f1()) == null ? void 0 : _a.foo;
  (_b = f2()) == null ? void 0 : _b.foo;
  console.log((_c = window.bar) != null ? _c : "foo");
};
const bar = function() {
  "use strict";
  var _a, _b, _c;
  const f1 = function() {
  };
  const f2 = () => {
  };
  (_a = f1()) == null ? void 0 : _a.foo;
  (_b = f2()) == null ? void 0 : _b.foo;
  console.log((_c = window.bar) != null ? _c : "foo");
};

esbuild try

This bug seems to exist since at least 0.5.1.

related: vitejs/vite#14094

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

No branches or pull requests

1 participant