Skip to content

Commit 469cff3

Browse files
authored
feat(eslint-plugin): [ban-ts-comment] support ts-expect-error (#1706)
1 parent 05030f8 commit 469cff3

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

Diff for: packages/eslint-plugin/docs/rules/ban-ts-comment.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Using these to suppress TypeScript Compiler Errors reduces the effectiveness of
66
The directive comments supported by TypeScript are:
77

88
```
9+
// @ts-expect-error
910
// @ts-ignore
1011
// @ts-nocheck
1112
// @ts-check
@@ -14,18 +15,20 @@ The directive comments supported by TypeScript are:
1415
## Rule Details
1516

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

1920
The configuration looks like this:
2021

2122
```
2223
interface Options {
24+
'ts-expect-error'?: boolean;
2325
'ts-ignore'?: boolean;
2426
'ts-nocheck'?: boolean;
2527
'ts-check'?: boolean;
2628
}
2729
2830
const defaultOptions: Options = {
31+
'ts-expect-error': true,
2932
'ts-ignore': true,
3033
'ts-nocheck': true,
3134
'ts-check': false

Diff for: packages/eslint-plugin/src/rules/ban-ts-comment.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { AST_TOKEN_TYPES } from '@typescript-eslint/experimental-utils';
22
import * as util from '../util';
33

44
interface Options {
5+
'ts-expect-error'?: boolean;
56
'ts-ignore'?: boolean;
67
'ts-nocheck'?: boolean;
78
'ts-check'?: boolean;
89
}
910

1011
const defaultOptions: [Options] = [
1112
{
13+
'ts-expect-error': true,
1214
'ts-ignore': true,
1315
'ts-nocheck': true,
1416
'ts-check': false,
@@ -34,6 +36,10 @@ export default util.createRule<[Options], MessageIds>({
3436
{
3537
type: 'object',
3638
properties: {
39+
'ts-expect-error': {
40+
type: 'boolean',
41+
default: true,
42+
},
3743
'ts-ignore': {
3844
type: 'boolean',
3945
default: true,
@@ -53,7 +59,7 @@ export default util.createRule<[Options], MessageIds>({
5359
},
5460
defaultOptions,
5561
create(context, [options]) {
56-
const tsCommentRegExp = /^\/*\s*@ts-(ignore|check|nocheck)/;
62+
const tsCommentRegExp = /^\/*\s*@ts-(expect-error|ignore|check|nocheck)/;
5763
const sourceCode = context.getSourceCode();
5864

5965
return {

Diff for: packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts

+73-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,79 @@ const ruleTester = new RuleTester({
55
parser: '@typescript-eslint/parser',
66
});
77

8-
ruleTester.run('ban-ts-comment', rule, {
8+
ruleTester.run('ts-expect-error', rule, {
9+
valid: [
10+
'// just a comment containing @ts-expect-error somewhere',
11+
'/* @ts-expect-error */',
12+
'/** @ts-expect-error */',
13+
`
14+
/*
15+
// @ts-expect-error in a block
16+
*/
17+
`,
18+
{
19+
code: '// @ts-expect-error',
20+
options: [{ 'ts-expect-error': false }],
21+
},
22+
],
23+
invalid: [
24+
{
25+
code: '// @ts-expect-error',
26+
options: [{ 'ts-expect-error': true }],
27+
errors: [
28+
{
29+
data: { directive: 'expect-error' },
30+
messageId: 'tsDirectiveComment',
31+
line: 1,
32+
column: 1,
33+
},
34+
],
35+
},
36+
{
37+
code: '// @ts-expect-error: Suppress next line',
38+
options: [{ 'ts-expect-error': true }],
39+
errors: [
40+
{
41+
data: { directive: 'expect-error' },
42+
messageId: 'tsDirectiveComment',
43+
line: 1,
44+
column: 1,
45+
},
46+
],
47+
},
48+
{
49+
code: '/////@ts-expect-error: Suppress next line',
50+
options: [{ 'ts-expect-error': true }],
51+
errors: [
52+
{
53+
data: { directive: 'expect-error' },
54+
messageId: 'tsDirectiveComment',
55+
line: 1,
56+
column: 1,
57+
},
58+
],
59+
},
60+
{
61+
code: `
62+
if (false) {
63+
// @ts-expect-error: Unreachable code error
64+
console.log('hello');
65+
}
66+
`,
67+
options: [{ 'ts-expect-error': true }],
68+
errors: [
69+
{
70+
data: { directive: 'expect-error' },
71+
messageId: 'tsDirectiveComment',
72+
line: 3,
73+
column: 3,
74+
},
75+
],
76+
},
77+
],
78+
});
79+
80+
ruleTester.run('ts-ignore', rule, {
981
valid: [
1082
'// just a comment containing @ts-ignore somewhere',
1183
'/* @ts-ignore */',

0 commit comments

Comments
 (0)