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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 10 additions & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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;

0 commit comments

Comments
 (0)