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

Handle member expressions in jsx-props-no-multi-spaces #1890

Merged
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
14 changes: 9 additions & 5 deletions lib/rules/jsx-props-no-multi-spaces.js
Expand Up @@ -27,12 +27,16 @@ module.exports = {
const sourceCode = context.getSourceCode();

function getPropName(propNode) {
if (propNode.type === 'JSXSpreadAttribute') {
return sourceCode.getText(propNode.argument);
} else if (propNode.type === 'JSXIdentifier') {
return propNode.name;
switch (propNode.type) {
case 'JSXSpreadAttribute':
return sourceCode.getText(propNode.argument);
case 'JSXIdentifier':
return propNode.name;
case 'JSXMemberExpression':
return `${getPropName(propNode.object)}.${propNode.property.name}`;
default:
return propNode.name.name;
}
return propNode.name.name;
}

function checkSpacing(prev, node) {
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/rules/jsx-props-no-multi-spaces.js
Expand Up @@ -58,6 +58,10 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
' foo {...test}',
' bar />'
].join('\n')
}, {
code: '<Foo.Bar baz="quux" />'
}, {
code: '<Foobar.Foo.Bar.Baz.Qux.Quux.Quuz.Corge.Grault.Garply.Waldo.Fred.Plugh xyzzy="thud" />'
}],

invalid: [{
Expand Down Expand Up @@ -106,5 +110,17 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
{message: 'Expected only one space between "foo" and "test"'},
{message: 'Expected only one space between "test" and "bar"'}
]
}, {
code: '<Foo.Bar baz="quux" />',
output: '<Foo.Bar baz="quux" />',
errors: [
{message: 'Expected only one space between "Foo.Bar" and "baz"'}
]
}, {
code: '<Foobar.Foo.Bar.Baz.Qux.Quux.Quuz.Corge.Grault.Garply.Waldo.Fred.Plugh xyzzy="thud" />',
output: '<Foobar.Foo.Bar.Baz.Qux.Quux.Quuz.Corge.Grault.Garply.Waldo.Fred.Plugh xyzzy="thud" />',
errors: [
{message: 'Expected only one space between "Foobar.Foo.Bar.Baz.Qux.Quux.Quuz.Corge.Grault.Garply.Waldo.Fred.Plugh" and "xyzzy"'}
]
}]
});