Skip to content

Commit c3c8b86

Browse files
a-tarasyukbradzacher
authored andcommitted
fix(eslint-plugin): [cons-type-assns] handle namespaced types (#975)
1 parent e348cb2 commit c3c8b86

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

packages/eslint-plugin/src/rules/consistent-type-assertions.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,14 @@ export default util.createRule<Options, MessageIds>({
9595
case AST_NODE_TYPES.TSUnknownKeyword:
9696
return false;
9797
case AST_NODE_TYPES.TSTypeReference:
98-
// Ignore `as const` and `<const>`
9998
return (
100-
node.typeName.type === AST_NODE_TYPES.Identifier &&
101-
node.typeName.name !== 'const'
99+
// Ignore `as const` and `<const>`
100+
(node.typeName.type === AST_NODE_TYPES.Identifier &&
101+
node.typeName.name !== 'const') ||
102+
// Allow qualified names which have dots between identifiers, `Foo.Bar`
103+
node.typeName.type === AST_NODE_TYPES.TSQualifiedName
102104
);
105+
103106
default:
104107
return true;
105108
}
@@ -115,12 +118,14 @@ export default util.createRule<Options, MessageIds>({
115118
) {
116119
return;
117120
}
121+
118122
if (
119123
options.objectLiteralTypeAssertions === 'allow-as-parameter' &&
120124
node.parent &&
121125
(node.parent.type === AST_NODE_TYPES.NewExpression ||
122126
node.parent.type === AST_NODE_TYPES.CallExpression ||
123-
node.parent.type === AST_NODE_TYPES.ThrowStatement)
127+
node.parent.type === AST_NODE_TYPES.ThrowStatement ||
128+
node.parent.type === AST_NODE_TYPES.AssignmentPattern)
124129
) {
125130
return;
126131
}

packages/eslint-plugin/tests/rules/consistent-type-assertions.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const OBJECT_LITERAL_ARGUMENT_AS_CASTS = `
2929
print({ bar: 5 } as Foo)
3030
new print({ bar: 5 } as Foo)
3131
function foo() { throw { bar: 5 } as Foo }
32+
function b(x = {} as Foo.Bar) {}
33+
function c(x = {} as Foo) {}
3234
`;
3335
const OBJECT_LITERAL_ARGUMENT_ANGLE_BRACKET_CASTS = `
3436
print(<Foo>{ bar: 5 })
@@ -269,6 +271,14 @@ ruleTester.run('consistent-type-assertions', rule, {
269271
messageId: 'unexpectedObjectTypeAssertion',
270272
line: 5,
271273
},
274+
{
275+
messageId: 'unexpectedObjectTypeAssertion',
276+
line: 6,
277+
},
278+
{
279+
messageId: 'unexpectedObjectTypeAssertion',
280+
line: 7,
281+
},
272282
],
273283
}),
274284
...batchedSingleLineTests({

0 commit comments

Comments
 (0)