add support for insertSpaceBeforeTypeAnnotation (#20466)

This commit is contained in:
Wenlu Wang
2018-01-08 13:25:02 -08:00
committed by Mohamed Hegazy
parent 6d596c058e
commit 7154df10df
8 changed files with 29 additions and 1 deletions
+1
View File
@@ -381,6 +381,7 @@ namespace FourSlash {
insertSpaceAfterTypeAssertion: false,
placeOpenBraceOnNewLineForFunctions: false,
placeOpenBraceOnNewLineForControlBlocks: false,
insertSpaceBeforeTypeAnnotation: false
};
// Open the first file by default
+1
View File
@@ -2556,6 +2556,7 @@ namespace ts.server.protocol {
insertSpaceBeforeFunctionParenthesis?: boolean;
placeOpenBraceOnNewLineForFunctions?: boolean;
placeOpenBraceOnNewLineForControlBlocks?: boolean;
insertSpaceBeforeTypeAnnotation?: boolean;
}
export interface CompilerOptions {
+17 -1
View File
@@ -47,7 +47,7 @@ namespace ts.formatting {
rule("IgnoreBeforeComment", anyToken, comments, anyContext, RuleAction.Ignore),
rule("IgnoreAfterLineComment", SyntaxKind.SingleLineCommentTrivia, anyToken, anyContext, RuleAction.Ignore),
rule("NoSpaceBeforeColon", anyToken, SyntaxKind.ColonToken, [isNonJsxSameLineTokenContext, isNotBinaryOpContext], RuleAction.Delete),
rule("NotSpaceBeforeColon", anyToken, SyntaxKind.ColonToken, [isNonJsxSameLineTokenContext, isNotBinaryOpContext, isNotTypeAnnotationContext], RuleAction.Delete),
rule("SpaceAfterColon", SyntaxKind.ColonToken, anyToken, [isNonJsxSameLineTokenContext, isNotBinaryOpContext], RuleAction.Space),
rule("NoSpaceBeforeQuestionMark", anyToken, SyntaxKind.QuestionToken, [isNonJsxSameLineTokenContext, isNotBinaryOpContext], RuleAction.Delete),
// insert space after '?' only when it is used in conditional operator
@@ -300,6 +300,9 @@ namespace ts.formatting {
rule("SpaceAfterTypeAssertion", SyntaxKind.GreaterThanToken, anyToken, [isOptionEnabled("insertSpaceAfterTypeAssertion"), isNonJsxSameLineTokenContext, isTypeAssertionContext], RuleAction.Space),
rule("NoSpaceAfterTypeAssertion", SyntaxKind.GreaterThanToken, anyToken, [isOptionDisabledOrUndefined("insertSpaceAfterTypeAssertion"), isNonJsxSameLineTokenContext, isTypeAssertionContext], RuleAction.Delete),
rule("SpaceBeforeTypeAnnotation", anyToken, SyntaxKind.ColonToken, [isOptionEnabled("insertSpaceBeforeTypeAnnotation"), isNonJsxSameLineTokenContext, isTypeAnnotationContext], RuleAction.Space),
rule("NoSpaceBeforeTypeAnnotation", anyToken, SyntaxKind.ColonToken, [isOptionDisabledOrUndefined("insertSpaceBeforeTypeAnnotation"), isNonJsxSameLineTokenContext, isTypeAnnotationContext], RuleAction.Delete),
];
// 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 {
return !isBinaryOpContext(context);
}
function isNotTypeAnnotationContext(context: FormattingContext): boolean {
return !isTypeAnnotationContext(context);
}
function isTypeAnnotationContext(context: FormattingContext): boolean {
const contextKind = context.contextNode.kind;
return contextKind === SyntaxKind.PropertyDeclaration ||
contextKind === SyntaxKind.PropertySignature ||
contextKind === SyntaxKind.Parameter ||
contextKind === SyntaxKind.VariableDeclaration ||
isFunctionLikeKind(contextKind);
}
function isConditionalOperatorContext(context: FormattingContext): boolean {
return context.contextNode.kind === SyntaxKind.ConditionalExpression;
}
+2
View File
@@ -597,6 +597,7 @@ namespace ts {
InsertSpaceBeforeFunctionParenthesis?: boolean;
PlaceOpenBraceOnNewLineForFunctions: boolean;
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
insertSpaceBeforeTypeAnnotation?: boolean;
}
export interface FormatCodeSettings extends EditorSettings {
@@ -615,6 +616,7 @@ namespace ts {
insertSpaceBeforeFunctionParenthesis?: boolean;
placeOpenBraceOnNewLineForFunctions?: boolean;
placeOpenBraceOnNewLineForControlBlocks?: boolean;
insertSpaceBeforeTypeAnnotation?: boolean;
}
export interface DefinitionInfo {
+3
View File
@@ -4209,6 +4209,7 @@ declare namespace ts {
InsertSpaceBeforeFunctionParenthesis?: boolean;
PlaceOpenBraceOnNewLineForFunctions: boolean;
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
insertSpaceBeforeTypeAnnotation?: boolean;
}
interface FormatCodeSettings extends EditorSettings {
insertSpaceAfterCommaDelimiter?: boolean;
@@ -4226,6 +4227,7 @@ declare namespace ts {
insertSpaceBeforeFunctionParenthesis?: boolean;
placeOpenBraceOnNewLineForFunctions?: boolean;
placeOpenBraceOnNewLineForControlBlocks?: boolean;
insertSpaceBeforeTypeAnnotation?: boolean;
}
interface DefinitionInfo {
fileName: string;
@@ -6845,6 +6847,7 @@ declare namespace ts.server.protocol {
insertSpaceBeforeFunctionParenthesis?: boolean;
placeOpenBraceOnNewLineForFunctions?: boolean;
placeOpenBraceOnNewLineForControlBlocks?: boolean;
insertSpaceBeforeTypeAnnotation?: boolean;
}
interface CompilerOptions {
allowJs?: boolean;
+2
View File
@@ -4209,6 +4209,7 @@ declare namespace ts {
InsertSpaceBeforeFunctionParenthesis?: boolean;
PlaceOpenBraceOnNewLineForFunctions: boolean;
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
insertSpaceBeforeTypeAnnotation?: boolean;
}
interface FormatCodeSettings extends EditorSettings {
insertSpaceAfterCommaDelimiter?: boolean;
@@ -4226,6 +4227,7 @@ declare namespace ts {
insertSpaceBeforeFunctionParenthesis?: boolean;
placeOpenBraceOnNewLineForFunctions?: boolean;
placeOpenBraceOnNewLineForControlBlocks?: boolean;
insertSpaceBeforeTypeAnnotation?: boolean;
}
interface DefinitionInfo {
fileName: string;
@@ -9,6 +9,7 @@
/////*insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets*/[1 ]; [ ]; []; [,];
/////*insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces*/`${1}`;`${ 1 }`
/////*insertSpaceAfterTypeAssertion*/const bar = <Bar> Thing.getFoo();
/////*insertSpaceBeforeTypeAnnotation*/const bar : number = 1;
/////*placeOpenBraceOnNewLineForFunctions*/class foo {
////}
/////*placeOpenBraceOnNewLineForControlBlocks*/if (true) {
@@ -26,6 +27,7 @@ runTest("insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis", " ( 1 )
runTest("insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets", "[ 1 ];[];[];[ , ];", "[1];[];[];[,];");
runTest("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces", "`${ 1 }`; `${ 1 }`", "`${1}`; `${1}`");
runTest("insertSpaceAfterTypeAssertion", "const bar = <Bar> Thing.getFoo();", "const bar = <Bar>Thing.getFoo();");
runTest("insertSpaceBeforeTypeAnnotation", "const bar : number = 1;", "const bar: number = 1;");
runTest("placeOpenBraceOnNewLineForFunctions", "class foo", "class foo {");
runTest("placeOpenBraceOnNewLineForControlBlocks", "if (true)", "if (true) {");
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}) {}");
+1
View File
@@ -97,6 +97,7 @@ declare namespace FourSlashInterface {
InsertSpaceAfterTypeAssertion: boolean;
PlaceOpenBraceOnNewLineForFunctions: boolean;
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
insertSpaceBeforeTypeAnnotation: boolean;
[s: string]: boolean | number | string | undefined;
}
interface Range {