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

Named-exports extras - Default export function with named exports #2186

Merged
merged 9 commits into from
May 9, 2020

Conversation

Sauloxd
Copy link
Contributor

@Sauloxd Sauloxd commented May 8, 2020

Related issue: #2184

This enhances the extras/named-exports.js to allow enumerable keys from default exported functions to be handled as named exports (previous behaviour was to select by object type).

I'm not sure if I should add an UMD bundle type fixture (as they seem kinda redundant with AMD fixtures), but it doest feels closer to the problem described in the issue. I can change that if it's better!

With the previous build, only the test regarding named exports + default object export passed (as it was the previous behaviour) and the named exports + default function export fails
With the new build, all tests passes.

Obs

I have no idea why dist/system-node.cjs file has extra code included (I built it with: yarn install + yarn build). Maybe it's due to my node-version/yarn-version being different or something like that?

Copy link
Collaborator

@joeldenning joeldenning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution on this! Always great to have more contributors join in on things. I've left one substantive comment and a few small stylistic ones. Once those are addressed, I think this will be ready to merge.

@@ -3,7 +3,7 @@
*/
(function (global) {
var systemJSPrototype = global.System.constructor.prototype;

Copy link
Collaborator

@joeldenning joeldenning May 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dist is in source control, but we only change it during publishes. We're considering removing it from source control in v7 to make it easier for contributors to send in PRs. But for now, could you remove your changes to dist?

git checkout master -- dist

for (var exportName in defaultExport) {
// default is not a named export
if (exportName !== 'default') {
if (defaultExport.hasOwnProperty(exportName) && exportName !== 'default') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if there is ever a case where the defaultExport does not have a hasOwnProperty function. The defaultExport can have any type of value. I've checked and numbers, strings, symbols, functions, objects, and arrays and they do have it. However, it's possible that someone could modify the prototype of the value such that it doesn't have a default export. An example is that ES module "namespace" objects do not have the object prototype, and are created with Object.create(null).

A tangential example where the object prototype is removed from some things - #2147. That particular example doesn't demonstrate a specific case where the code here would break things, but just shows that there are situations where values do not have the Object prototype.

In such cases where hasOwnProperty does not exist, this code will throw an error. Perhaps the safer implementation would be this?

Suggested change
if (defaultExport.hasOwnProperty(exportName) && exportName !== 'default') {
if (Object.prototype.hasOwnProperty.call(defaultExport, exportName) && exportName !== 'default') {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Object.hasOwnProperty is preferred for this reason.

if (typeof define === 'function' && define.amd) {
define(['./amd-dep.js'], factory);
} else {
throw 'Test suit only supports amd'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this project, we usually use semicolons:

Suggested change
throw 'Test suit only supports amd'
throw 'Test suit only supports amd';

if (typeof define === 'function' && define.amd) {
define(['./amd-dep.js'], factory);
} else {
throw 'Test suit only supports amd'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw 'Test suit only supports amd'
throw 'Test suit only supports amd';

Copy link
Member

@guybedford guybedford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Sauloxd for carrying this one through, it's a big help.

@Sauloxd
Copy link
Contributor Author

Sauloxd commented May 9, 2020

Thanks for reviewing guys! I pushed a new version with your suggestions.

Updated:

  • Use Object.protootype.hasOwnProperty instead of depending on export defaults always being inherited from Object
  • Added a new test for export defaults originated by Object.create(null)

Copy link
Collaborator

@joeldenning joeldenning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, thanks!

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

Successfully merging this pull request may close these issues.

3 participants