Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Add adjacent-overload-signatures (#37)
- Loading branch information
1 parent
4667709
commit f5aa4b8
Showing
4 changed files
with
812 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
79 changes: 79 additions & 0 deletions
79
packages/eslint-plugin-typescript/docs/rules/adjacent-overload-signatures.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,79 @@ | ||
# Enforces member overloads to be consecutive. | ||
|
||
Grouping overloaded members (methods and functions) together can improve readability of the code. | ||
|
||
## Rule Details | ||
|
||
This rule aims to standardise the way overloaded members are organized. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```ts | ||
declare namespace Foo { | ||
export function foo(s: string): void; | ||
export function foo(n: number): void; | ||
export function bar(): void; | ||
export function foo(sn: string | number): void; | ||
} | ||
|
||
type Foo = { | ||
foo(s: string): void; | ||
foo(n: number): void; | ||
bar(): void; | ||
foo(sn: string | number): void; | ||
} | ||
|
||
interface Foo { | ||
foo(s: string): void; | ||
foo(n: number): void; | ||
bar(): void; | ||
foo(sn: string | number): void; | ||
} | ||
|
||
class Foo { | ||
foo(s: string): void; | ||
foo(n: number): void; | ||
bar(): void {} | ||
foo(sn: string | number): void {} | ||
} | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```ts | ||
declare namespace Foo { | ||
export function foo(s: string): void; | ||
export function foo(n: number): void; | ||
export function foo(sn: string | number): void; | ||
export function bar(): void; | ||
} | ||
|
||
type Foo = { | ||
foo(s: string): void; | ||
foo(n: number): void; | ||
foo(sn: string | number): void; | ||
bar(): void; | ||
} | ||
|
||
interface Foo { | ||
foo(s: string): void; | ||
foo(n: number): void; | ||
foo(sn: string | number): void; | ||
bar(): void; | ||
} | ||
|
||
class Foo { | ||
foo(s: string): void; | ||
foo(n: number): void; | ||
foo(sn: string | number): void {} | ||
bar(): void {} | ||
} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't care about the general structure of the code, then you will not need this rule. | ||
|
||
## Compatibility | ||
|
||
* TSLint: [adjacent-overload-signatures](https://palantir.github.io/tslint/rules/adjacent-overload-signatures/) |
99 changes: 99 additions & 0 deletions
99
packages/eslint-plugin-typescript/lib/rules/adjacent-overload-signatures.js
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,99 @@ | ||
/** | ||
* @fileoverview Enforces member overloads to be consecutive. | ||
* @author Patricio Trevino | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "Enforces member overloads to be consecutive", | ||
category: "TypeScript" | ||
}, | ||
schema: [] | ||
}, | ||
|
||
create(context) { | ||
|
||
//---------------------------------------------------------------------- | ||
// Helpers | ||
//---------------------------------------------------------------------- | ||
|
||
/** | ||
* Gets the name of the member being processed. | ||
* @param {ASTNode} member the member being processed. | ||
* @returns {string} the name of the member or null if it's a member not relevant to the rule. | ||
* @private | ||
*/ | ||
function getMemberName(member) { | ||
switch (member.type) { | ||
case "ExportNamedDeclaration": { | ||
return member.declaration.id.name; | ||
} | ||
case "DeclareFunction": | ||
case "FunctionDeclaration": | ||
case "TSNamespaceFunctionDeclaration": { | ||
return member.id.name; | ||
} | ||
case "TSMethodSignature": { | ||
return member.key && (member.key.name || member.key.value) || | ||
member.name && (member.name.name || member.name.value); | ||
} | ||
case "TSCallSignature": { | ||
return "call"; | ||
} | ||
case "MethodDefinition": { | ||
return member.key.name || member.key.value; | ||
} | ||
default: { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Check the body for overload methods. | ||
* @param {ASTNode} node the body to be inspected. | ||
* @returns {void} | ||
* @private | ||
*/ | ||
function checkBodyForOverloadMethods(node) { | ||
const members = node.body || node.members; | ||
|
||
if (members) { | ||
const seen = []; | ||
let index, name, lastName; | ||
|
||
members.forEach(member => { | ||
name = getMemberName(member); | ||
index = seen.indexOf(name); | ||
if (index > -1 && lastName !== name) { | ||
context.report({ | ||
node: member, | ||
message: `All '${name}' signatures should be adjacent` | ||
}); | ||
} else if (index === -1) { | ||
seen.push(name); | ||
} | ||
|
||
lastName = name; | ||
}); | ||
} | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
// Public | ||
//---------------------------------------------------------------------- | ||
return { | ||
TSModuleBlock: checkBodyForOverloadMethods, | ||
TSTypeLiteral: checkBodyForOverloadMethods, | ||
TSInterfaceBody: checkBodyForOverloadMethods, | ||
ClassBody: checkBodyForOverloadMethods, | ||
Program: checkBodyForOverloadMethods | ||
}; | ||
} | ||
}; |
Oops, something went wrong.