microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.04k stars 12.49k forks source link

`\u` in String.raw #42887

Closed sugoroku-y closed 1 year ago

sugoroku-y commented 3 years ago

Bug Report

🔎 Search Terms

String.raw

🕗 Version & Regression Information

⏯ 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

DanielRosenwasser commented 3 years ago

@Kingwl you might be interested in this one.

ExE-Boss commented 3 years ago

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.

Kingwl commented 3 years ago

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:

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

ExE-Boss commented 3 years ago

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.

Kingwl commented 3 years ago

Ahhhh Okay. Got the point. Thanks.

elibarzilay commented 3 years ago

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

typescript-bot commented 2 years ago

: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: -

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.

bentongxyz commented 2 years ago

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

reverofevil commented 2 years ago

Almost created a duplicate to this issue.

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

reverofevil commented 2 years ago

@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.