TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

`\u` in String.raw

Open sugoroku-y opened this issue 3 years ago • 10 comments

Bug Report

🔎 Search Terms

String.raw

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about String.raw

⏯ Playground Link

Playground link with relevant code

💻 Code

String.raw`adc\def\ghi\jkl\mno\pqr\st\uvw\xyz`

🙁 Actual behavior

Error reported: error TS1125: Hexadecimal digit expected.

🙂 Expected behavior

Complete transpile

sugoroku-y avatar Feb 19 '21 20:02 sugoroku-y

@Kingwl you might be interested in this one.

DanielRosenwasser avatar Feb 20 '21 00:02 DanielRosenwasser

It’s not just String.raw, this applies to all tagged template literals, where the top‑level TemplateStringArray contains undefined for an unknown escape, so this should also update TemplateStringsArray:

const taggedTemplate = (template: TemplateStringsArray, ...substitutions: unknown[]) => {
	console.log(template, substitutions);
}

// Logs:
// [
// 	undefined, "bar", undefined,
// 	raw: [ "foo\\uvar", "bar", "baz\\u" ]
// ]
// [ 123, 456 ]
taggedTemplate`foo\uvar${123}bar${456}baz\u`;

Workbench Repro


Also, this doesn’t occur when the \u follows a substitution.

ExE-Boss avatar Feb 20 '21 05:02 ExE-Boss

It’s not just String.raw, this applies to all tagged template literals

Sorry I'm not 100% sure what your means. But seems the runtime behavior is as expected.

image

The current problem is falsely alarm.

Here's some context:

  • https://github.com/microsoft/TypeScript/pull/23801
  • https://tc39.github.io/proposal-template-literal-revision/

And I have found an PR who try to fix a part of the alarm: https://github.com/microsoft/TypeScript/pull/41030

Kingwl avatar Feb 20 '21 05:02 Kingwl

The definition of TemplateStringArray should include undefined in the top‑level index signature, which should be fixed alongside this to correctly describe the runtime behaviour as of https://github.com/tc39/ecma262/commit/138c9efc5460c22e175007ba2608b2c4238e11ef.

ExE-Boss avatar Feb 20 '21 06:02 ExE-Boss

Ahhhh Okay. Got the point. Thanks.

Kingwl avatar Feb 20 '21 06:02 Kingwl

I think that dealing with octal escapes in all strings #396 should be done before dealing with tagged templates as an exception...

elibarzilay avatar Jun 03 '21 05:06 elibarzilay

:wave: Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of this repro running against the nightly TypeScript.


Comment by @ExE-Boss

:x: Failed: -

  • Hexadecimal digit expected.
Historical Information
Version Reproduction Outputs
4.2.2, 4.3.2, 4.4.2, 4.5.2, 4.6.2

:x: Failed: -

  • Hexadecimal digit expected.

typescript-bot avatar Apr 14 '22 00:04 typescript-bot

Hi, is this issue being worked on? I would like to look into this as a first issue.

bentongxyz avatar Apr 28 '22 12:04 bentongxyz

Almost created a duplicate to this issue.

The proposal was here, normative section in ECMA-262 here.

reverofevil avatar May 22 '22 13:05 reverofevil

@ExE-Boss

The definition of TemplateStringArray should include undefined in the top‑level index signature,

I'm afraid such a change will break existing code. undefined is there only if a part of string template contains invalid escape characters, and this information is available in compile-time. I'd suggest replacing TemplateStringsArray with a readonly tuple of strings and undefineds, intersected with {raw: ...}. of equal-sized tuple of strings. This type should be backwards compatible.

reverofevil avatar May 22 '22 14:05 reverofevil