Skip to content

Commit e169799

Browse files
authored
Merge pull request #844 from petersendidit/FixStylePropRule
Fix style-prop-object to deal with null and spread props that can't be resolved
2 parents f0fd75b + 264754e commit e169799

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

lib/rules/style-prop-object.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = {
2929
return item.name === node.name;
3030
});
3131

32-
if (!variable || !variable.defs[0].node.init) {
32+
if (!variable || !variable.defs[0] || !variable.defs[0].node.init) {
3333
return;
3434
}
3535

@@ -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

+80
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,86 @@ ruleTester.run('style-prop-object', rule, {
114114
'}, \'My custom Elem\')'
115115
].join('\n'),
116116
parserOptions: parserOptions
117+
},
118+
{
119+
code: [
120+
'let style;',
121+
'<div style={style}></div>'
122+
].join('\n'),
123+
parserOptions: parserOptions
124+
},
125+
{
126+
code: '<div style={undefined}></div>',
127+
parserOptions: parserOptions
128+
},
129+
{
130+
code: [
131+
'const props = { style: undefined };',
132+
'<div {...props} />'
133+
].join('\n'),
134+
parserOptions: parserOptions
135+
},
136+
{
137+
code: [
138+
'const otherProps = { style: undefined };',
139+
'const { a, b, ...props } = otherProps;',
140+
'<div {...props} />'
141+
].join('\n'),
142+
parserOptions: parserOptions
143+
},
144+
{
145+
code: [
146+
'React.createElement("div", {',
147+
' style: undefined',
148+
'})'
149+
].join('\n'),
150+
parserOptions: parserOptions
151+
},
152+
{
153+
code: [
154+
'let style;',
155+
'React.createElement("div", {',
156+
' style',
157+
'})'
158+
].join('\n'),
159+
parserOptions: parserOptions
160+
},
161+
{
162+
code: '<div style={null}></div>',
163+
parserOptions: parserOptions
164+
},
165+
{
166+
code: [
167+
'const props = { style: null };',
168+
'<div {...props} />'
169+
].join('\n'),
170+
parserOptions: parserOptions
171+
},
172+
{
173+
code: [
174+
'const otherProps = { style: null };',
175+
'const { a, b, ...props } = otherProps;',
176+
'<div {...props} />'
177+
].join('\n'),
178+
parserOptions: parserOptions
179+
},
180+
{
181+
code: [
182+
'React.createElement("div", {',
183+
' style: null',
184+
'})'
185+
].join('\n'),
186+
parserOptions: parserOptions
187+
},
188+
{
189+
code: [
190+
'const MyComponent = (props) => {',
191+
' React.createElement(MyCustomElem, {',
192+
' ...props',
193+
' });',
194+
'};'
195+
].join('\n'),
196+
parserOptions: parserOptions
117197
}
118198
],
119199
invalid: [

0 commit comments

Comments
 (0)