-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Adds support for class completions after ASI inserted class property definition #32243
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1538,7 +1538,7 @@ namespace ts.Completions { | |
| * Relevant symbols are stored in the captured 'symbols' variable. | ||
| */ | ||
| function tryGetClassLikeCompletionSymbols(): GlobalsSearch { | ||
| const decl = tryGetObjectTypeDeclarationCompletionContainer(sourceFile, contextToken, location); | ||
| const decl = tryGetObjectTypeDeclarationCompletionContainer(sourceFile, contextToken, location, position); | ||
| if (!decl) return GlobalsSearch.Continue; | ||
|
|
||
| // We're looking up possible property names from parent type. | ||
|
|
@@ -2155,7 +2155,7 @@ namespace ts.Completions { | |
| * Returns the immediate owning class declaration of a context token, | ||
| * on the condition that one exists and that the context implies completion should be given. | ||
| */ | ||
| function tryGetObjectTypeDeclarationCompletionContainer(sourceFile: SourceFile, contextToken: Node | undefined, location: Node): ObjectTypeDeclaration | undefined { | ||
| function tryGetObjectTypeDeclarationCompletionContainer(sourceFile: SourceFile, contextToken: Node | undefined, location: Node, position: number): ObjectTypeDeclaration | undefined { | ||
| // class c { method() { } | method2() { } } | ||
| switch (location.kind) { | ||
| case SyntaxKind.SyntaxList: | ||
|
|
@@ -2165,9 +2165,15 @@ namespace ts.Completions { | |
| if (cls && !findChildOfKind(cls, SyntaxKind.CloseBraceToken, sourceFile)) { | ||
| return cls; | ||
| } | ||
| break; | ||
| case SyntaxKind.Identifier: // class c extends React.Component { a: () => 1\n compon| } | ||
| if (isFromObjectTypeDeclaration(location)) { | ||
| return findAncestor(location, isObjectTypeDeclaration); | ||
| } | ||
| } | ||
|
|
||
| if (!contextToken) return undefined; | ||
|
|
||
| switch (contextToken.kind) { | ||
| case SyntaxKind.SemicolonToken: // class c {getValue(): number; | } | ||
| case SyntaxKind.CloseBraceToken: // class c { method() { } | } | ||
|
|
@@ -2179,7 +2185,13 @@ namespace ts.Completions { | |
| case SyntaxKind.CommaToken: // class c {getValue(): number, | } | ||
| return tryCast(contextToken.parent, isObjectTypeDeclaration); | ||
| default: | ||
| if (!isFromObjectTypeDeclaration(contextToken)) return undefined; | ||
| if (!isFromObjectTypeDeclaration(contextToken)) { | ||
| // class c extends React.Component { a: () => 1\n| } | ||
| if (getLineAndCharacterOfPosition(sourceFile, contextToken.getEnd()).line !== getLineAndCharacterOfPosition(sourceFile, position).line && isObjectTypeDeclaration(location)) { | ||
|
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. Can you please reverse the order of checks here (that is checking 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. Switching the if ordering changes the behavior from what it previously was, and starts to trigger behavior which I think we don't want. This fourslash test fails: With Which is returning an empty array of completions |
||
| return location; | ||
| } | ||
| return undefined; | ||
| } | ||
| const isValidKeyword = isClassLike(contextToken.parent.parent) ? isClassMemberCompletionKeyword : isInterfaceOrTypeLiteralCompletionKeyword; | ||
| return (isValidKeyword(contextToken.kind) || contextToken.kind === SyntaxKind.AsteriskToken || isIdentifier(contextToken) && isValidKeyword(stringToToken(contextToken.text)!)) // TODO: GH#18217 | ||
| ? contextToken.parent.parent as ObjectTypeDeclaration : undefined; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /// <reference path='fourslash.ts'/> | ||
|
|
||
| //// class Parent { | ||
| //// protected shouldWork() { | ||
| //// console.log(); | ||
| //// } | ||
| //// } | ||
| //// | ||
| //// class Child extends Parent { | ||
| //// // this assumes ASI, but on next line wants to | ||
| //// x = () => 1 | ||
| //// shoul/*insideid*/ | ||
| //// } | ||
| //// | ||
| //// class ChildTwo extends Parent { | ||
| //// // this assumes ASI, but on next line wants to | ||
| //// x = () => 1 | ||
| //// /*root*/ //nothing | ||
| //// } | ||
|
|
||
| verify.completions({ marker: ["insideid", "root"], includes: "shouldWork", isNewIdentifierLocation: true }); |
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.
Adding the
isObjectTypeDeclaration(location)miiiight have been overly conservative, but I suggested it to err on the side of caution—the smallest reasonable net I could think of that would catch the repros provided.