Skip to content
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

prettierx space-before-function-paren option #6

Merged
merged 1 commit into from
Jan 2, 2019
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ prettierx <options> <file(s)>

| Option | Default value | CLI Override | API Override | Description |
| ------ | ------------- | ------------ | ------------ | ------------------------------ |
| TBD | TBD | TBD | TBD | TBD |
| Space before function parentheses | `false` | `--space-before-function-paren` | `spaceBeforeFunctionParen: <bool>` | Put a space before function parenthesis. |

<!-- - FUTURE TBD prettierx vs prettier (???):
![Prettier Banner](https://raw.githubusercontent.com/prettier/prettier-logo/master/images/prettier-banner-light.png)
Expand Down
8 changes: 8 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ These options cannot be used with `cursorOffset`.
| `0` | `--range-start <int>` | `rangeStart: <int>` |
| `Infinity` | `--range-end <int>` | `rangeEnd: <int>` |

## Space before function parentheses

Put a space before function parenthesis.

| Default | CLI Override | API Override |
| ------- | ------------------------------- | ---------------------------------- |
| `false` | `--space-before-function-paren` | `spaceBeforeFunctionParen: <bool>` |

## Parser

Specify which parser to use.
Expand Down
6 changes: 6 additions & 0 deletions src/language-js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ module.exports = {
oppositeDescription:
"Do not print semicolons, except at the beginning of lines which may need them."
},
spaceBeforeFunctionParen: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description: "Put a space before function parenthesis."
},
singleQuote: commonOptions.singleQuote,
jsxSingleQuote: {
since: "1.15.0",
Expand Down
5 changes: 4 additions & 1 deletion src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -3736,14 +3736,16 @@ function printMethod(path, options, print) {
key = concat(["[", key, "]"]);
}

parts.push(key);

parts.push(
key,
concat(
path.call(
valuePath => [
printFunctionTypeParameters(valuePath, options, print),
group(
concat([
options.spaceBeforeFunctionParen ? " " : "",
printFunctionParams(valuePath, print, options),
printReturnType(valuePath, print, options)
])
Expand Down Expand Up @@ -4234,6 +4236,7 @@ function printFunctionDeclaration(path, print, options) {
printFunctionTypeParameters(path, options, print),
group(
concat([
options.spaceBeforeFunctionParen ? " " : "",
printFunctionParams(path, print, options),
printReturnType(path, print, options)
])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`eslint.js 1`] = `
====================================options=====================================
parsers: ["flow", "typescript"]
printWidth: 80
spaceBeforeFunctionParen: true
| printWidth
=====================================input======================================
/*eslint space-before-function-paren: "error"*/
/*eslint-env es6*/

function foo() {
// ...
}

var bar = function() {
// ...
};

var bar = function foo() {
// ...
};

class Foo {
constructor() {
// ...
}
bar() {
// ...
}
}

var foo = {
bar() {
// ...
}
};

var foo = async() => 1

=====================================output=====================================
/*eslint space-before-function-paren: "error"*/
/*eslint-env es6*/

function foo () {
// ...
}

var bar = function () {
// ...
};

var bar = function foo () {
// ...
};

class Foo {
constructor () {
// ...
}
bar () {
// ...
}
}

var foo = {
bar () {
// ...
}
};

var foo = async () => 1;

================================================================================
`;

exports[`eslint.js 2`] = `
====================================options=====================================
parsers: ["flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
/*eslint space-before-function-paren: "error"*/
/*eslint-env es6*/

function foo() {
// ...
}

var bar = function() {
// ...
};

var bar = function foo() {
// ...
};

class Foo {
constructor() {
// ...
}
bar() {
// ...
}
}

var foo = {
bar() {
// ...
}
};

var foo = async() => 1

=====================================output=====================================
/*eslint space-before-function-paren: "error"*/
/*eslint-env es6*/

function foo() {
// ...
}

var bar = function() {
// ...
};

var bar = function foo() {
// ...
};

class Foo {
constructor() {
// ...
}
bar() {
// ...
}
}

var foo = {
bar() {
// ...
}
};

var foo = async () => 1;

================================================================================
`;
31 changes: 31 additions & 0 deletions tests/space-before-function-paren/eslint-compat/eslint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*eslint space-before-function-paren: "error"*/
/*eslint-env es6*/

function foo() {
// ...
}

var bar = function() {
// ...
};

var bar = function foo() {
// ...
};

class Foo {
constructor() {
// ...
}
bar() {
// ...
}
}

var foo = {
bar() {
// ...
}
};

var foo = async() => 1
2 changes: 2 additions & 0 deletions tests/space-before-function-paren/eslint-compat/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
run_spec(__dirname, ["flow", "typescript"], { spaceBeforeFunctionParen: true });
run_spec(__dirname, ["flow", "typescript"]);
Loading