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

Incorrect imported function this context using ES6 module #4706

Closed
falsandtru opened this issue Sep 9, 2015 · 4 comments
Closed

Incorrect imported function this context using ES6 module #4706

falsandtru opened this issue Sep 9, 2015 · 4 comments
Labels
Bug A bug in TypeScript ES6 Relates to the ES6 Spec Help Wanted You can do this
Milestone

Comments

@falsandtru
Copy link
Contributor

Code

// export.ts
export function foo() {
  'use strict'
  console.log(this);
}

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

foo();

expected

$ node import.js
undefined
undefined

actual

$ node import.js
undefined
{ foo: [Function: foo] }
@mhegazy mhegazy added Bug A bug in TypeScript Help Wanted You can do this labels Sep 10, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Sep 10, 2015

The emitter will need to identify call/new expressions from aliases, and rewrite them using as foo.call(void 0, arg)

A PR would be appreciated.

@falsandtru
Copy link
Contributor Author

Thanks for your acceptance.

@mhegazy mhegazy added this to the Community milestone Sep 18, 2015
@falsandtru
Copy link
Contributor Author

Babel's implementations:

(0, _assert2['default'])(true); // (A)

Note that the default export is always accessed via the exports object_assert2 (line A), never directly, like this:

var assert = _assert2.default;

The reason for that is support for cyclic dependencies.

http://www.2ality.com/2015/12/babel-commonjs.html

@mhegazy mhegazy added the ES6 Relates to the ES6 Spec label Feb 22, 2016
@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
@Andarist
Copy link
Contributor

Andarist commented Aug 1, 2023

As the current emit does this:

Object.defineProperty(exports, "__esModule", { value: true });
var export_1 = require("./export");
(0, export_1.foo)();

I think that the issue can be closed, cc @jakebailey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript ES6 Relates to the ES6 Spec Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

5 participants