Skip to content

Commit

Permalink
prettierx space-before-function-paren option (#6)
Browse files Browse the repository at this point in the history
ported (largely reimplemented) from prettier-miscellaneous
(arijs#22)

with ES test moved to eslint-compat subdirectory;
     test space-before-function-paren with real member function;
     test coverage added for space-before-function-paren not enabled;
     configuration & documentation according to new prettier format;
     tslint-compat test to cover use of TypeScript generics;
     issue with TypeScript generics resolved; and
     space-before-function-paren option added to README.md.

Co-authored-by: Christopher J. Brody <chris.brody@gmail.com>
Co-authored-by: Joseph Frazier <1212jtraceur@gmail.com>
Co-authored-by: Rafael Hengles <rafael@hengles.net>
Co-authored-by: Ika <ikatyang@gmail.com>
Co-authored-by: Lucas Azzola <derflatulator@gmail.com>
  • Loading branch information
5 people committed Jan 2, 2019
1 parent d7532ef commit 828aab3
Show file tree
Hide file tree
Showing 14 changed files with 533 additions and 4 deletions.
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

0 comments on commit 828aab3

Please sign in to comment.