TSLint : variable name must be in camelcase or uppercase
Asked Answered
O

3

50

I have some variable names starting with leading underscore , I still get this warning after updating my tslint.json

tslint.json

{
  "extends": "tslint:recommended",
  "rules": {
    "variable-name": [
      true,
      "ban-keywords",
      "check-format",
      "allow-leading-underscore"
    ]
  },
  "exclude": [
    "build/**/*",
    "node_modules/**/*",
    "tmp/**/*"
  ]
}

where am I wrong ?

thanks for feedback

UPDATE

I am using version 4.5.1 of TSLint

Outdated answered 29/3, 2017 at 17:0 Comment(4)
Looks right to me at a glance, what version of TSLint are you using?Patrickpatrilateral
I am using currently version 4.5.1.Outdated
Can you verify that TSLint is indeed using that config file? See if other changes to it affect things?Patrickpatrilateral
You right ... using tslint --project '.' --config ./tslint.json it works now ... I don't understand why tslint was not using it when not specified... ( maybe weird behaviour w Webstorm IDE.. I'm using it..)Outdated
P
86

You can solve the problem by editing your tslint.json and adding "allow-leading-underscore" to the "variable-name" array of your "rules".

// tslint.json contents
{
  // ...
  "rules": {
    // ...
    "variable-name": [
      true,
      // ...
      "allow-leading-underscore"
    ]
  },
  // ...
}
Predecessor answered 14/7, 2017 at 4:46 Comment(0)
N
23

I have updated tslint.json, configured the file and added optional arguments to the array of variable-name.

"allow-leading-underscore" allows underscores at the beginning (only has an effect if “check-format” specified)

"allow-pascal-case" allows PascalCase in addition to lowerCamelCase.

"allow-snake-case" allows snake_case in addition to lowerCamelCase.

"allow-trailing-underscore" allows underscores at the end. (only has an effect if “check-format” specified)

{
  // ...
  "rules": {
    "variable-name": [
      true,
      "allow-leading-underscore"
    ],
  },
  // ...
}

You can configure tslint.json according to your requirements.

This link might be helpful. https://palantir.github.io/tslint/rules/variable-name/

Newtonnext answered 21/1, 2020 at 12:25 Comment(1)
When answering an old question, your answer would be much more useful to other StackOverflow users if you included some context to explain how your answer helps. See: How do I write a good answer.Lilylilyan
G
3

You can solve the problem by editing your tslint.json and adding all this properties to the "variable-name" :

"variable-name": {
  "options": [
    "ban-keywords",
    "check-format",
    "allow-pascal-case",
    "allow-leading-underscore",
    "allow-snake-case",
    "allow-trailing-underscore",
    "require-const-for-all-caps"
  ]
}
Gwendolin answered 8/11, 2020 at 18:37 Comment(1)
Thanks for the more complete example.Jewell

© 2022 - 2024 — McMap. All rights reserved.