Skip to content

Commit 7154df1

Browse files
Kingwlmhegazy
authored andcommitted
add support for insertSpaceBeforeTypeAnnotation (#20466)
1 parent 6d596c0 commit 7154df1

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
@@ -381,6 +381,7 @@ namespace FourSlash {
381381
insertSpaceAfterTypeAssertion: false,
382382
placeOpenBraceOnNewLineForFunctions: false,
383383
placeOpenBraceOnNewLineForControlBlocks: false,
384+
insertSpaceBeforeTypeAnnotation: false
384385
};
385386

386387
// Open the first file by default

src/server/protocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,6 +2556,7 @@ namespace ts.server.protocol {
25562556
insertSpaceBeforeFunctionParenthesis?: boolean;
25572557
placeOpenBraceOnNewLineForFunctions?: boolean;
25582558
placeOpenBraceOnNewLineForControlBlocks?: boolean;
2559+
insertSpaceBeforeTypeAnnotation?: boolean;
25592560
}
25602561

25612562
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. Rules earlier in this list have priority over rules later in the list.
@@ -441,6 +444,19 @@ namespace ts.formatting {
441444
return !isBinaryOpContext(context);
442445
}
443446

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

src/services/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ namespace ts {
597597
InsertSpaceBeforeFunctionParenthesis?: boolean;
598598
PlaceOpenBraceOnNewLineForFunctions: boolean;
599599
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
600+
insertSpaceBeforeTypeAnnotation?: boolean;
600601
}
601602

602603
export interface FormatCodeSettings extends EditorSettings {
@@ -615,6 +616,7 @@ namespace ts {
615616
insertSpaceBeforeFunctionParenthesis?: boolean;
616617
placeOpenBraceOnNewLineForFunctions?: boolean;
617618
placeOpenBraceOnNewLineForControlBlocks?: boolean;
619+
insertSpaceBeforeTypeAnnotation?: boolean;
618620
}
619621

620622
export interface DefinitionInfo {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4209,6 +4209,7 @@ declare namespace ts {
42094209
InsertSpaceBeforeFunctionParenthesis?: boolean;
42104210
PlaceOpenBraceOnNewLineForFunctions: boolean;
42114211
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
4212+
insertSpaceBeforeTypeAnnotation?: boolean;
42124213
}
42134214
interface FormatCodeSettings extends EditorSettings {
42144215
insertSpaceAfterCommaDelimiter?: boolean;
@@ -4226,6 +4227,7 @@ declare namespace ts {
42264227
insertSpaceBeforeFunctionParenthesis?: boolean;
42274228
placeOpenBraceOnNewLineForFunctions?: boolean;
42284229
placeOpenBraceOnNewLineForControlBlocks?: boolean;
4230+
insertSpaceBeforeTypeAnnotation?: boolean;
42294231
}
42304232
interface DefinitionInfo {
42314233
fileName: string;
@@ -6845,6 +6847,7 @@ declare namespace ts.server.protocol {
68456847
insertSpaceBeforeFunctionParenthesis?: boolean;
68466848
placeOpenBraceOnNewLineForFunctions?: boolean;
68476849
placeOpenBraceOnNewLineForControlBlocks?: boolean;
6850+
insertSpaceBeforeTypeAnnotation?: boolean;
68486851
}
68496852
interface CompilerOptions {
68506853
allowJs?: boolean;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4209,6 +4209,7 @@ declare namespace ts {
42094209
InsertSpaceBeforeFunctionParenthesis?: boolean;
42104210
PlaceOpenBraceOnNewLineForFunctions: boolean;
42114211
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
4212+
insertSpaceBeforeTypeAnnotation?: boolean;
42124213
}
42134214
interface FormatCodeSettings extends EditorSettings {
42144215
insertSpaceAfterCommaDelimiter?: boolean;
@@ -4226,6 +4227,7 @@ declare namespace ts {
42264227
insertSpaceBeforeFunctionParenthesis?: boolean;
42274228
placeOpenBraceOnNewLineForFunctions?: boolean;
42284229
placeOpenBraceOnNewLineForControlBlocks?: boolean;
4230+
insertSpaceBeforeTypeAnnotation?: boolean;
42294231
}
42304232
interface DefinitionInfo {
42314233
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)