sway icon indicating copy to clipboard operation
sway copied to clipboard

Parts of the formatter use destructive methods

Open eureka-cpu opened this issue 2 years ago • 3 comments

We should avoid using .pop() and the like, and find work arounds for the current use cases that implement it. This will help us avoid creating unparsable code resulting in vague errors.

eureka-cpu avatar Nov 30 '22 06:11 eureka-cpu

Good point! What would be a typical workaround for lists of arguments with a separator where the separator should not appear at the end, i.e. foo, bar, baz? I think that's a common use case for .pop() in the formatter: we add the separator at each iteration and then .pop() the last one after the loop.

anton-trunov avatar Nov 30 '22 06:11 anton-trunov

Good point! What would be a typical workaround for lists of arguments with a separator where the separator should not appear at the end, i.e. foo, bar, baz? I think that's a common use case for .pop() in the formatter: we add the separator at each iteration and then .pop() the last one after the loop.

In an instance where do want to get rid of something like this, I think we should at least perform some kind of check before committing to it. In that specific instance we can use a peekable iterator and check for the last remaining item in a list.

eureka-cpu avatar Nov 30 '22 07:11 eureka-cpu

It's tricky, the AST gives us sometimes an optional final value, and other times the final value is in the primary list of items.

eureka-cpu avatar Nov 30 '22 07:11 eureka-cpu