Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [ban-ts-comment] support ts-expect-error #1706

Merged
merged 3 commits into from Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/eslint-plugin/docs/rules/ban-ts-comment.md
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
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
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