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

Preserve string delimiter in type printing. #60729

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ import {
isStatic,
isString,
isStringANonContextualKeyword,
isStringDoubleQuoted,
isStringLiteral,
isStringLiteralLike,
isStringOrNumericLiteralLike,
Expand Down Expand Up @@ -6309,8 +6310,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
* It also calls `setOriginalNode` to setup a `.original` pointer, since you basically *always* want these in the node builder.
*/
function setTextRange<T extends Node>(context: NodeBuilderContext, range: T, location: Node | undefined): T {
if (!nodeIsSynthesized(range) || !(range.flags & NodeFlags.Synthesized) || !context.enclosingFile || context.enclosingFile !== getSourceFileOfNode(getOriginalNode(range))) {
range = factory.cloneNode(range); // if `range` is synthesized or originates in another file, copy it so it definitely has synthetic positions
const nodeSourceFile = getSourceFileOfNode(getOriginalNode(range));
if (!nodeIsSynthesized(range) || !(range.flags & NodeFlags.Synthesized) || !context.enclosingFile || context.enclosingFile !== nodeSourceFile) {
if (range.kind === SyntaxKind.StringLiteral) {
const stringLiteral = range as Node as StringLiteral;
range = factory.createStringLiteral(stringLiteral.text, !!nodeSourceFile && !isStringDoubleQuoted(stringLiteral, nodeSourceFile)) as Node as T;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could instead use factory.createStringLiteralFromNode(stringLiteral) as that should pull the string's text from its original file.

}
else {
range = factory.cloneNode(range); // if `range` is synthesized or originates in another file, copy it so it definitely has synthetic positions
}
}
if (range === location) return range;
if (!location) {
Expand Down
6 changes: 2 additions & 4 deletions src/compiler/expressionToTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ import {
NodeBuilderFlags,
NodeFlags,
nodeIsMissing,
NoSubstitutionTemplateLiteral,
ObjectLiteralExpression,
ParameterDeclaration,
ParenthesizedExpression,
Expand Down Expand Up @@ -193,7 +192,7 @@ export function createSyntacticTypeNodeBuilder(
function reuseNode<T extends Node>(context: SyntacticTypeNodeBuilderContext, node: T, range?: Node): T;
function reuseNode<T extends Node>(context: SyntacticTypeNodeBuilderContext, node: T | undefined, range?: Node): T | undefined;
function reuseNode<T extends Node>(context: SyntacticTypeNodeBuilderContext, node: T | undefined, range: Node | undefined = node) {
return node === undefined ? undefined : resolver.markNodeReuse(context, node.flags & NodeFlags.Synthesized ? node : factory.cloneNode(node), range ?? node);
return node === undefined ? undefined : resolver.markNodeReuse(context, node, range ?? node);
}
function tryReuseExistingTypeNode(context: SyntacticTypeNodeBuilderContext, existing: TypeNode): TypeNode | undefined {
const { finalizeBoundary, startRecoveryScope, hadError, markError } = resolver.createRecoveryBoundary(context);
Expand Down Expand Up @@ -942,13 +941,12 @@ export function createSyntacticTypeNodeBuilder(
break;
default:
let typeKind: KeywordTypeSyntaxKind | undefined;
let primitiveNode = node as PrimitiveLiteral;
const primitiveNode = node as PrimitiveLiteral;
switch (node.kind) {
case SyntaxKind.NumericLiteral:
typeKind = SyntaxKind.NumberKeyword;
break;
case SyntaxKind.NoSubstitutionTemplateLiteral:
primitiveNode = factory.createStringLiteral((node as NoSubstitutionTemplateLiteral).text);
typeKind = SyntaxKind.StringKeyword;
break;
case SyntaxKind.StringLiteral:
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ambientErrors.types
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ declare var x = 4;

// Ambient functions with invalid overloads
declare function fn(x: number): string;
>fn : { (x: number): string; (x: "foo"): number; }
>fn : { (x: number): string; (x: 'foo'): number; }
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
> : ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^
>x : number
> : ^^^^^^

declare function fn(x: 'foo'): number;
>fn : { (x: number): string; (x: "foo"): number; }
>fn : { (x: number): string; (x: 'foo'): number; }
> : ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^
>x : "foo"
> : ^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ namespace GH30170 {
> : ^^^^^^^^^^^^^

function drawWithColor(currentColor: 'blue' | 'yellow' | undefined) {
>drawWithColor : (currentColor: "blue" | "yellow" | undefined) => void
>drawWithColor : (currentColor: 'blue' | 'yellow' | undefined) => void
> : ^ ^^ ^^^^^^^^^
>currentColor : "blue" | "yellow"
> : ^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ assignmentCompatWithObjectMembersStringNumericNames.ts(26,5): error TS2741: Prop
assignmentCompatWithObjectMembersStringNumericNames.ts(27,5): error TS2741: Property ''1.0'' is missing in type 'S2' but required in type 'T2'.
assignmentCompatWithObjectMembersStringNumericNames.ts(28,5): error TS2741: Property ''1'' is missing in type 'T' but required in type 'S2'.
assignmentCompatWithObjectMembersStringNumericNames.ts(29,5): error TS2741: Property ''1'' is missing in type '{ '1.0': string; baz?: string; }' but required in type 'S2'.
assignmentCompatWithObjectMembersStringNumericNames.ts(30,5): error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type 'S2'.
assignmentCompatWithObjectMembersStringNumericNames.ts(30,5): error TS2741: Property ''1'' is missing in type '{ "1.0": string; }' but required in type 'S2'.
assignmentCompatWithObjectMembersStringNumericNames.ts(32,5): error TS2741: Property ''1.'' is missing in type '{ '1.0': string; baz?: string; }' but required in type '{ '1.': string; bar?: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(33,5): error TS2741: Property ''1.0'' is missing in type '{ '1.': string; bar?: string; }' but required in type '{ '1.0': string; baz?: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(34,5): error TS2741: Property ''1.'' is missing in type 'S' but required in type '{ '1.': string; bar?: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(35,5): error TS2741: Property ''1.'' is missing in type 'S2' but required in type '{ '1.': string; bar?: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(36,5): error TS2741: Property ''1.'' is missing in type '{ '1.0': string; }' but required in type '{ '1.': string; bar?: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(38,5): error TS2741: Property ''1.0'' is missing in type '{ '1': string; }' but required in type '{ '1.0': string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(39,5): error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type '{ '1': string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(42,5): error TS2741: Property ''1.0'' is missing in type 'T' but required in type '{ '1.0': string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(36,5): error TS2741: Property ''1.'' is missing in type '{ "1.0": string; }' but required in type '{ '1.': string; bar?: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(38,5): error TS2741: Property ''1.0'' is missing in type '{ '1': string; }' but required in type '{ "1.0": string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(39,5): error TS2741: Property ''1'' is missing in type '{ "1.0": string; }' but required in type '{ "1": string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(42,5): error TS2741: Property ''1.0'' is missing in type 'T' but required in type '{ "1.0": string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(65,5): error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type 'S'.
assignmentCompatWithObjectMembersStringNumericNames.ts(71,5): error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type 'S2'.
assignmentCompatWithObjectMembersStringNumericNames.ts(71,5): error TS2741: Property ''1'' is missing in type '{ "1.0": string; }' but required in type 'S2'.
assignmentCompatWithObjectMembersStringNumericNames.ts(73,5): error TS2741: Property ''1.'' is missing in type '{ 1: string; baz?: string; }' but required in type '{ '1.': string; bar?: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(74,5): error TS2741: Property '1.0' is missing in type '{ '1.': string; bar?: string; }' but required in type '{ 1: string; baz?: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(75,5): error TS2741: Property ''1.'' is missing in type 'S' but required in type '{ '1.': string; bar?: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(76,5): error TS2741: Property ''1.'' is missing in type 'S2' but required in type '{ '1.': string; bar?: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(77,5): error TS2741: Property ''1.'' is missing in type '{ '1.0': string; }' but required in type '{ '1.': string; bar?: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(77,5): error TS2741: Property ''1.'' is missing in type '{ "1.0": string; }' but required in type '{ '1.': string; bar?: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(78,5): error TS2741: Property ''1.'' is missing in type '{ 1: string; }' but required in type '{ '1.': string; bar?: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(80,5): error TS2741: Property ''1.0'' is missing in type '{ 1: string; }' but required in type '{ '1.0': string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(81,5): error TS2741: Property '1.' is missing in type '{ '1.0': string; }' but required in type '{ 1: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(82,5): error TS2741: Property ''1.0'' is missing in type '{ 1: string; baz?: string; }' but required in type '{ '1.0': string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(83,5): error TS2741: Property ''1.0'' is missing in type 'T2' but required in type '{ '1.0': string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(84,5): error TS2741: Property ''1.0'' is missing in type 'T' but required in type '{ '1.0': string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(80,5): error TS2741: Property ''1.0'' is missing in type '{ 1: string; }' but required in type '{ "1.0": string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(81,5): error TS2741: Property '1.' is missing in type '{ "1.0": string; }' but required in type '{ 1: string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(82,5): error TS2741: Property ''1.0'' is missing in type '{ 1: string; baz?: string; }' but required in type '{ "1.0": string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(83,5): error TS2741: Property ''1.0'' is missing in type 'T2' but required in type '{ "1.0": string; }'.
assignmentCompatWithObjectMembersStringNumericNames.ts(84,5): error TS2741: Property ''1.0'' is missing in type 'T' but required in type '{ "1.0": string; }'.


==== assignmentCompatWithObjectMembersStringNumericNames.ts (29 errors) ====
Expand Down Expand Up @@ -82,7 +82,7 @@ assignmentCompatWithObjectMembersStringNumericNames.ts(84,5): error TS2741: Prop
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:10:20: ''1'' is declared here.
s2 = a2;
~~
!!! error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type 'S2'.
!!! error TS2741: Property ''1'' is missing in type '{ "1.0": string; }' but required in type 'S2'.
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:10:20: ''1'' is declared here.

a = b;
Expand All @@ -103,22 +103,22 @@ assignmentCompatWithObjectMembersStringNumericNames.ts(84,5): error TS2741: Prop
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:15:14: ''1.'' is declared here.
a = a2;
~
!!! error TS2741: Property ''1.'' is missing in type '{ '1.0': string; }' but required in type '{ '1.': string; bar?: string; }'.
!!! error TS2741: Property ''1.'' is missing in type '{ "1.0": string; }' but required in type '{ '1.': string; bar?: string; }'.
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:15:14: ''1.'' is declared here.

a2 = b2;
~~
!!! error TS2741: Property ''1.0'' is missing in type '{ '1': string; }' but required in type '{ '1.0': string; }'.
!!! error TS2741: Property ''1.0'' is missing in type '{ '1': string; }' but required in type '{ "1.0": string; }'.
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:18:16: ''1.0'' is declared here.
b2 = a2;
~~
!!! error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type '{ '1': string; }'.
!!! error TS2741: Property ''1'' is missing in type '{ "1.0": string; }' but required in type '{ "1": string; }'.
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:19:16: ''1'' is declared here.
a2 = b; // ok
a2 = t2; // ok
a2 = t;
~~
!!! error TS2741: Property ''1.0'' is missing in type 'T' but required in type '{ '1.0': string; }'.
!!! error TS2741: Property ''1.0'' is missing in type 'T' but required in type '{ "1.0": string; }'.
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:18:16: ''1.0'' is declared here.
}

Expand Down Expand Up @@ -153,7 +153,7 @@ assignmentCompatWithObjectMembersStringNumericNames.ts(84,5): error TS2741: Prop
s2 = b; // ok
s2 = a2; // error
~~
!!! error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type 'S2'.
!!! error TS2741: Property ''1'' is missing in type '{ "1.0": string; }' but required in type 'S2'.
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:51:20: ''1'' is declared here.

a = b; // error
Expand All @@ -174,7 +174,7 @@ assignmentCompatWithObjectMembersStringNumericNames.ts(84,5): error TS2741: Prop
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:56:14: ''1.'' is declared here.
a = a2; // error
~
!!! error TS2741: Property ''1.'' is missing in type '{ '1.0': string; }' but required in type '{ '1.': string; bar?: string; }'.
!!! error TS2741: Property ''1.'' is missing in type '{ "1.0": string; }' but required in type '{ '1.': string; bar?: string; }'.
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:56:14: ''1.'' is declared here.
a = b2; // error
~
Expand All @@ -183,22 +183,22 @@ assignmentCompatWithObjectMembersStringNumericNames.ts(84,5): error TS2741: Prop

a2 = b2; // error
~~
!!! error TS2741: Property ''1.0'' is missing in type '{ 1: string; }' but required in type '{ '1.0': string; }'.
!!! error TS2741: Property ''1.0'' is missing in type '{ 1: string; }' but required in type '{ "1.0": string; }'.
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:59:16: ''1.0'' is declared here.
b2 = a2; // error
~~
!!! error TS2741: Property '1.' is missing in type '{ '1.0': string; }' but required in type '{ 1: string; }'.
!!! error TS2741: Property '1.' is missing in type '{ "1.0": string; }' but required in type '{ 1: string; }'.
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:60:16: '1.' is declared here.
a2 = b; // error
~~
!!! error TS2741: Property ''1.0'' is missing in type '{ 1: string; baz?: string; }' but required in type '{ '1.0': string; }'.
!!! error TS2741: Property ''1.0'' is missing in type '{ 1: string; baz?: string; }' but required in type '{ "1.0": string; }'.
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:59:16: ''1.0'' is declared here.
a2 = t2; // error
~~
!!! error TS2741: Property ''1.0'' is missing in type 'T2' but required in type '{ '1.0': string; }'.
!!! error TS2741: Property ''1.0'' is missing in type 'T2' but required in type '{ "1.0": string; }'.
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:59:16: ''1.0'' is declared here.
a2 = t; // error
~~
!!! error TS2741: Property ''1.0'' is missing in type 'T' but required in type '{ '1.0': string; }'.
!!! error TS2741: Property ''1.0'' is missing in type 'T' but required in type '{ "1.0": string; }'.
!!! related TS2728 assignmentCompatWithObjectMembersStringNumericNames.ts:59:16: ''1.0'' is declared here.
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/callSignatureFunctionOverload.types
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

=== callSignatureFunctionOverload.ts ===
var foo: {
>foo : { (name: string): string; (name: "order"): string; (name: "content"): string; (name: "done"): string; }
>foo : { (name: string): string; (name: 'order'): string; (name: 'content'): string; (name: 'done'): string; }
> : ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^

(name: string): string;
Expand All @@ -23,7 +23,7 @@ var foo: {
}

var foo2: {
>foo2 : { (name: string): string; (name: "order"): string; (name: "order"): string; (name: "done"): string; }
>foo2 : { (name: string): string; (name: 'order'): string; (name: 'order'): string; (name: 'done'): string; }
> : ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^

(name: string): string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

=== callbackArgsDifferByOptionality.ts ===
function x3(callback: (x?: 'hi') => number);
>x3 : { (callback: (x?: "hi") => number): any; (callback: (x: string) => number): any; }
>x3 : { (callback: (x?: 'hi') => number): any; (callback: (x: string) => number): any; }
> : ^^^ ^^ ^^^^^^^^^ ^^ ^^^^^^^^^
>callback : (x?: "hi") => number
>callback : (x?: 'hi') => number
> : ^ ^^^ ^^^^^
>x : "hi"
> : ^^^^

function x3(callback: (x: string) => number);
>x3 : { (callback: (x?: "hi") => number): any; (callback: (x: string) => number): any; }
>x3 : { (callback: (x?: 'hi') => number): any; (callback: (x: string) => number): any; }
> : ^^^ ^^ ^^^^^^^^^ ^^ ^^^^^^^^^
>callback : (x: string) => number
> : ^ ^^ ^^^^^
>x : string
> : ^^^^^^

function x3(callback: (x: any) => number) {
>x3 : { (callback: (x?: "hi") => number): any; (callback: (x: string) => number): any; }
>x3 : { (callback: (x?: 'hi') => number): any; (callback: (x: string) => number): any; }
> : ^^^ ^^ ^^^^^^^^^ ^^ ^^^^^^^^^
>callback : (x: any) => number
> : ^ ^^ ^^^^^
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/callbackCrossModule.types
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function C() {
=== use.js ===
/** @param {import('./mod1').Con} k */
function f(k) {
>f : (k: import("./mod1").Con) => any
>f : (k: import('./mod1').Con) => any
> : ^ ^^ ^^^^^^^^
>k : import("mod1").Con
> : ^^^^^^^^^^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/checkJsdocTypeTag5.types
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var k = function (x) { return x }
/** @typedef {(x: 'hi' | 'bye') => 0 | 1 | 2} Argle */
/** @type {Argle} */
function blargle(s) {
>blargle : (x: "hi" | "bye") => 0 | 1 | 2
>blargle : (x: 'hi' | 'bye') => 0 | 1 | 2
> : ^ ^^ ^^^^^
>s : "hi" | "bye"
> : ^^^^^^^^^^^^
Expand All @@ -84,7 +84,7 @@ var zeroonetwo = blargle('hi')
> : ^^^^^^^^^
>blargle('hi') : 0 | 1 | 2
> : ^^^^^^^^^
>blargle : (x: "hi" | "bye") => 0 | 1 | 2
>blargle : (x: 'hi' | 'bye') => 0 | 1 | 2
> : ^ ^^ ^^^^^
>'hi' : "hi"
> : ^^^^
Expand Down
Loading
Loading