Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

suggestion: option for variable-name rule to allow leading underscore only for unused params #3442

Closed
sethbrenith opened this issue Nov 2, 2017 · 11 comments

Comments

@sethbrenith
Copy link

I'm just promoting a comment by @mohsen1 in this discussion into its own issue for visibility.

Can variable-name know about unused params? Most of us don't want to allow leading underscore elsewhere.

const _varName = 1; 
//    ~~~~~~~~       <-- this still needs to be a lint error
function foo(_: string, __: number, bar: boolean) {
  console.log('only interested in', bar);
}
@ajafff
Copy link
Contributor

ajafff commented Nov 4, 2017

The latest version of the naming-convention rule in my package tslint-consistent-codestyle allows you to configure exactly what you want:
https://github.com/ajafff/tslint-consistent-codestyle/blob/master/docs/naming-convention.md#examples
The important part for you is

"naming-convention": [
  true,
  // forbid leading and trailing underscores and enforce camelCase on EVERY name. will be overridden by subtypes if needed
  {"type": "default", "format": "camelCase", "leadingUnderscore": "forbid", "trailingUnderscore": "forbid"},
  ...
  // allow leading underscores for unused parameters, because `tsc --noUnusedParameters` will not flag underscore prefixed parameters
  // all other rules (trailingUnderscore: forbid, format: camelCase) still apply
  {"type": "parameter", "modifiers": "unused", "leadingUnderscore": "allow"}
]

@sethbrenith
Copy link
Author

Excellent, that's just what we were looking for! Thanks!

@adidahiya
Copy link
Contributor

Thanks for the link @ajafff. I would like this functionality incorporated into the variable-name rule if possible, as an isolated enhancement separate from other features of the naming-convention rule.

@mgol
Copy link

mgol commented Jul 26, 2018

This is now more important as in TypeScript 2.9 no-unused-variable is deprecated and if you switch to the compiler noUnusedLocals & noUnusedParameters options, leading underscores are a hint to the compiler that those params should be allowed to be unused. I'm making such changes to my code base but now the variable-name rule complains...

EDIT: Hmm, actually, it's already supported, see https://palantir.github.io/tslint/rules/variable-name/. Should this issue be closed?

@sethbrenith
Copy link
Author

@mgol All I see at that link is "allow-leading-underscore". This suggestion was about allowing leading underscores only for unused variables (and still banning them elsewhere).

@mgol
Copy link

mgol commented Jul 27, 2018

@sethbrenith Ah, sorry, I misinterpreted your request.

@aryzing
Copy link

aryzing commented Dec 30, 2018

@ajafff Since you're most familiar, Would you mind giving this PR a go?

@JoshuaKGoldberg given your participation in #3558, not sure if you're actively looking into this?

@JoshuaKGoldberg
Copy link
Contributor

@aryzing no plans on my part to address this issue, but if you want to send a PR that'd be great! 😊

@pronebird
Copy link

Oh man it would be great to fix this. @ts-ignore-ing this is so messed up.

@JoshuaKGoldberg
Copy link
Contributor

☠️ TSLint's time has come! ☠️

TSLint is no longer accepting most feature requests per #4534. See typescript-eslint.io for the new, shiny way to lint your TypeScript code with ESLint. ✨

It was a pleasure open sourcing with you all!

@JoshuaKGoldberg
Copy link
Contributor

🤖 Beep boop! 👉 TSLint is deprecated 👈 (#4534) and you should switch to typescript-eslint! 🤖

🔒 This issue is being locked to prevent further unnecessary discussions. Thank you! 👋

@palantir palantir locked and limited conversation to collaborators Mar 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants