Skip to content

Commit 56849fa

Browse files
committed
Merge pull request #407 from ewendel/jsx-curly-spacing-autofix
Add auto fix for jsx-curly-spacing
2 parents 402709f + e9bcfc1 commit 56849fa

File tree

2 files changed

+70
-13
lines changed

2 files changed

+70
-13
lines changed

lib/rules/jsx-curly-spacing.js

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
/**
22
* @fileoverview Enforce or disallow spaces inside of curly braces in JSX attributes.
3-
* @author Jamund Ferguson, Brandyn Bennett, Michael Ficarra, Vignesh Anand, Jamund Ferguson, Yannick Croissant
3+
* @author Jamund Ferguson
4+
* @author Brandyn Bennett
5+
* @author Michael Ficarra
6+
* @author Vignesh Anand
7+
* @author Jamund Ferguson
8+
* @author Yannick Croissant
9+
* @author Erik Wendel
410
*/
511
'use strict';
612

@@ -43,8 +49,15 @@ module.exports = function(context) {
4349
* @returns {void}
4450
*/
4551
function reportNoBeginningNewline(node, token) {
46-
context.report(node, token.loc.start,
47-
'There should be no newline after \'' + token.value + '\'');
52+
context.report({
53+
node: node,
54+
loc: token.loc.start,
55+
message: 'There should be no newline after \'' + token.value + '\'',
56+
fix: function(fixer) {
57+
var nextToken = context.getSourceCode().getTokenAfter(token);
58+
return fixer.replaceTextRange([token.range[1], nextToken.range[0]], spaced ? ' ' : '');
59+
}
60+
});
4861
}
4962

5063
/**
@@ -54,8 +67,15 @@ module.exports = function(context) {
5467
* @returns {void}
5568
*/
5669
function reportNoEndingNewline(node, token) {
57-
context.report(node, token.loc.start,
58-
'There should be no newline before \'' + token.value + '\'');
70+
context.report({
71+
node: node,
72+
loc: token.loc.start,
73+
message: 'There should be no newline before \'' + token.value + '\'',
74+
fix: function(fixer) {
75+
var previousToken = context.getSourceCode().getTokenBefore(token);
76+
return fixer.replaceTextRange([previousToken.range[1], token.range[0]], spaced ? ' ' : '');
77+
}
78+
});
5979
}
6080

6181
/**
@@ -65,8 +85,15 @@ module.exports = function(context) {
6585
* @returns {void}
6686
*/
6787
function reportNoBeginningSpace(node, token) {
68-
context.report(node, token.loc.start,
69-
'There should be no space after \'' + token.value + '\'');
88+
context.report({
89+
node: node,
90+
loc: token.loc.start,
91+
message: 'There should be no space after \'' + token.value + '\'',
92+
fix: function(fixer) {
93+
var nextToken = context.getSourceCode().getTokenAfter(token);
94+
return fixer.removeRange([token.range[1], nextToken.range[0]]);
95+
}
96+
});
7097
}
7198

7299
/**
@@ -76,8 +103,15 @@ module.exports = function(context) {
76103
* @returns {void}
77104
*/
78105
function reportNoEndingSpace(node, token) {
79-
context.report(node, token.loc.start,
80-
'There should be no space before \'' + token.value + '\'');
106+
context.report({
107+
node: node,
108+
loc: token.loc.start,
109+
message: 'There should be no space before \'' + token.value + '\'',
110+
fix: function(fixer) {
111+
var previousToken = context.getSourceCode().getTokenBefore(token);
112+
return fixer.removeRange([previousToken.range[1], token.range[0]]);
113+
}
114+
});
81115
}
82116

83117
/**
@@ -87,8 +121,14 @@ module.exports = function(context) {
87121
* @returns {void}
88122
*/
89123
function reportRequiredBeginningSpace(node, token) {
90-
context.report(node, token.loc.start,
91-
'A space is required after \'' + token.value + '\'');
124+
context.report({
125+
node: node,
126+
loc: token.loc.start,
127+
message: 'A space is required after \'' + token.value + '\'',
128+
fix: function(fixer) {
129+
return fixer.insertTextAfter(token, ' ');
130+
}
131+
});
92132
}
93133

94134
/**
@@ -98,8 +138,14 @@ module.exports = function(context) {
98138
* @returns {void}
99139
*/
100140
function reportRequiredEndingSpace(node, token) {
101-
context.report(node, token.loc.start,
102-
'A space is required before \'' + token.value + '\'');
141+
context.report({
142+
node: node,
143+
loc: token.loc.start,
144+
message: 'A space is required before \'' + token.value + '\'',
145+
fix: function(fixer) {
146+
return fixer.insertTextBefore(token, ' ');
147+
}
148+
});
103149
}
104150

105151
/**

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @fileoverview Enforce or disallow spaces inside of curly braces in JSX attributes.
33
* @author Yannick Croissant
4+
* @author Erik Wendel
45
*/
56
'use strict';
67

@@ -81,6 +82,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
8182

8283
invalid: [{
8384
code: '<App foo={ bar } />;',
85+
output: '<App foo={bar} />;',
8486
options: ['never'],
8587
errors: [{
8688
message: 'There should be no space after \'{\''
@@ -90,6 +92,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
9092
parserOptions: parserOptions
9193
}, {
9294
code: '<App foo={ bar } />;',
95+
output: '<App foo={bar} />;',
9396
options: ['never', {allowMultiline: false}],
9497
errors: [{
9598
message: 'There should be no space after \'{\''
@@ -99,6 +102,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
99102
parserOptions: parserOptions
100103
}, {
101104
code: '<App foo={bar} />;',
105+
output: '<App foo={ bar } />;',
102106
options: ['always'],
103107
errors: [{
104108
message: 'A space is required after \'{\''
@@ -108,6 +112,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
108112
parserOptions: parserOptions
109113
}, {
110114
code: '<App foo={bar} />;',
115+
output: '<App foo={ bar } />;',
111116
options: ['always', {allowMultiline: false}],
112117
errors: [{
113118
message: 'A space is required after \'{\''
@@ -117,27 +122,31 @@ ruleTester.run('jsx-curly-spacing', rule, {
117122
parserOptions: parserOptions
118123
}, {
119124
code: '<App foo={ bar} />;',
125+
output: '<App foo={ bar } />;',
120126
options: ['always'],
121127
errors: [{
122128
message: 'A space is required before \'}\''
123129
}],
124130
parserOptions: parserOptions
125131
}, {
126132
code: '<App foo={bar } />;',
133+
output: '<App foo={ bar } />;',
127134
options: ['always'],
128135
errors: [{
129136
message: 'A space is required after \'{\''
130137
}],
131138
parserOptions: parserOptions
132139
}, {
133140
code: '<App foo={ bar} />;',
141+
output: '<App foo={bar} />;',
134142
options: ['never'],
135143
errors: [{
136144
message: 'There should be no space after \'{\''
137145
}],
138146
parserOptions: parserOptions
139147
}, {
140148
code: '<App foo={bar } />;',
149+
output: '<App foo={bar} />;',
141150
options: ['never'],
142151
errors: [{
143152
message: 'There should be no space before \'}\''
@@ -149,6 +158,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
149158
'bar',
150159
'} />;'
151160
].join('\n'),
161+
output: '<App foo={bar} />;',
152162
options: ['never', {allowMultiline: false}],
153163
errors: [{
154164
message: 'There should be no space after \'{\''
@@ -162,6 +172,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
162172
'bar',
163173
'} />;'
164174
].join('\n'),
175+
output: '<App foo={ bar } />;',
165176
options: ['always', {allowMultiline: false}],
166177
errors: [{
167178
message: 'There should be no newline after \'{\''

0 commit comments

Comments
 (0)