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): no-empty-interface - add allowSingleExtend option#215

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
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
17 changes: 17 additions & 0 deletionspackages/eslint-plugin/docs/rules/no-empty-interface.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,6 +38,23 @@ interface Bar {
interface Baz extends Foo, Bar {}
```

### Options

This rule accepts a single object option with the following default configuration:

```json
{
"@typescript-eslint/no-empty-interface": [
"error",
{
"allowSingleExtends": false
}
]
}
```

- `allowSingleExtends: true` will silence warnings about extending a single interface without adding additional members

## When Not To Use It

If you don't care about having empty/meaningless interfaces, then you will not need this rule.
Expand Down
43 changes: 35 additions & 8 deletionspackages/eslint-plugin/src/rules/no-empty-interface.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,7 +6,14 @@
import { TSESTree } from '@typescript-eslint/typescript-estree';
import * as util from '../util';

export default util.createRule({
type Options = [
{
allowSingleExtends?: boolean;
}
];
type MessageIds = 'noEmpty' | 'noEmptyWithSuper';

export default util.createRule<Options, MessageIds>({
name: 'no-empty-interface',
meta: {
type: 'suggestion',
Expand All@@ -21,13 +28,28 @@ export default util.createRule({
noEmptyWithSuper:
'An interface declaring no members is equivalent to its supertype.'
},
schema: []
schema: [
{
type: 'object',
additionalProperties: false,
properties: {
allowSingleExtends: {
type: 'boolean'
}
}
}
]
},
defaultOptions: [],
create(context) {
defaultOptions: [
{
allowSingleExtends: false
}
],
create(context, [{ allowSingleExtends }]) {
return {
TSInterfaceDeclaration(node: TSESTree.TSInterfaceDeclaration) {
if (node.body.body.length !== 0) {
// interface contains members --> Nothing to report
return;
}

Expand All@@ -37,10 +59,15 @@ export default util.createRule({
messageId: 'noEmpty'
});
} else if (node.extends.length === 1) {
context.report({
node: node.id,
messageId: 'noEmptyWithSuper'
});
// interface extends exactly 1 interface --> Report depending on rule setting
if (allowSingleExtends) {
return;
} else {
context.report({
node: node.id,
messageId: 'noEmptyWithSuper'
});
}
}
}
};
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,7 +23,17 @@ interface Bar {

// valid because extending multiple interfaces can be used instead of a union type
interface Baz extends Foo, Bar {}
`
`,
{
code: `
interface Foo {
name: string;
}

interface Bar extends Foo {}
`,
options: [{ allowSingleExtends: true }]
}
],
invalid: [
{
Expand DownExpand Up@@ -54,6 +64,7 @@ interface Foo {

interface Bar extends Foo {}
`,
options: [{ allowSingleExtends: false }],
errors: [
{
messageId: 'noEmptyWithSuper',
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp