Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #322 from kristerkari/bugfix/double-slash-comment-…
…empty-line-before-autofix double-slash-comment-empty-line-before: bug with "never" option autofix
- Loading branch information
Showing
7 changed files
with
203 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import postcss from "postcss"; | ||
import { addEmptyLineBefore } from "../addEmptyLineBefore"; | ||
|
||
describe("addEmptyLineBefore", () => { | ||
it("adds single newline to the newline at the beginning", () => { | ||
expect(run("a {}\n b{}", "\n")).toBe("a {}\n\n b{}"); | ||
}); | ||
|
||
it("adds single newline to newline at the beginning with CRLF", () => { | ||
expect(run("a {}\r\n b{}", "\r\n")).toBe("a {}\r\n\r\n b{}"); | ||
}); | ||
|
||
it("adds single newline to newline at the end", () => { | ||
expect(run("a {}\t\nb{}", "\n")).toBe("a {}\t\n\nb{}"); | ||
}); | ||
|
||
it("adds single newline to newline at the end with CRLF", () => { | ||
expect(run("a {}\t\r\nb{}", "\r\n")).toBe("a {}\t\r\n\r\nb{}"); | ||
}); | ||
|
||
it("adds single newline to newline in the middle", () => { | ||
expect(run("a {} \n\tb{}", "\n")).toBe("a {} \n\n\tb{}"); | ||
}); | ||
|
||
it("adds single newline to newline in the middle with CRLF", () => { | ||
expect(run("a {} \r\n\tb{}", "\r\n")).toBe("a {} \r\n\r\n\tb{}"); | ||
}); | ||
|
||
it("adds two newlines if there aren't any existing newlines", () => { | ||
expect(run("a {} b{}", "\n")).toBe("a {}\n\n b{}"); | ||
}); | ||
|
||
it("adds two newlines if there aren't any existing newlines with CRLF", () => { | ||
expect(run("a {} b{}", "\r\n")).toBe("a {}\r\n\r\n b{}"); | ||
}); | ||
}); | ||
|
||
function run(css, lineEnding) { | ||
const root = postcss.parse(css); | ||
|
||
addEmptyLineBefore(root.nodes[1], lineEnding); | ||
|
||
return root.toString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import postcss from "postcss"; | ||
import { removeEmptyLinesBefore } from "../removeEmptyLinesBefore"; | ||
|
||
describe("removeEmptyLineBefore", () => { | ||
it("removes single newline from the newline at the beginning", () => { | ||
expect(run("a {}\n\n b{}", "\n")).toBe("a {}\n b{}"); | ||
}); | ||
|
||
it("removes single newline from newline at the beginning with CRLF", () => { | ||
expect(run("a {}\r\n\r\n b{}", "\r\n")).toBe("a {}\r\n b{}"); | ||
}); | ||
|
||
it("removes single newline from newline at the end", () => { | ||
expect(run("a {}\t\n\nb{}", "\n")).toBe("a {}\t\nb{}"); | ||
}); | ||
|
||
it("removes single newline from newline at the end with CRLF", () => { | ||
expect(run("a {}\t\r\n\r\nb{}", "\r\n")).toBe("a {}\t\r\nb{}"); | ||
}); | ||
|
||
it("removes single newline from newline in the middle", () => { | ||
expect(run("a {} \n\n\tb{}", "\n")).toBe("a {} \n\tb{}"); | ||
}); | ||
|
||
it("removes single newline to newline in the middle with CRLF", () => { | ||
expect(run("a {} \r\n\r\n\tb{}", "\r\n")).toBe("a {} \r\n\tb{}"); | ||
}); | ||
|
||
it("removes two newlines if there are three newlines", () => { | ||
expect(run("a {}\n\n\n b{}", "\n")).toBe("a {}\n b{}"); | ||
}); | ||
|
||
it("removes two newlines if there are three newlines with CRLF", () => { | ||
expect(run("a {}\r\n\r\n\r\n b{}", "\r\n")).toBe("a {}\r\n b{}"); | ||
}); | ||
|
||
it("removes three newlines if there are four newlines", () => { | ||
expect(run("a {}\n\n\n\n b{}", "\n")).toBe("a {}\n b{}"); | ||
}); | ||
|
||
it("removes three newlines if there are four newlines with CRLF", () => { | ||
expect(run("a {}\r\n\r\n\r\n\r\n b{}", "\r\n")).toBe("a {}\r\n b{}"); | ||
}); | ||
}); | ||
|
||
function run(css, lineEnding) { | ||
const root = postcss.parse(css); | ||
|
||
removeEmptyLinesBefore(root.nodes[1], lineEnding); | ||
|
||
return root.toString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { repeat } from "lodash"; | ||
|
||
// Add an empty line before a node. Mutates the node. | ||
export function addEmptyLineBefore( | ||
node /*: postcss$node*/, | ||
newline /*: '\n' | '\r\n'*/ | ||
) /*: postcss$node*/ { | ||
if (!/\r?\n/.test(node.raws.before)) { | ||
node.raws.before = repeat(newline, 2) + node.raws.before; | ||
} else { | ||
node.raws.before = node.raws.before.replace(/(\r?\n)/, `${newline}$1`); | ||
} | ||
|
||
return node; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Remove empty lines before a node. Mutates the node. | ||
export function removeEmptyLinesBefore( | ||
node /*: postcss$node*/, | ||
newline /*: '\n' | '\r\n'*/ | ||
) /*: postcss$node*/ { | ||
node.raws.before = node.raws.before.replace(/(\r?\n\s*\r?\n)+/g, newline); | ||
|
||
return node; | ||
} |