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

feat(macro): support JSX macro inside conditional expressions #1436

Merged

Conversation

thekip
Copy link
Collaborator

@thekip thekip commented Feb 14, 2023

Description

This PR adding syntax support for ConditionalExpressions in JSX Macro:

import { Trans } from '@lingui/macro'
<Trans>Hello, {props.world ? <Trans>world</Trans> : <Trans>guys</Trans>}</Trans>

To make reviewing easier, i split changes into 2 commits.

  • First commit refactoring JSX macro to operate with babel's path instead of node
  • Second commit fix actual issue.

We need path because babel doesn't allow traversing over plain node. And manual traversing wasn't a case here because there might be a syntax which we didn't expect. (multiple nested ternaries or function call which accepts jsx node, etc)

Also issue in SWC version repo: lingui/swc-plugin#16

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

fixes: #1175

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 necessary documentation (if appropriate)

@vercel
Copy link

vercel bot commented Feb 14, 2023

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

Name Status Preview Comments Updated
js-lingui ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 15, 2023 at 10:39AM (UTC)

@github-actions
Copy link

github-actions bot commented Feb 14, 2023

size-limit report 📦

Path Size
./packages/core/build/esm/index.js 1.76 KB (0%)
./packages/detect-locale/build/esm/index.js 812 B (0%)
./packages/react/build/esm/index.js 1.79 KB (0%)
./packages/remote-loader/build/esm/index.js 7.29 KB (0%)

@thekip thekip force-pushed the fix/trans-macro-conditional-expression branch from b34528b to dad400c Compare February 14, 2023 15:18
@thekip thekip changed the title Fix/trans macro conditional expression feature(macro): support JSX macro inside conditional expressions Feb 14, 2023
@thekip thekip marked this pull request as ready for review February 14, 2023 15:18
@thekip thekip force-pushed the fix/trans-macro-conditional-expression branch from dad400c to 2555690 Compare February 14, 2023 15:22
@codecov
Copy link

codecov bot commented Feb 14, 2023

Codecov Report

Base: 70.92% // Head: 71.02% // Increases project coverage by +0.09% 🎉

Coverage data is based on head (3a00d7a) compared to base (77d1c08).
Patch coverage: 89.15% of modified lines in pull request are covered.

❗ Current head 3a00d7a differs from pull request most recent head b24690d. Consider uploading reports for the commit b24690d to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #1436      +/-   ##
==========================================
+ Coverage   70.92%   71.02%   +0.09%     
==========================================
  Files          75       75              
  Lines        1940     1943       +3     
  Branches      525      526       +1     
==========================================
+ Hits         1376     1380       +4     
+ Misses        452      451       -1     
  Partials      112      112              
Impacted Files Coverage Δ
packages/macro/test/jsx-trans.ts 100.00% <ø> (ø)
packages/macro/src/macroJsx.ts 92.10% <87.83%> (+0.49%) ⬆️
packages/macro/src/index.ts 83.92% <100.00%> (+0.05%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@thekip thekip changed the title feature(macro): support JSX macro inside conditional expressions feat(macro): support JSX macro inside conditional expressions Feb 14, 2023
@Martin005 Martin005 linked an issue Feb 14, 2023 that may be closed by this pull request
Comment on lines +208 to +213
;<Trans
id={"Hello, {0}"}
values={{
0: props.world ? <Trans id={'world'} /> : <Trans id={'guys'} />
}}
/>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm thinking that we can automatically generate from 1 level ternary an ICU select expression:

Hello, { props.world, select, =true {world} =false {guys} }

Or i'm overengineering...

Copy link
Contributor

Choose a reason for hiding this comment

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

Select needs to always have the other case, so I don't really see a way how to do that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hello, { props.world, select, =true {world} other {guys} }

But it's overengineering anyway

Comment on lines +4 to +6
"lib": [
"ES2019.Array"
],
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is for Array.prototype.flat / Array.prototype.flatMap
Supported started from Node.js 11 AFAIK

Comment on lines +321 to +336
one: [
{
type: "arg",
name: "gender",
type: "Identifier",
}),
format: "select",
options: {
male: "he",
female: "she",
other: "they",
value: expect.objectContaining({
name: "gender",
type: "Identifier",
}),
format: "select",
options: {
male: "he",
female: "she",
other: "they",
},
},
},
],
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Was a single element - become an array

@andrii-bodnar andrii-bodnar merged commit 44f8360 into lingui:next Feb 15, 2023
@thekip thekip deleted the fix/trans-macro-conditional-expression branch February 15, 2023 12:34
andrii-bodnar pushed a commit that referenced this pull request Feb 20, 2023
* refactor(macro): pass  down and manipulate with `path` instead of `node`

* feature(macro): support JSX macro inside conditional expressions

* refactor(macro): remove alreadyVisitedCache in favor of dedupe references from babel-macro-plugin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JSX macros aren't working inside JSX expressions in JSX macros
3 participants