0

I'm looking for a tool that could help me to do the following operation:

Given that code:

const before = (arg1: string, arg2: string) => null;

I would like to have the following result:

const after = ({ arg1, arg2 }: { arg1: string; arg2: string }) => null;

Is it possible to do that with a snippet ? (for exemple, i select my args and it directly transform them)

2 Answers 2

1

Define the following Typescript snippet

  "Convert Arguments": {
    "prefix": "conarg",
    "body": [
      "{ ${TM_SELECTED_TEXT/(\\w+):[^,]*(,\\s*)?/$1$2/g} }: { ${TM_SELECTED_TEXT/,/;/g}}"
    ],
    "description": "Convert Arguments"
  }
  • select the arguments between ()
  • type conarg and select the snippet
Sign up to request clarification or add additional context in comments.

Comments

0

As I generally need to execute a considerably big list of changes, I tend to use Tasks (Ctrl+Shift+P/Run Task/[task name])
https://code.visualstudio.com/docs/editor/tasks

        {
            "label": "vueconv",
            "type": "shell",
            "command": "tsx scripts/vueconv ${file}",
            "problemMatcher": [],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": true,
                "panel": "shared",
                "showReuseMessage": false,
                "clear": true
            }
        },

My scripts read the file, make fixes (mostly with string.replace(/regex/g, callback)), and overwrite the file
I only use ${file}, but if you can also add line number or selection as script arguments
https://code.visualstudio.com/docs/editor/variables-reference

self-made somple replacer library

1 Comment

your typescriptlang.org link results in an almost empty script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.