Skip to content

Commit

Permalink
Fix: Handle member expressions in jsx-props-no-multi-spaces
Browse files Browse the repository at this point in the history
Resolves #1881
  • Loading branch information
alexzherdev authored and ljharb committed Jul 20, 2018
1 parent a0100f3 commit 2aa0955
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
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"'}
]
}]
});

0 comments on commit 2aa0955

Please sign in to comment.