From c3c8b8643553057398395df73c9d43757b576f11 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Tue, 17 Sep 2019 05:48:07 +0300 Subject: [PATCH] fix(eslint-plugin): [cons-type-assns] handle namespaced types (#975) --- .../src/rules/consistent-type-assertions.ts | 13 +++++++++---- .../tests/rules/consistent-type-assertions.test.ts | 10 ++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/src/rules/consistent-type-assertions.ts b/packages/eslint-plugin/src/rules/consistent-type-assertions.ts index a5f87250da0..8c1b853c147 100644 --- a/packages/eslint-plugin/src/rules/consistent-type-assertions.ts +++ b/packages/eslint-plugin/src/rules/consistent-type-assertions.ts @@ -95,11 +95,14 @@ export default util.createRule({ case AST_NODE_TYPES.TSUnknownKeyword: return false; case AST_NODE_TYPES.TSTypeReference: - // Ignore `as const` and `` return ( - node.typeName.type === AST_NODE_TYPES.Identifier && - node.typeName.name !== 'const' + // Ignore `as const` and `` + (node.typeName.type === AST_NODE_TYPES.Identifier && + node.typeName.name !== 'const') || + // Allow qualified names which have dots between identifiers, `Foo.Bar` + node.typeName.type === AST_NODE_TYPES.TSQualifiedName ); + default: return true; } @@ -115,12 +118,14 @@ export default util.createRule({ ) { return; } + if ( options.objectLiteralTypeAssertions === 'allow-as-parameter' && node.parent && (node.parent.type === AST_NODE_TYPES.NewExpression || node.parent.type === AST_NODE_TYPES.CallExpression || - node.parent.type === AST_NODE_TYPES.ThrowStatement) + node.parent.type === AST_NODE_TYPES.ThrowStatement || + node.parent.type === AST_NODE_TYPES.AssignmentPattern) ) { return; } diff --git a/packages/eslint-plugin/tests/rules/consistent-type-assertions.test.ts b/packages/eslint-plugin/tests/rules/consistent-type-assertions.test.ts index b631fdda9d4..71f51492d23 100644 --- a/packages/eslint-plugin/tests/rules/consistent-type-assertions.test.ts +++ b/packages/eslint-plugin/tests/rules/consistent-type-assertions.test.ts @@ -29,6 +29,8 @@ const OBJECT_LITERAL_ARGUMENT_AS_CASTS = ` print({ bar: 5 } as Foo) new print({ bar: 5 } as Foo) function foo() { throw { bar: 5 } as Foo } +function b(x = {} as Foo.Bar) {} +function c(x = {} as Foo) {} `; const OBJECT_LITERAL_ARGUMENT_ANGLE_BRACKET_CASTS = ` print({ bar: 5 }) @@ -269,6 +271,14 @@ ruleTester.run('consistent-type-assertions', rule, { messageId: 'unexpectedObjectTypeAssertion', line: 5, }, + { + messageId: 'unexpectedObjectTypeAssertion', + line: 6, + }, + { + messageId: 'unexpectedObjectTypeAssertion', + line: 7, + }, ], }), ...batchedSingleLineTests({