diff --git a/packages/eslint-plugin/src/rules/ban-ts-comment.ts b/packages/eslint-plugin/src/rules/ban-ts-comment.ts index 44970393cfc7..19001dcbd97a 100644 --- a/packages/eslint-plugin/src/rules/ban-ts-comment.ts +++ b/packages/eslint-plugin/src/rules/ban-ts-comment.ts @@ -182,7 +182,9 @@ export default createRule<[Options], MessageIds>({ } return { - Program(): void { + Program(node): void { + const firstStatement = node.body.at(0); + const comments = context.sourceCode.getAllComments(); comments.forEach(comment => { @@ -192,6 +194,14 @@ export default createRule<[Options], MessageIds>({ } const { directive, description } = match; + if ( + directive === 'nocheck' && + firstStatement && + firstStatement.loc.start.line <= comment.loc.start.line + ) { + return; + } + const fullDirective = `ts-${directive}` as keyof Options; const option = options[fullDirective]; 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 d89b560277ed..bfc2a4565700 100644 --- a/packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts +++ b/packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts @@ -992,6 +992,14 @@ ruleTester.run('ts-nocheck', rule, { `, '/** @ts-nocheck */', '/* @ts-nocheck */', + ` +const a = 1; + +// @ts-nocheck - should not be reported + +// TS error is not actually suppressed +const b: string = a; + `, ], invalid: [ { @@ -1028,22 +1036,6 @@ ruleTester.run('ts-nocheck', rule, { }, ], }, - { - code: ` -if (false) { - // @ts-nocheck: Unreachable code error - console.log('hello'); -} - `, - errors: [ - { - data: { directive: 'nocheck' }, - messageId: 'tsDirectiveComment', - line: 3, - column: 3, - }, - ], - }, { code: '// @ts-nocheck', options: [{ 'ts-nocheck': 'allow-with-description' }], @@ -1127,6 +1119,22 @@ if (false) { }, ], }, + { + // comment's column > first statement's column + // eslint-disable-next-line @typescript-eslint/internal/plugin-test-formatting + code: ` + // @ts-nocheck +const a: true = false; + `, + errors: [ + { + data: { directive: 'nocheck', minimumDescriptionLength: 3 }, + messageId: 'tsDirectiveComment', + line: 2, + column: 2, + }, + ], + }, ], }); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy