Skip to content

Commit db0193f

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

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

validator/rules/no-inline-unions.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,27 @@ export default createRule({
3535
parent?.type === 'TSTypeAnnotation' &&
3636
parent.parent?.type === 'TSPropertySignature';
3737

38-
if (isPropertyAnnotation || isInterfaceProperty) {
39-
context.report({
40-
node,
41-
messageId: 'noInlineUnion',
42-
data: {
43-
suggestion: 'Define a named type alias (e.g., "export type MyUnion = A | B") and use that type instead'
44-
}
45-
})
38+
if (!isPropertyAnnotation && !isInterfaceProperty) {
39+
return;
4640
}
41+
42+
// skip simple nullable patterns (Type | null or Type | undefined)
43+
if (node.types.length === 2) {
44+
const hasNullOrUndefined = node.types.some(t =>
45+
t.type === 'TSNullKeyword' || t.type === 'TSUndefinedKeyword'
46+
);
47+
if (hasNullOrUndefined) {
48+
return;
49+
}
50+
}
51+
52+
context.report({
53+
node,
54+
messageId: 'noInlineUnion',
55+
data: {
56+
suggestion: 'Define a named type alias (e.g., "export type MyUnion = A | B") and use that type instead'
57+
}
58+
})
4759
},
4860
}
4961
},

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ ruleTester.run('no-inline-unions', rule, {
5252
class MyClass {
5353
items: Array<Item>
5454
}`,
55+
`class MyClass {
56+
id: string | null
57+
}`,
58+
`interface Config {
59+
value: integer | undefined
60+
}`,
5561
],
5662
invalid: [
5763
{
@@ -84,12 +90,6 @@ ruleTester.run('no-inline-unions', rule, {
8490
}`,
8591
errors: [{ messageId: 'noInlineUnion' }]
8692
},
87-
{
88-
code: `class MyClass {
89-
data: SamplingConfiguration | null
90-
}`,
91-
errors: [{ messageId: 'noInlineUnion' }]
92-
},
9393
],
9494
})
9595

0 commit comments

Comments
 (0)