-
-
Notifications
You must be signed in to change notification settings - Fork 172
Improve handling of whitespace and duplicate class removal #276
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is important when using templating languages
We’d have to modify the start/end locations of many nodes in the AST. Technically, only AST nodes appearing _after_ the string on the same line but that might actually be sibling nodes and ancestor nodes. It’s a better option for now to disable it.
This was referenced Jun 4, 2024
bronisMateusz
pushed a commit
to bronisMateusz/prettier-plugin-tailwindcss-drupal
that referenced
this pull request
Apr 16, 2025
…abs#276) * Add tests * Be more careful about whitespace removal in concat expressions * Detect liquid concat expressions in newer ASTs * Make sure string splicing handles changing string length * Add option to preserve duplicate classes This is important when using templating languages * Disable whitespace removal in Svelte We’d have to modify the start/end locations of many nodes in the AST. Technically, only AST nodes appearing _after_ the string on the same line but that might actually be sibling nodes and ancestor nodes. It’s a better option for now to disable it. * Disable whitespace removal in Liquid * Disable duplicate removal in Liquid and Svelte * Update changelog * Update readme * Update CHANGELOG.md * Update README.md --------- Co-authored-by: Jonathan Reinink <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR does a handful of things:
Tweak whitespace handling inside concatenation expressions
For example, in the following code:
After formatting the output is
However, this results in invalid class names. We'll now only remove the whitespace on one side of each of the strings (depending on if it's the start or not), so the output will be:
This means you're less likely to need to add extra whitespace only strings or disable whitespace removal to ensure the output stays functionally the same.
Add
tailwindPreserveDuplicatesoptionNormally removing duplicate classes (whether known or unknown) is a safe operation to do. However, when using a templating language like Fluid or Blade it's possible to have "duplicate" classes that are important because they're compiler directives. The problem is that Prettier only considers the file to be HTML so we don't know about these directives.
For example, in Fluid, you might have something like this:
Our plugin sees
{f:if(condition:as a class,isCompact,as a class,then:as a class, etc… but doesn't know that they are directives so they are treated like unknown classes and moved to the front of the class list in their original order.Further, when we remove duplicate classes the second
then:,else:, and{f:if(condition:, etc… are all removed, resulting in invalid Fluid code:To fix this, we've added a new
tailwindPreserveDuplicatesoption that prevents this behavior. It isfalseby default but can be enabled if you're using a templating language where this is a problem:Fixes some bugs
Fixes #273