diff --git a/lib/rules/require-prop-type-constructor.js b/lib/rules/require-prop-type-constructor.js index 52e88b2e7..61d825039 100644 --- a/lib/rules/require-prop-type-constructor.js +++ b/lib/rules/require-prop-type-constructor.js @@ -19,7 +19,7 @@ const forbiddenTypes = [ 'UpdateExpression' ] -const isForbiddenType = nodeType => forbiddenTypes.indexOf(nodeType) > -1 +const isForbiddenType = node => forbiddenTypes.indexOf(node.type) > -1 && node.raw !== 'null' module.exports = { meta: { @@ -46,7 +46,7 @@ module.exports = { } const checkPropertyNode = (p) => { - if (isForbiddenType(p.value.type)) { + if (isForbiddenType(p.value)) { context.report({ node: p.value, message, @@ -57,7 +57,7 @@ module.exports = { }) } else if (p.value.type === 'ArrayExpression') { p.value.elements - .filter(prop => isForbiddenType(prop.type)) + .filter(prop => isForbiddenType(prop)) .forEach(prop => context.report({ node: prop, message, @@ -79,20 +79,21 @@ module.exports = { if (!node) return - node.value.properties.forEach(p => { - if (isForbiddenType(p.value.type) || p.value.type === 'ArrayExpression') { - checkPropertyNode(p) - } else if (p.value.type === 'ObjectExpression') { - const typeProperty = p.value.properties.find(prop => - prop.type === 'Property' && - prop.key.name === 'type' - ) + node.value.properties + .forEach(p => { + if (isForbiddenType(p.value) || p.value.type === 'ArrayExpression') { + checkPropertyNode(p) + } else if (p.value.type === 'ObjectExpression') { + const typeProperty = p.value.properties.find(prop => + prop.type === 'Property' && + prop.key.name === 'type' + ) - if (!typeProperty) return + if (!typeProperty) return - checkPropertyNode(typeProperty) - } - }) + checkPropertyNode(typeProperty) + } + }) }) } } diff --git a/tests/lib/rules/require-prop-type-constructor.js b/tests/lib/rules/require-prop-type-constructor.js index f0740385b..fc6e79fc3 100644 --- a/tests/lib/rules/require-prop-type-constructor.js +++ b/tests/lib/rules/require-prop-type-constructor.js @@ -37,7 +37,9 @@ ruleTester.run('require-prop-type-constructor', rule, { }, lastProp: { type: [Number, Boolean] - } + }, + nullProp: null, + nullTypeProp: { type: null } } } ` @@ -58,7 +60,8 @@ ruleTester.run('require-prop-type-constructor', rule, { }, lastProp: { type: ['Boolean'] - } + }, + nullProp: 'null' } } `, @@ -73,7 +76,8 @@ ruleTester.run('require-prop-type-constructor', rule, { }, lastProp: { type: [Boolean] - } + }, + nullProp: null } } `, @@ -92,6 +96,9 @@ ruleTester.run('require-prop-type-constructor', rule, { }, { message: 'The "type" property should be a constructor.', line: 11 + }, { + message: 'The "nullProp" property should be a constructor.', + line: 13 }] }, {