Tslint: Conflicting TypeScript TS6133 and tslint variable-name

Created on 7 Dec 2017  ·  5Comments  ·  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.8.0
  • __TypeScript version__: 2.6.1
  • __Running TSLint via__: CLI

TypeScript code being linted

export default async function errorhandler(ctx: Context, next: () => Promise<any>) {
  try {
    await next();
  } catch (err) {
    // some code here
  }
}

with tslint.json configuration:

{
  "extends": "tslint:recommended",
  "rules": {
    "arrow-parens": false,
    "indent": [true, "spaces", 2],
    "interface-name": [false],
    "member-access": [true, "no-public"],
    "no-require-imports": false,
    "no-switch-case-fall-through": true,
    "no-unused-variable": false,
    "quotemark": [true, "single"],
    "switch-default": true,
    "object-literal-sort-keys": false
  }
}

I was trying to configure vscode-tslint to show exact warnings for VS Code as tslint CLI does. It says:

Since tslint version 5 the rule no-unused-variable rule requires type information

So I changed no-unused-variable to false and also changed tsconfig.json noUnusedLocals and noUnusedParameters to true.

Actual behavior

With this configuration

  • TSC compilation fails with TS6133 error ('ctx' is declared but its value is never read)
  • tslint validation ok

If I change ctx to _ctx as some people advice.

  • TSC compilation ok
  • tslint validation fails with variable-name rule which expects _ctx to not start with underscore

It looks like there's no way to make tslint/tsc work properly without ignoring first line with tslint:ignore. Or maybe I'm wrong?

Duplicate Rule Enhancement

Most helpful comment

Well yes, but for me avoiding variable names like _ctx seems reasonable. From my perspective this seems more like a misleading behaviour of TS compiler. In my opinion it should not complain of first argument which is not used if second argument is used 🤷‍♂️

All 5 comments

"variable-name": { "options": { "check-format": ["allow-leading-underscore"] } }

https://palantir.github.io/tslint/rules/variable-name/

I do admit this is unintuitive and should probably be fixed in the recommended config for the next major version.

Well yes, but for me avoiding variable names like _ctx seems reasonable. From my perspective this seems more like a misleading behaviour of TS compiler. In my opinion it should not complain of first argument which is not used if second argument is used 🤷‍♂️

"variable-name": { "options": { "check-format": ["allow-leading-underscore"] } }

https://palantir.github.io/tslint/rules/variable-name/

I do admit this is unintuitive and should probably be fixed in the recommended config for the next major version.

Correct rule is now "variable-name": { "options": "allow-leading-underscore" },

I believe we should close this in favor of #3442. As mentioned in that issue, allow-leading-underscore does not differentiate between unused function arguments and other variable names that start with underscore.

Changing the recommended setting to allow all variables to start with underscore is a considerably larger change compared to only allowing underscores on function arguments.

Agreed, thanks for pointing that out @aryzing.

Was this page helpful?
0 / 5 - 0 ratings