'jsdoc to auto use description field

I have an object like this:

resource.foo= {
  name: "Foo",
  desc: "Some description"
}

I find myself writing:

/**
 * Some description
 */
resource.foo= {
  name: "Foo",
  desc: "Some description"
}

Anyway to get the desc field automatically without copying and pasting?



Solution 1:[1]

Here is another way to do it, with a keybonding. Using an extension I wrote, Find and Transform, put this into your keybindings.json:

{
  "key": "alt+d",                   // whatever keybinding you want
  "command": "findInCurrentFile",
  "args": {
    "preCommands": [
      "editor.action.jumpToBracket",
      "editor.action.selectToBracket",
      "editor.action.clipboardCopyAction",
      "editor.action.insertLineBefore"
    ],
    "replace": [
      "$${",
        "return `/**\n * ` + ${CLIPBOARD}.desc  + `\n */`",
      "}$$",
    ],
    "postCommands": "cancelSelection"
  }
}

You can run javascript in the replace, and yours is pretty easy:

${CLIPBOARD}.desc since the clipboard will be the object.

jsdoc with description from object

Note the cursor must be somewhere inside the object - that is, within the brackets/braces.

Solution 2:[2]

Sounds impossible to me. But if all you need is to show the description in VS Code's hints, here is the closest possible solution I could come up with:

const resource = {}

const desc = 'Some description'

resource.foo= {
  name: "Foo",
  /** @type {desc} */ desc,
}

First declare desc as a constant, and you'll be able to reference the constant type. Now you should be able to see the description shows in VS Code's type hints:

enter image description here

When you are trying to type resource.foo.desc you'll also see the description:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 CodinCat