Skip to content

Commit

Permalink
Get it running with interpolated hash keys in pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Sep 27, 2023
1 parent 24ad75b commit 0e25cee
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AllCops:
NewCops: enable
SuggestExtensions: false
TargetRubyVersion: 89_65_82_80.33
TargetRubyVersion: 80_82_73_83_77.33
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ Parser::Prism.parse_file("path/to/file.rb")

To run RuboCop using the `parser-prism` gem as the parser, you will need to require the `parser/prism/rubocop` file. This file injects `prism` into the known options for both `rubocop` and `rubocop-ast`, such that you can specify it in your `.rubocop.yml` file. Unfortunately `rubocop` doesn't support any direct way to do this, so we have to get a bit hacky.

First, set the `TargetRubyVersion` in your RuboCop configuration file to `89_65_82_80.33`. This is the version of Ruby that `prism` reports itself as. (The leading numbers are the ASCII values for `PRISM`.)
First, set the `TargetRubyVersion` in your RuboCop configuration file to `80_82_73_83_77.33`. This is the version of Ruby that `prism` reports itself as. (The leading numbers are the ASCII values for `PRISM`.)

```yaml
AllCops:
TargetRubyVersion: 89_65_82_80.33
TargetRubyVersion: 80_82_73_83_77.33
```
Now when you run `rubocop` you will need to require the `parser/prism/rubocop` file before executing so that it can inject the `prism` parser into the known options.
Expand Down
3 changes: 3 additions & 0 deletions lib/parser/prism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def default_encoding
Encoding::UTF_8
end

def yyerror
end

##
# Parses a source buffer and returns the AST.
#
Expand Down
12 changes: 8 additions & 4 deletions lib/parser/prism/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ def visit_assoc_node(node)
if node.value.is_a?(::Prism::ImplicitNode)
builder.pair_label([node.key.slice.chomp(":"), srange(node.key.location)])
elsif context_pattern && node.value.nil?
builder.match_hash_var([node.key.unescaped, srange(node.key.location)])
if node.key.is_a?(::Prism::SymbolNode)
builder.match_hash_var([node.key.unescaped, srange(node.key.location)])
else
builder.match_hash_var_from_str(token(node.key.opening_loc), visit_all(node.key.parts), token(node.key.closing_loc))
end
elsif node.operator_loc
builder.pair(visit(node.key), token(node.operator_loc), visit(node.value))
elsif node.key.is_a?(::Prism::SymbolNode) && node.key.opening_loc.nil?
Expand All @@ -78,7 +82,7 @@ def visit_assoc_node(node)
if node.key.is_a?(::Prism::SymbolNode)
[builder.string_internal([node.key.unescaped, srange(node.key.value_loc)])]
else
visit_all(node,key.parts)
visit_all(node.key.parts)
end

builder.pair_quoted(token(node.key.opening_loc), parts, token(node.key.closing_loc), visit(node.value))
Expand Down Expand Up @@ -757,7 +761,7 @@ def visit_in_node(node)
token(node.in_loc),
pattern,
guard,
srange_find(node.pattern.location.end_offset, node.statements&.location&.start_offset || node.location.end_offset, [";"]),
srange_find(node.pattern.location.end_offset, node.statements&.location&.start_offset || node.location.end_offset, [";", "then"]),
visit(node.statements)
)
end
Expand Down Expand Up @@ -1318,7 +1322,7 @@ def visit_splat_node(node)
elsif context_destructure
builder.restarg(token(node.operator_loc), token(node.expression&.location))
elsif context_pattern
builder.match_rest(token(node.operator_loc), visit(node.expression))
builder.match_rest(token(node.operator_loc), token(node.expression&.location))
else
builder.splat(token(node.operator_loc), visit(node.expression))
end
Expand Down
2 changes: 1 addition & 1 deletion lib/parser/prism/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module Parser
class Prism < Base
VERSION_3_3 = 89_65_82_80.33
VERSION_3_3 = 80_82_73_83_77.33
end
end

Expand Down

0 comments on commit 0e25cee

Please sign in to comment.