Skip to content

Commit

Permalink
fix(eslint-plugin): [method-signature-style] fix crash with methods w…
Browse files Browse the repository at this point in the history
…ithout a return type (#2836)

Fixes #2834
  • Loading branch information
bradzacher committed Dec 1, 2020
1 parent aadb39f commit fed89f2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
19 changes: 10 additions & 9 deletions packages/eslint-plugin/src/rules/method-signature-style.ts
Expand Up @@ -82,7 +82,11 @@ export default util.createRule<Options, MessageIds>({
function getMethodReturnType(
node: TSESTree.TSMethodSignature | TSESTree.TSFunctionType,
): string {
return sourceCode.getText(node.returnType!.typeAnnotation);
return node.returnType == null
? // if the method has no return type, it implicitly has an `any` return type
// we just make it explicit here so we can do the fix
'any'
: sourceCode.getText(node.returnType.typeAnnotation);
}

function getDelimiter(node: TSESTree.Node): string {
Expand Down Expand Up @@ -149,16 +153,13 @@ export default util.createRule<Options, MessageIds>({
methodNode,
...duplicatedKeyMethodNodes,
].sort((a, b) => (a.range[0] < b.range[0] ? -1 : 1));
const typeString = methodNodes.reduce(
(str, node, idx, nodes) => {
const typeString = methodNodes
.map(node => {
const params = getMethodParams(node);
const returnType = getMethodReturnType(node);
return `${str}(${params} => ${returnType})${
idx !== nodes.length - 1 ? ' & ' : ''
}`;
},
'',
);
return `(${params} => ${returnType})`;
})
.join(' & ');
const key = getMethodKey(methodNode);
const delimiter = getDelimiter(methodNode);
yield fixer.replaceText(
Expand Down
19 changes: 19 additions & 0 deletions packages/eslint-plugin/tests/rules/method-signature-style.test.ts
Expand Up @@ -427,5 +427,24 @@ declare const Foo: {
},
],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/2834
{
code: `
interface MyInterface {
methodReturningImplicitAny();
}
`,
output: `
interface MyInterface {
methodReturningImplicitAny: () => any;
}
`,
errors: [
{
messageId: 'errorMethod',
line: 3,
},
],
},
],
});

0 comments on commit fed89f2

Please sign in to comment.