Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [type-annotation-spacing] handle constructor types #1664

Merged
merged 1 commit into from Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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