Skip to content

Commit

Permalink
Handle multiline expression printing inside of element (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamzapasnik authored Jun 3, 2021
1 parent 069080c commit 33effb4
Show file tree
Hide file tree
Showing 19 changed files with 783 additions and 299 deletions.
23 changes: 18 additions & 5 deletions lib/formatter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { concat, indent, hardline, group } = require('prettier').doc.builders;
const { format } = require('prettier');
const { concat, indent, hardline, group, line } = require('prettier').doc.builders;
const { printDocToString } = require('prettier').doc.printer;

const formatMultilineExpressions = (tokens, options) => {
const formatMultilineExpressions = (tokens, options, embedTextToDoc) => {
return tokens
.map((token) => ({ ...token }))
.map((token) => {
Expand All @@ -18,7 +18,8 @@ const formatMultilineExpressions = (tokens, options) => {

try {
if (!formatRaw) {
formattedExpression = format(expression, { ...options, parser: 'ruby' });
formattedExpression = embedTextToDoc(expression, { ...options, parser: 'ruby' });
formattedExpression.parts.pop(); // removes newline at the end
}
} catch (error) {
formatRaw = true;
Expand Down Expand Up @@ -46,7 +47,7 @@ const formatMultilineExpressions = (tokens, options) => {
.join('\n');
}

if (formattedExpression.trim().match(/\r?\n/)) {
if (typeof formattedExpression === 'string' && formattedExpression.trim().match(/\r?\n/)) {
const formattedMultilineExpression = formattedExpression
.trim()
.split(/\r?\n/)
Expand All @@ -62,9 +63,21 @@ const formatMultilineExpressions = (tokens, options) => {
closingFullTag,
])
);
} else if (typeof formattedExpression === 'object') {
token.content = group(
concat([openingFullTag, indent(concat([line, formattedExpression])), line, closingFullTag])
);
} else {
token.content = group(concat([openingFullTag, ' ', formattedExpression.trim(), ' ', closingFullTag]));
}

// prettier-html-templates can't handle objects in this case, only strings
// TODO: multiline strings aren't formatted correctly, it'd be nice if we could pass the object
// maybe it will be achievable with 2.3 prettier compability
if (token.inElement && typeof token.content === 'object') {
const { formatted } = printDocToString(token.content, { ...options });
token.content = formatted;
}
}

return token;
Expand Down
2 changes: 1 addition & 1 deletion lib/printers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function print(path, options, _print) {
return prettier.format(options.originalText, { ...options, parser: 'html' });
}

const formattedTokens = formatMultilineExpressions(tokens, options);
const formattedTokens = formatMultilineExpressions(tokens, options, embedTextToDoc);

const isTextOnlyWithSingleExpression =
expressionsCount === 1 && !tokens.find((token) => token.type === 'text' && token.content.trim());
Expand Down
Loading

0 comments on commit 33effb4

Please sign in to comment.