Skip to content

Commit 0077016

Browse files
author
Andy
authored
Reduce duplicate code in getListByRange (#28455)
1 parent d569743 commit 0077016

File tree

1 file changed

+29
-46
lines changed

1 file changed

+29
-46
lines changed

src/services/formatting/smartIndenter.ts

+29-46
Original file line numberDiff line numberDiff line change
@@ -322,49 +322,24 @@ namespace ts.formatting {
322322
return false;
323323
}
324324

325-
function getListIfVisualStartEndIsInListRange(list: NodeArray<Node> | undefined, start: number, end: number, node: Node, sourceFile: SourceFile) {
326-
return list && rangeContainsVisualStartEnd(list) ? list : undefined;
327-
328-
// Assumes a list is wrapped by list tokens
329-
function rangeContainsVisualStartEnd(textRange: TextRange): boolean {
330-
const children = node.getChildren();
331-
for (let i = 1; i < children.length - 1; i++) {
332-
if (children[i].pos === textRange.pos && children[i].end === textRange.end) {
333-
return rangeContainsStartEnd({ pos: children[i - 1].end, end: children[i + 1].getStart(sourceFile) }, start, end);
334-
}
335-
}
336-
return rangeContainsStartEnd(textRange, start, end);
337-
}
338-
}
339-
340-
function getListIfStartEndIsInListRange(list: NodeArray<Node> | undefined, start: number, end: number) {
341-
return list && rangeContainsStartEnd(list, start, end) ? list : undefined;
342-
}
343-
344325
export function getContainingList(node: Node, sourceFile: SourceFile): NodeArray<Node> | undefined {
345-
if (node.parent) {
346-
return getListByRange(node.getStart(sourceFile), node.getEnd(), node.parent, sourceFile);
347-
}
348-
return undefined;
326+
return node.parent && getListByRange(node.getStart(sourceFile), node.getEnd(), node.parent, sourceFile);
349327
}
350328

351329
function getListByPosition(pos: number, node: Node, sourceFile: SourceFile): NodeArray<Node> | undefined {
352-
if (!node) {
353-
return;
354-
}
355-
return getListByRange(pos, pos, node, sourceFile);
330+
return node && getListByRange(pos, pos, node, sourceFile);
356331
}
357332

358333
function getListByRange(start: number, end: number, node: Node, sourceFile: SourceFile): NodeArray<Node> | undefined {
359334
switch (node.kind) {
360335
case SyntaxKind.TypeReference:
361-
return getListIfVisualStartEndIsInListRange((<TypeReferenceNode>node).typeArguments, start, end, node, sourceFile);
336+
return getList((<TypeReferenceNode>node).typeArguments);
362337
case SyntaxKind.ObjectLiteralExpression:
363-
return getListIfVisualStartEndIsInListRange((<ObjectLiteralExpression>node).properties, start, end, node, sourceFile);
338+
return getList((<ObjectLiteralExpression>node).properties);
364339
case SyntaxKind.ArrayLiteralExpression:
365-
return getListIfVisualStartEndIsInListRange((<ArrayLiteralExpression>node).elements, start, end, node, sourceFile);
340+
return getList((<ArrayLiteralExpression>node).elements);
366341
case SyntaxKind.TypeLiteral:
367-
return getListIfVisualStartEndIsInListRange((<TypeLiteralNode>node).members, start, end, node, sourceFile);
342+
return getList((<TypeLiteralNode>node).members);
368343
case SyntaxKind.FunctionDeclaration:
369344
case SyntaxKind.FunctionExpression:
370345
case SyntaxKind.ArrowFunction:
@@ -373,32 +348,40 @@ namespace ts.formatting {
373348
case SyntaxKind.CallSignature:
374349
case SyntaxKind.Constructor:
375350
case SyntaxKind.ConstructorType:
376-
case SyntaxKind.ConstructSignature: {
377-
return getListIfVisualStartEndIsInListRange((<SignatureDeclaration>node).typeParameters, start, end, node, sourceFile) ||
378-
getListIfVisualStartEndIsInListRange((<SignatureDeclaration>node).parameters, start, end, node, sourceFile);
379-
}
351+
case SyntaxKind.ConstructSignature:
352+
return getList((<SignatureDeclaration>node).typeParameters) || getList((<SignatureDeclaration>node).parameters);
380353
case SyntaxKind.ClassDeclaration:
381354
case SyntaxKind.ClassExpression:
382355
case SyntaxKind.InterfaceDeclaration:
383356
case SyntaxKind.TypeAliasDeclaration:
384-
case SyntaxKind.JSDocTemplateTag: {
385-
const { typeParameters } = <ClassDeclaration | ClassExpression | InterfaceDeclaration | TypeAliasDeclaration | JSDocTemplateTag>node;
386-
return getListIfVisualStartEndIsInListRange(typeParameters, start, end, node, sourceFile);
387-
}
357+
case SyntaxKind.JSDocTemplateTag:
358+
return getList((<ClassDeclaration | ClassExpression | InterfaceDeclaration | TypeAliasDeclaration | JSDocTemplateTag>node).typeParameters);
388359
case SyntaxKind.NewExpression:
389-
case SyntaxKind.CallExpression: {
390-
return getListIfVisualStartEndIsInListRange((<CallExpression>node).typeArguments, start, end, node, sourceFile) ||
391-
getListIfVisualStartEndIsInListRange((<CallExpression>node).arguments, start, end, node, sourceFile);
392-
}
360+
case SyntaxKind.CallExpression:
361+
return getList((<CallExpression>node).typeArguments) || getList((<CallExpression>node).arguments);
393362
case SyntaxKind.VariableDeclarationList:
394-
return getListIfStartEndIsInListRange((<VariableDeclarationList>node).declarations, start, end);
363+
return getList((<VariableDeclarationList>node).declarations);
395364
case SyntaxKind.NamedImports:
396365
case SyntaxKind.NamedExports:
397-
return getListIfVisualStartEndIsInListRange((<NamedImportsOrExports>node).elements, start, end, node, sourceFile);
366+
return getList((<NamedImportsOrExports>node).elements);
398367
case SyntaxKind.ObjectBindingPattern:
399368
case SyntaxKind.ArrayBindingPattern:
400-
return getListIfVisualStartEndIsInListRange((<ObjectBindingPattern | ArrayBindingPattern>node).elements, start, end, node, sourceFile);
369+
return getList((<ObjectBindingPattern | ArrayBindingPattern>node).elements);
370+
}
371+
372+
function getList(list: NodeArray<Node> | undefined): NodeArray<Node> | undefined {
373+
return list && rangeContainsStartEnd(getVisualListRange(node, list, sourceFile), start, end) ? list : undefined;
374+
}
375+
}
376+
377+
function getVisualListRange(node: Node, list: TextRange, sourceFile: SourceFile): TextRange {
378+
const children = node.getChildren(sourceFile);
379+
for (let i = 1; i < children.length - 1; i++) {
380+
if (children[i].pos === list.pos && children[i].end === list.end) {
381+
return { pos: children[i - 1].end, end: children[i + 1].getStart(sourceFile) };
382+
}
401383
}
384+
return list;
402385
}
403386

404387
function getActualIndentationForListStartLine(list: NodeArray<Node>, sourceFile: SourceFile, options: EditorSettings): number {

0 commit comments

Comments
 (0)