Skip to content

Commit

Permalink
Add auto fix for jsx-equals-spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
peet committed Mar 18, 2016
1 parent 3365231 commit ef0d1d4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ The plugin has a [recommended configuration](#user-content-recommended-configura
* [jsx-boolean-value](docs/rules/jsx-boolean-value.md): Enforce boolean attributes notation in JSX (fixable)
* [jsx-closing-bracket-location](docs/rules/jsx-closing-bracket-location.md): Validate closing bracket location in JSX
* [jsx-curly-spacing](docs/rules/jsx-curly-spacing.md): Enforce or disallow spaces inside of curly braces in JSX attributes (fixable)
* [jsx-equals-spacing](docs/rules/jsx-equals-spacing.md): Enforce or disallow spaces around equal signs in JSX attributes
* [jsx-equals-spacing](docs/rules/jsx-equals-spacing.md): Enforce or disallow spaces around equal signs in JSX attributes (fixable)
* [jsx-handler-names](docs/rules/jsx-handler-names.md): Enforce event handler naming conventions in JSX
* [jsx-indent-props](docs/rules/jsx-indent-props.md): Validate props indentation in JSX (fixable)
* [jsx-indent](docs/rules/jsx-indent.md): Validate JSX indentation
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/jsx-equals-spacing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Some style guides require or disallow spaces around equal signs.

**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line.

## Rule Details

This rule will enforce consistency of spacing around equal signs in JSX attributes, by requiring or disallowing one or more spaces before and after `=`.
Expand Down
20 changes: 16 additions & 4 deletions lib/rules/jsx-equals-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ module.exports = function(context) {
context.report({
node: attrNode,
loc: equalToken.loc.start,
message: 'There should be no space before \'=\''
message: 'There should be no space before \'=\'',
fix: function(fixer) {
return fixer.removeRange([attrNode.name.range[1], equalToken.start]);
}
});
}
if (spacedAfter) {
context.report({
node: attrNode,
loc: equalToken.loc.start,
message: 'There should be no space after \'=\''
message: 'There should be no space after \'=\'',
fix: function(fixer) {
return fixer.removeRange([equalToken.end, attrNode.value.range[0]]);
}
});
}
break;
Expand All @@ -59,14 +65,20 @@ module.exports = function(context) {
context.report({
node: attrNode,
loc: equalToken.loc.start,
message: 'A space is required before \'=\''
message: 'A space is required before \'=\'',
fix: function(fixer) {
return fixer.insertTextBefore(equalToken, ' ');
}
});
}
if (!spacedAfter) {
context.report({
node: attrNode,
loc: equalToken.loc.start,
message: 'A space is required after \'=\''
message: 'A space is required after \'=\'',
fix: function(fixer) {
return fixer.insertTextAfter(equalToken, ' ');
}
});
}
break;
Expand Down
9 changes: 9 additions & 0 deletions tests/lib/rules/jsx-equals-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ ruleTester.run('jsx-equals-spacing', rule, {

invalid: [{
code: '<App foo = {bar} />',
output: '<App foo={bar} />',
parserOptions: parserOptions,
errors: [
{message: 'There should be no space before \'=\''},
{message: 'There should be no space after \'=\''}
]
}, {
code: '<App foo = {bar} />',
output: '<App foo={bar} />',
options: ['never'],
parserOptions: parserOptions,
errors: [
Expand All @@ -98,20 +100,23 @@ ruleTester.run('jsx-equals-spacing', rule, {
]
}, {
code: '<App foo ={bar} />',
output: '<App foo={bar} />',
options: ['never'],
parserOptions: parserOptions,
errors: [
{message: 'There should be no space before \'=\''}
]
}, {
code: '<App foo= {bar} />',
output: '<App foo={bar} />',
options: ['never'],
parserOptions: parserOptions,
errors: [
{message: 'There should be no space after \'=\''}
]
}, {
code: '<App foo= {bar} bar = {baz} />',
output: '<App foo={bar} bar={baz} />',
options: ['never'],
parserOptions: parserOptions,
errors: [
Expand All @@ -121,6 +126,7 @@ ruleTester.run('jsx-equals-spacing', rule, {
]
}, {
code: '<App foo={bar} />',
output: '<App foo = {bar} />',
options: ['always'],
parserOptions: parserOptions,
errors: [
Expand All @@ -129,20 +135,23 @@ ruleTester.run('jsx-equals-spacing', rule, {
]
}, {
code: '<App foo ={bar} />',
output: '<App foo = {bar} />',
options: ['always'],
parserOptions: parserOptions,
errors: [
{message: 'A space is required after \'=\''}
]
}, {
code: '<App foo= {bar} />',
output: '<App foo = {bar} />',
options: ['always'],
parserOptions: parserOptions,
errors: [
{message: 'A space is required before \'=\''}
]
}, {
code: '<App foo={bar} bar ={baz} />',
output: '<App foo = {bar} bar = {baz} />',
options: ['always'],
parserOptions: parserOptions,
errors: [
Expand Down

0 comments on commit ef0d1d4

Please sign in to comment.