Skip to content

Commit d29dafb

Browse files
authored
fix(consistent-chaining): allow TSNonNullExpression (#41)
1 parent cd75e4b commit d29dafb

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/rules/consistent-chaining.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ const valids: ValidTestCase[] = [
3131
.split('')
3232
.map(Number)
3333
`,
34+
$`
35+
foo.bar!.baz
36+
.toString()
37+
.split('')
38+
.map(Number)
39+
`,
40+
$`
41+
foo.bar.baz
42+
.toString()!
43+
.split('')
44+
.map(Number)
45+
`,
46+
$`
47+
foo.bar.baz!
48+
.toString()
49+
.split('')
50+
.map(Number)
51+
`,
3452
]
3553

3654
// Check snapshot for fixed code
@@ -170,6 +188,64 @@ const invalid: InvalidTestCase[] = [
170188
.map()"
171189
`),
172190
},
191+
{
192+
code: $`
193+
foo.bar.bar
194+
.filter()!.map()
195+
`,
196+
output: o => expect(o)
197+
.toMatchInlineSnapshot(`
198+
"foo.bar.bar
199+
.filter()!
200+
.map()"
201+
`),
202+
},
203+
{
204+
code: $`
205+
Math
206+
.random()! .toString() .split('')!.map(Number)
207+
`,
208+
output: o => expect(o)
209+
.toMatchInlineSnapshot(`
210+
"Math
211+
.random()!
212+
.toString()
213+
.split('')!
214+
.map(Number)"
215+
`),
216+
},
217+
{
218+
code: $`
219+
foo({
220+
bar: true
221+
})!
222+
.bar!
223+
.baz().boo()
224+
`,
225+
output: o => expect(o)
226+
.toMatchInlineSnapshot(`
227+
"foo({
228+
bar: true
229+
})!
230+
.bar!
231+
.baz()
232+
.boo()"
233+
`),
234+
},
235+
{
236+
code: $`
237+
this.foo!
238+
.toString()
239+
.split('').map(Number)
240+
`,
241+
output: o => expect(o)
242+
.toMatchInlineSnapshot(`
243+
"this.foo!
244+
.toString()
245+
.split('')
246+
.map(Number)"
247+
`),
248+
},
173249
]
174250

175251
run({

src/rules/consistent-chaining.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export default createEslintRule<Options, MessageIds>({
7070
current = current.callee
7171
break
7272
}
73+
case 'TSNonNullExpression': {
74+
current = current.expression
75+
break
76+
}
7377
default: {
7478
// Other type of note, that means we are probably reaching out the head
7579
current = undefined
@@ -85,10 +89,10 @@ export default createEslintRule<Options, MessageIds>({
8589
const token = context.sourceCode.getTokenBefore(m.property)!
8690
const tokenBefore = context.sourceCode.getTokenBefore(token)!
8791
const currentMode: 'single' | 'multi' = token.loc.start.line === tokenBefore.loc.end.line ? 'single' : 'multi'
88-
92+
const object = m.object.type === 'TSNonNullExpression' ? m.object.expression : m.object
8993
if (
9094
leadingPropertyAcccess
91-
&& (m.object.type === 'ThisExpression' || m.object.type === 'Identifier' || m.object.type === 'MemberExpression' || m.object.type === 'Literal')
95+
&& (object.type === 'ThisExpression' || object.type === 'Identifier' || object.type === 'MemberExpression' || object.type === 'Literal')
9296
&& currentMode === 'single'
9397
) {
9498
return

0 commit comments

Comments
 (0)