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

Support multiline template literals #2527

Closed
hjylewis opened this issue Jan 21, 2021 · 9 comments
Closed

Support multiline template literals #2527

hjylewis opened this issue Jan 21, 2021 · 9 comments

Comments

@hjylewis
Copy link
Contributor

Which package?
ts-transformer, eslint-plugin-formatjs

Is your feature request related to a problem? Please describe.
Similar to #2252

Right now if you do something like this with a template literal to split the defaultMessage across multiple lines, eslint-plugin-formatjs/enforce-default-message will error since it's not a string literal or concatenation of string literals.

const myMessage = intl.formatMessage({
  defaultMessage: `Integer mattis eros cursus eros tempus, mollis
                   ullamcorper justo aliquam. Sed a diam vel eros
                   feugiat malesuada. Aliquam condimentum rhoncus
                   nibh tempus tempus. Nulla eget lobortis massa.`,
  id: 'test.loremIpsum'
});

Describe the solution you'd like
Ideally, since this a template literal containing no expressions, it is static and should be treated like a string literal.

Eslint should not error and the developer should be able to extract the message using the cli.

Sure a developer could just use the concatenated string literals solution but I think it would be more intuitive to support both. Unless you can think of a reason not to. Happy to try to open a PR for this.

@longlho
Copy link
Member

longlho commented Jan 22, 2021

The primary reason is that it intentionally preserves trailing whitespaces in the middle of your sentences so it basically has built-in footgun there. In your example when it got sent to translation vendor it'll become something like Integer mattis eros cursus eros tempus, mollis (a ton of spaces) ullamcorper justo aliquam. Sed a diam vel eros (a ton of spaces) feugiat malesuada. Aliquam condimentum rhoncus (a ton of spaces) nibh tempus tempus. Nulla eget lobortis massa..

@hjylewis
Copy link
Contributor Author

I could be mistaken but it looks like ts-transformer replaces those spaces with a single space.

I just checked and react-intl does the same thing with defaultMessage at runtime though I can't find where in the source code that's happening.

@longlho
Copy link
Member

longlho commented Jan 23, 2021

ts-transformer did it primarily bc of historical reasons. In cases where IDs are autogen as hash from defaultMessage & description, trimming whitespace results in ID mismatch so we prefer linting the source instead. Otherwise you have to do it both at runtime, everytime, and build time, which is brittle and annoying to debug.

@hjylewis
Copy link
Contributor Author

Fair enough makes sense.

In that case, if the concatenation of string literals is the ordained way to do multi-line defaultMessage or description that should probably be called out somewhere in the docs.

@longlho
Copy link
Member

longlho commented Jan 26, 2021

hmm we can't actually extract string literals concat. So it's basically would have to be a long string, or broken up into sentences.

@hjylewis
Copy link
Contributor Author

hmm we can't actually extract string literals concat. So it's basically would have to be a long string, or broken up into sentences.

Hmm, not sure I follow...

Doesn't this change 24454ba add support for extracting static string concat static?

@longlho
Copy link
Member

longlho commented Jan 26, 2021

You're right. I confused myself. We do support that :)

@longlho
Copy link
Member

longlho commented Jan 27, 2021

Had a discussion internally. This is something we can support.

@hjylewis
Copy link
Contributor Author

Awesome, thank you!

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

2 participants