Skip to content

Commit ad76338

Browse files
author
Andy
authored
Merge pull request #15528 from Microsoft/async-computed-method
Fix formatting for async computed method: Allow space between 'async' and '['
2 parents 7a0ee3c + b6bd396 commit ad76338

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

src/services/formatting/rules.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,9 @@ namespace ts.formatting {
283283
this.NoSpaceAfterDot = new Rule(RuleDescriptor.create3(SyntaxKind.DotToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete));
284284

285285
// No space before and after indexer
286-
this.NoSpaceBeforeOpenBracket = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.OpenBracketToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete));
286+
this.NoSpaceBeforeOpenBracket = new Rule(
287+
RuleDescriptor.create2(Shared.TokenRange.AnyExcept(SyntaxKind.AsyncKeyword), SyntaxKind.OpenBracketToken),
288+
RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete));
287289
this.NoSpaceAfterCloseBracket = new Rule(RuleDescriptor.create3(SyntaxKind.CloseBracketToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsNotBeforeBlockInFunctionDeclarationContext), RuleAction.Delete));
288290

289291
// Place a space before open brace in a function declaration

src/services/formatting/rulesMap.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ namespace ts.formatting {
4141
}
4242

4343
private FillRule(rule: Rule, rulesBucketConstructionStateList: RulesBucketConstructionState[]): void {
44-
const specificRule = rule.Descriptor.LeftTokenRange !== Shared.TokenRange.Any &&
45-
rule.Descriptor.RightTokenRange !== Shared.TokenRange.Any;
44+
const specificRule = rule.Descriptor.LeftTokenRange.isSpecific() && rule.Descriptor.RightTokenRange.isSpecific();
4645

4746
rule.Descriptor.LeftTokenRange.GetTokens().forEach((left) => {
4847
rule.Descriptor.RightTokenRange.GetTokens().forEach((right) => {

src/services/formatting/tokenRange.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace ts.formatting {
66
export interface ITokenAccess {
77
GetTokens(): SyntaxKind[];
88
Contains(token: SyntaxKind): boolean;
9+
isSpecific(): boolean;
910
}
1011

1112
export class TokenRangeAccess implements ITokenAccess {
@@ -27,6 +28,8 @@ namespace ts.formatting {
2728
public Contains(token: SyntaxKind): boolean {
2829
return this.tokens.indexOf(token) >= 0;
2930
}
31+
32+
public isSpecific() { return true; }
3033
}
3134

3235
export class TokenValuesAccess implements ITokenAccess {
@@ -43,6 +46,8 @@ namespace ts.formatting {
4346
public Contains(token: SyntaxKind): boolean {
4447
return this.tokens.indexOf(token) >= 0;
4548
}
49+
50+
public isSpecific() { return true; }
4651
}
4752

4853
export class TokenSingleValueAccess implements ITokenAccess {
@@ -56,15 +61,18 @@ namespace ts.formatting {
5661
public Contains(tokenValue: SyntaxKind): boolean {
5762
return tokenValue === this.token;
5863
}
64+
65+
public isSpecific() { return true; }
66+
}
67+
68+
const allTokens: SyntaxKind[] = [];
69+
for (let token = SyntaxKind.FirstToken; token <= SyntaxKind.LastToken; token++) {
70+
allTokens.push(token);
5971
}
6072

6173
export class TokenAllAccess implements ITokenAccess {
6274
public GetTokens(): SyntaxKind[] {
63-
const result: SyntaxKind[] = [];
64-
for (let token = SyntaxKind.FirstToken; token <= SyntaxKind.LastToken; token++) {
65-
result.push(token);
66-
}
67-
return result;
75+
return allTokens;
6876
}
6977

7078
public Contains(): boolean {
@@ -74,6 +82,22 @@ namespace ts.formatting {
7482
public toString(): string {
7583
return "[allTokens]";
7684
}
85+
86+
public isSpecific() { return false; }
87+
}
88+
89+
export class TokenAllExceptAccess implements ITokenAccess {
90+
constructor(readonly except: SyntaxKind) {}
91+
92+
public GetTokens(): SyntaxKind[] {
93+
return allTokens.filter(t => t !== this.except);
94+
}
95+
96+
public Contains(token: SyntaxKind): boolean {
97+
return token !== this.except;
98+
}
99+
100+
public isSpecific() { return false; }
77101
}
78102

79103
export class TokenRange {
@@ -92,8 +116,8 @@ namespace ts.formatting {
92116
return new TokenRange(new TokenRangeAccess(f, to, except));
93117
}
94118

95-
static AllTokens(): TokenRange {
96-
return new TokenRange(new TokenAllAccess());
119+
static AnyExcept(token: SyntaxKind): TokenRange {
120+
return new TokenRange(new TokenAllExceptAccess(token));
97121
}
98122

99123
public GetTokens(): SyntaxKind[] {
@@ -108,8 +132,12 @@ namespace ts.formatting {
108132
return this.tokenAccess.toString();
109133
}
110134

111-
static Any: TokenRange = TokenRange.AllTokens();
112-
static AnyIncludingMultilineComments = TokenRange.FromTokens(TokenRange.Any.GetTokens().concat([SyntaxKind.MultiLineCommentTrivia]));
135+
public isSpecific() {
136+
return this.tokenAccess.isSpecific();
137+
}
138+
139+
static Any: TokenRange = new TokenRange(new TokenAllAccess());
140+
static AnyIncludingMultilineComments = TokenRange.FromTokens([...allTokens, SyntaxKind.MultiLineCommentTrivia]);
113141
static Keywords = TokenRange.FromRange(SyntaxKind.FirstKeyword, SyntaxKind.LastKeyword);
114142
static BinaryOperators = TokenRange.FromRange(SyntaxKind.FirstBinaryOperator, SyntaxKind.LastBinaryOperator);
115143
static BinaryKeywordOperators = TokenRange.FromTokens([SyntaxKind.InKeyword, SyntaxKind.InstanceOfKeyword, SyntaxKind.OfKeyword, SyntaxKind.AsKeyword, SyntaxKind.IsKeyword]);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////class C {
4+
//// /*method*/async [0]() { }
5+
////}
6+
7+
format.document();
8+
goTo.marker("method");
9+
verify.currentLineContentIs(" async [0]() { }");

0 commit comments

Comments
 (0)