diff --git a/lib/rules/style-prop-object.js b/lib/rules/style-prop-object.js index 1b861ca160..8f3b3a470e 100644 --- a/lib/rules/style-prop-object.js +++ b/lib/rules/style-prop-object.js @@ -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; } @@ -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'); } } @@ -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') { diff --git a/tests/lib/rules/style-prop-object.js b/tests/lib/rules/style-prop-object.js index 19db43518a..8d4f1b711e 100644 --- a/tests/lib/rules/style-prop-object.js +++ b/tests/lib/rules/style-prop-object.js @@ -114,6 +114,86 @@ ruleTester.run('style-prop-object', rule, { '}, \'My custom Elem\')' ].join('\n'), parserOptions: parserOptions + }, + { + code: [ + 'let style;', + '
' + ].join('\n'), + parserOptions: parserOptions + }, + { + code: '
', + parserOptions: parserOptions + }, + { + code: [ + 'const props = { style: undefined };', + '
' + ].join('\n'), + parserOptions: parserOptions + }, + { + code: [ + 'const otherProps = { style: undefined };', + 'const { a, b, ...props } = otherProps;', + '
' + ].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: '
', + parserOptions: parserOptions + }, + { + code: [ + 'const props = { style: null };', + '
' + ].join('\n'), + parserOptions: parserOptions + }, + { + code: [ + 'const otherProps = { style: null };', + 'const { a, b, ...props } = otherProps;', + '
' + ].join('\n'), + parserOptions: parserOptions + }, + { + code: [ + 'React.createElement("div", {', + ' style: null', + '})' + ].join('\n'), + parserOptions: parserOptions + }, + { + code: [ + 'const MyComponent = (props) => {', + ' React.createElement(MyCustomElem, {', + ' ...props', + ' });', + '};' + ].join('\n'), + parserOptions: parserOptions } ], invalid: [