-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
white space of class attributes are not trimmed if at beginning of string #361
Comments
Oh, I just find that the solution would not work with mustache expressions between different classes: Sadly I am not sure how to fix this, |
Maybe one thought on how to do this: if (parent.type === 'Attribute') {
// Direct child of attribute value -> add literallines at end of lines
// so that other things don't break in unexpected places
if (parent.name === 'class' && path.getParentNode(1).type === 'Element' || path.getParentNode(1).type === 'InlineComponent') {
// Special treatment for class attribute on html elements. Prettier
// will force everything into one line, we deviate from that and preserve lines.
const afterMustacheTag = parent.value[parent.value.indexOf(node) - 1]?.type === "MustacheTag"
rawText = rawText.replace(/([^ \t\n]|^)(([ \t]+$)|([ \t]+(\r?\n))|[ \t]+)/g,
// Remove trailing whitespace in lines with non-whitespace characters
// except at the end of the string
(match, characterBeforeWhitespace, _, isEndOfString, isEndOfLine, endOfLine) => isEndOfString
? match
: characterBeforeWhitespace + (isEndOfLine ? endOfLine : characterBeforeWhitespace || afterMustacheTag ? ' ' : ''));
// Shrink trailing whitespace in case it's followed by a mustache tag
// and remove it completely if it's at the end of the string, but not
// if it's on its own line
rawText = rawText.replace(/([^ \t\n])[ \t]+$/, parent.value.indexOf(node) === parent.value.length - 1 ? '$1' : '$1 ');
}
return concat(replaceEndOfLineWith(rawText, literalline));
} Check if the previous node was a |
Found one more thing just now: The full patch would then be this: if (parent.type === 'Attribute') {
// Direct child of attribute value -> add literallines at end of lines
// so that other things don't break in unexpected places
if (parent.name === 'class' && path.getParentNode(1).type === 'Element' || path.getParentNode(1).type === 'InlineComponent') {
// Special treatment for class attribute on html elements. Prettier
// will force everything into one line, we deviate from that and preserve lines.
const afterMustacheTag = parent.value[parent.value.indexOf(node) - 1]?.type === "MustacheTag"
rawText = rawText.replace(/([^ \t\n]|^)(([ \t]+$)|([ \t]+(\r?\n))|[ \t]+)/g,
// Remove trailing whitespace in lines with non-whitespace characters
// except at the end of the string
(match, characterBeforeWhitespace, _, isEndOfString, isEndOfLine, endOfLine) => isEndOfString
? match
: characterBeforeWhitespace + (isEndOfLine ? endOfLine : characterBeforeWhitespace || afterMustacheTag ? ' ' : ''));
// Shrink trailing whitespace in case it's followed by a mustache tag
// and remove it completely if it's at the end of the string, but not
// if it's on its own line
rawText = rawText.replace(/([^ \t\n]|^)[ \t]+$/, parent.value.indexOf(node) === parent.value.length - 1 ? '$1' : '$1 ');
}
return concat(replaceEndOfLineWith(rawText, literalline));
} |
Any progress on this issue? |
See the code comments here: #356 and the corresponding line of code here:
prettier-plugin-svelte/src/print/index.ts
Line 194 in bd91bbf
White space at the beginning of the class attribute is not stripped when at the beginning of the string:
will turn into this:
and not (as it should be):
The problem is: the first spaces are not captured by the regex, since match group 1 consumes the initial spaces and then match group two does not match anything anymore. So the first match we have is between
foo
andbar
.We can fix this by extending the regexp to
rawText = rawText.replace(/([^ \t\n]|^)(([ \t]+$)|([ \t]+(\r?\n))|[ \t]+)/g
, now match group 1 will match the start of the string and the match group 2 will match the following leading spaces.Then, we have to fix the replace function: for the first match (everything before foo),
characterBeforeWhitespace
evaluates to `` (empty string),isEndOfLine
evaluates to `false`, and for that reason, in lineprettier-plugin-svelte/src/print/index.ts
Line 207 in bd91bbf
so if
characterBeforeWhitespace
is empty, do not add the space character but instead do add nothing.Example on regex101: https://regex101.com/r/xOuZbd/1
The text was updated successfully, but these errors were encountered: