Skip to content

Commit

Permalink
generatorStarSpacing option
Browse files Browse the repository at this point in the history
corresponding to eslint generator-star-spacing option

tested with and without space-before-function-paren enabled

as needed for consistency with standard & semistandard

based on proposal in prettier#5723

Co-authored-by: Christopher J. Brody <chris.brody@gmail.com>
Co-authored-by: Adam Stankiewicz <sheerun@sher.pl>
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
6 people committed Jan 6, 2019
1 parent 707da6f commit 39b01ee
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ prettierx <options> <file(s)>
| Option | Default value | CLI Override | API Override | Description |
| --------------------------------- | ------------- | ------------------------------- | ---------------------------------- | ---------------------------------------- |
| Space before function parentheses | `false` | `--space-before-function-paren` | `spaceBeforeFunctionParen: <bool>` | Put a space before function parenthesis. |
| Spaces around the star (\*\) in generator functions | `false` | `--generator-star-spacing` | `generatorStarSpacing: <bool>` | Add spaces around the star (\*\) in generator functions (before and after - from eslint). |

<!-- - 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 @@ -178,6 +178,14 @@ Put a space before function parenthesis.
| ------- | ------------------------------- | ---------------------------------- |
| `false` | `--space-before-function-paren` | `spaceBeforeFunctionParen: <bool>` |

## Generator star spacing

Add spaces around the star (\*\) in generator functions (before and after - from eslint).

| Default | CLI Override | API Override |
| ------- | ------------------------------- | ---------------------------------- |
| `false` | `--generator-star-spacing` | `generatorStarSpacing: <bool>` |

## Parser

Specify which parser to use.
Expand Down
7 changes: 7 additions & 0 deletions src/language-js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ module.exports = {
oppositeDescription:
"Do not print semicolons, except at the beginning of lines which may need them."
},
generatorStarSpacing: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description:
"Add spaces around the star ('*') in generator functions (before and after - from eslint)."
},
spaceBeforeFunctionParen: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
Expand Down
14 changes: 13 additions & 1 deletion src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -3723,6 +3723,9 @@ function printMethod(path, options, print) {
if (!kind || kind === "init" || kind === "method" || kind === "constructor") {
if (node.value.generator) {
parts.push("*");
if (options.generatorStarSpacing) {
parts.push(" ");
}
}
} else {
assert.ok(kind === "get" || kind === "set");
Expand Down Expand Up @@ -4226,6 +4229,9 @@ function printFunctionDeclaration(path, print, options) {
parts.push("function");

if (n.generator) {
if (options.generatorStarSpacing) {
parts.push(" ");
}
parts.push("*");
}
if (n.id) {
Expand All @@ -4236,7 +4242,10 @@ function printFunctionDeclaration(path, print, options) {
printFunctionTypeParameters(path, options, print),
group(
concat([
options.spaceBeforeFunctionParen ? " " : "",
options.spaceBeforeFunctionParen ||
(options.generatorStarSpacing && !n.id)
? " "
: "",
printFunctionParams(path, print, options),
printReturnType(path, print, options)
])
Expand All @@ -4257,6 +4266,9 @@ function printObjectMethod(path, options, print) {
}
if (objMethod.generator) {
parts.push("*");
if (options.generatorStarSpacing) {
parts.push(" ");
}
}
if (
objMethod.method ||
Expand Down
147 changes: 147 additions & 0 deletions tests/generator-star-spacing/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`correct.js 1`] = `
====================================options=====================================
generatorStarSpacing: true
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
// correct for:
// * "generator-star-spacing": [ "error", { "before": true, "after": true } ],
// but *not* for the following standard.js rules:
// * "space-before-function-paren": [ "error", "always" ],
// * "semi": [ "error", "never" ],
function * generator() {}
var anonymous = function * () {};
var shorthand = { * generator() {} };
class Example {
async * foo() {}
}
=====================================output=====================================
// correct for:
// * "generator-star-spacing": [ "error", { "before": true, "after": true } ],
// but *not* for the following standard.js rules:
// * "space-before-function-paren": [ "error", "always" ],
// * "semi": [ "error", "never" ],
function * generator() {}
var anonymous = function * () {};
var shorthand = { * generator() {} };
class Example {
async * foo() {}
}
================================================================================
`;

exports[`correct.js 2`] = `
====================================options=====================================
generatorStarSpacing: true
parsers: ["babel", "flow", "typescript"]
printWidth: 80
spaceBeforeFunctionParen: true
| printWidth
=====================================input======================================
// correct for:
// * "generator-star-spacing": [ "error", { "before": true, "after": true } ],
// but *not* for the following standard.js rules:
// * "space-before-function-paren": [ "error", "always" ],
// * "semi": [ "error", "never" ],
function * generator() {}
var anonymous = function * () {};
var shorthand = { * generator() {} };
class Example {
async * foo() {}
}
=====================================output=====================================
// correct for:
// * "generator-star-spacing": [ "error", { "before": true, "after": true } ],
// but *not* for the following standard.js rules:
// * "space-before-function-paren": [ "error", "always" ],
// * "semi": [ "error", "never" ],
function * generator () {}
var anonymous = function * () {};
var shorthand = { * generator () {} };
class Example {
async * foo () {}
}
================================================================================
`;

exports[`incorrect.js 1`] = `
====================================options=====================================
generatorStarSpacing: true
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
// *incorrect* for the following standard.js rules:
// * "generator-star-spacing": [ "error", { "before": true, "after": true } ],
// * "space-before-function-paren": [ "error", "always" ],
// * "semi": [ "error", "never" ],
function* generator() {}
var anonymous = function* () {};
var shorthand = { * generator() {} };
function *generator() {}
var anonymous = function *() {};
var shorthand = { *generator() {} };
=====================================output=====================================
// *incorrect* for the following standard.js rules:
// * "generator-star-spacing": [ "error", { "before": true, "after": true } ],
// * "space-before-function-paren": [ "error", "always" ],
// * "semi": [ "error", "never" ],
function * generator() {}
var anonymous = function * () {};
var shorthand = { * generator() {} };
function * generator() {}
var anonymous = function * () {};
var shorthand = { * generator() {} };
================================================================================
`;

exports[`incorrect.js 2`] = `
====================================options=====================================
generatorStarSpacing: true
parsers: ["babel", "flow", "typescript"]
printWidth: 80
spaceBeforeFunctionParen: true
| printWidth
=====================================input======================================
// *incorrect* for the following standard.js rules:
// * "generator-star-spacing": [ "error", { "before": true, "after": true } ],
// * "space-before-function-paren": [ "error", "always" ],
// * "semi": [ "error", "never" ],
function* generator() {}
var anonymous = function* () {};
var shorthand = { * generator() {} };
function *generator() {}
var anonymous = function *() {};
var shorthand = { *generator() {} };
=====================================output=====================================
// *incorrect* for the following standard.js rules:
// * "generator-star-spacing": [ "error", { "before": true, "after": true } ],
// * "space-before-function-paren": [ "error", "always" ],
// * "semi": [ "error", "never" ],
function * generator () {}
var anonymous = function * () {};
var shorthand = { * generator () {} };
function * generator () {}
var anonymous = function * () {};
var shorthand = { * generator () {} };
================================================================================
`;
12 changes: 12 additions & 0 deletions tests/generator-star-spacing/correct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// correct for:
// * "generator-star-spacing": [ "error", { "before": true, "after": true } ],
// but *not* for the following standard.js rules:
// * "space-before-function-paren": [ "error", "always" ],
// * "semi": [ "error", "never" ],

function * generator() {}
var anonymous = function * () {};
var shorthand = { * generator() {} };
class Example {
async * foo() {}
}
11 changes: 11 additions & 0 deletions tests/generator-star-spacing/incorrect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// *incorrect* for the following standard.js rules:
// * "generator-star-spacing": [ "error", { "before": true, "after": true } ],
// * "space-before-function-paren": [ "error", "always" ],
// * "semi": [ "error", "never" ],

function* generator() {}
var anonymous = function* () {};
var shorthand = { * generator() {} };
function *generator() {}
var anonymous = function *() {};
var shorthand = { *generator() {} };
7 changes: 7 additions & 0 deletions tests/generator-star-spacing/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
run_spec(__dirname, ["babel", "flow", "typescript"], {
generatorStarSpacing: true
});
run_spec(__dirname, ["babel", "flow", "typescript"], {
generatorStarSpacing: true,
spaceBeforeFunctionParen: true
});
4 changes: 4 additions & 0 deletions tests_integration/__tests__/__snapshots__/early-exit.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Format options:
--end-of-line <auto|lf|crlf|cr>
Which end of line characters to apply.
Defaults to auto.
--generator-star-spacing Add spaces around the star ('*') in generator functions (before and after - from eslint).
Defaults to false.
--html-whitespace-sensitivity <css|strict|ignore>
How to handle whitespaces in HTML.
Defaults to css.
Expand Down Expand Up @@ -217,6 +219,8 @@ Format options:
--end-of-line <auto|lf|crlf|cr>
Which end of line characters to apply.
Defaults to auto.
--generator-star-spacing Add spaces around the star ('*') in generator functions (before and after - from eslint).
Defaults to false.
--html-whitespace-sensitivity <css|strict|ignore>
How to handle whitespaces in HTML.
Defaults to css.
Expand Down
13 changes: 13 additions & 0 deletions tests_integration/__tests__/__snapshots__/help-options.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ exports[`show detailed usage with --help find-config-path (stdout) 1`] = `
exports[`show detailed usage with --help find-config-path (write) 1`] = `Array []`;
exports[`show detailed usage with --help generator-star-spacing (stderr) 1`] = `""`;
exports[`show detailed usage with --help generator-star-spacing (stdout) 1`] = `
"--generator-star-spacing
Add spaces around the star ('*') in generator functions (before and after - from eslint).
Default: false
"
`;
exports[`show detailed usage with --help generator-star-spacing (write) 1`] = `Array []`;
exports[`show detailed usage with --help help (stderr) 1`] = `""`;
exports[`show detailed usage with --help help (stdout) 1`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ exports[` 1`] = `
Defaults to auto.
+ --foo-option <bar|baz> foo description
+ Defaults to bar.
--generator-star-spacing Add spaces around the star ('*') in generator functions (before and after - from eslint).
Defaults to false.
--html-whitespace-sensitivity <css|strict|ignore>
How to handle whitespaces in HTML.
Defaults to css.
--jsx-bracket-same-line Put > on the last line instead of at a new line.
Defaults to false."
Defaults to css."
`;

exports[`show detailed external option with \`--help foo-option\` (stderr) 1`] = `""`;
Expand Down
5 changes: 5 additions & 0 deletions tests_integration/__tests__/__snapshots__/schema.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ This option cannot be used with --range-start and --range-end.",
"description": "Specify the input filepath. This will be used to do parser inference.",
"type": "string",
},
"generatorStarSpacing": Object {
"default": false,
"description": "Add spaces around the star ('*') in generator functions (before and after - from eslint).",
"type": "boolean",
},
"htmlWhitespaceSensitivity": Object {
"default": "css",
"description": "How to handle whitespaces in HTML.",
Expand Down
12 changes: 12 additions & 0 deletions tests_integration/__tests__/__snapshots__/support-info.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ Object {
"default": undefined,
"type": "path",
},
"generatorStarSpacing": Object {
"default": false,
"type": "boolean",
},
"htmlWhitespaceSensitivity": Object {
"choices": Array [
"css",
Expand Down Expand Up @@ -725,6 +729,14 @@ exports[`CLI --support-info (stdout) 1`] = `
\\"since\\": \\"1.4.0\\",
\\"type\\": \\"path\\"
},
{
\\"category\\": \\"JavaScript\\",
\\"default\\": false,
\\"description\\": \\"Add spaces around the star ('*') in generator functions (before and after - from eslint).\\",
\\"name\\": \\"generatorStarSpacing\\",
\\"pluginDefaults\\": {},
\\"type\\": \\"boolean\\"
},
{
\\"category\\": \\"HTML\\",
\\"choices\\": [
Expand Down

0 comments on commit 39b01ee

Please sign in to comment.