Skip to content

Commit

Permalink
feat(eslint-plugin): [ban-ts-comment] support ts-expect-error (#1706)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Apr 13, 2020
1 parent 05030f8 commit 469cff3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/eslint-plugin/docs/rules/ban-ts-comment.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Using these to suppress TypeScript Compiler Errors reduces the effectiveness of
The directive comments supported by TypeScript are:

```
// @ts-expect-error
// @ts-ignore
// @ts-nocheck
// @ts-check
Expand All @@ -14,18 +15,20 @@ The directive comments supported by TypeScript are:
## Rule Details

This rule lets you set which directive comments you want to allow in your codebase.
By default, only `@ts-check` is allowed, as it enables rather then suppresses errors.
By default, only `@ts-check` is allowed, as it enables rather than suppresses errors.

The configuration looks like this:

```
interface Options {
'ts-expect-error'?: boolean;
'ts-ignore'?: boolean;
'ts-nocheck'?: boolean;
'ts-check'?: boolean;
}
const defaultOptions: Options = {
'ts-expect-error': true,
'ts-ignore': true,
'ts-nocheck': true,
'ts-check': false
Expand Down
8 changes: 7 additions & 1 deletion packages/eslint-plugin/src/rules/ban-ts-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { AST_TOKEN_TYPES } from '@typescript-eslint/experimental-utils';
import * as util from '../util';

interface Options {
'ts-expect-error'?: boolean;
'ts-ignore'?: boolean;
'ts-nocheck'?: boolean;
'ts-check'?: boolean;
}

const defaultOptions: [Options] = [
{
'ts-expect-error': true,
'ts-ignore': true,
'ts-nocheck': true,
'ts-check': false,
Expand All @@ -34,6 +36,10 @@ export default util.createRule<[Options], MessageIds>({
{
type: 'object',
properties: {
'ts-expect-error': {
type: 'boolean',
default: true,
},
'ts-ignore': {
type: 'boolean',
default: true,
Expand All @@ -53,7 +59,7 @@ export default util.createRule<[Options], MessageIds>({
},
defaultOptions,
create(context, [options]) {
const tsCommentRegExp = /^\/*\s*@ts-(ignore|check|nocheck)/;
const tsCommentRegExp = /^\/*\s*@ts-(expect-error|ignore|check|nocheck)/;
const sourceCode = context.getSourceCode();

return {
Expand Down
74 changes: 73 additions & 1 deletion packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,79 @@ const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
});

ruleTester.run('ban-ts-comment', rule, {
ruleTester.run('ts-expect-error', rule, {
valid: [
'// just a comment containing @ts-expect-error somewhere',
'/* @ts-expect-error */',
'/** @ts-expect-error */',
`
/*
// @ts-expect-error in a block
*/
`,
{
code: '// @ts-expect-error',
options: [{ 'ts-expect-error': false }],
},
],
invalid: [
{
code: '// @ts-expect-error',
options: [{ 'ts-expect-error': true }],
errors: [
{
data: { directive: 'expect-error' },
messageId: 'tsDirectiveComment',
line: 1,
column: 1,
},
],
},
{
code: '// @ts-expect-error: Suppress next line',
options: [{ 'ts-expect-error': true }],
errors: [
{
data: { directive: 'expect-error' },
messageId: 'tsDirectiveComment',
line: 1,
column: 1,
},
],
},
{
code: '/////@ts-expect-error: Suppress next line',
options: [{ 'ts-expect-error': true }],
errors: [
{
data: { directive: 'expect-error' },
messageId: 'tsDirectiveComment',
line: 1,
column: 1,
},
],
},
{
code: `
if (false) {
// @ts-expect-error: Unreachable code error
console.log('hello');
}
`,
options: [{ 'ts-expect-error': true }],
errors: [
{
data: { directive: 'expect-error' },
messageId: 'tsDirectiveComment',
line: 3,
column: 3,
},
],
},
],
});

ruleTester.run('ts-ignore', rule, {
valid: [
'// just a comment containing @ts-ignore somewhere',
'/* @ts-ignore */',
Expand Down

0 comments on commit 469cff3

Please sign in to comment.