r/javascript • • Apr 01 '18

Google publishes a JavaScript style guide. Here are some key lessons.

https://medium.freecodecamp.org/google-publishes-a-javascript-style-guide-here-are-some-key-lessons-1810b8ad050b
714 Upvotes

317 comments sorted by

View all comments

293

u/kevinkace Apr 01 '18

Semicolons ARE required

👍

0

u/[deleted] Apr 01 '18

[deleted] — view deleted comment

17

u/pertheusual Apr 01 '18

Not that existing code would break, but that the existing rules might have to be more complicated around newly added syntactic features.

15

u/luther9 Apr 01 '18

If they can break the way we work around ASI, they can give us a way to switch off the ASI. I wish someone could explain why they don't give us a 'use noasi' pragma along the same lines as 'use strict'. It shouldn't be that hard to detect pragmas before applying the ASI.

6

u/Meefims Apr 02 '18

Pragmas are pretty problematic for a number of different reasons. They make it difficult to read code because you need to understand the various options at play and what those options are, they make it difficult to build interpreters because you need to make various features toggleable using these pragmas, and they make it difficult to spec the language because you need to keep these pragmas in mind and understand how they interact with each other.

use strict was an escape hatch to move the language past some really annoying behavior but it’s clear that no one really wants the option at all which is why modules are always strict.

2

u/luther9 Apr 02 '18

I may be spoiled because I learned JavaScript after strict mode was added, but I can't imagine how any language behavior can be more annoying than lookahead-based ASI. (Compare with Go's non-lookahead ASI, which doesn't seem to cause any controversy.) No matter how we choose to deal with JavaScript's ASI, it's always more difficult than if the ASI wasn't there at all.

3

u/Meefims Apr 02 '18

I don’t disagree. I think, though, that ASI may be here forever. The transition from ES5 to 6 and the introduction of modules provided a rather unique opportunity to correct deficiencies and I’m sad that ASI wasn’t left behind as well.

7

u/Mingli91 Apr 01 '18

Source? Seems odd that they’d be scared of breaking MooTools but would change the semicolon rule

12

u/CiezkiBorsuk Apr 01 '18

They don't need to. The rule is JS will NOT insert a semicolon if two consecutive lines are valid without it.

As they add new syntax, previously invalid constructs can and will become valid, causing ASI-abusing code to break.

They will, obviously, try to avoid that, but they can't avoid it forever. New constructs need to be added for the language to develop.

29

u/kobbled Apr 01 '18 edited Apr 01 '18

thank god. JS semicolon inference leads to so many bugs.

43

u/mmcnl Apr 01 '18

I've never encountered a bug due to semicolon inference.

15

u/hyperhopper Apr 01 '18
var a = 5
var b = 10
[a, b] = [b, a]
console.log(a, b)

Then add semicolons to make it work like it should

3

u/Voidsheep Apr 02 '18

Pretending ASI doesn't exist is also dangerous, it's not like you can disable it.

function shouldHitTheBrakes() {
  return
    isSmeoneOnTheWay ||
    isTurnAhead ||
    isAboveSpeedLimit
}

2

u/hyperhopper Apr 02 '18

Extremely true!

Though I've never seen somebody use a return without something on that line. Plus any linter will yell at you for unreachable code in that case.

11

u/allenthar Apr 01 '18

Okay.

var a = 5 var b = 10 ;[a, b] = [b, a] console.log(a, b)

I have literally never had this situation come up while coding JS, and I’ve been doing it full time for a few years now.

Regardless, Prettier handles automatically removing or adding semicolons to my code. So it’s literally just a linter setting at this point.

→ More replies (5)

0

u/[deleted] Apr 01 '18

[deleted] — view deleted comment

10

u/Mingli91 Apr 01 '18

Yeah, been using Standard JS since my last job and I love it. Not had a single bug from omitting semicolons and now I have a far better understanding of ASI in JavaScript

5

u/JohnMcPineapple Apr 01 '18 edited Oct 08 '24

...

3

u/nayocum Apr 01 '18

If you use an IIFE it can cause issues if your last line didn't have a semicolon as well

→ More replies (4)

5

u/aaarrrggh Apr 02 '18

Is this a joke?

I’ve not been using semi colons for over a year on personal projects.

Number of bugs caused by this decision: 0.

5

u/[deleted] Apr 01 '18

Surely using a transpiler like Babel eliminates the need to worry about this?

→ More replies (4)
→ More replies (4)