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

"Preserving" indentation of "injected" strings based on the indentation of their injection point #83

Closed josephrocca closed 10 months ago

josephrocca commented 10 months ago

I'm guessing there's enough prior art here to know that what I want here is not a good idea, all things considered, but I have a use case that looks something like this:

let dedent = await import("https://unpkg.com/string-dedent@3.0.1/dist/dedent.mjs?module").then(m => m.default);
if(true) {
  let plants = dedent`
    plants
      cabbage
      fern
  `;
  console.log(plants);
  let result = dedent`
    life
      animals
        rabbit
        frog
      ${plants}
  `;
  console.log(result);
}

That outputs this:

plants
  cabbage
  fern

life
  animals
    rabbit
    frog
  plants
  cabbage
  fern

Whereas I'm trying to outputs this:

life
  animals
    rabbit
    frog
  plants
    cabbage
    fern

I'm wondering - if the "injected" string is indented, then isn't the dev's intention clearly that the injected string should "live at" that level of indentation?

I guess the key question is: Are there reasonable/common cases where the behaviour I desire here is counter-intuitive?

It's very possible that I'm blinded by my specific use case here. I'm posting this issue because this is basically the first time I've needed to use a dedent library (my use case is LLM prompt construction), and it's possible (though unlikely) that some "fresh eyes" on this might help break away from prior art / conventional wisdom in a way that actually makes sense in terms creating an intuitive developer experience.

josephrocca commented 10 months ago

Please feel free to close if this is actually an age-old discussion - i.e. something that has been firmly established to be a bad idea.

jridgewell commented 10 months ago

Closing this as a dupe of https://github.com/tc39/proposal-string-dedent/issues/25. When I last brought this up in the committee, they recognized this use case but think it can be addressed as a follow up after the core proposal.

josephrocca commented 10 months ago

@jridgewell Ah, sorry for missing that. Can I quickly ask: Does the committee think that the non-preserved-expression-indent mode is more intuitive as the default behavior?

mon-jai commented 10 months ago

Maybe it is better for having it enabled by default?

jridgewell commented 10 months ago

Does the committee think that the non-preserved-expression-indent mode is more intuitive as the default behavior?

Correct. Just having regular string concatenation is the least surprising behavior.