Add a new trim command to trim whitespaces in the current line (#4772)

This commit is contained in:
Warren Seine authored and Ika committed 2018-10-06 20:18:52 +08:00
1 parent 30ec30daa9
commit 7920e6fdde
6 files changed
+152 -18

No files matched your search

@@ -0,0 +1,38 @@
"use strict";
const prettier = require("prettier/local");
const docPrinter = prettier.doc.printer;
const docBuilders = prettier.doc.builders;
const printDocToString = docPrinter.printDocToString;
const concat = docBuilders.concat;
const hardline = docBuilders.hardline;
const literalline = docBuilders.literalline;
const trim = docBuilders.trim;
const indent = docBuilders.indent;
const markAsRoot = docBuilders.markAsRoot;
describe("markAsRoot", () => {
test.each([
[
"with hardline will insert a newline with current indentation",
concat([indent(markAsRoot(indent(hardline))), "123"]),
"\n 123"
],
[
"with literalline will insert a newline with root indentation",
concat([indent(markAsRoot(indent(literalline))), "123"]),
"\n 123"
],
[
"followed by trim will trims up to the the first column, ignoring indented root",
concat([indent(markAsRoot(indent(literalline))), trim, "123"]),
"\n123"
]
])("%s", (_, doc, expected) => {
const result = printDocToString(doc, { printWidth: 80, tabWidth: 2 });
expect(result).toBeDefined();
expect(result.formatted).toEqual(expected);
});
});
+59
View File
@@ -0,0 +1,59 @@
"use strict";
const prettier = require("prettier/local");
const docPrinter = prettier.doc.printer;
const docBuilders = prettier.doc.builders;
const printDocToString = docPrinter.printDocToString;
const concat = docBuilders.concat;
const line = docBuilders.line;
const trim = docBuilders.trim;
const group = docBuilders.group;
const indent = docBuilders.indent;
// These tests don't use `runPrettier` because `trim` is not used by any
// bundled parser (only third-party plugins).
describe("trim", () => {
test.each([
["trims the current line", group(concat(["hello ", trim])), "hello"],
[
"trims existing indentation",
group(
concat([
"function()",
line,
"{",
indent(
concat([
line,
group(concat([trim, "#if DEBUG"])),
line,
"alert(42);",
line,
group(concat([trim, "#endif"]))
])
),
line,
"}"
])
),
`function()
{
#if DEBUG
alert(42);
#endif
}`
],
[
"ignores trimmed characters when fitting the line",
group(concat(["hello ", " ", trim, line, "world!"])),
"hello world!"
]
])("%s", (_, doc, expected) => {
const result = printDocToString(doc, { printWidth: 12, tabWidth: 2 });
expect(result).toBeDefined();
expect(result.formatted).toEqual(expected);
});
});