Skip to content

Format template internal spaces #5103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/services/formatting/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@ namespace ts.formatting {
// Async functions
public SpaceBetweenAsyncAndFunctionKeyword: Rule;

// Tagged template string
// Template strings
public SpaceBetweenTagAndTemplateString: Rule;
public NoSpaceAfterTemplateHeadAndMiddle: Rule;
public NoSpaceBeforeTemplateMiddleAndTail: Rule;

constructor() {
///
Expand Down Expand Up @@ -371,6 +373,8 @@ namespace ts.formatting {

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

// These rules are higher in priority than user-configurable rules.
this.HighPriorityCommonRules =
Expand Down Expand Up @@ -399,7 +403,7 @@ namespace ts.formatting {
this.SpaceBeforeBinaryKeywordOperator, this.SpaceAfterBinaryKeywordOperator,
this.SpaceAfterVoidOperator,
this.SpaceBetweenAsyncAndFunctionKeyword,
this.SpaceBetweenTagAndTemplateString,
this.SpaceBetweenTagAndTemplateString, this.NoSpaceAfterTemplateHeadAndMiddle, this.NoSpaceBeforeTemplateMiddleAndTail,

// TypeScript-specific rules
this.NoSpaceAfterConstructor, this.NoSpaceAfterModuleImport,
Expand Down
26 changes: 15 additions & 11 deletions tests/cases/fourslash/formatTemplateLiteral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
////var x = `sadasdasdasdasfegsfd
/////*1*/rasdesgeryt35t35y35 e4 ergt er 35t 3535 `;
////var y = `1${2}/*2*/3`;
////let z= `foo`/*3*/
////let w= `bar${3}`/*4*/
////
/////*formatStart*/
////let z= `foo`;/*3*/
////let w= `bar${3}`;/*4*/
////String.raw
//// `template`/*5*/
////String.raw`foo`/*6*/
////String.raw `bar${3}`/*7*/
//// `template`;/*5*/
////String.raw`foo`;/*6*/
////String.raw `bar${3}`;/*7*/
////`Write ${ JSON.stringify("") } and ${ (765) } and ${ 346 }`;/*spaceInside*/
/////*formatEnd*/


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

format.selection("formatStart", "formatEnd");

goTo.marker("3");
edit.insert(";");
verify.currentLineContentIs("let z = `foo`;");
goTo.marker("4");
edit.insert(";");
verify.currentLineContentIs("let w = `bar${3}`;");

goTo.marker("5");
edit.insert(";");
verify.currentLineContentIs(" `template`;");
goTo.marker("6");
edit.insert(";");
verify.currentLineContentIs("String.raw `foo`;");
goTo.marker("7");
edit.insert(";");
verify.currentLineContentIs("String.raw `bar${3}`;");
verify.currentLineContentIs("String.raw `bar${3}`;");

goTo.marker("spaceInside");
verify.currentLineContentIs('`Write ${JSON.stringify("")} and ${(765)} and ${346}`;');