From b4141d801dd7b22268c687feaa9b7b2477e91180 Mon Sep 17 00:00:00 2001 From: Andreas Opferkuch Date: Wed, 6 Sep 2017 18:18:14 +0200 Subject: [PATCH 1/3] [Tests] Added tests for jsx-curly-spacing that deal with comments in children. See also: https://github.com/yannickcr/eslint-plugin-react/issues/648#issuecomment-327519650 --- tests/lib/rules/jsx-curly-spacing.js | 76 ++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/tests/lib/rules/jsx-curly-spacing.js b/tests/lib/rules/jsx-curly-spacing.js index d42ce7b0fd..eaff650519 100644 --- a/tests/lib/rules/jsx-curly-spacing.js +++ b/tests/lib/rules/jsx-curly-spacing.js @@ -2148,6 +2148,40 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { message: 'A space is required before \'}\'' }] + }, { + code: [ + '', + '{ /* comment */ }', + ';' + ].join('\n'), + output: [ + '', + '{/* comment */}', + ';' + ].join('\n'), + options: [{when: 'never', children: true}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: [ + '', + '{/* comment */}', + ';' + ].join('\n'), + output: [ + '', + '{ /* comment */ }', + ';' + ].join('\n'), + options: [{when: 'always', children: true}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] }, { code: [ '', @@ -2180,5 +2214,47 @@ ruleTester.run('jsx-curly-spacing', rule, { errors: [{ message: 'There should be no newline after \'{\'' }] + }, { + code: [ + '{ /* comment */', + 'bar', + '} {', + 'baz', + '/* comment */ };' + ].join('\n'), + output: [ + '{/* comment */', + 'bar', + '} {', + 'baz', + '/* comment */};' + ].join('\n'), + options: [{when: 'never', children: true}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: [ + '{/* comment */', + 'bar', + '} {', + 'baz', + '/* comment */};' + ].join('\n'), + output: [ + '{ /* comment */', + 'bar', + '} {', + 'baz', + '/* comment */ };' + ].join('\n'), + options: [{when: 'always', children: true}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] }] }); From a2764d9e235a4aab89116ba5899a29b78c45aff4 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 19 Dec 2017 16:40:10 -0800 Subject: [PATCH 2/3] [Tests] number comments to more easily locate failures --- tests/lib/rules/jsx-curly-spacing.js | 102 +++++++++++++-------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/tests/lib/rules/jsx-curly-spacing.js b/tests/lib/rules/jsx-curly-spacing.js index eaff650519..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,12 +2151,12 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: [ '', - '{ /* comment */ }', + '{ /* comment 27 */ }', ';' ].join('\n'), output: [ '', - '{/* comment */}', + '{/* comment 27 */}', ';' ].join('\n'), options: [{when: 'never', children: true}], @@ -2168,12 +2168,12 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: [ '', - '{/* comment */}', + '{/* comment 28 */}', ';' ].join('\n'), output: [ '', - '{ /* comment */ }', + '{ /* comment 28 */ }', ';' ].join('\n'), options: [{when: 'always', children: true}], @@ -2185,13 +2185,13 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: [ '', - '{/*comment*/', + '{/*comment29*/', '}', '' ].join('\n'), output: [ '', - '{/*comment*/}', + '{/*comment29*/}', '' ].join('\n'), options: [{children: {when: 'never', allowMultiline: false}}], @@ -2202,12 +2202,12 @@ ruleTester.run('jsx-curly-spacing', rule, { code: [ '', '{', - '/*comment*/}', + '/*comment30*/}', '' ].join('\n'), output: [ '', - '{/*comment*/}', + '{/*comment30*/}', '' ].join('\n'), options: [{children: {when: 'never', allowMultiline: false}}], @@ -2216,18 +2216,18 @@ ruleTester.run('jsx-curly-spacing', rule, { }] }, { code: [ - '{ /* comment */', + '{ /* comment 31 */', 'bar', '} {', 'baz', - '/* comment */ };' + '/* comment 32 */ };' ].join('\n'), output: [ - '{/* comment */', + '{/* comment 31 */', 'bar', '} {', 'baz', - '/* comment */};' + '/* comment 32 */};' ].join('\n'), options: [{when: 'never', children: true}], errors: [{ @@ -2237,18 +2237,18 @@ ruleTester.run('jsx-curly-spacing', rule, { }] }, { code: [ - '{/* comment */', + '{/* comment 33 */', 'bar', '} {', 'baz', - '/* comment */};' + '/* comment 33 */};' ].join('\n'), output: [ - '{ /* comment */', + '{ /* comment 33 */', 'bar', '} {', 'baz', - '/* comment */ };' + '/* comment 33 */ };' ].join('\n'), options: [{when: 'always', children: true}], errors: [{ From e4de3600c281c87bcb18d62c10ce279374ddcde4 Mon Sep 17 00:00:00 2001 From: Andreas Opferkuch Date: Fri, 16 Feb 2018 01:24:43 +0100 Subject: [PATCH 3/3] [Fix] jsx-curly-spacing problem in new tests, fixes #1414 Before this, spaces before/after braces when there should be none resulted in two overlapping fixes for the full brace content. And so they required two passes. But the tester does only one pass, so only one white space was fixed and the test failed. By including comments in the range calculation for the fix, the range is now properly limited, making it possible to have start and end fixes in one pass. --- lib/rules/jsx-curly-spacing.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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'); } });