0

I'm doing internationalization for a website and I'm encountering a weird problem with string interpolation. The following variable will not compile as it will fail to Parsing error: Unexpected character ''. You can check this out yourself in www.jslint.com.

const production = `${t('solarPanel.production.daily')} ${moment(selectedTime).format('D.M.')}`;

For clarification:

  • t is a function from a translation library that will return a localized string.
  • moment is a date library that will return a string form of the date object selectedTime.

This isn't important though, because the error is in the syntax. It seems that the translation function in the first interpolation is the problem. The linter points the supposed empty string to be between )} after the function.

I can go around this problem simply by assigning the value of the translation function into a variable and including that in the first interpolation. However, I am interested in what is causing this problem in the first place?

4
  • Is JSLint aware of template strings? Because it might be throwing the error just because it doesn't handle ES6. EDIT: tried it in ESLint and set the level to be 2016 and it passes. The problems it throws is that t and moment are not in scope. Commented Mar 5, 2019 at 8:00
  • Have you tried to escape the ' ie write \' ? Commented Mar 5, 2019 at 8:08
  • @VLAZ, you're right. That must have caused the error on JSLint. However, the problem still appeared on my development environment. The odd thing was that the string template worked fine if I replaced the first interpolation with another moment function. Commented Mar 5, 2019 at 8:40
  • I'm beginning to think this is a Babel error, since it seems like it sometimes doesn't understand the ES6 template string syntax. I've seen this error come and go in the past few days, but I'll report back here once I encounter it again. Commented Mar 5, 2019 at 8:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.