Comments (4)
In the "What I would love" section, there's #{Expectations::ALL.node_pattern_union}
which I assume you meant to write as :expect
to correspond with the rest of the examples, right?
I think you simply have one too many ()
in your pattern...
def_node_matcher :expectation_with_return_block, <<~PATTERN
#expectation(
$(block #message_expectation? args _)
)
PATTERN
Otherwise you are asking for a node with no children and a type
that will match with expectaction
...
That should work (can't test it now but could later if it doesn't work).
Just for completion's sake, another way to write it (I prefer yours though):
def_node_matcher :expectation, <<~PATTERN
(send (send nil? :expect ...) :to $%with)
PATTERN
def_node_matcher :return_block, <<~PATTERN
(block #message_expectation? args _)
PATTERN
def on_send
if expectation(with: method(:return_block))
# ...
end
from rubocop-ast.
The
expectation_with_configured_response do |a, b|
actually only yields the capture that is declared inexpectation_with_configured_response
itself, not the one declared inexpectation
.
I don't really care about the first one, only about the one fromexpectation
, but it doesn't yield it.
Yes, that's a limitation where patterns must know, at compile time, how many captures there are and where they come from. This is necessary for example so that the {}
has the same number of captures no matter which branch was matched. So it is not possible to return captures from two node patterns at once. If you tried $#expectation
, this would not capture the result of expectation
, just the argument it was given.
That being said, we could consider a way to indicate that we want to capture the result of a method call, not it's argument, say #$expectation
; that might be quite useful...
In this particular case, since the capture you really want is the expectation
node pattern, then you need to call that directly. I think the other form I gave should work, and you can even capture both nodes:
def_node_matcher :expectation, <<~PATTERN
(send
$(send nil? :expect ...) :to $% # expect(foo).to be_bop
)
PATTERN
def_node_matcher :configured_response, <<~PATTERN
(send #message_expectation? ... _)
PATTERN
def expectation_with_configured_response(node, &block)
expectation(method(:configured_response), &block)
end
I'm impressed by how the node pattern evolved for the last year, child matching, descendant matching, now this.
Thanks :-) I'm working on an actual lexer/parser/compiler for NodePattern
that would allow a bit more flexibility and make it easier to debug
from rubocop-ast.
I think you simply have one too many () in your pattern...
You're right, thanks. I've never seen a node pattern without the outer braces of either kind, that confused me.
It kind of works, but the problem is different now (this code is slightly simplified):
def_node_matcher :expectation, <<~PATTERN
(send
$(send nil? :expect ...) :to % # expect(foo).to be_bop
)
PATTERN
def_node_matcher :expectation_with_configured_response, <<~PATTERN
#expectation(
$(send #message_expectation? ... _)
)
PATTERN
The expectation_with_configured_response do |a, b|
actually only yields the capture that is declared in expectation_with_configured_response
itself, not the one declared in expectation
.
I don't really care about the first one, only about the one from expectation
, but it doesn't yield it.
This is the commit that makes the change rubocop/rubocop-rspec@f7d813c
I've added captures to expectation_with_configured_response
/expectation_with_return_block
/expectation_with_blockpass_or_hash
not because I need them, but to show that they are yielded, but not the one from expectation
.
Again, if this is by design, I don't mind closing this.
Also, I'm impressed by how the node pattern evolved for the last year, child matching, descendant matching, now this. Thanks for moving it forward!
from rubocop-ast.
Thanks for the explanation, it all makes sense. Looking forward to the updates.
I'll close this for now, and will be looking at RuboCop core's cop improvements and will try to reflect those on RSpec-related cops.
from rubocop-ast.
Related Issues (20)
- rubocop-ast-0.4.0 has broken Layout/LineLength HOT 4
- Fix `traversal`
- Changelog link is a 404 HOT 3
- CI: check JRuby and other platforms
- Broken links in "Using Node Matcher Macros" documentation HOT 1
- Request: allow method calls on constants HOT 5
- NodePattern for numblock "count" element HOT 6
- `global_const?` and some other matchers should be available to cops
- Documentation for named `_` and backreferences in node pattern HOT 3
- Undefined method emit_forward_arg= in AST::Builder:Class HOT 3
- `ProcessedSource#ast_with_comments` doesn't differentiate between identical nodes HOT 4
- Issues with `parent_module_name`
- Missing files when attempting to do a gem build HOT 3
- `send_node.block_node` does not handle `numblock`s
- Long-shot parser -> lib-ruby-parser transition for performance boost? HOT 1
- Basic documentation of Node Pattern HOT 1
- Why is the live debugger not online anymore ? https://nodepattern.herokuapp.com/ HOT 4
- Do we need an MasgnNode ? HOT 2
- Experimental Prism feature listed as runtime dependency HOT 11
- Changelog for v1.31.2 is missing HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rubocop-ast.