HubSpot/prettier-plugin-hubl

Array index calls, e.g. "arr[0]" gets rewritten to "arr."

fabianrichter opened this issue · 2 comments

Description

I was trying the code formatting plugin today and played around with it. I recognized a breaking rewrite:
When calling an index of an array, e.g. array[0] it gets rewritten to array..

Expected behavior

It should stay the same.

To Reproduce

Original Source Code:

{% set arr = ["foo", "bar"] %}
{{ arr[1] }}

Formatted Source Code:

{% set arr = [ "foo", "bar"] %}
{{ arr. }}

Checklist

  • I have checked the known issues to make sure this isn’t already a known issue.

I've also been experiencing this bug, but it seems to be somewhat inconsistent. I believe it's related to a bug I just filed, though: #70

I think the formatter is interpreting the array index as an object bracket notation key, and trying to reformat to dot notation.

I'm not able to reproduce this bug, and I can't really see how it would be happening (since there is a check on the type of the value before the rewrite that caused #70), but in any case, there is a fix for #70 in PR #77 that should add some additional safety for this.

Do let us know if you have a consistent method to reproduce this issue.