Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [unified-signatures] type comparison and exported nodes #839

Merged
merged 16 commits into from Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 24 additions & 5 deletions packages/eslint-plugin/src/rules/unified-signatures.ts
Expand Up @@ -35,6 +35,9 @@ type ScopeNode =
| TSESTree.TSTypeLiteral;

type OverloadNode = MethodDefinition | SignatureDefinition;
type ContainingNode =
| TSESTree.ExportNamedDeclaration
| TSESTree.ExportDefaultDeclaration;

type SignatureDefinition =
| TSESTree.FunctionExpression
Expand Down Expand Up @@ -495,9 +498,17 @@ export default util.createRule({
currentScope = scopes.pop()!;
}

function addOverload(signature: OverloadNode, key?: string): void {
function addOverload(
signature: OverloadNode,
key?: string,
containingNode?: ContainingNode,
): void {
key = key || getOverloadKey(signature);
if (currentScope && signature.parent === currentScope.parent && key) {
if (
currentScope &&
(containingNode || signature).parent === currentScope.parent &&
key
) {
const overloads = currentScope.overloads.get(key);
if (overloads !== undefined) {
overloads.push(signature);
Expand All @@ -521,11 +532,10 @@ export default util.createRule({
createScope(node.body, node.typeParameters);
},
TSTypeLiteral: createScope,

// collect overloads
TSDeclareFunction(node): void {
if (node.id && !node.body) {
uniqueiniquity marked this conversation as resolved.
Show resolved Hide resolved
addOverload(node, node.id.name);
}
addOverload(node, node.id!.name, getExportingNode(node));
},
TSCallSignatureDeclaration: addOverload,
TSConstructSignatureDeclaration: addOverload,
Expand All @@ -540,6 +550,7 @@ export default util.createRule({
addOverload(node);
}
},

// validate scopes
'Program:exit': checkScope,
'TSModuleBlock:exit': checkScope,
Expand All @@ -550,6 +561,14 @@ export default util.createRule({
},
});

function getExportingNode(node: TSESTree.TSDeclareFunction) {
return node.parent &&
(node.parent.type === AST_NODE_TYPES.ExportNamedDeclaration ||
node.parent.type === AST_NODE_TYPES.ExportDefaultDeclaration)
? node.parent
: undefined;
}

function getOverloadKey(node: OverloadNode): string | undefined {
const info = getOverloadInfo(node);

Expand Down
34 changes: 34 additions & 0 deletions packages/eslint-plugin/tests/rules/unified-signatures.test.ts
Expand Up @@ -591,5 +591,39 @@ class Foo {
},
],
},
{
code: `
export function foo(line: number): number;
export function foo(line: number, character?: number): number;
`,
errors: [
{
messageId: 'omittingSingleParameter',
data: {
failureStringStart:
'These overloads can be combined into one signature',
},
line: 3,
column: 35,
},
],
},
{
code: `
declare function foo(line: number): number;
export function foo(line: number, character?: number): number;
`,
errors: [
{
messageId: 'omittingSingleParameter',
data: {
failureStringStart:
'These overloads can be combined into one signature',
},
line: 3,
column: 35,
},
],
},
],
});