Skip to content

(tpl) - JavaScript

template literals (`...`) and tagged template literals like html... or sql...

clojure
(tpl quasis? expr? tag?)

Where:

  1. quasis: the literal string fragments (the hello and ! in `hello ${name}!`) *
  2. expr: the interpolation expressions (the userId in `${userId}`) *
  3. tag: the tag function (the html in html`...`) *

Matching:

  1. template literals, e.g. `hello ${name}`
  2. tagged template literals, e.g. html`<div>${content}</div>`

Behavior

  • Empty templates like are matched.

Examples

Audit SQL tagged templates before moving to parameterized queries.

clojure
(tpl /select|insert|update|delete/i _ sql)

Selects in lines { 1, 2 } but not in { 3 }:

typescript
const q1 = sql`SELECT * FROM users WHERE id = ${userId}`
const q2 = sql`UPDATE users SET active = 1`
const q3 = `SELECT * FROM users`

Find URLs built with userId interpolation to migrate to a URL builder.

clojure
(tpl /\/users\// (id userId))

Selects in lines { 1 } but not in { 2, 3 }:

typescript
const url = `/users/${userId}`
const url2 = `/users/${otherId}`
const url3 = `/users/42`

Clean up CSS-in-JS templates that still use !important.

clojure
(tpl /!important/ _ css)

Selects in lines { 0 } but not in { 0, 0 }:

typescript
const button = css`
  color: red !important;
`
const card = css`
  color: blue;
`
const other = styled.div`
  color: red !important;
`

Arguments

quasis

  1. Identifier: shorthand for (str)
  2. String: shorthand for (str)
  3. Pattern: shorthand for (str)
  4. (str)
  5. (:into)
  6. (:nearest)
  7. (:and)
  8. (:or)
  9. (:not)
  10. (:text)
  11. (:kind)
  12. (:replace)
  13. (:replace-in)
  14. (:capture)

expr

tag

Copyright © 2022-present Semantic Works, Inc.