Skip to content

Commit

Permalink
fix(eslint-plugin): [prefer-function-type] correct fixer when signatu…
Browse files Browse the repository at this point in the history
…re ends with a semi (#3002)
  • Loading branch information
armano2 committed Feb 5, 2021
1 parent b41da18 commit 898dd39
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/eslint-plugin/src/rules/prefer-function-type.ts
Expand Up @@ -88,6 +88,10 @@ export default util.createRule({
colonPos + 1,
)}`;

const lastChar = suggestion.endsWith(';') ? ';' : '';
if (lastChar) {
suggestion = suggestion.slice(0, -1);
}
if (shouldWrapSuggestion(parent.parent)) {
suggestion = `(${suggestion})`;
}
Expand All @@ -98,16 +102,17 @@ export default util.createRule({
.slice(
parent.id.range[0],
parent.typeParameters.range[1],
)} = ${suggestion}`;
)} = ${suggestion}${lastChar}`;
}
return `type ${parent.id.name} = ${suggestion}`;
return `type ${parent.id.name} = ${suggestion}${lastChar}`;
}
return suggestion.endsWith(';') ? suggestion.slice(0, -1) : suggestion;
return suggestion;
}

/**
* @param member The TypeElement being checked
* @param node The parent of member being checked
* @param tsThisTypes
*/
function checkMember(
member: TSESTree.TypeElement,
Expand Down
34 changes: 34 additions & 0 deletions packages/eslint-plugin/tests/rules/prefer-function-type.test.ts
Expand Up @@ -268,5 +268,39 @@ type Foo = () => {
};
`,
},
{
code: noFormat`
type X = {} | { (): void; }
`,
errors: [
{
messageId: 'functionTypeOverCallableType',
type: AST_NODE_TYPES.TSCallSignatureDeclaration,
data: {
literalOrInterface: phrases[AST_NODE_TYPES.TSTypeLiteral],
},
},
],
output: noFormat`
type X = {} | (() => void)
`,
},
{
code: noFormat`
type X = {} & { (): void; };
`,
errors: [
{
messageId: 'functionTypeOverCallableType',
type: AST_NODE_TYPES.TSCallSignatureDeclaration,
data: {
literalOrInterface: phrases[AST_NODE_TYPES.TSTypeLiteral],
},
},
],
output: noFormat`
type X = {} & (() => void);
`,
},
],
});

0 comments on commit 898dd39

Please sign in to comment.