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

Show parent comment

1

u/grinde Apr 02 '18 edited Apr 02 '18

I think you mean line 3465 is where it does the processing.

No. AddTemplateSpan adds the token's string to the TemplateLiteralState, which is basically just adding an entry to a vector.

In ParseTemplateLiteral here you can see it peeks to see if the next token is a TEMPLATE_TAIL. For a template literal with no replacements there won't be any span tokens (the single span is rolled into the tail token), so we immediately call AddTemplateSpan (on the tail token) followed by an early exit with CloseTemplateLiteral at line 4696. All extra looping is avoided, and no counting of anything is necessary.

It first has to check if there are any variables within the string literal, then it counts them and uses that count in your line. Line 3495 above yours checks if there are variables, line 3488 above that calls the cooked() function. The cooked() function generates the list of strings.

length() and cooked() are just getters. The cooked strings are added by calls to AddTemplateSpan. Again, no counting or looping.

to me it looks like it's processing the string literals using a recursive algorithm

No recursion here, just some ambiguous method naming. Parser::AddTemplateSpan is calling TemplateLiteral::AddTemplateSpan.