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): [consistent-type-assertions] enforce assertionStyle for const assertions #4685

Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -84,13 +84,13 @@ export default util.createRule<Options, MessageIds>({
function reportIncorrectAssertionType(
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
): void {
const messageId = options.assertionStyle;

// If this node is `as const`, then don't report an error.
if (isConst(node.typeAnnotation)) {
if (isConst(node.typeAnnotation) && messageId === 'never') {
return;
}

const messageId = options.assertionStyle;

context.report({
node,
messageId,
Expand Down
Expand Up @@ -5,18 +5,26 @@ const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
});

const ANGLE_BRACKET_TESTS = `
const ANGLE_BRACKET_TESTS_EXCEPT_CONST_CASE = `
const x = <Foo>new Generic<int>();
const x = <A>b;
const x = <readonly number[]>[1];
const x = <a | b>('string');
const x = <a | b>('string');`;

const ANGLE_BRACKET_TESTS = `${ANGLE_BRACKET_TESTS_EXCEPT_CONST_CASE}
const x = <const>{ key: 'value' };
`;
const AS_TESTS = `

const AS_TESTS_EXCEPT_CONST_CASE = `
const x = new Generic<int>() as Foo;
const x = b as A;
const x = [1] as readonly number[];
const x = ('string') as a | b;
const x = ('string') as a | b;`;

const AS_TESTS = `${AS_TESTS_EXCEPT_CONST_CASE}
const x = { key: 'value' } as const;
`;

const OBJECT_LITERAL_AS_CASTS = `
const x = {} as Foo<int>;
`;
Expand Down Expand Up @@ -189,7 +197,7 @@ ruleTester.run('consistent-type-assertions', rule, {
],
}),
...batchedSingleLineTests({
code: AS_TESTS,
code: AS_TESTS_EXCEPT_CONST_CASE,
options: [
{
assertionStyle: 'never',
Expand Down Expand Up @@ -219,7 +227,7 @@ ruleTester.run('consistent-type-assertions', rule, {
],
}),
...batchedSingleLineTests({
code: ANGLE_BRACKET_TESTS,
code: ANGLE_BRACKET_TESTS_EXCEPT_CONST_CASE,
options: [
{
assertionStyle: 'never',
Expand Down