brigand / brigand/babel-plugin-flow-react-proptypes

Spreading args in inner anonymous function causes error "Cannot read property 'file' of undefined"

Open
#117 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
427
Forks
42
PR merge metrics
No merged PRs in 30d

Description

I originally reported this issue as https://github.com/babel/babel/issues/5901, but they redirected me here after I had tracked down what babel settings causes the problem. The cause is https://github.com/brigand/babel-plugin-flow-react-proptypes/blob/master/src/util.js#L37. It should use `Path#traverse` instead of the `babel-traverse` package.

I have a test case at https://github.com/Lajnold/babel-issue-5901/. It basically build the code below with `webpack` and `babel`, with `flow-react-proptypes` included as a `babel` plugin.

### Input Code
```
const singleFunction = (...args) => {
// Works
const copy = [...args];
};

const outerFunction = outer => (...args) => {
// Doesn't work
const copy = [...args];
};

const outerFunctionAndSlice = outer => (...args) => {
// Works
const copy = args.slice();
};

const outerFunctionAndSliceAndCallingOtherFunction = outer => (...args) => {
// Doesn't work
const copy = args.slice();
singleFunction(...args);
};

const outerFunctionAndSliceAndCallingOtherFunctionWithCopy = outer => (...args) => {
// Passing ...copy instead of ...args doesn't work either
const copy = args.slice();
singleFunction(...copy);
};
```

### Current Behavior
The functions that don't work generate this error:
```
ERROR in ./src/index.js
Module build failed: TypeError: src/index.js: Cannot read property 'file' of undefined
at Scope.toArray (node_modules\babel-traverse\lib\scope\index.js:437:24)
at getSpreadLiteral (node_modules\babel-plugin-transform-es2015-spread\lib\index.js:16:20)
at build (node_modules\babel-plugin-transform-es2015-spread\lib\index.js:56:20)
at PluginPass.ArrayExpression (node_modules\babel-plugin-transform-es2015-spread\lib\index.js:76:21)
at newFn (node_modules\babel-traverse\lib\visitors.js:276:21)
at NodePath._call (node_modules\babel-traverse\lib\path\context.js:76:18)
at NodePath.call (node_modules\babel-traverse\lib\path\context.js:48:17)
at NodePath.visit (node_modules\babel-traverse\lib\path\context.js:105:12)
at TraversalContext.visitQueue (node_modules\babel-traverse\lib\context.js:150:16)
at TraversalContext.visitSingle (node_modules\babel-traverse\lib\context.js:108:19)
at TraversalContext.visit (node_modules\babel-traverse\lib\context.js:192:19)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.