prefer-template

Require template literals instead of string concatenation

fixable

In "Hello, " + user.name + "!", quotes and plus signs cut the text into pieces, and a missing space at the edge of a piece is hard to see. A template literal keeps the text in one piece with the values in place.

A chain of + is reported if it mixes string literals with anything else. Joining only string literals is allowed, so you can still split a long string over two lines.

--fix rewrites the concatenation as a template literal.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"prefer-template" = "error" # or "warn"

Examples#

example.js2 problems
const greeting = "Hello, " + user.name + "!";
const url = base + "/users/" + id;
example.jsNo problems
const greeting = `Hello, ${user.name}!`;
const url = `${base}/users/${id}`;

const total = price + tax;
const notice = "This sentence is long, " +
  "so it is written as two strings.";