Skip to content
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

refactor(macro): remove whitespace cleaning from js macro #1897

Conversation

timofei-iatsenko
Copy link
Collaborator

@timofei-iatsenko timofei-iatsenko commented Mar 29, 2024

Description

As other developers noted, whitespace trimming is in JS macro redundant and counterintuitive.

If i write:

t`Hello 
world`

I'm expecting to get a message with a line break in the middle, and don't expect this to be trimmed.

Another example:

t`Label: ` + value

Note the space after ":", it's expected to be there, but "normalization" will remove it.

Other example would be a markdown, or just a whatever purpose user want to have an original formatting.

This PR completely remove whitespace trimming from JS macro, and left the value as-is.

Second important change

There was an unclear logic around unicode escaping in template literals, which I also removed in favor of always "cooked" value.

The template literal has 2 values, raw and cooked. For some historical reasons, the macro took a "raw" property (i followed git history up to creation of the file to see how this code evolved over time).

Then some additional cleanup happened over the "raw", for example double backslashes were removed to make line breaks works.

Then someone reported (#1098) that unicode sequences doesn't work, and an additional layer of logic was added. Now when there were any unicode sequences the code took "cooked" and "raw" when it's not.

Some of this "fixes" for "raw" string happened in the whitespace normalization process and become broken when I removed it.

I dug into that a little bit more, and found that using "cooked" value always give a correct result.

I'm a little bit concerned about this comment:

// if it's an unicode we keep the cooked value because it's the parsed value by babel (without unicode chars)
// This regex will detect if a string contains unicode chars, when they're we should interpolate them
// why? because platforms like react native doesn't parse them, just doing a JSON.parse makes them UTF-8 friendly

Since i didn't find any test covering this. @vonovak may be you can test these changes in React Native?

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Examples update

Fixes #1799

Checklist

  • I have read the CONTRIBUTING and CODE_OF_CONDUCT docs
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation (if appropriate)

Copy link

vercel bot commented Mar 29, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
js-lingui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 29, 2024 0:14am

Copy link

size-limit report 📦

Path Size
./packages/core/dist/index.mjs 2.86 KB (0%)
./packages/detect-locale/dist/index.mjs 723 B (0%)
./packages/react/dist/index.mjs 1.67 KB (0%)
./packages/remote-loader/dist/index.mjs 7.26 KB (0%)

Copy link

codecov bot commented Mar 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.16%. Comparing base (b031df7) to head (3fb9e80).

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #1897      +/-   ##
==========================================
- Coverage   77.18%   77.16%   -0.02%     
==========================================
  Files          84       84              
  Lines        2165     2159       -6     
  Branches      559      557       -2     
==========================================
- Hits         1671     1666       -5     
  Misses        381      381              
+ Partials      113      112       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

timofei-iatsenko added a commit to lingui/swc-plugin that referenced this pull request Mar 29, 2024
timofei-iatsenko added a commit to lingui/swc-plugin that referenced this pull request Mar 29, 2024
timofei-iatsenko added a commit to lingui/swc-plugin that referenced this pull request Mar 29, 2024
* feature: robust JSX whitespace handling

* cleanup

* refactor(js-macro): align whitespacing handling with js version

ref: lingui/js-lingui#1897

!BREAKING CHANGE
Copy link
Collaborator

@vonovak vonovak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with RN, seems to work fine. You're on fire @thekip !! ⚡⚡

@andrii-bodnar andrii-bodnar merged commit 1f18e68 into lingui:next Apr 4, 2024
17 checks passed
@timofei-iatsenko timofei-iatsenko deleted the refactor/normalize-whitespaces-js branch April 18, 2024 10:54
@andrii-bodnar andrii-bodnar added this to the v5 milestone Oct 23, 2024
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.

3 participants