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): [cons-type-assns] handle namespaced types #975

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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