Skip to content

Commit

Permalink
fix(flow,ts): format /* HTML */ templates (#5658)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Dec 19, 2018
1 parent 711c6d7 commit 65a9d3b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/language-js/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,9 +858,20 @@ function isBlockComment(comment) {
return comment.type === "Block" || comment.type === "CommentBlock";
}

function hasLeadingComment(node, fn = () => true) {
if (node.leadingComments) {
return node.leadingComments.some(fn);
}
if (node.comments) {
return node.comments.some(comment => comment.leading && fn(comment));
}
return false;
}

module.exports = {
handleOwnLineComment,
handleEndOfLineComment,
handleRemainingComment,
hasLeadingComment,
isBlockComment
};
11 changes: 5 additions & 6 deletions src/language-js/embed.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

const { isBlockComment, hasLeadingComment } = require("./comments");

const {
builders: {
indent,
Expand Down Expand Up @@ -508,12 +510,9 @@ function hasLanguageComment(node, languageName) {
// we will not trim the comment value and we will expect exactly one space on
// either side of the GraphQL string
// Also see ./clean.js
return (
node.leadingComments &&
node.leadingComments.some(
comment =>
comment.type === "CommentBlock" && comment.value === ` ${languageName} `
)
return hasLeadingComment(
node,
comment => isBlockComment(comment) && comment.value === ` ${languageName} `
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/multiparser_js_html/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`lit-html.js 1`] = `
====================================options=====================================
parsers: ["babylon"]
parsers: ["babylon", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
Expand Down
2 changes: 1 addition & 1 deletion tests/multiparser_js_html/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
run_spec(__dirname, ["babylon"]);
run_spec(__dirname, ["babylon", "flow", "typescript"]);

0 comments on commit 65a9d3b

Please sign in to comment.