diff --git a/crates/oxc_formatter/src/utils/member_chain/simple_argument.rs b/crates/oxc_formatter/src/utils/member_chain/simple_argument.rs index a4b72405466d0..045f47a08c8d4 100644 --- a/crates/oxc_formatter/src/utils/member_chain/simple_argument.rs +++ b/crates/oxc_formatter/src/utils/member_chain/simple_argument.rs @@ -96,6 +96,14 @@ impl<'a, 'b> SimpleArgument<'a, 'b> { Self::from(&computed_expression.expression).is_simple_impl(depth) && Self::from(&computed_expression.object).is_simple_impl(depth) } + // In Prettier's default `typescript` parser (typescript-estree) AST, + // `this.#v` is a `MemberExpression` with a `PrivateIdentifier` property. + // In oxc's AST, it's a separate type. + // The private field name is always simple, so only check the object. + // https://github.com/prettier/prettier/blob/093745f0ec429d3db47c1edd823357e0ef24e226/src/language-js/utilities/index.js#L643-L648 + Expression::PrivateFieldExpression(private_field) => { + Self::from(&private_field.object).is_simple_impl(depth) + } Expression::NewExpression(expr) => { Self::is_simple_call_like(&expr.callee, &expr.arguments, depth) } diff --git a/crates/oxc_formatter/tests/fixtures/ts/private-field-call-args.ts b/crates/oxc_formatter/tests/fixtures/ts/private-field-call-args.ts new file mode 100644 index 0000000000000..3a194f651adad --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/private-field-call-args.ts @@ -0,0 +1,15 @@ +// Private field access should not force multi-line call arguments +class Foo { + #t: NodeJS.Timeout | undefined = undefined; + #v: number; + + constructor(v: number) { + this.#v = v; + } + + start() { + this.#t = setInterval(() => { + console.log(); + }, this.#v); + } +} diff --git a/crates/oxc_formatter/tests/fixtures/ts/private-field-call-args.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/private-field-call-args.ts.snap new file mode 100644 index 0000000000000..9022aeb185f9c --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/private-field-call-args.ts.snap @@ -0,0 +1,60 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +// Private field access should not force multi-line call arguments +class Foo { + #t: NodeJS.Timeout | undefined = undefined; + #v: number; + + constructor(v: number) { + this.#v = v; + } + + start() { + this.#t = setInterval(() => { + console.log(); + }, this.#v); + } +} + +==================== Output ==================== +------------------ +{ printWidth: 80 } +------------------ +// Private field access should not force multi-line call arguments +class Foo { + #t: NodeJS.Timeout | undefined = undefined; + #v: number; + + constructor(v: number) { + this.#v = v; + } + + start() { + this.#t = setInterval(() => { + console.log(); + }, this.#v); + } +} + +------------------- +{ printWidth: 100 } +------------------- +// Private field access should not force multi-line call arguments +class Foo { + #t: NodeJS.Timeout | undefined = undefined; + #v: number; + + constructor(v: number) { + this.#v = v; + } + + start() { + this.#t = setInterval(() => { + console.log(); + }, this.#v); + } +} + +===================== End =====================