-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fix type keyword completions #32474
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
Fix type keyword completions #32474
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -947,11 +947,13 @@ namespace ts.Completions { | |
// Right of dot member completion list | ||
completionKind = CompletionKind.PropertyAccess; | ||
|
||
// Since this is qualified name check its a type node location | ||
// Since this is qualified name check it's a type node location | ||
const isImportType = isLiteralImportTypeNode(node); | ||
const isTypeLocation = insideJsDocTagTypeExpression || (isImportType && !(node as ImportTypeNode).isTypeOf) || isPartOfTypeNode(node.parent); | ||
const isTypeLocation = insideJsDocTagTypeExpression | ||
|| (isImportType && !(node as ImportTypeNode).isTypeOf) | ||
|| isPartOfTypeNode(node.parent) | ||
|| isPossiblyTypeArgumentPosition(contextToken, sourceFile, typeChecker); | ||
const isRhsOfImportDeclaration = isInRightSideOfInternalImportEqualsDeclaration(node); | ||
const allowTypeOrValue = isRhsOfImportDeclaration || (!isTypeLocation && isPossiblyTypeArgumentPosition(contextToken, sourceFile, typeChecker)); | ||
if (isEntityName(node) || isImportType) { | ||
const isNamespaceName = isModuleDeclaration(node.parent); | ||
if (isNamespaceName) isNewIdentifierLocation = true; | ||
|
@@ -968,7 +970,7 @@ namespace ts.Completions { | |
isNamespaceName | ||
// At `namespace N.M/**/`, if this is the only declaration of `M`, don't include `M` as a completion. | ||
? symbol => !!(symbol.flags & SymbolFlags.Namespace) && !symbol.declarations.every(d => d.parent === node.parent) | ||
: allowTypeOrValue ? | ||
: isRhsOfImportDeclaration ? | ||
// Any kind is allowed when dotting off namespace in internal import equals declaration | ||
symbol => isValidTypeAccess(symbol) || isValidValueAccess(symbol) : | ||
isTypeLocation ? isValidTypeAccess : isValidValueAccess; | ||
|
@@ -1181,7 +1183,6 @@ namespace ts.Completions { | |
|
||
function filterGlobalCompletion(symbols: Symbol[]): void { | ||
const isTypeOnly = isTypeOnlyCompletion(); | ||
const allowTypes = isTypeOnly || !isContextTokenValueLocation(contextToken) && isPossiblyTypeArgumentPosition(contextToken, sourceFile, typeChecker); | ||
if (isTypeOnly) { | ||
keywordFilters = isTypeAssertion() | ||
? KeywordCompletionFilters.TypeAssertionKeywords | ||
|
@@ -1202,12 +1203,9 @@ namespace ts.Completions { | |
return !!(symbol.flags & SymbolFlags.Namespace); | ||
} | ||
|
||
if (allowTypes) { | ||
// Its a type, but you can reach it by namespace.type as well | ||
const symbolAllowedAsType = symbolCanBeReferencedAtTypeLocation(symbol); | ||
if (symbolAllowedAsType || isTypeOnly) { | ||
return symbolAllowedAsType; | ||
} | ||
if (isTypeOnly) { | ||
// It's a type, but you can reach it by namespace.type as well | ||
return symbolCanBeReferencedAtTypeLocation(symbol); | ||
} | ||
} | ||
|
||
|
@@ -1221,7 +1219,11 @@ namespace ts.Completions { | |
} | ||
|
||
function isTypeOnlyCompletion(): boolean { | ||
return insideJsDocTagTypeExpression || !isContextTokenValueLocation(contextToken) && (isPartOfTypeNode(location) || isContextTokenTypeLocation(contextToken)); | ||
return insideJsDocTagTypeExpression | ||
|| !isContextTokenValueLocation(contextToken) && | ||
(isPossiblyTypeArgumentPosition(contextToken, sourceFile, typeChecker) | ||
|| isPartOfTypeNode(location) | ||
|| isContextTokenTypeLocation(contextToken)); | ||
} | ||
|
||
function isContextTokenValueLocation(contextToken: Node) { | ||
|
@@ -2060,16 +2062,18 @@ namespace ts.Completions { | |
case KeywordCompletionFilters.None: | ||
return false; | ||
case KeywordCompletionFilters.All: | ||
return kind === SyntaxKind.AsyncKeyword || SyntaxKind.AwaitKeyword || !isContextualKeyword(kind) && !isClassMemberCompletionKeyword(kind) || kind === SyntaxKind.DeclareKeyword || kind === SyntaxKind.ModuleKeyword | ||
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. @andrewbranch Here! |
||
return isFunctionLikeBodyKeyword(kind) | ||
|| kind === SyntaxKind.DeclareKeyword | ||
|| kind === SyntaxKind.ModuleKeyword | ||
|| isTypeKeyword(kind) && kind !== SyntaxKind.UndefinedKeyword; | ||
case KeywordCompletionFilters.FunctionLikeBodyKeywords: | ||
return isFunctionLikeBodyKeyword(kind); | ||
case KeywordCompletionFilters.ClassElementKeywords: | ||
return isClassMemberCompletionKeyword(kind); | ||
case KeywordCompletionFilters.InterfaceElementKeywords: | ||
return isInterfaceOrTypeLiteralCompletionKeyword(kind); | ||
case KeywordCompletionFilters.ConstructorParameterKeywords: | ||
return isParameterPropertyModifier(kind); | ||
case KeywordCompletionFilters.FunctionLikeBodyKeywords: | ||
return isFunctionLikeBodyKeyword(kind); | ||
case KeywordCompletionFilters.TypeAssertionKeywords: | ||
return isTypeKeyword(kind) || kind === SyntaxKind.ConstKeyword; | ||
case KeywordCompletionFilters.TypeKeywords: | ||
|
@@ -2132,7 +2136,9 @@ namespace ts.Completions { | |
} | ||
|
||
function isFunctionLikeBodyKeyword(kind: SyntaxKind) { | ||
return kind === SyntaxKind.AsyncKeyword || kind === SyntaxKind.AwaitKeyword || !isContextualKeyword(kind) && !isClassMemberCompletionKeyword(kind); | ||
return kind === SyntaxKind.AsyncKeyword | ||
|| kind === SyntaxKind.AwaitKeyword | ||
|| !isContextualKeyword(kind) && !isClassMemberCompletionKeyword(kind); | ||
} | ||
|
||
function keywordForNode(node: Node): SyntaxKind { | ||
|
27 changes: 27 additions & 0 deletions
27
tests/cases/fourslash/completionInFunctionLikeBody_includesPrimitiveTypes.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,27 @@ | ||
/// <reference path='fourslash.ts'/> | ||
|
||
//// class Foo<T> { } | ||
//// class Bar { } | ||
//// function includesTypes() { | ||
//// new Foo</*1*/ | ||
//// } | ||
//// function excludesTypes1() { | ||
//// new Bar</*2*/ | ||
//// } | ||
//// function excludesTypes2() { | ||
//// 1</*3*/ | ||
//// } | ||
|
||
verify.completions( | ||
{ | ||
marker: ["1"], | ||
includes: [ | ||
{ name: "string", sortText: completion.SortText.GlobalsOrKeywords }, | ||
{ name: "String", sortText: completion.SortText.GlobalsOrKeywords }, | ||
], | ||
}, | ||
{ | ||
marker: ["2", "3"], | ||
excludes: ["string"] | ||
} | ||
); |
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
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.
This is awesome. What was the change that actually made all these disappear from globals though?