Skip to content

Commit 18c3f5f

Browse files
author
Andy
authored
Use getFirstConstructorWithBody in one more place, and simplify other class members iteration (#23567)
1 parent 94cc59c commit 18c3f5f

File tree

4 files changed

+11
-22
lines changed

4 files changed

+11
-22
lines changed

src/compiler/utilities.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -2933,11 +2933,7 @@ namespace ts {
29332933
}
29342934

29352935
export function getFirstConstructorWithBody(node: ClassLikeDeclaration): ConstructorDeclaration {
2936-
return forEach(node.members, member => {
2937-
if (member.kind === SyntaxKind.Constructor && nodeIsPresent((<ConstructorDeclaration>member).body)) {
2938-
return <ConstructorDeclaration>member;
2939-
}
2940-
});
2936+
return find(node.members, (member): member is ConstructorDeclaration => isConstructorDeclaration(member) && nodeIsPresent(member.body));
29412937
}
29422938

29432939
function getSetAccessorValueParameter(accessor: SetAccessorDeclaration): ParameterDeclaration | undefined {

src/services/codefixes/fixStrictClassInitialization.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ namespace ts.codefix {
127127
const classDeclaration = getClassLikeDeclarationOfSymbol(type.symbol);
128128
if (!classDeclaration || hasModifier(classDeclaration, ModifierFlags.Abstract)) return undefined;
129129

130-
const constructorDeclaration = find<ClassElement, ConstructorDeclaration>(classDeclaration.members, (m): m is ConstructorDeclaration => isConstructorDeclaration(m) && !!m.body)!;
130+
const constructorDeclaration = getFirstConstructorWithBody(classDeclaration);
131131
if (constructorDeclaration && constructorDeclaration.parameters.length) return undefined;
132132

133133
return createNew(createIdentifier(type.symbol.name), /*typeArguments*/ undefined, /*argumentsArray*/ undefined);

src/services/findAllReferences.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -1014,21 +1014,17 @@ namespace ts.FindAllReferences.Core {
10141014

10151015
function addClassStaticThisReferences(referenceLocation: Node, search: Search, state: State): void {
10161016
addReference(referenceLocation, search.symbol, state);
1017-
if (!state.options.isForRename && isClassLike(referenceLocation.parent)) {
1018-
Debug.assert(referenceLocation.parent.name === referenceLocation);
1019-
// This is the class declaration.
1020-
addStaticThisReferences(referenceLocation.parent, state.referenceAdder(search.symbol));
1021-
}
1022-
}
1023-
1024-
function addStaticThisReferences(classLike: ClassLikeDeclaration, pusher: (node: Node) => void): void {
1017+
const classLike = referenceLocation.parent;
1018+
if (state.options.isForRename || !isClassLike(classLike)) return;
1019+
Debug.assert(classLike.name === referenceLocation);
1020+
const addRef = state.referenceAdder(search.symbol);
10251021
for (const member of classLike.members) {
10261022
if (!(isMethodOrAccessor(member) && hasModifier(member, ModifierFlags.Static))) {
10271023
continue;
10281024
}
10291025
member.body.forEachChild(function cb(node) {
10301026
if (node.kind === SyntaxKind.ThisKeyword) {
1031-
pusher(node);
1027+
addRef(node);
10321028
}
10331029
else if (!isFunctionLike(node)) {
10341030
node.forEachChild(cb);

src/services/jsDoc.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,10 @@ namespace ts.JsDoc {
365365
case SyntaxKind.FunctionExpression:
366366
case SyntaxKind.ArrowFunction:
367367
return (<FunctionExpression>rightHandSide).parameters;
368-
case SyntaxKind.ClassExpression:
369-
for (const member of (<ClassExpression>rightHandSide).members) {
370-
if (member.kind === SyntaxKind.Constructor) {
371-
return (<ConstructorDeclaration>member).parameters;
372-
}
373-
}
374-
break;
368+
case SyntaxKind.ClassExpression: {
369+
const ctr = find((rightHandSide as ClassExpression).members, isConstructorDeclaration);
370+
return ctr && ctr.parameters;
371+
}
375372
}
376373

377374
return emptyArray;

0 commit comments

Comments
 (0)