Skip to content

Conversation

@thecrypticace
Copy link
Contributor

Previously we'd check to see if a string literal was immediately nested inside a concat expression, for example:

export function Example() {
  return <div className={' flex ' + ' border ' + ' sm:hidden '} />
}

However, this breaks down when the strings being sorted are nested inside other expressions. For example, a ternary expression:

export function Example() {
  return <div class={'flex' + (standalone ? ' border ' : 'outline) + ' sm:hidden'}></div>
}

The way this works is if a string is nested inside a concat expression at any level then we'll do our best to keep the relevant whitespace. Obviously, due to runtime evaluation, we can't guarantee that all necessary whtiespace will be preserved, or that all unnecessary whitespace will be removed, but this improves the situation for the vast majority of cases.

Fixes #285

This depends on #287 and that PR must be merged first

@thecrypticace thecrypticace changed the title Improve concat expression detection Improve detection of string concatenation Jun 7, 2024
Base automatically changed from feat/convert-to-typescript to main June 11, 2024 13:02
@thecrypticace thecrypticace force-pushed the feat/better-whitespace-bailout branch from 42a53c3 to 25c5438 Compare June 11, 2024 14:41
@thecrypticace thecrypticace merged commit b047e02 into main Jun 11, 2024
@thecrypticace thecrypticace deleted the feat/better-whitespace-bailout branch June 11, 2024 14:44
bronisMateusz pushed a commit to bronisMateusz/prettier-plugin-tailwindcss-drupal that referenced this pull request Apr 16, 2025
* Improve `visit()` types

* Check for ancestor concat expressions

* Rename global usage of `createRequire`

* Update changelog

* Update src/index.ts

Co-authored-by: Robin Malfait <[email protected]>

* Update src/index.ts

Co-authored-by: Robin Malfait <[email protected]>

* Update src/index.ts

Co-authored-by: Robin Malfait <[email protected]>

---------

Co-authored-by: Robin Malfait <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Whitespace removal breaks on ternary conditions

3 participants