Skip to content
Permalink
Browse files

fix(eslint-plugin): [unified-signatures] type comparison and exported…

… nodes (#839)
  • Loading branch information...
uniqueiniquity authored and JamesHenry committed Aug 19, 2019
1 parent 4b0d2d9 commit 580eceb83183ddc7a4798eb5bc55a36f71dcda89
@@ -60,3 +60,6 @@ jspm_packages/
.DS_Store
.idea
dist

# Editor-specific metadata folders
.vs
@@ -35,6 +35,9 @@ type ScopeNode =
| TSESTree.TSTypeLiteral;

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

type SignatureDefinition =
| TSESTree.FunctionExpression
@@ -424,7 +427,8 @@ export default util.createRule({
a === b ||
(a !== undefined &&
b !== undefined &&
a.typeAnnotation.type === b.typeAnnotation.type)
sourceCode.getText(a.typeAnnotation) ===
sourceCode.getText(b.typeAnnotation))
);
}

@@ -495,9 +499,16 @@ 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
) {
const overloads = currentScope.overloads.get(key);
if (overloads !== undefined) {
overloads.push(signature);
@@ -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) {
addOverload(node, node.id.name);
}
addOverload(node, node.id.name, getExportingNode(node));
},
TSCallSignatureDeclaration: addOverload,
TSConstructSignatureDeclaration: addOverload,
@@ -540,6 +550,7 @@ export default util.createRule({
addOverload(node);
}
},

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

function getOverloadKey(node: OverloadNode): string | undefined {
function getExportingNode(
node: TSESTree.TSDeclareFunction,
):
| TSESTree.ExportNamedDeclaration
| TSESTree.ExportDefaultDeclaration
| undefined {
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 {
const info = getOverloadInfo(node);

return (
@@ -105,6 +105,30 @@ interface I {
function f<T extends number>(x: T[]): void;
function f<T extends string>(x: T): void;
`,
// Same name, different scopes
`
declare function foo(n: number): number;
declare module "hello" {
function foo(n: number, s: string): number;
}
`,
// children of block not checked to match TSLint
`
{
function block(): number;
function block(n: number): number;
function block(n?: number): number {
return 3;
}
}
`,
`
export interface Foo {
bar(baz: string): number[];
bar(): string[];
}
`,
],
invalid: [
{
@@ -591,5 +615,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,
},
],
},
],
});
@@ -1040,6 +1040,7 @@ export interface TSConstructSignatureDeclaration extends FunctionSignatureBase {
}

export interface TSDeclareFunction extends FunctionDeclarationBase {
id: Identifier;
type: AST_NODE_TYPES.TSDeclareFunction;
}

0 comments on commit 580eceb

Please sign in to comment.
You can’t perform that action at this time.