Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tagged template string considered an expression, not a function call #822

Closed
yoshuawuyts opened this Issue Mar 16, 2017 · 7 comments

Comments

3 participants
@yoshuawuyts
Copy link
Contributor

yoshuawuyts commented Mar 16, 2017

Intro

Template strings are a way to interpolate variables with strings. It's nice, and often more readable than traditional string concatenation

Tagged template strings take this behavior a bit further, and allow functions to be called as if they were strings - but instead the functions receive multiple strings, and a trailing array of positional arguments that the have been used for interpolation. This allows operating on the string and adding all sorts of interesting behavior.

Example

var css = require('sheetify')

// this is a static expression
`
  .js-yellow { color: #f7df1e }
  .bg-js-yellow { background-color: #f7df1e }
`

// this is a function call, which in this case has side effects
css`
  .js-yellow { color: #f7df1e }
  .bg-js-yellow { background-color: #f7df1e }
`

Bug

It appears that standard@9 has introduced a regression where tagged template strings are now considered static expressions - even though they're functions. I think we should probably look into this.

❯ standard
standard: Use JavaScript Standard Style (http://standardjs.com)
  /Users/anon/src/yoshuawuyts/berlinjs/index.js:14:1: Expected an assignm
ent or function call and instead saw an expression.

Thanks!

@yoshuawuyts yoshuawuyts added the bug label Mar 16, 2017

@feross

This comment has been minimized.

Copy link
Member

feross commented Mar 17, 2017

Thanks for the issue, @yoshuawuyts. This sounds like a bug in ESLint's no-unused-expressions rule. I'll open an issue with them.

@yoshuawuyts

This comment has been minimized.

Copy link
Contributor Author

yoshuawuyts commented Mar 17, 2017

@feross

This comment has been minimized.

Copy link
Member

feross commented Mar 17, 2017

Issue filed here: eslint/eslint#8268

@feross feross added the blocked label Mar 17, 2017

@feross

This comment has been minimized.

Copy link
Member

feross commented Mar 17, 2017

They actually already have a PR out that adds an option to ignore tagged template literals: eslint/eslint#8253 When that makes it into a release, I'll add the option and publish a new version on the v9 and v10 lines.

@vitorbal

This comment has been minimized.

Copy link

vitorbal commented Mar 17, 2017

@feross FYI, today's release will most likely contain this fix!

@feross

This comment has been minimized.

Copy link
Member

feross commented Mar 17, 2017

@vitorbal Excellent! Just saw that. I'll do a standard release as soon as the ESLint release is out. 👍

feross added a commit to standard/eslint-config-standard that referenced this issue Mar 18, 2017

Relax rule: Allow tagged template string expressions (no-unused-expre…
…ssions)

Fixes: standard/standard#822

Fixes "Expected an assignment or function call and instead saw an
expression." error when using a standalone tagged template string like:

```js
var css = require('sheetify')

css`
  .js-yellow { color: #f7df1e }
  .bg-js-yellow { background-color: #f7df1e }
`
```
@feross

This comment has been minimized.

Copy link
Member

feross commented Mar 18, 2017

Fix published as 9.0.2.

(Also, for users of the v10 beta, published as 10.0.0-beta.2.)

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.