Skip to content

Commit 326389b

Browse files
committed
More aggressive parens to match existing DT tests
1 parent 1c6f587 commit 326389b

File tree

65 files changed

+214
-199
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+214
-199
lines changed

src/compiler/factory/nodeFactory.ts

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ namespace ts {
4444
const factory: NodeFactory = {
4545
get parenthesizer() { return parenthesizerRules(); },
4646
get converters() { return converters(); },
47+
baseFactory,
48+
flags,
4749
createNodeArray,
4850
createNumericLiteral,
4951
createBigIntLiteral,

src/compiler/factory/parenthesizerRules.ts

+2
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ namespace ts {
434434
function parenthesizeConstituentTypeOfUnionType(type: TypeNode) {
435435
switch (type.kind) {
436436
case SyntaxKind.UnionType: // Not strictly necessary, but a union containing a union should have been flattened
437+
case SyntaxKind.IntersectionType: // Not strictly necessary, but makes generated output more readable and avoids breaks in DT tests
437438
return factory.createParenthesizedType(type);
438439
}
439440
return parenthesizeCheckTypeOfConditionalType(type);
@@ -501,6 +502,7 @@ namespace ts {
501502
switch (type.kind) {
502503
case SyntaxKind.InferType:
503504
case SyntaxKind.TypeOperator:
505+
case SyntaxKind.TypeQuery: // Not strictly necessary, but makes generated output more readable and avoids breaks in DT tests
504506
return factory.createParenthesizedType(type);
505507
}
506508
return parenthesizeOperandOfTypeOperator(type);

src/compiler/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7158,6 +7158,8 @@ namespace ts {
71587158
export interface NodeFactory {
71597159
/* @internal */ readonly parenthesizer: ParenthesizerRules;
71607160
/* @internal */ readonly converters: NodeConverters;
7161+
/* @internal */ readonly baseFactory: BaseNodeFactory;
7162+
/* @internal */ readonly flags: NodeFactoryFlags;
71617163
createNodeArray<T extends Node>(elements?: readonly T[], hasTrailingComma?: boolean): NodeArray<T>;
71627164

71637165
//

src/services/textChanges.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1123,8 +1123,17 @@ namespace ts.textChanges {
11231123
return skipTrivia(s, 0) === s.length;
11241124
}
11251125

1126+
// A transformation context that won't perform parenthesization, as some parenthesization rules
1127+
// are more aggressive than is strictly necessary.
1128+
const textChangesTransformationContext: TransformationContext = {
1129+
...nullTransformationContext,
1130+
factory: createNodeFactory(
1131+
nullTransformationContext.factory.flags | NodeFactoryFlags.NoParenthesizerRules,
1132+
nullTransformationContext.factory.baseFactory),
1133+
};
1134+
11261135
export function assignPositionsToNode(node: Node): Node {
1127-
const visited = visitEachChild(node, assignPositionsToNode, nullTransformationContext, assignPositionsToNodeArray, assignPositionsToNode);
1136+
const visited = visitEachChild(node, assignPositionsToNode, textChangesTransformationContext, assignPositionsToNodeArray, assignPositionsToNode);
11281137
// create proxy node for non synthesized nodes
11291138
const newNode = nodeIsSynthesized(visited) ? visited : Object.create(visited) as Node;
11301139
setTextRangePosEnd(newNode, getPos(node), getEnd(node));

tests/baselines/reference/abstractClassUnionInstantiation.types

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ new cls3(); // should work
7575

7676
[ConcreteA, ConcreteB].map(cls => new cls()); // should work
7777
>[ConcreteA, ConcreteB].map(cls => new cls()) : ConcreteA[]
78-
>[ConcreteA, ConcreteB].map : <U>(callbackfn: (value: typeof ConcreteA, index: number, array: typeof ConcreteA[]) => U, thisArg?: any) => U[]
79-
>[ConcreteA, ConcreteB] : typeof ConcreteA[]
78+
>[ConcreteA, ConcreteB].map : <U>(callbackfn: (value: typeof ConcreteA, index: number, array: (typeof ConcreteA)[]) => U, thisArg?: any) => U[]
79+
>[ConcreteA, ConcreteB] : (typeof ConcreteA)[]
8080
>ConcreteA : typeof ConcreteA
8181
>ConcreteB : typeof ConcreteB
82-
>map : <U>(callbackfn: (value: typeof ConcreteA, index: number, array: typeof ConcreteA[]) => U, thisArg?: any) => U[]
82+
>map : <U>(callbackfn: (value: typeof ConcreteA, index: number, array: (typeof ConcreteA)[]) => U, thisArg?: any) => U[]
8383
>cls => new cls() : (cls: typeof ConcreteA) => ConcreteA
8484
>cls : typeof ConcreteA
8585
>new cls() : ConcreteA

tests/baselines/reference/aliasUsageInArray.types

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ interface IHasVisualizationModel {
1515

1616
var xs: IHasVisualizationModel[] = [moduleA];
1717
>xs : IHasVisualizationModel[]
18-
>[moduleA] : typeof moduleA[]
18+
>[moduleA] : (typeof moduleA)[]
1919
>moduleA : typeof moduleA
2020

2121
var xs2: typeof moduleA[] = [moduleA];
22-
>xs2 : typeof moduleA[]
22+
>xs2 : (typeof moduleA)[]
2323
>moduleA : typeof moduleA
24-
>[moduleA] : typeof moduleA[]
24+
>[moduleA] : (typeof moduleA)[]
2525
>moduleA : typeof moduleA
2626

2727
=== tests/cases/compiler/aliasUsageInArray_backbone.ts ===

tests/baselines/reference/ambientConstLiterals.types

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ enum E { A, B, C, "non identifier" }
1212
>A : E.A
1313
>B : E.B
1414
>C : E.C
15-
>"non identifier" : typeof E["non identifier"]
15+
>"non identifier" : (typeof E)["non identifier"]
1616

1717
const c1 = "abc";
1818
>c1 : "abc"
@@ -54,8 +54,8 @@ const c8 = E.A;
5454
>A : E.A
5555

5656
const c8b = E["non identifier"];
57-
>c8b : typeof E["non identifier"]
58-
>E["non identifier"] : typeof E["non identifier"]
57+
>c8b : (typeof E)["non identifier"]
58+
>E["non identifier"] : (typeof E)["non identifier"]
5959
>E : typeof E
6060
>"non identifier" : "non identifier"
6161

tests/baselines/reference/arrayLiterals.types

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ var classArr = [new C(), new C()];
6565
>C : typeof C
6666

6767
var classTypeArray = [C, C, C];
68-
>classTypeArray : typeof C[]
69-
>[C, C, C] : typeof C[]
68+
>classTypeArray : (typeof C)[]
69+
>[C, C, C] : (typeof C)[]
7070
>C : typeof C
7171
>C : typeof C
7272
>C : typeof C
7373

7474
var classTypeArray: Array<typeof C>; // Should OK, not be a parse error
75-
>classTypeArray : typeof C[]
75+
>classTypeArray : (typeof C)[]
7676
>C : typeof C
7777

7878
// Contextual type C with numeric index signature makes array literal of EveryType E of type BCT(E,C)[]

tests/baselines/reference/arrayOfFunctionTypes3.types

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ class C {
2222
>foo : string
2323
}
2424
var y = [C, C];
25-
>y : typeof C[]
26-
>[C, C] : typeof C[]
25+
>y : (typeof C)[]
26+
>[C, C] : (typeof C)[]
2727
>C : typeof C
2828
>C : typeof C
2929

3030
var r3 = new y[0]();
3131
>r3 : C
3232
>new y[0]() : C
3333
>y[0] : typeof C
34-
>y : typeof C[]
34+
>y : (typeof C)[]
3535
>0 : 0
3636

3737
var a: { (x: number): number; (x: string): string; };

tests/baselines/reference/bitwiseNotOperatorWithEnumType.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ enum ENUM1 { A, B, "" };
55
>ENUM1 : ENUM1
66
>A : ENUM1.A
77
>B : ENUM1.B
8-
>"" : typeof ENUM1[""]
8+
>"" : (typeof ENUM1)[""]
99

1010
// enum type var
1111
var ResultIsNumber1 = ~ENUM1;

tests/baselines/reference/classCanExtendConstructorFunction.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ tests/cases/conformance/salsa/generic.js(20,32): error TS2345: Argument of type
77
tests/cases/conformance/salsa/second.ts(8,25): error TS2507: Type '(numberEaten: number) => void' is not a constructor function type.
88
tests/cases/conformance/salsa/second.ts(14,7): error TS2417: Class static side 'typeof Conestoga' incorrectly extends base class static side 'typeof Wagon'.
99
Types of property 'circle' are incompatible.
10-
Type '(others: typeof Wagon[]) => number' is not assignable to type '(wagons?: Wagon[]) => number'.
10+
Type '(others: (typeof Wagon)[]) => number' is not assignable to type '(wagons?: Wagon[]) => number'.
1111
Types of parameters 'others' and 'wagons' are incompatible.
12-
Type 'Wagon[]' is not assignable to type 'typeof Wagon[]'.
12+
Type 'Wagon[]' is not assignable to type '(typeof Wagon)[]'.
1313
Property 'circle' is missing in type 'Wagon' but required in type 'typeof Wagon'.
1414
tests/cases/conformance/salsa/second.ts(17,15): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
1515

@@ -93,9 +93,9 @@ tests/cases/conformance/salsa/second.ts(17,15): error TS2345: Argument of type '
9393
~~~~~~~~~
9494
!!! error TS2417: Class static side 'typeof Conestoga' incorrectly extends base class static side 'typeof Wagon'.
9595
!!! error TS2417: Types of property 'circle' are incompatible.
96-
!!! error TS2417: Type '(others: typeof Wagon[]) => number' is not assignable to type '(wagons?: Wagon[]) => number'.
96+
!!! error TS2417: Type '(others: (typeof Wagon)[]) => number' is not assignable to type '(wagons?: Wagon[]) => number'.
9797
!!! error TS2417: Types of parameters 'others' and 'wagons' are incompatible.
98-
!!! error TS2417: Type 'Wagon[]' is not assignable to type 'typeof Wagon[]'.
98+
!!! error TS2417: Type 'Wagon[]' is not assignable to type '(typeof Wagon)[]'.
9999
!!! error TS2417: Property 'circle' is missing in type 'Wagon' but required in type 'typeof Wagon'.
100100
!!! related TS2728 tests/cases/conformance/salsa/first.js:9:1: 'circle' is declared here.
101101
constructor(public drunkOO: true) {

tests/baselines/reference/classCanExtendConstructorFunction.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,12 @@ class Conestoga extends Wagon {
201201
// should error since others is not optional
202202
static circle(others: (typeof Wagon)[]) {
203203
>circle : (others: (typeof Wagon)[]) => number
204-
>others : typeof Wagon[]
204+
>others : (typeof Wagon)[]
205205
>Wagon : typeof Wagon
206206

207207
return others.length
208208
>others.length : number
209-
>others : typeof Wagon[]
209+
>others : (typeof Wagon)[]
210210
>length : number
211211
}
212212
}

tests/baselines/reference/constEnumErrors.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ var x = E2;
7272
>E2 : typeof E2
7373

7474
var y = [E2];
75-
>y : typeof E2[]
76-
>[E2] : typeof E2[]
75+
>y : (typeof E2)[]
76+
>[E2] : (typeof E2)[]
7777
>E2 : typeof E2
7878

7979
function foo(t: any): void {

tests/baselines/reference/constEnums.types

+14-14
Original file line numberDiff line numberDiff line change
@@ -184,25 +184,25 @@ const enum Comments {
184184
>Comments : Comments
185185

186186
"//",
187-
>"//" : typeof Comments["//"]
187+
>"//" : (typeof Comments)["//"]
188188

189189
"/*",
190-
>"/*" : typeof Comments["/*"]
190+
>"/*" : (typeof Comments)["/*"]
191191

192192
"*/",
193-
>"*/" : typeof Comments["*/"]
193+
>"*/" : (typeof Comments)["*/"]
194194

195195
"///",
196-
>"///" : typeof Comments["///"]
196+
>"///" : (typeof Comments)["///"]
197197

198198
"#",
199-
>"#" : typeof Comments["#"]
199+
>"#" : (typeof Comments)["#"]
200200

201201
"<!--",
202-
>"<!--" : typeof Comments["<!--"]
202+
>"<!--" : (typeof Comments)["<!--"]
203203

204204
"-->",
205-
>"-->" : typeof Comments["-->"]
205+
>"-->" : (typeof Comments)["-->"]
206206
}
207207

208208
module A {
@@ -624,37 +624,37 @@ function baz(c: Comments) {
624624
>c : Comments
625625

626626
case Comments["//"]:
627-
>Comments["//"] : typeof Comments["//"]
627+
>Comments["//"] : (typeof Comments)["//"]
628628
>Comments : typeof Comments
629629
>"//" : "//"
630630

631631
case Comments["/*"]:
632-
>Comments["/*"] : typeof Comments["/*"]
632+
>Comments["/*"] : (typeof Comments)["/*"]
633633
>Comments : typeof Comments
634634
>"/*" : "/*"
635635

636636
case Comments["*/"]:
637-
>Comments["*/"] : typeof Comments["*/"]
637+
>Comments["*/"] : (typeof Comments)["*/"]
638638
>Comments : typeof Comments
639639
>"*/" : "*/"
640640

641641
case Comments["///"]:
642-
>Comments["///"] : typeof Comments["///"]
642+
>Comments["///"] : (typeof Comments)["///"]
643643
>Comments : typeof Comments
644644
>"///" : "///"
645645

646646
case Comments["#"]:
647-
>Comments["#"] : typeof Comments["#"]
647+
>Comments["#"] : (typeof Comments)["#"]
648648
>Comments : typeof Comments
649649
>"#" : "#"
650650

651651
case Comments["<!--"]:
652-
>Comments["<!--"] : typeof Comments["<!--"]
652+
>Comments["<!--"] : (typeof Comments)["<!--"]
653653
>Comments : typeof Comments
654654
>"<!--" : "<!--"
655655

656656
case Comments["-->"]:
657-
>Comments["-->"] : typeof Comments["-->"]
657+
>Comments["-->"] : (typeof Comments)["-->"]
658658
>Comments : typeof Comments
659659
>"-->" : "-->"
660660

tests/baselines/reference/constructorTagOnClassConstructor.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export class Beta {
1313
}
1414

1515
const arr = [Alpha, Beta];
16-
>arr : typeof Alpha[]
17-
>[Alpha, Beta] : typeof Alpha[]
16+
>arr : (typeof Alpha)[]
17+
>[Alpha, Beta] : (typeof Alpha)[]
1818
>Alpha : typeof Alpha
1919
>Beta : typeof Beta
2020

tests/baselines/reference/controlFlowIfStatement.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function c<T>(data: string | T): T {
104104
>JSON.parse : (text: string, reviver?: (this: any, key: string, value: any) => any) => any
105105
>JSON : JSON
106106
>parse : (text: string, reviver?: (this: any, key: string, value: any) => any) => any
107-
>data : string | T & string
107+
>data : string | (T & string)
108108
}
109109
else {
110110
return data;

tests/baselines/reference/declFileEnums.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ enum e5 {
8383
>"Sunday" : e5.Sunday
8484

8585
"Weekend days"
86-
>"Weekend days" : typeof e5["Weekend days"]
86+
>"Weekend days" : (typeof e5)["Weekend days"]
8787
}
8888

8989

tests/baselines/reference/declarationEmitSpreadStringlyKeyedEnum.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ declare enum AgeGroups {
4242
}
4343
export declare const SpotifyAgeGroupEnum: {
4444
[x: number]: string;
45-
"0-17": typeof AgeGroups["0-17"];
46-
"18-22": typeof AgeGroups["18-22"];
47-
"23-27": typeof AgeGroups["23-27"];
48-
"28-34": typeof AgeGroups["28-34"];
49-
"35-44": typeof AgeGroups["35-44"];
50-
"45-59": typeof AgeGroups["45-59"];
51-
"60-150": typeof AgeGroups["60-150"];
45+
"0-17": (typeof AgeGroups)["0-17"];
46+
"18-22": (typeof AgeGroups)["18-22"];
47+
"23-27": (typeof AgeGroups)["23-27"];
48+
"28-34": (typeof AgeGroups)["28-34"];
49+
"35-44": (typeof AgeGroups)["35-44"];
50+
"45-59": (typeof AgeGroups)["45-59"];
51+
"60-150": (typeof AgeGroups)["60-150"];
5252
};
5353
export {};
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
=== tests/cases/compiler/declarationEmitSpreadStringlyKeyedEnum.ts ===
22
enum AgeGroups { "0-17" , "18-22" , "23-27" , "28-34" , "35-44" , "45-59" , "60-150" }
33
>AgeGroups : AgeGroups
4-
>"0-17" : typeof AgeGroups["0-17"]
5-
>"18-22" : typeof AgeGroups["18-22"]
6-
>"23-27" : typeof AgeGroups["23-27"]
7-
>"28-34" : typeof AgeGroups["28-34"]
8-
>"35-44" : typeof AgeGroups["35-44"]
9-
>"45-59" : typeof AgeGroups["45-59"]
10-
>"60-150" : typeof AgeGroups["60-150"]
4+
>"0-17" : (typeof AgeGroups)["0-17"]
5+
>"18-22" : (typeof AgeGroups)["18-22"]
6+
>"23-27" : (typeof AgeGroups)["23-27"]
7+
>"28-34" : (typeof AgeGroups)["28-34"]
8+
>"35-44" : (typeof AgeGroups)["35-44"]
9+
>"45-59" : (typeof AgeGroups)["45-59"]
10+
>"60-150" : (typeof AgeGroups)["60-150"]
1111

1212
export const SpotifyAgeGroupEnum = { ...AgeGroups };
13-
>SpotifyAgeGroupEnum : { [x: number]: string; "0-17": typeof AgeGroups["0-17"]; "18-22": typeof AgeGroups["18-22"]; "23-27": typeof AgeGroups["23-27"]; "28-34": typeof AgeGroups["28-34"]; "35-44": typeof AgeGroups["35-44"]; "45-59": typeof AgeGroups["45-59"]; "60-150": typeof AgeGroups["60-150"]; }
14-
>{ ...AgeGroups } : { [x: number]: string; "0-17": typeof AgeGroups["0-17"]; "18-22": typeof AgeGroups["18-22"]; "23-27": typeof AgeGroups["23-27"]; "28-34": typeof AgeGroups["28-34"]; "35-44": typeof AgeGroups["35-44"]; "45-59": typeof AgeGroups["45-59"]; "60-150": typeof AgeGroups["60-150"]; }
13+
>SpotifyAgeGroupEnum : { [x: number]: string; "0-17": (typeof AgeGroups)["0-17"]; "18-22": (typeof AgeGroups)["18-22"]; "23-27": (typeof AgeGroups)["23-27"]; "28-34": (typeof AgeGroups)["28-34"]; "35-44": (typeof AgeGroups)["35-44"]; "45-59": (typeof AgeGroups)["45-59"]; "60-150": (typeof AgeGroups)["60-150"]; }
14+
>{ ...AgeGroups } : { [x: number]: string; "0-17": (typeof AgeGroups)["0-17"]; "18-22": (typeof AgeGroups)["18-22"]; "23-27": (typeof AgeGroups)["23-27"]; "28-34": (typeof AgeGroups)["28-34"]; "35-44": (typeof AgeGroups)["35-44"]; "45-59": (typeof AgeGroups)["45-59"]; "60-150": (typeof AgeGroups)["60-150"]; }
1515
>AgeGroups : typeof AgeGroups
1616

tests/baselines/reference/decrementOperatorWithEnumType.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ enum ENUM1 { A, B, "" };
55
>ENUM1 : ENUM1
66
>A : ENUM1.A
77
>B : ENUM1.B
8-
>"" : typeof ENUM1[""]
8+
>"" : (typeof ENUM1)[""]
99

1010
// expression
1111
var ResultIsNumber1 = --ENUM1["A"];

tests/baselines/reference/decrementOperatorWithEnumTypeInvalidOperations.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ enum ENUM1 { A, B, "" };
88
>ENUM1 : ENUM1
99
>A : ENUM1.A
1010
>B : ENUM1.B
11-
>"" : typeof ENUM1[""]
11+
>"" : (typeof ENUM1)[""]
1212

1313
// enum type var
1414
var ResultIsNumber1 = --ENUM;

tests/baselines/reference/deleteOperatorWithEnumType.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ enum ENUM1 { A, B, "" };
88
>ENUM1 : ENUM1
99
>A : ENUM1.A
1010
>B : ENUM1.B
11-
>"" : typeof ENUM1[""]
11+
>"" : (typeof ENUM1)[""]
1212

1313
// enum type var
1414
var ResultIsBoolean1 = delete ENUM;

tests/baselines/reference/derivedClassSuperProperties.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -879,8 +879,8 @@ let a, b;
879879
>b : any
880880

881881
const DerivedWithLoops = [
882-
>DerivedWithLoops : typeof (Anonymous class)[]
883-
>[ class extends Base { prop = true; constructor() { for(super();;) {} } }, class extends Base { prop = true; constructor() { for(a; super();) {} } }, class extends Base { prop = true; constructor() { for(a; b; super()) {} } }, class extends Base { prop = true; constructor() { for(; ; super()) { break; } } }, class extends Base { prop = true; constructor() { for (const x of super()) {} } }, class extends Base { prop = true; constructor() { while (super()) {} } }, class extends Base { prop = true; constructor() { do {} while (super()); } }, class extends Base { prop = true; constructor() { if (super()) {} } }, class extends Base { prop = true; constructor() { switch (super()) {} } },] : typeof (Anonymous class)[]
882+
>DerivedWithLoops : (typeof (Anonymous class))[]
883+
>[ class extends Base { prop = true; constructor() { for(super();;) {} } }, class extends Base { prop = true; constructor() { for(a; super();) {} } }, class extends Base { prop = true; constructor() { for(a; b; super()) {} } }, class extends Base { prop = true; constructor() { for(; ; super()) { break; } } }, class extends Base { prop = true; constructor() { for (const x of super()) {} } }, class extends Base { prop = true; constructor() { while (super()) {} } }, class extends Base { prop = true; constructor() { do {} while (super()); } }, class extends Base { prop = true; constructor() { if (super()) {} } }, class extends Base { prop = true; constructor() { switch (super()) {} } },] : (typeof (Anonymous class))[]
884884

885885
class extends Base {
886886
>class extends Base { prop = true; constructor() { for(super();;) {} } } : typeof (Anonymous class)

0 commit comments

Comments
 (0)