Skip to content

Commit eb09f0f

Browse files
committed
modify rule to skip null undefined
1 parent db0193f commit eb09f0f

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

validator/rules/no-inline-unions.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,31 @@ export default createRule({
3939
return;
4040
}
4141

42-
// skip simple nullable patterns (Type | null or Type | undefined)
4342
if (node.types.length === 2) {
44-
const hasNullOrUndefined = node.types.some(t =>
45-
t.type === 'TSNullKeyword' || t.type === 'TSUndefinedKeyword'
46-
);
43+
const [first, second] = node.types;
44+
45+
// skip Type | null or Type | undefined
46+
const hasNullOrUndefined =
47+
first.type === 'TSNullKeyword' || first.type === 'TSUndefinedKeyword' ||
48+
second.type === 'TSNullKeyword' || second.type === 'TSUndefinedKeyword';
4749
if (hasNullOrUndefined) {
4850
return;
4951
}
52+
53+
// skip Type | Type[] pattern
54+
if (second.type === 'TSArrayType') {
55+
if (first.type === second.elementType?.type) {
56+
if (first.type === 'TSTypeReference' && second.elementType.type === 'TSTypeReference') {
57+
// for reference types, check names match
58+
if (first.typeName?.name === second.elementType.typeName?.name) {
59+
return;
60+
}
61+
} else if (first.type === second.elementType.type) {
62+
// for primitive types (string, number, etc.), types already match
63+
return;
64+
}
65+
}
66+
}
5067
}
5168

5269
context.report({

validator/test/no-inline-unions.test.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,14 @@ ruleTester.run('no-inline-unions', rule, {
5858
`interface Config {
5959
value: integer | undefined
6060
}`,
61+
`class MyClass {
62+
filter: QueryContainer | QueryContainer[]
63+
}`,
64+
`interface MyInterface {
65+
input: string | string[]
66+
}`,
6167
],
6268
invalid: [
63-
{
64-
code: `class MyClass {
65-
filter: QueryContainer | QueryContainer[]
66-
}`,
67-
errors: [{ messageId: 'noInlineUnion' }]
68-
},
69-
{
70-
code: `interface MyInterface {
71-
input: string | string[]
72-
}`,
73-
errors: [{ messageId: 'noInlineUnion' }]
74-
},
7569
{
7670
code: `export class Request {
7771
value: string | number | boolean

0 commit comments

Comments
 (0)