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

inconsistent message from extract and macro with multiple consecutive newlines / trailing spaces #1799

Closed
taozhou-glean opened this issue Nov 11, 2023 · 5 comments

Comments

@taozhou-glean
Copy link
Contributor

Describe the bug

just noticed this behavior difference when multiple consecutive newlines involved.

For extract: the newlines are extracted correctly
For macro: the newlines are collapsed due to

return text.replace(keepSpaceRe, " ").replace(keepNewLineRe, "\n").trim()

example from tests:

catalog.test.ts (\n\n preserved correctly):

image

macro, \n\n collapsed:

# macro/tests/js-t.ts
 {
    name: "Newlines are preserved",
    input: `
        import { t } from '@lingui/macro';
        t\`Multiline
          string\`
      `,
    expected: `
        import { i18n } from "@lingui/core";
        i18n._(
          /*i18n*/
          {
            id: "EfogM+",
            message: "Multiline\\nstring",
          }
        );
      `,
  },
  {
    name: "Newlines are preserved2",
    input: `
        import { t } from '@lingui/macro';
        t\`Multiline

          string\`
      `,
    expected: `
        import { i18n } from "@lingui/core";
        i18n._(
          /*i18n*/
          {
            id: "EfogM+",
            message: "Multiline\\nstring",
          }
        );
      `,
  },
```

- jsLingui version ``lingui --version``: 4.5.0
- Babel version ``npm list @babel/core``
- Macro support:
 - [x] I'm using SWC with `@lingui/swc-plugin`
 - [ ] I'm using Babel with `babel-macro-plugin`
 - [ ] I'm not using macro
- Your Babel config (e.g. `.babelrc`) or framework you use (Create React App, NextJs, Vite)
@taozhou-glean
Copy link
Contributor Author

we have to admit that normalize whitespace for jsx is unfortunate but has no choice, but I would suggest we skip normalization for macroJs ones, and just take whatever in the macro methods... which aligns with extract and matches user expectation as well

@taozhou-glean
Copy link
Contributor Author

or at least match the behavior between extract and macro :)

@taozhou-glean taozhou-glean changed the title inconsistent message from extract and macro with multiple consecutive newlines inconsistent message from extract and macro with multiple consecutive newlines / trailing spaces Nov 11, 2023
@timofei-iatsenko
Copy link
Collaborator

timofei-iatsenko commented Nov 11, 2023

The extract uses macro under the hood so the result should be the same. The difference might be if you use SWC plugin for macro, this way extract would use JS counterpart.

Could you provide an example of code which works inconsistently?

@taozhou-glean
Copy link
Contributor Author

taozhou-glean commented Nov 13, 2023

Could you provide an example of code which works inconsistently?

of course, the minimum example is:

t({message: "hello\n\nworld"})

from lingui extract, I am seeing:


#. js-lingui-id: DGn/t0
msgid ""
"hello\n"
"\n"
"world"
msgstr ""
"hello\n"
"\n"
"world"

while in compiled code, the lingui id it used to look up the message is actually from hello\nworld (just one new line instead)

@timofei-iatsenko
Copy link
Collaborator

There were differences between swc / babel macro in that matter. I followed your suggestion and completely removed whitespace cleaning from JS macro. I agree, that is useless and counterintuitive.

Babel JSX: #1882
Babel JS: #1897
SWC Port: lingui/swc-plugin#83

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

No branches or pull requests

3 participants