How can I reorder parameters with vscode?

Viewed 619

I have a simple function whose parameters are unintuitively ordered:

async blockUser(currentUser: User, userToBlock: User) { ... }

I would like to (using a proper refactoring tool rather than regexs) refactor the function, and all times the function is called, to reorder the parameters for the new function signature:

async blockUser(userToBlock: User, currentUser: User) { ... }

I'd have hoped vsCode would have this in the 'Refactor' menu but can't find it.

enter image description here

How can I reorder parameters with vscode?

2 Answers

You can try Shifter extension https://marketplace.visualstudio.com/items?itemName=Gruntfuggly.shifter

I'd suggest to use an object as a parameters of the function which will help you to avoid such cases.

interface User {
    name: string;
    age: number;
}

interface BlockUserParams {
    currentUser: User;
    userToBlock: User;
}

function blockUser({currentUser, userToBlock}: BlockUserParams) {
    console.log(currentUser, userToBlock);
}

In the absence of a proper, built-in method, you can always try a regex replacement like:
s/(blockUser)\(([^,]+), *([^,]+)\)/\1(\3, \2)/

Or, in the Find/Replace window:
Replace all (blockUser)\(([^,]+), *([^,]+)\) with $1($3, $2)

Related