Skip to content

Commit 6118f21

Browse files
author
Andy
authored
Add 'isParameterPropertyModifier' helper (microsoft#22841)
1 parent 4f16560 commit 6118f21

4 files changed

Lines changed: 17 additions & 64 deletions

File tree

src/compiler/parser.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5461,19 +5461,6 @@ namespace ts {
54615461
return finishNode(node);
54625462
}
54635463

5464-
function isClassMemberModifier(idToken: SyntaxKind) {
5465-
switch (idToken) {
5466-
case SyntaxKind.PublicKeyword:
5467-
case SyntaxKind.PrivateKeyword:
5468-
case SyntaxKind.ProtectedKeyword:
5469-
case SyntaxKind.StaticKeyword:
5470-
case SyntaxKind.ReadonlyKeyword:
5471-
return true;
5472-
default:
5473-
return false;
5474-
}
5475-
}
5476-
54775464
function isClassMemberStart(): boolean {
54785465
let idToken: SyntaxKind;
54795466

src/compiler/utilities.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5436,6 +5436,16 @@ namespace ts {
54365436
return false;
54375437
}
54385438

5439+
/* @internal */
5440+
export function isParameterPropertyModifier(kind: SyntaxKind): boolean {
5441+
return !!(modifierToFlag(kind) & ModifierFlags.ParameterPropertyModifier);
5442+
}
5443+
5444+
/* @internal */
5445+
export function isClassMemberModifier(idToken: SyntaxKind): boolean {
5446+
return isParameterPropertyModifier(idToken) || idToken === SyntaxKind.StaticKeyword;
5447+
}
5448+
54395449
export function isModifier(node: Node): node is Modifier {
54405450
return isModifierKind(node.kind);
54415451
}

src/services/codefixes/inferFromUsage.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace ts.codefix {
5858
}
5959

6060
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Node, errorCode: number, program: Program, cancellationToken: CancellationToken, seenFunctions?: Map<true>): Declaration | undefined {
61-
if (!isAllowedTokenKind(token.kind)) {
61+
if (!isParameterPropertyModifier(token.kind) && token.kind !== SyntaxKind.Identifier && token.kind !== SyntaxKind.DotDotDotToken) {
6262
return undefined;
6363
}
6464

@@ -125,20 +125,6 @@ namespace ts.codefix {
125125
}
126126
}
127127

128-
function isAllowedTokenKind(kind: SyntaxKind): boolean {
129-
switch (kind) {
130-
case SyntaxKind.Identifier:
131-
case SyntaxKind.DotDotDotToken:
132-
case SyntaxKind.PublicKeyword:
133-
case SyntaxKind.PrivateKeyword:
134-
case SyntaxKind.ProtectedKeyword:
135-
case SyntaxKind.ReadonlyKeyword:
136-
return true;
137-
default:
138-
return false;
139-
}
140-
}
141-
142128
function annotateVariableDeclaration(changes: textChanges.ChangeTracker, sourceFile: SourceFile, declaration: VariableDeclaration | PropertyDeclaration | PropertySignature, program: Program, cancellationToken: CancellationToken): void {
143129
if (isIdentifier(declaration.name)) {
144130
annotate(changes, sourceFile, declaration, inferTypeForVariableFromUsage(declaration.name, program, cancellationToken), program);

src/services/completions.ts

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,7 @@ namespace ts.Completions {
16451645
function isConstructorParameterCompletion(node: Node) {
16461646
return node.parent &&
16471647
isParameterOfConstructorDeclaration(node.parent) &&
1648-
(isConstructorParameterCompletionKeyword(node.kind) || isDeclarationName(node));
1648+
(isParameterPropertyModifier(node.kind) || isDeclarationName(node));
16491649
}
16501650

16511651
/**
@@ -1840,7 +1840,7 @@ namespace ts.Completions {
18401840
// - its name of the parameter and not being edited
18411841
// eg. constructor(a |<- this shouldnt show completion
18421842
if (!isIdentifier(contextToken) ||
1843-
isConstructorParameterCompletionKeyword(keywordForNode(contextToken)) ||
1843+
isParameterPropertyModifier(keywordForNode(contextToken)) ||
18441844
isCurrentlyEditingNode(contextToken)) {
18451845
return false;
18461846
}
@@ -2118,9 +2118,9 @@ namespace ts.Completions {
21182118
case KeywordCompletionFilters.InterfaceElementKeywords:
21192119
return isInterfaceOrTypeLiteralCompletionKeyword(kind);
21202120
case KeywordCompletionFilters.ConstructorParameterKeywords:
2121-
return isConstructorParameterCompletionKeyword(kind);
2121+
return isParameterPropertyModifier(kind);
21222122
case KeywordCompletionFilters.FunctionLikeBodyKeywords:
2123-
return isFunctionLikeBodyCompletionKeyword(kind);
2123+
return !isClassMemberCompletionKeyword(kind);
21242124
case KeywordCompletionFilters.TypeKeywords:
21252125
return isTypeKeyword(kind);
21262126
default:
@@ -2135,51 +2135,21 @@ namespace ts.Completions {
21352135

21362136
function isClassMemberCompletionKeyword(kind: SyntaxKind) {
21372137
switch (kind) {
2138-
case SyntaxKind.PublicKeyword:
2139-
case SyntaxKind.ProtectedKeyword:
2140-
case SyntaxKind.PrivateKeyword:
21412138
case SyntaxKind.AbstractKeyword:
2142-
case SyntaxKind.StaticKeyword:
21432139
case SyntaxKind.ConstructorKeyword:
2144-
case SyntaxKind.ReadonlyKeyword:
21452140
case SyntaxKind.GetKeyword:
21462141
case SyntaxKind.SetKeyword:
21472142
case SyntaxKind.AsyncKeyword:
21482143
return true;
2144+
default:
2145+
return isClassMemberModifier(kind);
21492146
}
21502147
}
21512148

21522149
function keywordForNode(node: Node): SyntaxKind {
21532150
return isIdentifier(node) ? node.originalKeywordKind || SyntaxKind.Unknown : node.kind;
21542151
}
21552152

2156-
function isConstructorParameterCompletionKeyword(kind: SyntaxKind) {
2157-
switch (kind) {
2158-
case SyntaxKind.PublicKeyword:
2159-
case SyntaxKind.PrivateKeyword:
2160-
case SyntaxKind.ProtectedKeyword:
2161-
case SyntaxKind.ReadonlyKeyword:
2162-
return true;
2163-
}
2164-
}
2165-
2166-
function isFunctionLikeBodyCompletionKeyword(kind: SyntaxKind) {
2167-
switch (kind) {
2168-
case SyntaxKind.PublicKeyword:
2169-
case SyntaxKind.PrivateKeyword:
2170-
case SyntaxKind.ProtectedKeyword:
2171-
case SyntaxKind.ReadonlyKeyword:
2172-
case SyntaxKind.ConstructorKeyword:
2173-
case SyntaxKind.StaticKeyword:
2174-
case SyntaxKind.AbstractKeyword:
2175-
case SyntaxKind.GetKeyword:
2176-
case SyntaxKind.SetKeyword:
2177-
case SyntaxKind.UndefinedKeyword:
2178-
return false;
2179-
}
2180-
return true;
2181-
}
2182-
21832153
function isEqualityOperatorKind(kind: SyntaxKind): kind is EqualityOperator {
21842154
switch (kind) {
21852155
case SyntaxKind.EqualsEqualsEqualsToken:

0 commit comments

Comments
 (0)