From 3c9ce093b94dbcb04d4395c33983d4cb3b0764ac Mon Sep 17 00:00:00 2001 From: Ed Lee Date: Sun, 19 Jun 2016 05:19:13 +0100 Subject: [PATCH] Fix: Keep indentation when fixing `padded-blocks` "never" (fixes #6454) (#6456) --- lib/rules/padded-blocks.js | 4 ++-- tests/lib/rules/padded-blocks.js | 26 ++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/rules/padded-blocks.js b/lib/rules/padded-blocks.js index 7c378d92025..7ec24c65d7d 100644 --- a/lib/rules/padded-blocks.js +++ b/lib/rules/padded-blocks.js @@ -190,7 +190,7 @@ module.exports = { node: node, loc: { line: openBrace.loc.start.line, column: openBrace.loc.start.column }, fix: function(fixer) { - return fixer.replaceTextRange([openBrace.end, nextToken.start], "\n"); + return fixer.replaceTextRange([openBrace.end, nextToken.start - nextToken.loc.start.column], "\n"); }, message: NEVER_MESSAGE }); @@ -204,7 +204,7 @@ module.exports = { loc: {line: closeBrace.loc.end.line, column: closeBrace.loc.end.column - 1 }, message: NEVER_MESSAGE, fix: function(fixer) { - return fixer.replaceTextRange([previousToken.end, closeBrace.start], "\n"); + return fixer.replaceTextRange([previousToken.end, closeBrace.start - closeBrace.loc.start.column], "\n"); } }); } diff --git a/tests/lib/rules/padded-blocks.js b/tests/lib/rules/padded-blocks.js index c9d11ea8ab6..c7357795254 100644 --- a/tests/lib/rules/padded-blocks.js +++ b/tests/lib/rules/padded-blocks.js @@ -307,7 +307,7 @@ ruleTester.run("padded-blocks", rule, { }, { code: "{\n\n\n a();\n\n\n}", - output: "{\na();\n}", + output: "{\n a();\n}", options: ["never"], errors: [ { @@ -331,6 +331,17 @@ ruleTester.run("padded-blocks", rule, { } ] }, + { + code: "{\n\n\ta();\n}", + output: "{\n\ta();\n}", + options: ["never"], + errors: [ + { + message: NEVER_MESSAGE, + line: 1 + } + ] + }, { code: "{\na();\n\n}", output: "{\na();\n}", @@ -342,6 +353,17 @@ ruleTester.run("padded-blocks", rule, { } ] }, + { + code: " {\n a();\n\n }", + output: " {\n a();\n }", + options: ["never"], + errors: [ + { + message: NEVER_MESSAGE, + line: 4 + } + ] + }, { code: "{\n// comment\nif (\n// comment\n a) {}\n\n}", output: "{\n\n// comment\nif (\n// comment\n a) {}\n\n}", @@ -392,7 +414,7 @@ ruleTester.run("padded-blocks", rule, { }, { code: "switch (a) {\ncase 0: foo();\n\n }", - output: "switch (a) {\ncase 0: foo();\n}", + output: "switch (a) {\ncase 0: foo();\n }", options: [{switches: "never"}], errors: [ {