Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub-components support for jsx-props-no-spreading #2534

Merged
merged 1 commit into from Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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]
}]
});