tc39 / proposal-string-dedent

TC39 Proposal to remove common leading indentation from multiline template strings
https://tc39.es/proposal-string-dedent/
MIT License
625 stars 17 forks source link

What happens to the final linebreak? #43

Closed bakkot closed 2 years ago

bakkot commented 2 years ago

Consider

String.dedent(`
  x
`);

What string does that end up being? Is it "x" or "x\n"? (Or I guess `"\nx\n"?)

The readme says "The closing line's preceding literal newline char is ignored." but I'm not sure what "ignored" means.

jridgewell commented 2 years ago

This outputs just "x".

jridgewell commented 2 years ago

Clarified this to say the newline is "removed" instead of "ignored".

ljharb commented 2 years ago

hmm, i'm not sure if that's desired behavior. The presence or absence of trailing newlines matter, especially for file contents (altho all files should always have trailing newlines, sometimes they don't, on purpose)

jridgewell commented 2 years ago

You can add leading and trailing newlines, but the opening and closing lines are removed. Only "content lines" remain, which may be empty:

String.dedent`

··Lines before and after remain in the output.

`;
^
^
^Lines before and after remain in the output.
^
^

(Using ^ to represent a line, · to represent leading whitespace)

ljharb commented 2 years ago

ah i guess in the literal form that's fine, because i know where i put a newline