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

Feedback #85

Open daniel-nagy opened 3 months ago

daniel-nagy commented 3 months ago

I started a list of potential ECMAScript proposals and this was on my list 😄.

I think the README does a pretty good job of explaining why this feature is necessary. However, I had a different syntax in mind. Have you considered using triple back ticks? e.g.

class MyClass {
  print() {
    console.log(```
      create table student(
        id int primary key,
        name text
      )
    ```);
  }
}

I think this would behave like multiline strings in Elixir. Sepcifically, the indentation of the last ``` would determine where the indentation starts. I call these lexically indented template literals.

Maybe I'm wrong, but it doesn't seem like this proposal would support the case where you want all lines to be indented by some amount. Specifically, this criteria

e.g.

class MyClass {
  print() {
    string.indent`
      create table student(
        id int primary key,
        name text
      )
    `;
  }
}

would result in

create table student(
  id int primary key,
  name text
)

but maybe I acutally want the starting indentation

  create table student(
    id int primary key,
    name text
  )

With this proposal, that doesn't seem possible.

Another benefit of triple back ticks, in my opinion, is that they are more ergonomic with tagged template literals. I can lift any existing tagged template literal by simply replacing single backticks with triple back ticks, e.g.

const query = String.dedent(sql)`
  create table student(
    id int primary key,
    name text
  )
`

vs

const query = sql```
  create table student(
    id int primary key,
    name text
  )


What do you think?
bakkot commented 3 months ago

Have you considered using triple back ticks?

Yes, please see existing issues. Note that the code you've given is in fact already legal and so cannot be used.

Maybe I'm wrong, but it doesn't seem like this proposal would support the case where you want all lines to be indented by some amount.

True, this proposal aims to solve dedenting, not indenting.

daniel-nagy commented 3 months ago

True, this proposal aims to solve dedenting, not indenting.

Wouldn't it be better if it solved both?

bakkot commented 3 months ago

Depends on what additional complexity is required to do so, and how valuable we think doing so is. Personally I don't think indenting is very valuable so I am not inclined to incur any additional complexity to enable it.

daniel-nagy commented 3 months ago

Personally I don't think indenting is very valuable so I am not inclined to incur any additional complexity to enable it.

I have run into use cases where I needed to control indenting.

daniel-nagy commented 3 months ago

Note that the code you've given is in fact already legal and so cannot be used.

Can you elaborate more on this? When I test the code I provided I get an error

Uncaught TypeError: "" is not a function

Even if the syntax is lexically valid, I don't think it has any semantic meaning.

bakkot commented 3 months ago

See previous issues.

daniel-nagy commented 3 months ago

Oof, ok