Skip to content

Bug in helper function that is emitted by the compiler for module re-exports #10476

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

Closed
gilboa23 opened this issue Aug 22, 2016 · 5 comments
Closed
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@gilboa23
Copy link

TypeScript Version: 1.8.10

Code

An AMD module with re-exports:

export * from "a/b";
export * from "a/c";

Generates the following code:

define(["require", "exports", "a/b", "a/c"], function (require, exports, b_1, c_1) {
   "use strict";
   function __export(m) {
      for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
   }
   __export(b_1);
   __export(c_1);
});

Problem:
The emitted helper '__export' function will fail if the exports object has an empty prototype (due to custom module loading system) and thus does not inherit the 'hasOwnProperty' method. It will also not handle correctly exported readonly properties (created by custom decorators).

Workaround:

As a workaround, i currently override the emitted function in my TypeScript code like so:

function __export(m: any) {
   Object.getOwnPropertyNames(m).forEach(p => Object.defineProperty(exports, p, Object.getOwnPropertyDescriptor(m, p)));
}
export * from "a/b";
export * from "a/c";

Please fix the emitted '__exports' function to code similar to the above workaround (at least for ES5/ES6 targets).

@mhegazy mhegazy added the Suggestion An idea for TypeScript label Aug 25, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Aug 25, 2016

forEach is not supported for ES3, so it will have to be a regular loop

 var p= Object.getOwnPropertyNames(m);
 for (var i =0, n = p.lenght; i<n; i++){
    exports[p[i]] = m[p[i]];
 }

@gilboa23
Copy link
Author

Ok., but this is not the bug i opened. The problem is that you're copying the exported property values instead of copying the property definitions. As i mentioned, when targeting ES5 and above you should use Object.defineProperty instead of simple assignment of the exported properties.

@gilboa23
Copy link
Author

Referencing PR #9097.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 14, 2016

correct related issue is #10905

@RyanCavanaugh RyanCavanaugh added the Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature label Aug 13, 2018
@RyanCavanaugh
Copy link
Member

We now use Object.prototype.hasOwnProperty.call(exports, p)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants