Eslint-plugin-import: Export * and export default

Created on 10 May 2016  路  6Comments  路  Source: benmosher/eslint-plugin-import

I get the error Multiple default exports when trying to export everything from another file including the default export e.g.

export * from './foo';
export { default as default } from './foo';

It looks like that this case should throw an error (as shown by this test case) however babel compiles the code export * from './foo'; to:

Object.keys(_foo).forEach(function (key) {
  if (key === "default") return;
  Object.defineProperty(exports, key, {
    enumerable: true,
    get: function get() {
      return _foo[key];
    }
  });
});

Reference

so the second export is necessary to ensure the default export also gets exported.

Any suggestions? I'm not sure whether it's actually babel which is at fault here or if the assumption in this plugin is wrong. Happy to contribute a PR if a solution is agreed on.

bug

All 6 comments

Interesting. I think the spec dictates that imported/exported namespaces include the default name; it's not distinct from other exported names, it just has special extra syntax for exporting/importing.

So I would argue to start that Babel is doing it wrong (maybe in the interest of backwards-compatibility?).

edit: this is wrong, see my next comment. 馃槼

@jkimbo Are you running ESLint on the transpiled code? If you do, that could explain the error, but I don't see why you would do that.

@jfmengels: if ./foo contains a default export, it should currently does report Multiple default exports on both lines.

@jkimbo: http://www.ecma-international.org/ecma-262/6.0/#sec-getexportednames, 7.d.i. seems to be filtering default exports from export * from ... statements. As such, looks like this plugin is in error and Babel has it right.

IIRC I did this because export * as ns from '...' _does_ include the default name binding, but that is from Lee Byron's export extensions proposal. I (apparently mistakenly) expected this to carry over to ES6 star exports.

Thanks for digging into it @benmosher ! I can attempt a PR to fix it (though not until tomorrow) if that would help?

@jkimbo Yes please, that'd be great :)

You're welcome to take a stab at it, but I think it goes pretty deep into the exported namespace tracking so it may get dicey. You may want to fail fast.

Was this page helpful?
0 / 5 - 0 ratings