Skip to content

Commit

Permalink
fix(eslint-plugin): [non-nullable-type-assertion-style] handle const …
Browse files Browse the repository at this point in the history
…assertion (#2881)
  • Loading branch information
yeonjuan committed Dec 16, 2020
1 parent d35a539 commit 53dc34d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
@@ -1,4 +1,7 @@
import { TSESTree } from '@typescript-eslint/experimental-utils';
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';

Expand Down Expand Up @@ -69,10 +72,24 @@ export default util.createRule({
return true;
};

const isConstAssertion = (
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
): boolean => {
return (
node.typeAnnotation.type === AST_NODE_TYPES.TSTypeReference &&
node.typeAnnotation.typeName.type === AST_NODE_TYPES.Identifier &&
node.typeAnnotation.typeName.name === 'const'
);
};

return {
'TSAsExpression, TSTypeAssertion'(
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
): void {
if (isConstAssertion(node)) {
return;
}

const originalTypes = getTypesIfNotLoose(node.expression);
if (!originalTypes) {
return;
Expand Down
Expand Up @@ -51,6 +51,9 @@ declare const x: T | number;
const y = x as NonNullable<T>;
`,
`
const foo = [] as const;
`,
],

invalid: [
Expand Down

0 comments on commit 53dc34d

Please sign in to comment.