Skip to content

Avoid unnecessary multiline parentheses - #42

Open
Morriar wants to merge 1 commit into
mainfrom
fix/prefer-not-nil-multiline-parentheses
Open

Avoid unnecessary multiline parentheses#42
Morriar wants to merge 1 commit into
mainfrom
fix/prefer-not-nil-multiline-parentheses

Conversation

@Morriar

@Morriar Morriar commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Avoid unnecessary parentheses when autocorrecting multiline T.must calls without comments.

Before:

variant = (
  InventoryItemVariant.preload(:variant).where(inventory_item_id: @inventory_item_id).first!.variant
).not_nil!

After:

variant = InventoryItemVariant.preload(:variant).where(inventory_item_id: @inventory_item_id).first!.variant.not_nil!

Calls containing comments keep the grouped form so comments and internal indentation are preserved.

def correction_for(node, argument, replacement)
return replacement unless node.multiline? && node.parenthesized_call?
return replacement if argument.first_line == node.loc.begin.line && argument.last_line == node.loc.end.line
return replacement unless comments_inside_parentheses?(node)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this will fail the following case:

value = (
  <<~TEXT
    hello
  TEXT
).not_nil!

by converting it to value = <<~TEXT.not_nil!

argument.source is just the <<~TEXT portion of the heredoc so returning the replacement in this case truncates the rest of the heredoc. Probably worth adding a test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I added a special case for heredoc. I decided to preserve the parenthesis around it to avoid messing with indentation.

So this:

foo = T.must(
  <<~FOO
    hello
  FOO
)

becomes this:

foo = (
  <<~FOO
    hello
  FOO
).not_nil!

instead of this:

foo = <<~FOO.not_nil!
    hello
  FOO

Base automatically changed from fix/prefer-not-nil-block-parentheses to fix/prefer-not-nil-multiline-receiver September 9, 2026 17:50
@Morriar
Morriar force-pushed the fix/prefer-not-nil-multiline-receiver branch from 4a89554 to 55210f9 Compare September 9, 2026 17:59
Base automatically changed from fix/prefer-not-nil-multiline-receiver to main September 9, 2026 18:00
@Morriar
Morriar force-pushed the fix/prefer-not-nil-multiline-parentheses branch from 94e64e5 to bc05c0b Compare September 9, 2026 18:10
@Morriar
Morriar force-pushed the fix/prefer-not-nil-multiline-parentheses branch from 552e922 to 4a85d32 Compare September 9, 2026 18:49
@Morriar
Morriar requested a review from egiurleo September 9, 2026 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants