Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add extension [no-dupe-class-members] (#1492)
- Loading branch information
1 parent
9e7d161
commit b22424e
Showing
7 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/eslint-plugin/docs/rules/no-dupe-class-members.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Disallow duplicate class members (`no-dupe-class-members`) | ||
|
||
## Rule Details | ||
|
||
This rule extends the base [`eslint/no-no-dupe-class-members`](https://eslint.org/docs/rules/no-no-dupe-class-members) rule. | ||
It adds support for TypeScript's method overload definitions. | ||
|
||
## How to use | ||
|
||
```cjson | ||
{ | ||
// note you must disable the base rule as it can report incorrect errors | ||
"no-no-dupe-class-members": "off", | ||
"@typescript-eslint/no-no-dupe-class-members": ["error"] | ||
} | ||
``` | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-no-dupe-class-members.md)</sup> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils'; | ||
import baseRule from 'eslint/lib/rules/no-dupe-class-members'; | ||
import * as util from '../util'; | ||
|
||
type Options = util.InferOptionsTypeFromRule<typeof baseRule>; | ||
type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>; | ||
|
||
export default util.createRule<Options, MessageIds>({ | ||
name: 'no-dupe-class-members', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow duplicate class members', | ||
category: 'Possible Errors', | ||
recommended: false, | ||
extendsBaseRule: true, | ||
}, | ||
schema: baseRule.meta.schema, | ||
messages: baseRule.meta.messages, | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const rules = baseRule.create(context); | ||
|
||
return { | ||
...rules, | ||
MethodDefinition(node): void { | ||
if (node.computed) { | ||
return; | ||
} | ||
|
||
if (node.value.type === AST_NODE_TYPES.TSEmptyBodyFunctionExpression) { | ||
return; | ||
} | ||
|
||
return rules.MethodDefinition(node); | ||
}, | ||
}; | ||
}, | ||
}); |
79 changes: 79 additions & 0 deletions
79
packages/eslint-plugin/tests/rules/no-dupe-class-members.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import rule from '../../src/rules/no-dupe-class-members'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('no-dupe-class-members', rule, { | ||
valid: [ | ||
'class A { foo() {} bar() {} }', | ||
'class A { static foo() {} foo() {} }', | ||
'class A { get foo() {} set foo(value) {} }', | ||
'class A { static foo() {} get foo() {} set foo(value) {} }', | ||
'class A { foo() { } } class B { foo() { } }', | ||
'class A { [foo]() {} foo() {} }', | ||
"class A { 'foo'() {} 'bar'() {} baz() {} }", | ||
"class A { *'foo'() {} *'bar'() {} *baz() {} }", | ||
"class A { get 'foo'() {} get 'bar'() {} get baz() {} }", | ||
'class A { 1() {} 2() {} }', | ||
` | ||
class Foo { | ||
foo(a: string): string; | ||
foo(a: number): number; | ||
foo(a: any): any {} | ||
} | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'class A { foo() {} foo() {} }', | ||
errors: [ | ||
{ line: 1, column: 20, messageId: 'unexpected', data: { name: 'foo' } }, | ||
], | ||
}, | ||
{ | ||
code: '!class A { foo() {} foo() {} };', | ||
errors: [ | ||
{ line: 1, column: 21, messageId: 'unexpected', data: { name: 'foo' } }, | ||
], | ||
}, | ||
{ | ||
code: "class A { 'foo'() {} 'foo'() {} }", | ||
errors: [ | ||
{ line: 1, column: 22, messageId: 'unexpected', data: { name: 'foo' } }, | ||
], | ||
}, | ||
{ | ||
code: 'class A { 10() {} 1e1() {} }', | ||
errors: [ | ||
{ line: 1, column: 19, messageId: 'unexpected', data: { name: '10' } }, | ||
], | ||
}, | ||
{ | ||
code: 'class A { foo() {} foo() {} foo() {} }', | ||
errors: [ | ||
{ line: 1, column: 20, messageId: 'unexpected', data: { name: 'foo' } }, | ||
{ line: 1, column: 29, messageId: 'unexpected', data: { name: 'foo' } }, | ||
], | ||
}, | ||
{ | ||
code: 'class A { static foo() {} static foo() {} }', | ||
errors: [ | ||
{ line: 1, column: 27, messageId: 'unexpected', data: { name: 'foo' } }, | ||
], | ||
}, | ||
{ | ||
code: 'class A { foo() {} get foo() {} }', | ||
errors: [ | ||
{ line: 1, column: 20, messageId: 'unexpected', data: { name: 'foo' } }, | ||
], | ||
}, | ||
{ | ||
code: 'class A { set foo(value) {} foo() {} }', | ||
errors: [ | ||
{ line: 1, column: 29, messageId: 'unexpected', data: { name: 'foo' } }, | ||
], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters