-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Missing property fix #14097
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 #14097
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9897c69
wip
92e4c6b
Get Widened Type
f047a6e
wip testing
18cba86
add tests
9110461
use getBaseTypeOfLiteralType
cf3b4d6
cleanup
1b6cf97
widen type, index signature, and add tests
6e198f9
Merge branch 'master' into MissingPropertyFix
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.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_accessor_for_missing_property_0), [token.getText()]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed and test added |
||
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/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); |
17 changes: 17 additions & 0 deletions
17
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,17 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// [|class A { | ||
//// constructor() { | ||
//// this.x = { a: 10, b: "hello" }; | ||
//// } | ||
//// }|] | ||
|
||
verify.rangeAfterCodeFix(` | ||
class A { | ||
x: { a: number; b: string; }; | ||
|
||
constructor() { | ||
this.x = { a: 10, b: "hello" }; | ||
} | ||
} | ||
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0); |
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
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.
you also want to widen it. i.e.
null
=>any