Skip to content

Commit fa49ac0

Browse files
authored
fix(19385): add space after brace in the multiline string template (#38742)
1 parent 68d2ee0 commit fa49ac0

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

src/services/formatting/rules.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,9 @@ namespace ts.formatting {
279279
rule("NoSpaceBetweenEmptyBraceBrackets", SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, [isOptionDisabled("insertSpaceAfterOpeningAndBeforeClosingEmptyBraces"), isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
280280

281281
// Insert space after opening and before closing template string braces
282-
rule("SpaceAfterTemplateHeadAndMiddle", [SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle], anyToken, [isOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), isNonJsxSameLineTokenContext], RuleAction.InsertSpace),
282+
rule("SpaceAfterTemplateHeadAndMiddle", [SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle], anyToken, [isOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), isNonJsxTextContext], RuleAction.InsertSpace, RuleFlags.CanDeleteNewLines),
283283
rule("SpaceBeforeTemplateMiddleAndTail", anyToken, [SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail], [isOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), isNonJsxSameLineTokenContext], RuleAction.InsertSpace),
284-
rule("NoSpaceAfterTemplateHeadAndMiddle", [SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle], anyToken, [isOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
284+
rule("NoSpaceAfterTemplateHeadAndMiddle", [SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle], anyToken, [isOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), isNonJsxTextContext], RuleAction.DeleteSpace, RuleFlags.CanDeleteNewLines),
285285
rule("NoSpaceBeforeTemplateMiddleAndTail", anyToken, [SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail], [isOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
286286

287287
// No space after { and before } in JSX expression
@@ -690,6 +690,10 @@ namespace ts.formatting {
690690
return context.TokensAreOnSameLine() && context.contextNode.kind !== SyntaxKind.JsxText;
691691
}
692692

693+
function isNonJsxTextContext(context: FormattingContext): boolean {
694+
return context.contextNode.kind !== SyntaxKind.JsxText;
695+
}
696+
693697
function isNonJsxElementOrFragmentContext(context: FormattingContext): boolean {
694698
return context.contextNode.kind !== SyntaxKind.JsxElement && context.contextNode.kind !== SyntaxKind.JsxFragment;
695699
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const a1 = `${ 1 }${ 1 }`;
4+
////const a2 = `
5+
//// ${ 1 }${ 1 }
6+
////`;
7+
////const a3 = `
8+
////
9+
////
10+
//// ${ 1 }${ 1 }
11+
////`;
12+
////const a4 = `
13+
////
14+
//// ${ 1 }${ 1 }
15+
////
16+
////`;
17+
////const a5 = `text ${ 1 } text ${ 1 } text`;
18+
////const a6 = `
19+
//// text ${ 1 }
20+
//// text ${ 1 }
21+
//// text
22+
////`;
23+
24+
format.setOption("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces", false);
25+
format.document();
26+
verify.currentFileContentIs(
27+
"const a1 = `${1}${1}`;\n" +
28+
29+
"const a2 = `\n" +
30+
" ${1}${1}\n" +
31+
"`;\n" +
32+
33+
"const a3 = `\n" +
34+
"\n" +
35+
"\n" +
36+
" ${1}${1}\n" +
37+
"`;\n" +
38+
39+
"const a4 = `\n" +
40+
"\n" +
41+
" ${1}${1}\n" +
42+
"\n" +
43+
"`;\n" +
44+
45+
"const a5 = `text ${1} text ${1} text`;\n" +
46+
47+
"const a6 = `\n" +
48+
" text ${1}\n" +
49+
" text ${1}\n" +
50+
" text\n" +
51+
"`;"
52+
);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const a1 = `${1}${1}`;
4+
////const a2 = `
5+
//// ${1}${1}
6+
////`;
7+
////const a3 = `
8+
////
9+
////
10+
//// ${1}${1}
11+
////`;
12+
////const a4 = `
13+
////
14+
//// ${1}${1}
15+
////
16+
////`;
17+
////const a5 = `text ${1} text ${1} text`;
18+
////const a6 = `
19+
//// text ${1}
20+
//// text ${1}
21+
//// text
22+
////`;
23+
24+
format.setOption("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces", true);
25+
format.document();
26+
verify.currentFileContentIs(
27+
"const a1 = `${ 1 }${ 1 }`;\n" +
28+
29+
"const a2 = `\n" +
30+
" ${ 1 }${ 1 }\n" +
31+
"`;\n" +
32+
33+
"const a3 = `\n" +
34+
"\n" +
35+
"\n" +
36+
" ${ 1 }${ 1 }\n" +
37+
"`;\n" +
38+
39+
"const a4 = `\n" +
40+
"\n" +
41+
" ${ 1 }${ 1 }\n" +
42+
"\n" +
43+
"`;\n" +
44+
45+
"const a5 = `text ${ 1 } text ${ 1 } text`;\n" +
46+
47+
"const a6 = `\n" +
48+
" text ${ 1 }\n" +
49+
" text ${ 1 }\n" +
50+
" text\n" +
51+
"`;"
52+
);

0 commit comments

Comments
 (0)