Fix trailing commas with a trailing comment (#664)
The reason why trailing comments on a line work with commas is because of a special `lineSuffix` document that delays printing the content until a \n is printed. But `lineSuffix` only worked for pure string. By making it work with arbitrary documents we can fix trailing commas with trailing comments. Fixes #538master
parent
287db4a9b6
commit
1a8e97f92b
|
@ -439,7 +439,7 @@ function printTrailingComment(commentPath, print, options, parentNode) {
|
||||||
comment
|
comment
|
||||||
);
|
);
|
||||||
|
|
||||||
return concat([hardline, isLineBeforeEmpty ? hardline : "", contents]);
|
return lineSuffix(concat([hardline, isLineBeforeEmpty ? hardline : "", contents]));
|
||||||
} else if (isBlock) {
|
} else if (isBlock) {
|
||||||
// Trailing block comments never need a newline
|
// Trailing block comments never need a newline
|
||||||
return concat([" ", contents]);
|
return concat([" ", contents]);
|
||||||
|
|
|
@ -63,11 +63,7 @@ function ifBreak(breakContents, flatContents) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function lineSuffix(contents) {
|
function lineSuffix(contents) {
|
||||||
if (typeof contents !== "string") {
|
assertDoc(contents);
|
||||||
throw new Error(
|
|
||||||
"lineSuffix only takes a string, but given: " + JSON.stringify(contents)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return { type: "line-suffix", contents };
|
return { type: "line-suffix", contents };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -87,7 +87,7 @@ function printDocToString(doc, width, newLine) {
|
||||||
let cmds = [[0, MODE_BREAK, doc]];
|
let cmds = [[0, MODE_BREAK, doc]];
|
||||||
let out = [];
|
let out = [];
|
||||||
let shouldRemeasure = false;
|
let shouldRemeasure = false;
|
||||||
let lineSuffix = "";
|
let lineSuffix = [];
|
||||||
|
|
||||||
while (cmds.length !== 0) {
|
while (cmds.length !== 0) {
|
||||||
const x = cmds.pop();
|
const x = cmds.pop();
|
||||||
|
@ -190,7 +190,7 @@ function printDocToString(doc, width, newLine) {
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case "line-suffix":
|
case "line-suffix":
|
||||||
lineSuffix += doc.contents;
|
lineSuffix.push([ind, mode, doc.contents]);
|
||||||
break;
|
break;
|
||||||
case "line":
|
case "line":
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
|
@ -215,8 +215,15 @@ function printDocToString(doc, width, newLine) {
|
||||||
}
|
}
|
||||||
|
|
||||||
case MODE_BREAK:
|
case MODE_BREAK:
|
||||||
|
if (lineSuffix.length) {
|
||||||
|
cmds.push([ind, mode, doc]);
|
||||||
|
[].push.apply(cmds, lineSuffix.reverse());
|
||||||
|
lineSuffix = [];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (doc.literal) {
|
if (doc.literal) {
|
||||||
out.push(lineSuffix + newLine);
|
out.push(newLine);
|
||||||
pos = 0;
|
pos = 0;
|
||||||
} else {
|
} else {
|
||||||
if (out.length > 0) {
|
if (out.length > 0) {
|
||||||
|
@ -227,11 +234,9 @@ function printDocToString(doc, width, newLine) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
out.push(lineSuffix + newLine + " ".repeat(ind));
|
out.push(newLine + " ".repeat(ind));
|
||||||
pos = ind;
|
pos = ind;
|
||||||
}
|
}
|
||||||
|
|
||||||
lineSuffix = "";
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -153,3 +153,153 @@ const aLong = {
|
||||||
};
|
};
|
||||||
"
|
"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`test trailing_whitespace.js 1`] = `
|
||||||
|
"let example = [
|
||||||
|
\'FOO\',
|
||||||
|
\'BAR\',
|
||||||
|
// Comment
|
||||||
|
];
|
||||||
|
|
||||||
|
foo({},
|
||||||
|
// Comment
|
||||||
|
);
|
||||||
|
|
||||||
|
o = {
|
||||||
|
state,
|
||||||
|
// Comment
|
||||||
|
};
|
||||||
|
|
||||||
|
o = {
|
||||||
|
state,
|
||||||
|
|
||||||
|
// Comment
|
||||||
|
};
|
||||||
|
|
||||||
|
function supersupersupersuperLongF(
|
||||||
|
supersupersupersuperLongA,
|
||||||
|
supersupersupersuperLongB
|
||||||
|
// Comment
|
||||||
|
) {
|
||||||
|
a
|
||||||
|
}
|
||||||
|
function supersupersupersuperLongF(
|
||||||
|
supersupersupersuperLongA,
|
||||||
|
supersupersupersuperLongB,
|
||||||
|
// Comment
|
||||||
|
) {
|
||||||
|
a
|
||||||
|
}
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
let example = [
|
||||||
|
\"FOO\",
|
||||||
|
\"BAR\"
|
||||||
|
// Comment
|
||||||
|
];
|
||||||
|
|
||||||
|
foo(
|
||||||
|
{}
|
||||||
|
// Comment
|
||||||
|
);
|
||||||
|
|
||||||
|
o = {
|
||||||
|
state
|
||||||
|
// Comment
|
||||||
|
};
|
||||||
|
|
||||||
|
o = {
|
||||||
|
state
|
||||||
|
// Comment
|
||||||
|
};
|
||||||
|
|
||||||
|
function supersupersupersuperLongF(
|
||||||
|
supersupersupersuperLongA,
|
||||||
|
supersupersupersuperLongB
|
||||||
|
) // Comment
|
||||||
|
{
|
||||||
|
a;
|
||||||
|
}
|
||||||
|
function supersupersupersuperLongF(
|
||||||
|
supersupersupersuperLongA,
|
||||||
|
supersupersupersuperLongB
|
||||||
|
) // Comment
|
||||||
|
{
|
||||||
|
a;
|
||||||
|
}
|
||||||
|
"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`test trailing_whitespace.js 2`] = `
|
||||||
|
"let example = [
|
||||||
|
\'FOO\',
|
||||||
|
\'BAR\',
|
||||||
|
// Comment
|
||||||
|
];
|
||||||
|
|
||||||
|
foo({},
|
||||||
|
// Comment
|
||||||
|
);
|
||||||
|
|
||||||
|
o = {
|
||||||
|
state,
|
||||||
|
// Comment
|
||||||
|
};
|
||||||
|
|
||||||
|
o = {
|
||||||
|
state,
|
||||||
|
|
||||||
|
// Comment
|
||||||
|
};
|
||||||
|
|
||||||
|
function supersupersupersuperLongF(
|
||||||
|
supersupersupersuperLongA,
|
||||||
|
supersupersupersuperLongB
|
||||||
|
// Comment
|
||||||
|
) {
|
||||||
|
a
|
||||||
|
}
|
||||||
|
function supersupersupersuperLongF(
|
||||||
|
supersupersupersuperLongA,
|
||||||
|
supersupersupersuperLongB,
|
||||||
|
// Comment
|
||||||
|
) {
|
||||||
|
a
|
||||||
|
}
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
let example = [
|
||||||
|
\"FOO\",
|
||||||
|
\"BAR\",
|
||||||
|
// Comment
|
||||||
|
];
|
||||||
|
|
||||||
|
foo(
|
||||||
|
{},
|
||||||
|
// Comment
|
||||||
|
);
|
||||||
|
|
||||||
|
o = {
|
||||||
|
state,
|
||||||
|
// Comment
|
||||||
|
};
|
||||||
|
|
||||||
|
o = {
|
||||||
|
state,
|
||||||
|
// Comment
|
||||||
|
};
|
||||||
|
|
||||||
|
function supersupersupersuperLongF(
|
||||||
|
supersupersupersuperLongA,
|
||||||
|
supersupersupersuperLongB,
|
||||||
|
) // Comment
|
||||||
|
{
|
||||||
|
a;
|
||||||
|
}
|
||||||
|
function supersupersupersuperLongF(
|
||||||
|
supersupersupersuperLongA,
|
||||||
|
supersupersupersuperLongB,
|
||||||
|
) // Comment
|
||||||
|
{
|
||||||
|
a;
|
||||||
|
}
|
||||||
|
"
|
||||||
|
`;
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
let example = [
|
||||||
|
'FOO',
|
||||||
|
'BAR',
|
||||||
|
// Comment
|
||||||
|
];
|
||||||
|
|
||||||
|
foo({},
|
||||||
|
// Comment
|
||||||
|
);
|
||||||
|
|
||||||
|
o = {
|
||||||
|
state,
|
||||||
|
// Comment
|
||||||
|
};
|
||||||
|
|
||||||
|
o = {
|
||||||
|
state,
|
||||||
|
|
||||||
|
// Comment
|
||||||
|
};
|
||||||
|
|
||||||
|
function supersupersupersuperLongF(
|
||||||
|
supersupersupersuperLongA,
|
||||||
|
supersupersupersuperLongB
|
||||||
|
// Comment
|
||||||
|
) {
|
||||||
|
a
|
||||||
|
}
|
||||||
|
function supersupersupersuperLongF(
|
||||||
|
supersupersupersuperLongA,
|
||||||
|
supersupersupersuperLongB,
|
||||||
|
// Comment
|
||||||
|
) {
|
||||||
|
a
|
||||||
|
}
|
Loading…
Reference in New Issue