mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-24 16:46:00 -05:00
apply suggested changes
This commit is contained in:
@@ -34,7 +34,7 @@ namespace ts.formatting {
|
||||
* the first token in line so it should be indented
|
||||
*/
|
||||
interface DynamicIndentation {
|
||||
getIndentationForToken(tokenLine: number, tokenKind: SyntaxKind, container: Node, suppressDelta?: boolean): number;
|
||||
getIndentationForToken(tokenLine: number, tokenKind: SyntaxKind, container: Node, suppressDelta: boolean): number;
|
||||
getIndentationForComment(owningToken: SyntaxKind, tokenIndentation: number, container: Node): number;
|
||||
/**
|
||||
* Indentation for open and close tokens of the node if it is block or another node that needs special indentation
|
||||
@@ -735,7 +735,6 @@ namespace ts.formatting {
|
||||
|
||||
let listDynamicIndentation = parentDynamicIndentation;
|
||||
let startLine = parentStartLine;
|
||||
let indentationOnListStartToken = parentDynamicIndentation.getIndentation();
|
||||
|
||||
if (listStartToken !== SyntaxKind.Unknown) {
|
||||
// introduce a new indentation scope for lists (including list start and end tokens)
|
||||
@@ -751,6 +750,7 @@ namespace ts.formatting {
|
||||
|
||||
consumeTokenAndAdvanceScanner(tokenInfo, parent, parentDynamicIndentation, parent);
|
||||
|
||||
let indentationOnListStartToken: number;
|
||||
if (indentationOnLastIndentedLine !== Constants.Unknown) {
|
||||
// scanner just processed list start token so consider last indentation as list indentation
|
||||
// function foo(): { // last indentation was 0, list item will be indented based on this value
|
||||
@@ -835,7 +835,7 @@ namespace ts.formatting {
|
||||
|
||||
if (indentToken) {
|
||||
const tokenIndentation = (isTokenInRange && !rangeContainsError(currentTokenInfo.token)) ?
|
||||
dynamicIndentation.getIndentationForToken(tokenStart.line, currentTokenInfo.token.kind, container, isListEndToken) :
|
||||
dynamicIndentation.getIndentationForToken(tokenStart.line, currentTokenInfo.token.kind, container, !!isListEndToken) :
|
||||
Constants.Unknown;
|
||||
|
||||
let indentNextTokenOrTrivia = true;
|
||||
|
||||
@@ -66,7 +66,7 @@ namespace ts.formatting {
|
||||
}
|
||||
}
|
||||
|
||||
const containerList = getListByPosition(position, precedingToken.parent);
|
||||
const containerList = getListByPosition(position, precedingToken.parent, sourceFile);
|
||||
// use list position if the preceding token is before any list items
|
||||
if (containerList && !rangeContainsRange(containerList, precedingToken)) {
|
||||
return getActualIndentationForListStartLine(containerList, sourceFile, options) + options.indentSize!; // TODO: GH#18217
|
||||
@@ -322,7 +322,7 @@ namespace ts.formatting {
|
||||
return false;
|
||||
}
|
||||
|
||||
function getListIfVisualStartEndIsInListRange(list: NodeArray<Node> | undefined, start: number, end: number, node: Node) {
|
||||
function getListIfVisualStartEndIsInListRange(list: NodeArray<Node> | undefined, start: number, end: number, node: Node, sourceFile: SourceFile) {
|
||||
return list && rangeContainsVisualStartEnd(list) ? list : undefined;
|
||||
|
||||
// Assumes a list is wrapped by list tokens
|
||||
@@ -330,7 +330,7 @@ namespace ts.formatting {
|
||||
const children = node.getChildren();
|
||||
for (let i = 1; i < children.length - 1; i++) {
|
||||
if (children[i].pos === textRange.pos && children[i].end === textRange.end) {
|
||||
return rangeContainsStartEnd({ pos: children[i - 1].end, end: children[i + 1].end - children[i + 1].getWidth() }, start, end);
|
||||
return rangeContainsStartEnd({ pos: children[i - 1].end, end: children[i + 1].getStart(sourceFile) }, start, end);
|
||||
}
|
||||
}
|
||||
return rangeContainsStartEnd(textRange, start, end);
|
||||
@@ -343,28 +343,28 @@ namespace ts.formatting {
|
||||
|
||||
export function getContainingList(node: Node, sourceFile: SourceFile): NodeArray<Node> | undefined {
|
||||
if (node.parent) {
|
||||
return getListByRange(node.getStart(sourceFile), node.getEnd(), node.parent);
|
||||
return getListByRange(node.getStart(sourceFile), node.getEnd(), node.parent, sourceFile);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function getListByPosition(pos: number, node: Node): NodeArray<Node> | undefined {
|
||||
function getListByPosition(pos: number, node: Node, sourceFile: SourceFile): NodeArray<Node> | undefined {
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
return getListByRange(pos, pos, node);
|
||||
return getListByRange(pos, pos, node, sourceFile);
|
||||
}
|
||||
|
||||
function getListByRange(start: number, end: number, node: Node): NodeArray<Node> | undefined {
|
||||
function getListByRange(start: number, end: number, node: Node, sourceFile: SourceFile): NodeArray<Node> | undefined {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.TypeReference:
|
||||
return getListIfVisualStartEndIsInListRange((<TypeReferenceNode>node).typeArguments, start, end, node);
|
||||
return getListIfVisualStartEndIsInListRange((<TypeReferenceNode>node).typeArguments, start, end, node, sourceFile);
|
||||
case SyntaxKind.ObjectLiteralExpression:
|
||||
return getListIfVisualStartEndIsInListRange((<ObjectLiteralExpression>node).properties, start, end, node);
|
||||
return getListIfVisualStartEndIsInListRange((<ObjectLiteralExpression>node).properties, start, end, node, sourceFile);
|
||||
case SyntaxKind.ArrayLiteralExpression:
|
||||
return getListIfVisualStartEndIsInListRange((<ArrayLiteralExpression>node).elements, start, end, node);
|
||||
return getListIfVisualStartEndIsInListRange((<ArrayLiteralExpression>node).elements, start, end, node, sourceFile);
|
||||
case SyntaxKind.TypeLiteral:
|
||||
return getListIfVisualStartEndIsInListRange((<TypeLiteralNode>node).members, start, end, node);
|
||||
return getListIfVisualStartEndIsInListRange((<TypeLiteralNode>node).members, start, end, node, sourceFile);
|
||||
case SyntaxKind.FunctionDeclaration:
|
||||
case SyntaxKind.FunctionExpression:
|
||||
case SyntaxKind.ArrowFunction:
|
||||
@@ -374,8 +374,8 @@ namespace ts.formatting {
|
||||
case SyntaxKind.Constructor:
|
||||
case SyntaxKind.ConstructorType:
|
||||
case SyntaxKind.ConstructSignature: {
|
||||
return getListIfVisualStartEndIsInListRange((<SignatureDeclaration>node).typeParameters, start, end, node) ||
|
||||
getListIfVisualStartEndIsInListRange((<SignatureDeclaration>node).parameters, start, end, node);
|
||||
return getListIfVisualStartEndIsInListRange((<SignatureDeclaration>node).typeParameters, start, end, node, sourceFile) ||
|
||||
getListIfVisualStartEndIsInListRange((<SignatureDeclaration>node).parameters, start, end, node, sourceFile);
|
||||
}
|
||||
case SyntaxKind.ClassDeclaration:
|
||||
case SyntaxKind.ClassExpression:
|
||||
@@ -383,21 +383,21 @@ namespace ts.formatting {
|
||||
case SyntaxKind.TypeAliasDeclaration:
|
||||
case SyntaxKind.JSDocTemplateTag: {
|
||||
const { typeParameters } = <ClassDeclaration | ClassExpression | InterfaceDeclaration | TypeAliasDeclaration | JSDocTemplateTag>node;
|
||||
return getListIfStartEndIsInListRange(typeParameters, start, end);
|
||||
return getListIfVisualStartEndIsInListRange(typeParameters, start, end, node, sourceFile);
|
||||
}
|
||||
case SyntaxKind.NewExpression:
|
||||
case SyntaxKind.CallExpression: {
|
||||
return getListIfVisualStartEndIsInListRange((<CallExpression>node).typeArguments, start, end, node) ||
|
||||
getListIfVisualStartEndIsInListRange((<CallExpression>node).arguments, start, end, node);
|
||||
return getListIfVisualStartEndIsInListRange((<CallExpression>node).typeArguments, start, end, node, sourceFile) ||
|
||||
getListIfVisualStartEndIsInListRange((<CallExpression>node).arguments, start, end, node, sourceFile);
|
||||
}
|
||||
case SyntaxKind.VariableDeclarationList:
|
||||
return getListIfStartEndIsInListRange((<VariableDeclarationList>node).declarations, start, end);
|
||||
case SyntaxKind.NamedImports:
|
||||
case SyntaxKind.NamedExports:
|
||||
return getListIfVisualStartEndIsInListRange((<NamedImportsOrExports>node).elements, start, end, node);
|
||||
return getListIfVisualStartEndIsInListRange((<NamedImportsOrExports>node).elements, start, end, node, sourceFile);
|
||||
case SyntaxKind.ObjectBindingPattern:
|
||||
case SyntaxKind.ArrayBindingPattern:
|
||||
return getListIfVisualStartEndIsInListRange((<ObjectBindingPattern | ArrayBindingPattern>node).elements, start, end, node);
|
||||
return getListIfVisualStartEndIsInListRange((<ObjectBindingPattern | ArrayBindingPattern>node).elements, start, end, node, sourceFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user