Skip to content

Commit

Permalink
widen type, index signature, and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Ozga committed Feb 16, 2017
1 parent a4cf12e commit f2770a1
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Expand Up @@ -84,6 +84,7 @@ namespace ts {
getIndexTypeOfType,
getBaseTypes,
getBaseTypeOfLiteralType,
getWidenedType,
getTypeFromTypeNode,
getParameterType: getTypeAtPosition,
getReturnTypeOfSignature,
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Expand Up @@ -3311,7 +3311,7 @@
"category": "Message",
"code": 90016
},
"Add index accessor for missing property '{0}'": {
"Add index signature for missing property '{0}'": {
"category": "Message",
"code": 90017
},
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Expand Up @@ -2383,6 +2383,7 @@
getIndexTypeOfType(type: Type, kind: IndexKind): Type;
getBaseTypes(type: InterfaceType): BaseType[];
getBaseTypeOfLiteralType(type: Type): Type;
getWidenedType(type: Type): Type;
getReturnTypeOfSignature(signature: Signature): Type;
/**
* Gets the type of a parameter at a given position in a signature.
Expand Down
4 changes: 2 additions & 2 deletions src/services/codefixes/fixAddMissingMember.ts
Expand Up @@ -37,7 +37,7 @@ namespace ts.codefix {
const binaryExpression = token.parent.parent as BinaryExpression;

const checker = context.program.getTypeChecker();
const widenedType = checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(binaryExpression.right));
const widenedType = checker.getWidenedType(checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(binaryExpression.right)));
typeString = checker.typeToString(widenedType);
}

Expand All @@ -54,7 +54,7 @@ namespace ts.codefix {
}]
},
{
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Add_index_accessor_for_missing_property_0), [token.getText()]),
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Add_index_signature_for_missing_property_0), [token.getText()]),
changes: [{
fileName: sourceFile.fileName,
textChanges: [{
Expand Down
@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />

//// [|class A {
//// constructor() {
//// this.x = 10;
//// }
//// }|]

verify.rangeAfterCodeFix(`
class A {
[name: string]: number;
constructor() {
this.x = 10;
}
}
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 1);
@@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />

//// [|class A {
//// constructor() {
//// this.x = function(x: number, y?: A){
//// return x > 0 ? x : y;
//// }
//// }
//// }|]

verify.rangeAfterCodeFix(`
class A {
x: (x: number, y?: A) => A;
constructor() {
this.x = function(x: number, y?: A){
return x > 0 ? x : y;
}
}
}
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);
@@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />

//// [|class A {
//// y: number;
//// constructor(public a: number) {
//// this.x = function(x: number, y?: A){
//// return x > 0 ? x : y;
//// }
//// }
//// }|]

verify.rangeAfterCodeFix(`
class A {
x: (x: number, y?: A) => number | A;
y: number;
constructor(public a: number) {
this.x = function(x: number, y?: A){
return x > 0 ? x : y;
}
}
}
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);
10 changes: 6 additions & 4 deletions tests/cases/fourslash/codeFixUndeclaredPropertyObjectLiteral.ts
Expand Up @@ -2,16 +2,18 @@

//// [|class A {
//// constructor() {
//// this.x = { a: 10, b: "hello" };
//// let e: any = 10;
//// this.x = { a: 10, b: "hello", c: undefined, d: null, e: e };
//// }
//// }|]

verify.rangeAfterCodeFix(`
class A {
x: { a: number; b: string; };
x: { a: number; b: string; c: any; d: any; e: any; };
constructor() {
this.x = { a: 10, b: "hello" };
let e: any = 10;
this.x = { a: 10, b: "hello", c: undefined, d: null, e: e };
}
}
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);
@@ -0,0 +1,21 @@
/// <reference path='fourslash.ts' />

// @strictNullChecks: true

//// [|class A {
//// constructor() {
//// let e: any = 10;
//// this.x = { a: 10, b: "hello", c: undefined, d: null, e: e };
//// }
//// }|]

verify.rangeAfterCodeFix(`
class A {
x: { a: number; b: string; c: undefined; d: null; e: any; };
constructor() {
let e: any = 10;
this.x = { a: 10, b: "hello", c: undefined, d: null, e: e };
}
}
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);
17 changes: 17 additions & 0 deletions tests/cases/fourslash/codeFixUndeclaredPropertyThisType.ts
@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />

//// [|class A {
//// constructor() {
//// this.mythis = this;
//// }
//// }|]

verify.rangeAfterCodeFix(`
class A {
mythis: this;
constructor() {
this.mythis = this;
}
}
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);

0 comments on commit f2770a1

Please sign in to comment.