Skip to content

Commit 76e8791

Browse files
committed
Fix style-prop-object to deal with null and spread props that can't be
resolved (Fixes jsx-eslint#809) (Fixes jsx-eslint#812)
1 parent 0348173 commit 76e8791

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

lib/rules/style-prop-object.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ module.exports = {
4848
) {
4949
if (node.arguments[1].type === 'ObjectExpression') {
5050
var style = node.arguments[1].properties.find(function(property) {
51-
return property.key.name === 'style' && !property.computed;
51+
return property.key && property.key.name === 'style' && !property.computed;
5252
});
5353
if (style) {
5454
if (style.value.type === 'Identifier') {
5555
checkIdentifiers(style.value);
56-
} else if (style.value.type === 'Literal') {
56+
} else if (style.value.type === 'Literal' && style.value.value !== null) {
5757
context.report(style.value, 'Style prop value must be an object');
5858
}
5959
}
@@ -68,7 +68,7 @@ module.exports = {
6868

6969
if (
7070
node.value.type !== 'JSXExpressionContainer'
71-
|| node.value.expression.type === 'Literal'
71+
|| (node.value.expression.type === 'Literal' && node.value.expression.value !== null)
7272
) {
7373
context.report(node, 'Style prop value must be an object');
7474
} else if (node.value.expression.type === 'Identifier') {

tests/lib/rules/style-prop-object.js

+37
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,43 @@ ruleTester.run('style-prop-object', rule, {
114114
'}, \'My custom Elem\')'
115115
].join('\n'),
116116
parserOptions: parserOptions
117+
},
118+
{
119+
code: '<div style={null}></div>',
120+
parserOptions: parserOptions
121+
},
122+
{
123+
code: [
124+
'const props = { style: null };',
125+
'<div {...props} />'
126+
].join('\n'),
127+
parserOptions: parserOptions
128+
},
129+
{
130+
code: [
131+
'const otherProps = { style: null };',
132+
'const { a, b, ...props } = otherProps;',
133+
'<div {...props} />'
134+
].join('\n'),
135+
parserOptions: parserOptions
136+
},
137+
{
138+
code: [
139+
'React.createElement("div", {',
140+
' style: null',
141+
'})'
142+
].join('\n'),
143+
parserOptions: parserOptions
144+
},
145+
{
146+
code: [
147+
'const MyComponent = (props) => {',
148+
' React.createElement(MyCustomElem, {',
149+
' ...props',
150+
' });',
151+
'};'
152+
].join('\n'),
153+
parserOptions: parserOptions
117154
}
118155
],
119156
invalid: [

0 commit comments

Comments
 (0)