Skip to content

Commit

Permalink
More aggressive parens to match existing DT tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Mar 15, 2022
1 parent 1c6f587 commit 326389b
Show file tree
Hide file tree
Showing 65 changed files with 214 additions and 199 deletions.
2 changes: 2 additions & 0 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ namespace ts {
const factory: NodeFactory = {
get parenthesizer() { return parenthesizerRules(); },
get converters() { return converters(); },
baseFactory,
flags,
createNodeArray,
createNumericLiteral,
createBigIntLiteral,
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/factory/parenthesizerRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ namespace ts {
function parenthesizeConstituentTypeOfUnionType(type: TypeNode) {
switch (type.kind) {
case SyntaxKind.UnionType: // Not strictly necessary, but a union containing a union should have been flattened
case SyntaxKind.IntersectionType: // Not strictly necessary, but makes generated output more readable and avoids breaks in DT tests
return factory.createParenthesizedType(type);
}
return parenthesizeCheckTypeOfConditionalType(type);
Expand Down Expand Up @@ -501,6 +502,7 @@ namespace ts {
switch (type.kind) {
case SyntaxKind.InferType:
case SyntaxKind.TypeOperator:
case SyntaxKind.TypeQuery: // Not strictly necessary, but makes generated output more readable and avoids breaks in DT tests
return factory.createParenthesizedType(type);
}
return parenthesizeOperandOfTypeOperator(type);
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7158,6 +7158,8 @@ namespace ts {
export interface NodeFactory {
/* @internal */ readonly parenthesizer: ParenthesizerRules;
/* @internal */ readonly converters: NodeConverters;
/* @internal */ readonly baseFactory: BaseNodeFactory;
/* @internal */ readonly flags: NodeFactoryFlags;
createNodeArray<T extends Node>(elements?: readonly T[], hasTrailingComma?: boolean): NodeArray<T>;

//
Expand Down
11 changes: 10 additions & 1 deletion src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,8 +1123,17 @@ namespace ts.textChanges {
return skipTrivia(s, 0) === s.length;
}

// A transformation context that won't perform parenthesization, as some parenthesization rules
// are more aggressive than is strictly necessary.
const textChangesTransformationContext: TransformationContext = {
...nullTransformationContext,
factory: createNodeFactory(
nullTransformationContext.factory.flags | NodeFactoryFlags.NoParenthesizerRules,
nullTransformationContext.factory.baseFactory),
};

export function assignPositionsToNode(node: Node): Node {
const visited = visitEachChild(node, assignPositionsToNode, nullTransformationContext, assignPositionsToNodeArray, assignPositionsToNode);
const visited = visitEachChild(node, assignPositionsToNode, textChangesTransformationContext, assignPositionsToNodeArray, assignPositionsToNode);
// create proxy node for non synthesized nodes
const newNode = nodeIsSynthesized(visited) ? visited : Object.create(visited) as Node;
setTextRangePosEnd(newNode, getPos(node), getEnd(node));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ new cls3(); // should work

[ConcreteA, ConcreteB].map(cls => new cls()); // should work
>[ConcreteA, ConcreteB].map(cls => new cls()) : ConcreteA[]
>[ConcreteA, ConcreteB].map : <U>(callbackfn: (value: typeof ConcreteA, index: number, array: typeof ConcreteA[]) => U, thisArg?: any) => U[]
>[ConcreteA, ConcreteB] : typeof ConcreteA[]
>[ConcreteA, ConcreteB].map : <U>(callbackfn: (value: typeof ConcreteA, index: number, array: (typeof ConcreteA)[]) => U, thisArg?: any) => U[]
>[ConcreteA, ConcreteB] : (typeof ConcreteA)[]
>ConcreteA : typeof ConcreteA
>ConcreteB : typeof ConcreteB
>map : <U>(callbackfn: (value: typeof ConcreteA, index: number, array: typeof ConcreteA[]) => U, thisArg?: any) => U[]
>map : <U>(callbackfn: (value: typeof ConcreteA, index: number, array: (typeof ConcreteA)[]) => U, thisArg?: any) => U[]
>cls => new cls() : (cls: typeof ConcreteA) => ConcreteA
>cls : typeof ConcreteA
>new cls() : ConcreteA
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/aliasUsageInArray.types
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ interface IHasVisualizationModel {

var xs: IHasVisualizationModel[] = [moduleA];
>xs : IHasVisualizationModel[]
>[moduleA] : typeof moduleA[]
>[moduleA] : (typeof moduleA)[]
>moduleA : typeof moduleA

var xs2: typeof moduleA[] = [moduleA];
>xs2 : typeof moduleA[]
>xs2 : (typeof moduleA)[]
>moduleA : typeof moduleA
>[moduleA] : typeof moduleA[]
>[moduleA] : (typeof moduleA)[]
>moduleA : typeof moduleA

=== tests/cases/compiler/aliasUsageInArray_backbone.ts ===
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/ambientConstLiterals.types
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum E { A, B, C, "non identifier" }
>A : E.A
>B : E.B
>C : E.C
>"non identifier" : typeof E["non identifier"]
>"non identifier" : (typeof E)["non identifier"]

const c1 = "abc";
>c1 : "abc"
Expand Down Expand Up @@ -54,8 +54,8 @@ const c8 = E.A;
>A : E.A

const c8b = E["non identifier"];
>c8b : typeof E["non identifier"]
>E["non identifier"] : typeof E["non identifier"]
>c8b : (typeof E)["non identifier"]
>E["non identifier"] : (typeof E)["non identifier"]
>E : typeof E
>"non identifier" : "non identifier"

Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/arrayLiterals.types
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ var classArr = [new C(), new C()];
>C : typeof C

var classTypeArray = [C, C, C];
>classTypeArray : typeof C[]
>[C, C, C] : typeof C[]
>classTypeArray : (typeof C)[]
>[C, C, C] : (typeof C)[]
>C : typeof C
>C : typeof C
>C : typeof C

var classTypeArray: Array<typeof C>; // Should OK, not be a parse error
>classTypeArray : typeof C[]
>classTypeArray : (typeof C)[]
>C : typeof C

// Contextual type C with numeric index signature makes array literal of EveryType E of type BCT(E,C)[]
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/arrayOfFunctionTypes3.types
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ class C {
>foo : string
}
var y = [C, C];
>y : typeof C[]
>[C, C] : typeof C[]
>y : (typeof C)[]
>[C, C] : (typeof C)[]
>C : typeof C
>C : typeof C

var r3 = new y[0]();
>r3 : C
>new y[0]() : C
>y[0] : typeof C
>y : typeof C[]
>y : (typeof C)[]
>0 : 0

var a: { (x: number): number; (x: string): string; };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ enum ENUM1 { A, B, "" };
>ENUM1 : ENUM1
>A : ENUM1.A
>B : ENUM1.B
>"" : typeof ENUM1[""]
>"" : (typeof ENUM1)[""]

// enum type var
var ResultIsNumber1 = ~ENUM1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ tests/cases/conformance/salsa/generic.js(20,32): error TS2345: Argument of type
tests/cases/conformance/salsa/second.ts(8,25): error TS2507: Type '(numberEaten: number) => void' is not a constructor function type.
tests/cases/conformance/salsa/second.ts(14,7): error TS2417: Class static side 'typeof Conestoga' incorrectly extends base class static side 'typeof Wagon'.
Types of property 'circle' are incompatible.
Type '(others: typeof Wagon[]) => number' is not assignable to type '(wagons?: Wagon[]) => number'.
Type '(others: (typeof Wagon)[]) => number' is not assignable to type '(wagons?: Wagon[]) => number'.
Types of parameters 'others' and 'wagons' are incompatible.
Type 'Wagon[]' is not assignable to type 'typeof Wagon[]'.
Type 'Wagon[]' is not assignable to type '(typeof Wagon)[]'.
Property 'circle' is missing in type 'Wagon' but required in type 'typeof Wagon'.
tests/cases/conformance/salsa/second.ts(17,15): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

Expand Down Expand Up @@ -93,9 +93,9 @@ tests/cases/conformance/salsa/second.ts(17,15): error TS2345: Argument of type '
~~~~~~~~~
!!! error TS2417: Class static side 'typeof Conestoga' incorrectly extends base class static side 'typeof Wagon'.
!!! error TS2417: Types of property 'circle' are incompatible.
!!! error TS2417: Type '(others: typeof Wagon[]) => number' is not assignable to type '(wagons?: Wagon[]) => number'.
!!! error TS2417: Type '(others: (typeof Wagon)[]) => number' is not assignable to type '(wagons?: Wagon[]) => number'.
!!! error TS2417: Types of parameters 'others' and 'wagons' are incompatible.
!!! error TS2417: Type 'Wagon[]' is not assignable to type 'typeof Wagon[]'.
!!! error TS2417: Type 'Wagon[]' is not assignable to type '(typeof Wagon)[]'.
!!! error TS2417: Property 'circle' is missing in type 'Wagon' but required in type 'typeof Wagon'.
!!! related TS2728 tests/cases/conformance/salsa/first.js:9:1: 'circle' is declared here.
constructor(public drunkOO: true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ class Conestoga extends Wagon {
// should error since others is not optional
static circle(others: (typeof Wagon)[]) {
>circle : (others: (typeof Wagon)[]) => number
>others : typeof Wagon[]
>others : (typeof Wagon)[]
>Wagon : typeof Wagon

return others.length
>others.length : number
>others : typeof Wagon[]
>others : (typeof Wagon)[]
>length : number
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/constEnumErrors.types
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ var x = E2;
>E2 : typeof E2

var y = [E2];
>y : typeof E2[]
>[E2] : typeof E2[]
>y : (typeof E2)[]
>[E2] : (typeof E2)[]
>E2 : typeof E2

function foo(t: any): void {
Expand Down
28 changes: 14 additions & 14 deletions tests/baselines/reference/constEnums.types
Original file line number Diff line number Diff line change
Expand Up @@ -184,25 +184,25 @@ const enum Comments {
>Comments : Comments

"//",
>"//" : typeof Comments["//"]
>"//" : (typeof Comments)["//"]

"/*",
>"/*" : typeof Comments["/*"]
>"/*" : (typeof Comments)["/*"]

"*/",
>"*/" : typeof Comments["*/"]
>"*/" : (typeof Comments)["*/"]

"///",
>"///" : typeof Comments["///"]
>"///" : (typeof Comments)["///"]

"#",
>"#" : typeof Comments["#"]
>"#" : (typeof Comments)["#"]

"<!--",
>"<!--" : typeof Comments["<!--"]
>"<!--" : (typeof Comments)["<!--"]

"-->",
>"-->" : typeof Comments["-->"]
>"-->" : (typeof Comments)["-->"]
}

module A {
Expand Down Expand Up @@ -624,37 +624,37 @@ function baz(c: Comments) {
>c : Comments

case Comments["//"]:
>Comments["//"] : typeof Comments["//"]
>Comments["//"] : (typeof Comments)["//"]
>Comments : typeof Comments
>"//" : "//"

case Comments["/*"]:
>Comments["/*"] : typeof Comments["/*"]
>Comments["/*"] : (typeof Comments)["/*"]
>Comments : typeof Comments
>"/*" : "/*"

case Comments["*/"]:
>Comments["*/"] : typeof Comments["*/"]
>Comments["*/"] : (typeof Comments)["*/"]
>Comments : typeof Comments
>"*/" : "*/"

case Comments["///"]:
>Comments["///"] : typeof Comments["///"]
>Comments["///"] : (typeof Comments)["///"]
>Comments : typeof Comments
>"///" : "///"

case Comments["#"]:
>Comments["#"] : typeof Comments["#"]
>Comments["#"] : (typeof Comments)["#"]
>Comments : typeof Comments
>"#" : "#"

case Comments["<!--"]:
>Comments["<!--"] : typeof Comments["<!--"]
>Comments["<!--"] : (typeof Comments)["<!--"]
>Comments : typeof Comments
>"<!--" : "<!--"

case Comments["-->"]:
>Comments["-->"] : typeof Comments["-->"]
>Comments["-->"] : (typeof Comments)["-->"]
>Comments : typeof Comments
>"-->" : "-->"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export class Beta {
}

const arr = [Alpha, Beta];
>arr : typeof Alpha[]
>[Alpha, Beta] : typeof Alpha[]
>arr : (typeof Alpha)[]
>[Alpha, Beta] : (typeof Alpha)[]
>Alpha : typeof Alpha
>Beta : typeof Beta

2 changes: 1 addition & 1 deletion tests/baselines/reference/controlFlowIfStatement.types
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function c<T>(data: string | T): T {
>JSON.parse : (text: string, reviver?: (this: any, key: string, value: any) => any) => any
>JSON : JSON
>parse : (text: string, reviver?: (this: any, key: string, value: any) => any) => any
>data : string | T & string
>data : string | (T & string)
}
else {
return data;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/declFileEnums.types
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ enum e5 {
>"Sunday" : e5.Sunday

"Weekend days"
>"Weekend days" : typeof e5["Weekend days"]
>"Weekend days" : (typeof e5)["Weekend days"]
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ declare enum AgeGroups {
}
export declare const 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"];
"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"];
};
export {};
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
=== tests/cases/compiler/declarationEmitSpreadStringlyKeyedEnum.ts ===
enum AgeGroups { "0-17" , "18-22" , "23-27" , "28-34" , "35-44" , "45-59" , "60-150" }
>AgeGroups : AgeGroups
>"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"]
>"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"]

export const SpotifyAgeGroupEnum = { ...AgeGroups };
>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"]; }
>{ ...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"]; }
>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"]; }
>{ ...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"]; }
>AgeGroups : typeof AgeGroups

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ enum ENUM1 { A, B, "" };
>ENUM1 : ENUM1
>A : ENUM1.A
>B : ENUM1.B
>"" : typeof ENUM1[""]
>"" : (typeof ENUM1)[""]

// expression
var ResultIsNumber1 = --ENUM1["A"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ enum ENUM1 { A, B, "" };
>ENUM1 : ENUM1
>A : ENUM1.A
>B : ENUM1.B
>"" : typeof ENUM1[""]
>"" : (typeof ENUM1)[""]

// enum type var
var ResultIsNumber1 = --ENUM;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/deleteOperatorWithEnumType.types
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ enum ENUM1 { A, B, "" };
>ENUM1 : ENUM1
>A : ENUM1.A
>B : ENUM1.B
>"" : typeof ENUM1[""]
>"" : (typeof ENUM1)[""]

// enum type var
var ResultIsBoolean1 = delete ENUM;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/derivedClassSuperProperties.types
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,8 @@ let a, b;
>b : any

const DerivedWithLoops = [
>DerivedWithLoops : typeof (Anonymous class)[]
>[ 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)[]
>DerivedWithLoops : (typeof (Anonymous class))[]
>[ 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))[]

class extends Base {
>class extends Base { prop = true; constructor() { for(super();;) {} } } : typeof (Anonymous class)
Expand Down
Loading

0 comments on commit 326389b

Please sign in to comment.