-
Notifications
You must be signed in to change notification settings - Fork 382
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
macro(babel) : There is a problem with transforming the embedded(nested) t-function #1745
Comments
I pulled the js-lingui (packages/macro) repository locally, and used jest to test my case, and it was also wrong. testFilePath: import { t as $t } from "@lingui/macro";
const a = $t`wrapper ${$t`nested`}`
const b = $t({
message: `wrapper ${$t`nested`}`,
}) Transformed code(in console):
|
Yeah, SWC plugin works differently then babel-macro. The discovering of macro function calls in babel version is delegated to babel-macro-plugin and in SWC plugin it has written in the plugin itself. The main question, what is the real-world scenario for this use case? Honestly speaking, i don't really want to invest time for fixing possibly rear use case in babel version of plugin. |
The project is compiled using the swc plugin, similar to the following code import { t as $t } from '@lingui/macro'
const columnsLen = 10
$t`Remove table ${columnsLen ? $t`columns` : $t`column`}` It works fine locally because swc is able to parse t functions inline. It is normal for the developer to open the page locally Guaranteeing 100% extraction is important because development simply doesn't realize errors locally (we expect local use to be safe) I know that issues like the above can be bypassed with icu format like select, but in general, the average developer is still used to template string conditional judgments (we can set up the specification, but there's no lint-like plugin to circumvent these writings, and once the rest of the team writes it that way, accidents happen) Please fix the issue |
swc plugin will remove the default message on the production, so if the cli extraction is incomplete,page will render hashId (But our users don't notice locally.) |
I agree that inconsistency in results between extractor which uses babel and swc which is used for actual program could lead to potentialy hard to catch errors. This is indeed should be fixed but i don't know when i will have time to look into this. I'm happy to review a PR if you feel brave to help with that 🙂 |
/**
* Filtering nested macro calls
*
* <Macro>
* <Macro /> <-- this would be filtered out
* </Macro>
*/
function isRootPath(allPath: NodePath[]) {
return (node: NodePath) =>
(function traverse(path): boolean {
if (!path.parentPath) {
return true
} else {
return !allPath.includes(path.parentPath) && traverse(path.parentPath)
}
})(node)
} I know it's this line that talks about embedded t-function filtering, but I don't understand why we need to filter before. For increase performance? Probably for jsx, the lift is more pronounced because Trans has many layers of nesting within it I would love to be involved in this project because lingui is very cool! I plan to go through the lingui-swc source code first and compare the differences between the two implementations (I'm trying to learn rust today) |
The difference is very big, actually. SWC version written from scratch, zero lines from babel implementation are used. All this filtering is needed because of babel-macro-plugin integration instead of just plugin as it's done for SWC. How it works for babel-macro:
How SWC plugin works: We do traverse from the Root of AST and compile nodes when they appear in the tree. It means if we found any node which is belongs to Lingui whole that AST branch would be processed and replaced in one pass. So as you see babel macro plugin actually bringing more mess into implementation, because the lingui usecase is not so straightforward. |
Thanks for the explanation!I've learned the key to the problem. |
fixed by #1867 |
Describe the bug
macro(babel) : There is a problem with transforming the embedded t-function
To Reproduce
Steps to reproduce the behavior, possibly with minimal code sample, e.g:
In babel-macro, it will be transformed
Expected behavior
This is the result of the macro-swc-plugin conversion (and also the code for the target)
As you can see, the swc macro plugin and the babel macro plugin transformations don't match, and babel is missing the embedded t function transformation
Additional context
Add any other context about the problem here.
lingui --version
4.3.0
npm list @babel/core
7.21.4
@lingui/swc-plugin
babel-macro-plugin
.babelrc
) or framework you use (Create React App, NextJs, Vite)The text was updated successfully, but these errors were encountered: