question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong variable in literal strings

See original GitHub issue

Describe the bug Lingui confuses variables (?)

To Reproduce Try this code:

import React from 'react'
import { t } from '@lingui/macro'

export default class MyComponent extends React.Component {
  render () {
    const totalPrice = 10
    let message
    if (totalPrice > 0) {
      const limit = 50
      message = i18n._(t`This is the great limit ${limit}`)
    } else if (totalPrice <= 0) {
      const limit = 10
      message = i18n._(
        t`This is the small limit ${limit}`
      )
    }
    return message
  }
}

Now use babel to transpile it, you will get this:

...snip...

      if (totalPrice > 0) {
        var limit = 50;
        message = i18n._( /*i18n*/{
          id: 'This is the great limit {limit}',
          values: {
            limit: limit
          }
        });
      } else if (totalPrice <= 0) {
        var _limit = 10;
        message = i18n._( /*i18n*/{
          id: 'This is the small limit {limit}',
          values: {
            limit: limit
          }
        });
      }
      return message;

...snip...

As you can see the second “limit” variable is still limit

Expected behavior The second snip code should use _limit variable as follow:

        var _limit = 10;
        message = i18n._( /*i18n*/{
          id: 'This is the small limit {limit}',
          values: {
            limit: _limit
          }

Additional context

  • jsLingui version 2.7.4
  • Babel version
project@2.0.1 /myhome/project-test
├─┬ babel-cli@6.26.0
│ ├── babel-core@6.26.3  deduped
│ └─┬ babel-register@6.26.0
│   └── babel-core@6.26.3  deduped
└── babel-core@6.26.3 
  • .babelrc:
{
  "presets": ["es2015", "stage-0", "react"],
  "plugins":[
    "macros",
  ]
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
semoalcommented, Nov 5, 2020

https://codesandbox.io/s/epic-butterfly-ou8ww?file=/src/index.js @MartijnHols here you have, updated the codesandbox to v3.x.x and looks fixed 👍🏻

1reaction
MartijnHolscommented, Jul 2, 2019

I just ran into this bug. It also occurs when defining a var inside a switch case along the lines of;

switch (err) {
  case errors.invalidEmail:
    const email = form.values.email
    return <Trans><strong>{email}</strong> is invalid</Trans>
  // ...
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Handle the Unclosed String Literal Error in Java - Rollbar
Python unclosed string literal error refers to the Java compiler failing to interpret a string literal due to the missing of a double...
Read more >
Using string variables instead of string literals causes error
A string literal and a std::string are two completely different things. Usually when a function f expects a const char* you can pass...
Read more >
jQuery Undetermined String Literal Error - SitePoint
If your trying to assign a string that covers multiple lines to a variable you might see the “Undetermined String Literal” error.
Read more >
literal string with variable substitution syntax in script throws error at ...
we have this script which is generating message data into a file as part of one of our services. the message data needs...
Read more >
Template literals (Template strings) - JavaScript | MDN
However, a tagged template literal may not result in a string; ... However, invalid escape sequences will cause a syntax error, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found