Skip to content

Commit 53cff21

Browse files
committed
Merge pull request #5103 from SaschaNaz/formatStringTemplates
Format template internal spaces
2 parents edc938f + c9e237c commit 53cff21

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/services/formatting/rules.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,10 @@ namespace ts.formatting {
216216
// Async functions
217217
public SpaceBetweenAsyncAndFunctionKeyword: Rule;
218218

219-
// Tagged template string
219+
// Template strings
220220
public SpaceBetweenTagAndTemplateString: Rule;
221+
public NoSpaceAfterTemplateHeadAndMiddle: Rule;
222+
public NoSpaceBeforeTemplateMiddleAndTail: Rule;
221223

222224
constructor() {
223225
///
@@ -371,6 +373,8 @@ namespace ts.formatting {
371373

372374
// template string
373375
this.SpaceBetweenTagAndTemplateString = new Rule(RuleDescriptor.create3(SyntaxKind.Identifier, Shared.TokenRange.FromTokens([SyntaxKind.NoSubstitutionTemplateLiteral, SyntaxKind.TemplateHead])), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
376+
this.NoSpaceAfterTemplateHeadAndMiddle = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
377+
this.NoSpaceBeforeTemplateMiddleAndTail = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail])), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
374378

375379
// These rules are higher in priority than user-configurable rules.
376380
this.HighPriorityCommonRules =
@@ -399,7 +403,7 @@ namespace ts.formatting {
399403
this.SpaceBeforeBinaryKeywordOperator, this.SpaceAfterBinaryKeywordOperator,
400404
this.SpaceAfterVoidOperator,
401405
this.SpaceBetweenAsyncAndFunctionKeyword,
402-
this.SpaceBetweenTagAndTemplateString,
406+
this.SpaceBetweenTagAndTemplateString, this.NoSpaceAfterTemplateHeadAndMiddle, this.NoSpaceBeforeTemplateMiddleAndTail,
403407

404408
// TypeScript-specific rules
405409
this.NoSpaceAfterConstructor, this.NoSpaceAfterModuleImport,

tests/cases/fourslash/formatTemplateLiteral.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
////var x = `sadasdasdasdasfegsfd
33
/////*1*/rasdesgeryt35t35y35 e4 ergt er 35t 3535 `;
44
////var y = `1${2}/*2*/3`;
5-
////let z= `foo`/*3*/
6-
////let w= `bar${3}`/*4*/
5+
////
6+
/////*formatStart*/
7+
////let z= `foo`;/*3*/
8+
////let w= `bar${3}`;/*4*/
79
////String.raw
8-
//// `template`/*5*/
9-
////String.raw`foo`/*6*/
10-
////String.raw `bar${3}`/*7*/
10+
//// `template`;/*5*/
11+
////String.raw`foo`;/*6*/
12+
////String.raw `bar${3}`;/*7*/
13+
////`Write ${ JSON.stringify("") } and ${ (765) } and ${ 346 }`;/*spaceInside*/
14+
/////*formatEnd*/
1115

1216

1317
goTo.marker("1");
@@ -18,19 +22,19 @@ edit.insert("\r\n");
1822
verify.indentationIs(0);
1923
verify.currentLineContentIs("3`;")
2024

25+
format.selection("formatStart", "formatEnd");
26+
2127
goTo.marker("3");
22-
edit.insert(";");
2328
verify.currentLineContentIs("let z = `foo`;");
2429
goTo.marker("4");
25-
edit.insert(";");
2630
verify.currentLineContentIs("let w = `bar${3}`;");
2731

2832
goTo.marker("5");
29-
edit.insert(";");
3033
verify.currentLineContentIs(" `template`;");
3134
goTo.marker("6");
32-
edit.insert(";");
3335
verify.currentLineContentIs("String.raw `foo`;");
3436
goTo.marker("7");
35-
edit.insert(";");
36-
verify.currentLineContentIs("String.raw `bar${3}`;");
37+
verify.currentLineContentIs("String.raw `bar${3}`;");
38+
39+
goTo.marker("spaceInside");
40+
verify.currentLineContentIs('`Write ${JSON.stringify("")} and ${(765)} and ${346}`;');

0 commit comments

Comments
 (0)