A prettier plugin to add a space before function parentheses for function definitions (but not function calls) in JS and TS. Requires Prettier 3.0.0 or later.
npm install -D prettier prettier-plugin-space-before-function-paren
There are no config options for this plugin. All you need to do is actually include it in your Prettier config:
{
"plugins": ["prettier-plugin-space-before-function-paren"]
}Important
Due to Prettier limitations, to use this plugin with other plugins formatting JS/TS code, install the prettier-plugin-merge plugin and add it to the end of your plugins array. This plugin will be used last and preserve changes made by the previous plugins.
function test(a, b) {
return a + b;
}becomes:
function test (a, b) {
return a + b;
}async function test(a, b) {
return a + b;
}becomes:
async function test (a, b) {
return a + b;
}const foo = {
method(a, b) {
return a + b;
}
};becomes:
const foo = {
method (a, b) {
return a + b;
}
};const foo = {
async method(a, b) {
return a + b;
}
};becomes:
const foo = {
async method (a, b) {
return a + b;
}
};const foo = {
[methodName]() {
return 42;
}
};becomes:
const foo = {
[methodName] () {
return 42;
}
};const foo = {
get foo() {
return true;
}
};becomes:
const foo = {
get foo () {
return true;
}
};const foo = {
get [name]() {
return this._name;
}
};becomes:
const foo = {
get [name] () {
return this._name;
}
};const foo = {
set foo(value) {
this._foo = value;
}
};becomes:
const foo = {
set foo (value) {
this._foo = value;
}
};const foo = {
set [name](value) {
this._name = value;
}
};becomes:
const foo = {
set [name] (value) {
this._name = value;
}
};class Foo {
constructor(a, b) {
this.a = a;
this.b = b;
}
}becomes:
class Foo {
constructor (a, b) {
this.a = a;
this.b = b;
}
}class Foo {
method(a, b) {
return a + b;
}
}becomes:
class Foo {
method (a, b) {
return a + b;
}
}class Foo {
#method(a, b) {
return a + b;
}
}becomes:
class Foo {
#method (a, b) {
return a + b;
}
}class Foo {
async method(a, b) {
return a + b;
}
}becomes:
class Foo {
async method (a, b) {
return a + b;
}
}class Foo {
async #method(a, b) {
return a + b;
}
}becomes:
class Foo {
async #method (a, b) {
return a + b;
}
}class Foo {
[methodName]() {
return 42;
}
}becomes:
class Foo {
[methodName] () {
return 42;
}
}class Foo {
static method(a, b) {
return a + b;
}
}becomes:
class Foo {
static method (a, b) {
return a + b;
}
}class Foo {
static #method(a, b) {
return a + b;
}
}becomes:
class Foo {
static #method (a, b) {
return a + b;
}
}class Foo {
get foo() {
return true;
}
}becomes:
class Foo {
get foo () {
return true;
}
}class Foo {
get #foo() {
return true;
}
}becomes:
class Foo {
get #foo () {
return true;
}
}class Foo {
set foo(value) {
this._foo = value;
}
}becomes:
class Foo {
set foo (value) {
this._foo = value;
}
}class Foo {
set #foo(value) {
this._foo = value;
}
}becomes:
class Foo {
set #foo (value) {
this._foo = value;
}
}function* test() {
yield 1;
}becomes:
function* test () {
yield 1;
}const foo = {
*method() {
yield 1;
}
};becomes:
const foo = {
*method () {
yield 1;
}
};class Foo {
method = function named() {};
}becomes:
class Foo {
method = function named () {};
}export function test(a, b) {
return a + b;
}becomes:
export function test (a, b) {
return a + b;
}export default function test(a, b) {
return a + b;
}becomes:
export default function test (a, b) {
return a + b;
}export async function test(a, b) {
return a + b;
}becomes:
export async function test (a, b) {
return a + b;
}export function* test() {
yield 1;
}becomes:
export function* test () {
yield 1;
}test(1, 2);becomes:
test(1, 2);const add = (a, b) => a + b;becomes:
const add = (a, b) => a + b;Prettier already handles this case.
const add = function(a, b) {
return a + b;
};becomes:
const add = function (a, b) {
return a + b;
};Prettier already handles this case.
export default function(a, b) {
return a + b;
}becomes:
export default function (a, b) {
return a + b;
}function test(a: number, b: number): number {
return a + b;
}becomes:
function test (a: number, b: number): number {
return a + b;
}async function test(a: number, b: number): Promise<number> {
return a + b;
}becomes:
async function test (a: number, b: number): Promise<number> {
return a + b;
}const foo = {
method(a: number, b: number): number {
return a + b;
}
};becomes:
const foo = {
method (a: number, b: number): number {
return a + b;
}
};const foo = {
async method(a: number, b: number): Promise<number> {
return a + b;
}
};becomes:
const foo = {
async method (a: number, b: number): Promise<number> {
return a + b;
}
};const foo = {
[methodName](): void {
return;
}
};becomes:
const foo = {
[methodName] (): void {
return;
}
};const foo = {
get foo(): boolean {
return true;
}
};becomes:
const foo = {
get foo (): boolean {
return true;
}
};const foo = {
get [name](): string {
return '';
}
};becomes:
const foo = {
get [name] (): string {
return "";
}
};const foo = {
set foo(value: boolean) {
this._foo = value;
}
};becomes:
const foo = {
set foo (value: boolean) {
this._foo = value;
}
};const foo = {
set [name](value: string) {
this._name = value;
}
};becomes:
const foo = {
set [name] (value: string) {
this._name = value;
}
};class Foo {
constructor(a: number, b: number) {
this.a = a;
this.b = b;
}
}becomes:
class Foo {
constructor (a: number, b: number) {
this.a = a;
this.b = b;
}
}class Foo {
method(a: number, b: number): number {
return a + b;
}
}becomes:
class Foo {
method (a: number, b: number): number {
return a + b;
}
}class Foo {
async method(a: number, b: number): Promise<number> {
return a + b;
}
}becomes:
class Foo {
async method (a: number, b: number): Promise<number> {
return a + b;
}
}class Foo {
static method(a: number, b: number): number {
return a + b;
}
}becomes:
class Foo {
static method (a: number, b: number): number {
return a + b;
}
}abstract class Foo {
abstract method(a: number, b: number): number;
}becomes:
abstract class Foo {
abstract method (a: number, b: number): number;
}class Foo {
private method(a: number, b: number): number {
return a + b;
}
}becomes:
class Foo {
private method (a: number, b: number): number {
return a + b;
}
}class Foo {
#method(a: number, b: number): number {
return a + b;
}
}becomes:
class Foo {
#method (a: number, b: number): number {
return a + b;
}
}class Foo {
protected method(a: number, b: number): number {
return a + b;
}
}becomes:
class Foo {
protected method (a: number, b: number): number {
return a + b;
}
}class Foo {
public method(a: number, b: number): number {
return a + b;
}
}becomes:
class Foo {
public method (a: number, b: number): number {
return a + b;
}
}class Foo {
method?(a: number, b: number): number {
return a + b;
}
}becomes:
class Foo {
method? (a: number, b: number): number {
return a + b;
}
}class Foo {
get foo(): boolean {
return true;
}
}becomes:
class Foo {
get foo (): boolean {
return true;
}
}class Foo {
get #foo(): boolean {
return true;
}
}becomes:
class Foo {
get #foo (): boolean {
return true;
}
}class Foo {
set foo(value: boolean) {
this._foo = value;
}
}becomes:
class Foo {
set foo (value: boolean) {
this._foo = value;
}
}class Foo {
set #foo(value: boolean) {
this._foo = value;
}
}becomes:
class Foo {
set #foo (value: boolean) {
this._foo = value;
}
}class Foo {
[methodName](): void {
return;
}
}becomes:
class Foo {
[methodName] (): void {
return;
}
}class Foo {
method<T>(a: T): T {
return a;
}
}becomes:
class Foo {
method<T> (a: T): T {
return a;
}
}function* test(): Generator<number> {
yield 1;
}becomes:
function* test (): Generator<number> {
yield 1;
}interface Foo {
method(a: number, b: number): number;
}becomes:
interface Foo {
method (a: number, b: number): number;
}interface Foo {
method?(a: number): number;
}becomes:
interface Foo {
method? (a: number): number;
}function foo<T>(arg: T): T {
return arg;
}becomes:
function foo<T> (arg: T): T {
return arg;
}export function test(a: number, b: number): number {
return a + b;
}becomes:
export function test (a: number, b: number): number {
return a + b;
}export default function test(a: number, b: number): number {
return a + b;
}becomes:
export default function test (a: number, b: number): number {
return a + b;
}export async function test(a: number, b: number): Promise<number> {
return a + b;
}becomes:
export async function test (a: number, b: number): Promise<number> {
return a + b;
}export function* test(): Generator<number> {
yield 1;
}becomes:
export function* test (): Generator<number> {
yield 1;
}declare namespace Foo {
export function test(a: number, b: number): number;
}becomes:
declare namespace Foo {
export function test (a: number, b: number): number;
}declare function foo(a: number): void;becomes:
declare function foo (a: number): void;type MethodType = {
method(): void;
};becomes:
type MethodType = {
method (): void;
};class Foo {
@decorator(() => {})
method(): void {}
}becomes:
class Foo {
@decorator(() => {})
method (): void {}
}test<number>(1, 2);becomes:
test<number>(1, 2);const add = (a: number, b: number): number => a + b;becomes:
const add = (a: number, b: number): number => a + b;Prettier already handles this case.
const add = function(a: number, b: number): number {
return a + b;
};becomes:
const add = function (a: number, b: number): number {
return a + b;
};Prettier already handles this case.
export default function(a: number, b: number): number {
return a + b;
}becomes:
export default function (a: number, b: number): number {
return a + b;
}type NumberCallback = (x: number) => number;becomes:
type NumberCallback = (x: number) => number;type Func = { (a: string): number };becomes:
type Func = { (a: string): number };interface Foo {
method: (a: number) => number;
}becomes:
interface Foo {
method: (a: number) => number;
}Prettier already handles this case.
type Ctor = {
new(): Foo;
};becomes:
type Ctor = {
new (): Foo;
};interface Foo {
[key: string]: number;
}becomes:
interface Foo {
[key: string]: number;
}let a = { value: null, prev: null, next: null as never };becomes:
let a = { value: null, prev: null, next: null as never };Current version is a proof of concept, please try it out and give feedback!