Eslint: Expected { after 'while' condition

I forked Formatting toolbar to add some very custom formatting stuff.

It has discourse-formatting-toolbar/formatting_bbcode.js at master · MonDiscourse/discourse-formatting-toolbar · GitHub

My code looks like this:

function replaceTitle(text) {
  while (
    text !==
    (text = text.replace(/\[title\](.+?)\[\/title\]/gi, function (match, p) {
      return `<div class="job-title">${p}</div>`;
    }))
  );
  return text;
}

But eslint complains:

    9:3  error  Expected { after 'while' condition  curly

I tried moving that while and stuff after it onto a single line (the formatting here is applied by vscode and usually works with the other plugins I manage), but that didn’t help either.

Can someone provide me with a clue?

EDIT: Well, it turns out that I don’t actually need that function, but I would still like to know what the problem is.

1 Like

The reason for the eslint warning is that you need curly braces surrounding your return text;

That is, it should look like so:

  while (
    text !==
    (text = text.replace(/\[title\](.+?)\[\/title\]/gi, function (match, p) {
      return `<div class="job-title">${p}</div>`;
    }))
  ) {
    return text;
  }

Although JavaScript allows for the omission of the curly braces, ESLint enforces the consistent curly brace style.

More details on the curly rule here:

3 Likes