@@ -5,7 +5,7 @@ export const RULE_NAME = 'consistent-chaining'
55export type MessageIds = 'shouldWrap' | 'shouldNotWrap'
66export 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