-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Missing property fix 2.2 #14133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Missing property fix 2.2 #14133
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
533262c
wip
2187e67
Get Widened Type
f133a67
wip testing
150e2fb
add tests
d9e0fff
use getBaseTypeOfLiteralType
a4cf12e
cleanup
f2770a1
widen type, index signature, and add tests
b62b467
add periods
aozgaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No full stop here?