From d87bcb4d6d5ae793a80f4337b06c275a1551d44c Mon Sep 17 00:00:00 2001 From: Gyandeep Singh Date: Mon, 22 Aug 2016 10:12:45 -0500 Subject: [PATCH] Update: auto-fix for (fixes #6941) --- docs/rules/comma-style.md | 2 ++ lib/rules/comma-style.js | 49 +++++++++++++++++++++++++++------- tests/lib/ignored-paths.js | 10 +++---- tests/lib/rules/comma-style.js | 20 ++++++++++++++ 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/docs/rules/comma-style.md b/docs/rules/comma-style.md index 16bf03900b5f..37ffabdbea36 100644 --- a/docs/rules/comma-style.md +++ b/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 diff --git a/lib/rules/comma-style.js b/lib/rules/comma-style.js index 1ce226b39f81..c13127e3d078 100644 --- a/lib/rules/comma-style.js +++ b/lib/rules/comma-style.js @@ -18,7 +18,7 @@ module.exports = { category: "Stylistic Issues", recommended: false }, - + fixable: true, schema: [ { enum: ["first", "last"] @@ -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"); + } + }); } } diff --git a/tests/lib/ignored-paths.js b/tests/lib/ignored-paths.js index de642324d793..a71560d22933 100644 --- a/tests/lib/ignored-paths.js +++ b/tests/lib/ignored-paths.js @@ -544,7 +544,7 @@ describe("IgnoredPaths", function() { it("should ignore default folders when there is no eslintignore file", function() { const cwd = getFixturePath("no-ignore-file"); - const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: cwd }); + const ignoredPaths = new IgnoredPaths({ ignore: true, cwd }); const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker(); const resolve = createResolve(cwd); @@ -558,7 +558,7 @@ describe("IgnoredPaths", function() { it("should ignore default folders there is an ignore file without unignored defaults", function() { const cwd = getFixturePath(); - const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath(".eslintignore"), cwd: cwd }); + const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath(".eslintignore"), cwd }); const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker(); const resolve = createResolve(cwd); @@ -572,7 +572,7 @@ describe("IgnoredPaths", function() { it("should not ignore things which are re-included in ignore file", function() { const cwd = getFixturePath(); - const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath(".eslintignoreWithUnignoredDefaults"), cwd: cwd }); + const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath(".eslintignoreWithUnignoredDefaults"), cwd }); const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker(); const resolve = createResolve(cwd); @@ -588,7 +588,7 @@ describe("IgnoredPaths", function() { it("should ignore files which we try to re-include in ignore file when ignore option is disabled", function() { const cwd = getFixturePath(); - const ignoredPaths = new IgnoredPaths({ ignore: false, ignorePath: getFixturePath(".eslintignoreWithUnignoredDefaults"), cwd: cwd }); + const ignoredPaths = new IgnoredPaths({ ignore: false, ignorePath: getFixturePath(".eslintignoreWithUnignoredDefaults"), cwd }); const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker(); const resolve = createResolve(cwd); @@ -604,7 +604,7 @@ describe("IgnoredPaths", function() { it("should not ignore dirs which are re-included by ignorePattern", function() { const cwd = getFixturePath("no-ignore-file"); - const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: cwd, ignorePattern: "!/node_modules/package" }); + const ignoredPaths = new IgnoredPaths({ ignore: true, cwd, ignorePattern: "!/node_modules/package" }); const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker(); const resolve = createResolve(cwd); diff --git a/tests/lib/rules/comma-style.js b/tests/lib/rules/comma-style.js index 9c5378e249f5..75f18a511d7a 100644 --- a/tests/lib/rules/comma-style.js +++ b/tests/lib/rules/comma-style.js @@ -88,6 +88,7 @@ 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" @@ -95,6 +96,7 @@ ruleTester.run("comma-style", rule, { }, { code: "var foo = 1\n,bar = 2;", + output: "var foo = 1,\nbar = 2;", errors: [{ message: LAST_MSG, type: "VariableDeclarator" @@ -102,6 +104,7 @@ ruleTester.run("comma-style", rule, { }, { code: "f([1,2\n,3]);", + output: "f([1,2,\n3]);", errors: [{ message: LAST_MSG, type: "Literal" @@ -109,6 +112,7 @@ ruleTester.run("comma-style", rule, { }, { code: "f([1,2\n,]);", + output: "f([1,2,\n]);", errors: [{ message: LAST_MSG, type: "Punctuator" @@ -116,6 +120,7 @@ ruleTester.run("comma-style", rule, { }, { code: "f([,2\n,3]);", + output: "f([,2,\n3]);", errors: [{ message: LAST_MSG, type: "Literal" @@ -123,6 +128,7 @@ ruleTester.run("comma-style", rule, { }, { code: "var foo = ['apples'\n, 'oranges'];", + output: "var foo = ['apples',\n 'oranges'];", errors: [{ message: LAST_MSG, type: "Literal" @@ -130,6 +136,7 @@ ruleTester.run("comma-style", rule, { }, { code: "var foo = 1,\nbar = 2;", + output: "var foo = 1\n,bar = 2;", options: ["first"], errors: [{ message: FIRST_MSG, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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"