Skip to content

Commit

Permalink
fix(eslint-plugin): [type-annotation-spacing] handle constructor types (
Browse files Browse the repository at this point in the history
#1664)

Fixes #1663
  • Loading branch information
bradzacher committed Mar 3, 2020
1 parent 91423e4 commit fbf1640
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/type-annotation-spacing.ts
Expand Up @@ -5,6 +5,7 @@ import {
isFunction,
isFunctionOrFunctionType,
isIdentifier,
isTSConstructorType,
isTSFunctionType,
isVariableDeclarator,
} from '../util';
Expand Down Expand Up @@ -93,7 +94,7 @@ function getRules(
): WhitespaceRule {
const scope = node?.parent?.parent;

if (isTSFunctionType(scope)) {
if (isTSFunctionType(scope) || isTSConstructorType(scope)) {
return rules.arrow;
} else if (isIdentifier(scope)) {
return getIdentifierRules(rules, scope);
Expand Down
10 changes: 10 additions & 0 deletions packages/eslint-plugin/src/util/astUtils.ts
Expand Up @@ -109,6 +109,7 @@ function isFunctionType(
node: TSESTree.Node | undefined,
): node is
| TSESTree.TSCallSignatureDeclaration
| TSESTree.TSConstructorType
| TSESTree.TSConstructSignatureDeclaration
| TSESTree.TSEmptyBodyFunctionExpression
| TSESTree.TSFunctionType
Expand All @@ -119,6 +120,7 @@ function isFunctionType(

return [
AST_NODE_TYPES.TSCallSignatureDeclaration,
AST_NODE_TYPES.TSConstructorType,
AST_NODE_TYPES.TSConstructSignatureDeclaration,
AST_NODE_TYPES.TSEmptyBodyFunctionExpression,
AST_NODE_TYPES.TSFunctionType,
Expand All @@ -133,6 +135,7 @@ function isFunctionOrFunctionType(
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| TSESTree.TSCallSignatureDeclaration
| TSESTree.TSConstructorType
| TSESTree.TSConstructSignatureDeclaration
| TSESTree.TSEmptyBodyFunctionExpression
| TSESTree.TSFunctionType
Expand All @@ -146,6 +149,12 @@ function isTSFunctionType(
return node?.type === AST_NODE_TYPES.TSFunctionType;
}

function isTSConstructorType(
node: TSESTree.Node | undefined,
): node is TSESTree.TSConstructorType {
return node?.type === AST_NODE_TYPES.TSConstructorType;
}

function isClassOrTypeElement(
node: TSESTree.Node | undefined,
): node is TSESTree.ClassElement | TSESTree.TypeElement {
Expand Down Expand Up @@ -248,6 +257,7 @@ export {
isOptionalOptionalChain,
isSetter,
isTokenOnSameLine,
isTSConstructorType,
isTSFunctionType,
isTypeAssertion,
isVariableDeclarator,
Expand Down
Expand Up @@ -1174,6 +1174,8 @@ interface Foo {
},
],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/1663
'type ConstructorFn = new (...args: any[]) => any;',
],
invalid: [
{
Expand Down

0 comments on commit fbf1640

Please sign in to comment.