Skip to content

Commit

Permalink
fix(52177): Wrong space in type assertions when using array (#52184)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk authored Jan 11, 2023
1 parent a05b7ec commit 6860373
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/services/formatting/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,12 @@ export function getAllRules(): RuleSpec[] {
rule("NoSpaceBetweenCloseParenAndAngularBracket", SyntaxKind.CloseParenToken, SyntaxKind.LessThanToken, [isNonJsxSameLineTokenContext, isTypeArgumentOrParameterOrAssertionContext], RuleAction.DeleteSpace),
rule("NoSpaceAfterOpenAngularBracket", SyntaxKind.LessThanToken, anyToken, [isNonJsxSameLineTokenContext, isTypeArgumentOrParameterOrAssertionContext], RuleAction.DeleteSpace),
rule("NoSpaceBeforeCloseAngularBracket", anyToken, SyntaxKind.GreaterThanToken, [isNonJsxSameLineTokenContext, isTypeArgumentOrParameterOrAssertionContext], RuleAction.DeleteSpace),
rule("NoSpaceAfterCloseAngularBracket",
SyntaxKind.GreaterThanToken,
[SyntaxKind.OpenParenToken, SyntaxKind.OpenBracketToken, SyntaxKind.GreaterThanToken, SyntaxKind.CommaToken],
[isNonJsxSameLineTokenContext, isTypeArgumentOrParameterOrAssertionContext, isNotFunctionDeclContext /*To prevent an interference with the SpaceBeforeOpenParenInFuncDecl rule*/],
RuleAction.DeleteSpace),
rule("NoSpaceAfterCloseAngularBracket", SyntaxKind.GreaterThanToken, [SyntaxKind.OpenParenToken, SyntaxKind.OpenBracketToken, SyntaxKind.GreaterThanToken, SyntaxKind.CommaToken], [
isNonJsxSameLineTokenContext,
isTypeArgumentOrParameterOrAssertionContext,
isNotFunctionDeclContext /*To prevent an interference with the SpaceBeforeOpenParenInFuncDecl rule*/,
isNonTypeAssertionContext
], RuleAction.DeleteSpace),

// decorators
rule("SpaceBeforeAt", [SyntaxKind.CloseParenToken, SyntaxKind.Identifier], SyntaxKind.AtToken, [isNonJsxSameLineTokenContext], RuleAction.InsertSpace),
Expand Down Expand Up @@ -835,6 +836,10 @@ function isTypeAssertionContext(context: FormattingContext): boolean {
return context.contextNode.kind === SyntaxKind.TypeAssertionExpression;
}

function isNonTypeAssertionContext(context: FormattingContext): boolean {
return !isTypeAssertionContext(context);
}

function isVoidOpContext(context: FormattingContext): boolean {
return context.currentTokenSpan.kind === SyntaxKind.VoidKeyword && context.currentTokenParent.kind === SyntaxKind.VoidExpression;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/cases/fourslash/formatInsertSpaceAfterTypeAssertion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference path="fourslash.ts" />

////let a = <string> "";
////let b = <number> 1;
////let c = <any[]> [];
////let d = <string[]> [];
////let e = <string[]> ["e"];

format.setFormatOptions({
insertSpaceAfterTypeAssertion: true,
})
format.document();
verify.currentFileContentIs(
`let a=<string> "";
let b=<number> 1;
let c=<any[]> [];
let d=<string[]> [];
let e=<string[]> ["e"];`
);

0 comments on commit 6860373

Please sign in to comment.