asapach / babel-plugin-rewire-exports

Babel plugin for stubbing [ES6, ES2015] module exports
MIT License
66 stars 7 forks source link

Wrong names are exported #13

Closed kostia closed 5 years ago

kostia commented 5 years ago

Following code gets "rewired" with wrong name for eggs:

// spamAndEggs.ts

export function spam() {
  return {
    eggs() {
      eggs();
    },
  };
}

export function eggs() {}

The exports then look weird:

import * as spamAndEggs from './spamAndEggs';
console.log(Object.keys(spamAndEggs));
// => ['spam', 'rewire$spam', 'rewire$_eggs2', 'eggs']

Any ideas how to fix that?

apepper commented 5 years ago

I added a test-case for it: https://github.com/asapach/babel-plugin-rewire-exports/compare/master...apepper:issue_13_bugreport

asapach commented 5 years ago

I've tried your original example and I get the following exports - spam, eggs, rewire$spam, rewire$eggs, restore:

var spam = _spam,
    eggs = _eggs;

function _spam() {
  return {
    eggs() {
      eggs();
    }

  };
}

export { spam };

function _eggs() {}

export { eggs };
export function rewire$spam($stub) {
  spam = $stub;
}
export function rewire$eggs($stub) {
  eggs = $stub;
}
export function restore() {
  spam = _spam;
  eggs = _eggs;
}

Your original example doesn't have restore as an export, which is suspicious.

And your other example - spam, broken, ok, rewire$spam, rewire$broken, rewire$ok, restore:

var spam = _spam,
    broken = _broken,
    ok = _ok;

function _spam() {
  return {
    ok,

    broken() {
      broken();
    }

  };
}

export { spam };

function _broken() {
  return true;
}

export { broken };

function _ok() {
  return false;
}

export { ok };
export function rewire$spam($stub) {
  spam = $stub;
}
export function rewire$broken($stub) {
  broken = $stub;
}
export function rewire$ok($stub) {
  ok = $stub;
}
export function restore() {
  spam = _spam;
  broken = _broken;
  ok = _ok;
}

So I'm unable to reproduce the issue :sweat:. Do you have a more complete example? Maybe a small repo with a repro case?

apepper commented 5 years ago

I added a package-lock.json to https://github.com/asapach/babel-plugin-rewire-exports/compare/master...apepper:issue_13_bugreport to better reproduce the error.

I'm using max osx with node version 11.14.0.

asapach commented 5 years ago

OK, I think I got it: you have to transpile it down to ES5:

exports.rewire$_eggs2 = rewire$_eggs2;
var _eggs2 = _eggs;
exports.eggs = _eggs2;

function _spam() {
  return {
    eggs: function eggs() {
      _eggs2();
    }
  };
}
asapach commented 5 years ago

Should be fixed in v1.1.0

kostia commented 5 years ago

Thanks a lot! 👍