Tslint: Ignore a line with a single comment

Created on 2 Jun 2014  ·  31Comments  ·  Source: palantir/tslint

Is there a way to ignore a single line, like in jshint?
I have tried // tslint ignore:line but that does not seem to work.

P2 Fixed Accepting PRs Feature Request

Most helpful comment

For anyone here to find the a workaround solution

/* tslint:disable */ - Disable all rules for the rest of the file
/* tslint:enable */ - Enable all rules for the rest of the file

e.g.
/* tslint:disable */ window['foo']= window['bar']; /* tslint:enable */

All 31 comments

unfortunately, we don't have such a thing yet. you have to enable & disable the line explictly.

How do you disable a line?

For anyone here to find the a workaround solution

/* tslint:disable */ - Disable all rules for the rest of the file
/* tslint:enable */ - Enable all rules for the rest of the file

e.g.
/* tslint:disable */ window['foo']= window['bar']; /* tslint:enable */

has there been any investigation into implementing a feature similar as described above?

@patsissons what feature exactly? It is currently possible to enable/disable a single line with two comments.

the feature would be less verbose line disabling. ESLint has multiple methods of doing single line rule disabes.

For example,

// eslint-disable-next-line rule-name
var x; // eslint-disable-line rule1 rule2

The wrapping method is great as a redundancy for complex rule disables, but inline (or next-line) tends to leave the code less polluted (this is my opinion of course, but probably not that unreasonable).

This is noisy:

/* tslint:disable */ window['foo']= window['bar']; /* tslint:enable */

This is much better:

var x; // eslint-disable-line rule1 rule2

Or even just:

var x; // tslint:disable

The fact that it's a line comment would imply that it would only disable that one line just fine I think.

Or at worst:

var x; // tslint:disable-line

accepting PRs for this. here's a recent somewhat related change that allowed // instead of /* comments for disabling (two comments are still required though): https://github.com/palantir/tslint/pull/1134

I will try and get to this tonight, if it's not too challenging hopefully a PR tonight too

👍 Let's follow closely to ESLint here:

someCode(); // tslint:disable-line

// tslint:disable-next-line
someCode();

and

someCode(); // tslint:disable-line:rule1 rule2

// tslint:disable-next-line:rule1
someCode();

That's precisely what I would like to achieve :+1:

though, just for clarity, ESLint does not use colons, their style is the following:

// eslint-disable-next-line rule1, rule2
someCode();

I will assume that you would prefer we stick to the TSLint style that you included above (colons and no commas separating rules).

👍 Let's stick with the TSLint style for consistency

I have this working now and I am just going through some commit cleanup before i create the PR. The strategy was to covert the -line and -next-line variants into their equivalent fully expressed comment switches. This is done by tracking the line start position (for -line switches) and performing a look-ahead for end of the following line (for -next-line switches). These seems to work quite well and has minimal side effects since it is simply acting as an alias for the more verbose format.

Just a note before I get my PR in order, due to the way the disabled interval code works, you won't be able to do something like this

// tslint:disable-next-line:quotemark variable-name
var AAAaA = 'test' // tslint:enable-line variable-name

I honestly never expect someone to do something like that, but I just wanted to note it here. You can still do standard nesting like this

// tslint:disable
var AAAaA = 'test' // tslint:enable-line:quotemark
// tslint:enable-next-line:variable-name
var AAAaA = 'test'
// tslint:enable

Question about the implementation, does this work?

/**
 * this is a very long line and violate max-line-length. // tslint:disable-line:max-line-length
 */

No, the switch comments are not parsed like that, they must be formatted correctly. I believe in your example, you would place the single line disable comment outside the multi-line comment and that ought to work as expected.

Thanks for clarification, got it.

@lijunle just revisiting your example, I think my explanation was actually somewhat incorrect. I don't think you could achieve your intended result with single line disables. I believe the parser would not be able to reach back to properly disable the whole multi-line comment. to disable a rule in a multi-line comment you would need to wrap the comment with a switch pair.

none of the above works
are you people talking about features proposition or already implemented ones ?
hard to follow

@phil123456 https://github.com/palantir/tslint#rule-flags

// tslint:disable-next-line <optional rule identifier>
var foo = 123;

Bloody hell. Neither the words "suppress" nor "ignore" appear in that documentation. I can't believe how much googling it took to lead me to this issue, read all the stuff above, only to find it's a supported, documented feature, but the appropriate keywords aren't in the docs so it's impossible to Google. Do you have to clone the whole repo and submit a PR for that, or is there a quicker way to suggest fixes to .md files?

@pbarranis you can make a new ticket with that as a feature request. If they don't make the change then making a PR might be the next step.

By the way, you can also do it on the same line:

console.log("poop"); // tslint:disable-line no-console

At least as of [email protected].

This doesn't work with rules like ordered-imports

//@ts-ignore
https://palantir.github.io/tslint/rules/

unfortunately this page only shows how to ban usage of this. It doesnt show how to use it properly or explain how much tsLint ignores when you use that. Dont know why it's not mentioned on this page which explains how to suppress rules https://palantir.github.io/tslint/usage/rule-flags/
And I agree with the comment above that asked WHY doesnt this page mention the words "suppress" or "ignore" so it can be easily found when using Google?
FYI this page now says TSLint is being EOL this year : https://github.com/palantir/tslint#tslint-rule-flags

Ah, tslint is eventually (not yet) getting merged in to eslint, so that's nice I suppose.

The point is, now tslint supports the ignore next line feature.

Example:

// tslint:disable-next-line

Or

// tslint:disable-next-line:rule1 rule2 rule3

For more detail & example see https://palantir.github.io/tslint/usage/rule-flags/

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

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

Was this page helpful?
0 / 5 - 0 ratings