fix(19385): add space after brace in the multiline string template (#38742)

This commit is contained in:
Alexander T
2020-06-01 20:30:52 +03:00
committed by GitHub
parent 68d2ee055c
commit fa49ac0b78
3 changed files with 110 additions and 2 deletions

View File

@@ -279,9 +279,9 @@ namespace ts.formatting {
rule("NoSpaceBetweenEmptyBraceBrackets", SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, [isOptionDisabled("insertSpaceAfterOpeningAndBeforeClosingEmptyBraces"), isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
// Insert space after opening and before closing template string braces
rule("SpaceAfterTemplateHeadAndMiddle", [SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle], anyToken, [isOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), isNonJsxSameLineTokenContext], RuleAction.InsertSpace),
rule("SpaceAfterTemplateHeadAndMiddle", [SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle], anyToken, [isOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), isNonJsxTextContext], RuleAction.InsertSpace, RuleFlags.CanDeleteNewLines),
rule("SpaceBeforeTemplateMiddleAndTail", anyToken, [SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail], [isOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), isNonJsxSameLineTokenContext], RuleAction.InsertSpace),
rule("NoSpaceAfterTemplateHeadAndMiddle", [SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle], anyToken, [isOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
rule("NoSpaceAfterTemplateHeadAndMiddle", [SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle], anyToken, [isOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), isNonJsxTextContext], RuleAction.DeleteSpace, RuleFlags.CanDeleteNewLines),
rule("NoSpaceBeforeTemplateMiddleAndTail", anyToken, [SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail], [isOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
// No space after { and before } in JSX expression
@@ -690,6 +690,10 @@ namespace ts.formatting {
return context.TokensAreOnSameLine() && context.contextNode.kind !== SyntaxKind.JsxText;
}
function isNonJsxTextContext(context: FormattingContext): boolean {
return context.contextNode.kind !== SyntaxKind.JsxText;
}
function isNonJsxElementOrFragmentContext(context: FormattingContext): boolean {
return context.contextNode.kind !== SyntaxKind.JsxElement && context.contextNode.kind !== SyntaxKind.JsxFragment;
}

View File

@@ -0,0 +1,52 @@
/// <reference path='fourslash.ts' />
////const a1 = `${ 1 }${ 1 }`;
////const a2 = `
//// ${ 1 }${ 1 }
////`;
////const a3 = `
////
////
//// ${ 1 }${ 1 }
////`;
////const a4 = `
////
//// ${ 1 }${ 1 }
////
////`;
////const a5 = `text ${ 1 } text ${ 1 } text`;
////const a6 = `
//// text ${ 1 }
//// text ${ 1 }
//// text
////`;
format.setOption("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces", false);
format.document();
verify.currentFileContentIs(
"const a1 = `${1}${1}`;\n" +
"const a2 = `\n" +
" ${1}${1}\n" +
"`;\n" +
"const a3 = `\n" +
"\n" +
"\n" +
" ${1}${1}\n" +
"`;\n" +
"const a4 = `\n" +
"\n" +
" ${1}${1}\n" +
"\n" +
"`;\n" +
"const a5 = `text ${1} text ${1} text`;\n" +
"const a6 = `\n" +
" text ${1}\n" +
" text ${1}\n" +
" text\n" +
"`;"
);

View File

@@ -0,0 +1,52 @@
/// <reference path='fourslash.ts' />
////const a1 = `${1}${1}`;
////const a2 = `
//// ${1}${1}
////`;
////const a3 = `
////
////
//// ${1}${1}
////`;
////const a4 = `
////
//// ${1}${1}
////
////`;
////const a5 = `text ${1} text ${1} text`;
////const a6 = `
//// text ${1}
//// text ${1}
//// text
////`;
format.setOption("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces", true);
format.document();
verify.currentFileContentIs(
"const a1 = `${ 1 }${ 1 }`;\n" +
"const a2 = `\n" +
" ${ 1 }${ 1 }\n" +
"`;\n" +
"const a3 = `\n" +
"\n" +
"\n" +
" ${ 1 }${ 1 }\n" +
"`;\n" +
"const a4 = `\n" +
"\n" +
" ${ 1 }${ 1 }\n" +
"\n" +
"`;\n" +
"const a5 = `text ${ 1 } text ${ 1 } text`;\n" +
"const a6 = `\n" +
" text ${ 1 }\n" +
" text ${ 1 }\n" +
" text\n" +
"`;"
);