Skip to content

(tpl)

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

• Identifier: shorthand for (str)
• String: shorthand for (str)
• Pattern: shorthand for (str)
(str)
(:into) (:and) (:or) (:not) (:text) (:kind) (:replace) (:capture)

expr

(arr) (bin) (bool) (call) (comp) (fun) (id) (id+) (jsx) (mem) (null) (num) (obj) (of) (regex) (regex+) (str) (str*) (str+) (tpl) (tpl*) (:into) (:and) (:or) (:not) (:text) (:kind) (:replace) (:capture)

tag

• Identifier: shorthand for (id)
• Pattern: shorthand for (id)
(:kind) (:ref) (:text) (arr) (attr) (bool) (call) (child) (comp) (export) (fun) (id) (id+) (import) (jsx) (mem) (null) (num) (obj) (of) (prop) (regex) (regex+) (str) (str*) (str+) (tpl) (tpl*) (var) (:into) (:and) (:or) (:not) (:replace) (:capture)

Copyright © 2022-present Semantic Works, Inc.