最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ESLint error about Unexpected String Concatenation - Stack Overflow

programmeradmin5浏览0评论

I have the following:

return (useWordBoundary ? subString.substr(0, subString.lastIndexOf(' ')) : subString) + '\u2026';

Eslint is rejecting my code saying:

Suggest using template literals instead of string concatenation. (prefer-template)

What is wrong with the above?

I have the following:

return (useWordBoundary ? subString.substr(0, subString.lastIndexOf(' ')) : subString) + '\u2026';

Eslint is rejecting my code saying:

Suggest using template literals instead of string concatenation. (prefer-template)

What is wrong with the above?

Share Improve this question edited Feb 1, 2018 at 19:34 Muhammad Omer Aslam 23.8k9 gold badges46 silver badges69 bronze badges asked Feb 1, 2018 at 19:33 AnnaSmAnnaSm 2,3006 gold badges24 silver badges35 bronze badges 1
  • 2 What don't you understand? Do you know what a template literal is? – Barmar Commented Feb 1, 2018 at 19:35
Add a ment  | 

5 Answers 5

Reset to default 10

It's a style question really. In your example, there's arguably not a lot of benefit in using template literals:

return `${useWordBoundary ? subString.substr(0, subString.lastIndexOf(' ')) : subString}\u2026`;

In other situations, it makes code more readable:

return `Hello, ${name}!`;

// vs.

return 'Hello, ' + name + '!';

If you choose to, you can selectively disable a rule for a specific line via a ment:

// eslint-disable-next-line prefer-template
return (useWordBoundary ? subString.substr(0, subString.lastIndexOf(' ')) : subString) + '\u2026';

Template literals are also known as String Interpolation.

Your code must follow this syntax:

return `${(useWordBoundary ? subString.substr(0, subString.lastIndexOf(' ')) : subString)} '\u2026'`;

See? isn't necessary to concatenate using + operator.

var greeting = "Hello";

var pleteGreeting = `${greeting} World!`;

console.log(pleteGreeting);
See? the string is built using the parameters within this expression: ${}

Resource

  • Template literals (String Interpolation)

Based on that rule's documentation it simply flags any string concatenation with +. Instead, it suggests using ES6 template literals. Here's how you'd do that in your code:

return `${(useWordBoundary ? subString.substr(0, subString.lastIndexOf(' ')) : subString)}\u2026`;

You can read more about template literals here: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Template_literals

Here is a different example using eslint error: Unexpected string concatenation | prefer-template:

(YES)

const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);

vs

(NO)

const value = '; ' + document.cookie;
const parts = value.split('; ' + name + '=');

In my case I found it useful for not only template related variables but also scripting variables as well, like: I was getting this error for a concatenation as it was like this:

   if ($('#' + data.id_field).children().length === 0) {
  $(e.target).prepend($(e.target), ContactWrapper);
}

So I fixed it by using like this:

    if ($(`#${data.id_field}`).children().length === 0) {
  $(e.target).prepend($(e.target), ContactWrapper);
}

Hope this example helps!

发布评论

评论列表(0)

  1. 暂无评论