prettier-plugin-erb
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -5,2 +5,8 @@ # Changelog | ||
## v0.3.0 - 3 June 2021 | ||
### Bug fixes | ||
- Fix formatting of expressions inside elements | ||
## v0.2.0 - 25 May 2021 | ||
@@ -7,0 +13,0 @@ |
@@ -1,5 +0,5 @@ | ||
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 | ||
@@ -21,3 +21,4 @@ .map((token) => ({ ...token })) | ||
if (!formatRaw) { | ||
formattedExpression = format(expression, { ...options, parser: 'ruby' }); | ||
formattedExpression = embedTextToDoc(expression, { ...options, parser: 'ruby' }); | ||
formattedExpression.parts.pop(); // removes newline at the end | ||
} | ||
@@ -50,3 +51,3 @@ } catch (error) { | ||
if (formattedExpression.trim().match(/\r?\n/)) { | ||
if (typeof formattedExpression === 'string' && formattedExpression.trim().match(/\r?\n/)) { | ||
const formattedMultilineExpression = formattedExpression | ||
@@ -67,5 +68,17 @@ .trim() | ||
); | ||
} 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; | ||
} | ||
} | ||
@@ -72,0 +85,0 @@ |
@@ -22,3 +22,3 @@ const { encodeExpressions, decodeExpressions } = require('prettier-html-templates'); | ||
const formattedTokens = formatMultilineExpressions(tokens, options); | ||
const formattedTokens = formatMultilineExpressions(tokens, options, embedTextToDoc); | ||
@@ -25,0 +25,0 @@ const isTextOnlyWithSingleExpression = |
{ | ||
"name": "prettier-plugin-erb", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"license": "MIT", | ||
@@ -48,3 +48,3 @@ "description": "Prettier plugin for .html.erb files", | ||
"husky": "^6.0.0", | ||
"jest": "^26.6.3", | ||
"jest": "^27.0.3", | ||
"jest-snapshot-serializer-raw": "^1.1.0", | ||
@@ -51,0 +51,0 @@ "lint-staged": "^11.0.0", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45226
35
517