Closed NullVoxPopuli closed 8 months ago
This will be the hack / line that triggers it: https://github.com/gitKrystan/prettier-plugin-ember-template-tag/blob/main/src/parse/preprocess.ts#L83
Just got bitten by this. I cut down Preston's example to something that is really small (for testing), can't be (according to my experiements) really trimmed down and still reproduces given issue:
<template>
Testing line, incorrectly indented.
{{#if true}}
{{#if true}}
<link href="/////styles/" />
{{else}}
<link href="/////styles/" />
{{/if}}
{{/if}}
</template>
This throws an error:
["ERROR" - 17:59:31] Error formatting document.
["ERROR" - 17:59:31] Invalid count value: -1
RangeError: Invalid count value: -1
at String.repeat (<anonymous>)
I tried to see if there is a set of unit tests that I could use to isolate the reason for this issue, but did not find one. I'm happy to write test coverage for these from scratch, but I can't figure out how to manually construct the arguments for the preprocessTemplateRange
function? IDK how can one build template: Template,
?
Ah found it. I can export and then use:
import { preprocess } from '../../src/parse/index.js';
Other Devs at my company were reporting this, it was working fine for me. After rm -rf node_modules
and then npm i
I now get the issue if that adds any context.
The issue will be probably around this line where we add escaping for forward slashes. That explains why my test case above needed that incredible amount of forward slashes to fail properly (<link href="/////styles/" />
).
\\/
because single forward slash would do something wrong in the output format?spaces
calculation?<template>hi</template>
will be translated as: {/*hi */}
. I'd assume it is to preserve the overall length of the file and not shuffle things around? Sadly if I'm right with the /
=> \\/
translation, this won't be reliably possible as the resulting code can be longer than the original, so it might move things around.Thanks for your efforts. Your fix seems to indicate the issue was in the /
in the links. The code that is throwing this error for me has no links. But does have nested {{#if}}
and an {{unless}}
, is it possible the /
in the closing {{/if}}
as also causing the issue?
Removing ifs one at a time what seems to trigger it for me is (in my real code, haven't created a test app)
{{#if}}
{{#if}}
{{/if}}
{{#if}}
{{/if}}
{{/if}}
Removing one of the two nested nested ifs seems to get by
Ah yes, any forward slash will count. So even those in closing ifs.
Therefore the minimal reproduction is: <template>////////////////</template>
:)
The ' '.repeat(spaces)
there seems to be there to make sure that:
"<template>${foo1}</template>".length === "{/*${foo2}${magic_amount_of_spaces}*/}".length
$foo1 != $foo2
.$foo1
expansion of /
into \\/
in $foo2
it can happen that $foo1.length < $foo2.length
.$magic_amount_of_spaces
to make the first code snippet work is not going to work.I'm going to try to alter my PR to remove this functionality altogether. And try to run the plugin locally to see what will happen. Hope it's not a show-stopper.
Ok there seems to be a pickle.
I can't remove the / => \\/
functionality, because following template:
<template>/* comment */</template>
will cause following error: SyntaxError: Unterminated regular expression.
.
Also if I try to remove the ' '.repeat(spaces)
then the convertAst
function will explode as things won't be where they are expected to be. So not sure if there is.a good, simple solution?
I did a quick experiement to see what will happen if instead of / => \\/
we just replace the forward slash with random non-offending character. i.e: / => Q
and so far my test environment does not seem to crash and also formats correctly all the edge cases I put into tests. If we just want to "hold" the space of the template contents, then we could just fill whole with spaces and call it a day? I mean there might be other traps somewhere down the road, so why risk it?
PR https://github.com/gitKrystan/prettier-plugin-ember-template-tag/pull/259 IMO fixes this issue. There is only one drawback in replacing /
with arbitrary non-slash-forward-character. But to my knowledge this causes least amount of code churn (and possible bugs). Not the prettiest, but works.
The fix for this now lives in https://github.com/gitKrystan/prettier-plugin-ember-template-tag/releases/tag/v2.0.2. Thanks again to @MichalBryxi for the PR! β€οΈ
thanks !
π Describe the Bug
Run on this file:
π Actual Behavior
π€ Expected Behavior
no error
π Environment
β Additional Context
Add any other context about the problem here.