Skip to content

Commit 9c3c686

Browse files
authored
fix(eslint-plugin): [ban-ts-comment] support block comments (typescript-eslint#2644)
1 parent 0d696c7 commit 9c3c686

File tree

4 files changed

+222
-33
lines changed

4 files changed

+222
-33
lines changed

packages/eslint-plugin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int
100100
| [`@typescript-eslint/adjacent-overload-signatures`](./docs/rules/adjacent-overload-signatures.md) | Require that member overloads be consecutive | :heavy_check_mark: | | |
101101
| [`@typescript-eslint/array-type`](./docs/rules/array-type.md) | Requires using either `T[]` or `Array<T>` for arrays | | :wrench: | |
102102
| [`@typescript-eslint/await-thenable`](./docs/rules/await-thenable.md) | Disallows awaiting a value that is not a Thenable | :heavy_check_mark: | | :thought_balloon: |
103-
| [`@typescript-eslint/ban-ts-comment`](./docs/rules/ban-ts-comment.md) | Bans `// @ts-<directive>` comments from being used or requires descriptions after directive | :heavy_check_mark: | | |
103+
| [`@typescript-eslint/ban-ts-comment`](./docs/rules/ban-ts-comment.md) | Bans `@ts-<directive>` comments from being used or requires descriptions after directive | :heavy_check_mark: | | |
104104
| [`@typescript-eslint/ban-tslint-comment`](./docs/rules/ban-tslint-comment.md) | Bans `// tslint:<rule-flag>` comments from being used | | :wrench: | |
105105
| [`@typescript-eslint/ban-types`](./docs/rules/ban-types.md) | Bans specific types from being used | :heavy_check_mark: | :wrench: | |
106106
| [`@typescript-eslint/class-literal-property-style`](./docs/rules/class-literal-property-style.md) | Ensures that literals on classes are exposed in a consistent style | | :wrench: | |

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Bans `// @ts-<directive>` comments from being used or requires descriptions after directive (`ban-ts-comment`)
1+
# Bans `@ts-<directive>` comments from being used or requires descriptions after directive (`ban-ts-comment`)
22

33
TypeScript provides several directive comments that can be used to alter how it processes files.
44
Using these to suppress TypeScript Compiler Errors reduces the effectiveness of TypeScript overall.
@@ -41,13 +41,19 @@ const defaultOptions: Options = {
4141

4242
A value of `true` for a particular directive means that this rule will report if it finds any usage of said directive.
4343

44-
For example, with the defaults above the following patterns are considered warnings:
44+
For example, with the defaults above the following patterns are considered warnings for single line or comment block lines:
4545

4646
```ts
4747
if (false) {
4848
// @ts-ignore: Unreachable code error
4949
console.log('hello');
5050
}
51+
if (false) {
52+
/*
53+
@ts-ignore: Unreachable code error
54+
*/
55+
console.log('hello');
56+
}
5157
```
5258

5359
The following patterns are not warnings:
@@ -63,22 +69,32 @@ if (false) {
6369

6470
A value of `'allow-with-description'` for a particular directive means that this rule will report if it finds a directive that does not have a description following the directive (on the same line).
6571

66-
For example, with `{ 'ts-expect-error': 'allow-with-description' }` the following pattern is considered a warning:
72+
For example, with `{ 'ts-expect-error': 'allow-with-description' }` the following patterns are considered a warning:
6773

6874
```ts
6975
if (false) {
7076
// @ts-expect-error
7177
console.log('hello');
7278
}
79+
if (false) {
80+
/* @ts-expect-error */
81+
console.log('hello');
82+
}
7383
```
7484

75-
The following pattern is not a warning:
85+
The following patterns are not a warning:
7686

7787
```ts
7888
if (false) {
7989
// @ts-expect-error: Unreachable code error
8090
console.log('hello');
8191
}
92+
if (false) {
93+
/*
94+
@ts-expect-error: Unreachable code error
95+
*/
96+
console.log('hello');
97+
}
8298
```
8399

84100
### `minimumDescriptionLength`

packages/eslint-plugin/src/rules/ban-ts-comment.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ export default util.createRule<[Options], MessageIds>({
2121
type: 'problem',
2222
docs: {
2323
description:
24-
'Bans `// @ts-<directive>` comments from being used or requires descriptions after directive',
24+
'Bans `@ts-<directive>` comments from being used or requires descriptions after directive',
2525
category: 'Best Practices',
2626
recommended: 'error',
2727
},
2828
messages: {
2929
tsDirectiveComment:
30-
'Do not use "// @ts-{{directive}}" because it alters compilation errors.',
30+
'Do not use "@ts-{{directive}}" because it alters compilation errors.',
3131
tsDirectiveCommentRequiresDescription:
32-
'Include a description after the "// @ts-{{directive}}" directive to explain why the @ts-{{directive}} is necessary. The description must be {{minimumDescriptionLength}} characters or longer.',
32+
'Include a description after the "@ts-{{directive}}" directive to explain why the @ts-{{directive}} is necessary. The description must be {{minimumDescriptionLength}} characters or longer.',
3333
},
3434
schema: [
3535
{
@@ -98,20 +98,25 @@ export default util.createRule<[Options], MessageIds>({
9898
},
9999
],
100100
create(context, [options]) {
101-
const tsCommentRegExp = /^\/*\s*@ts-(expect-error|ignore|check|nocheck)(.*)/;
101+
/*
102+
The regex used are taken from the ones used in the official TypeScript repo -
103+
https://github.com/microsoft/TypeScript/blob/master/src/compiler/scanner.ts#L281-L289
104+
*/
105+
const commentDirectiveRegExSingleLine = /^\/*\s*@ts-(expect-error|ignore|check|nocheck)(.*)/;
106+
const commentDirectiveRegExMultiLine = /^\s*(?:\/|\*)*\s*@ts-(expect-error|ignore|check|nocheck)(.*)/;
102107
const sourceCode = context.getSourceCode();
103108

104109
return {
105110
Program(): void {
106111
const comments = sourceCode.getAllComments();
107112

108113
comments.forEach(comment => {
114+
let regExp = commentDirectiveRegExSingleLine;
115+
109116
if (comment.type !== AST_TOKEN_TYPES.Line) {
110-
return;
117+
regExp = commentDirectiveRegExMultiLine;
111118
}
112-
113-
const [, directive, description] =
114-
tsCommentRegExp.exec(comment.value) ?? [];
119+
const [, directive, description] = regExp.exec(comment.value) ?? [];
115120

116121
const fullDirective = `ts-${directive}` as keyof Options;
117122

0 commit comments

Comments
 (0)
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