Skip to content

Commit

Permalink
Merge pull request #407 from ewendel/jsx-curly-spacing-autofix
Browse files Browse the repository at this point in the history
Add auto fix for jsx-curly-spacing
  • Loading branch information
yannickcr committed Jan 23, 2016
2 parents 402709f + e9bcfc1 commit 56849fa
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
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

0 comments on commit 56849fa

Please sign in to comment.