Skip to content

Commit

Permalink
Update: auto-fix for comma-style (fixes #6941)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyandeeps committed Aug 23, 2016
1 parent 3a1763c commit 94ed804
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/rules/comma-style.md
@@ -1,5 +1,7 @@
# Comma style (comma-style)

(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule.

The Comma Style rule enforces styles for comma-separated lists. There are two comma styles primarily used in JavaScript:

* The standard style, in which commas are placed at the end of the current line
Expand Down
49 changes: 39 additions & 10 deletions lib/rules/comma-style.js
Expand Up @@ -18,7 +18,7 @@ module.exports = {
category: "Stylistic Issues",
recommended: false
},

fixable: "code",
schema: [
{
enum: ["first", "last"]
Expand Down Expand Up @@ -82,21 +82,50 @@ module.exports = {
!astUtils.isTokenOnSameLine(previousItemToken, commaToken)) {

// lone comma
context.report(reportItem, {
line: commaToken.loc.end.line,
column: commaToken.loc.start.column
}, "Bad line breaking before and after ','.");
context.report({
node: reportItem,
loc: {
line: commaToken.loc.end.line,
column: commaToken.loc.start.column
},
message: "Bad line breaking before and after ','.",
fix(fixer) {
return fixer.removeRange([
previousItemToken.range[1],
commaToken.range[0]
]);
}
});

} else if (style === "first" && !astUtils.isTokenOnSameLine(commaToken, currentItemToken)) {

context.report(reportItem, "',' should be placed first.");
context.report({
node: reportItem,
message: "',' should be placed first.",
fix(fixer) {
return fixer.replaceTextRange([
commaToken.range[0],
currentItemToken.range[0]
], "\n,");
}
});

} else if (style === "last" && astUtils.isTokenOnSameLine(commaToken, currentItemToken)) {

context.report(reportItem, {
line: commaToken.loc.end.line,
column: commaToken.loc.end.column
}, "',' should be placed last.");
context.report({
node: reportItem,
loc: {
line: commaToken.loc.end.line,
column: commaToken.loc.end.column
},
message: "',' should be placed last.",
fix(fixer) {
return fixer.replaceTextRange([
previousItemToken.range[1],
commaToken.range[1]
], ",\n");
}
});
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/comma-style.js
Expand Up @@ -88,48 +88,55 @@ ruleTester.run("comma-style", rule, {
invalid: [
{
code: "var foo = 1\n,\nbar = 2;",
output: "var foo = 1,\nbar = 2;",
errors: [{
message: BAD_LN_BRK_MSG,
type: "VariableDeclarator"
}]
},
{
code: "var foo = 1\n,bar = 2;",
output: "var foo = 1,\nbar = 2;",
errors: [{
message: LAST_MSG,
type: "VariableDeclarator"
}]
},
{
code: "f([1,2\n,3]);",
output: "f([1,2,\n3]);",
errors: [{
message: LAST_MSG,
type: "Literal"
}]
},
{
code: "f([1,2\n,]);",
output: "f([1,2,\n]);",
errors: [{
message: LAST_MSG,
type: "Punctuator"
}]
},
{
code: "f([,2\n,3]);",
output: "f([,2,\n3]);",
errors: [{
message: LAST_MSG,
type: "Literal"
}]
},
{
code: "var foo = ['apples'\n, 'oranges'];",
output: "var foo = ['apples',\n 'oranges'];",
errors: [{
message: LAST_MSG,
type: "Literal"
}]
},
{
code: "var foo = 1,\nbar = 2;",
output: "var foo = 1\n,bar = 2;",
options: ["first"],
errors: [{
message: FIRST_MSG,
Expand All @@ -138,6 +145,7 @@ ruleTester.run("comma-style", rule, {
},
{
code: "f([1,\n2,3]);",
output: "f([1\n,2,3]);",
options: ["first"],
errors: [{
message: FIRST_MSG,
Expand All @@ -146,6 +154,7 @@ ruleTester.run("comma-style", rule, {
},
{
code: "var foo = ['apples', \n 'oranges'];",
output: "var foo = ['apples'\n,'oranges'];",
options: ["first"],
errors: [{
message: FIRST_MSG,
Expand All @@ -154,6 +163,7 @@ ruleTester.run("comma-style", rule, {
},
{
code: "var foo = {'a': 1, \n 'b': 2\n ,'c': 3};",
output: "var foo = {'a': 1\n,'b': 2\n ,'c': 3};",
options: ["first"],
errors: [{
message: FIRST_MSG,
Expand All @@ -162,6 +172,7 @@ ruleTester.run("comma-style", rule, {
},
{
code: "var foo = {'a': 1, \n 'b': 2\n ,'c': 3};",
output: "var foo = {'a': 1\n,'b': 2\n ,'c': 3};",
options: ["first"],
errors: [{
message: FIRST_MSG,
Expand All @@ -170,6 +181,7 @@ ruleTester.run("comma-style", rule, {
},
{
code: "var a = 'a',\no = 'o',\narr = [1,\n2];",
output: "var a = 'a',\no = 'o',\narr = [1\n,2];",
options: ["first", {exceptions: {VariableDeclaration: true}}],
errors: [{
message: FIRST_MSG,
Expand All @@ -178,6 +190,7 @@ ruleTester.run("comma-style", rule, {
},
{
code: "var a = 'a',\nobj = {a: 'a',\nb: 'b'};",
output: "var a = 'a',\nobj = {a: 'a'\n,b: 'b'};",
options: ["first", {exceptions: {VariableDeclaration: true}}],
errors: [{
message: FIRST_MSG,
Expand All @@ -186,6 +199,7 @@ ruleTester.run("comma-style", rule, {
},
{
code: "var a = 'a',\nobj = {a: 'a',\nb: 'b'};",
output: "var a = 'a'\n,obj = {a: 'a',\nb: 'b'};",
options: ["first", {exceptions: {ObjectExpression: true}}],
errors: [{
message: FIRST_MSG,
Expand All @@ -194,6 +208,7 @@ ruleTester.run("comma-style", rule, {
},
{
code: "var a = 'a',\narr = [1,\n2];",
output: "var a = 'a'\n,arr = [1,\n2];",
options: ["first", {exceptions: {ArrayExpression: true}}],
errors: [{
message: FIRST_MSG,
Expand All @@ -202,6 +217,7 @@ ruleTester.run("comma-style", rule, {
},
{
code: "var ar =[1,\n{a: 'a',\nb: 'b'}];",
output: "var ar =[1,\n{a: 'a'\n,b: 'b'}];",
options: ["first", {exceptions: {ArrayExpression: true}}],
errors: [{
message: FIRST_MSG,
Expand All @@ -210,6 +226,7 @@ ruleTester.run("comma-style", rule, {
},
{
code: "var ar =[1,\n{a: 'a',\nb: 'b'}];",
output: "var ar =[1\n,{a: 'a',\nb: 'b'}];",
options: ["first", {exceptions: {ObjectExpression: true}}],
errors: [{
message: FIRST_MSG,
Expand All @@ -218,6 +235,7 @@ ruleTester.run("comma-style", rule, {
},
{
code: "var ar ={fst:1,\nsnd: [1,\n2]};",
output: "var ar ={fst:1,\nsnd: [1\n,2]};",
options: ["first", {exceptions: {ObjectExpression: true}}],
errors: [{
message: FIRST_MSG,
Expand All @@ -226,6 +244,7 @@ ruleTester.run("comma-style", rule, {
},
{
code: "var ar ={fst:1,\nsnd: [1,\n2]};",
output: "var ar ={fst:1\n,snd: [1,\n2]};",
options: ["first", {exceptions: {ArrayExpression: true}}],
errors: [{
message: FIRST_MSG,
Expand All @@ -234,6 +253,7 @@ ruleTester.run("comma-style", rule, {
},
{
code: "var foo = [\n(bar\n)\n,\nbaz\n];",
output: "var foo = [\n(bar\n),\nbaz\n];",
errors: [{
message: BAD_LN_BRK_MSG,
type: "Identifier"
Expand Down

0 comments on commit 94ed804

Please sign in to comment.