Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): createban-ts-comment rule#1361

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
bradzacher merged 6 commits intotypescript-eslint:masterfromG-Rath:add-ban-ts-nocheck-rule
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions.cspell.json
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,6 +61,7 @@
"estree",
"linebreaks",
"necroing",
"nocheck",
"nullish",
"parameterised",
"performant",
Expand Down
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/README.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -98,7 +98,7 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int
| [`@typescript-eslint/adjacent-overload-signatures`](./docs/rules/adjacent-overload-signatures.md) | Require that member overloads be consecutive | :heavy_check_mark: | | |
| [`@typescript-eslint/array-type`](./docs/rules/array-type.md) | Requires using either `T[]` or `Array<T>` for arrays | | :wrench: | |
| [`@typescript-eslint/await-thenable`](./docs/rules/await-thenable.md) | Disallows awaiting a value that is not a Thenable | :heavy_check_mark: | | :thought_balloon: |
| [`@typescript-eslint/ban-ts-ignore`](./docs/rules/ban-ts-ignore.md)| Bans// @ts-ignore” comments from being used| :heavy_check_mark: | | |
| [`@typescript-eslint/ban-ts-comment`](./docs/rules/ban-ts-comment.md) | Bans`// @ts-<directive>` comments from being used| | | |
| [`@typescript-eslint/ban-types`](./docs/rules/ban-types.md) | Bans specific types from being used | :heavy_check_mark: | :wrench: | |
| [`@typescript-eslint/consistent-type-assertions`](./docs/rules/consistent-type-assertions.md) | Enforces consistent usage of type assertions | :heavy_check_mark: | | |
| [`@typescript-eslint/consistent-type-definitions`](./docs/rules/consistent-type-definitions.md) | Consistent with type definition either `interface` or `type` | | :wrench: | |
Expand Down
65 changes: 65 additions & 0 deletionspackages/eslint-plugin/docs/rules/ban-ts-comment.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
# Bans `// @ts-<directive>` comments from being used (`ban-ts-comment`)

TypeScript provides several directive comments that can be used to alter how it processes files.
Using these to suppress TypeScript Compiler Errors reduces the effectiveness of TypeScript overall.

The directive comments supported by TypeScript are:

```
// @ts-ignore
// @ts-nocheck
// @ts-check
```

## 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.

The configuration looks like this:

```
interface Options {
'ts-ignore'?: boolean;
'ts-nocheck'?: boolean;
'ts-check'?: boolean;
}

const defaultOptions: Options = {
'ts-ignore': true,
'ts-nocheck': true,
'ts-check': false
}
```

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

For example, with the defaults above the following patterns are considered warnings:

```ts
if (false) {
// @ts-ignore: Unreachable code error
console.log('hello');
}
```

The following patterns are not warnings:

```ts
if (false) {
// Compiler warns about unreachable code error
console.log('hello');
}
```

## When Not To Use It

If you want to use all of the TypeScript directives.

## Further Reading

- TypeScript [Type Checking JavaScript Files](https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html)

## Compatibility

- TSLint: [ban-ts-ignore](https://palantir.github.io/tslint/rules/ban-ts-ignore/)
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/docs/rules/ban-ts-ignore.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
# Bans “// @ts-ignore” comments from being used (`ban-ts-ignore`)

This rule has been deprecated in favor of [`ban-ts-comment`](./ban-ts-comment.md)

Suppressing TypeScript Compiler Errors can be hard to discover.

## Rule Details
Expand Down
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/src/configs/all.json
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,7 +4,7 @@
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/ban-ts-ignore": "error",
"@typescript-eslint/ban-ts-comment": "error",
"@typescript-eslint/ban-types": "error",
"brace-style": "off",
"@typescript-eslint/brace-style": "error",
Expand Down
82 changes: 82 additions & 0 deletionspackages/eslint-plugin/src/rules/ban-ts-comment.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
import * as util from '../util';

interface Options {
'ts-ignore'?: boolean;
'ts-nocheck'?: boolean;
'ts-check'?: boolean;
}

const defaultOptions: [Options] = [
{
'ts-ignore': true,
'ts-nocheck': true,
'ts-check': false,
},
];

type MessageIds = 'tsDirectiveComment';

export default util.createRule<[Options], MessageIds>({
name: 'ban-ts-comment',
meta: {
type: 'problem',
docs: {
description: 'Bans `// @ts-<directive>` comments from being used',
category: 'Best Practices',
recommended: false,
},
messages: {
tsDirectiveComment:
'Do not use "// @ts-{directive}" because it alters compilation errors.',
},
schema: [
{
type: 'object',
properties: {
'ts-ignore': {
type: 'boolean',
default: true,
},
'ts-nocheck': {
type: 'boolean',
default: true,
},
'ts-check': {
type: 'boolean',
default: false,
},
},
additionalProperties: false,
},
],
},
defaultOptions,
create(context, [options]) {
const tsCommentRegExp = /^\/*\s*@ts-(ignore|check|nocheck)/;
const sourceCode = context.getSourceCode();

return {
Program(): void {
const comments = sourceCode.getAllComments();

comments.forEach(comment => {
if (comment.type !== 'Line') {
return;
}

const [, directive] = tsCommentRegExp.exec(comment.value) ?? [];

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

if (options[fullDirective]) {
context.report({
data: { directive },
node: comment,
messageId: 'tsDirectiveComment',
});
}
});
},
};
},
});
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/src/rules/ban-ts-ignore.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,6 +14,8 @@ export default util.createRule({
tsIgnoreComment:
'Do not use "// @ts-ignore" comments because they suppress compilation errors.',
},
deprecated: true,
replacedBy: ['@typescript-eslint/ban-ts-comment'],
},
defaultOptions: [],
create(context) {
Expand Down
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/src/rules/index.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,7 @@ import adjacentOverloadSignatures from './adjacent-overload-signatures';
import arrayType from './array-type';
import awaitThenable from './await-thenable';
import banTsIgnore from './ban-ts-ignore';
import banTsComment from './ban-ts-comment';
import banTypes from './ban-types';
import braceStyle from './brace-style';
import camelcase from './camelcase';
Expand DownExpand Up@@ -85,6 +86,7 @@ export default {
'array-type': arrayType,
'await-thenable': awaitThenable,
'ban-ts-ignore': banTsIgnore,
'ban-ts-comment': banTsComment,
'ban-types': banTypes,
'brace-style': braceStyle,
camelcase: camelcase,
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp