From 6118f211d1b650f061084d9b5c74cfa4e64cc7a7 Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 27 Mar 2018 19:36:42 -0700 Subject: [PATCH] Add 'isParameterPropertyModifier' helper (#22841) --- src/compiler/parser.ts | 13 -------- src/compiler/utilities.ts | 10 ++++++ src/services/codefixes/inferFromUsage.ts | 16 +-------- src/services/completions.ts | 42 ++++-------------------- 4 files changed, 17 insertions(+), 64 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index b5c780de934..c3709fa4e0c 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5461,19 +5461,6 @@ namespace ts { return finishNode(node); } - function isClassMemberModifier(idToken: SyntaxKind) { - switch (idToken) { - case SyntaxKind.PublicKeyword: - case SyntaxKind.PrivateKeyword: - case SyntaxKind.ProtectedKeyword: - case SyntaxKind.StaticKeyword: - case SyntaxKind.ReadonlyKeyword: - return true; - default: - return false; - } - } - function isClassMemberStart(): boolean { let idToken: SyntaxKind; diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index fa49837fa84..1022e6c11eb 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -5436,6 +5436,16 @@ namespace ts { return false; } + /* @internal */ + export function isParameterPropertyModifier(kind: SyntaxKind): boolean { + return !!(modifierToFlag(kind) & ModifierFlags.ParameterPropertyModifier); + } + + /* @internal */ + export function isClassMemberModifier(idToken: SyntaxKind): boolean { + return isParameterPropertyModifier(idToken) || idToken === SyntaxKind.StaticKeyword; + } + export function isModifier(node: Node): node is Modifier { return isModifierKind(node.kind); } diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 8d46bbf4ded..8398bc7c6d5 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -58,7 +58,7 @@ namespace ts.codefix { } function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Node, errorCode: number, program: Program, cancellationToken: CancellationToken, seenFunctions?: Map): Declaration | undefined { - if (!isAllowedTokenKind(token.kind)) { + if (!isParameterPropertyModifier(token.kind) && token.kind !== SyntaxKind.Identifier && token.kind !== SyntaxKind.DotDotDotToken) { return undefined; } @@ -125,20 +125,6 @@ namespace ts.codefix { } } - function isAllowedTokenKind(kind: SyntaxKind): boolean { - switch (kind) { - case SyntaxKind.Identifier: - case SyntaxKind.DotDotDotToken: - case SyntaxKind.PublicKeyword: - case SyntaxKind.PrivateKeyword: - case SyntaxKind.ProtectedKeyword: - case SyntaxKind.ReadonlyKeyword: - return true; - default: - return false; - } - } - function annotateVariableDeclaration(changes: textChanges.ChangeTracker, sourceFile: SourceFile, declaration: VariableDeclaration | PropertyDeclaration | PropertySignature, program: Program, cancellationToken: CancellationToken): void { if (isIdentifier(declaration.name)) { annotate(changes, sourceFile, declaration, inferTypeForVariableFromUsage(declaration.name, program, cancellationToken), program); diff --git a/src/services/completions.ts b/src/services/completions.ts index 57e071efc1a..c0f11d516b9 100644 --- a/src/services/completions.ts +++ b/src/services/completions.ts @@ -1645,7 +1645,7 @@ namespace ts.Completions { function isConstructorParameterCompletion(node: Node) { return node.parent && isParameterOfConstructorDeclaration(node.parent) && - (isConstructorParameterCompletionKeyword(node.kind) || isDeclarationName(node)); + (isParameterPropertyModifier(node.kind) || isDeclarationName(node)); } /** @@ -1840,7 +1840,7 @@ namespace ts.Completions { // - its name of the parameter and not being edited // eg. constructor(a |<- this shouldnt show completion if (!isIdentifier(contextToken) || - isConstructorParameterCompletionKeyword(keywordForNode(contextToken)) || + isParameterPropertyModifier(keywordForNode(contextToken)) || isCurrentlyEditingNode(contextToken)) { return false; } @@ -2118,9 +2118,9 @@ namespace ts.Completions { case KeywordCompletionFilters.InterfaceElementKeywords: return isInterfaceOrTypeLiteralCompletionKeyword(kind); case KeywordCompletionFilters.ConstructorParameterKeywords: - return isConstructorParameterCompletionKeyword(kind); + return isParameterPropertyModifier(kind); case KeywordCompletionFilters.FunctionLikeBodyKeywords: - return isFunctionLikeBodyCompletionKeyword(kind); + return !isClassMemberCompletionKeyword(kind); case KeywordCompletionFilters.TypeKeywords: return isTypeKeyword(kind); default: @@ -2135,17 +2135,14 @@ namespace ts.Completions { function isClassMemberCompletionKeyword(kind: SyntaxKind) { switch (kind) { - case SyntaxKind.PublicKeyword: - case SyntaxKind.ProtectedKeyword: - case SyntaxKind.PrivateKeyword: case SyntaxKind.AbstractKeyword: - case SyntaxKind.StaticKeyword: case SyntaxKind.ConstructorKeyword: - case SyntaxKind.ReadonlyKeyword: case SyntaxKind.GetKeyword: case SyntaxKind.SetKeyword: case SyntaxKind.AsyncKeyword: return true; + default: + return isClassMemberModifier(kind); } } @@ -2153,33 +2150,6 @@ namespace ts.Completions { return isIdentifier(node) ? node.originalKeywordKind || SyntaxKind.Unknown : node.kind; } - function isConstructorParameterCompletionKeyword(kind: SyntaxKind) { - switch (kind) { - case SyntaxKind.PublicKeyword: - case SyntaxKind.PrivateKeyword: - case SyntaxKind.ProtectedKeyword: - case SyntaxKind.ReadonlyKeyword: - return true; - } - } - - function isFunctionLikeBodyCompletionKeyword(kind: SyntaxKind) { - switch (kind) { - case SyntaxKind.PublicKeyword: - case SyntaxKind.PrivateKeyword: - case SyntaxKind.ProtectedKeyword: - case SyntaxKind.ReadonlyKeyword: - case SyntaxKind.ConstructorKeyword: - case SyntaxKind.StaticKeyword: - case SyntaxKind.AbstractKeyword: - case SyntaxKind.GetKeyword: - case SyntaxKind.SetKeyword: - case SyntaxKind.UndefinedKeyword: - return false; - } - return true; - } - function isEqualityOperatorKind(kind: SyntaxKind): kind is EqualityOperator { switch (kind) { case SyntaxKind.EqualsEqualsEqualsToken: