From 4fc9f6bf96ad19e60b53eb106705e607ba972e60 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Tue, 3 Mar 2020 01:18:22 -0800 Subject: [PATCH] fix(eslint-plugin): [type-annotation-spacing] handle constructor types Fixes #1663 --- .../eslint-plugin/src/rules/type-annotation-spacing.ts | 3 ++- packages/eslint-plugin/src/util/astUtils.ts | 10 ++++++++++ .../tests/rules/type-annotation-spacing.test.ts | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/type-annotation-spacing.ts b/packages/eslint-plugin/src/rules/type-annotation-spacing.ts index 5b9dbfbaa4ad..b33ec8fd7b41 100644 --- a/packages/eslint-plugin/src/rules/type-annotation-spacing.ts +++ b/packages/eslint-plugin/src/rules/type-annotation-spacing.ts @@ -5,6 +5,7 @@ import { isFunction, isFunctionOrFunctionType, isIdentifier, + isTSConstructorType, isTSFunctionType, isVariableDeclarator, } from '../util'; @@ -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); diff --git a/packages/eslint-plugin/src/util/astUtils.ts b/packages/eslint-plugin/src/util/astUtils.ts index 3dffeb90e01f..03f917e4ffbd 100644 --- a/packages/eslint-plugin/src/util/astUtils.ts +++ b/packages/eslint-plugin/src/util/astUtils.ts @@ -109,6 +109,7 @@ function isFunctionType( node: TSESTree.Node | undefined, ): node is | TSESTree.TSCallSignatureDeclaration + | TSESTree.TSConstructorType | TSESTree.TSConstructSignatureDeclaration | TSESTree.TSEmptyBodyFunctionExpression | TSESTree.TSFunctionType @@ -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, @@ -133,6 +135,7 @@ function isFunctionOrFunctionType( | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.TSCallSignatureDeclaration + | TSESTree.TSConstructorType | TSESTree.TSConstructSignatureDeclaration | TSESTree.TSEmptyBodyFunctionExpression | TSESTree.TSFunctionType @@ -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 { @@ -248,6 +257,7 @@ export { isOptionalOptionalChain, isSetter, isTokenOnSameLine, + isTSConstructorType, isTSFunctionType, isTypeAssertion, isVariableDeclarator, diff --git a/packages/eslint-plugin/tests/rules/type-annotation-spacing.test.ts b/packages/eslint-plugin/tests/rules/type-annotation-spacing.test.ts index e496743afc3e..6b0a3682578c 100644 --- a/packages/eslint-plugin/tests/rules/type-annotation-spacing.test.ts +++ b/packages/eslint-plugin/tests/rules/type-annotation-spacing.test.ts @@ -1174,6 +1174,8 @@ interface Foo { }, ], }, + // https://github.com/typescript-eslint/typescript-eslint/issues/1663 + 'type ConstructorFn = new (...args: any[]) => any;', ], invalid: [ { 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