Skip to content

Commit

Permalink
Add --space-before-function-paren option (#22)
Browse files Browse the repository at this point in the history
* Add test demonstrating behavior on eslint space-before-function-paren sample code

* Add --space-before-function-paren option

This corresponds to ESLint's `space-before-function-paren` "always"
option: http://eslint.org/docs/rules/space-before-function-paren#always

See prettier#1139

* Document --space-before-function-paren option
  • Loading branch information
josephfrazier authored and rhengles committed Jun 20, 2017
1 parent ec98ca6 commit a93175c
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -295,6 +295,7 @@ Prettier ships with a handful of customizable format options, usable in both the
| **JSX Brackets on Same Line** - Put the `>` of a multi-line JSX element at the end of the last line instead of being alone on the next line | `false` | CLI: `--jsx-bracket-same-line` <br />API: `jsxBracketSameLine: <bool>` |
| **Align Object Properties** - Align colons in multiline object literals. Does nothing if object has computed property names. | `false` | CLI: `--align-object-properties` <br/>API: `alignObjectProperties: <bool>` |
| **No Space in Empty Function** - Omit space before empty anonymous function body.<br /><br />Valid options: <br /> - `true` <br /> - `false` | `false` | CLI: `--no-space-empty-fn` <br/>API: `noSpaceEmptyFn: <bool>` |
| **Space before Function Paren** - Put a [space before function parenthesis](http://eslint.org/docs/rules/space-before-function-paren#always).<br /><br />Valid options: <br /> - `true` <br /> - `false` | `false` | CLI: `--space-before-function-paren` <br/>API: `spaceBeforeFunctionParen: <bool>` |
| **Cursor Offset** - Specify where the cursor is. This option only works with `prettier.formatWithCursor`, and cannot be used with `rangeStart` and `rangeEnd`. | `-1` | CLI: `--cursor-offset <int>` <br />API: `cursorOffset: <int>` |
| **Range Start** - Format code starting at a given character offset. The range will extend backwards to the start of the first line containing the selected statement. This option cannot be used with `cursorOffset`. | `0` | CLI: `--range-start <int>` <br />API: `rangeStart: <int>` |
| **Range End** - Format code ending at a given character offset (exclusive). The range will extend forwards to the end of the selected statement. This option cannot be used with `cursorOffset`. | `Infinity` | CLI: `--range-end <int>` <br />API: `rangeEnd: <int>` |
Expand Down
4 changes: 4 additions & 0 deletions bin/prettier.js
Expand Up @@ -29,6 +29,7 @@ const argv = minimist(process.argv.slice(2), {
"jsx-bracket-same-line",
"align-object-properties",
"space-empty-fn",
"space-before-function-paren",
// The supports-color package (a sub sub dependency) looks directly at
// `process.argv` for `--no-color` and such-like options. The reason it is
// listed here is to avoid "Ignored unknown option: --no-color" warnings.
Expand Down Expand Up @@ -167,6 +168,7 @@ const options = {
jsxBracketSameLine: argv["jsx-bracket-same-line"],
alignObjectProperties: argv["align-object-properties"],
noSpaceEmptyFn: !argv["space-empty-fn"],
spaceBeforeFunctionParen: argv["space-before-function-paren"],
filepath: argv["stdin-filepath"],
trailingComma: getTrailingComma(),
parser: getParserOption()
Expand Down Expand Up @@ -262,6 +264,8 @@ if (argv["help"] || (!filepatterns.length && !stdin)) {
" --align-object-properties\n" +
" Align colons in multiline object literals. Does nothing if object has computed property names.\n" +
" --no-space-empty-fn Omit space before empty function body. Defaults to false.\n" +
" --space-before-function-paren\n" +
" Put a space before function parenthesis. Defaults to false.\n" +
" --parser <flow|babylon|typescript|postcss|json>\n" +
" Specify which parse to use. Defaults to babylon.\n" +
" --cursor-offset <int> Print (to stderr) where a cursor at the given position would move to after formatting.\n" +
Expand Down
1 change: 1 addition & 0 deletions src/options.js
Expand Up @@ -47,6 +47,7 @@ const defaults = {
noSpaceEmptyFn: false,
parser: "babylon",
semi: true,
spaceBeforeFunctionParen: false,
__log: false
};

Expand Down
3 changes: 3 additions & 0 deletions src/printer.js
Expand Up @@ -2848,6 +2848,7 @@ function printMethod(path, options, print) {
printFunctionTypeParameters(valuePath, options, print),
group(
concat([
options.spaceBeforeFunctionParen ? " " : "",
printFunctionParams(valuePath, print, options),
printReturnType(valuePath, print)
])
Expand Down Expand Up @@ -3175,6 +3176,7 @@ function printFunctionDeclaration(path, print, options) {
printFunctionTypeParameters(path, options, print),
group(
concat([
options.spaceBeforeFunctionParen ? " " : "",
printFunctionParams(path, print, options),
printReturnType(path, print)
])
Expand Down Expand Up @@ -3217,6 +3219,7 @@ function printObjectMethod(path, options, print) {
printFunctionTypeParameters(path, options, print),
group(
concat([
options.spaceBeforeFunctionParen ? " " : "",
printFunctionParams(path, print, options),
printReturnType(path, print)
])
Expand Down
62 changes: 62 additions & 0 deletions tests/space-before-function-paren/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,62 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`eslint.js 1`] = `
/*eslint space-before-function-paren: "error"*/
/*eslint-env es6*/
function foo() {
// ...
}
var bar = function() {
// ...
};
var bar = function foo() {
// ...
};
class Foo {
constructor() {
// ...
}
}
var foo = {
bar() {
// ...
}
};
var foo = async() => 1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*eslint space-before-function-paren: "error"*/
/*eslint-env es6*/
function foo () {
// ...
}
var bar = function () {
// ...
};
var bar = function foo () {
// ...
};
class Foo {
constructor () {
// ...
}
}
var foo = {
bar () {
// ...
}
};
var foo = async () => 1;
`;
28 changes: 28 additions & 0 deletions tests/space-before-function-paren/eslint.js
@@ -0,0 +1,28 @@
/*eslint space-before-function-paren: "error"*/
/*eslint-env es6*/

function foo() {
// ...
}

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

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

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

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

var foo = async() => 1
4 changes: 4 additions & 0 deletions tests/space-before-function-paren/jsfmt.spec.js
@@ -0,0 +1,4 @@
run_spec(__dirname, { spaceBeforeFunctionParen: true }, [
"babylon",
"typescript"
]);

0 comments on commit a93175c

Please sign in to comment.