-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0708dd8
commit d383385
Showing
8 changed files
with
344 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
function isJSXElementOrReactCreateElement(node) { | ||
const { | ||
type, | ||
callee, | ||
} = node; | ||
|
||
if (type === 'JSXElement') { | ||
return true; | ||
} | ||
|
||
if (callee && callee.object && callee.object.name === 'React' && | ||
callee.property.name === 'createElement') { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function isReturningJSXElement(path) { | ||
/** | ||
* Early exit for ArrowFunctionExpressions, there is no ReturnStatement node. | ||
*/ | ||
if (path.node.init && path.node.init.body && isJSXElementOrReactCreateElement(path.node.init.body)) { | ||
return true; | ||
} | ||
|
||
let visited = false; | ||
|
||
path.traverse({ | ||
ReturnStatement(path2) { | ||
// We have already found what we are looking for. | ||
if (visited) { | ||
return; | ||
} | ||
|
||
const argument = path2.get('argument'); | ||
|
||
// Nothing is returned | ||
if (!argument.node) { | ||
return; | ||
} | ||
|
||
if (isJSXElementOrReactCreateElement(argument.node)) { | ||
visited = true; | ||
return; | ||
} | ||
|
||
if (argument.node.type === 'CallExpression') { | ||
const name = argument.get('callee').node.name; | ||
const binding = path.scope.getBinding(name); | ||
|
||
if (!binding) { | ||
return; | ||
} | ||
|
||
if (isReturningJSXElement(binding.path)) { | ||
visited = true; | ||
} | ||
} | ||
}, | ||
}); | ||
|
||
return visited; | ||
} | ||
|
||
const validPossibleStatelessComponentTypes = [ | ||
'Property', | ||
'VariableDeclarator', | ||
'FunctionDeclaration', | ||
'ArrowFunctionExpression', | ||
]; | ||
|
||
/** | ||
* Returns `true` if the path represents a function which returns a JSXElement | ||
*/ | ||
export default function isStatelessComponent(path) { | ||
const node = path.node; | ||
|
||
if (validPossibleStatelessComponentTypes.indexOf(node.type) === -1) { | ||
return false; | ||
} | ||
|
||
if(path.get('body').get('type').node == 'JSXElement') { | ||
return true; | ||
} | ||
if (isReturningJSXElement(path)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react'; | ||
|
||
const Button = ({ children, onClick, style = {} }) => ( | ||
<button | ||
style={{ }} | ||
onClick={onClick} | ||
> | ||
{children} | ||
</button> | ||
); | ||
|
||
Button.propTypes = { | ||
children: React.PropTypes.string.isRequired, | ||
onClick: React.PropTypes.func, | ||
style: React.PropTypes.object, | ||
}; | ||
|
||
export default Button; | ||
|
||
let A; | ||
A = [1,2,2,2]; | ||
|
||
function abc() { | ||
let c = function cef() { | ||
A = 'str'; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
|
||
var _react = require('react'); | ||
|
||
var _react2 = _interopRequireDefault(_react); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
var Button = function Button(_ref) { | ||
var children = _ref.children; | ||
var onClick = _ref.onClick; | ||
var _ref$style = _ref.style; | ||
var style = _ref$style === undefined ? {} : _ref$style; | ||
return _react2.default.createElement( | ||
'button', | ||
{ | ||
style: {}, | ||
onClick: onClick | ||
}, | ||
children | ||
); | ||
}; | ||
|
||
Button.propTypes = { | ||
children: _react2.default.PropTypes.string.isRequired, | ||
onClick: _react2.default.PropTypes.func, | ||
style: _react2.default.PropTypes.object | ||
}; | ||
|
||
exports.default = Button; | ||
|
||
|
||
var A = void 0; | ||
A = [1, 2, 2, 2]; | ||
|
||
function abc() { | ||
var c = function cef() { | ||
A = 'str'; | ||
}; | ||
} | ||
Button.__docgenInfo = { | ||
description: '', | ||
methods: [], | ||
props: { | ||
children: { | ||
type: { | ||
name: 'string' | ||
}, | ||
required: true, | ||
description: '' | ||
}, | ||
onClick: { | ||
type: { | ||
name: 'func' | ||
}, | ||
required: false, | ||
description: '' | ||
}, | ||
style: { | ||
type: { | ||
name: 'object' | ||
}, | ||
required: false, | ||
description: '' | ||
} | ||
} | ||
}; | ||
|
||
if (typeof STORYBOOK_REACT_CLASSES !== 'undefined') { | ||
STORYBOOK_REACT_CLASSES['test/fixtures/case3/actual.js'] = { | ||
name: 'Button', | ||
docgenInfo: Button.__docgenInfo, | ||
path: 'test/fixtures/case3/actual.js' | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
|
||
const Button = ({ children, onClick, style = {} }) => { | ||
return ( | ||
<button | ||
style={{ }} | ||
onClick={onClick} | ||
> | ||
{children} | ||
</button> | ||
); | ||
}; | ||
|
||
Button.propTypes = { | ||
children: React.PropTypes.string.isRequired, | ||
onClick: React.PropTypes.func, | ||
style: React.PropTypes.object, | ||
}; | ||
|
||
export default Button; | ||
|
||
let A; | ||
A = [1,2,2,2]; | ||
|
||
function abc() { | ||
let c = function cef() { | ||
A = 'str'; | ||
}; | ||
} |
Oops, something went wrong.