Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix style-prop-object to deal with null and spread props that can't be resolved #844

Merged
merged 1 commit into from
Sep 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/rules/style-prop-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
return item.name === node.name;
});

if (!variable || !variable.defs[0].node.init) {
if (!variable || !variable.defs[0] || !variable.defs[0].node.init) {
return;
}

Expand All @@ -48,12 +48,12 @@ module.exports = {
) {
if (node.arguments[1].type === 'ObjectExpression') {
var style = node.arguments[1].properties.find(function(property) {
return property.key.name === 'style' && !property.computed;
return property.key && property.key.name === 'style' && !property.computed;
});
if (style) {
if (style.value.type === 'Identifier') {
checkIdentifiers(style.value);
} else if (style.value.type === 'Literal') {
} else if (style.value.type === 'Literal' && style.value.value !== null) {
context.report(style.value, 'Style prop value must be an object');
}
}
Expand All @@ -68,7 +68,7 @@ module.exports = {

if (
node.value.type !== 'JSXExpressionContainer'
|| node.value.expression.type === 'Literal'
|| (node.value.expression.type === 'Literal' && node.value.expression.value !== null)
) {
context.report(node, 'Style prop value must be an object');
} else if (node.value.expression.type === 'Identifier') {
Expand Down
80 changes: 80 additions & 0 deletions tests/lib/rules/style-prop-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,86 @@ ruleTester.run('style-prop-object', rule, {
'}, \'My custom Elem\')'
].join('\n'),
parserOptions: parserOptions
},
{
code: [
'let style;',
'<div style={style}></div>'
].join('\n'),
parserOptions: parserOptions
},
{
code: '<div style={undefined}></div>',
parserOptions: parserOptions
},
{
code: [
'const props = { style: undefined };',
'<div {...props} />'
].join('\n'),
parserOptions: parserOptions
},
{
code: [
'const otherProps = { style: undefined };',
'const { a, b, ...props } = otherProps;',
'<div {...props} />'
].join('\n'),
parserOptions: parserOptions
},
{
code: [
'React.createElement("div", {',
' style: undefined',
'})'
].join('\n'),
parserOptions: parserOptions
},
{
code: [
'let style;',
'React.createElement("div", {',
' style',
'})'
].join('\n'),
parserOptions: parserOptions
},
{
code: '<div style={null}></div>',
parserOptions: parserOptions
},
{
code: [
'const props = { style: null };',
'<div {...props} />'
].join('\n'),
parserOptions: parserOptions
},
{
code: [
'const otherProps = { style: null };',
'const { a, b, ...props } = otherProps;',
'<div {...props} />'
].join('\n'),
parserOptions: parserOptions
},
{
code: [
'React.createElement("div", {',
' style: null',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will you please also add tests like the null tests but for undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops meant to add those. Fixed.

'})'
].join('\n'),
parserOptions: parserOptions
},
{
code: [
'const MyComponent = (props) => {',
' React.createElement(MyCustomElem, {',
' ...props',
' });',
'};'
].join('\n'),
parserOptions: parserOptions
}
],
invalid: [
Expand Down