diff --git a/lib/rules/jsx-curly-spacing.js b/lib/rules/jsx-curly-spacing.js
index d9db3c2fb3..9aaa4d3520 100644
--- a/lib/rules/jsx-curly-spacing.js
+++ b/lib/rules/jsx-curly-spacing.js
@@ -228,6 +228,13 @@ module.exports = {
message: `There should be no space after '${token.value}'`,
fix: function(fixer) {
const nextToken = sourceCode.getTokenAfter(token);
+ const nextComment = sourceCode.getCommentsAfter(token);
+
+ // Take comments into consideration to narrow the fix range to what is actually affected. (See #1414)
+ if (nextComment.length > 0) {
+ return fixByTrimmingWhitespace(fixer, token.range[1], Math.min(nextToken.range[0], nextComment[0].start), 'start');
+ }
+
return fixByTrimmingWhitespace(fixer, token.range[1], nextToken.range[0], 'start');
}
});
@@ -246,6 +253,13 @@ module.exports = {
message: `There should be no space before '${token.value}'`,
fix: function(fixer) {
const previousToken = sourceCode.getTokenBefore(token);
+ const previousComment = sourceCode.getCommentsBefore(token);
+
+ // Take comments into consideration to narrow the fix range to what is actually affected. (See #1414)
+ if (previousComment.length > 0) {
+ return fixByTrimmingWhitespace(fixer, Math.max(previousToken.range[1], previousComment[0].end), token.range[0], 'end');
+ }
+
return fixByTrimmingWhitespace(fixer, previousToken.range[1], token.range[0], 'end');
}
});
diff --git a/tests/lib/rules/jsx-curly-spacing.js b/tests/lib/rules/jsx-curly-spacing.js
index d42ce7b0fd..18078f9538 100644
--- a/tests/lib/rules/jsx-curly-spacing.js
+++ b/tests/lib/rules/jsx-curly-spacing.js
@@ -59,9 +59,9 @@ ruleTester.run('jsx-curly-spacing', rule, {
';'
].join('\n')
}, {
- code: '{ foo /* comment */ }'
+ code: '{ foo /* comment 1 */ }'
}, {
- code: '{ /* comment */ foo }'
+ code: '{ /* comment 1 */ foo }'
}, {
code: ';',
options: [{attributes: true}]
@@ -264,7 +264,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
].join('\n'),
options: [{attributes: {when: 'never', allowMultiline: true}}]
}, {
- code: ';',
+ code: ';',
options: [{attributes: {when: 'never'}}]
}, {
code: ';',
@@ -345,11 +345,11 @@ ruleTester.run('jsx-curly-spacing', rule, {
options: [{children: {when: 'never', allowMultiline: true}}]
}, {
code: [
- '{/* comment */};'
+ '{/* comment 3 */};'
].join('\n'),
options: [{children: {when: 'never'}}]
}, {
- code: '{bar/* comment */};',
+ code: '{bar/* comment 4 */};',
options: [{children: {when: 'never'}}]
}, {
code: '{ bar };',
@@ -411,7 +411,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
].join('\n'),
options: [{attributes: {when: 'never', allowMultiline: true}}]
}, {
- code: ';',
+ code: ';',
options: [{attributes: {when: 'never'}}]
}, {
code: ';'
@@ -440,13 +440,13 @@ ruleTester.run('jsx-curly-spacing', rule, {
].join('\n'),
options: [{attributes: {when: 'always'}}]
}, {
- code: ';',
+ code: ';',
options: [{attributes: {when: 'never'}}]
}, {
code: '',
options: [{attributes: {when: 'never', spacing: {objectLiterals: 'always'}}}]
}, {
- code: '{bar/* comment */};',
+ code: '{bar/* comment 8 */};',
options: [{children: {when: 'never'}}]
}, {
code: '{bar} {baz};'
@@ -475,7 +475,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
].join('\n'),
options: [{children: {when: 'always'}}]
}, {
- code: '{bar/* comment */} {baz/* comment */};',
+ code: '{bar/* comment 9 */} {baz/* comment 10 */};',
options: [{children: {when: 'never'}}]
}, {
code: '{3} { {a: 2} }',
@@ -572,11 +572,11 @@ ruleTester.run('jsx-curly-spacing', rule, {
options: ['never']
}, {
code: [
- '{/* comment */};'
+ '{/* comment 11 */};'
].join('\n'),
options: ['never']
}, {
- code: ';',
+ code: ';',
options: ['never']
}, {
code: ';',
@@ -636,7 +636,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
].join('\n'),
options: ['never']
}, {
- code: ';',
+ code: ';',
options: ['never']
}, {
code: ';',
@@ -663,7 +663,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
].join('\n'),
options: ['always']
}, {
- code: ';',
+ code: ';',
options: ['never']
}, {
code: '',
@@ -1514,16 +1514,16 @@ ruleTester.run('jsx-curly-spacing', rule, {
message: 'A space is required before \'}\''
}]
}, {
- code: '',
- output: '',
+ code: '',
+ output: '',
errors: [{
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}]
}, {
- code: '',
- output: '',
+ code: '',
+ output: '',
options: [{attributes: {when: 'always'}}],
errors: [{
message: 'A space is required after \'{\''
@@ -1531,16 +1531,16 @@ ruleTester.run('jsx-curly-spacing', rule, {
message: 'A space is required before \'}\''
}]
}, {
- code: '',
- output: '',
+ code: '',
+ output: '',
errors: [{
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}]
}, {
- code: '',
- output: '',
+ code: '',
+ output: '',
options: [{attributes: {when: 'always'}}],
errors: [{
message: 'A space is required after \'{\''
@@ -1687,8 +1687,8 @@ ruleTester.run('jsx-curly-spacing', rule, {
message: 'A space is required before \'}\''
}]
}, {
- code: '{foo /* comment */}',
- output: '{ foo /* comment */ }',
+ code: '{foo /* comment 20 */}',
+ output: '{ foo /* comment 20 */ }',
options: [{children: {when: 'always'}}],
errors: [{
message: 'A space is required after \'{\''
@@ -1696,8 +1696,8 @@ ruleTester.run('jsx-curly-spacing', rule, {
message: 'A space is required before \'}\''
}]
}, {
- code: '{/* comment */ foo}',
- output: '{ /* comment */ foo }',
+ code: '{/* comment 21 */ foo}',
+ output: '{ /* comment 21 */ foo }',
options: [{children: {when: 'always'}}],
errors: [{
message: 'A space is required after \'{\''
@@ -2108,8 +2108,8 @@ ruleTester.run('jsx-curly-spacing', rule, {
message: 'A space is required before \'}\''
}]
}, {
- code: '',
- output: '',
+ code: '',
+ output: '',
options: ['always'],
errors: [{
message: 'A space is required after \'{\''
@@ -2117,8 +2117,8 @@ ruleTester.run('jsx-curly-spacing', rule, {
message: 'A space is required before \'}\''
}]
}, {
- code: '',
- output: '',
+ code: '',
+ output: '',
options: ['always'],
errors: [{
message: 'A space is required after \'{\''
@@ -2126,22 +2126,22 @@ ruleTester.run('jsx-curly-spacing', rule, {
message: 'A space is required before \'}\''
}]
}, {
- code: '{/*comment*/ }',
- output: '{/*comment*/}',
+ code: '{/*comment24*/ }',
+ output: '{/*comment24*/}',
options: [{children: {when: 'never'}}],
errors: [{
message: 'There should be no space before \'}\''
}]
}, {
- code: '{ /*comment*/}',
- output: '{/*comment*/}',
+ code: '{ /*comment25*/}',
+ output: '{/*comment25*/}',
options: [{children: {when: 'never'}}],
errors: [{
message: 'There should be no space after \'{\''
}]
}, {
- code: '{/*comment*/}',
- output: '{ /*comment*/ }',
+ code: '{/*comment26*/}',
+ output: '{ /*comment26*/ }',
options: [{children: {when: 'always'}}],
errors: [{
message: 'A space is required after \'{\''
@@ -2151,13 +2151,47 @@ ruleTester.run('jsx-curly-spacing', rule, {
}, {
code: [
'',
- '{/*comment*/',
+ '{ /* comment 27 */ }',
+ ';'
+ ].join('\n'),
+ output: [
+ '',
+ '{/* comment 27 */}',
+ ';'
+ ].join('\n'),
+ options: [{when: 'never', children: true}],
+ errors: [{
+ message: 'There should be no space after \'{\''
+ }, {
+ message: 'There should be no space before \'}\''
+ }]
+ }, {
+ code: [
+ '',
+ '{/* comment 28 */}',
+ ';'
+ ].join('\n'),
+ output: [
+ '',
+ '{ /* comment 28 */ }',
+ ';'
+ ].join('\n'),
+ options: [{when: 'always', children: true}],
+ errors: [{
+ message: 'A space is required after \'{\''
+ }, {
+ message: 'A space is required before \'}\''
+ }]
+ }, {
+ code: [
+ '',
+ '{/*comment29*/',
'}',
''
].join('\n'),
output: [
'',
- '{/*comment*/}',
+ '{/*comment29*/}',
''
].join('\n'),
options: [{children: {when: 'never', allowMultiline: false}}],
@@ -2168,17 +2202,59 @@ ruleTester.run('jsx-curly-spacing', rule, {
code: [
'',
'{',
- '/*comment*/}',
+ '/*comment30*/}',
''
].join('\n'),
output: [
'',
- '{/*comment*/}',
+ '{/*comment30*/}',
''
].join('\n'),
options: [{children: {when: 'never', allowMultiline: false}}],
errors: [{
message: 'There should be no newline after \'{\''
}]
+ }, {
+ code: [
+ '{ /* comment 31 */',
+ 'bar',
+ '} {',
+ 'baz',
+ '/* comment 32 */ };'
+ ].join('\n'),
+ output: [
+ '{/* comment 31 */',
+ 'bar',
+ '} {',
+ 'baz',
+ '/* comment 32 */};'
+ ].join('\n'),
+ options: [{when: 'never', children: true}],
+ errors: [{
+ message: 'There should be no space after \'{\''
+ }, {
+ message: 'There should be no space before \'}\''
+ }]
+ }, {
+ code: [
+ '{/* comment 33 */',
+ 'bar',
+ '} {',
+ 'baz',
+ '/* comment 33 */};'
+ ].join('\n'),
+ output: [
+ '{ /* comment 33 */',
+ 'bar',
+ '} {',
+ 'baz',
+ '/* comment 33 */ };'
+ ].join('\n'),
+ options: [{when: 'always', children: true}],
+ errors: [{
+ message: 'A space is required after \'{\''
+ }, {
+ message: 'A space is required before \'}\''
+ }]
}]
});