ipatalas / vscode-postfix-ts

Postfix notation for TypeScript/Javascript - extension for VS Code
MIT License
158 stars 43 forks source link

.undefined should create a "typeof" check #27

Closed harrison-symes closed 5 years ago

harrison-symes commented 5 years ago

Current postfixing .undefined to the end of an entry fixes it to:

if (value === undefined) {

}

It is safer to check if (tpyeof value === "undefined") { instead.

The same goes for notundefined

Alternatively, create a new postfix for this specific typeof scenario

ipatalas commented 5 years ago

I'm not a big fan of that approach. Here is why: https://www.codereadability.com/how-to-check-for-undefined-in-javascript/

I'll think about introducing a setting that would toggle one or the other version. I really doubt anyone would use both versions in the same project.

In the meantime you can play around with custom templates to get this done:

"postfix.customTemplates": [
    {
      "name": "undef",
      "body": "if (typeof {{expr}} === 'undefined') {\n${0}\n}",
      "description": "if (typeof expr === 'undefined')",
      "when": [
        "identifier",
        "expression"
      ]
    }
  ]
ipatalas commented 5 years ago

image

Release should be ready quite soon.