Closed Bug 1283886 Opened 4 years ago Closed 4 years ago

[ESLint] Allow template literals

Categories

(DevTools :: General, defect)

defect
Not set

Tracking

(firefox50 fixed)

RESOLVED FIXED
Firefox 50
Tracking Status
firefox50 --- fixed

People

(Reporter: jryans, Assigned: jryans)

References

Details

Attachments

(1 file)

When building multi-line messages, it's nice to be able to use template literals so you don't alternate quote styles depending on whether substitution is used.
Comment on attachment 8767246 [details]
Bug 1283886 - Allow template literals.

https://reviewboard.mozilla.org/r/61828/#review58612

Thank you.
This seems like a reasonable change to me; and I was under the impression we were already using these anyhow.
Attachment #8767246 - Flags: review?(ttromey) → review+
Pushed by jryans@gmail.com:
https://hg.mozilla.org/integration/fx-team/rev/20d8f274cc88
Allow template literals. r=tromey
https://hg.mozilla.org/mozilla-central/rev/20d8f274cc88
Status: ASSIGNED → RESOLVED
Closed: 4 years ago
Resolution: --- → FIXED
Target Milestone: --- → Firefox 50
Looks like this change accidentally allows single-line template strings in unnecessary situations such as: `foo` or `bar` when "foo" or "bar" would work better. This makes the source code more inconsistent, and while the impact is not noticeable, using template strings is a bit slower perf-wise, because it expects some parsing work to be done.

:jryans, any chance we can make the rule more strict ?
Flags: needinfo?(jryans)
(In reply to Tim Nguyen :ntim (use needinfo?) from comment #5)
> Looks like this change accidentally allows single-line template strings in
> unnecessary situations such as: `foo` or `bar` when "foo" or "bar" would
> work better. This makes the source code more inconsistent, and while the
> impact is not noticeable, using template strings is a bit slower perf-wise,
> because it expects some parsing work to be done.
> 
> :jryans, any chance we can make the rule more strict ?

What do you have in mind?  I am not aware of an existing ESLint rule configuration that would reject:

var bob = `Hello`;

while allowing:

var bob = `Hello ${name} ` +
          `it's nice to meet you`;

How common are these cases of standalone template strings?
Flags: needinfo?(jryans) → needinfo?(ntim.bugs)
(In reply to Tim Nguyen :ntim (use needinfo?) from comment #5)
> This makes the source code more inconsistent, and while the
> impact is not noticeable, using template strings is a bit slower perf-wise,
> because it expects some parsing work to be done.

First, I agree with the consistency argument.
Also if I see a backquote, I expect to see some substitution (or a newline), so it's extra
work while reading the code, looking for something that isn't there.

However, I was curious about the perf impact, so I looked.
Two functions that differ only in their quoting style compile to the same code:

js> function f1() { return `hello`; }
js> function f2() { return "hello"; } 
js> dis(f1)
flags: CONSTRUCTOR
loc     op
-----   --
main:
00000:  string "hello"
00005:  return
00006:  retrval

Source notes:
 ofs line    pc  delta desc     args
---- ---- ----- ------ -------- ------
  0:    1     0 [   0] colspan 16
  2:    1     5 [   5] colspan 16

js> dis(f2)
flags: CONSTRUCTOR
loc     op
-----   --
main:
00000:  string "hello"
00005:  return
00006:  retrval

Source notes:
 ofs line    pc  delta desc     args
---- ---- ----- ------ -------- ------
  0:    2     0 [   0] colspan 16
  2:    2     5 [   5] colspan 16
(In reply to Tom Tromey :tromey from comment #7)
> (In reply to Tim Nguyen :ntim (use needinfo?) from comment #5)
> > This makes the source code more inconsistent, and while the
> > impact is not noticeable, using template strings is a bit slower perf-wise,
> > because it expects some parsing work to be done.
> 
> First, I agree with the consistency argument.
> Also if I see a backquote, I expect to see some substitution (or a newline),
> so it's extra
> work while reading the code, looking for something that isn't there.
Yes I agree.

> However, I was curious about the perf impact, so I looked.
> Two functions that differ only in their quoting style compile to the same
> code:
> 
> js> function f1() { return `hello`; }
> js> function f2() { return "hello"; } 
> js> dis(f1)
> flags: CONSTRUCTOR
> loc     op
> -----   --
> main:
> 00000:  string "hello"
> 00005:  return
> 00006:  retrval
> 
> Source notes:
>  ofs line    pc  delta desc     args
> ---- ---- ----- ------ -------- ------
>   0:    1     0 [   0] colspan 16
>   2:    1     5 [   5] colspan 16
> 
> js> dis(f2)
> flags: CONSTRUCTOR
> loc     op
> -----   --
> main:
> 00000:  string "hello"
> 00005:  return
> 00006:  retrval
> 
> Source notes:
>  ofs line    pc  delta desc     args
> ---- ---- ----- ------ -------- ------
>   0:    2     0 [   0] colspan 16
>   2:    2     5 [   5] colspan 16

Interesting. I've read somewhere that template strings were slower, but maybe it was a comparison between `foo${bar}` and "foo" + bar.
Flags: needinfo?(ntim.bugs)
Product: Firefox → DevTools
You need to log in before you can comment on or make changes to this bug.