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

Always add a space after the function keyword #3903

Merged
merged 9 commits into from
Jan 20, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions changelog_unreleased/javascript/pr-3903.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#### Always add a space after the `function` keyword ([#3903](https://github.com/prettier/prettier/pull/3903) by [@j-f1](https://github.com/j-f1), [@josephfrazier](https://github.com/josephfrazier), [@sosukesuzuki](https://github.com/sosukesuzuki), [@thorn0](https://github.com/thorn0))

Previously, a space would be added after the `function` keyword in function declarations, but not in function expressions. Now, for consistency, a space is always added after the `function` keyword, including in generators. Also, a space is now added between `yield` and `*`, which can help with [catching bugs](https://github.com/prettier/prettier/issues/7028#user-content-bugs).

<!-- prettier-ignore -->
```js
// Input
const identity = function (value) {
return value;
};
function identity (value) {
return value;
}
function * getRawText() {
for (const token of tokens) {
yield* token.getRawText();
}
}
const f = function<T>(value: T) {}

// Prettier stable
const identity = function(value) {
return value;
};
function identity(value) {
return value;
}
function* getRawText() {
for (const token of tokens) {
yield* token.getRawText();
}
}
const f = function<T>(value: T) {};

// Prettier master
const identity = function (value) {
return value;
};
function identity(value) {
return value;
}
function *getRawText() {
for (const token of tokens) {
yield *token.getRawText();
}
}
const f = function <T>(value: T) {};
```
10 changes: 7 additions & 3 deletions src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -917,11 +917,14 @@ function printPathNoParens(path, options, print, args) {
case "YieldExpression":
parts.push("yield");

if (n.delegate || n.argument) {
parts.push(" ");
}
if (n.delegate) {
parts.push("*");
}
if (n.argument) {
parts.push(" ", path.call(print, "argument"));
parts.push(path.call(print, "argument"));
}

return concat(parts);
Expand Down Expand Up @@ -4390,13 +4393,14 @@ function printFunctionDeclaration(path, print, options) {
parts.push("async ");
}

parts.push("function");
parts.push("function ");

if (n.generator) {
parts.push("*");
}

if (n.id) {
parts.push(" ", path.call(print, "id"));
parts.push(path.call(print, "id"));
}

parts.push(
Expand Down
12 changes: 6 additions & 6 deletions tests/async/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class X {
}

=====================================output=====================================
async function* a() {
yield* b();
async function *a() {
yield *b();
}

class X {
async *b() {
yield* a();
yield *a();
}
}

Expand Down Expand Up @@ -61,7 +61,7 @@ async function f1() {
async function g() {
invariant((await driver.navigator.getUrl()).substr(-7));
}
function* f2() {
function *f2() {
!(yield a);
}
async function f3() {
Expand Down Expand Up @@ -101,7 +101,7 @@ async function f() {
const result = typeof fn === "function" ? await fn() : null;
}

(async function() {
(async function () {
console.log(await (true ? Promise.resolve("A") : Promise.resolve("B")));
})();

Expand Down Expand Up @@ -148,7 +148,7 @@ async function *f(){ await (yield x); }
async function f2(){ await (() => {}); }

=====================================output=====================================
async function* f() {
async function *f() {
await (yield x);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/class_extends/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class a5 extends class {} {}
class a6 extends (b ? c : d) {}

// "FunctionExpression"
class a7 extends function() {} {}
class a7 extends function () {} {}

// "LogicalExpression"
class a8 extends (b || c) {}
Expand All @@ -116,7 +116,7 @@ class a14 extends (void b) {}
class a15 extends (++b) {}

// "YieldExpression"
function* f2() {
function *f2() {
// Flow has a bug parsing it.
// class a extends (yield 1) {}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/comments/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2802,7 +2802,7 @@ f3 = (
// TODO this is a very very very very long comment that makes it go > 80 columns
) => {};

f4 = function(
f4 = function (
currentRequest: { a: number }
// TODO this is a very very very very long comment that makes it go > 80 columns
) {};
Expand Down Expand Up @@ -2995,7 +2995,7 @@ f3 = (
// TODO this is a very very very very long comment that makes it go > 80 columns
) => {}

f4 = function(
f4 = function (
currentRequest: { a: number }
// TODO this is a very very very very long comment that makes it go > 80 columns
) {}
Expand Down
8 changes: 4 additions & 4 deletions tests/conditional/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ const { configureStore } = process.env.NODE_ENV === "production"
var inspect =
4 === util.inspect.length
? // node <= 0.8.x
function(v, colors) {
function (v, colors) {
return util.inspect(v, void 0, void 0, colors);
}
: // node > 0.8.x
function(v, colors) {
function (v, colors) {
return util.inspect(v, { colors: colors });
};

var inspect =
4 === util.inspect.length
? // node <= 0.8.x
function(v, colors) {
function (v, colors) {
return util.inspect(v, void 0, void 0, colors);
}
: // node > 0.8.x
function(v, colors) {
function (v, colors) {
return util.inspect(v, { colors: colors });
};

Expand Down
4 changes: 2 additions & 2 deletions tests/cursor/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ printWidth: 80
(function() {return <|> 15})()

=====================================output=====================================
(function() {
(function () {
return <|>15;
})();

Expand All @@ -102,7 +102,7 @@ printWidth: 80
(function(){return <|>15})()

=====================================output=====================================
(function() {
(function () {
return <|>15;
})();

Expand Down
2 changes: 1 addition & 1 deletion tests/destructuring/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const {

function f({ data: { name } }) {}

const UserComponent = function({
const UserComponent = function ({
name: { first, last },
organisation: {
address: { street: orgStreetAddress, postcode: orgPostcode },
Expand Down
6 changes: 3 additions & 3 deletions tests/empty_paren_comment/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ let f4 = () => doThing(a, /* ... */ b);

=====================================output=====================================
let f1 = (/* ... */) => {};
(function(/* ... */) {})(/* ... */);
(function (/* ... */) {})(/* ... */);
function f2(/* ... */) {}

const obj = {
f(/* ... */) {},
f: (/* ... */) => {},
f: function(/* ... */) {},
f: function (/* ... */) {},
f: function f(/* ... */) {},
};

Expand All @@ -78,7 +78,7 @@ class Foo {
f = (/* ... */) => {};
static f(/* ... */) {}
static f = (/* ... */) => {};
static f = function(/* ... */) {};
static f = function (/* ... */) {};
static f = function f(/* ... */) {};
}

Expand Down
4 changes: 2 additions & 2 deletions tests/es6modules/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ printWidth: 80
export default function() {}

=====================================output=====================================
export default function() {}
export default function () {}

================================================================================
`;
Expand Down Expand Up @@ -107,7 +107,7 @@ printWidth: 80
export default (function() {});

=====================================output=====================================
export default (function() {});
export default (function () {});

================================================================================
`;
Expand Down
4 changes: 2 additions & 2 deletions tests/export_default/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ printWidth: 80
export default (function() {} + foo)\`\`;

=====================================output=====================================
export default (function() {} + foo)\`\`;
export default (function () {} + foo)\`\`;

================================================================================
`;
Expand Down Expand Up @@ -65,7 +65,7 @@ printWidth: 80
export default (function() {}).toString();

=====================================output=====================================
export default (function() {}.toString());
export default (function () {}.toString());

================================================================================
`;
Expand Down
18 changes: 9 additions & 9 deletions tests/first_argument_expansion/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,19 @@ func((args) => {
}, result => result && console.log("success"))

=====================================output=====================================
setTimeout(function() {
setTimeout(function () {
thing();
}, 500);

["a", "b", "c"].reduce(function(item, thing) {
["a", "b", "c"].reduce(function (item, thing) {
return thing + " " + item;
}, "letters:");

func(() => {
thing();
}, identifier);

func(function() {
func(function () {
thing();
}, this.props.timeout * 1000);

Expand Down Expand Up @@ -165,7 +165,7 @@ func(
);

func(
function() {
function () {
return thing();
},
1 ? 2 : 3
Expand Down Expand Up @@ -208,11 +208,11 @@ compose(
b => b * b
);

somthing.reduce(function(item, thing) {
somthing.reduce(function (item, thing) {
return (thing.blah = item);
}, {});

somthing.reduce(function(item, thing) {
somthing.reduce(function (item, thing) {
return thing.push(item);
}, []);

Expand All @@ -226,7 +226,7 @@ reallyLongLongLongLongLongLongLongLongLongLongLongLongLongLongMethod(
// Don't do the rest of these

func(
function() {
function () {
thing();
},
true,
Expand Down Expand Up @@ -263,14 +263,14 @@ renderThing(

setTimeout(
// Something
function() {
function () {
thing();
},
500
);

setTimeout(
/* blip */ function() {
/* blip */ function () {
thing();
},
500
Expand Down
4 changes: 2 additions & 2 deletions tests/flow/annot/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function foo(str: string, i: number): string {
}
var bar: (str: number, i: number) => string = foo;

var qux = function(str: string, i: number): number {
var qux = function (str: string, i: number): number {
return foo(str, i);
};

Expand Down Expand Up @@ -334,7 +334,7 @@ function insertMany<K, V>(

class Foo<A> {
bar<B>() {
return function<C>(a: A, b: B, c: C): void {
return function <C>(a: A, b: B, c: C): void {
([a, b, c]: [A, B, C]);
};
}
Expand Down