diff --git a/lib/rules/jsx-no-constructed-context-values.js b/lib/rules/jsx-no-constructed-context-values.js index da0409791a..0412ea046d 100644 --- a/lib/rules/jsx-no-constructed-context-values.js +++ b/lib/rules/jsx-no-constructed-context-values.js @@ -143,12 +143,15 @@ module.exports = { return { JSXOpeningElement(node) { const openingElementName = node.name; - if (openingElementName.type !== 'JSXMemberExpression') { - // Has no member - return; - } - const isJsxContext = openingElementName.property.name === 'Provider'; + // Consider an element a context provider if the name is either: + // - identifier and ends with Provider (i.e. SomeContextProvider) + // - member expression and has Provider as a property (i.e. SomeContext.Provider) + const isJsxContext = (openingElementName.type === 'JSXMemberExpression' + && openingElementName.property.name === 'Provider') + || (openingElementName.type === 'JSXIdentifier' + && openingElementName.name.endsWith('Provider')); + if (!isJsxContext) { // Member is not Provider return; diff --git a/tests/lib/rules/jsx-no-constructed-context-values.js b/tests/lib/rules/jsx-no-constructed-context-values.js index 7a59da820c..4188a01365 100644 --- a/tests/lib/rules/jsx-no-constructed-context-values.js +++ b/tests/lib/rules/jsx-no-constructed-context-values.js @@ -33,6 +33,9 @@ ruleTester.run('react-no-constructed-context-values', rule, { { code: '', }, + { + code: '', + }, { code: '', }, @@ -155,6 +158,20 @@ ruleTester.run('react-no-constructed-context-values', rule, { }, }], }, + { + // Invalid because object construction creates a new identity + // Duplicate of above test but using an identifier as the context provider name + code: 'function Component() { const foo = {}; return () }', + errors: [{ + messageId: 'withIdentifierMsg', + data: { + variableName: 'foo', + type: 'object', + nodeLine: '1', + usageLine: '1', + }, + }], + }, { // Invalid because array construction creates a new identity code: 'function Component() { const foo = []; return () }',