From 6ae1d0190d93a802e316df882fc4d78d63a87744 Mon Sep 17 00:00:00 2001 From: Rebecca Stevens Date: Mon, 26 Jul 2021 08:09:47 +1200 Subject: [PATCH] fix: don't mark type aliases as violations re #153 --- src/rules/prefer-readonly-type.ts | 50 ++++++++++++++++--------------- src/util/tree.ts | 8 +++++ src/util/typeguard.ts | 6 ++++ 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/rules/prefer-readonly-type.ts b/src/rules/prefer-readonly-type.ts index 31a993e52..dd407ca72 100644 --- a/src/rules/prefer-readonly-type.ts +++ b/src/rules/prefer-readonly-type.ts @@ -16,7 +16,7 @@ import { } from "~/common/ignore-options"; import type { RuleContext, RuleMetaData, RuleResult } from "~/util/rule"; import { createRule, getTypeOfNode } from "~/util/rule"; -import { isInReturnType } from "~/util/tree"; +import { isInReturnType, isInTSTypeAliasDeclaration } from "~/util/tree"; import { isArrayType, isAssignmentPattern, @@ -185,33 +185,35 @@ function checkTypeReference( context: RuleContext, options: Options ): RuleResult { - if (isIdentifier(node.typeName)) { - if ( - options.ignoreCollections && - mutableTypeRegex.test(node.typeName.name) - ) { - return { - context, - descriptors: [], - }; - } - const immutableType = mutableToImmutableTypes.get(node.typeName.name); + if ( + !isIdentifier(node.typeName) || + (options.ignoreCollections && mutableTypeRegex.test(node.typeName.name)) || + isInTSTypeAliasDeclaration(node) + ) { return { context, - descriptors: - immutableType !== undefined && - immutableType.length > 0 && - (!options.allowMutableReturnType || !isInReturnType(node)) - ? [ - { - node, - messageId: "type", - fix: (fixer) => fixer.replaceText(node.typeName, immutableType), - }, - ] - : [], + descriptors: [], + }; + } + + const immutableType = mutableToImmutableTypes.get(node.typeName.name); + if ( + immutableType !== undefined && + immutableType.length > 0 && + (!options.allowMutableReturnType || !isInReturnType(node)) + ) { + return { + context, + descriptors: [ + { + node, + messageId: "type", + fix: (fixer) => fixer.replaceText(node.typeName, immutableType), + }, + ], }; } + return { context, descriptors: [], diff --git a/src/util/tree.ts b/src/util/tree.ts index 88cf73dae..56e8a0b27 100644 --- a/src/util/tree.ts +++ b/src/util/tree.ts @@ -10,6 +10,7 @@ import { isMethodDefinition, isProperty, isTSInterfaceBody, + isTSTypeAliasDeclaration, } from "./typeguard"; /** @@ -81,6 +82,13 @@ export function isInReturnType(node: TSESTree.Node): boolean { ); } +/** + * Is the given node in a TS Type Alias Declaration. + */ +export function isInTSTypeAliasDeclaration(node: TSESTree.Node): boolean { + return getAncestorOfType(isTSTypeAliasDeclaration, node) !== null; +} + /** * Is the given identifier a property of an object? */ diff --git a/src/util/typeguard.ts b/src/util/typeguard.ts index 4ed50bacd..6f95e8b96 100644 --- a/src/util/typeguard.ts +++ b/src/util/typeguard.ts @@ -234,6 +234,12 @@ export function isTSTupleType( return node.type === AST_NODE_TYPES.TSTupleType; } +export function isTSTypeAliasDeclaration( + node: TSESTree.Node +): node is TSESTree.TSTypeAliasDeclaration { + return node.type === AST_NODE_TYPES.TSTypeAliasDeclaration; +} + export function isTSTypeAnnotation( node: TSESTree.Node ): node is TSESTree.TSTypeAnnotation {