Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Fix 'space-before-function-paren' for anonymous/arrow generic functions #3085

Merged
merged 4 commits into from
Sep 17, 2017
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
15 changes: 12 additions & 3 deletions src/rules/spaceBeforeFunctionParenRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,21 @@ function walk(ctx: Lint.WalkContext<Options>): void {
function getOption(node: ts.Node, options: Options): Option | undefined {
switch (node.kind) {
case ts.SyntaxKind.ArrowFunction:
return Lint.hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword) ? options.asyncArrow : undefined;
return !hasTypeParameters(node) && Lint.hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword)
? options.asyncArrow : undefined;

case ts.SyntaxKind.Constructor:
return options.constructor;

case ts.SyntaxKind.FunctionDeclaration:
// name is optional for function declaration which is default export (TS will emit error in other cases).
// Can be handled in the same way as function expression.
case ts.SyntaxKind.FunctionExpression:
return (node as ts.FunctionExpression).name !== undefined ? options.named : options.anonymous;
case ts.SyntaxKind.FunctionExpression: {
const functionName = (node as ts.FunctionExpression).name;
const hasName = functionName !== undefined && functionName.text !== "";

return hasName ? options.named : !hasTypeParameters(node) ? options.anonymous : undefined;
}

case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.MethodSignature:
Expand All @@ -131,3 +136,7 @@ function getOption(node: ts.Node, options: Options): Option | undefined {
return undefined;
}
}

function hasTypeParameters(node: ts.Node): boolean {
return (node as ts.SignatureDeclaration).typeParameters !== undefined;
}
12 changes: 12 additions & 0 deletions test/rules/space-before-function-paren/always/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var f = function (): void {
};
var f = function (a: string, cb: ()=>{}): void {};

function <T>() {}
var f = function <T>() {};

// Named
function foobar (){}
Expand All @@ -21,6 +23,8 @@ var f = function foobar (): void{
};
var f = function foobar (a: string, cb: ()=>{}): void{};

function foobar<T> () {}
var f = function foobar<T> () {}

// Default export (name ommited)
export default function () {}
Expand All @@ -35,17 +39,25 @@ var arrow = async () => {};

async x => x;

<T>() => {};
var arrow = <T>() => {};

async <T>() => {};
var arrow = async <T>() => {};

// Method
interface IMyInterface {
one ();
two (): void;
three (a: string, cb: ()=>{}): void;
four<T> ();
}
class MyClass {
one () {}
two (): void {
}
three (a: string, cb: ()=>{}): void {}
four<T> () {}
}


Expand Down
16 changes: 16 additions & 0 deletions test/rules/space-before-function-paren/always/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var f = function(): void {
var f = function(a: string, cb: ()=>{}): void {};
~ [space-before-function-paren]

function <T>() {}
var f = function <T>() {};

// Named
function foobar(){}
Expand All @@ -33,6 +35,10 @@ var f = function foobar(): void{
var f = function foobar(a: string, cb: ()=>{}): void{};
~ [space-before-function-paren]

function foobar<T>() {}
~ [space-before-function-paren]
var f = function foobar<T>() {}
~ [space-before-function-paren]

// Default export (name ommited)
export default function() {}
Expand All @@ -50,6 +56,12 @@ var arrow = async() => {};

async x => x;

<T>() => {};
var arrow = <T>() => {};

async <T>() => {};
var arrow = async <T>() => {};

// Method
interface IMyInterface {
one();
Expand All @@ -58,6 +70,8 @@ interface IMyInterface {
~ [space-before-function-paren]
three(a: string, cb: ()=>{}): void;
~ [space-before-function-paren]
four<T>();
~ [space-before-function-paren]
}
class MyClass {
one() {}
Expand All @@ -67,6 +81,8 @@ class MyClass {
}
three(a: string, cb: ()=>{}): void {}
~ [space-before-function-paren]
four<T>() {}
~ [space-before-function-paren]
}


Expand Down
12 changes: 12 additions & 0 deletions test/rules/space-before-function-paren/mixed/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var f = function (): void {
};
var f = function (a: string, cb: ()=>{}): void {};

function <T>() {}
var f = function <T>() {};

// Named
function foobar(){}
Expand All @@ -16,6 +18,9 @@ var f = function foobar(): void{
};
var f = function foobar(a: string, cb: ()=>{}): void{};

function foobar<T>() {}
var f = function foobar<T>() {}

// Default export (name ommited)
export default function () {}

Expand All @@ -27,18 +32,25 @@ var arrow = () => {};
async () => {};
var arrow = async () => {};

<T>() => {};
var arrow = <T>() => {};

async <T>() => {};
var arrow = async <T>() => {};

// Method
interface IMyInterface {
one();
two(): void;
three(a: string, cb: ()=>{}): void;
four<T>();
}
class MyClass {
one() {}
two(): void {
}
three(a: string, cb: ()=>{}): void {}
four<T>() {}
}


Expand Down
16 changes: 16 additions & 0 deletions test/rules/space-before-function-paren/mixed/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var f = function(): void {
var f = function(a: string, cb: ()=>{}): void {};
~ [missing-space]

function <T>() {}
var f = function <T>() {};

// Named
function foobar (){}
Expand All @@ -25,6 +27,11 @@ var f = function foobar (): void{
var f = function foobar (a: string, cb: ()=>{}): void{};
~ [invalid-space]

function foobar<T> () {}
~ [invalid-space]
var f = function foobar<T> () {}
~ [invalid-space]

// Default export (name ommited)
export default function() {}
~ [missing-space]
Expand All @@ -39,6 +46,11 @@ async() => {};
var arrow = async() => {};
~ [missing-space]

<T>() => {};
var arrow = <T>() => {};

async <T>() => {};
var arrow = async <T>() => {};

// Method
interface IMyInterface {
Expand All @@ -48,6 +60,8 @@ interface IMyInterface {
~ [invalid-space]
three (a: string, cb: ()=>{}): void;
~ [invalid-space]
four<T> ();
~ [invalid-space]
}
class MyClass {
one () {}
Expand All @@ -57,6 +71,8 @@ class MyClass {
}
three (a: string, cb: ()=>{}): void {}
~ [invalid-space]
four<T> () {}
~ [invalid-space]
}


Expand Down
12 changes: 12 additions & 0 deletions test/rules/space-before-function-paren/never/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var f = function(): void {
};
var f = function(a: string, cb: ()=>{}): void {};

function <T>() {}
var f = function <T>() {};

// Named
function foobar(){}
Expand All @@ -21,6 +23,9 @@ var f = function foobar(): void{
};
var f = function foobar(a: string, cb: ()=>{}): void{};

function foobar<T>() {}
var f = function foobar<T>() {}

// Default export (name ommited)
export default function() {}

Expand All @@ -32,18 +37,25 @@ var arrow = () => {};
async() => {};
var arrow = async() => {};

<T>() => {};
var arrow = <T>() => {};

async <T>() => {};
var arrow = async <T>() => {};

// Method
interface IMyInterface {
one();
two(): void;
three(a: string, cb: ()=>{}): void;
four<T>();
}
class MyClass {
one() {}
two(): void {
}
three(a: string, cb: ()=>{}): void {}
four<T>() {}

get a() {}
set a() {}
Expand Down
16 changes: 16 additions & 0 deletions test/rules/space-before-function-paren/never/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var f = function (): void {
var f = function (a: string, cb: ()=>{}): void {};
~ [0]

function <T>() {}
var f = function <T>() {};

// Named
function foobar (){}
Expand All @@ -33,6 +35,11 @@ var f = function foobar (): void{
var f = function foobar (a: string, cb: ()=>{}): void{};
~ [0]

function foobar<T> () {}
~ [0]
var f = function foobar<T> () {}
~ [0]

// Default export (name ommited)
export default function () {}
~ [0]
Expand All @@ -47,6 +54,11 @@ async () => {};
var arrow = async () => {};
~ [0]

<T>() => {};
var arrow = <T>() => {};

async <T>() => {};
var arrow = async <T>() => {};

// Method
interface IMyInterface {
Expand All @@ -56,6 +68,8 @@ interface IMyInterface {
~ [0]
three (a: string, cb: ()=>{}): void;
~ [0]
four<T> ();
~ [0]
}
class MyClass {
one () {}
Expand All @@ -65,6 +79,8 @@ class MyClass {
}
three (a: string, cb: ()=>{}): void {}
~ [0]
four<T> () {}
~ [0]

get a () {}
~ [0]
Expand Down