From 469cff332c041f38f60de052769287342455cff1 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Mon, 13 Apr 2020 13:02:00 +1200 Subject: [PATCH] feat(eslint-plugin): [ban-ts-comment] support `ts-expect-error` (#1706) --- .../docs/rules/ban-ts-comment.md | 5 +- .../eslint-plugin/src/rules/ban-ts-comment.ts | 8 +- .../tests/rules/ban-ts-comment.test.ts | 74 ++++++++++++++++++- 3 files changed, 84 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/ban-ts-comment.md b/packages/eslint-plugin/docs/rules/ban-ts-comment.md index 58f99dc048a..b1d93409c28 100644 --- a/packages/eslint-plugin/docs/rules/ban-ts-comment.md +++ b/packages/eslint-plugin/docs/rules/ban-ts-comment.md @@ -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 @@ -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 diff --git a/packages/eslint-plugin/src/rules/ban-ts-comment.ts b/packages/eslint-plugin/src/rules/ban-ts-comment.ts index 53505cfea22..a422349eefa 100644 --- a/packages/eslint-plugin/src/rules/ban-ts-comment.ts +++ b/packages/eslint-plugin/src/rules/ban-ts-comment.ts @@ -2,6 +2,7 @@ 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; @@ -9,6 +10,7 @@ interface Options { const defaultOptions: [Options] = [ { + 'ts-expect-error': true, 'ts-ignore': true, 'ts-nocheck': true, 'ts-check': false, @@ -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, @@ -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 { diff --git a/packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts b/packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts index fcbb788a308..89ff1db4a61 100644 --- a/packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts +++ b/packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts @@ -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 */',