Skip to content

Commit 743d8db

Browse files
lankaapuramhegazy
authored andcommitted
Fix to remove extra space between function and array index. (microsoft#21113)
microsoft#21111
1 parent da593ca commit 743d8db

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

src/services/formatting/rules.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace ts.formatting {
1111
for (let token = SyntaxKind.FirstToken; token <= SyntaxKind.LastToken; token++) {
1212
allTokens.push(token);
1313
}
14-
function anyTokenExcept(token: SyntaxKind): TokenRange {
15-
return { tokens: allTokens.filter(t => t !== token), isSpecific: false };
14+
function anyTokenExcept(...tokens: SyntaxKind[]): TokenRange {
15+
return { tokens: allTokens.filter(t => !tokens.some(t2 => t2 === t)), isSpecific: false };
1616
}
1717

1818
const anyToken: TokenRange = { tokens: allTokens, isSpecific: false };
@@ -316,6 +316,11 @@ namespace ts.formatting {
316316

317317
rule("NoSpaceBeforeComma", anyToken, SyntaxKind.CommaToken, [isNonJsxSameLineTokenContext], RuleAction.Delete),
318318

319+
// No space before and after indexer `x[]`
320+
rule("NoSpaceBeforeOpenBracket", anyTokenExcept(SyntaxKind.AsyncKeyword, SyntaxKind.CaseKeyword), SyntaxKind.OpenBracketToken, [isNonJsxSameLineTokenContext], RuleAction.Delete),
321+
rule("NoSpaceAfterCloseBracket", SyntaxKind.CloseBracketToken, anyToken, [isNonJsxSameLineTokenContext, isNotBeforeBlockInFunctionDeclarationContext], RuleAction.Delete),
322+
rule("SpaceAfterSemicolon", SyntaxKind.SemicolonToken, anyToken, [isNonJsxSameLineTokenContext], RuleAction.Space),
323+
319324
// Add a space between statements. All keywords except (do,else,case) has open/close parens after them.
320325
// So, we have a rule to add a space for [),Any], [do,Any], [else,Any], and [case,Any]
321326
rule(
@@ -326,11 +331,6 @@ namespace ts.formatting {
326331
RuleAction.Space),
327332
// This low-pri rule takes care of "try {" and "finally {" in case the rule SpaceBeforeOpenBraceInControl didn't execute on FormatOnEnter.
328333
rule("SpaceAfterTryFinally", [SyntaxKind.TryKeyword, SyntaxKind.FinallyKeyword], SyntaxKind.OpenBraceToken, [isNonJsxSameLineTokenContext], RuleAction.Space),
329-
330-
// No space before and after indexer `x[]`
331-
rule("NoSpaceBeforeOpenBracket", anyTokenExcept(SyntaxKind.AsyncKeyword), SyntaxKind.OpenBracketToken, [isNonJsxSameLineTokenContext], RuleAction.Delete),
332-
rule("NoSpaceAfterCloseBracket", SyntaxKind.CloseBracketToken, anyToken, [isNonJsxSameLineTokenContext, isNotBeforeBlockInFunctionDeclarationContext], RuleAction.Delete),
333-
rule("SpaceAfterSemicolon", SyntaxKind.SemicolonToken, anyToken, [isNonJsxSameLineTokenContext], RuleAction.Space),
334334
];
335335

336336
return [
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path="../fourslash.ts"/>
2+
3+
////
4+
////function test() {
5+
//// return [];
6+
////}
7+
////
8+
////test() [0]
9+
////
10+
11+
format.document();
12+
verify.currentFileContentIs(
13+
`
14+
function test() {
15+
return [];
16+
}
17+
18+
test()[0]
19+
`);

0 commit comments

Comments
 (0)