-
Notifications
You must be signed in to change notification settings - Fork 667
feat(rome_rowan): implement DoubleEndedIterator
for node lists
#2672
Conversation
Deploying with Cloudflare Pages
|
!bench_formatter |
Formatter Benchmark Results
|
!bench_formatter |
Formatter Benchmark Results
|
Not sure we are implementing the fn main() {
let list = vec![1,2,3,4];
let mut iter = list.iter();
println!("{:?}", iter.next());
println!("{:?}", iter.next_back());
println!("{:?}", iter.next());
println!("{:?}", iter.next_back());
println!("{:?}", iter.next());
println!("{:?}", iter.next_back());
} outputs
This happens because:
I tried the following test and failed: #[test]
fn double_iterator_meet_at_middle() {
let list = build_list(vec![
(Some(1), Some(",")),
(Some(2), Some(",")),
(Some(3), Some(",")),
(Some(4), None),
]);
let mut iter = list.elements();
let element = iter.next().unwrap();
assert_eq!(element.node().unwrap().text(), "1");
let element = iter.next_back().unwrap();
assert_eq!(element.node().unwrap().text(), "4");
let element = iter.next().unwrap();
assert_eq!(element.node().unwrap().text(), "2");
let element = iter.next_back().unwrap();
assert_eq!(element.node().unwrap().text(), "3");
assert!(iter.next().is_none()); // line 701
assert!(iter.next_back().is_none());
} and failed with:
|
Damn I missed that part! Thank you for looking into this! |
Parser conformance results on ubuntu-latestjs/262
jsx/babel
symbols/microsoft
ts/babel
ts/microsoft
|
Please re-read the description, there's more context now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good. I'm a bit surprised by the amount of special case handling that is necessary and are wondering if we can simplify that a bit. But you will know better because you looked into all edge cases
3c17eb7
to
2339019
Compare
78ab67c
to
a9d1923
Compare
@MichaReiser I addressed your concerns, your help was really helpful, thank you! Now the code should be clear and better to read. I believe we are ready to ship it! |
a9d1923
to
4120e45
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
Summary
This PR adds the ability to consume an
AstSeparatedList
from the back by implementingDoubleEndedIterator
.The reason why I implemented this is because there have been cases where I needed to retrieve the last elements of a list. This will be useful, for example, inside the formatter when implementing the formatting of call arguments.
Checking a separated list from the back adds a bit more of complexity because the last separator is not mandatory, which means that we should handle cases where this is not an error.
In order to achieve that, I had to make the slots peekable, because sometimes we don't want to consume two slots. An example is when we have
[1, separator, 3, separator, 5]
. We want to consume only5
in the first iteration, but then we want to consumeseparator
and3
.I added the
unreacheable
macro. If we hit that macro, it means that are edge cases we have to handle.Test Plan
Created new tests to cover the cases:
None
is returnedassert_rev_elements
, we callrev
on all vectors (rev
needsnext_back
), so we should cover all the edge cases that were previously handled.