Skip to content

Commit 25807fc

Browse files
fix: don't mark type aliases as violations
re #153
1 parent bfe6f03 commit 25807fc

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

src/rules/prefer-readonly-type.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from "~/common/ignore-options";
1717
import type { RuleContext, RuleMetaData, RuleResult } from "~/util/rule";
1818
import { createRule, getTypeOfNode } from "~/util/rule";
19-
import { isInReturnType } from "~/util/tree";
19+
import { isInReturnType, isInTSTypeAliasDeclaration } from "~/util/tree";
2020
import {
2121
isArrayType,
2222
isAssignmentPattern,
@@ -184,33 +184,35 @@ function checkTypeReference(
184184
context: RuleContext<keyof typeof errorMessages, Options>,
185185
options: Options
186186
): RuleResult<keyof typeof errorMessages, Options> {
187-
if (isIdentifier(node.typeName)) {
188-
if (
189-
options.ignoreCollections &&
190-
mutableTypeRegex.test(node.typeName.name)
191-
) {
192-
return {
193-
context,
194-
descriptors: [],
195-
};
196-
}
197-
const immutableType = mutableToImmutableTypes.get(node.typeName.name);
187+
if (
188+
!isIdentifier(node.typeName) ||
189+
(options.ignoreCollections && mutableTypeRegex.test(node.typeName.name)) ||
190+
isInTSTypeAliasDeclaration(node)
191+
) {
198192
return {
199193
context,
200-
descriptors:
201-
immutableType !== undefined &&
202-
immutableType.length > 0 &&
203-
(!options.allowMutableReturnType || !isInReturnType(node))
204-
? [
205-
{
206-
node,
207-
messageId: "type",
208-
fix: (fixer) => fixer.replaceText(node.typeName, immutableType),
209-
},
210-
]
211-
: [],
194+
descriptors: [],
195+
};
196+
}
197+
198+
const immutableType = mutableToImmutableTypes.get(node.typeName.name);
199+
if (
200+
immutableType !== undefined &&
201+
immutableType.length > 0 &&
202+
(!options.allowMutableReturnType || !isInReturnType(node))
203+
) {
204+
return {
205+
context,
206+
descriptors: [
207+
{
208+
node,
209+
messageId: "type",
210+
fix: (fixer) => fixer.replaceText(node.typeName, immutableType),
211+
},
212+
],
212213
};
213214
}
215+
214216
return {
215217
context,
216218
descriptors: [],

src/util/tree.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
isMethodDefinition,
1111
isProperty,
1212
isTSInterfaceBody,
13+
isTSTypeAliasDeclaration,
1314
} from "./typeguard";
1415

1516
/**
@@ -81,6 +82,13 @@ export function isInReturnType(node: TSESTree.Node): boolean {
8182
);
8283
}
8384

85+
/**
86+
* Is the given node in a TS Type Alias Declaration.
87+
*/
88+
export function isInTSTypeAliasDeclaration(node: TSESTree.Node): boolean {
89+
return getAncestorOfType(isTSTypeAliasDeclaration, node) !== null;
90+
}
91+
8492
/**
8593
* Is the given identifier a property of an object?
8694
*/

src/util/typeguard.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ export function isTSTupleType(
222222
return node.type === AST_NODE_TYPES.TSTupleType;
223223
}
224224

225+
export function isTSTypeAliasDeclaration(
226+
node: TSESTree.Node
227+
): node is TSESTree.TSTypeAliasDeclaration {
228+
return node.type === AST_NODE_TYPES.TSTypeAliasDeclaration;
229+
}
230+
225231
export function isTSTypeAnnotation(
226232
node: TSESTree.Node
227233
): node is TSESTree.TSTypeAnnotation {

0 commit comments

Comments
 (0)