Skip to content

Commit fbf1640

Browse files
authored
fix(eslint-plugin): [type-annotation-spacing] handle constructor types (typescript-eslint#1664)
Fixes typescript-eslint#1663
1 parent 91423e4 commit fbf1640

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

packages/eslint-plugin/src/rules/type-annotation-spacing.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
isFunction,
66
isFunctionOrFunctionType,
77
isIdentifier,
8+
isTSConstructorType,
89
isTSFunctionType,
910
isVariableDeclarator,
1011
} from '../util';
@@ -93,7 +94,7 @@ function getRules(
9394
): WhitespaceRule {
9495
const scope = node?.parent?.parent;
9596

96-
if (isTSFunctionType(scope)) {
97+
if (isTSFunctionType(scope) || isTSConstructorType(scope)) {
9798
return rules.arrow;
9899
} else if (isIdentifier(scope)) {
99100
return getIdentifierRules(rules, scope);

packages/eslint-plugin/src/util/astUtils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ function isFunctionType(
109109
node: TSESTree.Node | undefined,
110110
): node is
111111
| TSESTree.TSCallSignatureDeclaration
112+
| TSESTree.TSConstructorType
112113
| TSESTree.TSConstructSignatureDeclaration
113114
| TSESTree.TSEmptyBodyFunctionExpression
114115
| TSESTree.TSFunctionType
@@ -119,6 +120,7 @@ function isFunctionType(
119120

120121
return [
121122
AST_NODE_TYPES.TSCallSignatureDeclaration,
123+
AST_NODE_TYPES.TSConstructorType,
122124
AST_NODE_TYPES.TSConstructSignatureDeclaration,
123125
AST_NODE_TYPES.TSEmptyBodyFunctionExpression,
124126
AST_NODE_TYPES.TSFunctionType,
@@ -133,6 +135,7 @@ function isFunctionOrFunctionType(
133135
| TSESTree.FunctionDeclaration
134136
| TSESTree.FunctionExpression
135137
| TSESTree.TSCallSignatureDeclaration
138+
| TSESTree.TSConstructorType
136139
| TSESTree.TSConstructSignatureDeclaration
137140
| TSESTree.TSEmptyBodyFunctionExpression
138141
| TSESTree.TSFunctionType
@@ -146,6 +149,12 @@ function isTSFunctionType(
146149
return node?.type === AST_NODE_TYPES.TSFunctionType;
147150
}
148151

152+
function isTSConstructorType(
153+
node: TSESTree.Node | undefined,
154+
): node is TSESTree.TSConstructorType {
155+
return node?.type === AST_NODE_TYPES.TSConstructorType;
156+
}
157+
149158
function isClassOrTypeElement(
150159
node: TSESTree.Node | undefined,
151160
): node is TSESTree.ClassElement | TSESTree.TypeElement {
@@ -248,6 +257,7 @@ export {
248257
isOptionalOptionalChain,
249258
isSetter,
250259
isTokenOnSameLine,
260+
isTSConstructorType,
251261
isTSFunctionType,
252262
isTypeAssertion,
253263
isVariableDeclarator,

packages/eslint-plugin/tests/rules/type-annotation-spacing.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,8 @@ interface Foo {
11741174
},
11751175
],
11761176
},
1177+
// https://github.com/typescript-eslint/typescript-eslint/issues/1663
1178+
'type ConstructorFn = new (...args: any[]) => any;',
11771179
],
11781180
invalid: [
11791181
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy