Skip to content

Commit 60b2d6a

Browse files
author
Gabriela Araujo Britto
committed
remove unnecessary closures
1 parent 8c4ab69 commit 60b2d6a

File tree

1 file changed

+106
-108
lines changed

1 file changed

+106
-108
lines changed

src/services/refactors/convertToNamedParameters.ts

Lines changed: 106 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,20 @@ namespace ts.refactor.convertToNamedParameters {
7979
const checker = program.getTypeChecker();
8080

8181
const references = flatMap(names, /*mapfn*/ name => FindAllReferences.getReferenceEntriesForNode(-1, name, program, program.getSourceFiles(), cancellationToken));
82-
const isConstructor = isConstructorDeclaration(functionDeclaration);
83-
const groupedReferences = groupReferences(references, isConstructor);
82+
const groupedReferences = groupReferences(references);
8483

8584
if (!every(groupedReferences.declarations, decl => contains(names, decl))) {
8685
groupedReferences.valid = false;
8786
}
8887

8988
return groupedReferences;
9089

91-
function groupReferences(referenceEntries: ReadonlyArray<FindAllReferences.Entry>, isConstructor: boolean): GroupedReferences {
90+
function groupReferences(referenceEntries: ReadonlyArray<FindAllReferences.Entry>): GroupedReferences {
9291
const classReferences: ClassReferences = { accessExpressions: [], typeUsages: [] };
9392
const groupedReferences: GroupedReferences = { functionCalls: [], declarations: [], classReferences, valid: true };
9493
const functionSymbols = map(functionNames, checker.getSymbolAtLocation);
9594
const classSymbols = map(classNames, checker.getSymbolAtLocation);
95+
const isConstructor = isConstructorDeclaration(functionDeclaration);
9696

9797
for (const entry of referenceEntries) {
9898
if (entry.kind !== FindAllReferences.EntryKind.Node) {
@@ -141,95 +141,93 @@ namespace ts.refactor.convertToNamedParameters {
141141

142142
return groupedReferences;
143143
}
144+
}
144145

145-
function symbolComparer(a: Symbol, b: Symbol): boolean {
146-
return getSymbolTarget(a) === getSymbolTarget(b);
147-
}
146+
function symbolComparer(a: Symbol, b: Symbol): boolean {
147+
return getSymbolTarget(a) === getSymbolTarget(b);
148+
}
148149

149-
function entryToDeclaration(entry: FindAllReferences.NodeEntry): Node | undefined {
150-
if (isDeclaration(entry.node.parent)) {
151-
return entry.node;
152-
}
153-
return undefined;
150+
function entryToDeclaration(entry: FindAllReferences.NodeEntry): Node | undefined {
151+
if (isDeclaration(entry.node.parent)) {
152+
return entry.node;
154153
}
154+
return undefined;
155+
}
155156

156-
function entryToFunctionCall(entry: FindAllReferences.NodeEntry): CallExpression | NewExpression | undefined {
157-
if (entry.node.parent) {
158-
const functionReference = entry.node;
159-
const parent = functionReference.parent;
160-
switch (parent.kind) {
161-
// Function call (foo(...) or super(...))
162-
case SyntaxKind.CallExpression:
163-
const callExpression = tryCast(parent, isCallExpression);
164-
if (callExpression && callExpression.expression === functionReference) {
157+
function entryToFunctionCall(entry: FindAllReferences.NodeEntry): CallExpression | NewExpression | undefined {
158+
if (entry.node.parent) {
159+
const functionReference = entry.node;
160+
const parent = functionReference.parent;
161+
switch (parent.kind) {
162+
// Function call (foo(...) or super(...))
163+
case SyntaxKind.CallExpression:
164+
const callExpression = tryCast(parent, isCallExpression);
165+
if (callExpression && callExpression.expression === functionReference) {
166+
return callExpression;
167+
}
168+
break;
169+
// Constructor call (new Foo(...))
170+
case SyntaxKind.NewExpression:
171+
const newExpression = tryCast(parent, isNewExpression);
172+
if (newExpression && newExpression.expression === functionReference) {
173+
return newExpression;
174+
}
175+
break;
176+
// Method call (x.foo(...))
177+
case SyntaxKind.PropertyAccessExpression:
178+
const propertyAccessExpression = tryCast(parent, isPropertyAccessExpression);
179+
if (propertyAccessExpression && propertyAccessExpression.parent && propertyAccessExpression.name === functionReference) {
180+
const callExpression = tryCast(propertyAccessExpression.parent, isCallExpression);
181+
if (callExpression && callExpression.expression === propertyAccessExpression) {
165182
return callExpression;
166183
}
167-
break;
168-
// Constructor call (new Foo(...))
169-
case SyntaxKind.NewExpression:
170-
const newExpression = tryCast(parent, isNewExpression);
171-
if (newExpression && newExpression.expression === functionReference) {
172-
return newExpression;
173-
}
174-
break;
175-
// Method call (x.foo(...))
176-
case SyntaxKind.PropertyAccessExpression:
177-
const propertyAccessExpression = tryCast(parent, isPropertyAccessExpression);
178-
if (propertyAccessExpression && propertyAccessExpression.parent && propertyAccessExpression.name === functionReference) {
179-
const callExpression = tryCast(propertyAccessExpression.parent, isCallExpression);
180-
if (callExpression && callExpression.expression === propertyAccessExpression) {
181-
return callExpression;
182-
}
183-
}
184-
break;
185-
// Method call (x["foo"](...))
186-
case SyntaxKind.ElementAccessExpression:
187-
const elementAccessExpression = tryCast(parent, isElementAccessExpression);
188-
if (elementAccessExpression && elementAccessExpression.parent && elementAccessExpression.argumentExpression === functionReference) {
189-
const callExpression = tryCast(elementAccessExpression.parent, isCallExpression);
190-
if (callExpression && callExpression.expression === elementAccessExpression) {
191-
return callExpression;
192-
}
184+
}
185+
break;
186+
// Method call (x["foo"](...))
187+
case SyntaxKind.ElementAccessExpression:
188+
const elementAccessExpression = tryCast(parent, isElementAccessExpression);
189+
if (elementAccessExpression && elementAccessExpression.parent && elementAccessExpression.argumentExpression === functionReference) {
190+
const callExpression = tryCast(elementAccessExpression.parent, isCallExpression);
191+
if (callExpression && callExpression.expression === elementAccessExpression) {
192+
return callExpression;
193193
}
194-
break;
195-
}
194+
}
195+
break;
196196
}
197-
return undefined;
198197
}
198+
return undefined;
199+
}
199200

200-
function entryToAccessExpression(entry: FindAllReferences.NodeEntry): ElementAccessExpression | PropertyAccessExpression | undefined {
201-
if (entry.node.parent) {
202-
const reference = entry.node;
203-
const parent = reference.parent;
204-
switch (parent.kind) {
205-
// `C.foo`
206-
case SyntaxKind.PropertyAccessExpression:
207-
const propertyAccessExpression = tryCast(parent, isPropertyAccessExpression);
208-
if (propertyAccessExpression && propertyAccessExpression.expression === reference) {
209-
return propertyAccessExpression;
210-
}
211-
break;
212-
// `C["foo"]`
213-
case SyntaxKind.ElementAccessExpression:
214-
const elementAccessExpression = tryCast(parent, isElementAccessExpression);
215-
if (elementAccessExpression && elementAccessExpression.expression === reference) {
216-
return elementAccessExpression;
217-
}
218-
break;
219-
}
201+
function entryToAccessExpression(entry: FindAllReferences.NodeEntry): ElementAccessExpression | PropertyAccessExpression | undefined {
202+
if (entry.node.parent) {
203+
const reference = entry.node;
204+
const parent = reference.parent;
205+
switch (parent.kind) {
206+
// `C.foo`
207+
case SyntaxKind.PropertyAccessExpression:
208+
const propertyAccessExpression = tryCast(parent, isPropertyAccessExpression);
209+
if (propertyAccessExpression && propertyAccessExpression.expression === reference) {
210+
return propertyAccessExpression;
211+
}
212+
break;
213+
// `C["foo"]`
214+
case SyntaxKind.ElementAccessExpression:
215+
const elementAccessExpression = tryCast(parent, isElementAccessExpression);
216+
if (elementAccessExpression && elementAccessExpression.expression === reference) {
217+
return elementAccessExpression;
218+
}
219+
break;
220220
}
221-
return undefined;
222221
}
222+
return undefined;
223+
}
223224

224-
function entryToType(entry: FindAllReferences.Entry): Node | undefined {
225-
if (entry.kind === FindAllReferences.EntryKind.Node) {
226-
const reference = entry.node;
227-
if (getMeaningFromLocation(reference) === SemanticMeaning.Type || isExpressionWithTypeArgumentsInClassExtendsClause(reference.parent)) {
228-
return reference;
229-
}
230-
}
231-
return undefined;
225+
function entryToType(entry: FindAllReferences.NodeEntry): Node | undefined {
226+
const reference = entry.node;
227+
if (getMeaningFromLocation(reference) === SemanticMeaning.Type || isExpressionWithTypeArgumentsInClassExtendsClause(reference.parent)) {
228+
return reference;
232229
}
230+
return undefined;
233231
}
234232

235233
function getFunctionDeclarationAtPosition(file: SourceFile, startPosition: number, checker: TypeChecker): ValidFunctionDeclaration | undefined {
@@ -261,18 +259,18 @@ namespace ts.refactor.convertToNamedParameters {
261259
return isValidVariableDeclaration(functionDeclaration.parent);
262260
}
263261
return false;
262+
}
264263

265-
function isValidParameterNodeArray(parameters: NodeArray<ParameterDeclaration>): parameters is ValidParameterNodeArray {
266-
return getRefactorableParametersLength(parameters) >= minimumParameterLength && every(parameters, isValidParameterDeclaration);
267-
}
264+
function isValidParameterNodeArray(parameters: NodeArray<ParameterDeclaration>): parameters is ValidParameterNodeArray {
265+
return getRefactorableParametersLength(parameters) >= minimumParameterLength && every(parameters, isValidParameterDeclaration);
266+
}
268267

269-
function isValidParameterDeclaration(paramDeclaration: ParameterDeclaration): paramDeclaration is ValidParameterDeclaration {
270-
return !paramDeclaration.modifiers && !paramDeclaration.decorators && isIdentifier(paramDeclaration.name);
271-
}
268+
function isValidParameterDeclaration(paramDeclaration: ParameterDeclaration): paramDeclaration is ValidParameterDeclaration {
269+
return !paramDeclaration.modifiers && !paramDeclaration.decorators && isIdentifier(paramDeclaration.name);
270+
}
272271

273-
function isValidVariableDeclaration(node: Node): node is ValidVariableDeclaration {
274-
return isVariableDeclaration(node) && isVarConst(node) && isIdentifier(node.name) && !node.type; // TODO: GH#30113
275-
}
272+
function isValidVariableDeclaration(node: Node): node is ValidVariableDeclaration {
273+
return isVariableDeclaration(node) && isVarConst(node) && isIdentifier(node.name) && !node.type; // TODO: GH#30113
276274
}
277275

278276
function hasThisParameter(parameters: NodeArray<ParameterDeclaration>): boolean {
@@ -357,20 +355,6 @@ namespace ts.refactor.convertToNamedParameters {
357355
}
358356
return createNodeArray([objectParameter]);
359357

360-
function createBindingElementFromParameterDeclaration(parameterDeclaration: ValidParameterDeclaration): BindingElement {
361-
const element = createBindingElement(
362-
/*dotDotDotToken*/ undefined,
363-
/*propertyName*/ undefined,
364-
getParameterName(parameterDeclaration),
365-
isRestParameter(parameterDeclaration) ? createArrayLiteral() : parameterDeclaration.initializer);
366-
367-
suppressLeadingAndTrailingTrivia(element);
368-
if (parameterDeclaration.initializer && element.initializer) {
369-
copyComments(parameterDeclaration.initializer, element.initializer);
370-
}
371-
return element;
372-
}
373-
374358
function createParameterTypeNode(parameters: NodeArray<ValidParameterDeclaration>): TypeLiteralNode {
375359
const members = map(parameters, createPropertySignatureFromParameterDeclaration);
376360
const typeNode = addEmitFlags(createTypeLiteralNode(members), EmitFlags.SingleLine);
@@ -406,6 +390,20 @@ namespace ts.refactor.convertToNamedParameters {
406390
}
407391
}
408392

393+
function createBindingElementFromParameterDeclaration(parameterDeclaration: ValidParameterDeclaration): BindingElement {
394+
const element = createBindingElement(
395+
/*dotDotDotToken*/ undefined,
396+
/*propertyName*/ undefined,
397+
getParameterName(parameterDeclaration),
398+
isRestParameter(parameterDeclaration) ? createArrayLiteral() : parameterDeclaration.initializer);
399+
400+
suppressLeadingAndTrailingTrivia(element);
401+
if (parameterDeclaration.initializer && element.initializer) {
402+
copyComments(parameterDeclaration.initializer, element.initializer);
403+
}
404+
return element;
405+
}
406+
409407
function copyComments(sourceNode: Node, targetNode: Node) {
410408
const sourceFile = sourceNode.getSourceFile();
411409
const text = sourceFile.text;
@@ -416,15 +414,15 @@ namespace ts.refactor.convertToNamedParameters {
416414
copyTrailingAsLeadingComments(sourceNode, targetNode, sourceFile);
417415
}
418416
copyTrailingComments(sourceNode, targetNode, sourceFile);
417+
}
419418

420-
function hasLeadingLineBreak(node: Node, text: string) {
421-
const start = node.getFullStart();
422-
const end = node.getStart();
423-
for (let i = start; i < end; i++) {
424-
if (text.charCodeAt(i) === CharacterCodes.lineFeed) return true;
425-
}
426-
return false;
419+
function hasLeadingLineBreak(node: Node, text: string) {
420+
const start = node.getFullStart();
421+
const end = node.getStart();
422+
for (let i = start; i < end; i++) {
423+
if (text.charCodeAt(i) === CharacterCodes.lineFeed) return true;
427424
}
425+
return false;
428426
}
429427

430428
function getParameterName(paramDeclaration: ValidParameterDeclaration) {

0 commit comments

Comments
 (0)