Skip to content

Commit 582ff0d

Browse files
committed
1 parent 0e469a7 commit 582ff0d

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/services/formatting/formattingContext.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace ts.formatting {
1212
private contextNodeAllOnSameLine: boolean;
1313
private nextNodeAllOnSameLine: boolean;
1414
private tokensAreOnSameLine: boolean;
15+
private spanBetweenTokensIsOnSingleLine: boolean;
1516
private contextNodeBlockIsOnOneLine: boolean;
1617
private nextNodeBlockIsOnOneLine: boolean;
1718

@@ -35,6 +36,7 @@ namespace ts.formatting {
3536
this.contextNodeAllOnSameLine = undefined;
3637
this.nextNodeAllOnSameLine = undefined;
3738
this.tokensAreOnSameLine = undefined;
39+
this.spanBetweenTokensIsOnSingleLine = undefined;
3840
this.contextNodeBlockIsOnOneLine = undefined;
3941
this.nextNodeBlockIsOnOneLine = undefined;
4042
}
@@ -65,6 +67,16 @@ namespace ts.formatting {
6567
return this.tokensAreOnSameLine;
6668
}
6769

70+
public SpanBetweenTokensIsOnSingleLine(): boolean {
71+
if (this.spanBetweenTokensIsOnSingleLine === undefined) {
72+
const startLine = this.sourceFile.getLineAndCharacterOfPosition(this.currentTokenSpan.end).line;
73+
const endLine = this.sourceFile.getLineAndCharacterOfPosition(this.nextTokenSpan.pos).line;
74+
this.spanBetweenTokensIsOnSingleLine = (startLine === endLine);
75+
}
76+
77+
return this.spanBetweenTokensIsOnSingleLine;
78+
}
79+
6880
public ContextNodeBlockIsOnOneLine() {
6981
if (this.contextNodeBlockIsOnOneLine === undefined) {
7082
this.contextNodeBlockIsOnOneLine = this.BlockIsOnOneLine(this.contextNode);
@@ -98,4 +110,4 @@ namespace ts.formatting {
98110
return false;
99111
}
100112
}
101-
}
113+
}

src/services/formatting/rules.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,10 @@ namespace ts.formatting {
459459
this.NoSpaceBeforeCloseBracket = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBracketToken), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets"), Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete));
460460

461461
// Insert space after opening and before closing template string braces
462-
this.NoSpaceAfterTemplateHeadAndMiddle = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete));
463-
this.SpaceAfterTemplateHeadAndMiddle = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxSameLineTokenContext), RuleAction.Space));
464-
this.NoSpaceBeforeTemplateMiddleAndTail = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail])), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete));
465-
this.SpaceBeforeTemplateMiddleAndTail = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail])), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxSameLineTokenContext), RuleAction.Space));
462+
this.NoSpaceAfterTemplateHeadAndMiddle = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxAndSpanBetweenIsSingleLineTokenContext), RuleAction.Delete));
463+
this.SpaceAfterTemplateHeadAndMiddle = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxAndSpanBetweenIsSingleLineTokenContext), RuleAction.Space));
464+
this.NoSpaceBeforeTemplateMiddleAndTail = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail])), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxAndSpanBetweenIsSingleLineTokenContext), RuleAction.Delete));
465+
this.SpaceBeforeTemplateMiddleAndTail = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail])), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxAndSpanBetweenIsSingleLineTokenContext), RuleAction.Space));
466466

467467
// No space after { and before } in JSX expression
468468
this.NoSpaceAfterOpenBraceInJsxExpression = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces"), Rules.IsNonJsxSameLineTokenContext, Rules.IsJsxExpressionContext), RuleAction.Delete));
@@ -817,6 +817,10 @@ namespace ts.formatting {
817817
return context.TokensAreOnSameLine() && context.contextNode.kind !== SyntaxKind.JsxText;
818818
}
819819

820+
static IsNonJsxAndSpanBetweenIsSingleLineTokenContext(context: FormattingContext): boolean {
821+
return context.SpanBetweenTokensIsOnSingleLine() && context.contextNode.kind !== SyntaxKind.JsxText;
822+
}
823+
820824
static IsNonJsxElementContext(context: FormattingContext): boolean {
821825
return context.contextNode.kind !== SyntaxKind.JsxElement;
822826
}

0 commit comments

Comments
 (0)