Tslint: Ban property

Created on 19 May 2017  路  4Comments  路  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.2.0
  • __TypeScript version__: 2.4.0-dev.20170519
  • __Running TSLint via__: CLI

TypeScript code being linted

[-1, 2, -3].map(Math.abs);

with tslint.json configuration:

{
    "rules": {
        "ban": [
            true,
            ["Math", "abs"]
        ]
    }
}

Actual behavior

No error.

Expected behavior

A lint failure.

This would be a breaking change, but it makes sense to me. Maybe put it under an option until 6.0?

Aged Away Breaking Change Bug 馃尮 R.I.P. 馃尮

Most helpful comment

Another use case - I want to ban usage of length and name on the DOM window global. It's a rare gotcha but it's gotten me multiple times after refactoring.

console.log('surprise, these are defined in lib.dom.d.ts', length, name);

The following TypeScript-based solutions don't work: (because Subsequent variable declarations must have the same type.)

declare var length: never;
// nor
interface Window {
  length: never;
}

Reference in TypeScript issues - https://github.com/Microsoft/TypeScript/issues/14306

All 4 comments

How come ban is restricted to call expressions? Can we just remove this line?
https://github.com/palantir/tslint/blob/master/src/rules/banRule.ts#L149

Another use case - I want to ban usage of length and name on the DOM window global. It's a rare gotcha but it's gotten me multiple times after refactoring.

console.log('surprise, these are defined in lib.dom.d.ts', length, name);

The following TypeScript-based solutions don't work: (because Subsequent variable declarations must have the same type.)

declare var length: never;
// nor
interface Window {
  length: never;
}

Reference in TypeScript issues - https://github.com/Microsoft/TypeScript/issues/14306

@ryanatkn I believe your case would be covered by #3824.

I'm looking for banning those global variables, but also usages of Function.length. Could this rule be expanded to cover property accesses too?

~Removing~ Ignoring the Type: Breaking Change label per #4811. Now accepting PRs!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

denkomanceski picture denkomanceski  路  3Comments

cateyes99 picture cateyes99  路  3Comments

ghost picture ghost  路  3Comments

avanderhoorn picture avanderhoorn  路  3Comments