Skip to content

Commit

Permalink
fix(eslint-plugin): [consistent-type-assertions] enforce assertionSty…
Browse files Browse the repository at this point in the history
…le for `const` assertions (#4685)
  • Loading branch information
gtkatakura committed Mar 18, 2022
1 parent 0fe0683 commit 8ec05be
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
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

0 comments on commit 8ec05be

Please sign in to comment.