asapach / babel-plugin-rewire-exports

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

Eliminate redundant variables #18

Closed sergei-startsev closed 5 years ago

sergei-startsev commented 5 years ago

The plugin generates redundant variables for some cases, e.g.:

In

export const foo = 'bar';

Out

const foo = 'bar';
var _foo = foo;
export { _foo as foo };
var _foo2 = _foo;
export function rewire$foo($stub) {
  _foo = $stub;
}
export function restore() {
  _foo = _foo2;
}

Added _foo2 variable is redundant here, _foo can be restored by using original foo:

const foo = 'bar';
var _foo = foo;
export { _foo as foo };
- var _foo2 = _foo;
export function rewire$foo($stub) {
  _foo = $stub;
}
export function restore() {
-  _foo = _foo2;
+  _foo = foo;
}

The PR eliminates redundant variables, it generates variables to capture only if required.