Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AST::InPatternNode node #183

Merged
merged 1 commit into from May 26, 2021
Merged

Conversation

koic
Copy link
Member

@koic koic commented May 10, 2021

This PR adds AST::InPatternNode node.
It aims to make access to in pattern node of the pattern matching syntax convenient and readable.

For example, pattern matching syntax AST can use in_pattern.body and prevents the following error:

NoMethodError:
  undefined method `body' for s(:in_pattern,
    s(:array_pattern,
      s(:match_var, :a)), nil,
    s(:int, 1)):RuboCop::AST::Node

This PR adds `AST::InPatternNode` node.
It aims to make access to `in` pattern node of the pattern matching syntax
convenient and readable.

For example, pattern matching syntax AST can use `in_pattern.body` and
prevents the following error:

```console
NoMethodError:
  undefined method `body' for s(:in_pattern,
    s(:array_pattern,
      s(:match_var, :a)), nil,
    s(:int, 1)):RuboCop::AST::Node
```
@koic koic force-pushed the add_ast_in_pattern_node branch from c93f20a to 05b3f40 Compare May 10, 2021 02:29
@koic
Copy link
Member Author

koic commented May 26, 2021

@marcandre @bbatsov I'd like to use this PR's feature in rubocop/rubocop#9825. Can you review, merge, and release?

@bbatsov
Copy link
Contributor

bbatsov commented May 26, 2021

The change looks good to me. I'll do a release now.

@bbatsov bbatsov merged commit 1e853be into rubocop:master May 26, 2021
@bbatsov
Copy link
Contributor

bbatsov commented May 26, 2021

Done.

@koic koic deleted the add_ast_in_pattern_node branch May 26, 2021 06:05
@koic
Copy link
Member Author

koic commented May 26, 2021

@bbatsov Thank you!

koic added a commit to koic/rubocop that referenced this pull request May 26, 2021
This PR adds new `Lint/EmptyInPattern` cop for Ruby 2.7's pattern matching.
It checks for the presence of `in` branches without a body.

```ruby
# bad
case condition
in [a]
  do_something
in [a, b]
end

# good
case condition
in [a]
  do_something
in [a, b]
  nil
end

# good - AllowComments: true (default)
case condition
in [a]
  do_something
in [a, b]
  # noop
end

# bad - AllowComments: false
case condition
in [a]
  do_something
in [a, b]
  # noop
end
```

This cop is similar to `Lint/EmptyWhen`, but with different supported
syntax and Ruby version (requires 2.7 or higher).

And this PR use rubocop/rubocop-ast#183 feature,
so it requires RuboCop AST 1.6.0 or higher.
@marcandre
Copy link
Contributor

Oh, this fell through the cracks, sorry 😅

Don't hesitate to ping me after 1 or 2 days

bbatsov pushed a commit to rubocop/rubocop that referenced this pull request May 27, 2021
This PR adds new `Lint/EmptyInPattern` cop for Ruby 2.7's pattern matching.
It checks for the presence of `in` branches without a body.

```ruby
# bad
case condition
in [a]
  do_something
in [a, b]
end

# good
case condition
in [a]
  do_something
in [a, b]
  nil
end

# good - AllowComments: true (default)
case condition
in [a]
  do_something
in [a, b]
  # noop
end

# bad - AllowComments: false
case condition
in [a]
  do_something
in [a, b]
  # noop
end
```

This cop is similar to `Lint/EmptyWhen`, but with different supported
syntax and Ruby version (requires 2.7 or higher).

And this PR use rubocop/rubocop-ast#183 feature,
so it requires RuboCop AST 1.6.0 or higher.
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.

None yet

3 participants