Skip to content

Commit

Permalink
fix(eslint-plugin): [cons-type-assns] handle namespaced types (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk authored and bradzacher committed Sep 17, 2019
1 parent e348cb2 commit c3c8b86
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/eslint-plugin/src/rules/consistent-type-assertions.ts
Expand Up @@ -95,11 +95,14 @@ export default util.createRule<Options, MessageIds>({
case AST_NODE_TYPES.TSUnknownKeyword:
return false;
case AST_NODE_TYPES.TSTypeReference:
// Ignore `as const` and `<const>`
return (
node.typeName.type === AST_NODE_TYPES.Identifier &&
node.typeName.name !== 'const'
// Ignore `as const` and `<const>`
(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;
}
Expand All @@ -115,12 +118,14 @@ export default util.createRule<Options, MessageIds>({
) {
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;
}
Expand Down
Expand Up @@ -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(<Foo>{ bar: 5 })
Expand Down Expand Up @@ -269,6 +271,14 @@ ruleTester.run('consistent-type-assertions', rule, {
messageId: 'unexpectedObjectTypeAssertion',
line: 5,
},
{
messageId: 'unexpectedObjectTypeAssertion',
line: 6,
},
{
messageId: 'unexpectedObjectTypeAssertion',
line: 7,
},
],
}),
...batchedSingleLineTests({
Expand Down

0 comments on commit c3c8b86

Please sign in to comment.