From dba72f40f20efa53d472119af63132ca4bf8b497 Mon Sep 17 00:00:00 2001 From: fisker Date: Mon, 2 Nov 2020 13:09:24 +0800 Subject: [PATCH 1/2] JS: Keep comments in `messageAST` --- src/language-js/clean.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/language-js/clean.js b/src/language-js/clean.js index f695551fe78d..d3f388d9ff85 100644 --- a/src/language-js/clean.js +++ b/src/language-js/clean.js @@ -3,7 +3,6 @@ const ignoredProperties = new Set([ "range", "raw", - "comments", "leadingComments", "trailingComments", "innerComments", @@ -21,6 +20,13 @@ function clean(ast, newObj, parent) { delete newObj.sourceType; } + if (ast.type === "Line" || ast.type === "CommentLine") { + newObj.value = newObj.value.trimEnd(); + } + if ( ast.type === "Block" || ast.type === "CommentBlock") { + newObj.value = newObj.value.split("\n").map(line => line.trim()).join("\n"); + } + if ( ast.type === "BigIntLiteral" || ast.type === "BigIntLiteralTypeAnnotation" From e338ff4248512422141c87fe0192ccf30e3323b4 Mon Sep 17 00:00:00 2001 From: fisker Date: Mon, 2 Nov 2020 14:23:39 +0800 Subject: [PATCH 2/2] Flow and `insertPragma` --- src/language-js/clean.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/language-js/clean.js b/src/language-js/clean.js index d3f388d9ff85..8983e93701b0 100644 --- a/src/language-js/clean.js +++ b/src/language-js/clean.js @@ -23,8 +23,20 @@ function clean(ast, newObj, parent) { if (ast.type === "Line" || ast.type === "CommentLine") { newObj.value = newObj.value.trimEnd(); } - if ( ast.type === "Block" || ast.type === "CommentBlock") { - newObj.value = newObj.value.split("\n").map(line => line.trim()).join("\n"); + if (ast.type === "Block" || ast.type === "CommentBlock") { + const comment = newObj.value.trim(); + if (comment === "* @format") { + return null; + // Flow comment + } else if (comment.startsWith("::") || comment === "* @format") { + newObj.value = ""; + } else { + const lines = comment + .split("\n") + .map((line) => line.trim()) + .filter((line) => line === "* @format"); + newObj.value = lines.join("\n"); + } } if (