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

Fix jsx-curly-spacing: spacing around spread operator #610

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions lib/rules/jsx-curly-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,14 @@ module.exports = function(context) {
/**
* Determines if spacing in curly braces is valid.
* @param {ASTNode} node The AST node to check.
* @param {Token} first The first token to check (should be the opening brace)
* @param {Token} second The second token to check (should be first after the opening brace)
* @param {Token} penultimate The penultimate token to check (should be last before closing brace)
* @param {Token} last The last token to check (should be closing brace)
* @returns {void}
*/
function validateBraceSpacing(node, first, second, penultimate, last) {
function validateBraceSpacing(node) {
var first = context.getFirstToken(node);
var last = sourceCode.getLastToken(node);
var second = context.getTokenAfter(first);
var penultimate = sourceCode.getTokenBefore(last);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just put these declarations inside the function to reduce copy pasting


if (spaced) {
if (!sourceCode.isSpaceBetweenTokens(first, second)) {
reportRequiredBeginningSpace(node, first);
Expand Down Expand Up @@ -181,14 +182,8 @@ module.exports = function(context) {
// --------------------------------------------------------------------------

return {
JSXExpressionContainer: function(node) {
var first = context.getFirstToken(node);
var last = sourceCode.getLastToken(node);
var second = context.getTokenAfter(first);
var penultimate = sourceCode.getTokenBefore(last);

validateBraceSpacing(node, first, second, penultimate, last);
}
JSXExpressionContainer: validateBraceSpacing,
JSXSpreadAttribute: validateBraceSpacing
};
};

Expand Down
319 changes: 317 additions & 2 deletions tests/lib/rules/jsx-curly-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,87 @@ ruleTester.run('jsx-curly-spacing', rule, {
}, {
code: '<App foo={bar/* comment */} />;',
options: ['never'],
parserOptions: parserOptions,
parser: 'babel-eslint'
parserOptions: parserOptions
}, {
code: '<App {...bar} />;',
parserOptions: parserOptions
}, {
code: '<App {...bar} />;',
options: ['never'],
parserOptions: parserOptions
}, {
code: '<App { ...bar } />;',
options: ['always'],
parserOptions: parserOptions
}, {
code: '<App { ...bar } />;',
options: ['always', {allowMultiline: false}],
parserOptions: parserOptions
}, {
code: [
'<App {',
'...bar',
'} />;'
].join('\n'),
options: ['always'],
parserOptions: parserOptions
}, {
code: [
'<App {',
'...bar',
'} />;'
].join('\n'),
options: ['always'],
parserOptions: parserOptions
}, {
code: [
'<App {',
'...bar',
'} />;'
].join('\n'),
options: ['never'],
parserOptions: parserOptions
}, {
code: '<App {...bar/* comment */} />;',
options: ['never'],
parserOptions: parserOptions
}, {
code: '<App foo={bar} {...baz} />;',
parserOptions: parserOptions
}, {
code: '<App foo={bar} {...baz} />;',
options: ['never'],
parserOptions: parserOptions
}, {
code: '<App foo={ bar } { ...baz } />;',
options: ['always'],
parserOptions: parserOptions
}, {
code: '<App foo={ bar } { ...baz } />;',
options: ['always', {allowMultiline: false}],
parserOptions: parserOptions
}, {
code: '<App foo={{ bar:baz }} {...baz} />;',
options: ['never'],
parserOptions: parserOptions
}, {
code: '<App foo={ {bar:baz} } { ...baz } />;',
options: ['always'],
parserOptions: parserOptions
}, {
code: [
'<App foo={',
'bar',
'} {',
'...bar',
'}/>;'
].join('\n'),
options: ['always'],
parserOptions: parserOptions
}, {
code: '<App foo={bar/* comment */} {...baz/* comment */} />;',
options: ['never'],
parserOptions: parserOptions
}],

invalid: [{
Expand Down Expand Up @@ -185,5 +264,241 @@ ruleTester.run('jsx-curly-spacing', rule, {
message: 'There should be no newline before \'}\''
}],
parserOptions: parserOptions
}, {
code: '<App { ...bar } />;',
output: '<App {...bar} />;',
options: ['never'],
errors: [{
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}],
parserOptions: parserOptions
}, {
code: '<App { ...bar } />;',
output: '<App {...bar} />;',
options: ['never', {allowMultiline: false}],
errors: [{
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}],
parserOptions: parserOptions
}, {
code: '<App {...bar} />;',
output: '<App { ...bar } />;',
options: ['always'],
errors: [{
message: 'A space is required after \'{\''
}, {
message: 'A space is required before \'}\''
}],
parserOptions: parserOptions
}, {
code: '<App {...bar} />;',
output: '<App { ...bar } />;',
options: ['always', {allowMultiline: false}],
errors: [{
message: 'A space is required after \'{\''
}, {
message: 'A space is required before \'}\''
}],
parserOptions: parserOptions
}, {
code: '<App { ...bar} />;',
output: '<App { ...bar } />;',
options: ['always'],
errors: [{
message: 'A space is required before \'}\''
}],
parserOptions: parserOptions
}, {
code: '<App {...bar } />;',
output: '<App { ...bar } />;',
options: ['always'],
errors: [{
message: 'A space is required after \'{\''
}],
parserOptions: parserOptions
}, {
code: '<App { ...bar} />;',
output: '<App {...bar} />;',
options: ['never'],
errors: [{
message: 'There should be no space after \'{\''
}],
parserOptions: parserOptions
}, {
code: '<App {...bar } />;',
output: '<App {...bar} />;',
options: ['never'],
errors: [{
message: 'There should be no space before \'}\''
}],
parserOptions: parserOptions
}, {
code: [
'<App {',
'...bar',
'} />;'
].join('\n'),
output: '<App {...bar} />;',
options: ['never', {allowMultiline: false}],
errors: [{
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}],
parserOptions: parserOptions
}, {
code: [
'<App {',
'...bar',
'} />;'
].join('\n'),
output: '<App { ...bar } />;',
options: ['always', {allowMultiline: false}],
errors: [{
message: 'There should be no newline after \'{\''
}, {
message: 'There should be no newline before \'}\''
}],
parserOptions: parserOptions
}, {
code: '<App foo={ bar } { ...baz } />;',
output: '<App foo={bar} {...baz} />;',
options: ['never'],
errors: [{
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}, {
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}],
parserOptions: parserOptions
}, {
code: '<App foo={ bar } { ...baz } />;',
output: '<App foo={bar} {...baz} />;',
options: ['never', {allowMultiline: false}],
errors: [{
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}, {
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}],
parserOptions: parserOptions
}, {
code: '<App foo={bar} {...baz} />;',
output: '<App foo={ bar } { ...baz } />;',
options: ['always'],
errors: [{
message: 'A space is required after \'{\''
}, {
message: 'A space is required before \'}\''
}, {
message: 'A space is required after \'{\''
}, {
message: 'A space is required before \'}\''
}],
parserOptions: parserOptions
}, {
code: '<App foo={bar} {...baz} />;',
output: '<App foo={ bar } { ...baz } />;',
options: ['always', {allowMultiline: false}],
errors: [{
message: 'A space is required after \'{\''
}, {
message: 'A space is required before \'}\''
}, {
message: 'A space is required after \'{\''
}, {
message: 'A space is required before \'}\''
}],
parserOptions: parserOptions
}, {
code: '<App foo={ bar} { ...baz} />;',
output: '<App foo={ bar } { ...baz } />;',
options: ['always'],
errors: [{
message: 'A space is required before \'}\''
}, {
message: 'A space is required before \'}\''
}],
parserOptions: parserOptions
}, {
code: '<App foo={bar } {...baz } />;',
output: '<App foo={ bar } { ...baz } />;',
options: ['always'],
errors: [{
message: 'A space is required after \'{\''
}, {
message: 'A space is required after \'{\''
}],
parserOptions: parserOptions
}, {
code: '<App foo={ bar} { ...baz} />;',
output: '<App foo={bar} {...baz} />;',
options: ['never'],
errors: [{
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space after \'{\''
}],
parserOptions: parserOptions
}, {
code: '<App foo={bar } {...baz } />;',
output: '<App foo={bar} {...baz} />;',
options: ['never'],
errors: [{
message: 'There should be no space before \'}\''
}, {
message: 'There should be no space before \'}\''
}],
parserOptions: parserOptions
}, {
code: [
'<App foo={',
'bar',
'} {',
'...baz',
'} />;'
].join('\n'),
output: '<App foo={bar} {...baz} />;',
options: ['never', {allowMultiline: false}],
errors: [{
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}, {
message: 'There should be no space after \'{\''
}, {
message: 'There should be no space before \'}\''
}],
parserOptions: parserOptions
}, {
code: [
'<App foo={',
'bar',
'} {',
'...baz',
'} />;'
].join('\n'),
output: '<App foo={ bar } { ...baz } />;',
options: ['always', {allowMultiline: false}],
errors: [{
message: 'There should be no newline after \'{\''
}, {
message: 'There should be no newline before \'}\''
}, {
message: 'There should be no newline after \'{\''
}, {
message: 'There should be no newline before \'}\''
}],
parserOptions: parserOptions
}]
});