gitKrystan / prettier-plugin-ember-template-tag

A prettier plugin for formatting Ember template tags
MIT License
22 stars 12 forks source link

Fix RangeError in files with multiple forward slashes #259

Closed MichalBryxi closed 2 months ago

MichalBryxi commented 3 months ago
MichalBryxi commented 3 months ago

Ok, this PR is ready for review. It fixed the issues. I failed with every other avenue. It adds good chunk of test coverage. And cleans up few bits.

cc: @gitKrystan

miguelcobain commented 3 months ago

Maybe it would be beneficial to use a placeholder character that is less common than -? Unless I'm completely missing the way this change works.

MichalBryxi commented 3 months ago

That's possible, with few notes:

Pushed.

gitKrystan commented 2 months ago

Thanks for looking into this. Just got back from Spring Break and it was nice to see something magically removed from my TODO list while I was gone. 🎉

I've been working to remove this preprocess hack for a while because it's been the cause of many bugs, and I don't entirely understand why it's necessary.

In the meantime, this fix seems to work.

Can you take a look at the lint failures? Then I will merge.

[lint:eslint] /home/runner/work/prettier-plugin-ember-template-tag/prettier-plugin-ember-template-tag/tests/unit-tests/preprocess.test.ts
[lint:eslint]    1:1   error  Run autofix to sort these imports!                                             simple-import-sort/imports
[lint:eslint]   54:63  error  Async arrow function has no 'await' expression                                 @typescript-eslint/require-await
[lint:eslint]   56:17  error  Use `for...of` instead of `.forEach(...)`                                          unicorn/no-array-for-each
[lint:eslint]   56:36  error  The variable `i` should be named `index`. A more descriptive name will do too  unicorn/prevent-abbreviations

Thanks also for setting up the unit tests!

MichalBryxi commented 2 months ago

Oh, sorry for not checking the linting. I guess I rely too much on "push & let CI tell me" 🙃

MichalBryxi commented 2 months ago

Pushed. The linter gods should be satisfied now @gitKrystan ...

MichalBryxi commented 2 months ago

My hunch to as of why this is necessary is that we're working with two (or maybe three) "systems":

  1. The code that is being passed around as simple string.
  2. The parsed Template that has items like r.range.start and range that need to "align well" with it's own contents and also the code from (1).
  3. The parsed "Glimmer template" that has similar properties to (2) and needs to satisfy the same set of requirements as in (2).

AND

The amount of characters (a, b, c) and multi byte characters has to match in all cases. For example the code breaks in one way if I try to simply count how many bytes are occupied and replace the contents of the template with the same amount of single byte characters. And it then fails in other way when I count the number of characters and replace the template with the same amount of single byte characters.

I suspect (speculation) that for "proper" fix we'd need to:

But IDK, that's just a feeling from my experiments.