Skip to content

Commit 3b3d715

Browse files
author
Andrew Ochsner
committed
Add InsertSpaceAfterConstructor option & additonal test cases
Fixes #12234
1 parent 7bf73be commit 3b3d715

File tree

8 files changed

+30
-3
lines changed

8 files changed

+30
-3
lines changed

src/harness/fourslash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ namespace FourSlash {
341341
insertSpaceAfterCommaDelimiter: true,
342342
insertSpaceAfterSemicolonInForStatements: true,
343343
insertSpaceBeforeAndAfterBinaryOperators: true,
344+
insertSpaceAfterConstructor: false,
344345
insertSpaceAfterKeywordsInControlFlowStatements: true,
345346
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
346347
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,

src/server/protocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,7 @@ namespace ts.server.protocol {
21942194
insertSpaceAfterCommaDelimiter?: boolean;
21952195
insertSpaceAfterSemicolonInForStatements?: boolean;
21962196
insertSpaceBeforeAndAfterBinaryOperators?: boolean;
2197+
insertSpaceAfterConstructor?: boolean;
21972198
insertSpaceAfterKeywordsInControlFlowStatements?: boolean;
21982199
insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean;
21992200
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;

src/server/utilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ namespace ts.server {
7878
newLineCharacter: host.newLine || "\n",
7979
convertTabsToSpaces: true,
8080
indentStyle: ts.IndentStyle.Smart,
81+
insertSpaceAfterConstructor: false,
8182
insertSpaceAfterCommaDelimiter: true,
8283
insertSpaceAfterSemicolonInForStatements: true,
8384
insertSpaceBeforeAndAfterBinaryOperators: true,

src/services/formatting/rules.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ namespace ts.formatting {
113113
// TypeScript-specific rules
114114

115115
// Treat constructor as an identifier in a function declaration, and remove spaces between constructor and following left parentheses
116+
public SpaceAfterConstructor: Rule;
116117
public NoSpaceAfterConstructor: Rule;
117118

118119
// Use of module as a function call. e.g.: import m2 = module("m2");
@@ -354,6 +355,7 @@ namespace ts.formatting {
354355
// TypeScript-specific higher priority rules
355356

356357
// Treat constructor as an identifier in a function declaration, and remove spaces between constructor and following left parentheses
358+
this.SpaceAfterConstructor = new Rule(RuleDescriptor.create1(SyntaxKind.ConstructorKeyword, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext), RuleAction.Space));
357359
this.NoSpaceAfterConstructor = new Rule(RuleDescriptor.create1(SyntaxKind.ConstructorKeyword, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete));
358360

359361
// Use of module as a function call. e.g.: import m2 = module("m2");
@@ -439,7 +441,7 @@ namespace ts.formatting {
439441
this.NoSpaceBeforeEqualInJsxAttribute, this.NoSpaceAfterEqualInJsxAttribute,
440442

441443
// TypeScript-specific rules
442-
this.NoSpaceAfterConstructor, this.NoSpaceAfterModuleImport,
444+
this.NoSpaceAfterModuleImport,
443445
this.SpaceAfterCertainTypeScriptKeywords, this.SpaceBeforeCertainTypeScriptKeywords,
444446
this.SpaceAfterModuleName,
445447
this.SpaceBeforeArrow, this.SpaceAfterArrow,

src/services/formatting/rulesProvider.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ namespace ts.formatting {
3838
private createActiveRules(options: ts.FormatCodeSettings): Rule[] {
3939
let rules = this.globalRules.HighPriorityCommonRules.slice(0);
4040

41+
if (options.insertSpaceAfterConstructor) {
42+
rules.push(this.globalRules.SpaceAfterConstructor);
43+
}
44+
else {
45+
rules.push(this.globalRules.NoSpaceAfterConstructor);
46+
}
47+
4148
if (options.insertSpaceAfterCommaDelimiter) {
4249
rules.push(this.globalRules.SpaceAfterComma);
4350
}

src/services/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ namespace ts {
418418
InsertSpaceAfterCommaDelimiter: boolean;
419419
InsertSpaceAfterSemicolonInForStatements: boolean;
420420
InsertSpaceBeforeAndAfterBinaryOperators: boolean;
421+
InsertSpaceAfterConstructor?: boolean;
421422
InsertSpaceAfterKeywordsInControlFlowStatements: boolean;
422423
InsertSpaceAfterFunctionKeywordForAnonymousFunctions: boolean;
423424
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: boolean;
@@ -435,6 +436,7 @@ namespace ts {
435436
insertSpaceAfterCommaDelimiter?: boolean;
436437
insertSpaceAfterSemicolonInForStatements?: boolean;
437438
insertSpaceBeforeAndAfterBinaryOperators?: boolean;
439+
insertSpaceAfterConstructor?: boolean;
438440
insertSpaceAfterKeywordsInControlFlowStatements?: boolean;
439441
insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean;
440442
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;

tests/cases/fourslash/formattingSpaceBeforeFunctionParen.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
/////*1*/function foo() { }
44
/////*2*/function boo () { }
5+
/////*3*/var bar = function foo() { };
6+
/////*4*/var foo = { bar() { } };
57

68
format.setOption("InsertSpaceBeforeFunctionParenthesis", true);
79

@@ -10,4 +12,8 @@ format.document();
1012
goTo.marker('1');
1113
verify.currentLineContentIs('function foo () { }');
1214
goTo.marker('2');
13-
verify.currentLineContentIs('function boo () { }');
15+
verify.currentLineContentIs('function boo () { }');
16+
goTo.marker('3');
17+
verify.currentLineContentIs('var bar = function foo () { };');
18+
goTo.marker('4');
19+
verify.currentLineContentIs('var foo = { bar () { } };');

tests/cases/fourslash/formattingSpacesAfterConstructor.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,11 @@
33
/////*1*/class test { constructor () { } }
44
format.document();
55
goTo.marker("1");
6-
verify.currentLineContentIs("class test { constructor() { } }");
6+
verify.currentLineContentIs("class test { constructor() { } }");
7+
8+
/////*2*/class test { constructor () { } }
9+
format.setOption("InsertSpaceAfterConstructor", true);
10+
11+
format.document();
12+
goTo.marker("2");
13+
verify.currentLineContentIs("class test { constructor () { } }");

0 commit comments

Comments
 (0)