Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad output when used as a template tag on strings that contain an escape sequence #33

Open
1 task done
noinkling opened this issue Mar 2, 2025 · 2 comments
Open
1 task done
Labels
enhancement New feature or request
Milestone

Comments

@noinkling
Copy link

Current behaviour

// Tab escape:

red(`foo\tbar`) // '\x1B[31mfoo\tbar\x1B[39m'
red`foo\tbar`   // '\x1B[31mfoo\\tbar\x1B[39m'

console.log(red(`foo\tbar`)) // foo	bar
console.log(red`foo\tbar`)   // foo\tbar

// Identity (pointless) escape:

red(`\p`) // '\x1B[31mp\x1B[39m'
red`\p`   // '\x1B[31m\\p\x1B[39m'

console.log(red(`\p`)) // p
console.log(red`\p`)   // \p

// Backslash escape:

red(`\\`) // '\x1B[31m\\\x1B[39m'
red`\\`   // '\x1B[31m\\\\\x1B[39m'

console.log(red(`\\`)) // \
console.log(red`\\`)   // \\

// Newline escape:

red(`foo\nbar`) // '\x1B[31mfoo\x1B[39m\n\x1B[31mbar\x1B[39m'
red`foo\nbar`   // '\x1B[31mfoo\x1B[39m\n\x1B[31mbar\x1B[39m' <- Hey it matches!

console.log(red(`foo\nbar`))
// foo
// bar
console.log(red`foo\nbar`)
// foo
// bar

// Backslash escape that happens to have an "n" after it:

red(`\\nice`) // '\x1B[31m\\nice\x1B[39m'
red`\\nice`   // '\x1B[31m\\\x1B[39m\n\x1B[31mice\x1B[39m' <- Oh no...

console.log(red(`\\nice`)) // \nice
console.log(red`\\nice`)
// \
// ice

Expected behaviour

Template tag result should be the same as the standard function call result.

Environment

  • library version: 3.16.0

Additional context

I believe this line is the problem:

? String.raw(arg, ...values).replace(/\\n/g, LF)

The "Motivation" section on the String.cooked ES proposal explains the issue: https://github.com/tc39/proposal-string-cooked?tab=readme-ov-file#motivation

In other words, I believe it can be fixed by changing to: String.raw({ raw: arg }, ...values) (without the newline replacement hack)

Appreciation for the useful project

  • After the problem is resolved, do not forget to give a star ⭐
@webdiscus webdiscus added the backlog This will be implemented later label Mar 2, 2025
@webdiscus webdiscus added this to the development milestone Mar 2, 2025
@webdiscus webdiscus added enhancement New feature or request and removed backlog This will be implemented later labels Mar 2, 2025
@webdiscus webdiscus modified the milestones: development, test Mar 2, 2025
@webdiscus
Copy link
Owner

Hello @noinkling,

In other words, I believe it can be fixed by changing to: String.raw({ raw: arg }, ...values) (without the newline replacement hack)

Thanks for the suggestion. I'll make tests and release a beta version.

@webdiscus
Copy link
Owner

@noinkling the v4.0.0-beta.1 is released with the support of escape sequences in template literals.
Can you please check it?

@webdiscus webdiscus modified the milestones: development, test Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants