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

Commit

Permalink
space-before-function-paren: Ignore for async arrow function with no …
Browse files Browse the repository at this point in the history
…parentheses (#2833)
  • Loading branch information
andy-hanson authored and adidahiya committed May 30, 2017
1 parent 5875607 commit 237b86f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/rules/spaceBeforeFunctionParenRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,27 @@ function walk(ctx: Lint.WalkContext<Options>): void {
ts.forEachChild(sourceFile, function cb(node: ts.Node): void {
const option = getOption(node, options);
if (option !== undefined) {
const openParen = Lint.childOfKind(node, ts.SyntaxKind.OpenParenToken)!;
const hasSpace = ts.isWhiteSpaceLike(sourceFile.text.charCodeAt(openParen.end - 2));

if (hasSpace && option === "never") {
const pos = openParen.getStart() - 1;
ctx.addFailureAt(pos, 1, Rule.INVALID_WHITESPACE_ERROR, Lint.Replacement.deleteText(pos, 1));
} else if (!hasSpace && option === "always") {
const pos = openParen.getStart();
ctx.addFailureAt(pos, 1, Rule.MISSING_WHITESPACE_ERROR, Lint.Replacement.appendText(pos, " "));
}
check(node, option);
}

ts.forEachChild(node, cb);
});

function check(node: ts.Node, option: "always" | "never"): void {
const openParen = Lint.childOfKind(node, ts.SyntaxKind.OpenParenToken);
// openParen may be missing for an async arrow function `async x => ...`.
if (openParen === undefined) { return; }

const hasSpace = ts.isWhiteSpaceLike(sourceFile.text.charCodeAt(openParen.end - 2));

if (hasSpace && option === "never") {
const pos = openParen.getStart() - 1;
ctx.addFailureAt(pos, 1, Rule.INVALID_WHITESPACE_ERROR, Lint.Replacement.deleteText(pos, 1));
} else if (!hasSpace && option === "always") {
const pos = openParen.getStart();
ctx.addFailureAt(pos, 1, Rule.MISSING_WHITESPACE_ERROR, Lint.Replacement.appendText(pos, " "));
}
}
}

function getOption(node: ts.Node, options: Options): Option | undefined {
Expand Down
1 change: 1 addition & 0 deletions test/rules/space-before-function-paren/always/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var arrow = () => {};
async () => {};
var arrow = async () => {};

async x => x;

// Method
interface IMyInterface {
Expand Down
1 change: 1 addition & 0 deletions test/rules/space-before-function-paren/always/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async() => {};
var arrow = async() => {};
~ [space-before-function-paren]

async x => x;

// Method
interface IMyInterface {
Expand Down

0 comments on commit 237b86f

Please sign in to comment.