decaf icon indicating copy to clipboard operation
decaf copied to clipboard

Template strings

Open juliankrispel opened this issue 9 years ago • 10 comments

Missing feature!

Template strings like this:

bom = 'yoyoyo #{dwq}'

should transpile to

bom = `yoyoyo ${dwq}`;

juliankrispel avatar Jan 25 '16 16:01 juliankrispel

So the problem with this one is that coffeescript has multiple compile steps.

Some of the compilation (like the compiling of strings) is actually done inside the lexer -_-.

By the time coffeescript gives me the AST, the string "hello #{world}" will already have been turned into "hello " + world by the lexer.

There are two solutions to this issue:

1 - Monkey patching coffee script (which would mean a massive extra file). 2 - A decaf-specific fork of the coffee-script compiler with that part disabled. 3 - Turning #{} into ${} so that the coffeescript lexer ignores it and later converting double-quote strings with ${} to backtick strings.

Any thoughts @flying-sheep @eventualbuddha ?

juliankrispel avatar Jan 30 '16 05:01 juliankrispel

or 4 - working together with @jashkenas to sort this out.

we’d reopen jashkenas/coffeescript#2365 for single line comments, and help fixing jashkenas/coffeescript#4192

flying-sheep avatar Jan 30 '16 11:01 flying-sheep

Another idea: Automatically output ES6 template strings instead of string concatenation, regardless of whether the CoffeeScript input was "#{a} and #{b}" or a + ' and ' + b. Pros: No changes to CoffeeScript needed. Automatic code improvement. Cons: Dunno?

lydell avatar Jan 30 '16 12:01 lydell

sure! why didn’t i think of this? :smiley:

flying-sheep avatar Jan 30 '16 12:01 flying-sheep

I would vote for detecting string additions that actually were originally string interpolations. This can be done by looking at the unrewritten tokens for the node (decaffeinate-parser does that). I'm afk so can't provide details now.

Also, esnext will automatically turn string additions into interpolations as you suggest. You don't want to do it with a + "foo" because a could be a number.

eventualbuddha avatar Jan 30 '16 18:01 eventualbuddha

You don't want to do it with a + "foo" because a could be a number.

1 + "foo" === "1foo"

lydell avatar Jan 30 '16 18:01 lydell

True… so you'd just have to be careful that you don't do it when two non-strings could be added, like a + b + "c".

eventualbuddha avatar Jan 30 '16 21:01 eventualbuddha

well, that could be translated to

`${a + b}c`

algo idea:

  1. given a sequence of additions containing string literals
  2. collect the addition sequence of all leading non-string-literals and put them in a leading substitution if there are any (${})
  3. proceed as normal
a + b + "c" + d + e + "f" + g

is left-associative so the "c" + d + e is ("c" + d) + e.

flying-sheep avatar Jan 30 '16 21:01 flying-sheep

Honestly I'd like to preserve the code authors intention as much as possible. I thought about turning all string concatenation into template strings but I'm not convinced that it better serves peoples use-case or simplifies this problem.

The purpose of decaf is first and foremost not a code-improvement tool but a code-translation tool. I'd like to stick with the original intent here.

juliankrispel avatar Feb 01 '16 03:02 juliankrispel

sure. now that we can change the CS AST upstream, that’s also no longer a problem

flying-sheep avatar Feb 01 '16 08:02 flying-sheep