Improve handling of whitespace and duplicate class removal #276
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
tailwindPreserveDuplicates
optionNormally 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
tailwindPreserveDuplicates
option that prevents this behavior. It isfalse
by default but can be enabled if you're using a templating language where this is a problem:Fixes some bugs
Fixes #273