Skip to content

Commit

Permalink
Fix constant-folding-plugin for default export function
Browse files Browse the repository at this point in the history
Summary:
**Summary**

When using ES modules it is possible that a function id is null, for example `export default function() {}` so we need to add a check to avoid an error.

Fixes #177

**Test plan**

Added a test and made sure it failed before the fix and passes after. Also tested by applying the fix locally in an app where I found the bug.
Closes #178

Differential Revision: D8247142

Pulled By: mjesun

fbshipit-source-id: c4642ba45b2818fb9d30b5e705250563c969c84e
  • Loading branch information
janicduplessis authored and facebook-github-bot committed Jun 3, 2018
1 parent 0054fe8 commit 5e55377
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,15 @@ describe('constant expressions', () => {
'var plusZero=0;var zero=0;var minusZero=-0;',
);
});

it('does not mess up default exports', () => {
let code = `export default function () {}`;
expect(fold('arbitrary.js', code)).toEqual('export default function(){}');
code = `export default () => {}`;
expect(fold('arbitrary.js', code)).toEqual('export default(()=>{});');
code = `export default class {}`;
expect(fold('arbitrary.js', code)).toEqual('export default class{}');
code = `export default 1`;
expect(fold('arbitrary.js', code)).toEqual('export default 1;');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ function constantFoldingPlugin(context: {types: BabelTypes}) {

const FunctionDeclaration = {
exit(path: Object, state: Object) {
const binding = path.scope.getBinding(path.node.id.name);
const binding =
path.node.id !== null && path.scope.getBinding(path.node.id.name);

if (binding && !binding.referenced) {
state.stripped = true;
Expand Down

0 comments on commit 5e55377

Please sign in to comment.