Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 25 additions & 10 deletions packages/eslint-plugin/lib/rules/adjacent-overload-signatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ module.exports = {
}
}

/**
* Determine whether two methods are the same or not
* @param {{ name: string; static: boolean }} method1 a method to compare
* @param {{ name: string; static: boolean }} method2 another method to compare with
* @returns {boolean} true if two methods are the same
* @private
*/
function isSameMethod(method1, method2) {
return method1.name === method2.name && method1.static === method2.static;
}

/**
* Check the body for overload methods.
* @param {ASTNode} node the body to be inspected.
Expand All @@ -83,28 +94,32 @@ module.exports = {
const members = node.body || node.members;

if (members) {
let name;
let index;
let lastName;
const seen = [];
let lastMethod;
const seenMethods = [];

members.forEach(member => {
name = getMemberName(member);
const name = getMemberName(member);
const method = {
name,
static: member.static
};

index = seen.indexOf(name);
if (index > -1 && lastName !== name) {
const index = seenMethods.findIndex(seenMethod =>
isSameMethod(method, seenMethod)
);
if (index > -1 && !isSameMethod(method, lastMethod)) {
context.report({
node: member,
messageId: 'adjacentSignature',
data: {
name
name: (method.static ? 'static ' : '') + method.name
}
});
} else if (name && index === -1) {
seen.push(name);
seenMethods.push(method);
}

lastName = name;
lastMethod = method;
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,23 @@ class Foo {
foo(sn: string | number): void {}
bar(): void {}
baz(): void {}
}
`,
`
class Foo {
name: string;
static foo(s: string): void;
static foo(n: number): void;
static foo(sn: string | number): void {}
bar(): void {}
baz(): void {}
}
`,
`
class Test {
static test() {}
untest() {}
test() {}
}
`,
// examples from https://github.com/nzakas/eslint-plugin-typescript/issues/138
Expand Down Expand Up @@ -789,6 +806,26 @@ class Foo {
column: 5
}
]
},
{
code: `
class Foo {
static foo(s: string): void;
name: string;
static foo(n: number): void;
static foo(sn: string | number): void {}
bar(): void {}
baz(): void {}
}
`,
errors: [
{
messageId: 'adjacentSignature',
data: { name: 'static foo' },
line: 5,
column: 5
}
]
}
]
});