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

Reconsider syntax proposal #76

Open hax opened 1 year ago

hax commented 1 year ago

I suggest we reconsider syntax option because with syntax, we just solve #75.

There are also other reasons, copy from original issue:

One problem of current template literal is, you can't have unescaped ` in the literal (it always end the literal). This is very inconvenient for text contains backticks, eg. markdown.

This issue is not directly relate to dedent, but consider the main use cases of dedent is long text, I really hope we can revisit the syntax options which could allow unescaped backticks.

Syntax also make String.dedent(tag) unnecessary. I feel String.dedent(tag) is a little bit hard to understand for average programmers, and String.dedent(obj.foo) may not work. (need String.dedent(obj.foo.bind(obj)).)

Originally posted by @hax in https://github.com/tc39/proposal-string-dedent/issues/40#issuecomment-1188570725

bakkot commented 1 year ago

I don't think the question of how to handle caching is significant enough to warrant revisiting syntax. It's really only relevant in an edge case which most people will never encounter.

wan2land commented 1 year ago

With MySQL, the following situations are common.

const query = `
select *
from \`users\`
where \`name\` = ?
`

If implemented via String.dedent, this problem is repeated.

In the "In other languages" section of the README, the grammar(heredoc) is implemented separately from the default string("") for other languages because it also includes these escape issues.

sken130 commented 1 year ago

If we are to introduce new syntax, we should make it worthwhile by including more features and solving as many problems as possible, otherwise the features proposed too late will become breaking changes in the future.

Here is my proposal of adding raw string literal to ECMAScript/JavaScript to solve multiple problems together:

https://es.discourse.group/t/raw-string-literals-that-can-contain-any-arbitrary-text-without-the-need-for-special-escape-sequences/1757/3

(Mentioned in #40 as well)

dimensia commented 1 year ago

Does this feature compose with other tagged template literals? e.g.:

  const mySql = sql`
    SELECT *
      FROM myTable...
  `;  

If not please reconsider a syntax-based solution that will work with existing tagged-template literals.

bakkot commented 1 year ago

Does this feature compose with other tagged template literals?

Yes:

const mySql = String.dedent(sql)`
  SELECT *
    FROM myTable...
`;  
hax commented 1 year ago

@sken130 I really like your proposal, but you need a champion (TC39 delegate) to bring it to the committee. 😉

hax commented 1 year ago

@bakkot What if we want compose more? For example, also compose String.raw?

bakkot commented 1 year ago

@hax You already can't compose String.raw with another template tag. This won't change that either way. (Besides, there's no reason to - template tags can look at the raw component themselves, if they want to, so there's no need to use String.raw if you're using another template tag already.)

hax commented 7 months ago

Hi everyone, I plan to write a raw string syntax proposal (maybe based on @sken130 work) and present to the committee for stage 1, if I can catch the deadline of next meeting.

Besides the use cases this issue or @sken130 's discourse post already mentioned, the main reason I decide to champion it myself is because I feel really pain when writing LLM code in JS --- if you want to write prompt in markdown or contains JS code directly in the prompts.

I will update this issue when the proposal repo ready.

hax commented 7 months ago

Please check the repo: https://github.com/hax/proposal-raw-string-literals

@sken130 Feel free to add your real name to authors if you wish.