-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14097 from aozgaa/MissingPropertyFix
Missing property fix
- Loading branch information
Showing
20 changed files
with
288 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
registerCodeFix({ | ||
errorCodes: [Diagnostics.Property_0_does_not_exist_on_type_1.code], | ||
getCodeActions: getActionsForAddMissingMember | ||
}); | ||
|
||
function getActionsForAddMissingMember(context: CodeFixContext): CodeAction[] | undefined { | ||
|
||
const sourceFile = context.sourceFile; | ||
const start = context.span.start; | ||
// This is the identifier of the missing property. eg: | ||
// this.missing = 1; | ||
// ^^^^^^^ | ||
const token = getTokenAtPosition(sourceFile, start); | ||
|
||
if (token.kind != SyntaxKind.Identifier) { | ||
return undefined; | ||
} | ||
|
||
const classDeclaration = getContainingClass(token); | ||
if (!classDeclaration) { | ||
return undefined; | ||
} | ||
|
||
if (!(token.parent && token.parent.kind === SyntaxKind.PropertyAccessExpression)) { | ||
return undefined; | ||
} | ||
|
||
if ((token.parent as PropertyAccessExpression).expression.kind !== SyntaxKind.ThisKeyword) { | ||
return undefined; | ||
} | ||
|
||
let typeString = "any"; | ||
|
||
if (token.parent.parent.kind === SyntaxKind.BinaryExpression) { | ||
const binaryExpression = token.parent.parent as BinaryExpression; | ||
|
||
const checker = context.program.getTypeChecker(); | ||
const widenedType = checker.getWidenedType(checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(binaryExpression.right))); | ||
typeString = checker.typeToString(widenedType); | ||
} | ||
|
||
const startPos = classDeclaration.members.pos; | ||
|
||
return [{ | ||
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Add_declaration_for_missing_property_0), [token.getText()]), | ||
changes: [{ | ||
fileName: sourceFile.fileName, | ||
textChanges: [{ | ||
span: { start: startPos, length: 0 }, | ||
newText: `${token.getFullText(sourceFile)}: ${typeString};` | ||
}] | ||
}] | ||
}, | ||
{ | ||
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Add_index_signature_for_missing_property_0), [token.getText()]), | ||
changes: [{ | ||
fileName: sourceFile.fileName, | ||
textChanges: [{ | ||
span: { start: startPos, length: 0 }, | ||
newText: `[name: string]: ${typeString};` | ||
}] | ||
}] | ||
}]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// class A { | ||
//// a: number; | ||
//// b: string; | ||
//// constructor(public x: any) {} | ||
//// } | ||
//// [|class B { | ||
//// constructor() { | ||
//// this.x = new A(3); | ||
//// } | ||
//// }|] | ||
|
||
verify.rangeAfterCodeFix(` | ||
class B { | ||
x: A; | ||
constructor() { | ||
this.x = new A(3); | ||
} | ||
} | ||
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0); |
22 changes: 22 additions & 0 deletions
22
tests/cases/fourslash/codeFixUndeclaredClassInstanceWithTypeParams.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// class A<T> { | ||
//// a: number; | ||
//// b: string; | ||
//// constructor(public x: T) {} | ||
//// } | ||
//// [|class B { | ||
//// constructor() { | ||
//// this.x = new A(3); | ||
//// } | ||
//// }|] | ||
|
||
verify.rangeAfterCodeFix(` | ||
class B { | ||
x: A<number>; | ||
constructor() { | ||
this.x = new A(3); | ||
} | ||
} | ||
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0); |
17 changes: 17 additions & 0 deletions
17
tests/cases/fourslash/codeFixUndeclaredIndexSignatureNumericLiteral.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
20 changes: 20 additions & 0 deletions
20
tests/cases/fourslash/codeFixUndeclaredPropertyFunctionEmptyClass.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
22 changes: 22 additions & 0 deletions
22
tests/cases/fourslash/codeFixUndeclaredPropertyFunctionNonEmptyClass.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
17 changes: 17 additions & 0 deletions
17
tests/cases/fourslash/codeFixUndeclaredPropertyNumericLiteral.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// [|class A { | ||
//// constructor() { | ||
//// this.x = 10; | ||
//// } | ||
//// }|] | ||
|
||
verify.rangeAfterCodeFix(` | ||
class A { | ||
x: number; | ||
constructor() { | ||
this.x = 10; | ||
} | ||
} | ||
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0); |
19 changes: 19 additions & 0 deletions
19
tests/cases/fourslash/codeFixUndeclaredPropertyObjectLiteral.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// [|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: any; d: any; 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); |
21 changes: 21 additions & 0 deletions
21
tests/cases/fourslash/codeFixUndeclaredPropertyObjectLiteralStrictNullChecks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
Oops, something went wrong.