Remove extra line after directive with comment (#8536)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-06-11 08:45:44 +08:00
1 parent b5aa575575
commit 34d847c1cc
2 files changed
+57 -7

No files matched your search

+9 -6
View File
@@ -449,12 +449,17 @@ function printPathNoParens(path, options, print, args) {
return concat(parts);
case "Program":
case "Program": {
const hasContents =
!n.body.every(({ type }) => type === "EmptyStatement") || n.comments;
// Babel 6
if (n.directives) {
path.each((childPath) => {
const directivesCount = n.directives.length;
path.map((childPath, index) => {
parts.push(print(childPath), semi, hardline);
if (
(index < directivesCount - 1 || hasContents) &&
isNextLineEmpty(
options.originalText,
childPath.getValue(),
@@ -477,14 +482,12 @@ function printPathNoParens(path, options, print, args) {
);
// Only force a trailing newline if there were any contents.
if (
!n.body.every(({ type }) => type === "EmptyStatement") ||
n.comments
) {
if (hasContents) {
parts.push(hardline);
}
return concat(parts);
}
// Babel extension.
case "EmptyStatement":
return "";
+48 -1
View File
@@ -1 +1,48 @@
run_spec(__dirname, ["babel", "flow", "typescript"]);
const { outdent } = require("outdent");
run_spec(
{
dirname: __dirname,
snippets: [
{
code: outdent`
'use strict';
// comment
`,
output:
outdent`
"use strict";
// comment
` + "\n",
},
{
code: outdent`
'use strict';
// comment
`,
output:
outdent`
"use strict";
// comment
` + "\n",
},
{
code:
outdent`
'use strict';
// comment
` + "\n",
output:
outdent`
"use strict";
// comment
` + "\n",
},
],
},
["babel", "flow", "typescript"]
);