Skip to content

Commit

Permalink
fix: don't mark type aliases as violations
Browse files Browse the repository at this point in the history
re #153
  • Loading branch information
RebeccaStevens committed Jul 31, 2021
1 parent 2c747d2 commit 32b6761
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
50 changes: 26 additions & 24 deletions src/rules/prefer-readonly-type.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -185,33 +185,35 @@ function checkTypeReference(
context: RuleContext<keyof typeof errorMessages, Options>,
options: Options
): RuleResult<keyof typeof errorMessages, Options> {
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: [],
Expand Down
8 changes: 8 additions & 0 deletions src/util/tree.ts
Expand Up @@ -10,6 +10,7 @@ import {
isMethodDefinition,
isProperty,
isTSInterfaceBody,
isTSTypeAliasDeclaration,
} from "./typeguard";

/**
Expand Down Expand Up @@ -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?
*/
Expand Down
6 changes: 6 additions & 0 deletions src/util/typeguard.ts
Expand Up @@ -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 {
Expand Down

0 comments on commit 32b6761

Please sign in to comment.