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

Add auto fix for jsx-curly-spacing #407

Merged
merged 1 commit into from
Jan 23, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 59 additions & 13 deletions lib/rules/jsx-curly-spacing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/**
* @fileoverview Enforce or disallow spaces inside of curly braces in JSX attributes.
* @author Jamund Ferguson, Brandyn Bennett, Michael Ficarra, Vignesh Anand, Jamund Ferguson, Yannick Croissant
* @author Jamund Ferguson
* @author Brandyn Bennett
* @author Michael Ficarra
* @author Vignesh Anand
* @author Jamund Ferguson
* @author Yannick Croissant
* @author Erik Wendel
*/
'use strict';

Expand Down Expand Up @@ -43,8 +49,15 @@ module.exports = function(context) {
* @returns {void}
*/
function reportNoBeginningNewline(node, token) {
context.report(node, token.loc.start,
'There should be no newline after \'' + token.value + '\'');
context.report({
node: node,
loc: token.loc.start,
message: 'There should be no newline after \'' + token.value + '\'',
fix: function(fixer) {
var nextToken = context.getSourceCode().getTokenAfter(token);
return fixer.replaceTextRange([token.range[1], nextToken.range[0]], spaced ? ' ' : '');
}
});
}

/**
Expand All @@ -54,8 +67,15 @@ module.exports = function(context) {
* @returns {void}
*/
function reportNoEndingNewline(node, token) {
context.report(node, token.loc.start,
'There should be no newline before \'' + token.value + '\'');
context.report({
node: node,
loc: token.loc.start,
message: 'There should be no newline before \'' + token.value + '\'',
fix: function(fixer) {
var previousToken = context.getSourceCode().getTokenBefore(token);
return fixer.replaceTextRange([previousToken.range[1], token.range[0]], spaced ? ' ' : '');
}
});
}

/**
Expand All @@ -65,8 +85,15 @@ module.exports = function(context) {
* @returns {void}
*/
function reportNoBeginningSpace(node, token) {
context.report(node, token.loc.start,
'There should be no space after \'' + token.value + '\'');
context.report({
node: node,
loc: token.loc.start,
message: 'There should be no space after \'' + token.value + '\'',
fix: function(fixer) {
var nextToken = context.getSourceCode().getTokenAfter(token);
return fixer.removeRange([token.range[1], nextToken.range[0]]);
}
});
}

/**
Expand All @@ -76,8 +103,15 @@ module.exports = function(context) {
* @returns {void}
*/
function reportNoEndingSpace(node, token) {
context.report(node, token.loc.start,
'There should be no space before \'' + token.value + '\'');
context.report({
node: node,
loc: token.loc.start,
message: 'There should be no space before \'' + token.value + '\'',
fix: function(fixer) {
var previousToken = context.getSourceCode().getTokenBefore(token);
return fixer.removeRange([previousToken.range[1], token.range[0]]);
}
});
}

/**
Expand All @@ -87,8 +121,14 @@ module.exports = function(context) {
* @returns {void}
*/
function reportRequiredBeginningSpace(node, token) {
context.report(node, token.loc.start,
'A space is required after \'' + token.value + '\'');
context.report({
node: node,
loc: token.loc.start,
message: 'A space is required after \'' + token.value + '\'',
fix: function(fixer) {
return fixer.insertTextAfter(token, ' ');
}
});
}

/**
Expand All @@ -98,8 +138,14 @@ module.exports = function(context) {
* @returns {void}
*/
function reportRequiredEndingSpace(node, token) {
context.report(node, token.loc.start,
'A space is required before \'' + token.value + '\'');
context.report({
node: node,
loc: token.loc.start,
message: 'A space is required before \'' + token.value + '\'',
fix: function(fixer) {
return fixer.insertTextBefore(token, ' ');
}
});
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/jsx-curly-spacing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @fileoverview Enforce or disallow spaces inside of curly braces in JSX attributes.
* @author Yannick Croissant
* @author Erik Wendel
*/
'use strict';

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

invalid: [{
code: '<App foo={ bar } />;',
output: '<App foo={bar} />;',
options: ['never'],
errors: [{
message: 'There should be no space after \'{\''
Expand All @@ -90,6 +92,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
parserOptions: parserOptions
}, {
code: '<App foo={ bar } />;',
output: '<App foo={bar} />;',
options: ['never', {allowMultiline: false}],
errors: [{
message: 'There should be no space after \'{\''
Expand All @@ -99,6 +102,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
parserOptions: parserOptions
}, {
code: '<App foo={bar} />;',
output: '<App foo={ bar } />;',
options: ['always'],
errors: [{
message: 'A space is required after \'{\''
Expand All @@ -108,6 +112,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
parserOptions: parserOptions
}, {
code: '<App foo={bar} />;',
output: '<App foo={ bar } />;',
options: ['always', {allowMultiline: false}],
errors: [{
message: 'A space is required after \'{\''
Expand All @@ -117,27 +122,31 @@ ruleTester.run('jsx-curly-spacing', rule, {
parserOptions: parserOptions
}, {
code: '<App foo={ bar} />;',
output: '<App foo={ bar } />;',
options: ['always'],
errors: [{
message: 'A space is required before \'}\''
}],
parserOptions: parserOptions
}, {
code: '<App foo={bar } />;',
output: '<App foo={ bar } />;',
options: ['always'],
errors: [{
message: 'A space is required after \'{\''
}],
parserOptions: parserOptions
}, {
code: '<App foo={ bar} />;',
output: '<App foo={bar} />;',
options: ['never'],
errors: [{
message: 'There should be no space after \'{\''
}],
parserOptions: parserOptions
}, {
code: '<App foo={bar } />;',
output: '<App foo={bar} />;',
options: ['never'],
errors: [{
message: 'There should be no space before \'}\''
Expand All @@ -149,6 +158,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
'bar',
'} />;'
].join('\n'),
output: '<App foo={bar} />;',
options: ['never', {allowMultiline: false}],
errors: [{
message: 'There should be no space after \'{\''
Expand All @@ -162,6 +172,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
'bar',
'} />;'
].join('\n'),
output: '<App foo={ bar } />;',
options: ['always', {allowMultiline: false}],
errors: [{
message: 'There should be no newline after \'{\''
Expand Down