Skip to main content

js/useTemplate

require template literals instead of string concatenation

ESLint Equivalent: prefer-template

Examples

Invalid

const foo = 'bar'; console.log(foo + 'baz')

 file.ts:1:31 lint/js/useTemplate  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Template literals are preferred over string concatenation.

    const foo = 'bar'; console.log(foo + 'baz')
                                   ^^^^^^^^^^^

  Safe fix

  - foo·+·"baz"
  + `${foo}baz`


console.log((1 * 2) + 'foo')

 file.ts:1:12 lint/js/useTemplate  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Template literals are preferred over string concatenation.

    console.log((1 * 2) + 'foo')
                ^^^^^^^^^^^^^^^

  Safe fix

  - 1·*·2·+·"foo"
  + `${1·*·2}foo`


console.log(1 + 'foo' + 2 + 'bar' + 'baz' + 3)

 file.ts:1:12 lint/js/useTemplate  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Template literals are preferred over string concatenation.

    console.log(1 + 'foo' + 2 + 'bar' + 'baz' + 3)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  Safe fix

  - 1·+·"foo"·+·2·+·"bar"·+·"baz"·+·3
  + `${1}foo${2}barbaz${3}`


console.log((1 + 'foo') * 2)

 file.ts:1:13 lint/js/useTemplate  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Template literals are preferred over string concatenation.

    console.log((1 + 'foo') * 2)
                 ^^^^^^^^^

  Safe fix

  - 1·+·"foo"
  + `${1}foo`


console.log((1 * (2 + 'foo')) + 'bar')

 file.ts:1:12 lint/js/useTemplate  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Template literals are preferred over string concatenation.

    console.log((1 * (2 + 'foo')) + 'bar')
                ^^^^^^^^^^^^^^^^^^^^^^^^^

  Safe fix

  - 1·*·(2·+·"foo")·+·"bar"
  + `${1·*·(2·+·"foo")}bar`

 file.ts:1:18 lint/js/useTemplate  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Template literals are preferred over string concatenation.

    console.log((1 * (2 + 'foo')) + 'bar')
                      ^^^^^^^^^

  Safe fix

  - 2·+·"foo"
  + `${2}foo`


console.log('foo' + 1)

 file.ts:1:12 lint/js/useTemplate  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Template literals are preferred over string concatenation.

    console.log('foo' + 1)
                ^^^^^^^^^

  Safe fix

  - "foo"·+·1
  + `foo${1}`


console.log('foo' + `bar${`baz${'bat' + 'bam'}`}` + 'boo')

 file.ts:1:12 lint/js/useTemplate  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Template literals are preferred over string concatenation.

    console.log('foo' + `bar${`baz${'bat' + 'bam'}`}` + 'boo')
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  Safe fix

  - "foo"·+·`bar${`baz${"bat"·+·"bam"}`}`·+·"boo"
  + `foobarbaz${"bat"·+·"bam"}boo`


console.log('foo' + 1 + 2)

 file.ts:1:12 lint/js/useTemplate  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Template literals are preferred over string concatenation.

    console.log('foo' + 1 + 2)
                ^^^^^^^^^^^^^

  Safe fix

  - "foo"·+·1·+·2
  + `foo${1}${2}`

Valid

console.log('foo' + 'bar')
console.log(foo() + '\n')