-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Functions wrapped in __importDefault
with allowSyntheticDefaultImports emit incorrect function calls
#35420
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
Comments
A simpler repro case would be importing |
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. |
Yes, that’s the same bug, thanks. |
Fix is up at #35877 |
* 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]>
@rbuckton Sorry to disturb you, but your PR seems to wrap all imported symbols with |
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
The following code:
Is transpiled into the following JS with
esModuleInterop
&allowSyntheticDefaultImports
:Full
tsconfig.json
:(CodeSandbox.)
Executing this code leads to:
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:
In comparison, Babel + TS emits this (which works):
Babel playground.
TypeScript Version: 3.7.2
Search Terms:
esModuleInterop
,allowSyntheticDefaultImports
, emit, transpile, downlevel compile, ES Modules, default, wrapper, Node.js, module.The text was updated successfully, but these errors were encountered: