Skip to content

Commit

Permalink
Fix: Remove extraneous linefeeds in one-var fixer (fixes #10741) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
st-sloth authored and aladdin-add committed Feb 8, 2019
1 parent 389362a commit 84ce72f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/rules/one-var.js
Expand Up @@ -340,7 +340,11 @@ module.exports = {
* y`
* ^ afterComma
*/
if (afterComma.loc.start.line > tokenAfterDeclarator.loc.end.line || afterComma.type === "Line" || afterComma.type === "Block") {
if (
afterComma.loc.start.line > tokenAfterDeclarator.loc.end.line ||
afterComma.type === "Line" ||
afterComma.type === "Block"
) {
let lastComment = afterComma;

while (lastComment.type === "Line" || lastComment.type === "Block") {
Expand All @@ -349,7 +353,7 @@ module.exports = {

return fixer.replaceTextRange(
[tokenAfterDeclarator.range[0], lastComment.range[0]],
`;\n${sourceCode.text.slice(tokenAfterDeclarator.range[1], lastComment.range[0])}\n${declaration.kind} `
`;${sourceCode.text.slice(tokenAfterDeclarator.range[1], lastComment.range[0])}${declaration.kind} `
);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/lib/rules/one-var.js
Expand Up @@ -884,7 +884,7 @@ ruleTester.run("one-var", rule, {
},
{
code: "const foo = 1,\n bar = 2;",
output: "const foo = 1;\n\n \nconst bar = 2;",
output: "const foo = 1;\n const bar = 2;",
options: [{ initialized: "never" }],
parserOptions: { ecmaVersion: 6 },
errors: [{
Expand All @@ -896,7 +896,7 @@ ruleTester.run("one-var", rule, {
},
{
code: "var foo = 1,\n bar = 2;",
output: "var foo = 1;\n\n \nvar bar = 2;",
output: "var foo = 1;\n var bar = 2;",
options: [{ initialized: "never" }],
errors: [{
message: "Split initialized 'var' declarations into multiple statements.",
Expand All @@ -907,7 +907,7 @@ ruleTester.run("one-var", rule, {
},
{
code: "var foo = 1, // comment\n bar = 2;",
output: "var foo = 1;\n // comment\n \nvar bar = 2;",
output: "var foo = 1; // comment\n var bar = 2;",
options: [{ initialized: "never" }],
errors: [{
message: "Split initialized 'var' declarations into multiple statements.",
Expand All @@ -929,7 +929,7 @@ ruleTester.run("one-var", rule, {
},
{
code: "var f, /* test */ l;",
output: "var f;\n /* test */ \nvar l;",
output: "var f; /* test */ var l;",
options: ["never"],
errors: [{
message: "Split 'var' declarations into multiple statements.",
Expand Down

0 comments on commit 84ce72f

Please sign in to comment.