Skip to content

Commit d9550d4

Browse files
committed
add support for insertSpaceBeforeTypeAnnotation
1 parent e42ecea commit d9550d4

File tree

8 files changed

+29
-1
lines changed

8 files changed

+29
-1
lines changed

src/harness/fourslash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ namespace FourSlash {
375375
insertSpaceAfterTypeAssertion: false,
376376
placeOpenBraceOnNewLineForFunctions: false,
377377
placeOpenBraceOnNewLineForControlBlocks: false,
378+
insertSpaceBeforeTypeAnnotation: false
378379
};
379380

380381
// Open the first file by default

src/server/protocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2488,6 +2488,7 @@ namespace ts.server.protocol {
24882488
insertSpaceBeforeFunctionParenthesis?: boolean;
24892489
placeOpenBraceOnNewLineForFunctions?: boolean;
24902490
placeOpenBraceOnNewLineForControlBlocks?: boolean;
2491+
insertSpaceBeforeTypeAnnotation?: boolean;
24912492
}
24922493

24932494
export interface CompilerOptions {

src/services/formatting/rules.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace ts.formatting {
4747
rule("IgnoreBeforeComment", anyToken, comments, anyContext, RuleAction.Ignore),
4848
rule("IgnoreAfterLineComment", SyntaxKind.SingleLineCommentTrivia, anyToken, anyContext, RuleAction.Ignore),
4949

50-
rule("NoSpaceBeforeColon", anyToken, SyntaxKind.ColonToken, [isNonJsxSameLineTokenContext, isNotBinaryOpContext], RuleAction.Delete),
50+
rule("NotSpaceBeforeColon", anyToken, SyntaxKind.ColonToken, [isNonJsxSameLineTokenContext, isNotBinaryOpContext, isNotTypeAnnotationContext], RuleAction.Delete),
5151
rule("SpaceAfterColon", SyntaxKind.ColonToken, anyToken, [isNonJsxSameLineTokenContext, isNotBinaryOpContext], RuleAction.Space),
5252
rule("NoSpaceBeforeQuestionMark", anyToken, SyntaxKind.QuestionToken, [isNonJsxSameLineTokenContext, isNotBinaryOpContext], RuleAction.Delete),
5353
// insert space after '?' only when it is used in conditional operator
@@ -300,6 +300,9 @@ namespace ts.formatting {
300300

301301
rule("SpaceAfterTypeAssertion", SyntaxKind.GreaterThanToken, anyToken, [isOptionEnabled("insertSpaceAfterTypeAssertion"), isNonJsxSameLineTokenContext, isTypeAssertionContext], RuleAction.Space),
302302
rule("NoSpaceAfterTypeAssertion", SyntaxKind.GreaterThanToken, anyToken, [isOptionDisabledOrUndefined("insertSpaceAfterTypeAssertion"), isNonJsxSameLineTokenContext, isTypeAssertionContext], RuleAction.Delete),
303+
304+
rule("SpaceBeforeTypeAnnotation", anyToken, SyntaxKind.ColonToken, [isOptionEnabled("insertSpaceBeforeTypeAnnotation"), isNonJsxSameLineTokenContext, isTypeAnnotationContext], RuleAction.Space),
305+
rule("NoSpaceBeforeTypeAnnotation", anyToken, SyntaxKind.ColonToken, [isOptionDisabledOrUndefined("insertSpaceBeforeTypeAnnotation"), isNonJsxSameLineTokenContext, isTypeAnnotationContext], RuleAction.Delete),
303306
];
304307

305308
// These rules are lower in priority than user-configurable
@@ -440,6 +443,19 @@ namespace ts.formatting {
440443
return !isBinaryOpContext(context);
441444
}
442445

446+
function isNotTypeAnnotationContext(context: FormattingContext): boolean {
447+
return !isTypeAnnotationContext(context);
448+
}
449+
450+
function isTypeAnnotationContext(context: FormattingContext): boolean {
451+
const contextKind = context.contextNode.kind;
452+
return contextKind === SyntaxKind.PropertyDeclaration ||
453+
contextKind === SyntaxKind.PropertySignature ||
454+
contextKind === SyntaxKind.Parameter ||
455+
contextKind === SyntaxKind.VariableDeclaration ||
456+
isFunctionLikeKind(contextKind);
457+
}
458+
443459
function isConditionalOperatorContext(context: FormattingContext): boolean {
444460
return context.contextNode.kind === SyntaxKind.ConditionalExpression;
445461
}

src/services/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ namespace ts {
571571
InsertSpaceBeforeFunctionParenthesis?: boolean;
572572
PlaceOpenBraceOnNewLineForFunctions: boolean;
573573
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
574+
insertSpaceBeforeTypeAnnotation?: boolean;
574575
}
575576

576577
export interface FormatCodeSettings extends EditorSettings {
@@ -589,6 +590,7 @@ namespace ts {
589590
insertSpaceBeforeFunctionParenthesis?: boolean;
590591
placeOpenBraceOnNewLineForFunctions?: boolean;
591592
placeOpenBraceOnNewLineForControlBlocks?: boolean;
593+
insertSpaceBeforeTypeAnnotation?: boolean;
592594
}
593595

594596
export interface DefinitionInfo {

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4211,6 +4211,7 @@ declare namespace ts {
42114211
InsertSpaceBeforeFunctionParenthesis?: boolean;
42124212
PlaceOpenBraceOnNewLineForFunctions: boolean;
42134213
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
4214+
insertSpaceBeforeTypeAnnotation?: boolean;
42144215
}
42154216
interface FormatCodeSettings extends EditorSettings {
42164217
insertSpaceAfterCommaDelimiter?: boolean;
@@ -4228,6 +4229,7 @@ declare namespace ts {
42284229
insertSpaceBeforeFunctionParenthesis?: boolean;
42294230
placeOpenBraceOnNewLineForFunctions?: boolean;
42304231
placeOpenBraceOnNewLineForControlBlocks?: boolean;
4232+
insertSpaceBeforeTypeAnnotation?: boolean;
42314233
}
42324234
interface DefinitionInfo {
42334235
fileName: string;
@@ -6830,6 +6832,7 @@ declare namespace ts.server.protocol {
68306832
insertSpaceBeforeFunctionParenthesis?: boolean;
68316833
placeOpenBraceOnNewLineForFunctions?: boolean;
68326834
placeOpenBraceOnNewLineForControlBlocks?: boolean;
6835+
insertSpaceBeforeTypeAnnotation?: boolean;
68336836
}
68346837
interface CompilerOptions {
68356838
allowJs?: boolean;

tests/baselines/reference/api/typescript.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4211,6 +4211,7 @@ declare namespace ts {
42114211
InsertSpaceBeforeFunctionParenthesis?: boolean;
42124212
PlaceOpenBraceOnNewLineForFunctions: boolean;
42134213
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
4214+
insertSpaceBeforeTypeAnnotation?: boolean;
42144215
}
42154216
interface FormatCodeSettings extends EditorSettings {
42164217
insertSpaceAfterCommaDelimiter?: boolean;
@@ -4228,6 +4229,7 @@ declare namespace ts {
42284229
insertSpaceBeforeFunctionParenthesis?: boolean;
42294230
placeOpenBraceOnNewLineForFunctions?: boolean;
42304231
placeOpenBraceOnNewLineForControlBlocks?: boolean;
4232+
insertSpaceBeforeTypeAnnotation?: boolean;
42314233
}
42324234
interface DefinitionInfo {
42334235
fileName: string;

tests/cases/fourslash/formattingOptionsChange.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/////*insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets*/[1 ]; [ ]; []; [,];
1010
/////*insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces*/`${1}`;`${ 1 }`
1111
/////*insertSpaceAfterTypeAssertion*/const bar = <Bar> Thing.getFoo();
12+
/////*insertSpaceBeforeTypeAnnotation*/const bar : number = 1;
1213
/////*placeOpenBraceOnNewLineForFunctions*/class foo {
1314
////}
1415
/////*placeOpenBraceOnNewLineForControlBlocks*/if (true) {
@@ -26,6 +27,7 @@ runTest("insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis", " ( 1 )
2627
runTest("insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets", "[ 1 ];[];[];[ , ];", "[1];[];[];[,];");
2728
runTest("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces", "`${ 1 }`; `${ 1 }`", "`${1}`; `${1}`");
2829
runTest("insertSpaceAfterTypeAssertion", "const bar = <Bar> Thing.getFoo();", "const bar = <Bar>Thing.getFoo();");
30+
runTest("insertSpaceBeforeTypeAnnotation", "const bar : number = 1;", "const bar: number = 1;");
2931
runTest("placeOpenBraceOnNewLineForFunctions", "class foo", "class foo {");
3032
runTest("placeOpenBraceOnNewLineForControlBlocks", "if (true)", "if (true) {");
3133
runTest("insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces", "{ var t = 1 }; var { a, b } = { a: 'sw', b: 'r' }; function f({ a, b }) { }", "{var t = 1}; var {a, b} = {a: 'sw', b: 'r'}; function f({a, b}) {}");

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ declare namespace FourSlashInterface {
9797
InsertSpaceAfterTypeAssertion: boolean;
9898
PlaceOpenBraceOnNewLineForFunctions: boolean;
9999
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
100+
insertSpaceBeforeTypeAnnotation: boolean;
100101
[s: string]: boolean | number | string | undefined;
101102
}
102103
interface Range {

0 commit comments

Comments
 (0)