Skip to content

Commit e3c27f9

Browse files
committed
Merge pull request #506 from peet/autofix-jsx-equals-spacing
Add auto fix for jsx-equals-spacing
2 parents 3365231 + ef0d1d4 commit e3c27f9

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ The plugin has a [recommended configuration](#user-content-recommended-configura
101101
* [jsx-boolean-value](docs/rules/jsx-boolean-value.md): Enforce boolean attributes notation in JSX (fixable)
102102
* [jsx-closing-bracket-location](docs/rules/jsx-closing-bracket-location.md): Validate closing bracket location in JSX
103103
* [jsx-curly-spacing](docs/rules/jsx-curly-spacing.md): Enforce or disallow spaces inside of curly braces in JSX attributes (fixable)
104-
* [jsx-equals-spacing](docs/rules/jsx-equals-spacing.md): Enforce or disallow spaces around equal signs in JSX attributes
104+
* [jsx-equals-spacing](docs/rules/jsx-equals-spacing.md): Enforce or disallow spaces around equal signs in JSX attributes (fixable)
105105
* [jsx-handler-names](docs/rules/jsx-handler-names.md): Enforce event handler naming conventions in JSX
106106
* [jsx-indent-props](docs/rules/jsx-indent-props.md): Validate props indentation in JSX (fixable)
107107
* [jsx-indent](docs/rules/jsx-indent.md): Validate JSX indentation

docs/rules/jsx-equals-spacing.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Some style guides require or disallow spaces around equal signs.
44

5+
**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line.
6+
57
## Rule Details
68

79
This rule will enforce consistency of spacing around equal signs in JSX attributes, by requiring or disallowing one or more spaces before and after `=`.

lib/rules/jsx-equals-spacing.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,20 @@ module.exports = function(context) {
4343
context.report({
4444
node: attrNode,
4545
loc: equalToken.loc.start,
46-
message: 'There should be no space before \'=\''
46+
message: 'There should be no space before \'=\'',
47+
fix: function(fixer) {
48+
return fixer.removeRange([attrNode.name.range[1], equalToken.start]);
49+
}
4750
});
4851
}
4952
if (spacedAfter) {
5053
context.report({
5154
node: attrNode,
5255
loc: equalToken.loc.start,
53-
message: 'There should be no space after \'=\''
56+
message: 'There should be no space after \'=\'',
57+
fix: function(fixer) {
58+
return fixer.removeRange([equalToken.end, attrNode.value.range[0]]);
59+
}
5460
});
5561
}
5662
break;
@@ -59,14 +65,20 @@ module.exports = function(context) {
5965
context.report({
6066
node: attrNode,
6167
loc: equalToken.loc.start,
62-
message: 'A space is required before \'=\''
68+
message: 'A space is required before \'=\'',
69+
fix: function(fixer) {
70+
return fixer.insertTextBefore(equalToken, ' ');
71+
}
6372
});
6473
}
6574
if (!spacedAfter) {
6675
context.report({
6776
node: attrNode,
6877
loc: equalToken.loc.start,
69-
message: 'A space is required after \'=\''
78+
message: 'A space is required after \'=\'',
79+
fix: function(fixer) {
80+
return fixer.insertTextAfter(equalToken, ' ');
81+
}
7082
});
7183
}
7284
break;

tests/lib/rules/jsx-equals-spacing.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,15 @@ ruleTester.run('jsx-equals-spacing', rule, {
8383

8484
invalid: [{
8585
code: '<App foo = {bar} />',
86+
output: '<App foo={bar} />',
8687
parserOptions: parserOptions,
8788
errors: [
8889
{message: 'There should be no space before \'=\''},
8990
{message: 'There should be no space after \'=\''}
9091
]
9192
}, {
9293
code: '<App foo = {bar} />',
94+
output: '<App foo={bar} />',
9395
options: ['never'],
9496
parserOptions: parserOptions,
9597
errors: [
@@ -98,20 +100,23 @@ ruleTester.run('jsx-equals-spacing', rule, {
98100
]
99101
}, {
100102
code: '<App foo ={bar} />',
103+
output: '<App foo={bar} />',
101104
options: ['never'],
102105
parserOptions: parserOptions,
103106
errors: [
104107
{message: 'There should be no space before \'=\''}
105108
]
106109
}, {
107110
code: '<App foo= {bar} />',
111+
output: '<App foo={bar} />',
108112
options: ['never'],
109113
parserOptions: parserOptions,
110114
errors: [
111115
{message: 'There should be no space after \'=\''}
112116
]
113117
}, {
114118
code: '<App foo= {bar} bar = {baz} />',
119+
output: '<App foo={bar} bar={baz} />',
115120
options: ['never'],
116121
parserOptions: parserOptions,
117122
errors: [
@@ -121,6 +126,7 @@ ruleTester.run('jsx-equals-spacing', rule, {
121126
]
122127
}, {
123128
code: '<App foo={bar} />',
129+
output: '<App foo = {bar} />',
124130
options: ['always'],
125131
parserOptions: parserOptions,
126132
errors: [
@@ -129,20 +135,23 @@ ruleTester.run('jsx-equals-spacing', rule, {
129135
]
130136
}, {
131137
code: '<App foo ={bar} />',
138+
output: '<App foo = {bar} />',
132139
options: ['always'],
133140
parserOptions: parserOptions,
134141
errors: [
135142
{message: 'A space is required after \'=\''}
136143
]
137144
}, {
138145
code: '<App foo= {bar} />',
146+
output: '<App foo = {bar} />',
139147
options: ['always'],
140148
parserOptions: parserOptions,
141149
errors: [
142150
{message: 'A space is required before \'=\''}
143151
]
144152
}, {
145153
code: '<App foo={bar} bar ={baz} />',
154+
output: '<App foo = {bar} bar = {baz} />',
146155
options: ['always'],
147156
parserOptions: parserOptions,
148157
errors: [

0 commit comments

Comments
 (0)