Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for jsx-curly-spacing that deal with comments in children. #1414

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/rules/jsx-curly-spacing.js
Expand Up @@ -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');
}
});
Expand All @@ -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');
}
});
Expand Down
154 changes: 115 additions & 39 deletions tests/lib/rules/jsx-curly-spacing.js
Expand Up @@ -59,9 +59,9 @@ ruleTester.run('jsx-curly-spacing', rule, {
'</App>;'
].join('\n')
}, {
code: '<App>{ foo /* comment */ }</App>'
code: '<App>{ foo /* comment 1 */ }</App>'
}, {
code: '<App>{ /* comment */ foo }</App>'
code: '<App>{ /* comment 1 */ foo }</App>'
}, {
code: '<App foo={bar} />;',
options: [{attributes: true}]
Expand Down Expand Up @@ -264,7 +264,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
].join('\n'),
options: [{attributes: {when: 'never', allowMultiline: true}}]
}, {
code: '<App foo={bar/* comment */} />;',
code: '<App foo={bar/* comment 2 */} />;',
options: [{attributes: {when: 'never'}}]
}, {
code: '<App foo={ bar } />;',
Expand Down Expand Up @@ -345,11 +345,11 @@ ruleTester.run('jsx-curly-spacing', rule, {
options: [{children: {when: 'never', allowMultiline: true}}]
}, {
code: [
'<App>{/* comment */}</App>;'
'<App>{/* comment 3 */}</App>;'
].join('\n'),
options: [{children: {when: 'never'}}]
}, {
code: '<App>{bar/* comment */}</App>;',
code: '<App>{bar/* comment 4 */}</App>;',
options: [{children: {when: 'never'}}]
}, {
code: '<App>{ bar }</App>;',
Expand Down Expand Up @@ -411,7 +411,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
].join('\n'),
options: [{attributes: {when: 'never', allowMultiline: true}}]
}, {
code: '<App {...bar/* comment */} />;',
code: '<App {...bar/* comment 5 */} />;',
options: [{attributes: {when: 'never'}}]
}, {
code: '<App foo={bar} {...baz} />;'
Expand Down Expand Up @@ -440,13 +440,13 @@ ruleTester.run('jsx-curly-spacing', rule, {
].join('\n'),
options: [{attributes: {when: 'always'}}]
}, {
code: '<App foo={bar/* comment */} {...baz/* comment */} />;',
code: '<App foo={bar/* comment 6 */} {...baz/* comment 7 */} />;',
options: [{attributes: {when: 'never'}}]
}, {
code: '<App foo={3} bar={ {a: 2} } />',
options: [{attributes: {when: 'never', spacing: {objectLiterals: 'always'}}}]
}, {
code: '<App>{bar/* comment */}</App>;',
code: '<App>{bar/* comment 8 */}</App>;',
options: [{children: {when: 'never'}}]
}, {
code: '<App>{bar} {baz}</App>;'
Expand Down Expand Up @@ -475,7 +475,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
].join('\n'),
options: [{children: {when: 'always'}}]
}, {
code: '<App>{bar/* comment */} {baz/* comment */}</App>;',
code: '<App>{bar/* comment 9 */} {baz/* comment 10 */}</App>;',
options: [{children: {when: 'never'}}]
}, {
code: '<App>{3} { {a: 2} }</App>',
Expand Down Expand Up @@ -572,11 +572,11 @@ ruleTester.run('jsx-curly-spacing', rule, {
options: ['never']
}, {
code: [
'<App>{/* comment */}</App>;'
'<App>{/* comment 11 */}</App>;'
].join('\n'),
options: ['never']
}, {
code: '<App foo={bar/* comment */} />;',
code: '<App foo={bar/* comment 12 */} />;',
options: ['never']
}, {
code: '<App foo={ bar } />;',
Expand Down Expand Up @@ -636,7 +636,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
].join('\n'),
options: ['never']
}, {
code: '<App {...bar/* comment */} />;',
code: '<App {...bar/* comment 13 */} />;',
options: ['never']
}, {
code: '<App foo={bar} {...baz} />;',
Expand All @@ -663,7 +663,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
].join('\n'),
options: ['always']
}, {
code: '<App foo={bar/* comment */} {...baz/* comment */} />;',
code: '<App foo={bar/* comment 14 */} {...baz/* comment 15 */} />;',
options: ['never']
}, {
code: '<App foo={3} bar={ {a: 2} } />',
Expand Down Expand Up @@ -1514,33 +1514,33 @@ ruleTester.run('jsx-curly-spacing', rule, {
message: 'A space is required before \'}\''
}]
}, {
code: '<App foo={ foo /* comment */ } />',
output: '<App foo={foo /* comment */} />',
code: '<App foo={ foo /* comment 16 */ } />',
output: '<App foo={foo /* comment 16 */} />',
errors: [{
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}]
}, {
code: '<App foo={foo /* comment */} />',
output: '<App foo={ foo /* comment */ } />',
code: '<App foo={foo /* comment 17 */} />',
output: '<App foo={ foo /* comment 17 */ } />',
options: [{attributes: {when: 'always'}}],
errors: [{
message: 'A space is required after \'{\''
}, {
message: 'A space is required before \'}\''
}]
}, {
code: '<App foo={ /* comment */ foo } />',
output: '<App foo={/* comment */ foo} />',
code: '<App foo={ /* comment 18 */ foo } />',
output: '<App foo={/* comment 18 */ foo} />',
errors: [{
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}]
}, {
code: '<App foo={/* comment */ foo} />',
output: '<App foo={ /* comment */ foo } />',
code: '<App foo={/* comment 19 */ foo} />',
output: '<App foo={ /* comment 19 */ foo } />',
options: [{attributes: {when: 'always'}}],
errors: [{
message: 'A space is required after \'{\''
Expand Down Expand Up @@ -1687,17 +1687,17 @@ ruleTester.run('jsx-curly-spacing', rule, {
message: 'A space is required before \'}\''
}]
}, {
code: '<App>{foo /* comment */}</App>',
output: '<App>{ foo /* comment */ }</App>',
code: '<App>{foo /* comment 20 */}</App>',
output: '<App>{ foo /* comment 20 */ }</App>',
options: [{children: {when: 'always'}}],
errors: [{
message: 'A space is required after \'{\''
}, {
message: 'A space is required before \'}\''
}]
}, {
code: '<App>{/* comment */ foo}</App>',
output: '<App>{ /* comment */ foo }</App>',
code: '<App>{/* comment 21 */ foo}</App>',
output: '<App>{ /* comment 21 */ foo }</App>',
options: [{children: {when: 'always'}}],
errors: [{
message: 'A space is required after \'{\''
Expand Down Expand Up @@ -2108,40 +2108,40 @@ ruleTester.run('jsx-curly-spacing', rule, {
message: 'A space is required before \'}\''
}]
}, {
code: '<App foo={foo /* comment */} />',
output: '<App foo={ foo /* comment */ } />',
code: '<App foo={foo /* comment 22 */} />',
output: '<App foo={ foo /* comment 22 */ } />',
options: ['always'],
errors: [{
message: 'A space is required after \'{\''
}, {
message: 'A space is required before \'}\''
}]
}, {
code: '<App foo={/* comment */ foo} />',
output: '<App foo={ /* comment */ foo } />',
code: '<App foo={/* comment 23 */ foo} />',
output: '<App foo={ /* comment 23 */ foo } />',
options: ['always'],
errors: [{
message: 'A space is required after \'{\''
}, {
message: 'A space is required before \'}\''
}]
}, {
code: '<App>{/*comment*/ }</App>',
output: '<App>{/*comment*/}</App>',
code: '<App>{/*comment24*/ }</App>',
output: '<App>{/*comment24*/}</App>',
options: [{children: {when: 'never'}}],
errors: [{
message: 'There should be no space before \'}\''
}]
}, {
code: '<App>{ /*comment*/}</App>',
output: '<App>{/*comment*/}</App>',
code: '<App>{ /*comment25*/}</App>',
output: '<App>{/*comment25*/}</App>',
options: [{children: {when: 'never'}}],
errors: [{
message: 'There should be no space after \'{\''
}]
}, {
code: '<App>{/*comment*/}</App>',
output: '<App>{ /*comment*/ }</App>',
code: '<App>{/*comment26*/}</App>',
output: '<App>{ /*comment26*/ }</App>',
options: [{children: {when: 'always'}}],
errors: [{
message: 'A space is required after \'{\''
Expand All @@ -2151,13 +2151,47 @@ ruleTester.run('jsx-curly-spacing', rule, {
}, {
code: [
'<App>',
'{/*comment*/',
'{ /* comment 27 */ }',
'</App>;'
].join('\n'),
output: [
'<App>',
'{/* comment 27 */}',
'</App>;'
].join('\n'),
options: [{when: 'never', children: true}],
errors: [{
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}]
}, {
code: [
'<App>',
'{/* comment 28 */}',
'</App>;'
].join('\n'),
output: [
'<App>',
'{ /* comment 28 */ }',
'</App>;'
].join('\n'),
options: [{when: 'always', children: true}],
errors: [{
message: 'A space is required after \'{\''
}, {
message: 'A space is required before \'}\''
}]
}, {
code: [
'<App>',
'{/*comment29*/',
'}',
'</App>'
].join('\n'),
output: [
'<App>',
'{/*comment*/}',
'{/*comment29*/}',
'</App>'
].join('\n'),
options: [{children: {when: 'never', allowMultiline: false}}],
Expand All @@ -2168,17 +2202,59 @@ ruleTester.run('jsx-curly-spacing', rule, {
code: [
'<App>',
'{',
'/*comment*/}',
'/*comment30*/}',
'</App>'
].join('\n'),
output: [
'<App>',
'{/*comment*/}',
'{/*comment30*/}',
'</App>'
].join('\n'),
options: [{children: {when: 'never', allowMultiline: false}}],
errors: [{
message: 'There should be no newline after \'{\''
}]
}, {
code: [
'<App>{ /* comment 31 */',
'bar',
'} {',
'baz',
'/* comment 32 */ }</App>;'
].join('\n'),
output: [
'<App>{/* comment 31 */',
'bar',
'} {',
'baz',
'/* comment 32 */}</App>;'
].join('\n'),
options: [{when: 'never', children: true}],
errors: [{
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}]
}, {
code: [
'<App>{/* comment 33 */',
'bar',
'} {',
'baz',
'/* comment 33 */}</App>;'
].join('\n'),
output: [
'<App>{ /* comment 33 */',
'bar',
'} {',
'baz',
'/* comment 33 */ }</App>;'
].join('\n'),
options: [{when: 'always', children: true}],
errors: [{
message: 'A space is required after \'{\''
}, {
message: 'A space is required before \'}\''
}]
}]
});