Skip to content

Commit 6ae1d01

Browse files
fix: don't mark type aliases as violations
re #153
1 parent 71ca205 commit 6ae1d01

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,
@@ -185,33 +185,35 @@ function checkTypeReference(
185185
context: RuleContext<keyof typeof errorMessages, Options>,
186186
options: Options
187187
): RuleResult<keyof typeof errorMessages, Options> {
188-
if (isIdentifier(node.typeName)) {
189-
if (
190-
options.ignoreCollections &&
191-
mutableTypeRegex.test(node.typeName.name)
192-
) {
193-
return {
194-
context,
195-
descriptors: [],
196-
};
197-
}
198-
const immutableType = mutableToImmutableTypes.get(node.typeName.name);
188+
if (
189+
!isIdentifier(node.typeName) ||
190+
(options.ignoreCollections && mutableTypeRegex.test(node.typeName.name)) ||
191+
isInTSTypeAliasDeclaration(node)
192+
) {
199193
return {
200194
context,
201-
descriptors:
202-
immutableType !== undefined &&
203-
immutableType.length > 0 &&
204-
(!options.allowMutableReturnType || !isInReturnType(node))
205-
? [
206-
{
207-
node,
208-
messageId: "type",
209-
fix: (fixer) => fixer.replaceText(node.typeName, immutableType),
210-
},
211-
]
212-
: [],
195+
descriptors: [],
196+
};
197+
}
198+
199+
const immutableType = mutableToImmutableTypes.get(node.typeName.name);
200+
if (
201+
immutableType !== undefined &&
202+
immutableType.length > 0 &&
203+
(!options.allowMutableReturnType || !isInReturnType(node))
204+
) {
205+
return {
206+
context,
207+
descriptors: [
208+
{
209+
node,
210+
messageId: "type",
211+
fix: (fixer) => fixer.replaceText(node.typeName, immutableType),
212+
},
213+
],
213214
};
214215
}
216+
215217
return {
216218
context,
217219
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
@@ -234,6 +234,12 @@ export function isTSTupleType(
234234
return node.type === AST_NODE_TYPES.TSTupleType;
235235
}
236236

237+
export function isTSTypeAliasDeclaration(
238+
node: TSESTree.Node
239+
): node is TSESTree.TSTypeAliasDeclaration {
240+
return node.type === AST_NODE_TYPES.TSTypeAliasDeclaration;
241+
}
242+
237243
export function isTSTypeAnnotation(
238244
node: TSESTree.Node
239245
): node is TSESTree.TSTypeAnnotation {

0 commit comments

Comments
 (0)