Skip to content

Commit

Permalink
feat(eslint-plugin): add ban-ts-ignore rule (#276)
Browse files Browse the repository at this point in the history
* feat(eslint-plugin): add ban-ts-ignore rule

* test(eslint-plugin): add more tests
  • Loading branch information
ldrick authored and bradzacher committed Feb 14, 2019
1 parent df8ed1f commit 859ab29
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Then you should add `airbnb` (or `airbnb-base`) to your `extends` section of `.e
| [`@typescript-eslint/adjacent-overload-signatures`](./docs/rules/adjacent-overload-signatures.md) | Require that member overloads be consecutive (`adjacent-overload-signatures` from TSLint) | :heavy_check_mark: | |
| [`@typescript-eslint/array-type`](./docs/rules/array-type.md) | Requires using either `T[]` or `Array<T>` for arrays (`array-type` from TSLint) | :heavy_check_mark: | :wrench: |
| [`@typescript-eslint/ban-types`](./docs/rules/ban-types.md) | Enforces that types will not to be used (`ban-types` from TSLint) | :heavy_check_mark: | :wrench: |
| [`@typescript-eslint/ban-ts-ignore`](./docs/rules/ban-ts-ignore.md) | Bans “// @ts-ignore” comments from being used (`ban-ts-ignore` from TSLint) | :heavy_check_mark: | |
| [`@typescript-eslint/camelcase`](./docs/rules/camelcase.md) | Enforce camelCase naming convention | :heavy_check_mark: | |
| [`@typescript-eslint/class-name-casing`](./docs/rules/class-name-casing.md) | Require PascalCased class and interface names (`class-name` from TSLint) | :heavy_check_mark: | |
| [`@typescript-eslint/explicit-function-return-type`](./docs/rules/explicit-function-return-type.md) | Require explicit return types on functions and class methods | :heavy_check_mark: | |
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
| TSLint rule | | ESLint rule |
| --------------------------------- | :-: | ---------------------------------------------------- |
| [`adjacent-overload-signatures`] || [`@typescript-eslint/adjacent-overload-signatures`] |
| [`ban-ts-ignore`] | 🛑 | N/A |
| [`ban-ts-ignore`] | | [`@typescript-eslint/ban-ts-ignore`] |
| [`ban-types`] || [`@typescript-eslint/ban-types`] |
| [`member-access`] || [`@typescript-eslint/explicit-member-accessibility`] |
| [`member-ordering`] || [`@typescript-eslint/member-ordering`] |
Expand Down Expand Up @@ -575,6 +575,7 @@ Relevant plugins: [`chai-expect-keywords`](https://github.com/gavinaiken/eslint-

[`@typescript-eslint/adjacent-overload-signatures`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/adjacent-overload-signatures.md
[`@typescript-eslint/ban-types`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/ban-types.md
[`@typescript-eslint/ban-ts-ignore`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/ban-ts-ignore.md
[`@typescript-eslint/explicit-member-accessibility`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md
[`@typescript-eslint/member-ordering`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/member-ordering.md
[`@typescript-eslint/no-explicit-any`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-explicit-any.md
Expand Down
37 changes: 37 additions & 0 deletions packages/eslint-plugin/docs/rules/ban-ts-ignore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Bans “// @ts-ignore” comments from being used (ban-ts-ignore)

Suppressing Typescript Compiler Errors can be hard to discover.

## Rule Details

Does not allow the use of `// @ts-ignore` comments.

The following patterns are considered warnings:

```ts
if (false) {
// @ts-ignore: Unreachable code error
console.log('hello');
}
```

The following patterns are not warnings:

```ts
if (false) {
// Compiler warns about unreachable code error
console.log('hello');
}
```

## When Not To Use It

If you are sure, compiler errors won't affect functionality and you need to disable them.

## Further Reading

- TypeScript [Type Checking JavaScript Files](https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html)

## Compatibility

- TSLint: [ban-ts-ignore](https://palantir.github.io/tslint/rules/ban-ts-ignore/)
47 changes: 47 additions & 0 deletions packages/eslint-plugin/src/rules/ban-ts-ignore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @fileoverview Bans “// @ts-ignore” comments from being used.
* @author Ricky Lippmann <https://github.com/ldrick>
*/

import * as util from '../util';

export default util.createRule({
name: 'ban-ts-ignore',
meta: {
type: 'problem',
docs: {
description: 'Bans “// @ts-ignore” comments from being used.',
tslintRuleName: 'ban-ts-ignore',
category: 'Best Practices',
recommended: 'error'
},
schema: [],
messages: {
tsIgnoreComment:
'Do not use "// @ts-ignore" comments because they suppress compilation errors.'
}
},
defaultOptions: [],
create(context) {
const tsIgnoreRegExp = /^\/*\s*@ts-ignore/;
const sourceCode = context.getSourceCode();

return {
Program(): void {
const comments = sourceCode.getAllComments();

comments.forEach(comment => {
if (comment.type !== 'Line') {
return;
}
if (tsIgnoreRegExp.test(comment.value)) {
context.report({
node: comment,
messageId: 'tsIgnoreComment'
});
}
});
}
};
}
});
65 changes: 65 additions & 0 deletions packages/eslint-plugin/tests/rules/ban-ts-ignore.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import rule from '../../src/rules/ban-ts-ignore';
import { RuleTester } from '../RuleTester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser'
});

ruleTester.run('ban-ts-ignore', rule, {
valid: [
`// just a comment containing @ts-ignore somewhere`,
`/* @ts-ignore */`,
`/** @ts-ignore */`,
`/*
// @ts-ignore in a block
*/`
],
invalid: [
{
code: '// @ts-ignore',
errors: [
{
messageId: 'tsIgnoreComment',
line: 1,
column: 1
}
]
},
{
code: '// @ts-ignore: Suppress next line',
errors: [
{
messageId: 'tsIgnoreComment',
line: 1,
column: 1
}
]
},
{
code: '/////@ts-ignore: Suppress next line',
errors: [
{
messageId: 'tsIgnoreComment',
line: 1,
column: 1
}
]
},
{
code: `
if (false) {
// @ts-ignore: Unreachable code error
console.log("hello");
}
`,
parser: '@typescript-eslint/parser',
errors: [
{
messageId: 'tsIgnoreComment',
line: 3,
column: 3
}
]
}
]
});

0 comments on commit 859ab29

Please sign in to comment.