Skip to content

Commit 2b72751

Browse files
author
Kevin Lang
committed
make (no) space after type assertion user configurable
1 parent da8fc5d commit 2b72751

7 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/harness/fourslash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ namespace FourSlash {
326326
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
327327
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
328328
InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
329+
InsertSpaceAfterTypeAssertion: false,
329330
PlaceOpenBraceOnNewLineForFunctions: false,
330331
PlaceOpenBraceOnNewLineForControlBlocks: false,
331332
};

src/server/editorServices.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,7 @@ namespace ts.server {
15841584
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
15851585
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
15861586
InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
1587+
InsertSpaceAfterTypeAssertion: false,
15871588
PlaceOpenBraceOnNewLineForFunctions: false,
15881589
PlaceOpenBraceOnNewLineForControlBlocks: false,
15891590
});

src/services/formatting/rules.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ namespace ts.formatting {
136136
public NoSpaceAfterOpenAngularBracket: Rule;
137137
public NoSpaceBeforeCloseAngularBracket: Rule;
138138
public NoSpaceAfterCloseAngularBracket: Rule;
139-
public NoSpaceAfterTypeAssertion: Rule;
140139

141140
// Remove spaces in empty interface literals. e.g.: x: {}
142141
public NoSpaceBetweenEmptyInterfaceBraceBrackets: Rule;
@@ -238,6 +237,10 @@ namespace ts.formatting {
238237
public NoSpaceBeforeEqualInJsxAttribute: Rule;
239238
public NoSpaceAfterEqualInJsxAttribute: Rule;
240239

240+
// No space after type assertions
241+
public NoSpaceAfterTypeAssertion: Rule;
242+
public SpaceAfterTypeAssertion: Rule;
243+
241244
constructor() {
242245
///
243246
/// Common Rules
@@ -371,7 +374,6 @@ namespace ts.formatting {
371374
this.NoSpaceAfterOpenAngularBracket = new Rule(RuleDescriptor.create3(SyntaxKind.LessThanToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsTypeArgumentOrParameterOrAssertionContext), RuleAction.Delete));
372375
this.NoSpaceBeforeCloseAngularBracket = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.GreaterThanToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsTypeArgumentOrParameterOrAssertionContext), RuleAction.Delete));
373376
this.NoSpaceAfterCloseAngularBracket = new Rule(RuleDescriptor.create3(SyntaxKind.GreaterThanToken, Shared.TokenRange.FromTokens([SyntaxKind.OpenParenToken, SyntaxKind.OpenBracketToken, SyntaxKind.GreaterThanToken, SyntaxKind.CommaToken])), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsTypeArgumentOrParameterOrAssertionContext), RuleAction.Delete));
374-
this.NoSpaceAfterTypeAssertion = new Rule(RuleDescriptor.create3(SyntaxKind.GreaterThanToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsTypeAssertionContext), RuleAction.Delete));
375377

376378
// Remove spaces in empty interface literals. e.g.: x: {}
377379
this.NoSpaceBetweenEmptyInterfaceBraceBrackets = new Rule(RuleDescriptor.create1(SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsObjectTypeContext), RuleAction.Delete));
@@ -443,7 +445,6 @@ namespace ts.formatting {
443445
this.NoSpaceAfterOpenAngularBracket,
444446
this.NoSpaceBeforeCloseAngularBracket,
445447
this.NoSpaceAfterCloseAngularBracket,
446-
this.NoSpaceAfterTypeAssertion,
447448
this.SpaceBeforeAt,
448449
this.NoSpaceAfterAt,
449450
this.SpaceAfterDecorator,
@@ -522,6 +523,11 @@ namespace ts.formatting {
522523
// Insert space after function keyword for anonymous functions
523524
this.SpaceAfterAnonymousFunctionKeyword = new Rule(RuleDescriptor.create1(SyntaxKind.FunctionKeyword, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext), RuleAction.Space));
524525
this.NoSpaceAfterAnonymousFunctionKeyword = new Rule(RuleDescriptor.create1(SyntaxKind.FunctionKeyword, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext), RuleAction.Delete));
526+
527+
// No space after type assertion
528+
this.NoSpaceAfterTypeAssertion = new Rule(RuleDescriptor.create3(SyntaxKind.GreaterThanToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsTypeAssertionContext), RuleAction.Delete));
529+
this.SpaceAfterTypeAssertion = new Rule(RuleDescriptor.create3(SyntaxKind.GreaterThanToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsTypeAssertionContext), RuleAction.Space));
530+
525531
}
526532

527533
///

src/services/formatting/rulesProvider.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,16 @@ namespace ts.formatting {
124124
rules.push(this.globalRules.NewLineBeforeOpenBraceInTypeScriptDeclWithBlock);
125125
}
126126

127+
if (options.InsertSpaceAfterTypeAssertion) {
128+
rules.push(this.globalRules.SpaceAfterTypeAssertion);
129+
}
130+
else {
131+
rules.push(this.globalRules.NoSpaceAfterTypeAssertion);
132+
}
133+
127134
rules = rules.concat(this.globalRules.LowPriorityCommonRules);
128135

129136
return rules;
130137
}
131138
}
132-
}
139+
}

src/services/services.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,7 @@ namespace ts {
13561356
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: boolean;
13571357
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: boolean;
13581358
InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean;
1359+
InsertSpaceAfterTypeAssertion: boolean;
13591360
PlaceOpenBraceOnNewLineForFunctions: boolean;
13601361
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
13611362
[s: string]: boolean | number | string | undefined;

tests/cases/fourslash/formattingOptionsChange.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/////*InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis*/(1 )
99
/////*InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets*/[1 ]; [ ]; []; [,];
1010
/////*InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces*/`${1}`;`${ 1 }`
11+
/////*InsertSpaceAfterTypeAssertion*/const bar = <Bar> Thing.getFoo();
1112
/////*PlaceOpenBraceOnNewLineForFunctions*/class foo {
1213
////}
1314
/////*PlaceOpenBraceOnNewLineForControlBlocks*/if (true) {
@@ -21,6 +22,7 @@ runTest("InsertSpaceAfterFunctionKeywordForAnonymousFunctions", "(function () {
2122
runTest("InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis", " ( 1 )", " (1)");
2223
runTest("InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets", "[ 1 ];[];[];[ , ];", "[1];[];[];[,];");
2324
runTest("InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces", "`${ 1 }`; `${ 1 }`", "`${1}`; `${1}`");
25+
runTest("InsertSpaceAfterTypeAssertion", "const bar = <Bar> Thing.getFoo();", "const bar = <Bar>Thing.getFoo();");
2426
runTest("PlaceOpenBraceOnNewLineForFunctions", "class foo", "class foo {");
2527
runTest("PlaceOpenBraceOnNewLineForControlBlocks", "if ( true )", "if ( true ) {");
2628

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ declare namespace FourSlashInterface {
8383
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: boolean;
8484
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: boolean;
8585
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: boolean;
86+
InsertSpaceAfterTypeAssertion: boolean;
8687
PlaceOpenBraceOnNewLineForFunctions: boolean;
8788
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
8889
[s: string]: boolean | number | string | undefined;

0 commit comments

Comments
 (0)