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

Functions wrapped in __importDefault with allowSyntheticDefaultImports emit incorrect function calls #35420

Closed
borekb opened this issue Nov 29, 2019 · 5 comments · Fixed by #35877 or #44624
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@borekb
Copy link

borekb commented Nov 29, 2019

The following code:

import allSettled from "promise.allsettled";

const resolved = Promise.resolve(42);
const rejected = Promise.reject(-1);

allSettled([resolved, rejected]).then(results => {
  console.log(results);
});

Is transpiled into the following JS with esModuleInterop & allowSyntheticDefaultImports:

"use strict";
var __importDefault =
  (this && this.__importDefault) ||
  function(mod) {
    return mod && mod.__esModule ? mod : { default: mod };
  };
Object.defineProperty(exports, "__esModule", { value: true });
const promise_allsettled_1 = __importDefault(require("promise.allsettled"));
const resolved = Promise.resolve(42);
const rejected = Promise.reject(-1);
promise_allsettled_1.default([resolved, rejected]).then(results => {
  console.log(results);
});

Full tsconfig.json:

{
  "compilerOptions": {
    "target": "es2018",
    "module": "commonjs",
    "strict": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
  },
  "include": [
    "src"
  ]
}

(CodeSandbox.)

Executing this code leads to:

TypeError: #<Object> is not a constructor
    at Object.resolve (<anonymous>)
    at Object.PromiseResolve (/sandbox/node_modules/es-abstract/es2018.js:160:10)
    at /sandbox/node_modules/promise.allsettled/implementation.js:26:24
    at Function.from (<anonymous>)
    at Object.allSettled (/sandbox/node_modules/promise.allsettled/implementation.js:19:22)
    at Object.allSettled [as default] (/sandbox/node_modules/promise.allsettled/index.js:16:9)
    at Object.<anonymous> (/sandbox/src/demo.ts:6:11)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Module.m._compile (/sandbox/node_modules/ts-node/src/index.ts:536:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:787:10)

That is because the promise.allsettled package handles this according to the spec, see es-shims/Promise.allSettled#5. The author of that library is @ljharb who will have deeper understanding of the specifics than I do. (Also, thanks for nudging me to report this issue!)

The problem is most likely the emitted function call, which looks like this:

promise_allsettled_1.default([resolved, rejected]).then(results => {
  console.log(results);
});

In comparison, Babel + TS emits this (which works):

(0, _promise.default)([resolved, rejected]).then(function (results) {
  console.log(results);
});

Babel playground.

TypeScript Version: 3.7.2

Search Terms: esModuleInterop, allowSyntheticDefaultImports, emit, transpile, downlevel compile, ES Modules, default, wrapper, Node.js, module.

@ljharb
Copy link
Contributor

ljharb commented Nov 29, 2019

A simpler repro case would be importing module.exports = function () { 'use strict'; return !!this; } (with esModuleInterop and synthetic imports enabled, of course) - a correct downleveling would make invoking this function with no implied receiver return false; TS’s output currently seems to make it return true.

@ajafff
Copy link
Contributor

ajafff commented Dec 27, 2019

AFAICT the same bug exists for named imports.

// foo.ts
export function foo() { return !!this; }

// bar.ts
import { foo } from './foo.js';
foo();

// transpiled bar.js
var foo_js_1 = require('./foo.js');
foo_js_1.foo(); // this line should be `(0, foo_js_1.foo)()`

@ljharb please correct me if I'm wrong.

@ljharb
Copy link
Contributor

ljharb commented Dec 27, 2019

Yes, that’s the same bug, thanks.

@ajafff
Copy link
Contributor

ajafff commented Dec 27, 2019

Fix is up at #35877

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 22, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.8.1 milestone Jan 22, 2020
@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label Aug 31, 2020
rbuckton pushed a commit to ajafff/TypeScript that referenced this issue Feb 9, 2021
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Feb 9, 2021
rbuckton added a commit that referenced this issue Mar 3, 2021
* fix receiver of imported and exported functions

fixes: #35420

* Rebase against master and clean up substitution flow

* Add evaluator tests

* Fix evaluator tests

Co-authored-by: Ron Buckton <[email protected]>
@gdh1995
Copy link
Contributor

gdh1995 commented Jul 4, 2021

@rbuckton Sorry to disturb you, but your PR seems to wrap all imported symbols with (0, ...), but I think it's not necessary and may be skipped when the target is a "const arrow function".

copybara-service bot pushed a commit to google/closure-compiler that referenced this issue Sep 10, 2021
Previously, JSCompiler special cased to allow only calls to "eval" to be indirected using the parenthesized comma pattern ("(0, eval)(...)").

TypeScript 4.4 uses the same pattern to indirect all calls to imported functions, to avoid leaking the module object as a this pointer to them.

Because JSCompiler considers this code suspiciously useless, it'd retain it, hurting dead code removal in side-effect free functions whose calls would otherwise be eligible for removal.

With this change, JSCompiler considers this code non-suspicious, and thus does not specially preserve it from removal.

See also microsoft/TypeScript#35420

PiperOrigin-RevId: 395961706
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
7 participants