Skip to content

Commit

Permalink
[Fix] jsx-props-no-spreading: add support for namespaced jsx compon…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
jonathanpalma authored and ljharb committed Dec 20, 2019
1 parent e69b113 commit f9aee94
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Expand Up @@ -7,6 +7,7 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
quote_type = single

[*.md]
trim_trailing_whitespace = false
16 changes: 14 additions & 2 deletions lib/rules/jsx-props-no-spreading.js
Expand Up @@ -79,11 +79,23 @@ module.exports = {
const exceptions = configuration.exceptions || DEFAULTS.exceptions;
const isException = (tag, allExceptions) => allExceptions.indexOf(tag) !== -1;
const isProperty = property => property.type === 'Property';
const getTagNameFromMemberExpression = node => `${node.property.parent.object.name}.${node.property.name}`;
return {
JSXSpreadAttribute(node) {
const tagName = node.parent.name.name;
const jsxOpeningElement = node.parent.name;
const type = jsxOpeningElement.type;

let tagName;
if (type === 'JSXIdentifier') {
tagName = jsxOpeningElement.name;
} else if (type === 'JSXMemberExpression') {
tagName = getTagNameFromMemberExpression(jsxOpeningElement);
} else {
tagName = undefined;
}

const isHTMLTag = tagName && tagName[0] !== tagName[0].toUpperCase();
const isCustomTag = tagName && tagName[0] === tagName[0].toUpperCase();
const isCustomTag = tagName && (tagName[0] === tagName[0].toUpperCase() || tagName.includes('.'));
if (
isHTMLTag &&
((ignoreHtmlTags && !isException(tagName, exceptions)) ||
Expand Down
41 changes: 41 additions & 0 deletions tests/lib/rules/jsx-props-no-spreading.js
Expand Up @@ -89,6 +89,37 @@ ruleTester.run('jsx-props-no-spreading', rule, {
</App>
`,
options: [{explicitSpread: 'ignore'}]
}, {
code: [
'const props = {};',
'<App>',
' <components.Group {...props}/>',
' <Nav.Item {...props}/>',
'</App>'
].join('\n'),
options: [{exceptions: ['components.Group', 'Nav.Item']}]
}, {
code: [
'const props = {};',
'<App>',
' <components.Group {...props}/>',
' <Nav.Item {...props}/>',
'</App>'
].join('\n'),
options: [{custom: 'ignore'}]
}, {
code: [
'const props = {};',
'<App>',
' <components.Group {...props}/>',
' <Nav.Item {...props}/>',
'</App>'
].join('\n'),
options: [{
custom: 'enforce',
html: 'ignore',
exceptions: ['components.Group', 'Nav.Item']
}]
}],

invalid: [{
Expand Down Expand Up @@ -192,5 +223,15 @@ ruleTester.run('jsx-props-no-spreading', rule, {
`,
options: [{explicitSpread: 'ignore'}],
errors: [expectedError]
}, {
code: [
'const props = {};',
'<App>',
' <components.Group {...props}/>',
' <Nav.Item {...props}/>',
'</App>'
].join('\n'),
options: [{exceptions: ['components.DropdownIndicator', 'Nav.Item']}],
errors: [expectedError]
}]
});

0 comments on commit f9aee94

Please sign in to comment.