-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Some tweaks for backtick strings #45099
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tests/baselines/reference/templateLiteralsInTypes.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
tests/cases/compiler/templateLiteralsInTypes.ts(3,1): error TS2554: Expected 2 arguments, but got 1. | ||
tests/cases/compiler/templateLiteralsInTypes.ts(3,8): error TS2339: Property 'foo' does not exist on type '`${string}:\t${number}\r\n`'. | ||
|
||
|
||
==== tests/cases/compiler/templateLiteralsInTypes.ts (2 errors) ==== | ||
const f = (hdr: string, val: number) => `${hdr}:\t${val}\r\n` as `${string}:\t${number}\r\n`; | ||
|
||
f("x").foo; | ||
~~~~~~ | ||
!!! error TS2554: Expected 2 arguments, but got 1. | ||
!!! related TS6210 tests/cases/compiler/templateLiteralsInTypes.ts:1:25: An argument for 'val' was not provided. | ||
~~~ | ||
!!! error TS2339: Property 'foo' does not exist on type '`${string}:\t${number}\r\n`'. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//// [templateLiteralsInTypes.ts] | ||
const f = (hdr: string, val: number) => `${hdr}:\t${val}\r\n` as `${string}:\t${number}\r\n`; | ||
|
||
f("x").foo; | ||
|
||
|
||
//// [templateLiteralsInTypes.js] | ||
"use strict"; | ||
var f = function (hdr, val) { return hdr + ":\t" + val + "\r\n"; }; | ||
f("x").foo; | ||
|
||
|
||
//// [templateLiteralsInTypes.d.ts] | ||
declare const f: (hdr: string, val: number) => `${string}:\t${number}\r\n`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
=== tests/cases/compiler/templateLiteralsInTypes.ts === | ||
const f = (hdr: string, val: number) => `${hdr}:\t${val}\r\n` as `${string}:\t${number}\r\n`; | ||
>f : Symbol(f, Decl(templateLiteralsInTypes.ts, 0, 5)) | ||
>hdr : Symbol(hdr, Decl(templateLiteralsInTypes.ts, 0, 11)) | ||
>val : Symbol(val, Decl(templateLiteralsInTypes.ts, 0, 23)) | ||
>hdr : Symbol(hdr, Decl(templateLiteralsInTypes.ts, 0, 11)) | ||
>val : Symbol(val, Decl(templateLiteralsInTypes.ts, 0, 23)) | ||
|
||
f("x").foo; | ||
>f : Symbol(f, Decl(templateLiteralsInTypes.ts, 0, 5)) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
=== tests/cases/compiler/templateLiteralsInTypes.ts === | ||
const f = (hdr: string, val: number) => `${hdr}:\t${val}\r\n` as `${string}:\t${number}\r\n`; | ||
>f : (hdr: string, val: number) => `${string}:\t${number}\r\n` | ||
>(hdr: string, val: number) => `${hdr}:\t${val}\r\n` as `${string}:\t${number}\r\n` : (hdr: string, val: number) => `${string}:\t${number}\r\n` | ||
>hdr : string | ||
>val : number | ||
>`${hdr}:\t${val}\r\n` as `${string}:\t${number}\r\n` : `${string}:\t${number}\r\n` | ||
>`${hdr}:\t${val}\r\n` : `${string}:\t${number}\r\n` | ||
>hdr : string | ||
>val : number | ||
|
||
f("x").foo; | ||
>f("x").foo : any | ||
>f("x") : `${string}:\t${number}\r\n` | ||
>f : (hdr: string, val: number) => `${string}:\t${number}\r\n` | ||
>"x" : "x" | ||
>foo : any | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// @strict: true | ||
// @declaration: true | ||
|
||
const f = (hdr: string, val: number) => `${hdr}:\t${val}\r\n` as `${string}:\t${number}\r\n`; | ||
|
||
f("x").foo; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test for this change? I'm unclear on why we wouldn't want to preserve these.
backtickQuoteEsacpedCharsRegExp
is only used when the quote character isCharacterCodes.backtick
, which mean's we're preserving the raw text of the template string as much as possible, so it seems like adding extra escapes would break anyone using the raw strings.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll see if I can add a test for this.
Re the reason, I listed two reasons in the commit log:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I can see that being a possible issue, however I'm still not sure
"<TAB>foo"
should become`\tfoo`
since it is still semantically different between the raw and cooked forms. There are only two ways to get to this code path: 1) Via a user-defined transformation, in which case we would want to preserve what was written, or 2) via one of our code fixes. If we want to normalize TAB or FF in the code fix, that's fine, but I'm concerned whether this would affect anyone using custom transforms. This is a small enough corner case its probably reasonable however, and we can address it if there's feedback.StringLiteral can't contain a raw CRLF unless its part of a LineContinuation, which becomes an empty code sequence, per NOTE 2 of https://tc39.es/ecma262/#sec-literals-string-literals and in https://tc39.es/ecma262/#sec-static-semantics-sv, so that wouldn't matter in a StringLiteral → TemplateLiteral conversion. The escape sequence
\r\n
in a string literal should remain as the escape sequence\r\n
in the template literal, but we shouldn't just convert a raw CRLF into the escape sequence as that could impact custom transforms (which I could see taking a reliance on the CRLF->LF normalization of template literals).So, -1 on converting CRLF into the escaped form, +1 on converting other characters (at least until someone raises an issue).