Skip to content

Commit f7c4351

Browse files
committed
feat(consistent-chaining): change allowFirstPropertyAccess to allowLeadingPropertyAccess
1 parent 1a7fba8 commit f7c4351

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

src/rules/consistent-chaining.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ const valids: ValidTestCase[] = [
1919
.split('')
2020
.map(Number)
2121
`,
22+
$`
23+
foo.bar.baz()
24+
.toString()
25+
.split('')
26+
.map(Number)
27+
`,
28+
$`
29+
foo.bar.baz
30+
.toString()
31+
.split('')
32+
.map(Number)
33+
`,
2234
]
2335

2436
// Check snapshot for fixed code
@@ -146,6 +158,18 @@ const invalid: InvalidTestCase[] = [
146158
.filter(x => x)"
147159
`),
148160
},
161+
{
162+
code: $`
163+
foo.bar.bar
164+
.filter().map()
165+
`,
166+
output: o => expect(o)
167+
.toMatchInlineSnapshot(`
168+
"foo.bar.bar
169+
.filter()
170+
.map()"
171+
`),
172+
},
149173
]
150174

151175
run({

src/rules/consistent-chaining.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const RULE_NAME = 'consistent-chaining'
55
export type MessageIds = 'shouldWrap' | 'shouldNotWrap'
66
export type Options = [
77
{
8-
allowFirstPropertyAccess?: boolean
8+
allowLeadingPropertyAccess?: boolean
99
},
1010
]
1111

@@ -21,9 +21,9 @@ export default createEslintRule<Options, MessageIds>({
2121
{
2222
type: 'object',
2323
properties: {
24-
allowFirstPropertyAccess: {
24+
allowLeadingPropertyAccess: {
2525
type: 'boolean',
26-
description: 'Allow first property access to be on the same line',
26+
description: 'Allow leading property access to be on the same line',
2727
default: true,
2828
},
2929
},
@@ -37,14 +37,14 @@ export default createEslintRule<Options, MessageIds>({
3737
},
3838
defaultOptions: [
3939
{
40-
allowFirstPropertyAccess: true,
40+
allowLeadingPropertyAccess: true,
4141
},
4242
],
4343
create: (context) => {
4444
const knownRoot = new WeakSet<any>()
4545

4646
const {
47-
allowFirstPropertyAccess = true,
47+
allowLeadingPropertyAccess = true,
4848
} = context.options[0] || {}
4949

5050
return {
@@ -78,21 +78,23 @@ export default createEslintRule<Options, MessageIds>({
7878
}
7979
}
8080

81+
let leadingPropertyAcccess = allowLeadingPropertyAccess
8182
let mode: 'single' | 'multi' | null = null
8283

83-
members.forEach((m, idx) => {
84+
members.forEach((m) => {
8485
const token = context.sourceCode.getTokenBefore(m.property)!
8586
const tokenBefore = context.sourceCode.getTokenBefore(token)!
8687
const currentMode: 'single' | 'multi' = token.loc.start.line === tokenBefore.loc.end.line ? 'single' : 'multi'
8788

8889
if (
89-
idx === 0
90-
&& allowFirstPropertyAccess
91-
&& (m.object.type === 'ThisExpression' || m.object.type === 'Identifier')
90+
leadingPropertyAcccess
91+
&& (m.object.type === 'ThisExpression' || m.object.type === 'Identifier' || m.object.type === 'MemberExpression' || m.object.type === 'Literal')
9292
&& currentMode === 'single'
9393
) {
9494
return
9595
}
96+
97+
leadingPropertyAcccess = false
9698
if (mode == null) {
9799
mode = currentMode
98100
return

0 commit comments

Comments
 (0)